ruby_memprofiler_pprof 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +7 -0
  2. data/ext/ruby_memprofiler_pprof/backtrace.c +429 -0
  3. data/ext/ruby_memprofiler_pprof/collector.c +1055 -0
  4. data/ext/ruby_memprofiler_pprof/compat.c +182 -0
  5. data/ext/ruby_memprofiler_pprof/extconf.rb +72 -0
  6. data/ext/ruby_memprofiler_pprof/pprof.upb.c +170 -0
  7. data/ext/ruby_memprofiler_pprof/pprof.upb.h +848 -0
  8. data/ext/ruby_memprofiler_pprof/pprof_out.c +285 -0
  9. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.c +11 -0
  10. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.h +301 -0
  11. data/ext/ruby_memprofiler_pprof/strtab.c +391 -0
  12. data/ext/ruby_memprofiler_pprof/vendor/upb/BUILD +719 -0
  13. data/ext/ruby_memprofiler_pprof/vendor/upb/CONTRIBUTING.md +37 -0
  14. data/ext/ruby_memprofiler_pprof/vendor/upb/DESIGN.md +201 -0
  15. data/ext/ruby_memprofiler_pprof/vendor/upb/LICENSE +26 -0
  16. data/ext/ruby_memprofiler_pprof/vendor/upb/README.md +78 -0
  17. data/ext/ruby_memprofiler_pprof/vendor/upb/WORKSPACE +58 -0
  18. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/BUILD +53 -0
  19. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/amalgamate.py +129 -0
  20. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/build_defs.bzl +160 -0
  21. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/lua.BUILD +127 -0
  22. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/protobuf.patch +54 -0
  23. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/py_proto_library.bzl +137 -0
  24. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/python_downloads.bzl +84 -0
  25. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/system_python.bzl +101 -0
  26. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/upb_proto_library.bzl +388 -0
  27. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/workspace_deps.bzl +89 -0
  28. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD +252 -0
  29. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD.googleapis +54 -0
  30. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/benchmark.cc +333 -0
  31. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/build_defs.bzl +88 -0
  32. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/compare.py +118 -0
  33. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor.proto +888 -0
  34. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor_sv.proto +890 -0
  35. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/empty.proto +6 -0
  36. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_protobuf_binary_cc.py +64 -0
  37. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_synthetic_protos.py +118 -0
  38. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_upb_binary_c.py +65 -0
  39. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/BUILD.bazel +102 -0
  40. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/README.md +23 -0
  41. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/build_defs.bzl +73 -0
  42. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/make_cmakelists.py +340 -0
  43. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test.py +57 -0
  44. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test_lib.py +186 -0
  45. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/render.py +43 -0
  46. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/style-guide.md +65 -0
  47. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/vs-cpp-protos.md +255 -0
  48. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/wrapping-upb.md +444 -0
  49. data/ext/ruby_memprofiler_pprof/vendor/upb/python/BUILD +216 -0
  50. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.c +394 -0
  51. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.h +63 -0
  52. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.c +1694 -0
  53. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.h +80 -0
  54. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.c +704 -0
  55. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.h +114 -0
  56. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.c +650 -0
  57. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.h +48 -0
  58. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/BUILD.bazel +193 -0
  59. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/dist.bzl +190 -0
  60. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.c +247 -0
  61. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.h +39 -0
  62. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.c +522 -0
  63. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.h +66 -0
  64. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.c +1909 -0
  65. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.h +101 -0
  66. data/ext/ruby_memprofiler_pprof/vendor/upb/python/minimal_test.py +183 -0
  67. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/BUILD +70 -0
  68. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/README.md +11 -0
  69. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_database_test_wrapper.py +30 -0
  70. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_pool_test_wrapper.py +45 -0
  71. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_test_wrapper.py +46 -0
  72. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/generator_test_wrapper.py +30 -0
  73. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/json_format_test_wrapper.py +30 -0
  74. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/keywords_test_wrapper.py +30 -0
  75. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_factory_test_wrapper.py +37 -0
  76. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_test_wrapper.py +52 -0
  77. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/proto_builder_test_wrapper.py +32 -0
  78. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/pyproto_test_wrapper.bzl +36 -0
  79. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/reflection_test_wrapper.py +45 -0
  80. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/service_reflection_test_wrapper.py +30 -0
  81. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/symbol_database_test_wrapper.py +30 -0
  82. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_encoding_test_wrapper.py +30 -0
  83. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_format_test_wrapper.py +30 -0
  84. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/unknown_fields_test_wrapper.py +30 -0
  85. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/well_known_types_test_wrapper.py +36 -0
  86. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/wire_format_test_wrapper.py +30 -0
  87. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.c +350 -0
  88. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.h +230 -0
  89. data/ext/ruby_memprofiler_pprof/vendor/upb/python/py_extension.bzl +55 -0
  90. data/ext/ruby_memprofiler_pprof/vendor/upb/python/python_api.h +61 -0
  91. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.c +828 -0
  92. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.h +69 -0
  93. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.c +404 -0
  94. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.h +39 -0
  95. data/ext/ruby_memprofiler_pprof/vendor/upb/python/version_script.lds +6 -0
  96. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/LICENSE +32 -0
  97. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/README.google +9 -0
  98. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/console.lua +156 -0
  99. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/lunit.lua +725 -0
  100. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/BUILD +19 -0
  101. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/LICENSE +21 -0
  102. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/naive.c +92 -0
  103. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-neon.c +157 -0
  104. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-sse.c +170 -0
  105. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/utf8_range.h +9 -0
  106. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/BUILD.bazel +129 -0
  107. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/README.md +8 -0
  108. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/def.c +939 -0
  109. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/lua_proto_library.bzl +138 -0
  110. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/main.c +83 -0
  111. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/msg.c +1118 -0
  112. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test.proto +69 -0
  113. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test_upb.lua +846 -0
  114. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.c +258 -0
  115. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.h +132 -0
  116. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.lua +58 -0
  117. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upbc.cc +134 -0
  118. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.c +192 -0
  119. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.h +174 -0
  120. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb.c +346 -0
  121. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb_failures.txt +1 -0
  122. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.c +1221 -0
  123. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.h +94 -0
  124. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.c +1055 -0
  125. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.h +153 -0
  126. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_internal.h +211 -0
  127. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.c +3262 -0
  128. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.h +414 -0
  129. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.hpp +438 -0
  130. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/empty.proto +1 -0
  131. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.c +604 -0
  132. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.h +71 -0
  133. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/BUILD +13 -0
  134. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/file_descriptor_parsenew_fuzzer.cc +43 -0
  135. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.c +1509 -0
  136. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.h +47 -0
  137. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.c +776 -0
  138. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.h +62 -0
  139. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.c +1147 -0
  140. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.h +189 -0
  141. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.hpp +112 -0
  142. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.c +363 -0
  143. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.h +263 -0
  144. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_internal.h +59 -0
  145. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_test.cc +425 -0
  146. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_test.cc +230 -0
  147. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.c +428 -0
  148. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.h +114 -0
  149. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_internal.h +836 -0
  150. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.cc +491 -0
  151. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.proto +195 -0
  152. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_def.inc +261 -0
  153. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_undef.inc +62 -0
  154. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.c +323 -0
  155. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.h +109 -0
  156. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.hpp +37 -0
  157. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table.c +926 -0
  158. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table_internal.h +385 -0
  159. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test.proto +74 -0
  160. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.cc +186 -0
  161. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.proto +12 -0
  162. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_generated_code.cc +977 -0
  163. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_table.cc +580 -0
  164. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.c +472 -0
  165. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.h +64 -0
  166. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.c +362 -0
  167. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.h +378 -0
  168. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.hpp +115 -0
  169. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb_internal.h +68 -0
  170. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/BUILD +121 -0
  171. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/README.md +7 -0
  172. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.c +300 -0
  173. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.h +66 -0
  174. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare_test.cc +236 -0
  175. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.c +572 -0
  176. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.h +62 -0
  177. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_public_import_test.proto +32 -0
  178. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_regular_import_test.proto +36 -0
  179. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.cc +143 -0
  180. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.proto +119 -0
  181. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_weak_import_test.proto +28 -0
  182. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_wweak_import_test.proto +28 -0
  183. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.c +311 -0
  184. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.h +94 -0
  185. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.cc +202 -0
  186. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.proto +48 -0
  187. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/BUILD +78 -0
  188. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.cc +77 -0
  189. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.h +112 -0
  190. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upb.cc +1997 -0
  191. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upbdefs.cc +193 -0
  192. data/lib/ruby_memprofiler_pprof/atfork.rb +77 -0
  193. data/lib/ruby_memprofiler_pprof/block_flusher.rb +61 -0
  194. data/lib/ruby_memprofiler_pprof/file_flusher.rb +45 -0
  195. data/lib/ruby_memprofiler_pprof/profile_app.rb +30 -0
  196. data/lib/ruby_memprofiler_pprof/profile_data.rb +18 -0
  197. data/lib/ruby_memprofiler_pprof/version.rb +5 -0
  198. data/lib/ruby_memprofiler_pprof.rb +8 -0
  199. data/libexec/ruby_memprofiler_pprof_profile +16 -0
  200. metadata +257 -0
