dub 0.2.2 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dub might be problematic. Click here for more details.

Files changed (267) hide show
  1. data/.gitignore +8 -0
  2. data/History.txt +53 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +67 -0
  5. data/Rakefile +59 -0
  6. data/dub.gemspec +201 -0
  7. data/lib/dub/argument.rb +275 -0
  8. data/lib/dub/entities_unescape.rb +9 -0
  9. data/lib/dub/function.rb +163 -0
  10. data/lib/dub/function_group.rb +72 -0
  11. data/lib/dub/generator.rb +15 -0
  12. data/lib/dub/group.rb +24 -0
  13. data/lib/dub/klass.rb +255 -0
  14. data/lib/dub/lua/class.cpp.erb +84 -0
  15. data/lib/dub/lua/class_gen.rb +97 -0
  16. data/lib/dub/lua/function.cpp.erb +18 -0
  17. data/lib/dub/lua/function_gen.rb +265 -0
  18. data/lib/dub/lua/group.cpp.erb +9 -0
  19. data/lib/dub/lua/lua_cpp_helper.h +141 -0
  20. data/lib/dub/lua/namespace.cpp.erb +40 -0
  21. data/lib/dub/lua/namespace_gen.rb +88 -0
  22. data/lib/dub/lua.rb +24 -0
  23. data/lib/dub/member_extraction.rb +92 -0
  24. data/lib/dub/namespace.rb +277 -0
  25. data/lib/dub/opts_parser.rb +30 -0
  26. data/lib/dub/parser.rb +46 -0
  27. data/lib/dub/templates/lua_template.erb +21 -0
  28. data/lib/dub/version.rb +3 -0
  29. data/lib/dub.rb +24 -20
  30. data/test/argument_test.rb +547 -0
  31. data/test/fixtures/app/CMakeLists.txt +54 -0
  32. data/test/fixtures/app/Doxyfile +1600 -0
  33. data/test/fixtures/app/bindings/all_lua.cpp +299 -0
  34. data/test/fixtures/app/include/matrix.h +162 -0
  35. data/test/fixtures/app/make_lua_bindings.rb +13 -0
  36. data/test/fixtures/app/vendor/lua/CMakeLists.txt +25 -0
  37. data/test/fixtures/app/vendor/lua/COPYRIGHT +34 -0
  38. data/test/fixtures/app/vendor/lua/HISTORY +183 -0
  39. data/test/fixtures/app/vendor/lua/INSTALL +99 -0
  40. data/test/fixtures/app/vendor/lua/Makefile +183 -0
  41. data/test/fixtures/app/vendor/lua/README +37 -0
  42. data/test/fixtures/app/vendor/lua/lapi.c +1080 -0
  43. data/test/fixtures/app/vendor/lua/lapi.h +16 -0
  44. data/test/fixtures/app/vendor/lua/lauxlib.c +653 -0
  45. data/test/fixtures/app/vendor/lua/lauxlib.h +174 -0
  46. data/test/fixtures/app/vendor/lua/lbaselib.c +643 -0
  47. data/test/fixtures/app/vendor/lua/lcode.c +839 -0
  48. data/test/fixtures/app/vendor/lua/lcode.h +76 -0
  49. data/test/fixtures/app/vendor/lua/ldblib.c +397 -0
  50. data/test/fixtures/app/vendor/lua/ldebug.c +622 -0
  51. data/test/fixtures/app/vendor/lua/ldebug.h +33 -0
  52. data/test/fixtures/app/vendor/lua/ldo.c +516 -0
  53. data/test/fixtures/app/vendor/lua/ldo.h +57 -0
  54. data/test/fixtures/app/vendor/lua/ldump.c +164 -0
  55. data/test/fixtures/app/vendor/lua/lfunc.c +174 -0
  56. data/test/fixtures/app/vendor/lua/lfunc.h +34 -0
  57. data/test/fixtures/app/vendor/lua/lgc.c +711 -0
  58. data/test/fixtures/app/vendor/lua/lgc.h +110 -0
  59. data/test/fixtures/app/vendor/lua/liblua.a +0 -0
  60. data/test/fixtures/app/vendor/lua/linit.c +38 -0
  61. data/test/fixtures/app/vendor/lua/liolib.c +532 -0
  62. data/test/fixtures/app/vendor/lua/llex.c +461 -0
  63. data/test/fixtures/app/vendor/lua/llex.h +81 -0
  64. data/test/fixtures/app/vendor/lua/llimits.h +128 -0
  65. data/test/fixtures/app/vendor/lua/lmathlib.c +263 -0
  66. data/test/fixtures/app/vendor/lua/lmem.c +86 -0
  67. data/test/fixtures/app/vendor/lua/lmem.h +49 -0
  68. data/test/fixtures/app/vendor/lua/loadlib.c +664 -0
  69. data/test/fixtures/app/vendor/lua/lobject.c +214 -0
  70. data/test/fixtures/app/vendor/lua/lobject.h +381 -0
  71. data/test/fixtures/app/vendor/lua/lopcodes.c +102 -0
  72. data/test/fixtures/app/vendor/lua/lopcodes.h +268 -0
  73. data/test/fixtures/app/vendor/lua/loslib.c +244 -0
  74. data/test/fixtures/app/vendor/lua/lparser.c +1337 -0
  75. data/test/fixtures/app/vendor/lua/lparser.h +82 -0
  76. data/test/fixtures/app/vendor/lua/lstate.c +214 -0
  77. data/test/fixtures/app/vendor/lua/lstate.h +168 -0
  78. data/test/fixtures/app/vendor/lua/lstring.c +111 -0
  79. data/test/fixtures/app/vendor/lua/lstring.h +31 -0
  80. data/test/fixtures/app/vendor/lua/lstrlib.c +868 -0
  81. data/test/fixtures/app/vendor/lua/ltable.c +588 -0
  82. data/test/fixtures/app/vendor/lua/ltable.h +40 -0
  83. data/test/fixtures/app/vendor/lua/ltablib.c +278 -0
  84. data/test/fixtures/app/vendor/lua/ltm.c +75 -0
  85. data/test/fixtures/app/vendor/lua/ltm.h +54 -0
  86. data/test/fixtures/app/vendor/lua/lua.c +695 -0
  87. data/test/fixtures/app/vendor/lua/lua.h +385 -0
  88. data/test/fixtures/app/vendor/lua/lua_dub_helper.h +77 -0
  89. data/test/fixtures/app/vendor/lua/luac +0 -0
  90. data/test/fixtures/app/vendor/lua/luac.c +200 -0
  91. data/test/fixtures/app/vendor/lua/luaconf.h +762 -0
  92. data/test/fixtures/app/vendor/lua/lualib.h +53 -0
  93. data/test/fixtures/app/vendor/lua/lundump.c +223 -0
  94. data/test/fixtures/app/vendor/lua/lundump.h +36 -0
  95. data/test/fixtures/app/vendor/lua/lvm.c +765 -0
  96. data/test/fixtures/app/vendor/lua/lvm.h +36 -0
  97. data/test/fixtures/app/vendor/lua/lzio.c +82 -0
  98. data/test/fixtures/app/vendor/lua/lzio.h +67 -0
  99. data/test/fixtures/app/vendor/lua/matrix.h +102 -0
  100. data/test/fixtures/app/vendor/lua/print.c +227 -0
  101. data/test/fixtures/app/vendor/lua/test/README +26 -0
  102. data/test/fixtures/app/vendor/lua/test/bisect.lua +27 -0
  103. data/test/fixtures/app/vendor/lua/test/cf.lua +16 -0
  104. data/test/fixtures/app/vendor/lua/test/echo.lua +5 -0
  105. data/test/fixtures/app/vendor/lua/test/env.lua +7 -0
  106. data/test/fixtures/app/vendor/lua/test/factorial.lua +32 -0
  107. data/test/fixtures/app/vendor/lua/test/fib.lua +40 -0
  108. data/test/fixtures/app/vendor/lua/test/fibfor.lua +13 -0
  109. data/test/fixtures/app/vendor/lua/test/globals.lua +13 -0
  110. data/test/fixtures/app/vendor/lua/test/hello.lua +3 -0
  111. data/test/fixtures/app/vendor/lua/test/life.lua +111 -0
  112. data/test/fixtures/app/vendor/lua/test/luac.lua +7 -0
  113. data/test/fixtures/app/vendor/lua/test/printf.lua +7 -0
  114. data/test/fixtures/app/vendor/lua/test/readonly.lua +12 -0
  115. data/test/fixtures/app/vendor/lua/test/sieve.lua +29 -0
  116. data/test/fixtures/app/vendor/lua/test/sort.lua +66 -0
  117. data/test/fixtures/app/vendor/lua/test/table.lua +12 -0
  118. data/test/fixtures/app/vendor/lua/test/trace-calls.lua +32 -0
  119. data/test/fixtures/app/vendor/lua/test/trace-globals.lua +38 -0
  120. data/test/fixtures/app/vendor/lua/test/xd.lua +14 -0
  121. data/test/fixtures/app/xml/classdub_1_1_matrix.xml +341 -0
  122. data/test/fixtures/app/xml/classdub_1_1_t_mat.xml +252 -0
  123. data/test/fixtures/app/xml/combine.xslt +15 -0
  124. data/test/fixtures/app/xml/compound.xsd +814 -0
  125. data/test/fixtures/app/xml/dir_53661a2bdeb1d55e60581a7e15deb763.xml +12 -0
  126. data/test/fixtures/app/xml/index.xml +48 -0
  127. data/test/fixtures/app/xml/index.xsd +66 -0
  128. data/test/fixtures/app/xml/matrix_8h.xml +179 -0
  129. data/test/fixtures/app/xml/namespacedub.xml +41 -0
  130. data/test/fixtures/classcv_1_1_mat.xml +1996 -0
  131. data/test/fixtures/classcv_1_1_point__.xml +341 -0
  132. data/test/fixtures/classcv_1_1_scalar__.xml +269 -0
  133. data/test/fixtures/classcv_1_1_size__.xml +270 -0
  134. data/test/fixtures/dummy_class.cpp.erb +1 -0
  135. data/test/fixtures/dummy_function.cpp.erb +1 -0
  136. data/test/fixtures/group___magic_type.xml +406 -0
  137. data/test/fixtures/namespacecv.xml +12659 -0
  138. data/test/function_group_test.rb +24 -0
  139. data/test/function_test.rb +395 -0
  140. data/test/group_test.rb +155 -0
  141. data/test/helper.rb +34 -0
  142. data/test/klass_test.rb +422 -0
  143. data/test/lua_function_gen_test.rb +215 -0
  144. data/test/namespace_test.rb +220 -0
  145. data/test/parser_test.rb +36 -0
  146. metadata +232 -274
  147. checksums.yaml +0 -7
  148. data/lib/open_api_sdk/analytics.rb +0 -99
  149. data/lib/open_api_sdk/domains.rb +0 -353
  150. data/lib/open_api_sdk/dub.rb +0 -88
  151. data/lib/open_api_sdk/links.rb +0 -766
  152. data/lib/open_api_sdk/metatags.rb +0 -54
  153. data/lib/open_api_sdk/models/operations/bulkcreatelinks_response.rb +0 -60
  154. data/lib/open_api_sdk/models/operations/bulkupdatelinks_requestbody.rb +0 -27
  155. data/lib/open_api_sdk/models/operations/bulkupdatelinks_response.rb +0 -60
  156. data/lib/open_api_sdk/models/operations/color.rb +0 -24
  157. data/lib/open_api_sdk/models/operations/createdomain_requestbody.rb +0 -33
  158. data/lib/open_api_sdk/models/operations/createdomain_response.rb +0 -60
  159. data/lib/open_api_sdk/models/operations/createlink_requestbody.rb +0 -95
  160. data/lib/open_api_sdk/models/operations/createlink_response.rb +0 -60
  161. data/lib/open_api_sdk/models/operations/createtag_requestbody.rb +0 -32
  162. data/lib/open_api_sdk/models/operations/createtag_response.rb +0 -60
  163. data/lib/open_api_sdk/models/operations/data.rb +0 -83
  164. data/lib/open_api_sdk/models/operations/deletedomain_request.rb +0 -24
  165. data/lib/open_api_sdk/models/operations/deletedomain_response.rb +0 -60
  166. data/lib/open_api_sdk/models/operations/deletedomain_responsebody.rb +0 -24
  167. data/lib/open_api_sdk/models/operations/deletelink_request.rb +0 -24
  168. data/lib/open_api_sdk/models/operations/deletelink_response.rb +0 -60
  169. data/lib/open_api_sdk/models/operations/deletelink_responsebody.rb +0 -24
  170. data/lib/open_api_sdk/models/operations/event.rb +0 -21
  171. data/lib/open_api_sdk/models/operations/getlinkinfo_request.rb +0 -33
  172. data/lib/open_api_sdk/models/operations/getlinkinfo_response.rb +0 -60
  173. data/lib/open_api_sdk/models/operations/getlinks_request.rb +0 -51
  174. data/lib/open_api_sdk/models/operations/getlinks_response.rb +0 -60
  175. data/lib/open_api_sdk/models/operations/getlinkscount_request.rb +0 -48
  176. data/lib/open_api_sdk/models/operations/getlinkscount_response.rb +0 -60
  177. data/lib/open_api_sdk/models/operations/getmetatags_request.rb +0 -24
  178. data/lib/open_api_sdk/models/operations/getmetatags_response.rb +0 -33
  179. data/lib/open_api_sdk/models/operations/getmetatags_responsebody.rb +0 -30
  180. data/lib/open_api_sdk/models/operations/getqrcode_request.rb +0 -39
  181. data/lib/open_api_sdk/models/operations/getqrcode_response.rb +0 -60
  182. data/lib/open_api_sdk/models/operations/gettags_response.rb +0 -60
  183. data/lib/open_api_sdk/models/operations/getworkspace_request.rb +0 -24
  184. data/lib/open_api_sdk/models/operations/getworkspace_response.rb +0 -60
  185. data/lib/open_api_sdk/models/operations/groupby.rb +0 -28
  186. data/lib/open_api_sdk/models/operations/interval.rb +0 -25
  187. data/lib/open_api_sdk/models/operations/level.rb +0 -21
  188. data/lib/open_api_sdk/models/operations/listdomains_response.rb +0 -60
  189. data/lib/open_api_sdk/models/operations/paymentprocessor.rb +0 -20
  190. data/lib/open_api_sdk/models/operations/requestbody.rb +0 -95
  191. data/lib/open_api_sdk/models/operations/retrieveanalytics_request.rb +0 -81
  192. data/lib/open_api_sdk/models/operations/retrieveanalytics_response.rb +0 -60
  193. data/lib/open_api_sdk/models/operations/sort.rb +0 -20
  194. data/lib/open_api_sdk/models/operations/trackcustomer_requestbody.rb +0 -33
  195. data/lib/open_api_sdk/models/operations/trackcustomer_response.rb +0 -60
  196. data/lib/open_api_sdk/models/operations/trackcustomer_responsebody.rb +0 -33
  197. data/lib/open_api_sdk/models/operations/tracklead_requestbody.rb +0 -42
  198. data/lib/open_api_sdk/models/operations/tracklead_response.rb +0 -60
  199. data/lib/open_api_sdk/models/operations/tracklead_responsebody.rb +0 -42
  200. data/lib/open_api_sdk/models/operations/tracksale_requestbody.rb +0 -42
  201. data/lib/open_api_sdk/models/operations/tracksale_response.rb +0 -60
  202. data/lib/open_api_sdk/models/operations/tracksale_responsebody.rb +0 -42
  203. data/lib/open_api_sdk/models/operations/updatedomain_request.rb +0 -27
  204. data/lib/open_api_sdk/models/operations/updatedomain_requestbody.rb +0 -33
  205. data/lib/open_api_sdk/models/operations/updatedomain_response.rb +0 -60
  206. data/lib/open_api_sdk/models/operations/updatelink_request.rb +0 -27
  207. data/lib/open_api_sdk/models/operations/updatelink_requestbody.rb +0 -95
  208. data/lib/open_api_sdk/models/operations/updatelink_response.rb +0 -60
  209. data/lib/open_api_sdk/models/operations/updatetag_color.rb +0 -24
  210. data/lib/open_api_sdk/models/operations/updatetag_request.rb +0 -27
  211. data/lib/open_api_sdk/models/operations/updatetag_requestbody.rb +0 -32
  212. data/lib/open_api_sdk/models/operations/updatetag_response.rb +0 -60
  213. data/lib/open_api_sdk/models/operations/updateworkspace_request.rb +0 -27
  214. data/lib/open_api_sdk/models/operations/updateworkspace_requestbody.rb +0 -27
  215. data/lib/open_api_sdk/models/operations/updateworkspace_response.rb +0 -60
  216. data/lib/open_api_sdk/models/operations/upsertlink_requestbody.rb +0 -95
  217. data/lib/open_api_sdk/models/operations/upsertlink_response.rb +0 -60
  218. data/lib/open_api_sdk/models/operations.rb +0 -74
  219. data/lib/open_api_sdk/models/shared/badrequest.rb +0 -24
  220. data/lib/open_api_sdk/models/shared/code.rb +0 -18
  221. data/lib/open_api_sdk/models/shared/color.rb +0 -24
  222. data/lib/open_api_sdk/models/shared/conflict.rb +0 -24
  223. data/lib/open_api_sdk/models/shared/conflict_code.rb +0 -18
  224. data/lib/open_api_sdk/models/shared/conflict_error.rb +0 -30
  225. data/lib/open_api_sdk/models/shared/countrycode.rb +0 -267
  226. data/lib/open_api_sdk/models/shared/domains.rb +0 -27
  227. data/lib/open_api_sdk/models/shared/domainschema.rb +0 -48
  228. data/lib/open_api_sdk/models/shared/error.rb +0 -30
  229. data/lib/open_api_sdk/models/shared/forbidden.rb +0 -24
  230. data/lib/open_api_sdk/models/shared/forbidden_code.rb +0 -18
  231. data/lib/open_api_sdk/models/shared/forbidden_error.rb +0 -30
  232. data/lib/open_api_sdk/models/shared/geo.rb +0 -771
  233. data/lib/open_api_sdk/models/shared/internalservererror.rb +0 -24
  234. data/lib/open_api_sdk/models/shared/internalservererror_code.rb +0 -18
  235. data/lib/open_api_sdk/models/shared/internalservererror_error.rb +0 -30
  236. data/lib/open_api_sdk/models/shared/inviteexpired.rb +0 -24
  237. data/lib/open_api_sdk/models/shared/inviteexpired_code.rb +0 -18
  238. data/lib/open_api_sdk/models/shared/inviteexpired_error.rb +0 -30
  239. data/lib/open_api_sdk/models/shared/linkgeotargeting.rb +0 -771
  240. data/lib/open_api_sdk/models/shared/linkschema.rb +0 -142
  241. data/lib/open_api_sdk/models/shared/notfound.rb +0 -24
  242. data/lib/open_api_sdk/models/shared/notfound_code.rb +0 -18
  243. data/lib/open_api_sdk/models/shared/notfound_error.rb +0 -30
  244. data/lib/open_api_sdk/models/shared/plan.rb +0 -24
  245. data/lib/open_api_sdk/models/shared/ratelimitexceeded.rb +0 -24
  246. data/lib/open_api_sdk/models/shared/ratelimitexceeded_code.rb +0 -18
  247. data/lib/open_api_sdk/models/shared/ratelimitexceeded_error.rb +0 -30
  248. data/lib/open_api_sdk/models/shared/role.rb +0 -19
  249. data/lib/open_api_sdk/models/shared/security.rb +0 -24
  250. data/lib/open_api_sdk/models/shared/tagschema.rb +0 -30
  251. data/lib/open_api_sdk/models/shared/unauthorized.rb +0 -24
  252. data/lib/open_api_sdk/models/shared/unauthorized_code.rb +0 -18
  253. data/lib/open_api_sdk/models/shared/unauthorized_error.rb +0 -30
  254. data/lib/open_api_sdk/models/shared/unprocessableentity.rb +0 -24
  255. data/lib/open_api_sdk/models/shared/unprocessableentity_code.rb +0 -18
  256. data/lib/open_api_sdk/models/shared/unprocessableentity_error.rb +0 -30
  257. data/lib/open_api_sdk/models/shared/users.rb +0 -24
  258. data/lib/open_api_sdk/models/shared/workspaceschema.rb +0 -81
  259. data/lib/open_api_sdk/models/shared.rb +0 -49
  260. data/lib/open_api_sdk/qr_codes.rb +0 -97
  261. data/lib/open_api_sdk/sdkconfiguration.rb +0 -52
  262. data/lib/open_api_sdk/tags.rb +0 -272
  263. data/lib/open_api_sdk/track.rb +0 -276
  264. data/lib/open_api_sdk/utils/metadata_fields.rb +0 -150
  265. data/lib/open_api_sdk/utils/t.rb +0 -59
  266. data/lib/open_api_sdk/utils/utils.rb +0 -772
  267. data/lib/open_api_sdk/workspaces.rb +0 -192
