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