ruby_memprofiler_pprof 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/ruby_memprofiler_pprof/backtrace.c +429 -0
- data/ext/ruby_memprofiler_pprof/collector.c +1055 -0
- data/ext/ruby_memprofiler_pprof/compat.c +182 -0
- data/ext/ruby_memprofiler_pprof/extconf.rb +72 -0
- data/ext/ruby_memprofiler_pprof/pprof.upb.c +170 -0
- data/ext/ruby_memprofiler_pprof/pprof.upb.h +848 -0
- data/ext/ruby_memprofiler_pprof/pprof_out.c +285 -0
- data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.c +11 -0
- data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.h +301 -0
- data/ext/ruby_memprofiler_pprof/strtab.c +391 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/BUILD +719 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/CONTRIBUTING.md +37 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/DESIGN.md +201 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/LICENSE +26 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/README.md +78 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/WORKSPACE +58 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/BUILD +53 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/amalgamate.py +129 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/build_defs.bzl +160 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/lua.BUILD +127 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/protobuf.patch +54 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/py_proto_library.bzl +137 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/python_downloads.bzl +84 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/system_python.bzl +101 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/upb_proto_library.bzl +388 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/workspace_deps.bzl +89 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD +252 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD.googleapis +54 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/benchmark.cc +333 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/build_defs.bzl +88 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/compare.py +118 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor.proto +888 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor_sv.proto +890 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/empty.proto +6 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_protobuf_binary_cc.py +64 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_synthetic_protos.py +118 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_upb_binary_c.py +65 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/BUILD.bazel +102 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/README.md +23 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/build_defs.bzl +73 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/make_cmakelists.py +340 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test.py +57 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test_lib.py +186 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/render.py +43 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/style-guide.md +65 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/vs-cpp-protos.md +255 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/wrapping-upb.md +444 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/BUILD +216 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.c +394 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.h +63 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.c +1694 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.h +80 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.c +704 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.h +114 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.c +650 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.h +48 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/BUILD.bazel +193 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/dist.bzl +190 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.c +247 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.h +39 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.c +522 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.h +66 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.c +1909 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.h +101 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/minimal_test.py +183 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/BUILD +70 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/README.md +11 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_database_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_pool_test_wrapper.py +45 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_test_wrapper.py +46 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/generator_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/json_format_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/keywords_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_factory_test_wrapper.py +37 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_test_wrapper.py +52 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/proto_builder_test_wrapper.py +32 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/pyproto_test_wrapper.bzl +36 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/reflection_test_wrapper.py +45 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/service_reflection_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/symbol_database_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_encoding_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_format_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/unknown_fields_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/well_known_types_test_wrapper.py +36 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/wire_format_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.c +350 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.h +230 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/py_extension.bzl +55 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/python_api.h +61 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.c +828 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.h +69 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.c +404 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.h +39 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/version_script.lds +6 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/LICENSE +32 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/README.google +9 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/console.lua +156 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/lunit.lua +725 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/BUILD +19 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/LICENSE +21 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/naive.c +92 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-neon.c +157 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-sse.c +170 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/utf8_range.h +9 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/BUILD.bazel +129 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/README.md +8 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/def.c +939 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/lua_proto_library.bzl +138 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/main.c +83 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/msg.c +1118 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test.proto +69 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test_upb.lua +846 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.c +258 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.h +132 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.lua +58 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upbc.cc +134 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.c +192 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.h +174 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb.c +346 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb_failures.txt +1 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.c +1221 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.h +94 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.c +1055 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.h +153 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_internal.h +211 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.c +3262 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.h +414 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.hpp +438 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/empty.proto +1 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.c +604 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.h +71 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/BUILD +13 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/file_descriptor_parsenew_fuzzer.cc +43 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.c +1509 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.h +47 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.c +776 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.h +62 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.c +1147 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.h +189 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.hpp +112 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.c +363 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.h +263 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_internal.h +59 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_test.cc +425 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_test.cc +230 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.c +428 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.h +114 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_internal.h +836 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.cc +491 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.proto +195 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_def.inc +261 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_undef.inc +62 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.c +323 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.h +109 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.hpp +37 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table.c +926 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table_internal.h +385 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test.proto +74 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.cc +186 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.proto +12 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_generated_code.cc +977 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_table.cc +580 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.c +472 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.h +64 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.c +362 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.h +378 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.hpp +115 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb_internal.h +68 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/BUILD +121 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/README.md +7 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.c +300 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.h +66 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare_test.cc +236 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.c +572 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.h +62 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_public_import_test.proto +32 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_regular_import_test.proto +36 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.cc +143 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.proto +119 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_weak_import_test.proto +28 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_wweak_import_test.proto +28 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.c +311 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.h +94 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.cc +202 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.proto +48 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/BUILD +78 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.cc +77 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.h +112 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upb.cc +1997 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upbdefs.cc +193 -0
- data/lib/ruby_memprofiler_pprof/atfork.rb +77 -0
- data/lib/ruby_memprofiler_pprof/block_flusher.rb +61 -0
- data/lib/ruby_memprofiler_pprof/file_flusher.rb +45 -0
- data/lib/ruby_memprofiler_pprof/profile_app.rb +30 -0
- data/lib/ruby_memprofiler_pprof/profile_data.rb +18 -0
- data/lib/ruby_memprofiler_pprof/version.rb +5 -0
- data/lib/ruby_memprofiler_pprof.rb +8 -0
- data/libexec/ruby_memprofiler_pprof_profile +16 -0
- metadata +257 -0
@@ -0,0 +1,258 @@
|
|
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
|
+
/*
|
29
|
+
* require("lua") -- A Lua extension for upb.
|
30
|
+
*
|
31
|
+
* Exposes only the core library
|
32
|
+
* (sub-libraries are exposed in other extensions).
|
33
|
+
*
|
34
|
+
* 64-bit woes: Lua can only represent numbers of type lua_Number (which is
|
35
|
+
* double unless the user specifically overrides this). Doubles can represent
|
36
|
+
* the entire range of 64-bit integers, but lose precision once the integers are
|
37
|
+
* greater than 2^53.
|
38
|
+
*
|
39
|
+
* Lua 5.3 is adding support for integers, which will allow for 64-bit
|
40
|
+
* integers (which can be interpreted as signed or unsigned).
|
41
|
+
*
|
42
|
+
* LuaJIT supports 64-bit signed and unsigned boxed representations
|
43
|
+
* through its "cdata" mechanism, but this is not portable to regular Lua.
|
44
|
+
*
|
45
|
+
* Hopefully Lua 5.3 will come soon enough that we can either use Lua 5.3
|
46
|
+
* integer support or LuaJIT 64-bit cdata for users that need the entire
|
47
|
+
* domain of [u]int64 values.
|
48
|
+
*/
|
49
|
+
|
50
|
+
#include "upb/bindings/lua/upb.h"
|
51
|
+
|
52
|
+
#include <float.h>
|
53
|
+
#include <math.h>
|
54
|
+
#include <stdint.h>
|
55
|
+
#include <stdlib.h>
|
56
|
+
#include <string.h>
|
57
|
+
|
58
|
+
#include "lauxlib.h"
|
59
|
+
#include "upb/msg.h"
|
60
|
+
|
61
|
+
/* Lua compatibility code *****************************************************/
|
62
|
+
|
63
|
+
/* Shims for upcoming Lua 5.3 functionality. */
|
64
|
+
static bool lua_isinteger(lua_State* L, int argn) {
|
65
|
+
LUPB_UNUSED(L);
|
66
|
+
LUPB_UNUSED(argn);
|
67
|
+
return false;
|
68
|
+
}
|
69
|
+
|
70
|
+
/* Utility functions **********************************************************/
|
71
|
+
|
72
|
+
void lupb_checkstatus(lua_State* L, upb_Status* s) {
|
73
|
+
if (!upb_Status_IsOk(s)) {
|
74
|
+
lua_pushstring(L, upb_Status_ErrorMessage(s));
|
75
|
+
lua_error(L);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
/* Pushes a new userdata with the given metatable. */
|
80
|
+
void* lupb_newuserdata(lua_State* L, size_t size, int n, const char* type) {
|
81
|
+
#if LUA_VERSION_NUM >= 504
|
82
|
+
void* ret = lua_newuserdatauv(L, size, n);
|
83
|
+
#else
|
84
|
+
void* ret = lua_newuserdata(L, size);
|
85
|
+
lua_createtable(L, 0, n);
|
86
|
+
lua_setuservalue(L, -2);
|
87
|
+
#endif
|
88
|
+
|
89
|
+
/* Set metatable. */
|
90
|
+
luaL_getmetatable(L, type);
|
91
|
+
assert(!lua_isnil(L, -1)); /* Should have been created by luaopen_upb. */
|
92
|
+
lua_setmetatable(L, -2);
|
93
|
+
|
94
|
+
return ret;
|
95
|
+
}
|
96
|
+
|
97
|
+
#if LUA_VERSION_NUM < 504
|
98
|
+
int lua_setiuservalue(lua_State* L, int index, int n) {
|
99
|
+
lua_getuservalue(L, index);
|
100
|
+
lua_insert(L, -2);
|
101
|
+
lua_rawseti(L, -2, n);
|
102
|
+
lua_pop(L, 1);
|
103
|
+
return 1;
|
104
|
+
}
|
105
|
+
|
106
|
+
int lua_getiuservalue(lua_State* L, int index, int n) {
|
107
|
+
lua_getuservalue(L, index);
|
108
|
+
lua_rawgeti(L, -1, n);
|
109
|
+
lua_replace(L, -2);
|
110
|
+
return 1;
|
111
|
+
}
|
112
|
+
#endif
|
113
|
+
|
114
|
+
/* We use this function as the __index metamethod when a type has both methods
|
115
|
+
* and an __index metamethod. */
|
116
|
+
int lupb_indexmm(lua_State* L) {
|
117
|
+
/* Look up in __index table (which is a closure param). */
|
118
|
+
lua_pushvalue(L, 2);
|
119
|
+
lua_rawget(L, lua_upvalueindex(1));
|
120
|
+
if (!lua_isnil(L, -1)) {
|
121
|
+
return 1;
|
122
|
+
}
|
123
|
+
|
124
|
+
/* Not found, chain to user __index metamethod. */
|
125
|
+
lua_pushvalue(L, lua_upvalueindex(2));
|
126
|
+
lua_pushvalue(L, 1);
|
127
|
+
lua_pushvalue(L, 2);
|
128
|
+
lua_call(L, 2, 1);
|
129
|
+
return 1;
|
130
|
+
}
|
131
|
+
|
132
|
+
void lupb_register_type(lua_State* L, const char* name, const luaL_Reg* m,
|
133
|
+
const luaL_Reg* mm) {
|
134
|
+
luaL_newmetatable(L, name);
|
135
|
+
|
136
|
+
if (mm) {
|
137
|
+
lupb_setfuncs(L, mm);
|
138
|
+
}
|
139
|
+
|
140
|
+
if (m) {
|
141
|
+
lua_createtable(L, 0, 0); /* __index table */
|
142
|
+
lupb_setfuncs(L, m);
|
143
|
+
|
144
|
+
/* Methods go in the mt's __index slot. If the user also specified an
|
145
|
+
* __index metamethod, use our custom lupb_indexmm() that can check both. */
|
146
|
+
lua_getfield(L, -2, "__index");
|
147
|
+
if (lua_isnil(L, -1)) {
|
148
|
+
lua_pop(L, 1);
|
149
|
+
} else {
|
150
|
+
lua_pushcclosure(L, &lupb_indexmm, 2);
|
151
|
+
}
|
152
|
+
lua_setfield(L, -2, "__index");
|
153
|
+
}
|
154
|
+
|
155
|
+
lua_pop(L, 1); /* The mt. */
|
156
|
+
}
|
157
|
+
|
158
|
+
/* Scalar type mapping ********************************************************/
|
159
|
+
|
160
|
+
/* Functions that convert scalar/primitive values (numbers, strings, bool)
|
161
|
+
* between Lua and C/upb. Handles type/range checking. */
|
162
|
+
|
163
|
+
bool lupb_checkbool(lua_State* L, int narg) {
|
164
|
+
if (!lua_isboolean(L, narg)) {
|
165
|
+
luaL_error(L, "must be true or false");
|
166
|
+
}
|
167
|
+
return lua_toboolean(L, narg);
|
168
|
+
}
|
169
|
+
|
170
|
+
/* Unlike luaL_checkstring(), this does not allow implicit conversion to
|
171
|
+
* string. */
|
172
|
+
const char* lupb_checkstring(lua_State* L, int narg, size_t* len) {
|
173
|
+
if (lua_type(L, narg) != LUA_TSTRING) {
|
174
|
+
luaL_error(L, "Expected string");
|
175
|
+
}
|
176
|
+
|
177
|
+
return lua_tolstring(L, narg, len);
|
178
|
+
}
|
179
|
+
|
180
|
+
/* Unlike luaL_checkinteger, these do not implicitly convert from string or
|
181
|
+
* round an existing double value. We allow floating-point input, but only if
|
182
|
+
* the actual value is integral. */
|
183
|
+
#define INTCHECK(type, ctype, min, max) \
|
184
|
+
ctype lupb_check##type(lua_State* L, int narg) { \
|
185
|
+
double n; \
|
186
|
+
if (lua_isinteger(L, narg)) { \
|
187
|
+
return lua_tointeger(L, narg); \
|
188
|
+
} \
|
189
|
+
\
|
190
|
+
/* Prevent implicit conversion from string. */ \
|
191
|
+
luaL_checktype(L, narg, LUA_TNUMBER); \
|
192
|
+
n = lua_tonumber(L, narg); \
|
193
|
+
\
|
194
|
+
/* Check this double has no fractional part and remains in bounds. \
|
195
|
+
* Consider INT64_MIN and INT64_MAX: \
|
196
|
+
* 1. INT64_MIN -(2^63) is a power of 2, so this converts to a double. \
|
197
|
+
* 2. INT64_MAX (2^63 - 1) is not a power of 2, and conversion of \
|
198
|
+
* out-of-range integer values to a double can lead to undefined behavior. \
|
199
|
+
* On some compilers, this conversion can return 0, but it also can return \
|
200
|
+
* the max value. To deal with this, we can first divide by 2 to prevent \
|
201
|
+
* the overflow, multiply it back, and add 1 to find the true limit. */ \
|
202
|
+
double i; \
|
203
|
+
double max_value = (((double)max / 2) * 2) + 1; \
|
204
|
+
if ((modf(n, &i) != 0.0) || n < min || n >= max_value) { \
|
205
|
+
luaL_error(L, "number %f was not an integer or out of range for " #type, \
|
206
|
+
n); \
|
207
|
+
} \
|
208
|
+
return (ctype)n; \
|
209
|
+
} \
|
210
|
+
void lupb_push##type(lua_State* L, ctype val) { \
|
211
|
+
/* TODO: push integer for Lua >= 5.3, 64-bit cdata for LuaJIT. */ \
|
212
|
+
/* This is lossy for some [u]int64 values, which isn't great, but */ \
|
213
|
+
/* crashing when we encounter these values seems worse. */ \
|
214
|
+
lua_pushnumber(L, val); \
|
215
|
+
}
|
216
|
+
|
217
|
+
INTCHECK(int64, int64_t, INT64_MIN, INT64_MAX)
|
218
|
+
INTCHECK(int32, int32_t, INT32_MIN, INT32_MAX)
|
219
|
+
INTCHECK(uint64, uint64_t, 0, UINT64_MAX)
|
220
|
+
INTCHECK(uint32, uint32_t, 0, UINT32_MAX)
|
221
|
+
|
222
|
+
double lupb_checkdouble(lua_State* L, int narg) {
|
223
|
+
/* If we were being really hard-nosed here, we'd check whether the input was
|
224
|
+
* an integer that has no precise double representation. But doubles aren't
|
225
|
+
* generally expected to be exact like integers are, and worse this could
|
226
|
+
* cause data-dependent runtime errors: one run of the program could work fine
|
227
|
+
* because the integer calculations happened to be exactly representable in
|
228
|
+
* double, while the next could crash because of subtly different input. */
|
229
|
+
|
230
|
+
luaL_checktype(L, narg, LUA_TNUMBER); /* lua_tonumber() auto-converts. */
|
231
|
+
return lua_tonumber(L, narg);
|
232
|
+
}
|
233
|
+
|
234
|
+
float lupb_checkfloat(lua_State* L, int narg) {
|
235
|
+
/* We don't worry about checking whether the input can be exactly converted to
|
236
|
+
* float -- see above. */
|
237
|
+
|
238
|
+
luaL_checktype(L, narg, LUA_TNUMBER); /* lua_tonumber() auto-converts. */
|
239
|
+
return lua_tonumber(L, narg);
|
240
|
+
}
|
241
|
+
|
242
|
+
void lupb_pushdouble(lua_State* L, double d) { lua_pushnumber(L, d); }
|
243
|
+
|
244
|
+
void lupb_pushfloat(lua_State* L, float d) { lua_pushnumber(L, d); }
|
245
|
+
|
246
|
+
/* Library entry point ********************************************************/
|
247
|
+
|
248
|
+
int luaopen_lupb(lua_State* L) {
|
249
|
+
#if LUA_VERSION_NUM == 501
|
250
|
+
const struct luaL_Reg funcs[] = {{NULL, NULL}};
|
251
|
+
luaL_register(L, "upb_c", funcs);
|
252
|
+
#else
|
253
|
+
lua_createtable(L, 0, 8);
|
254
|
+
#endif
|
255
|
+
lupb_def_registertypes(L);
|
256
|
+
lupb_msg_registertypes(L);
|
257
|
+
return 1; /* Return package table. */
|
258
|
+
}
|
@@ -0,0 +1,132 @@
|
|
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
|
+
/*
|
29
|
+
* Shared definitions for upb Lua modules.
|
30
|
+
*/
|
31
|
+
|
32
|
+
#ifndef UPB_LUA_UPB_H_
|
33
|
+
#define UPB_LUA_UPB_H_
|
34
|
+
|
35
|
+
#include "lauxlib.h"
|
36
|
+
#include "upb/def.h"
|
37
|
+
#include "upb/msg.h"
|
38
|
+
#include "upb/reflection.h"
|
39
|
+
|
40
|
+
/* Lua changes its API in incompatible ways in every minor release.
|
41
|
+
* This is some shim code to paper over the differences. */
|
42
|
+
|
43
|
+
#if LUA_VERSION_NUM == 501
|
44
|
+
#define lua_rawlen lua_objlen
|
45
|
+
#define lua_setuservalue(L, idx) lua_setfenv(L, idx)
|
46
|
+
#define lua_getuservalue(L, idx) lua_getfenv(L, idx)
|
47
|
+
#define lupb_setfuncs(L, l) luaL_register(L, NULL, l)
|
48
|
+
#elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504
|
49
|
+
#define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0)
|
50
|
+
#else
|
51
|
+
#error Only Lua 5.1-5.4 are supported
|
52
|
+
#endif
|
53
|
+
|
54
|
+
/* Create a new userdata with the given type and |n| uservals, which are popped
|
55
|
+
* from the stack to initialize the userdata. */
|
56
|
+
void* lupb_newuserdata(lua_State* L, size_t size, int n, const char* type);
|
57
|
+
|
58
|
+
#if LUA_VERSION_NUM < 504
|
59
|
+
/* Polyfills for this Lua 5.4 function. Pushes userval |n| for the userdata at
|
60
|
+
* |index|. */
|
61
|
+
int lua_setiuservalue(lua_State* L, int index, int n);
|
62
|
+
int lua_getiuservalue(lua_State* L, int index, int n);
|
63
|
+
#endif
|
64
|
+
|
65
|
+
/* Registers a type with the given name, methods, and metamethods. */
|
66
|
+
void lupb_register_type(lua_State* L, const char* name, const luaL_Reg* m,
|
67
|
+
const luaL_Reg* mm);
|
68
|
+
|
69
|
+
/* Checks the given upb_Status and throws a Lua error if it is not ok. */
|
70
|
+
void lupb_checkstatus(lua_State* L, upb_Status* s);
|
71
|
+
|
72
|
+
int luaopen_lupb(lua_State* L);
|
73
|
+
|
74
|
+
/* C <-> Lua value conversions. ***********************************************/
|
75
|
+
|
76
|
+
/* Custom check/push functions. Unlike the Lua equivalents, they are pinned to
|
77
|
+
* specific C types (instead of lua_Number, etc), and do not allow any implicit
|
78
|
+
* conversion or data loss. */
|
79
|
+
int64_t lupb_checkint64(lua_State* L, int narg);
|
80
|
+
int32_t lupb_checkint32(lua_State* L, int narg);
|
81
|
+
uint64_t lupb_checkuint64(lua_State* L, int narg);
|
82
|
+
uint32_t lupb_checkuint32(lua_State* L, int narg);
|
83
|
+
double lupb_checkdouble(lua_State* L, int narg);
|
84
|
+
float lupb_checkfloat(lua_State* L, int narg);
|
85
|
+
bool lupb_checkbool(lua_State* L, int narg);
|
86
|
+
const char* lupb_checkstring(lua_State* L, int narg, size_t* len);
|
87
|
+
const char* lupb_checkname(lua_State* L, int narg);
|
88
|
+
|
89
|
+
void lupb_pushint64(lua_State* L, int64_t val);
|
90
|
+
void lupb_pushint32(lua_State* L, int32_t val);
|
91
|
+
void lupb_pushuint64(lua_State* L, uint64_t val);
|
92
|
+
void lupb_pushuint32(lua_State* L, uint32_t val);
|
93
|
+
|
94
|
+
/** From def.c. ***************************************************************/
|
95
|
+
|
96
|
+
const upb_MessageDef* lupb_MessageDef_check(lua_State* L, int narg);
|
97
|
+
const upb_EnumDef* lupb_EnumDef_check(lua_State* L, int narg);
|
98
|
+
const upb_FieldDef* lupb_FieldDef_check(lua_State* L, int narg);
|
99
|
+
upb_DefPool* lupb_DefPool_check(lua_State* L, int narg);
|
100
|
+
void lupb_MessageDef_pushsubmsgdef(lua_State* L, const upb_FieldDef* f);
|
101
|
+
|
102
|
+
void lupb_def_registertypes(lua_State* L);
|
103
|
+
|
104
|
+
/** From msg.c. ***************************************************************/
|
105
|
+
|
106
|
+
void lupb_pushmsgval(lua_State* L, int container, upb_CType type,
|
107
|
+
upb_MessageValue val);
|
108
|
+
int lupb_MessageDef_call(lua_State* L);
|
109
|
+
upb_Arena* lupb_Arena_pushnew(lua_State* L);
|
110
|
+
|
111
|
+
void lupb_msg_registertypes(lua_State* L);
|
112
|
+
|
113
|
+
#define lupb_assert(L, predicate) \
|
114
|
+
if (!(predicate)) \
|
115
|
+
luaL_error(L, "internal error: %s, %s:%d ", #predicate, __FILE__, __LINE__);
|
116
|
+
|
117
|
+
#define LUPB_UNUSED(var) (void)var
|
118
|
+
|
119
|
+
#if defined(__GNUC__) || defined(__clang__)
|
120
|
+
#define LUPB_UNREACHABLE() \
|
121
|
+
do { \
|
122
|
+
assert(0); \
|
123
|
+
__builtin_unreachable(); \
|
124
|
+
} while (0)
|
125
|
+
#else
|
126
|
+
#define LUPB_UNREACHABLE() \
|
127
|
+
do { \
|
128
|
+
assert(0); \
|
129
|
+
} while (0)
|
130
|
+
#endif
|
131
|
+
|
132
|
+
#endif /* UPB_LUA_UPB_H_ */
|
@@ -0,0 +1,58 @@
|
|
1
|
+
--[[--------------------------------------------------------------------------
|
2
|
+
|
3
|
+
Copyright (c) 2009-2021, Google LLC
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
8
|
+
* Redistributions of source code must retain the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
10
|
+
* Redistributions in binary form must reproduce the above copyright
|
11
|
+
notice, this list of conditions and the following disclaimer in the
|
12
|
+
documentation and/or other materials provided with the distribution.
|
13
|
+
* Neither the name of Google LLC nor the
|
14
|
+
names of its contributors may be used to endorse or promote products
|
15
|
+
derived from this software without specific prior written permission.
|
16
|
+
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
18
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
|
21
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
23
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
24
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
|
28
|
+
--]]--------------------------------------------------------------------------
|
29
|
+
|
30
|
+
local upb = require("lupb")
|
31
|
+
|
32
|
+
upb.generated_pool = upb.SymbolTable()
|
33
|
+
|
34
|
+
local module_metatable = {
|
35
|
+
__index = function(t, k)
|
36
|
+
local package = t._filedef:package()
|
37
|
+
if package then
|
38
|
+
k = package .. "." .. k
|
39
|
+
end
|
40
|
+
local pool = upb.generated_pool
|
41
|
+
local def = pool:lookup_msg(k) or pool:lookup_enum(k)
|
42
|
+
local v = nil
|
43
|
+
if def and def:file():name() == t._filedef:name() then
|
44
|
+
v = def
|
45
|
+
t[k] = v
|
46
|
+
end
|
47
|
+
return v
|
48
|
+
end
|
49
|
+
}
|
50
|
+
|
51
|
+
function upb._generated_module(desc_string)
|
52
|
+
local file = upb.generated_pool:add_file(desc_string)
|
53
|
+
local module = {_filedef = file}
|
54
|
+
setmetatable(module, module_metatable)
|
55
|
+
return module
|
56
|
+
end
|
57
|
+
|
58
|
+
return upb
|
@@ -0,0 +1,134 @@
|
|
1
|
+
// Copyright (c) 2009-2021, Google LLC
|
2
|
+
// All rights reserved.
|
3
|
+
//
|
4
|
+
// Redistribution and use in source and binary forms, with or without
|
5
|
+
// modification, are permitted provided that the following conditions are met:
|
6
|
+
// * Redistributions of source code must retain the above copyright
|
7
|
+
// notice, this list of conditions and the following disclaimer.
|
8
|
+
// * Redistributions in binary form must reproduce the above copyright
|
9
|
+
// notice, this list of conditions and the following disclaimer in the
|
10
|
+
// documentation and/or other materials provided with the distribution.
|
11
|
+
// * Neither the name of Google LLC nor the
|
12
|
+
// names of its contributors may be used to endorse or promote products
|
13
|
+
// derived from this software without specific prior written permission.
|
14
|
+
//
|
15
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16
|
+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17
|
+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
18
|
+
// ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
|
19
|
+
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
20
|
+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
21
|
+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
22
|
+
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
24
|
+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25
|
+
|
26
|
+
#include "google/protobuf/compiler/code_generator.h"
|
27
|
+
#include "google/protobuf/compiler/plugin.h"
|
28
|
+
#include "google/protobuf/io/printer.h"
|
29
|
+
#include "google/protobuf/descriptor.pb.h"
|
30
|
+
#include "google/protobuf/descriptor.h"
|
31
|
+
#include "absl/strings/str_replace.h"
|
32
|
+
#include "absl/strings/string_view.h"
|
33
|
+
#include "absl/strings/substitute.h"
|
34
|
+
|
35
|
+
namespace protoc = ::google::protobuf::compiler;
|
36
|
+
namespace protobuf = ::google::protobuf;
|
37
|
+
|
38
|
+
class LuaGenerator : public protoc::CodeGenerator {
|
39
|
+
bool Generate(const protobuf::FileDescriptor* file,
|
40
|
+
const std::string& parameter, protoc::GeneratorContext* context,
|
41
|
+
std::string* error) const override;
|
42
|
+
};
|
43
|
+
|
44
|
+
static std::string StripExtension(absl::string_view fname) {
|
45
|
+
size_t lastdot = fname.find_last_of('.');
|
46
|
+
if (lastdot == std::string::npos) {
|
47
|
+
return std::string(fname);
|
48
|
+
}
|
49
|
+
return std::string(fname.substr(0, lastdot));
|
50
|
+
}
|
51
|
+
|
52
|
+
static std::string Filename(const protobuf::FileDescriptor* file) {
|
53
|
+
return StripExtension(file->name()) + "_pb.lua";
|
54
|
+
}
|
55
|
+
|
56
|
+
static std::string ModuleName(const protobuf::FileDescriptor* file) {
|
57
|
+
std::string ret = StripExtension(file->name()) + "_pb";
|
58
|
+
return absl::StrReplaceAll(ret, {{"/", "."}});
|
59
|
+
}
|
60
|
+
|
61
|
+
static void PrintHexDigit(char digit, protobuf::io::Printer* printer) {
|
62
|
+
char text;
|
63
|
+
if (digit < 10) {
|
64
|
+
text = '0' + digit;
|
65
|
+
} else {
|
66
|
+
text = 'A' + (digit - 10);
|
67
|
+
}
|
68
|
+
printer->WriteRaw(&text, 1);
|
69
|
+
}
|
70
|
+
|
71
|
+
static void PrintString(int max_cols, absl::string_view* str,
|
72
|
+
protobuf::io::Printer* printer) {
|
73
|
+
printer->Print("\'");
|
74
|
+
while (max_cols > 0 && !str->empty()) {
|
75
|
+
char ch = (*str)[0];
|
76
|
+
if (ch == '\\') {
|
77
|
+
printer->PrintRaw("\\\\");
|
78
|
+
max_cols--;
|
79
|
+
} else if (ch == '\'') {
|
80
|
+
printer->PrintRaw("\\'");
|
81
|
+
max_cols--;
|
82
|
+
} else if (isprint(ch)) {
|
83
|
+
printer->WriteRaw(&ch, 1);
|
84
|
+
max_cols--;
|
85
|
+
} else {
|
86
|
+
unsigned char byte = ch;
|
87
|
+
printer->PrintRaw("\\x");
|
88
|
+
PrintHexDigit(byte >> 4, printer);
|
89
|
+
PrintHexDigit(byte & 15, printer);
|
90
|
+
max_cols -= 4;
|
91
|
+
}
|
92
|
+
str->remove_prefix(1);
|
93
|
+
}
|
94
|
+
printer->Print("\'");
|
95
|
+
}
|
96
|
+
|
97
|
+
bool LuaGenerator::Generate(const protobuf::FileDescriptor* file,
|
98
|
+
const std::string& /* parameter */,
|
99
|
+
protoc::GeneratorContext* context,
|
100
|
+
std::string* /* error */) const {
|
101
|
+
std::string filename = Filename(file);
|
102
|
+
protobuf::io::ZeroCopyOutputStream* out = context->Open(filename);
|
103
|
+
protobuf::io::Printer printer(out, '$');
|
104
|
+
|
105
|
+
for (int i = 0; i < file->dependency_count(); i++) {
|
106
|
+
const protobuf::FileDescriptor* dep = file->dependency(i);
|
107
|
+
printer.Print("require('$name$')\n", "name", ModuleName(dep));
|
108
|
+
}
|
109
|
+
|
110
|
+
printer.Print("local upb = require('upb')\n");
|
111
|
+
|
112
|
+
protobuf::FileDescriptorProto file_proto;
|
113
|
+
file->CopyTo(&file_proto);
|
114
|
+
std::string file_data;
|
115
|
+
file_proto.SerializeToString(&file_data);
|
116
|
+
|
117
|
+
printer.Print("local descriptor = table.concat({\n");
|
118
|
+
absl::string_view data(file_data);
|
119
|
+
while (!data.empty()) {
|
120
|
+
printer.Print(" ");
|
121
|
+
PrintString(72, &data, &printer);
|
122
|
+
printer.Print(",\n");
|
123
|
+
}
|
124
|
+
printer.Print("})\n");
|
125
|
+
|
126
|
+
printer.Print("return upb._generated_module(descriptor)\n");
|
127
|
+
|
128
|
+
return true;
|
129
|
+
}
|
130
|
+
|
131
|
+
int main(int argc, char** argv) {
|
132
|
+
LuaGenerator generator;
|
133
|
+
return google::protobuf::compiler::PluginMain(argc, argv, &generator);
|
134
|
+
}
|