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