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