Skip to content

Commit dba2e03

Browse files
committed
Move HookEvent enum to LuaThread class, update hook API docs
1 parent 3cb753b commit dba2e03

File tree

5 files changed

+107
-20
lines changed

5 files changed

+107
-20
lines changed

doc_classes/LuaThread.xml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,66 @@
1010
<tutorials>
1111
</tutorials>
1212
<methods>
13+
<method name="get_hook" qualifiers="const">
14+
<return type="Variant" />
15+
<description>
16+
Returns the current hook function.
17+
See [method set_hook].
18+
</description>
19+
</method>
20+
<method name="get_hook_count" qualifiers="const">
21+
<return type="int" />
22+
<description>
23+
Returns the current hook count.
24+
See [method set_hook].
25+
</description>
26+
</method>
27+
<method name="get_hook_mask" qualifiers="const">
28+
<return type="int" enum="LuaThread.HookMask" is_bitfield="true" />
29+
<description>
30+
Returns the current hook mask.
31+
See [method set_hook].
32+
</description>
33+
</method>
34+
<method name="get_stack_info" qualifiers="const">
35+
<return type="LuaDebug[]" />
36+
<description>
37+
</description>
38+
</method>
39+
<method name="get_stack_level_info" qualifiers="const">
40+
<return type="LuaDebug" />
41+
<param index="0" name="level" type="int" />
42+
<description>
43+
</description>
44+
</method>
45+
<method name="get_traceback" qualifiers="const">
46+
<return type="String" />
47+
<param index="0" name="message" type="String" default="&quot;&quot;" />
48+
<param index="1" name="level" type="int" default="0" />
49+
<description>
50+
</description>
51+
</method>
1352
<method name="is_main_thread" qualifiers="const">
1453
<return type="bool" />
1554
<description>
1655
Whether this thread is the [LuaState]'s main thread.
1756
See also [member LuaState.main_thread].
1857
</description>
1958
</method>
59+
<method name="set_hook">
60+
<return type="void" />
61+
<param index="0" name="hook" type="Variant" />
62+
<param index="1" name="mask" type="int" enum="LuaThread.HookMask" is_bitfield="true" />
63+
<param index="2" name="count" type="int" default="0" />
64+
<description>
65+
Sets the debugging hook function.
66+
[param hook] is the Hook function, which should be either a [Callable] or [LuaFunction]. Pass [code]null[/code] to disable hooks.
67+
[param mask] specifies on which events the hook will be called: it is formed by a bitwise OR of the constants [constant HOOK_MASK_CALL], [constant HOOK_MASK_RETURN], [constant HOOK_MASK_LINE] and [constant HOOK_MASK_COUNT]. Pass 0 to disable hooks.
68+
[param count] is only meaningful when the mask includes [constant HOOK_MASK_COUNT].
69+
See [enum HookEvent] for the available hook events.
70+
See also [url]https://www.lua.org/manual/5.4/manual.html#lua_sethook[/url].
71+
</description>
72+
</method>
2073
</methods>
2174
<members>
2275
<member name="status" type="int" setter="" getter="get_status" enum="LuaThread.Status">
@@ -45,5 +98,37 @@
4598
<constant name="STATUS_DEAD" value="-1" enum="Status">
4699
The thread has finished execution.
47100
</constant>
101+
<constant name="HOOK_MASK_CALL" value="1" enum="HookMask" is_bitfield="true">
102+
Mask passed to [method set_hook] to subscribe to [constant HOOK_EVENT_CALL] and [constant HOOK_EVENT_TAIL_CALL] hooks.
103+
</constant>
104+
<constant name="HOOK_MASK_RETURN" value="2" enum="HookMask" is_bitfield="true">
105+
Mask passed to [method set_hook] to subscribe to [constant HOOK_EVENT_RETURN] hooks.
106+
</constant>
107+
<constant name="HOOK_MASK_LINE" value="4" enum="HookMask" is_bitfield="true">
108+
Mask passed to [method set_hook] to subscribe to [constant HOOK_EVENT_LINE] hooks.
109+
</constant>
110+
<constant name="HOOK_MASK_COUNT" value="8" enum="HookMask" is_bitfield="true">
111+
Mask passed to [method set_hook] to subscribe to [constant HOOK_EVENT_COUNT] hooks.
112+
</constant>
113+
<constant name="HOOK_EVENT_CALL" value="0" enum="HookEvent">
114+
Hook event that is called when the interpreter calls a function. The hook is called just after Lua enters the new function.
115+
</constant>
116+
<constant name="HOOK_EVENT_RETURN" value="1" enum="HookEvent">
117+
Hook event that is called when the interpreter returns from a function. The hook is called just before Lua leaves the function.
118+
</constant>
119+
<constant name="HOOK_EVENT_LINE" value="2" enum="HookEvent">
120+
Hook event that is called when the interpreter is about to start the execution of a new line of code, or when it jumps back in the code (even to the same line). This event only happens while Lua is executing a Lua function.
121+
</constant>
122+
<constant name="HOOK_EVENT_COUNT" value="3" enum="HookEvent">
123+
Hook event that is called after the interpreter executes every [code]count[/code] instructions. This event only happens while Lua is executing a Lua function.
124+
</constant>
125+
<constant name="HOOK_EVENT_TAIL_CALL" value="4" enum="HookEvent">
126+
Hook event that is called when the interpreter calls a function as a tail call. In this case, there will be no corresponding return event.
127+
</constant>
128+
<constant name="HOOK_OK" value="0" enum="HookResult">
129+
</constant>
130+
<constant name="HOOK_YIELD" value="-1" enum="HookResult">
131+
Return this value from line or count hook events to yield execution of the current Lua function that triggered the hook.
132+
</constant>
48133
</constants>
49134
</class>

