libcopp  2.2.0
upb-and-lua-binding.cpp
Go to the documentation of this file.
1 // Copyright 2022 atframework
2 
3 #include <cstdlib>
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 
8 #include "upb/def.hpp"
9 #include "upb/json_decode.h"
10 #include "upb/json_encode.h"
11 #include "upb/upb.hpp"
12 
13 #include "test_pb.upb.h"
14 #include "test_pb.upbdefs.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 #include "lauxlib.h"
20 #include "lua.h"
21 #include "lualib.h"
22 
23 #if defined (UPB_BINDING_LUA_WITH_LEGACY_BINDING)
24 # include "upb/lua/upb.h"
25 #else
26 # include "lua/upb.h"
27 #endif
28 #ifdef __cplusplus
29 }
30 #endif
31 
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]);
39  } else {
40  init_script += "package.path = '" + std::string(argv[i]) + "/?.lua;' .. package.path\n";
41  }
42  }
43 
44  // Lua binding
45  {
46  lua_State* L = luaL_newstate();
47  luaL_openlibs(L);
48 
49  // luaopen_lupb(L);
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;
56  }
57 
58  if (load_files.empty()) {
59  upb::Arena arena;
60  cmake_toolset_test_message* test_msg = cmake_toolset_test_message_new(arena.ptr());
61  // cmake_toolset_test_message_set_str(test_msg, upb_StringView_FromString("hello world!")); // New implementation
62  // cmake_toolset_test_message_set_str(test_msg, upb_strview_makez("hello world!")); // Old implementation
63  cmake_toolset_test_message_set_i64(test_msg, 123321);
64  size_t size;
65  cmake_toolset_test_message_serialize(test_msg, arena.ptr(), &size);
66  std::cout << "Serialized test message size: " << size << std::endl;
67 
68  std::cout << "Run lua code:" << std::endl;
69  std::string script =
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());
77 
78  std::cout << "End" << std::endl;
79  } else {
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);
85  if (new_top > top) {
86  if (lua_isstring(L, top + 1) || lua_isnumber(L, top + 1)) {
87  std::cerr << lua_tostring(L, top + 1) << std::endl;
88  } else {
89  lua_getglobal(L, "tostring");
90  lua_pushvalue(L, top + 1);
91  lua_call(L, 1, 1);
92  std::cerr << lua_tostring(L, -1) << std::endl;
93  }
94  }
95  }
96  lua_settop(L, top);
97  }
98  }
99 
100  lua_close(L);
101  }
102  return 0;
103 }
constexpr auto size(TCONTAINER &&container) -> decltype(container.size())
Definition: span.h:44
int main(int argc, char *argv[])