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