1%header %{
2
3template <typename T>
4void
5PushSBClass(lua_State* L, T* obj);
6
7// This function is called from Lua::CallBreakpointCallback
8llvm::Expected<bool>
9lldb_private::LLDBSwigLuaBreakpointCallbackFunction
10(
11   lua_State *L,
12   lldb::StackFrameSP stop_frame_sp,
13   lldb::BreakpointLocationSP bp_loc_sp,
14   const StructuredDataImpl &extra_args_impl
15)
16{
17   lldb::SBFrame sb_frame(stop_frame_sp);
18   lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
19   int nargs = 2;
20
21   lldb::SBStructuredData extra_args(extra_args_impl);
22
23   // Push the Lua wrappers
24   PushSBClass(L, &sb_frame);
25   PushSBClass(L, &sb_bp_loc);
26
27   if (extra_args.IsValid()) {
28      PushSBClass(L, &extra_args);
29      nargs++;
30   }
31
32   // Call into the Lua callback passing 'sb_frame' and 'sb_bp_loc'.
33   // Expects a boolean return.
34   if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
35      llvm::Error E = llvm::make_error<llvm::StringError>(
36            llvm::formatv("{0}\n", lua_tostring(L, -1)),
37            llvm::inconvertibleErrorCode());
38      // Pop error message from the stack.
39      lua_pop(L, 1);
40      return std::move(E);
41   }
42
43   // Boolean return from the callback
44   bool stop = lua_toboolean(L, -1);
45   lua_pop(L, 1);
46
47   return stop;
48}
49
50// This function is called from Lua::CallWatchpointCallback
51llvm::Expected<bool>
52lldb_private::LLDBSwigLuaWatchpointCallbackFunction
53(
54   lua_State *L,
55   lldb::StackFrameSP stop_frame_sp,
56   lldb::WatchpointSP wp_sp
57)
58{
59   lldb::SBFrame sb_frame(stop_frame_sp);
60   lldb::SBWatchpoint sb_wp(wp_sp);
61   int nargs = 2;
62
63   // Push the Lua wrappers
64   PushSBClass(L, &sb_frame);
65   PushSBClass(L, &sb_wp);
66
67   // Call into the Lua callback passing 'sb_frame' and 'sb_wp'.
68   // Expects a boolean return.
69   if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
70      llvm::Error E = llvm::make_error<llvm::StringError>(
71            llvm::formatv("{0}\n", lua_tostring(L, -1)),
72            llvm::inconvertibleErrorCode());
73      // Pop error message from the stack.
74      lua_pop(L, 1);
75      return std::move(E);
76   }
77
78   // Boolean return from the callback
79   bool stop = lua_toboolean(L, -1);
80   lua_pop(L, 1);
81
82   return stop;
83}
84
85static void
86LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton) {
87   lua_State *L = (lua_State *)baton;
88
89   lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback);
90   lua_gettable(L, LUA_REGISTRYINDEX);
91
92   // FIXME: There's no way to report errors back to the user
93   lua_pushstring(L, str);
94   lua_pcall(L, 1, 0, 0);
95}
96
97static int LLDBSwigLuaCloseFileHandle(lua_State *L) {
98   return luaL_error(L, "You cannot close a file handle used by lldb.");
99}
100
101%}
102