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