1 | // Aseprite |
2 | // Copyright (C) 2019 Igara Studio S.A. |
3 | // |
4 | // This program is distributed under the terms of |
5 | // the End-User License Agreement for Aseprite. |
6 | |
7 | #ifdef HAVE_CONFIG_H |
8 | #include "config.h" |
9 | #endif |
10 | |
11 | #include "app/script/luacpp.h" |
12 | #include "base/version.h" |
13 | |
14 | namespace app { |
15 | namespace script { |
16 | |
17 | namespace { |
18 | |
19 | base::Version Version_new(lua_State* L, int index) |
20 | { |
21 | base::Version ver; |
22 | if (auto ver2 = may_get_obj<base::Version>(L, index)) { |
23 | ver = *ver2; |
24 | } |
25 | else if (const char* verStr = lua_tostring(L, index)) { |
26 | ver = base::Version(verStr); |
27 | } |
28 | return ver; |
29 | } |
30 | |
31 | int Version_new(lua_State* L) |
32 | { |
33 | push_obj(L, Version_new(L, 1)); |
34 | return 1; |
35 | } |
36 | |
37 | int Version_gc(lua_State* L) |
38 | { |
39 | get_obj<base::Version>(L, 1)->~Version(); |
40 | return 0; |
41 | } |
42 | |
43 | int Version_eq(lua_State* L) |
44 | { |
45 | const auto a = get_obj<base::Version>(L, 1); |
46 | const auto b = get_obj<base::Version>(L, 2); |
47 | lua_pushboolean(L, *a == *b); |
48 | return 1; |
49 | } |
50 | |
51 | int Version_lt(lua_State* L) |
52 | { |
53 | const auto a = get_obj<base::Version>(L, 1); |
54 | const auto b = get_obj<base::Version>(L, 2); |
55 | lua_pushboolean(L, *a < *b); |
56 | return 1; |
57 | } |
58 | |
59 | int Version_le(lua_State* L) |
60 | { |
61 | const auto a = get_obj<base::Version>(L, 1); |
62 | const auto b = get_obj<base::Version>(L, 2); |
63 | lua_pushboolean(L, (*a < *b) || (*a == *b)); |
64 | return 1; |
65 | } |
66 | |
67 | int Version_tostring(lua_State* L) |
68 | { |
69 | const auto ver = get_obj<base::Version>(L, 1); |
70 | lua_pushstring(L, ver->str().c_str()); |
71 | return 1; |
72 | } |
73 | |
74 | int Version_get_major(lua_State* L) |
75 | { |
76 | const auto ver = get_obj<base::Version>(L, 1); |
77 | const auto& numbers = ver->numbers(); |
78 | lua_pushinteger(L, numbers.size() > 0 ? numbers[0]: 0); |
79 | return 1; |
80 | } |
81 | |
82 | int Version_get_minor(lua_State* L) |
83 | { |
84 | const auto ver = get_obj<base::Version>(L, 1); |
85 | const auto& numbers = ver->numbers(); |
86 | lua_pushinteger(L, numbers.size() > 1 ? numbers[1]: 0); |
87 | return 1; |
88 | } |
89 | |
90 | int Version_get_patch(lua_State* L) |
91 | { |
92 | const auto ver = get_obj<base::Version>(L, 1); |
93 | const auto& numbers = ver->numbers(); |
94 | lua_pushinteger(L, numbers.size() > 2 ? numbers[2]: 0); |
95 | return 1; |
96 | } |
97 | |
98 | int Version_get_prereleaseLabel(lua_State* L) |
99 | { |
100 | const auto ver = get_obj<base::Version>(L, 1); |
101 | lua_pushstring(L, ver->prereleaseLabel().c_str()); |
102 | return 1; |
103 | } |
104 | |
105 | int Version_get_prereleaseNumber(lua_State* L) |
106 | { |
107 | const auto ver = get_obj<base::Version>(L, 1); |
108 | lua_pushinteger(L, ver->prereleaseNumber()); |
109 | return 1; |
110 | } |
111 | |
112 | const luaL_Reg Version_methods[] = { |
113 | { "__gc" , Version_gc }, |
114 | { "__eq" , Version_eq }, |
115 | { "__lt" , Version_lt }, |
116 | { "__le" , Version_le }, |
117 | { "__tostring" , Version_tostring }, |
118 | { nullptr, nullptr } |
119 | }; |
120 | |
121 | const Property Version_properties[] = { |
122 | { "major" , Version_get_major, nullptr }, |
123 | { "minor" , Version_get_minor, nullptr }, |
124 | { "patch" , Version_get_patch, nullptr }, |
125 | { "prereleaseLabel" , Version_get_prereleaseLabel, nullptr }, |
126 | { "prereleaseNumber" , Version_get_prereleaseNumber, nullptr }, |
127 | { nullptr, nullptr, nullptr } |
128 | }; |
129 | |
130 | } // anonymous namespace |
131 | |
132 | DEF_MTNAME(base::Version); |
133 | |
134 | void register_version_class(lua_State* L) |
135 | { |
136 | using base::Version; |
137 | REG_CLASS(L, Version); |
138 | REG_CLASS_NEW(L, Version); |
139 | REG_CLASS_PROPERTIES(L, Version); |
140 | } |
141 | |
142 | void push_version(lua_State* L, const base::Version& ver) |
143 | { |
144 | push_obj(L, ver); |
145 | } |
146 | |
147 | } // namespace script |
148 | } // namespace app |
149 | |