@@ -0,0 +1,97 @@
1
+
2
+ require 'dub/generator'
3
+ require 'erb'
4
+
5
+ module Dub
6
+ module Lua
7
+ class ClassGen < Dub::Generator
8
+ attr_accessor :template_path
9
+
10
+ def initialize
11
+ load_erb
12
+ end
13
+
14
+ def template_path=(template_path)
15
+ @template_path = template_path
16
+ load_erb
17
+ end
18
+
19
+ def klass(klass)
20
+ @class = klass
21
+ @class_template.result(binding)
22
+ end
23
+
24
+ def function_generator
25
+ Lua.function_generator
26
+ end
27
+
28
+ def method_registration(klass = @class)
29
+ member_methods = (klass.members || []).map do |method|
30
+ next if method.static?
31
+ "{%-20s, #{method.method_name(0)}}" % method.name.inspect
32
+ end.compact
33
+
34
+ member_methods << "{%-20s, #{klass.tostring_name}}" % "__tostring".inspect
35
+ member_methods << "{%-20s, #{klass.destructor_name}}" % "__gc".inspect
36
+
37
+ member_methods.join(",\n")
38
+ end
39
+
40
+ def namespace_methods_registration(klass = @class)
41
+ global_methods = klass.names.map do |name|
42
+ "{%-20s, #{klass.constructor.method_name(0)}}" % name.inspect
43
+ end
44
+
45
+ (klass.members || []).map do |method|
46
+ next unless method.static?
47
+ global_methods << "{%-20s, #{method.method_name(0)}}" % "#{klass.name}_#{method.name}".inspect
48
+ end
49
+
50
+ global_methods.join(",\n")
51
+ end
52
+
53
+ def constants_registration(klass = @class)
54
+ klass.enums.map do |name|
55
+ "{%-20s, #{klass.full_type}::#{name}}" % name.inspect
56
+ end.join(",\n")
57
+ end
58
+
59
+ def members_list(all_members, ignore_list = [])
60
+ list = all_members.map do |member_or_group|
61
+ if ignore_list.include?(member_or_group.name)
62
+ nil
63
+ elsif member_or_group.kind_of?(Array)
64
+ members_list(member_or_group)
65
+ elsif ignore_member?(member_or_group)
66
+ nil
67
+ else
68
+ member_or_group
69
+ end
70
+ end
71
+
72
+ list.compact!
73
+ list == [] ? nil : list
74
+ end
75
+
76
+ def ignore_member?(member)
77
+ if member.name =~ /^~/ || # do not build constructor
78
+ member.name =~ /^operator/ || # no conversion operators
79
+ member.has_complex_arguments? || # no complex arguments or return values
80
+ member.has_array_arguments? ||
81
+ member.vararg? ||
82
+ member.original_signature =~ /void\s+\*/ # used to detect return value and parameters
83
+ true # ignore
84
+ elsif return_value = member.return_value
85
+ return_value.type =~ />$/ || # no complex return types
86
+ return_value.is_native? && member.return_value.is_pointer?
87
+ else
88
+ false # ok, do not ignore
89
+ end
90
+ end
91
+
92
+ def load_erb
93
+ @class_template = ::ERB.new(File.read(@template_path || File.join(File.dirname(__FILE__), 'class.cpp.erb')))
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,18 @@
1
+ <%= comment(@function) %>
2
+ <%= signature(@function) %> {
3
+ try {
4
+ <%= indent(body(@function), 4) %>
5
+ } catch (std::exception &e) {
6
+ std::string *s = new std::string("<%= @function.id_name %>: ");
7
+ s->append(e.what());
8
+ lua_pushstring(L, s->c_str());
9
+ delete s;
10
+ lua_error(L);
11
+ // never reached
12
+ return 0;
13
+ } catch (...) {
14
+ lua_pushstring(L, "<%= @function.id_name %>: Unknown exception");
15
+ lua_error(L);
16
+ return 0;
17
+ }
18
+ }
@@ -0,0 +1,265 @@
1
+ require 'dub/generator'
2
+ require 'erb'
3
+
4
+ module Dub
5
+ module Lua
6
+ class FunctionGen < Dub::Generator
7
+ attr_accessor :template_path
8
+
9
+ NUMBER_TYPES = [
10
+ 'float',
11
+ 'double',
12
+ 'time_t',
13
+ ]
14
+
15
+ INT_TYPES = [
16
+ 'int',
17
+ 'size_t',
18
+ 'unsigned int',
19
+ 'uint',
20
+ 'uchar',
21
+ 'char',
22
+ ]
23
+
24
+ BOOL_TYPES = [
25
+ 'bool',
26
+ ]
27
+
28
+ def initialize
29
+ load_erb
30
+ @custom_types = []
31
+ end
32
+
33
+ def template_path=(template_path)
34
+ @template_path = template_path
35
+ load_erb
36
+ end
37
+
38
+ def custom_type(regexp, &block)
39
+ @custom_types << [regexp, block]
40
+ end
41
+
42
+ # Produce bindings for a group of overloaded functions
43
+ def group(group)
44
+ @group = group
45
+ if @group.members
46
+ @group_template.result(binding)
47
+ else
48
+ ''
49
+ end
50
+ end
51
+
52
+ def function(function)
53
+ @function = function
54
+ @function_template.result(binding)
55
+ end
56
+
57
+ def function_generator
58
+ self
59
+ end
60
+
61
+ def namespace_generator
62
+ Dub::Lua.namespace_generator
63
+ end
64
+
65
+ def chooser_body(group = @group)
66
+ decision_tree = Argument.decision_tree(group.members)
67
+ res = []
68
+ res << "int type__ = lua_type(L, 1);"
69
+ if flatten_hash(decision_tree).include?(nil)
70
+ res << "int top__ = lua_gettop(L);"
71
+ end
72
+ res << switch(decision_tree)
73
+ res.join("\n")
74
+ end
75
+
76
+ # Create a switch to choose the correct method from argument types (overloaded functions)
77
+ def switch(hash_or_function, depth = 1)
78
+ if hash_or_function.kind_of?(Function)
79
+ method_call(hash_or_function)
80
+ else
81
+ res = []
82
+ res << "type__ = lua_type(L, #{depth});" unless depth == 1
83
+ else_prefix = ''
84
+ default_sub_group = nil
85
+ hash_or_function.each do |type, sub_group|
86
+ default_sub_group = sub_group
87
+ case type
88
+ when :number
89
+ res << "#{else_prefix}if (type__ == LUA_TNUMBER) {"
90
+ when :string
91
+ res << "#{else_prefix}if (type__ == LUA_TSTRING) {"
92
+ when nil
93
+ res << "#{else_prefix}if (top__ < #{depth}) {"
94
+ else
95
+ res << "#{else_prefix}if (type__ == LUA_TUSERDATA && is_userdata(L, #{depth}, \"#{type}\")) {"
96
+ end
97
+
98
+ res << indent(switch(sub_group, depth + 1), 2)
99
+
100
+ else_prefix = '} else '
101
+ end
102
+
103
+ last = default_sub_group.kind_of?(Hash) ? flatten_hash(default_sub_group).last : default_sub_group
104
+
105
+ res << "} else {"
106
+ res << " // use any to raise errors"
107
+ res << indent(method_call(last), 2)
108
+ res << "}"
109
+ res.join("\n")
110
+ end
111
+ end
112
+
113
+ def signature(func, overloaded_index = nil)
114
+ "static int #{method_name(func, overloaded_index)}(lua_State *L)"
115
+ end
116
+
117
+ def body(func)
118
+ res = []
119
+
120
+ if func.member_method? && !func.constructor? && !func.static?
121
+ klass = func.parent
122
+ res << "#{klass.name} *self__ = *((#{klass.name}**)luaL_checkudata(L, 1, #{klass.id_name.inspect}));"
123
+ res << "lua_remove(L, 1);"
124
+ end
125
+
126
+ if func.has_default_arguments?
127
+ res << "int top__ = lua_gettop(L);"
128
+ if return_value = func.return_value
129
+ res << "#{return_value.create_type} retval__;"
130
+ end
131
+ end
132
+
133
+ if_indent = 0
134
+ func.arguments.each_with_index do |arg, i|
135
+ if arg.has_default?
136
+ res << indent("if (top__ < #{i+1}) {", if_indent)
137
+ res << indent(" #{call_string(func, i)}", if_indent)
138
+ res << indent("} else {", if_indent)
139
+ if_indent += 2
140
+ end
141
+ res << indent(get_arg(arg, i + 1), if_indent)
142
+ end
143
+ res << indent(call_string(func, func.arguments.count), if_indent)
144
+ while if_indent > 0
145
+ if_indent -= 2
146
+ res << indent("}", if_indent)
147
+ end
148
+
149
+ res << return_value(func)
150
+ res.join("\n")
151
+ end
152
+
153
+ def method_name(func, overloaded_index = nil)
154
+ overloaded_index ||= func.overloaded_index
155
+ overloaded_index = '' if overloaded_index == 0
156
+ "#{func.prefix}_#{func.name}#{overloaded_index}"
157
+ end
158
+
159
+ def method_call(func)
160
+ "return #{method_name(func)}(L);"
161
+ end
162
+
163
+ def call_string(func, upto_arg = nil)
164
+ upto_arg ||= func.arguments.count
165
+ if upto_arg == 0
166
+ call_string = "#{func.call_name}();"
167
+ else
168
+ call_string = "#{func.call_name}(#{func.arguments[0..(upto_arg-1)].map{|arg| arg.in_call_type}.join(', ')});"
169
+ end
170
+
171
+ if func.constructor?
172
+ call_string = "new #{call_string}"
173
+ elsif func.member_method? && !func.static?
174
+ call_string = "self__->#{call_string}"
175
+ end
176
+
177
+
178
+ if return_value = func.return_value
179
+ if func.has_default_arguments?
180
+ "retval__ = #{call_string}"
181
+ else
182
+ "#{return_value.create_type} retval__ = #{call_string}"
183
+ end
184
+ else
185
+ call_string
186
+ end
187
+ end
188
+
189
+ def return_value(func)
190
+ res = []
191
+ if return_value = func.return_value
192
+ case Argument.type_group(return_value)
193
+ when :number
194
+ res << "lua_pushnumber(L, retval__);"
195
+ when :boolean
196
+ res << "lua_pushboolean(L, retval__);"
197
+ when :string
198
+ raise "Not supported yet"
199
+ else
200
+ if func.constructor?
201
+ prefix = func.klass.prefix
202
+ else
203
+ prefix = func.prefix
204
+ end
205
+ res << "lua_pushclass<#{return_value.type}>(L, retval__, \"#{return_value.id_name}\");"
206
+ end
207
+ res << "return 1;"
208
+ else
209
+ res << "return 0;"
210
+ end
211
+ res.join("\n")
212
+ end
213
+
214
+ # Get argument and verify type
215
+ # // luaL_argcheck could be better to report errors like "expected Mat"
216
+ def get_arg(arg, stack_pos)
217
+ type_def = "#{arg.create_type}#{arg.name}#{arg.array_suffix}"
218
+ if custom_type = @custom_types.detect {|reg,proc| type_def =~ reg}
219
+ custom_type[1].call(type_def, arg, stack_pos)
220
+ elsif arg.is_native?
221
+ if arg.is_pointer?
222
+ if arg.type == 'char'
223
+ type_def = "const #{type_def}" unless arg.is_const?
224
+ "#{type_def} = luaL_checkstring(L, #{stack_pos});"
225
+ else
226
+ # retrieve by using a table accessor
227
+ # TODO: we should have a hint on required sizes !
228
+ "\nDubArgPointer<#{arg.type}> ptr_#{arg.name};\n" +
229
+ "#{type_def} = ptr_#{arg.name}(L, #{stack_pos});"
230
+ end
231
+ else
232
+ if NUMBER_TYPES.include?(arg.type)
233
+ "#{type_def} = luaL_checknumber(L, #{stack_pos});"
234
+ elsif BOOL_TYPES.include?(arg.type)
235
+ "#{type_def} = lua_toboolean(L, #{stack_pos});"
236
+ elsif INT_TYPES.include?(arg.type)
237
+ "#{type_def} = luaL_checkint(L, #{stack_pos});"
238
+ else
239
+ raise "Unsuported type: #{arg.type}"
240
+ end
241
+ end
242
+ else
243
+ "#{type_def} = *((#{arg.create_type}*)luaL_checkudata(L, #{stack_pos}, #{arg.id_name.inspect}));"
244
+ end
245
+ end
246
+
247
+ def flatten_hash(hash)
248
+ list = []
249
+ hash.each do |k, v|
250
+ if v.kind_of?(Hash)
251
+ list << [k, flatten_hash(v)]
252
+ else
253
+ list << [k, v]
254
+ end
255
+ end
256
+ list.flatten
257
+ end
258
+
259
+ def load_erb
260
+ @function_template = ::ERB.new(File.read(@template_path ||File.join(File.dirname(__FILE__), 'function.cpp.erb')))
261
+ @group_template = ::ERB.new(File.read(File.join(File.dirname(__FILE__), 'group.cpp.erb')))
262
+ end
263
+ end # FunctionGen
264
+ end # Lua
265
+ end # Dub
@@ -0,0 +1,9 @@
1
+ <% @group.members.each do |f| %>
2
+ <%= function(f) %>
3
+
4
+ <% end %>
5
+
6
+ /** Overloaded function chooser for <%= @group.name %>(...) */
7
+ <%= signature(@group.first, 0) %> {
8
+ <%= indent(chooser_body, 2) %>
9
+ }
@@ -0,0 +1,141 @@
1
+
2
+ #ifndef DOXY_GENERATOR_LIB_DOXY_GENERATOR_INCLUDE_LUA_DOXY_HELPER_H_
3
+ #define DOXY_GENERATOR_LIB_DOXY_GENERATOR_INCLUDE_LUA_DOXY_HELPER_H_
4
+
5
+ #include <stdlib.h> // malloc
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+ // We need C linkage because lua lib is compiled as C code
11
+ #include "lua.h"
12
+ #include "lauxlib.h"
13
+
14
+ #ifdef __cplusplus
15
+ }
16
+ #endif
17
+ /** ======================================== lua_pushclass */
18
+
19
+ /** Push a custom type on the stack.
20
+ * Since the value is passed as a pointer, we assume it has been created
21
+ * using 'new' and Lua can safely call delete when it needs to garbage-
22
+ * -collect it.
23
+ */
24
+ template<class T>
25
+ void lua_pushclass(lua_State *L, T *ptr, const char *type_name) {
26
+ T **userdata = (T**)lua_newuserdata(L, sizeof(T*));
27
+ *userdata = ptr;
28
+
29
+ // the userdata is now on top of the stack
30
+
31
+ // set metatable (contains methods)
32
+ luaL_getmetatable(L, type_name);
33
+ lua_setmetatable(L, -2);
34
+ }
35
+
36
+ /** Push a custom type on the stack.
37
+ * Since the value is passed by value, we have to allocate a copy
38
+ * using 'new' so that Lua can keep it.
39
+ */
40
+ template<class T>
41
+ void lua_pushclass(lua_State *L, T &val, const char *type_name) {
42
+ T *val_ptr = new T(val);
43
+ lua_pushclass<T>(L, val_ptr, type_name);
44
+ }
45
+
46
+ /** ======================================== DubArgPointer */
47
+
48
+ /** This class is a helper to provide pointer to data from
49
+ * Lua to C (but maybe it's not a good idea).
50
+ */
51
+ template<class T>
52
+ class DubArgPointer {
53
+ public:
54
+ DubArgPointer() : data(NULL) {}
55
+
56
+ ~DubArgPointer() {
57
+ if (data) free(data);
58
+ }
59
+
60
+ // TODO: we should have a hint on required sizes !
61
+ T *operator()(lua_State *L, int index) {
62
+ if (!lua_istable(L, index)) throw 1;
63
+
64
+ size_t size = lua_objlen(L, index);
65
+ if (size == 0) return NULL;
66
+
67
+ data = (T*)malloc(size * sizeof(T));
68
+ if (!data) throw 1;
69
+
70
+ for(size_t i=0; i < size; ++i) {
71
+ data[i] = get_value_at(L, index, i+1);
72
+ }
73
+ return data;
74
+ }
75
+
76
+ private:
77
+ T get_value_at(lua_State *L, int table_index, int index) {
78
+ lua_pushinteger(L, index + 1);
79
+ lua_gettable(L, index);
80
+ T value = luaL_checknumber(L, -1);
81
+ lua_pop(L, 1);
82
+ return value;
83
+ }
84
+
85
+ T *data;
86
+ };
87
+
88
+ /** ======================================== is_userdata */
89
+
90
+ inline bool is_userdata(lua_State *L, int index, const char *tname) {
91
+ void *p = lua_touserdata(L, index);
92
+ if (p != NULL) { /* value is a userdata? */
93
+ if (lua_getmetatable(L, index)) { /* does it have a metatable? */
94
+ lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
95
+ if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
96
+ lua_pop(L, 2); /* remove both metatables */
97
+ // type match
98
+ return true;
99
+ }
100
+ }
101
+ }
102
+ // type does not match
103
+ return false;
104
+ }
105
+
106
+ /** ======================================== register_constants */
107
+
108
+
109
+ typedef struct lua_constants_Reg {
110
+ const char *name;
111
+ double constant;
112
+ } lua_constants_Reg;
113
+
114
+ inline int libsize (const lua_constants_Reg *l) {
115
+ int size = 0;
116
+ for (; l->name; l++) size++;
117
+ return size;
118
+ }
119
+
120
+ inline void register_constants(lua_State *L, const char *name_space, const lua_constants_Reg *l) {
121
+ if (name_space) {
122
+ /* compute size hint for new table. */
123
+ int size = libsize(l);
124
+
125
+ /* try global variable (and create one if it does not exist) */
126
+ if (luaL_findtable(L, LUA_GLOBALSINDEX, name_space, size) != NULL)
127
+ luaL_error(L, "name conflict for module " LUA_QS, name_space);
128
+
129
+ /* found name_space in global index ==> stack -1 */
130
+ }
131
+ for (; l->name; l++) {
132
+ /* push each constant into the name_space (stack position = -1)*/
133
+ lua_pushnumber(L, l->constant);
134
+ lua_setfield(L, -2, l->name);
135
+ }
136
+ /* pop name_space */
137
+ lua_pop(L, 1);
138
+ }
139
+
140
+
141
+ #endif // DOXY_GENERATOR_LIB_DOXY_GENERATOR_INCLUDE_LUA_DOXY_HELPER_H_
@@ -0,0 +1,40 @@
1
+ #include "<%= @namespace.header %>"
2
+
3
+ #include "lua_cpp_helper.h"
4
+
5
+ using namespace <%= @namespace.name %>;
6
+
7
+ <% if @namespace.members; @namespace.members.each do |function| %>
8
+
9
+ <%= function %>
10
+
11
+ <% end; end %>
12
+
13
+ // Register namespace
14
+
15
+
16
+ static const struct luaL_Reg <%= @namespace.name %>_functions[] = {
17
+ <%= indent(functions_registration, 2) %>,
18
+ {NULL, NULL},
19
+ };
20
+
21
+ <% if @namespace.has_constants? %>
22
+ static const struct lua_constants_Reg <%= @namespace.name %>_namespace_constants[] = {
23
+ <%= indent(constants_registration, 2) %>,
24
+ {NULL, NULL},
25
+ };
26
+ <% end %>
27
+
28
+ #ifdef DUB_LUA_NO_OPEN
29
+ int luaload_<%= @namespace.lib_name %>(lua_State *L) {
30
+ #else
31
+ extern "C" int luaopen_<%= @namespace.lib_name %>(lua_State *L) {
32
+ #endif
33
+ // register functions
34
+ luaL_register(L, <%= @namespace.name.inspect %>, <%= @namespace.name %>_functions);
35
+ <% if @namespace.has_constants? %>
36
+ // register namespace enums
37
+ register_constants(L, <%= @namespace.id_name.inspect %>, <%= @namespace.name %>_namespace_constants);
38
+ <% end %>
39
+ return 0;
40
+ }
@@ -0,0 +1,88 @@
1
+ require 'dub/generator'
2
+ require 'erb'
3
+
4
+ module Dub
5
+ module Lua
6
+ class NamespaceGen < Dub::Generator
7
+ def initialize
8
+ @namespace_template = ::ERB.new(File.read(File.join(File.dirname(__FILE__), 'namespace.cpp.erb')))
9
+ end
10
+
11
+ def namespace(namespace)
12
+ @namespace = namespace
13
+ @namespace_template.result(binding)
14
+ end
15
+
16
+ def class_generator
17
+ Lua.class_generator
18
+ end
19
+
20
+ def function_generator
21
+ Lua.function_generator
22
+ end
23
+
24
+ def functions_registration(namespace = @namespace)
25
+ (namespace.members || []).map do |method|
26
+ "{%-32s, #{method.method_name(0)}}" % method.name.inspect
27
+ end.join(",\n")
28
+ end
29
+
30
+ def constants_registration(namespace = @namespace)
31
+ res = []
32
+ if namespace.has_enums?
33
+ res << namespace.enums.map do |name|
34
+ "{%-32s, #{namespace.full_type}::#{name}}" % name.inspect
35
+ end.join(",\n")
36
+ end
37
+
38
+ if namespace.has_defines?
39
+ res << namespace.defines.map do |name|
40
+ "{%-32s, #{name}}" % name.inspect
41
+ end.join(",\n")
42
+ end
43
+
44
+ same = namespace.enums & namespace.defines
45
+ unless same.empty?
46
+ # Should never happen (not sure if it would compile ok in the first place)
47
+ puts "Warning: the following are present both as enum and define: #{same.inspect}"
48
+ end
49
+ res.join(",\n\n")
50
+ end
51
+
52
+ def members_list(all_members, ignore_list = [])
53
+ list = (all_members || []).map do |member_or_group|
54
+ if ignore_list.include?(member_or_group.name)
55
+ nil
56
+ elsif member_or_group.kind_of?(Array)
57
+ members_list(member_or_group)
58
+ elsif ignore_member?(member_or_group)
59
+ nil
60
+ else
61
+ member_or_group
62
+ end
63
+ end
64
+
65
+ list.compact!
66
+ list == [] ? nil : list
67
+ end
68
+
69
+ def ignore_member?(member)
70
+ if member.name =~ /^~/ || # do not build constructor
71
+ member.name =~ /^operator/ || # no conversion operators
72
+ member.original_signature =~ />/ || # no complex types in signature
73
+ member.has_array_arguments? ||
74
+ member.vararg? ||
75
+ member.original_signature =~ /void\s+\*/ ||
76
+ member.has_class_pointer_arguments?
77
+
78
+ true # ignore
79
+ elsif return_value = member.return_value
80
+ return_value.type =~ />$/ || # no complex return types
81
+ return_value.is_native? && member.return_value.is_pointer?
82
+ else
83
+ false # ok, do not ignore
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
data/lib/dub/lua.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'dub/lua/function_gen'
2
+ require 'dub/lua/namespace_gen'
3
+ require 'dub/lua/class_gen'
4
+
5
+ module Dub
6
+ module Lua
7
+ def self.function_generator
8
+ @@function_generator ||= Dub::Lua::FunctionGen.new
9
+ end
10
+
11
+ def self.class_generator
12
+ @@class_generator ||= Dub::Lua::ClassGen.new
13
+ end
14
+
15
+ def self.namespace_generator
16
+ @@namespace_generator ||= Dub::Lua::NamespaceGen.new
17
+ end
18
+
19
+ def self.bind(object)
20
+ object.bind(self)
21
+ object
22
+ end
23
+ end # Lua
24
+ end # Dub