forked from WeaselGames/godot_luaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluaCallable.cpp
More file actions
65 lines (52 loc) · 1.83 KB
/
Copy pathluaCallable.cpp
File metadata and controls
65 lines (52 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "luaCallable.h"
#include "lua.h"
#include "core/templates/hashfuncs.h"
// I used "GDScriptLambdaCallable" as a template for this
LuaCallable::LuaCallable(Ref<RefCounted> p_obj, int ref, lua_State *p_state){
obj = p_obj;
funcRef = ref;
state = p_state;
h = (uint32_t)hash_djb2_one_64((uint64_t)this);
}
bool LuaCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
// Lua callables are only compared by reference.
return p_a == p_b;
}
bool LuaCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
// Lua callables are only compared by reference.
return p_a < p_b;
}
CallableCustom::CompareEqualFunc LuaCallable::get_compare_equal_func() const {
return compare_equal;
}
CallableCustom::CompareLessFunc LuaCallable::get_compare_less_func() const {
return compare_less;
}
ObjectID LuaCallable::get_object() const {
return obj->get_instance_id();
}
String LuaCallable::get_as_text() const {
// I dont know of a way to get a useful name from the function
// For now we are just using the callables hash.
return vformat("luaCallable 0x%X", h);
}
uint32_t LuaCallable::hash() const {
return h;
}
void LuaCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
// Geting the lua function via the referance stored in funcRef
lua_rawgeti(state, LUA_REGISTRYINDEX, funcRef);
// Push all the argument on to the stack
for (int i = 0; i < p_argcount; i++) {
Lua::pushVariant(*p_arguments[i], state);
}
// execute the function using a protected call.
int ret = lua_pcall(state, p_argcount, 1, 0);
if (ret != LUA_OK) {
r_return_value = Lua::handleError(ret, state);
} else r_return_value = Lua::getVariant(1, state, obj);
lua_pop(state, 1);
}
int LuaCallable::getFuncRef() {
return funcRef;
}