• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include <lauxlib.h>
9 #include <lua.h>
10 #include <lualib.h>
11 #include <signal.h>
12 
13 #include "lua/upb.h"
14 
15 lua_State* L;
16 
interrupt(lua_State * L,lua_Debug * ar)17 static void interrupt(lua_State* L, lua_Debug* ar) {
18   (void)ar;
19   lua_sethook(L, NULL, 0, 0);
20   luaL_error(L, "SIGINT");
21 }
22 
sighandler(int i)23 static void sighandler(int i) {
24   fprintf(stderr, "Signal!\n");
25   signal(i, SIG_DFL);
26   lua_sethook(L, interrupt, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
27 }
28 
29 const char* init =
30     "package.preload['lupb'] = ... "
31     "package.path = '"
32     "./?.lua;"
33     "./third_party/lunit/?.lua;"
34     "external/com_google_protobuf/?.lua;"
35     "external/com_google_protobuf/src/?.lua;"
36     "bazel-bin/?.lua;"
37     "bazel-bin/external/com_google_protobuf/src/?.lua;"
38     "bazel-bin/external/com_google_protobuf/?.lua;"
39     "lua/?.lua;"
40     // These additional paths handle the case where this test is invoked from
41     // the protobuf repo's Bazel workspace.
42     "external/?.lua;"
43     "external/third_party/lunit/?.lua;"
44     "src/?.lua;"
45     "bazel-bin/external/?.lua;"
46     "external/lua/?.lua"
47     "'";
48 
main(int argc,char ** argv)49 int main(int argc, char** argv) {
50   if (argc < 2) {
51     fprintf(stderr, "missing argument with path to .lua file\n");
52     return 1;
53   }
54 
55   int ret = 0;
56   L = luaL_newstate();
57   luaL_openlibs(L);
58   lua_pushcfunction(L, luaopen_lupb);
59   ret = luaL_loadstring(L, init);
60   lua_pushcfunction(L, luaopen_lupb);
61 
62   signal(SIGINT, sighandler);
63   ret = ret || lua_pcall(L, 1, LUA_MULTRET, 0) || luaL_dofile(L, argv[1]);
64   signal(SIGINT, SIG_DFL);
65 
66   if (ret) {
67     fprintf(stderr, "error testing Lua: %s\n", lua_tostring(L, -1));
68     ret = 1;
69   }
70 
71   lua_close(L);
72   return ret;
73 }
74