src/LuaDebug.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ LuaDebug::LuaDebug() : debug() {}
2727
LuaDebug::LuaDebug(const lua_Debug& debug) : debug(debug) {}
2828
LuaDebug::LuaDebug(lua_Debug&& debug) : debug(std::move(debug)) {}
2929

30-
LuaDebug::HookEvent LuaDebug::get_event() const {
31-
return (LuaDebug::HookEvent) debug.event;
30+
LuaThread::HookEvent LuaDebug::get_event() const {
31+
return (LuaThread::HookEvent) debug.event;
3232
}
3333

3434
String LuaDebug::get_name() const {
@@ -91,12 +91,6 @@ void LuaDebug::fill_info(const sol::protected_function& f, lua_Debug *ar) {
9191
}
9292

9393
void LuaDebug::_bind_methods() {
94-
BIND_ENUM_CONSTANT(EVENT_CALL);
95-
BIND_ENUM_CONSTANT(EVENT_RETURN);
96-
BIND_ENUM_CONSTANT(EVENT_LINE);
97-
BIND_ENUM_CONSTANT(EVENT_COUNT);
98-
BIND_ENUM_CONSTANT(EVENT_TAIL_CALL);
99-
10094
ClassDB::bind_method(D_METHOD("get_event"), &LuaDebug::get_event);
10195
ClassDB::bind_method(D_METHOD("get_name"), &LuaDebug::get_name);
10296
ClassDB::bind_method(D_METHOD("get_name_what"), &LuaDebug::get_name_what);
@@ -112,7 +106,7 @@ void LuaDebug::_bind_methods() {
112106
ClassDB::bind_method(D_METHOD("is_vararg"), &LuaDebug::is_vararg);
113107
#endif
114108

115-
ADD_PROPERTY(PropertyInfo(Variant::INT, "event", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_ENUM, "HookEvent"), "", "get_event");
109+
ADD_PROPERTY(PropertyInfo(Variant::INT, "event", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_ENUM, "LuaThread::HookEvent"), "", "get_event");
116110
ADD_PROPERTY(PropertyInfo(Variant::STRING, "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_name");
117111
ADD_PROPERTY(PropertyInfo(Variant::STRING, "name_what", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_name_what");
118112
ADD_PROPERTY(PropertyInfo(Variant::STRING, "what", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_what");

src/LuaDebug.hpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <godot_cpp/classes/ref_counted.hpp>
2626
#include <sol/sol.hpp>
2727

28+
#include "LuaThread.hpp"
29+
2830
using namespace godot;
2931

3032
namespace luagdextension {
@@ -33,19 +35,11 @@ class LuaDebug : public RefCounted {
3335
GDCLASS(LuaDebug, RefCounted);
3436

3537
public:
36-
enum HookEvent {
37-
EVENT_CALL = LUA_HOOKCALL,
38-
EVENT_RETURN = LUA_HOOKRET,
39-
EVENT_LINE = LUA_HOOKLINE,
40-
EVENT_COUNT = LUA_HOOKCOUNT,
41-
EVENT_TAIL_CALL = LUA_HOOKTAILCALL,
42-
};
43-
4438
LuaDebug();
4539
LuaDebug(const lua_Debug& debug);
4640
LuaDebug(lua_Debug&& debug);
4741

48-
HookEvent get_event() const;
42+
LuaThread::HookEvent get_event() const;
4943
String get_name() const;
5044
String get_name_what() const;
5145
String get_what() const;
@@ -71,6 +65,5 @@ class LuaDebug : public RefCounted {
7165
};
7266

7367
}
74-
VARIANT_ENUM_CAST(luagdextension::LuaDebug::HookEvent);
7568

7669
#endif // __LUA_DEBUG_HPP__

src/LuaThread.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ void LuaThread::_bind_methods() {
168168
BIND_BITFIELD_FLAG(HOOK_MASK_LINE);
169169
BIND_BITFIELD_FLAG(HOOK_MASK_COUNT);
170170

171+
BIND_ENUM_CONSTANT(HOOK_EVENT_CALL);
172+
BIND_ENUM_CONSTANT(HOOK_EVENT_RETURN);
173+
BIND_ENUM_CONSTANT(HOOK_EVENT_LINE);
174+
BIND_ENUM_CONSTANT(HOOK_EVENT_COUNT);
175+
BIND_ENUM_CONSTANT(HOOK_EVENT_TAIL_CALL);
176+
171177
BIND_ENUM_CONSTANT(HOOK_OK);
172178
BIND_ENUM_CONSTANT(HOOK_YIELD);
173179

src/LuaThread.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ class LuaThread : public LuaObjectSubclass<sol::thread> {
5151
HOOK_MASK_COUNT = LUA_MASKCOUNT,
5252
};
5353

54+
enum HookEvent {
55+
HOOK_EVENT_CALL = LUA_HOOKCALL,
56+
HOOK_EVENT_RETURN = LUA_HOOKRET,
57+
HOOK_EVENT_LINE = LUA_HOOKLINE,
58+
HOOK_EVENT_COUNT = LUA_HOOKCOUNT,
59+
HOOK_EVENT_TAIL_CALL = LUA_HOOKTAILCALL,
60+
};
61+
5462
enum HookResult {
5563
HOOK_OK = 0,
5664
HOOK_YIELD = -1,
@@ -77,8 +85,9 @@ class LuaThread : public LuaObjectSubclass<sol::thread> {
7785
};
7886

7987
}
80-
VARIANT_ENUM_CAST(luagdextension::LuaThread::Status);
88+
VARIANT_ENUM_CAST(luagdextension::LuaThread::HookEvent);
8189
VARIANT_ENUM_CAST(luagdextension::LuaThread::HookResult);
90+
VARIANT_ENUM_CAST(luagdextension::LuaThread::Status);
8291
VARIANT_BITFIELD_CAST(luagdextension::LuaThread::HookMask);
8392

8493
#endif // __LUA_THREAD_HPP__

0 commit comments

Comments
 (0)