9 #include "upb/json_decode.h"
10 #include "upb/json_encode.h"
11 #include "upb/upb.hpp"
13 #include "test_pb.upb.h"
14 #include "test_pb.upbdefs.h"
23 #if defined (UPB_BINDING_LUA_WITH_LEGACY_BINDING)
24 # include "upb/lua/upb.h"
32 int main(
int argc,
char* argv[]) {
33 std::string init_script =
"package.preload['lupb'] = ... ";
34 std::vector<std::string> load_files;
35 for (
int i = 1; i < argc; ++i) {
36 size_t len = strlen(argv[i]);
37 if (len >= 4 && std::string(&argv[i][len - 4]) ==
".lua") {
38 load_files.push_back(argv[i]);
40 init_script +=
"package.path = '" + std::string(argv[i]) +
"/?.lua;' .. package.path\n";
46 lua_State* L = luaL_newstate();
50 std::cout <<
"Load lupb ..." << std::endl;
51 lua_pushcfunction(L, luaopen_lupb);
52 luaL_loadstring(L, init_script.c_str());
53 lua_pushcfunction(L, luaopen_lupb);
54 if (lua_pcall(L, 1, LUA_MULTRET, 0)) {
55 std::cout <<
"Load lupb failed" << std::endl;
58 if (load_files.empty()) {
60 cmake_toolset_test_message* test_msg = cmake_toolset_test_message_new(arena.ptr());
63 cmake_toolset_test_message_set_i64(test_msg, 123321);
65 cmake_toolset_test_message_serialize(test_msg, arena.ptr(), &
size);
66 std::cout <<
"Serialized test message size: " <<
size << std::endl;
68 std::cout <<
"Run lua code:" << std::endl;
70 "local _, err = pcall(function() print(string.format('Message Count: %g', "
71 "require('test_pb_pb')._filedef:msgcount())) end)\n";
72 script +=
"if err ~= nil then print(err) end\n";
73 luaL_dostring(L, script.c_str());
74 script =
"local _, err = pcall(function() print('File Name: ' .. require('test_pb_pb')._filedef:name()) end)\n";
75 script +=
"if err ~= nil then print(err) end\n";
76 luaL_dostring(L, script.c_str());
78 std::cout <<
"End" << std::endl;
80 int top = lua_gettop(L);
81 for (
auto& script_file : load_files) {
82 std::cout <<
"Run lua file:" << script_file << std::endl;
83 if (LUA_OK != luaL_dofile(L, script_file.c_str())) {
84 int new_top = lua_gettop(L);
86 if (lua_isstring(L, top + 1) || lua_isnumber(L, top + 1)) {
87 std::cerr << lua_tostring(L, top + 1) << std::endl;
89 lua_getglobal(L,
"tostring");
90 lua_pushvalue(L, top + 1);
92 std::cerr << lua_tostring(L, -1) << std::endl;
constexpr auto size(TCONTAINER &&container) -> decltype(container.size())
int main(int argc, char *argv[])