xref: /freebsd-src/contrib/llvm-project/lldb/bindings/lua/lua-wrapper.swig (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1e8d8bef9SDimitry Andric%header %{
2e8d8bef9SDimitry Andric
30eae32dcSDimitry Andrictemplate <typename T> void PushSBClass(lua_State * L, T * obj);
4e8d8bef9SDimitry Andric
5e8d8bef9SDimitry Andric// This function is called from Lua::CallBreakpointCallback
6*06c3fb27SDimitry Andricllvm::Expected<bool>
7*06c3fb27SDimitry Andriclldb_private::lua::SWIGBridge::LLDBSwigLuaBreakpointCallbackFunction(
80eae32dcSDimitry Andric    lua_State * L, lldb::StackFrameSP stop_frame_sp,
9e8d8bef9SDimitry Andric    lldb::BreakpointLocationSP bp_loc_sp,
100eae32dcSDimitry Andric    const StructuredDataImpl &extra_args_impl) {
11e8d8bef9SDimitry Andric  lldb::SBFrame sb_frame(stop_frame_sp);
12e8d8bef9SDimitry Andric  lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
13e8d8bef9SDimitry Andric  int nargs = 2;
14e8d8bef9SDimitry Andric
150eae32dcSDimitry Andric  lldb::SBStructuredData extra_args(extra_args_impl);
16e8d8bef9SDimitry Andric
17e8d8bef9SDimitry Andric  // Push the Lua wrappers
18e8d8bef9SDimitry Andric  PushSBClass(L, &sb_frame);
19e8d8bef9SDimitry Andric  PushSBClass(L, &sb_bp_loc);
20e8d8bef9SDimitry Andric
210eae32dcSDimitry Andric  if (extra_args.IsValid()) {
220eae32dcSDimitry Andric    PushSBClass(L, &extra_args);
23e8d8bef9SDimitry Andric    nargs++;
24e8d8bef9SDimitry Andric  }
25e8d8bef9SDimitry Andric
26e8d8bef9SDimitry Andric  // Call into the Lua callback passing 'sb_frame' and 'sb_bp_loc'.
27e8d8bef9SDimitry Andric  // Expects a boolean return.
28e8d8bef9SDimitry Andric  if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
29e8d8bef9SDimitry Andric    llvm::Error E = llvm::make_error<llvm::StringError>(
30e8d8bef9SDimitry Andric        llvm::formatv("{0}\n", lua_tostring(L, -1)),
31e8d8bef9SDimitry Andric        llvm::inconvertibleErrorCode());
32e8d8bef9SDimitry Andric    // Pop error message from the stack.
33e8d8bef9SDimitry Andric    lua_pop(L, 1);
34e8d8bef9SDimitry Andric    return std::move(E);
35e8d8bef9SDimitry Andric  }
36e8d8bef9SDimitry Andric
37e8d8bef9SDimitry Andric  // Boolean return from the callback
38e8d8bef9SDimitry Andric  bool stop = lua_toboolean(L, -1);
39e8d8bef9SDimitry Andric  lua_pop(L, 1);
40e8d8bef9SDimitry Andric
41e8d8bef9SDimitry Andric  return stop;
42e8d8bef9SDimitry Andric}
43e8d8bef9SDimitry Andric
44fe6060f1SDimitry Andric// This function is called from Lua::CallWatchpointCallback
45*06c3fb27SDimitry Andricllvm::Expected<bool>
46*06c3fb27SDimitry Andriclldb_private::lua::SWIGBridge::LLDBSwigLuaWatchpointCallbackFunction(
470eae32dcSDimitry Andric    lua_State * L, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp) {
48fe6060f1SDimitry Andric  lldb::SBFrame sb_frame(stop_frame_sp);
49fe6060f1SDimitry Andric  lldb::SBWatchpoint sb_wp(wp_sp);
50fe6060f1SDimitry Andric  int nargs = 2;
51fe6060f1SDimitry Andric
52fe6060f1SDimitry Andric  // Push the Lua wrappers
53fe6060f1SDimitry Andric  PushSBClass(L, &sb_frame);
54fe6060f1SDimitry Andric  PushSBClass(L, &sb_wp);
55fe6060f1SDimitry Andric
56fe6060f1SDimitry Andric  // Call into the Lua callback passing 'sb_frame' and 'sb_wp'.
57fe6060f1SDimitry Andric  // Expects a boolean return.
58fe6060f1SDimitry Andric  if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
59fe6060f1SDimitry Andric    llvm::Error E = llvm::make_error<llvm::StringError>(
60fe6060f1SDimitry Andric        llvm::formatv("{0}\n", lua_tostring(L, -1)),
61fe6060f1SDimitry Andric        llvm::inconvertibleErrorCode());
62fe6060f1SDimitry Andric    // Pop error message from the stack.
63fe6060f1SDimitry Andric    lua_pop(L, 1);
64fe6060f1SDimitry Andric    return std::move(E);
65fe6060f1SDimitry Andric  }
66fe6060f1SDimitry Andric
67fe6060f1SDimitry Andric  // Boolean return from the callback
68fe6060f1SDimitry Andric  bool stop = lua_toboolean(L, -1);
69fe6060f1SDimitry Andric  lua_pop(L, 1);
70fe6060f1SDimitry Andric
71fe6060f1SDimitry Andric  return stop;
72fe6060f1SDimitry Andric}
73fe6060f1SDimitry Andric
740eae32dcSDimitry Andricstatic void LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton) {
75349cc55cSDimitry Andric  lua_State *L = (lua_State *)baton;
76349cc55cSDimitry Andric
77349cc55cSDimitry Andric  lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback);
78349cc55cSDimitry Andric  lua_gettable(L, LUA_REGISTRYINDEX);
79349cc55cSDimitry Andric
80349cc55cSDimitry Andric  // FIXME: There's no way to report errors back to the user
81349cc55cSDimitry Andric  lua_pushstring(L, str);
82349cc55cSDimitry Andric  lua_pcall(L, 1, 0, 0);
83349cc55cSDimitry Andric}
84349cc55cSDimitry Andric
850eae32dcSDimitry Andricstatic int LLDBSwigLuaCloseFileHandle(lua_State * L) {
86349cc55cSDimitry Andric  return luaL_error(L, "You cannot close a file handle used by lldb.");
87349cc55cSDimitry Andric}
88e8d8bef9SDimitry Andric
89e8d8bef9SDimitry Andric%}
90