1%header %{
2
3template <typename T>
4void
5PushSBClass(lua_State* L, T* obj);
6
7%}
8
9%wrapper %{
10
11// This function is called from Lua::CallBreakpointCallback
12SWIGEXPORT llvm::Expected<bool>
13LLDBSwigLuaBreakpointCallbackFunction
14(
15   lua_State *L,
16   lldb::StackFrameSP stop_frame_sp,
17   lldb::BreakpointLocationSP bp_loc_sp,
18   StructuredDataImpl *extra_args_impl
19)
20{
21   lldb::SBFrame sb_frame(stop_frame_sp);
22   lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
23   int nargs = 2;
24
25   llvm::Optional<lldb::SBStructuredData> extra_args;
26   if (extra_args_impl)
27      extra_args = lldb::SBStructuredData(extra_args_impl);
28
29   // Push the Lua wrappers
30   PushSBClass(L, &sb_frame);
31   PushSBClass(L, &sb_bp_loc);
32
33   if (extra_args.hasValue()) {
34      PushSBClass(L, extra_args.getPointer());
35      nargs++;
36   }
37
38   // Call into the Lua callback passing 'sb_frame' and 'sb_bp_loc'.
39   // Expects a boolean return.
40   if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
41      llvm::Error E = llvm::make_error<llvm::StringError>(
42            llvm::formatv("{0}\n", lua_tostring(L, -1)),
43            llvm::inconvertibleErrorCode());
44      // Pop error message from the stack.
45      lua_pop(L, 1);
46      return std::move(E);
47   }
48
49   // Boolean return from the callback
50   bool stop = lua_toboolean(L, -1);
51   lua_pop(L, 1);
52
53   return stop;
54}
55
56
57%}
58