@@ -0,0 +1,939 @@
1
+ /*
2
+ * Copyright (c) 2009-2021, Google LLC
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ * * Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ * * Neither the name of Google LLC nor the
13
+ * names of its contributors may be used to endorse or promote products
14
+ * derived from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include "upb/def.h"
29
+
30
+ #include <float.h>
31
+ #include <math.h>
32
+ #include <stdlib.h>
33
+ #include <string.h>
34
+
35
+ #include "lauxlib.h"
36
+ #include "upb/bindings/lua/upb.h"
37
+ #include "upb/reflection.h"
38
+
39
+ #define LUPB_ENUMDEF "lupb.enumdef"
40
+ #define LUPB_ENUMVALDEF "lupb.enumvaldef"
41
+ #define LUPB_FIELDDEF "lupb.fielddef"
42
+ #define LUPB_FILEDEF "lupb.filedef"
43
+ #define LUPB_MSGDEF "lupb.msgdef"
44
+ #define LUPB_ONEOFDEF "lupb.oneof"
45
+ #define LUPB_SYMTAB "lupb.symtab"
46
+ #define LUPB_OBJCACHE "lupb.objcache"
47
+
48
+ static void lupb_DefPool_pushwrapper(lua_State* L, int narg, const void* def,
49
+ const char* type);
50
+
51
+ /* lupb_wrapper ***************************************************************/
52
+
53
+ /* Wrappers around upb def objects. The userval contains a reference to the
54
+ * symtab. */
55
+
56
+ #define LUPB_SYMTAB_INDEX 1
57
+
58
+ typedef struct {
59
+ const void* def; /* upb_MessageDef, upb_EnumDef, upb_OneofDef, etc. */
60
+ } lupb_wrapper;
61
+
62
+ static const void* lupb_wrapper_check(lua_State* L, int narg,
63
+ const char* type) {
64
+ lupb_wrapper* w = luaL_checkudata(L, narg, type);
65
+ return w->def;
66
+ }
67
+
68
+ static void lupb_wrapper_pushsymtab(lua_State* L, int narg) {
69
+ lua_getiuservalue(L, narg, LUPB_SYMTAB_INDEX);
70
+ }
71
+
72
+ /* lupb_wrapper_pushwrapper()
73
+ *
74
+ * For a given def wrapper at index |narg|, pushes a wrapper for the given |def|
75
+ * and the given |type|. The new wrapper will be part of the same symtab. */
76
+ static void lupb_wrapper_pushwrapper(lua_State* L, int narg, const void* def,
77
+ const char* type) {
78
+ lupb_wrapper_pushsymtab(L, narg);
79
+ lupb_DefPool_pushwrapper(L, -1, def, type);
80
+ lua_replace(L, -2); /* Remove symtab from stack. */
81
+ }
82
+
83
+ /* lupb_MessageDef_pushsubmsgdef()
84
+ *
85
+ * Pops the msgdef wrapper at the top of the stack and replaces it with a msgdef
86
+ * wrapper for field |f| of this msgdef (submsg may not be direct, for example
87
+ * it may be the submessage of the map value).
88
+ */
89
+ void lupb_MessageDef_pushsubmsgdef(lua_State* L, const upb_FieldDef* f) {
90
+ const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
91
+ assert(m);
92
+ lupb_wrapper_pushwrapper(L, -1, m, LUPB_MSGDEF);
93
+ lua_replace(L, -2); /* Replace msgdef with submsgdef. */
94
+ }
95
+
96
+ /* lupb_FieldDef **************************************************************/
97
+
98
+ const upb_FieldDef* lupb_FieldDef_check(lua_State* L, int narg) {
99
+ return lupb_wrapper_check(L, narg, LUPB_FIELDDEF);
100
+ }
101
+
102
+ static int lupb_FieldDef_ContainingOneof(lua_State* L) {
103
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
104
+ const upb_OneofDef* o = upb_FieldDef_ContainingOneof(f);
105
+ lupb_wrapper_pushwrapper(L, 1, o, LUPB_ONEOFDEF);
106
+ return 1;
107
+ }
108
+
109
+ static int lupb_FieldDef_ContainingType(lua_State* L) {
110
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
111
+ const upb_MessageDef* m = upb_FieldDef_ContainingType(f);
112
+ lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
113
+ return 1;
114
+ }
115
+
116
+ static int lupb_FieldDef_Default(lua_State* L) {
117
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
118
+ upb_CType type = upb_FieldDef_CType(f);
119
+ if (type == kUpb_CType_Message) {
120
+ return luaL_error(L, "Message fields do not have explicit defaults.");
121
+ }
122
+ lupb_pushmsgval(L, 0, type, upb_FieldDef_Default(f));
123
+ return 1;
124
+ }
125
+
126
+ static int lupb_FieldDef_Type(lua_State* L) {
127
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
128
+ lua_pushnumber(L, upb_FieldDef_Type(f));
129
+ return 1;
130
+ }
131
+
132
+ static int lupb_FieldDef_HasSubDef(lua_State* L) {
133
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
134
+ lua_pushboolean(L, upb_FieldDef_HasSubDef(f));
135
+ return 1;
136
+ }
137
+
138
+ static int lupb_FieldDef_Index(lua_State* L) {
139
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
140
+ lua_pushinteger(L, upb_FieldDef_Index(f));
141
+ return 1;
142
+ }
143
+
144
+ static int lupb_FieldDef_IsExtension(lua_State* L) {
145
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
146
+ lua_pushboolean(L, upb_FieldDef_IsExtension(f));
147
+ return 1;
148
+ }
149
+
150
+ static int lupb_FieldDef_Label(lua_State* L) {
151
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
152
+ lua_pushinteger(L, upb_FieldDef_Label(f));
153
+ return 1;
154
+ }
155
+
156
+ static int lupb_FieldDef_Name(lua_State* L) {
157
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
158
+ lua_pushstring(L, upb_FieldDef_Name(f));
159
+ return 1;
160
+ }
161
+
162
+ static int lupb_FieldDef_Number(lua_State* L) {
163
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
164
+ int32_t num = upb_FieldDef_Number(f);
165
+ if (num) {
166
+ lua_pushinteger(L, num);
167
+ } else {
168
+ lua_pushnil(L);
169
+ }
170
+ return 1;
171
+ }
172
+
173
+ static int lupb_FieldDef_IsPacked(lua_State* L) {
174
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
175
+ lua_pushboolean(L, upb_FieldDef_IsPacked(f));
176
+ return 1;
177
+ }
178
+
179
+ static int lupb_FieldDef_MessageSubDef(lua_State* L) {
180
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
181
+ const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
182
+ lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
183
+ return 1;
184
+ }
185
+
186
+ static int lupb_FieldDef_EnumSubDef(lua_State* L) {
187
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
188
+ const upb_EnumDef* e = upb_FieldDef_EnumSubDef(f);
189
+ lupb_wrapper_pushwrapper(L, 1, e, LUPB_ENUMDEF);
190
+ return 1;
191
+ }
192
+
193
+ static int lupb_FieldDef_CType(lua_State* L) {
194
+ const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
195
+ lua_pushinteger(L, upb_FieldDef_CType(f));
196
+ return 1;
197
+ }
198
+
199
+ static const struct luaL_Reg lupb_FieldDef_m[] = {
200
+ {"containing_oneof", lupb_FieldDef_ContainingOneof},
201
+ {"containing_type", lupb_FieldDef_ContainingType},
202
+ {"default", lupb_FieldDef_Default},
203
+ {"descriptor_type", lupb_FieldDef_Type},
204
+ {"has_subdef", lupb_FieldDef_HasSubDef},
205
+ {"index", lupb_FieldDef_Index},
206
+ {"is_extension", lupb_FieldDef_IsExtension},
207
+ {"label", lupb_FieldDef_Label},
208
+ {"name", lupb_FieldDef_Name},
209
+ {"number", lupb_FieldDef_Number},
210
+ {"packed", lupb_FieldDef_IsPacked},
211
+ {"msgsubdef", lupb_FieldDef_MessageSubDef},
212
+ {"enumsubdef", lupb_FieldDef_EnumSubDef},
213
+ {"type", lupb_FieldDef_CType},
214
+ {NULL, NULL}};
215
+
216
+ /* lupb_OneofDef **************************************************************/
217
+
218
+ const upb_OneofDef* lupb_OneofDef_check(lua_State* L, int narg) {
219
+ return lupb_wrapper_check(L, narg, LUPB_ONEOFDEF);
220
+ }
221
+
222
+ static int lupb_OneofDef_ContainingType(lua_State* L) {
223
+ const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
224
+ const upb_MessageDef* m = upb_OneofDef_ContainingType(o);
225
+ lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
226
+ return 1;
227
+ }
228
+
229
+ static int lupb_OneofDef_Field(lua_State* L) {
230
+ const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
231
+ int32_t idx = lupb_checkint32(L, 2);
232
+ int count = upb_OneofDef_FieldCount(o);
233
+
234
+ if (idx < 0 || idx >= count) {
235
+ const char* msg =
236
+ lua_pushfstring(L, "index %d exceeds field count %d", idx, count);
237
+ return luaL_argerror(L, 2, msg);
238
+ }
239
+
240
+ lupb_wrapper_pushwrapper(L, 1, upb_OneofDef_Field(o, idx), LUPB_FIELDDEF);
241
+ return 1;
242
+ }
243
+
244
+ static int lupb_oneofiter_next(lua_State* L) {
245
+ const upb_OneofDef* o = lupb_OneofDef_check(L, lua_upvalueindex(1));
246
+ int* index = lua_touserdata(L, lua_upvalueindex(2));
247
+ const upb_FieldDef* f;
248
+ if (*index == upb_OneofDef_FieldCount(o)) return 0;
249
+ f = upb_OneofDef_Field(o, (*index)++);
250
+ lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), f, LUPB_FIELDDEF);
251
+ return 1;
252
+ }
253
+
254
+ static int lupb_OneofDef_Fields(lua_State* L) {
255
+ int* index = lua_newuserdata(L, sizeof(int));
256
+ lupb_OneofDef_check(L, 1);
257
+ *index = 0;
258
+
259
+ /* Closure upvalues are: oneofdef, index. */
260
+ lua_pushcclosure(L, &lupb_oneofiter_next, 2);
261
+ return 1;
262
+ }
263
+
264
+ static int lupb_OneofDef_len(lua_State* L) {
265
+ const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
266
+ lua_pushinteger(L, upb_OneofDef_FieldCount(o));
267
+ return 1;
268
+ }
269
+
270
+ /* lupb_OneofDef_lookupfield()
271
+ *
272
+ * Handles:
273
+ * oneof.lookup_field(field_number)
274
+ * oneof.lookup_field(field_name)
275
+ */
276
+ static int lupb_OneofDef_lookupfield(lua_State* L) {
277
+ const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
278
+ const upb_FieldDef* f;
279
+
280
+ switch (lua_type(L, 2)) {
281
+ case LUA_TNUMBER:
282
+ f = upb_OneofDef_LookupNumber(o, lua_tointeger(L, 2));
283
+ break;
284
+ case LUA_TSTRING:
285
+ f = upb_OneofDef_LookupName(o, lua_tostring(L, 2));
286
+ break;
287
+ default: {
288
+ const char* msg = lua_pushfstring(L, "number or string expected, got %s",
289
+ luaL_typename(L, 2));
290
+ return luaL_argerror(L, 2, msg);
291
+ }
292
+ }
293
+
294
+ lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF);
295
+ return 1;
296
+ }
297
+
298
+ static int lupb_OneofDef_Name(lua_State* L) {
299
+ const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
300
+ lua_pushstring(L, upb_OneofDef_Name(o));
301
+ return 1;
302
+ }
303
+
304
+ static const struct luaL_Reg lupb_OneofDef_m[] = {
305
+ {"containing_type", lupb_OneofDef_ContainingType},
306
+ {"field", lupb_OneofDef_Field},
307
+ {"fields", lupb_OneofDef_Fields},
308
+ {"lookup_field", lupb_OneofDef_lookupfield},
309
+ {"name", lupb_OneofDef_Name},
310
+ {NULL, NULL}};
311
+
312
+ static const struct luaL_Reg lupb_OneofDef_mm[] = {{"__len", lupb_OneofDef_len},
313
+ {NULL, NULL}};
314
+
315
+ /* lupb_MessageDef
316
+ * ****************************************************************/
317
+
318
+ typedef struct {
319
+ const upb_MessageDef* md;
320
+ } lupb_MessageDef;
321
+
322
+ const upb_MessageDef* lupb_MessageDef_check(lua_State* L, int narg) {
323
+ return lupb_wrapper_check(L, narg, LUPB_MSGDEF);
324
+ }
325
+
326
+ static int lupb_MessageDef_FieldCount(lua_State* L) {
327
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
328
+ lua_pushinteger(L, upb_MessageDef_FieldCount(m));
329
+ return 1;
330
+ }
331
+
332
+ static int lupb_MessageDef_OneofCount(lua_State* L) {
333
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
334
+ lua_pushinteger(L, upb_MessageDef_OneofCount(m));
335
+ return 1;
336
+ }
337
+
338
+ static bool lupb_MessageDef_pushnested(lua_State* L, int msgdef, int name) {
339
+ const upb_MessageDef* m = lupb_MessageDef_check(L, msgdef);
340
+ lupb_wrapper_pushsymtab(L, msgdef);
341
+ upb_DefPool* symtab = lupb_DefPool_check(L, -1);
342
+ lua_pop(L, 1);
343
+
344
+ /* Construct full package.Message.SubMessage name. */
345
+ lua_pushstring(L, upb_MessageDef_FullName(m));
346
+ lua_pushstring(L, ".");
347
+ lua_pushvalue(L, name);
348
+ lua_concat(L, 3);
349
+ const char* nested_name = lua_tostring(L, -1);
350
+
351
+ /* Try lookup. */
352
+ const upb_MessageDef* nested =
353
+ upb_DefPool_FindMessageByName(symtab, nested_name);
354
+ if (!nested) return false;
355
+ lupb_wrapper_pushwrapper(L, msgdef, nested, LUPB_MSGDEF);
356
+ return true;
357
+ }
358
+
359
+ /* lupb_MessageDef_Field()
360
+ *
361
+ * Handles:
362
+ * msg.field(field_number) -> fielddef
363
+ * msg.field(field_name) -> fielddef
364
+ */
365
+ static int lupb_MessageDef_Field(lua_State* L) {
366
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
367
+ const upb_FieldDef* f;
368
+
369
+ switch (lua_type(L, 2)) {
370
+ case LUA_TNUMBER:
371
+ f = upb_MessageDef_FindFieldByNumber(m, lua_tointeger(L, 2));
372
+ break;
373
+ case LUA_TSTRING:
374
+ f = upb_MessageDef_FindFieldByName(m, lua_tostring(L, 2));
375
+ break;
376
+ default: {
377
+ const char* msg = lua_pushfstring(L, "number or string expected, got %s",
378
+ luaL_typename(L, 2));
379
+ return luaL_argerror(L, 2, msg);
380
+ }
381
+ }
382
+
383
+ lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF);
384
+ return 1;
385
+ }
386
+
387
+ /* lupb_MessageDef_FindByNameWithSize()
388
+ *
389
+ * Handles:
390
+ * msg.lookup_name(name) -> fielddef or oneofdef
391
+ */
392
+ static int lupb_MessageDef_FindByNameWithSize(lua_State* L) {
393
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
394
+ const upb_FieldDef* f;
395
+ const upb_OneofDef* o;
396
+
397
+ if (!upb_MessageDef_FindByName(m, lua_tostring(L, 2), &f, &o)) {
398
+ lua_pushnil(L);
399
+ } else if (o) {
400
+ lupb_wrapper_pushwrapper(L, 1, o, LUPB_ONEOFDEF);
401
+ } else {
402
+ lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF);
403
+ }
404
+
405
+ return 1;
406
+ }
407
+
408
+ /* lupb_MessageDef_Name()
409
+ *
410
+ * Handles:
411
+ * msg.name() -> string
412
+ */
413
+ static int lupb_MessageDef_Name(lua_State* L) {
414
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
415
+ lua_pushstring(L, upb_MessageDef_Name(m));
416
+ return 1;
417
+ }
418
+
419
+ static int lupb_msgfielditer_next(lua_State* L) {
420
+ const upb_MessageDef* m = lupb_MessageDef_check(L, lua_upvalueindex(1));
421
+ int* index = lua_touserdata(L, lua_upvalueindex(2));
422
+ const upb_FieldDef* f;
423
+ if (*index == upb_MessageDef_FieldCount(m)) return 0;
424
+ f = upb_MessageDef_Field(m, (*index)++);
425
+ lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), f, LUPB_FIELDDEF);
426
+ return 1;
427
+ }
428
+
429
+ static int lupb_MessageDef_Fields(lua_State* L) {
430
+ int* index = lua_newuserdata(L, sizeof(int));
431
+ lupb_MessageDef_check(L, 1);
432
+ *index = 0;
433
+
434
+ /* Closure upvalues are: msgdef, index. */
435
+ lua_pushcclosure(L, &lupb_msgfielditer_next, 2);
436
+ return 1;
437
+ }
438
+
439
+ static int lupb_MessageDef_File(lua_State* L) {
440
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
441
+ const upb_FileDef* file = upb_MessageDef_File(m);
442
+ lupb_wrapper_pushwrapper(L, 1, file, LUPB_FILEDEF);
443
+ return 1;
444
+ }
445
+
446
+ static int lupb_MessageDef_FullName(lua_State* L) {
447
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
448
+ lua_pushstring(L, upb_MessageDef_FullName(m));
449
+ return 1;
450
+ }
451
+
452
+ static int lupb_MessageDef_index(lua_State* L) {
453
+ if (!lupb_MessageDef_pushnested(L, 1, 2)) {
454
+ luaL_error(L, "No such nested message");
455
+ }
456
+ return 1;
457
+ }
458
+
459
+ static int lupb_msgoneofiter_next(lua_State* L) {
460
+ const upb_MessageDef* m = lupb_MessageDef_check(L, lua_upvalueindex(1));
461
+ int* index = lua_touserdata(L, lua_upvalueindex(2));
462
+ const upb_OneofDef* o;
463
+ if (*index == upb_MessageDef_OneofCount(m)) return 0;
464
+ o = upb_MessageDef_Oneof(m, (*index)++);
465
+ lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), o, LUPB_ONEOFDEF);
466
+ return 1;
467
+ }
468
+
469
+ static int lupb_MessageDef_Oneofs(lua_State* L) {
470
+ int* index = lua_newuserdata(L, sizeof(int));
471
+ lupb_MessageDef_check(L, 1);
472
+ *index = 0;
473
+
474
+ /* Closure upvalues are: msgdef, index. */
475
+ lua_pushcclosure(L, &lupb_msgoneofiter_next, 2);
476
+ return 1;
477
+ }
478
+
479
+ static int lupb_MessageDef_IsMapEntry(lua_State* L) {
480
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
481
+ lua_pushboolean(L, upb_MessageDef_IsMapEntry(m));
482
+ return 1;
483
+ }
484
+
485
+ static int lupb_MessageDef_Syntax(lua_State* L) {
486
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
487
+ lua_pushinteger(L, upb_MessageDef_Syntax(m));
488
+ return 1;
489
+ }
490
+
491
+ static int lupb_MessageDef_tostring(lua_State* L) {
492
+ const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
493
+ lua_pushfstring(L, "<upb.MessageDef name=%s, field_count=%d>",
494
+ upb_MessageDef_FullName(m),
495
+ (int)upb_MessageDef_FieldCount(m));
496
+ return 1;
497
+ }
498
+
499
+ static const struct luaL_Reg lupb_MessageDef_mm[] = {
500
+ {"__call", lupb_MessageDef_call},
501
+ {"__index", lupb_MessageDef_index},
502
+ {"__len", lupb_MessageDef_FieldCount},
503
+ {"__tostring", lupb_MessageDef_tostring},
504
+ {NULL, NULL}};
505
+
506
+ static const struct luaL_Reg lupb_MessageDef_m[] = {
507
+ {"field", lupb_MessageDef_Field},
508
+ {"fields", lupb_MessageDef_Fields},
509
+ {"field_count", lupb_MessageDef_FieldCount},
510
+ {"file", lupb_MessageDef_File},
511
+ {"full_name", lupb_MessageDef_FullName},
512
+ {"lookup_name", lupb_MessageDef_FindByNameWithSize},
513
+ {"name", lupb_MessageDef_Name},
514
+ {"oneof_count", lupb_MessageDef_OneofCount},
515
+ {"oneofs", lupb_MessageDef_Oneofs},
516
+ {"syntax", lupb_MessageDef_Syntax},
517
+ {"_map_entry", lupb_MessageDef_IsMapEntry},
518
+ {NULL, NULL}};
519
+
520
+ /* lupb_EnumDef ***************************************************************/
521
+
522
+ const upb_EnumDef* lupb_EnumDef_check(lua_State* L, int narg) {
523
+ return lupb_wrapper_check(L, narg, LUPB_ENUMDEF);
524
+ }
525
+
526
+ static int lupb_EnumDef_len(lua_State* L) {
527
+ const upb_EnumDef* e = lupb_EnumDef_check(L, 1);
528
+ lua_pushinteger(L, upb_EnumDef_ValueCount(e));
529
+ return 1;
530
+ }
531
+
532
+ static int lupb_EnumDef_File(lua_State* L) {
533
+ const upb_EnumDef* e = lupb_EnumDef_check(L, 1);
534
+ const upb_FileDef* file = upb_EnumDef_File(e);
535
+ lupb_wrapper_pushwrapper(L, 1, file, LUPB_FILEDEF);
536
+ return 1;
537
+ }
538
+
539
+ /* lupb_EnumDef_Value()
540
+ *
541
+ * Handles:
542
+ * enum.value(number) -> enumval
543
+ * enum.value(name) -> enumval
544
+ */
545
+ static int lupb_EnumDef_Value(lua_State* L) {
546
+ const upb_EnumDef* e = lupb_EnumDef_check(L, 1);
547
+ const upb_EnumValueDef* ev;
548
+
549
+ switch (lua_type(L, 2)) {
550
+ case LUA_TNUMBER:
551
+ ev = upb_EnumDef_FindValueByNumber(e, lupb_checkint32(L, 2));
552
+ break;
553
+ case LUA_TSTRING:
554
+ ev = upb_EnumDef_FindValueByName(e, lua_tostring(L, 2));
555
+ break;
556
+ default: {
557
+ const char* msg = lua_pushfstring(L, "number or string expected, got %s",
558
+ luaL_typename(L, 2));
559
+ return luaL_argerror(L, 2, msg);
560
+ }
561
+ }
562
+
563
+ lupb_wrapper_pushwrapper(L, 1, ev, LUPB_ENUMVALDEF);
564
+ return 1;
565
+ }
566
+
567
+ static const struct luaL_Reg lupb_EnumDef_mm[] = {{"__len", lupb_EnumDef_len},
568
+ {NULL, NULL}};
569
+
570
+ static const struct luaL_Reg lupb_EnumDef_m[] = {
571
+ {"file", lupb_EnumDef_File}, {"value", lupb_EnumDef_Value}, {NULL, NULL}};
572
+
573
+ /* lupb_EnumValueDef
574
+ * ************************************************************/
575
+
576
+ const upb_EnumValueDef* lupb_enumvaldef_check(lua_State* L, int narg) {
577
+ return lupb_wrapper_check(L, narg, LUPB_ENUMVALDEF);
578
+ }
579
+
580
+ static int lupb_EnumValueDef_Enum(lua_State* L) {
581
+ const upb_EnumValueDef* ev = lupb_enumvaldef_check(L, 1);
582
+ const upb_EnumDef* e = upb_EnumValueDef_Enum(ev);
583
+ lupb_wrapper_pushwrapper(L, 1, e, LUPB_ENUMDEF);
584
+ return 1;
585
+ }
586
+
587
+ static int lupb_EnumValueDef_FullName(lua_State* L) {
588
+ const upb_EnumValueDef* ev = lupb_enumvaldef_check(L, 1);
589
+ lua_pushstring(L, upb_EnumValueDef_FullName(ev));
590
+ return 1;
591
+ }
592
+
593
+ static int lupb_EnumValueDef_Name(lua_State* L) {
594
+ const upb_EnumValueDef* ev = lupb_enumvaldef_check(L, 1);
595
+ lua_pushstring(L, upb_EnumValueDef_Name(ev));
596
+ return 1;
597
+ }
598
+
599
+ static int lupb_EnumValueDef_Number(lua_State* L) {
600
+ const upb_EnumValueDef* ev = lupb_enumvaldef_check(L, 1);
601
+ lupb_pushint32(L, upb_EnumValueDef_Number(ev));
602
+ return 1;
603
+ }
604
+
605
+ static const struct luaL_Reg lupb_enumvaldef_m[] = {
606
+ {"enum", lupb_EnumValueDef_Enum},
607
+ {"full_name", lupb_EnumValueDef_FullName},
608
+ {"name", lupb_EnumValueDef_Name},
609
+ {"number", lupb_EnumValueDef_Number},
610
+ {NULL, NULL}};
611
+
612
+ /* lupb_FileDef ***************************************************************/
613
+
614
+ const upb_FileDef* lupb_FileDef_check(lua_State* L, int narg) {
615
+ return lupb_wrapper_check(L, narg, LUPB_FILEDEF);
616
+ }
617
+
618
+ static int lupb_FileDef_Dependency(lua_State* L) {
619
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
620
+ int index = luaL_checkint(L, 2);
621
+ const upb_FileDef* dep = upb_FileDef_Dependency(f, index);
622
+ lupb_wrapper_pushwrapper(L, 1, dep, LUPB_FILEDEF);
623
+ return 1;
624
+ }
625
+
626
+ static int lupb_FileDef_DependencyCount(lua_State* L) {
627
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
628
+ lua_pushnumber(L, upb_FileDef_DependencyCount(f));
629
+ return 1;
630
+ }
631
+
632
+ static int lupb_FileDef_enum(lua_State* L) {
633
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
634
+ int index = luaL_checkint(L, 2);
635
+ const upb_EnumDef* e = upb_FileDef_TopLevelEnum(f, index);
636
+ lupb_wrapper_pushwrapper(L, 1, e, LUPB_ENUMDEF);
637
+ return 1;
638
+ }
639
+
640
+ static int lupb_FileDef_enumcount(lua_State* L) {
641
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
642
+ lua_pushnumber(L, upb_FileDef_TopLevelEnumCount(f));
643
+ return 1;
644
+ }
645
+
646
+ static int lupb_FileDef_msg(lua_State* L) {
647
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
648
+ int index = luaL_checkint(L, 2);
649
+ const upb_MessageDef* m = upb_FileDef_TopLevelMessage(f, index);
650
+ lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
651
+ return 1;
652
+ }
653
+
654
+ static int lupb_FileDef_msgcount(lua_State* L) {
655
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
656
+ lua_pushnumber(L, upb_FileDef_TopLevelMessageCount(f));
657
+ return 1;
658
+ }
659
+
660
+ static int lupb_FileDef_Name(lua_State* L) {
661
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
662
+ lua_pushstring(L, upb_FileDef_Name(f));
663
+ return 1;
664
+ }
665
+
666
+ static int lupb_FileDef_Package(lua_State* L) {
667
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
668
+ lua_pushstring(L, upb_FileDef_Package(f));
669
+ return 1;
670
+ }
671
+
672
+ static int lupb_FileDef_Pool(lua_State* L) {
673
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
674
+ const upb_DefPool* symtab = upb_FileDef_Pool(f);
675
+ lupb_wrapper_pushwrapper(L, 1, symtab, LUPB_SYMTAB);
676
+ return 1;
677
+ }
678
+
679
+ static int lupb_FileDef_Syntax(lua_State* L) {
680
+ const upb_FileDef* f = lupb_FileDef_check(L, 1);
681
+ lua_pushnumber(L, upb_FileDef_Syntax(f));
682
+ return 1;
683
+ }
684
+
685
+ static const struct luaL_Reg lupb_FileDef_m[] = {
686
+ {"dep", lupb_FileDef_Dependency},
687
+ {"depcount", lupb_FileDef_DependencyCount},
688
+ {"enum", lupb_FileDef_enum},
689
+ {"enumcount", lupb_FileDef_enumcount},
690
+ {"msg", lupb_FileDef_msg},
691
+ {"msgcount", lupb_FileDef_msgcount},
692
+ {"name", lupb_FileDef_Name},
693
+ {"package", lupb_FileDef_Package},
694
+ {"symtab", lupb_FileDef_Pool},
695
+ {"syntax", lupb_FileDef_Syntax},
696
+ {NULL, NULL}};
697
+
698
+ /* lupb_DefPool
699
+ * ****************************************************************/
700
+
701
+ /* The symtab owns all defs. Thus GC-rooting the symtab ensures that all
702
+ * underlying defs stay alive.
703
+ *
704
+ * The symtab's userval is a cache of def* -> object. */
705
+
706
+ #define LUPB_CACHE_INDEX 1
707
+
708
+ typedef struct {
709
+ upb_DefPool* symtab;
710
+ } lupb_DefPool;
711
+
712
+ upb_DefPool* lupb_DefPool_check(lua_State* L, int narg) {
713
+ lupb_DefPool* lsymtab = luaL_checkudata(L, narg, LUPB_SYMTAB);
714
+ if (!lsymtab->symtab) {
715
+ luaL_error(L, "called into dead object");
716
+ }
717
+ return lsymtab->symtab;
718
+ }
719
+
720
+ void lupb_DefPool_pushwrapper(lua_State* L, int narg, const void* def,
721
+ const char* type) {
722
+ narg = lua_absindex(L, narg);
723
+ assert(luaL_testudata(L, narg, LUPB_SYMTAB));
724
+
725
+ if (def == NULL) {
726
+ lua_pushnil(L);
727
+ return;
728
+ }
729
+
730
+ lua_getiuservalue(L, narg, LUPB_CACHE_INDEX); /* Get cache. */
731
+
732
+ /* Index by "def" pointer. */
733
+ lua_rawgetp(L, -1, def);
734
+
735
+ /* Stack is now: cache, cached value. */
736
+ if (lua_isnil(L, -1)) {
737
+ /* Create new wrapper. */
738
+ lupb_wrapper* w = lupb_newuserdata(L, sizeof(*w), 1, type);
739
+ w->def = def;
740
+ lua_replace(L, -2); /* Replace nil */
741
+
742
+ /* Set symtab as userval. */
743
+ lua_pushvalue(L, narg);
744
+ lua_setiuservalue(L, -2, LUPB_SYMTAB_INDEX);
745
+
746
+ /* Add wrapper to the the cache. */
747
+ lua_pushvalue(L, -1);
748
+ lua_rawsetp(L, -3, def);
749
+ }
750
+
751
+ lua_replace(L, -2); /* Remove cache, leaving only the wrapper. */
752
+ }
753
+
754
+ /* upb_DefPool_New()
755
+ *
756
+ * Handles:
757
+ * upb.SymbolTable() -> <new instance>
758
+ */
759
+ static int lupb_DefPool_New(lua_State* L) {
760
+ lupb_DefPool* lsymtab = lupb_newuserdata(L, sizeof(*lsymtab), 1, LUPB_SYMTAB);
761
+ lsymtab->symtab = upb_DefPool_New();
762
+
763
+ /* Create our object cache. */
764
+ lua_newtable(L);
765
+
766
+ /* Cache metatable: specifies that values are weak. */
767
+ lua_createtable(L, 0, 1);
768
+ lua_pushstring(L, "v");
769
+ lua_setfield(L, -2, "__mode");
770
+ lua_setmetatable(L, -2);
771
+
772
+ /* Put the symtab itself in the cache metatable. */
773
+ lua_pushvalue(L, -2);
774
+ lua_rawsetp(L, -2, lsymtab->symtab);
775
+
776
+ /* Set the cache as our userval. */
777
+ lua_setiuservalue(L, -2, LUPB_CACHE_INDEX);
778
+
779
+ return 1;
780
+ }
781
+
782
+ static int lupb_DefPool_gc(lua_State* L) {
783
+ lupb_DefPool* lsymtab = luaL_checkudata(L, 1, LUPB_SYMTAB);
784
+ upb_DefPool_Free(lsymtab->symtab);
785
+ lsymtab->symtab = NULL;
786
+ return 0;
787
+ }
788
+
789
+ static int lupb_DefPool_AddFile(lua_State* L) {
790
+ size_t len;
791
+ upb_DefPool* s = lupb_DefPool_check(L, 1);
792
+ const char* str = luaL_checklstring(L, 2, &len);
793
+ upb_Arena* arena = lupb_Arena_pushnew(L);
794
+ const google_protobuf_FileDescriptorProto* file;
795
+ const upb_FileDef* file_def;
796
+ upb_Status status;
797
+
798
+ upb_Status_Clear(&status);
799
+ file = google_protobuf_FileDescriptorProto_parse(str, len, arena);
800
+
801
+ if (!file) {
802
+ luaL_argerror(L, 2, "failed to parse descriptor");
803
+ }
804
+
805
+ file_def = upb_DefPool_AddFile(s, file, &status);
806
+ lupb_checkstatus(L, &status);
807
+
808
+ lupb_DefPool_pushwrapper(L, 1, file_def, LUPB_FILEDEF);
809
+
810
+ return 1;
811
+ }
812
+
813
+ static int lupb_DefPool_addset(lua_State* L) {
814
+ size_t i, n, len;
815
+ const google_protobuf_FileDescriptorProto* const* files;
816
+ google_protobuf_FileDescriptorSet* set;
817
+ upb_DefPool* s = lupb_DefPool_check(L, 1);
818
+ const char* str = luaL_checklstring(L, 2, &len);
819
+ upb_Arena* arena = lupb_Arena_pushnew(L);
820
+ upb_Status status;
821
+
822
+ upb_Status_Clear(&status);
823
+ set = google_protobuf_FileDescriptorSet_parse(str, len, arena);
824
+
825
+ if (!set) {
826
+ luaL_argerror(L, 2, "failed to parse descriptor");
827
+ }
828
+
829
+ files = google_protobuf_FileDescriptorSet_file(set, &n);
830
+ for (i = 0; i < n; i++) {
831
+ upb_DefPool_AddFile(s, files[i], &status);
832
+ lupb_checkstatus(L, &status);
833
+ }
834
+
835
+ return 0;
836
+ }
837
+
838
+ static int lupb_DefPool_FindMessageByName(lua_State* L) {
839
+ const upb_DefPool* s = lupb_DefPool_check(L, 1);
840
+ const upb_MessageDef* m =
841
+ upb_DefPool_FindMessageByName(s, luaL_checkstring(L, 2));
842
+ lupb_DefPool_pushwrapper(L, 1, m, LUPB_MSGDEF);
843
+ return 1;
844
+ }
845
+
846
+ static int lupb_DefPool_FindEnumByName(lua_State* L) {
847
+ const upb_DefPool* s = lupb_DefPool_check(L, 1);
848
+ const upb_EnumDef* e = upb_DefPool_FindEnumByName(s, luaL_checkstring(L, 2));
849
+ lupb_DefPool_pushwrapper(L, 1, e, LUPB_ENUMDEF);
850
+ return 1;
851
+ }
852
+
853
+ static int lupb_DefPool_FindEnumByNameval(lua_State* L) {
854
+ const upb_DefPool* s = lupb_DefPool_check(L, 1);
855
+ const upb_EnumValueDef* e =
856
+ upb_DefPool_FindEnumByNameval(s, luaL_checkstring(L, 2));
857
+ lupb_DefPool_pushwrapper(L, 1, e, LUPB_ENUMVALDEF);
858
+ return 1;
859
+ }
860
+
861
+ static int lupb_DefPool_tostring(lua_State* L) {
862
+ lua_pushfstring(L, "<upb.SymbolTable>");
863
+ return 1;
864
+ }
865
+
866
+ static const struct luaL_Reg lupb_DefPool_m[] = {
867
+ {"add_file", lupb_DefPool_AddFile},
868
+ {"add_set", lupb_DefPool_addset},
869
+ {"lookup_msg", lupb_DefPool_FindMessageByName},
870
+ {"lookup_enum", lupb_DefPool_FindEnumByName},
871
+ {"lookup_enumval", lupb_DefPool_FindEnumByNameval},
872
+ {NULL, NULL}};
873
+
874
+ static const struct luaL_Reg lupb_DefPool_mm[] = {
875
+ {"__gc", lupb_DefPool_gc},
876
+ {"__tostring", lupb_DefPool_tostring},
877
+ {NULL, NULL}};
878
+
879
+ /* lupb toplevel **************************************************************/
880
+
881
+ static void lupb_setfieldi(lua_State* L, const char* field, int i) {
882
+ lua_pushinteger(L, i);
883
+ lua_setfield(L, -2, field);
884
+ }
885
+
886
+ static const struct luaL_Reg lupbdef_toplevel_m[] = {
887
+ {"SymbolTable", lupb_DefPool_New}, {NULL, NULL}};
888
+
889
+ void lupb_def_registertypes(lua_State* L) {
890
+ lupb_setfuncs(L, lupbdef_toplevel_m);
891
+
892
+ /* Register types. */
893
+ lupb_register_type(L, LUPB_ENUMDEF, lupb_EnumDef_m, lupb_EnumDef_mm);
894
+ lupb_register_type(L, LUPB_ENUMVALDEF, lupb_enumvaldef_m, NULL);
895
+ lupb_register_type(L, LUPB_FIELDDEF, lupb_FieldDef_m, NULL);
896
+ lupb_register_type(L, LUPB_FILEDEF, lupb_FileDef_m, NULL);
897
+ lupb_register_type(L, LUPB_MSGDEF, lupb_MessageDef_m, lupb_MessageDef_mm);
898
+ lupb_register_type(L, LUPB_ONEOFDEF, lupb_OneofDef_m, lupb_OneofDef_mm);
899
+ lupb_register_type(L, LUPB_SYMTAB, lupb_DefPool_m, lupb_DefPool_mm);
900
+
901
+ /* Register constants. */
902
+ lupb_setfieldi(L, "LABEL_OPTIONAL", kUpb_Label_Optional);
903
+ lupb_setfieldi(L, "LABEL_REQUIRED", kUpb_Label_Required);
904
+ lupb_setfieldi(L, "LABEL_REPEATED", kUpb_Label_Repeated);
905
+
906
+ lupb_setfieldi(L, "TYPE_DOUBLE", kUpb_CType_Double);
907
+ lupb_setfieldi(L, "TYPE_FLOAT", kUpb_CType_Float);
908
+ lupb_setfieldi(L, "TYPE_INT64", kUpb_CType_Int64);
909
+ lupb_setfieldi(L, "TYPE_UINT64", kUpb_CType_UInt64);
910
+ lupb_setfieldi(L, "TYPE_INT32", kUpb_CType_Int32);
911
+ lupb_setfieldi(L, "TYPE_BOOL", kUpb_CType_Bool);
912
+ lupb_setfieldi(L, "TYPE_STRING", kUpb_CType_String);
913
+ lupb_setfieldi(L, "TYPE_MESSAGE", kUpb_CType_Message);
914
+ lupb_setfieldi(L, "TYPE_BYTES", kUpb_CType_Bytes);
915
+ lupb_setfieldi(L, "TYPE_UINT32", kUpb_CType_UInt32);
916
+ lupb_setfieldi(L, "TYPE_ENUM", kUpb_CType_Enum);
917
+
918
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_DOUBLE", kUpb_FieldType_Double);
919
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_FLOAT", kUpb_FieldType_Float);
920
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_INT64", kUpb_FieldType_Int64);
921
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_UINT64", kUpb_FieldType_UInt64);
922
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_INT32", kUpb_FieldType_Int32);
923
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_FIXED64", kUpb_FieldType_Fixed64);
924
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_FIXED32", kUpb_FieldType_Fixed32);
925
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_BOOL", kUpb_FieldType_Bool);
926
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_STRING", kUpb_FieldType_String);
927
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_GROUP", kUpb_FieldType_Group);
928
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_MESSAGE", kUpb_FieldType_Message);
929
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_BYTES", kUpb_FieldType_Bytes);
930
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_UINT32", kUpb_FieldType_UInt32);
931
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_ENUM", kUpb_FieldType_Enum);
932
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_SFIXED32", kUpb_FieldType_SFixed32);
933
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_SFIXED64", kUpb_FieldType_SFixed64);
934
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_SINT32", kUpb_FieldType_SInt32);
935
+ lupb_setfieldi(L, "DESCRIPTOR_TYPE_SINT64", kUpb_FieldType_SInt64);
936
+
937
+ lupb_setfieldi(L, "SYNTAX_PROTO2", kUpb_Syntax_Proto2);
938
+ lupb_setfieldi(L, "SYNTAX_PROTO3", kUpb_Syntax_Proto3);
939
+ }