Skip to content

Commit

Permalink
Cast metamethod index to string (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey2k authored May 23, 2023
1 parent f9c97cd commit dd11bf9
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
90 changes: 90 additions & 0 deletions project/testing/tests/general.base_types.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
extends UnitTest
var lua: LuaAPI

func _ready():
# Since we are using poly here, we need to make sure to call super for _methods
super._ready()
# id will determine the load order
id = 9825

lua = LuaAPI.new()

# testName and testDescription are for any needed context about the test.
testName = "General.base_types"
testDescription = "
Test base godot types expose to lua.
"

func fail():
status = false
done = true

func _process(delta):
# Since we are using poly here, we need to make sure to call super for _methods
super._process(delta)

var err = lua.do_string("
vec2 = Vector2(1.5, 1.0)
vec2Floor = vec2.floor()
vec3 = Vector3(1.5, 1.0, 1.0)
vec3Floor = vec3.floor()
")
if err is LuaError:
errors.append(err)
return fail()

var vec2 = lua.pull_variant("vec2")
if vec2 is LuaError:
errors.append(vec2)
return fail()

if not vec2 is Vector2:
errors.append(LuaError.new_error("vec2 is not type Vector2 but is %d" % typeof(vec2)))
return fail()

if vec2.x < 1.4 and vec2.x > 1.6:
errors.append(LuaError.new_error("vec2 is not (1.5, 1.0) but is (%f, %f)" % [vec2.x, vec2.y]))
return fail()

var vec2Floor = lua.pull_variant("vec2Floor")
if vec2Floor is LuaError:
errors.append(vec2Floor)
return fail()

if not vec2Floor is Vector2:
errors.append(LuaError.new_error("vec2Floor is not type Vector2 but is %d" % typeof(vec2Floor)))
return fail()

if vec2Floor.x < 0.9 and vec2Floor.x > 1.1:
errors.append(LuaError.new_error("vec2Floor is not (1.0, 1.0) but is (%f, %f)" % [vec2Floor.x, vec2Floor.y]))
return fail()


var vec3 = lua.pull_variant("vec3")
if vec3 is LuaError:
errors.append(vec3)
return fail()

if not vec3 is Vector3:
errors.append(LuaError.new_error("vec3 is not type Vector3 but is %d" % typeof(vec3)))
return fail()

if vec3.x < 1.4 and vec3.x > 1.6:
errors.append(LuaError.new_error("vec3 is not (1.5, 1.0, 1.0) but is (%f, %f, %f)" % [vec3.x, vec3.y, vec3.z]))
return fail()

var vec3Floor = lua.pull_variant("vec3Floor")
if vec3Floor is LuaError:
errors.append(vec3Floor)
return fail()

if not vec3Floor is Vector3:
errors.append(LuaError.new_error("vec2Floor is not type Vector3 but is %d" % typeof(vec3Floor)))
return fail()

if vec3Floor.x < 0.9 and vec3Floor.x > 1.1:
errors.append(LuaError.new_error("vec3Floor is not (1.0, 1.0, 1.0) but is (%f, %f, %f)" % [vec3Floor.x, vec3Floor.y, vec3Floor.z]))
return fail()

done = true
5 changes: 4 additions & 1 deletion src/luaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ int LuaState::luaCallableCall(lua_State* state) {
return 0;
}

if (returned.get_type() != Variant::Type::OBJECT) return 1;
if (LuaTuple* tuple = Object::cast_to<LuaTuple>(returned.operator Object*()); tuple != nullptr)
return tuple->size();
return 1;
Expand Down Expand Up @@ -773,7 +774,8 @@ int LuaState::luaCallableCall(lua_State* state) {
lua_error(state);
return 0;
}


if (returned.get_type() != Variant::Type::OBJECT) return 1;
if (LuaTuple* tuple = dynamic_cast<LuaTuple*>(returned.operator Object*()); tuple != nullptr)
return tuple->size();
return 1;
Expand Down Expand Up @@ -824,6 +826,7 @@ int LuaState::luaUserdataFuncCall(lua_State* state) {
#endif

LuaState::pushVariant(state, returned);
if (returned.get_type() != Variant::Type::OBJECT) return 1;
#ifndef LAPI_GDEXTENSION
if (LuaTuple* tuple = Object::cast_to<LuaTuple>(returned.operator Object*()); tuple != nullptr)
#else
Expand Down
1 change: 0 additions & 1 deletion src/luaState.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class LuaState {
// Lua functions
static int luaErrorHandler(lua_State* state);
static int luaPrint(lua_State* state);
static int luaExposedFuncCall(lua_State* state);
static int luaUserdataFuncCall(lua_State* state);
static int luaCallableCall(lua_State* state);
private:
Expand Down
14 changes: 7 additions & 7 deletions src/metatables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void LuaState::createVector2Metatable() {
luaL_newmetatable(L, "mt_Vector2");

LUA_METAMETHOD_TEMPLATE(L, -1, "__index", {
if (arg1.has_method(arg2)) {
if (arg1.has_method(arg2.operator String())) {
lua_pushlightuserdata(inner_state, lua_touserdata(inner_state, 1));
LuaState::pushVariant(inner_state, arg2);
lua_pushcclosure(inner_state, luaUserdataFuncCall, 2);
Expand Down Expand Up @@ -201,7 +201,7 @@ void LuaState::createVector3Metatable() {
luaL_newmetatable(L, "mt_Vector3");

LUA_METAMETHOD_TEMPLATE(L, -1, "__index", {
if (arg1.has_method(arg2)) {
if (arg1.has_method(arg2.operator String())) {
lua_pushlightuserdata(inner_state, lua_touserdata(inner_state, 1));
LuaState::pushVariant(inner_state, arg2);
lua_pushcclosure(inner_state, luaUserdataFuncCall, 2);
Expand Down Expand Up @@ -270,7 +270,7 @@ void LuaState::createRect2Metatable() {

LUA_METAMETHOD_TEMPLATE(L, -1, "__index", {
// Index was not found, so check to see if there is a matching function
if (arg1.has_method(arg2)) {
if (arg1.has_method(arg2.operator String())) {
lua_pushlightuserdata(inner_state, lua_touserdata(inner_state,1));
LuaState::pushVariant(inner_state, arg2);
lua_pushcclosure(inner_state, luaUserdataFuncCall, 2);
Expand Down Expand Up @@ -301,7 +301,7 @@ void LuaState::createPlaneMetatable() {
luaL_newmetatable(L, "mt_Plane");

LUA_METAMETHOD_TEMPLATE(L, -1, "__index", {
if (arg1.has_method(arg2)) {
if (arg1.has_method(arg2.operator String())) {
lua_pushlightuserdata(inner_state, lua_touserdata(inner_state, 1));
LuaState::pushVariant(inner_state, arg2);
lua_pushcclosure(inner_state, luaUserdataFuncCall, 2);
Expand Down Expand Up @@ -331,7 +331,7 @@ void LuaState::createColorMetatable() {
luaL_newmetatable(L, "mt_Color");

LUA_METAMETHOD_TEMPLATE(L, -1, "__index", {
if (arg1.has_method(arg2)) {
if (arg1.has_method(arg2.operator String())) {
lua_pushlightuserdata(inner_state, lua_touserdata(inner_state,1));
LuaState::pushVariant(inner_state, arg2);
lua_pushcclosure(inner_state, luaUserdataFuncCall, 2);
Expand Down Expand Up @@ -399,7 +399,7 @@ void LuaState::createSignalMetatable() {
luaL_newmetatable(L, "mt_Signal");

LUA_METAMETHOD_TEMPLATE(L, -1, "__index", {
if (arg1.has_method(arg2)) {
if (arg1.has_method(arg2.operator String())) {
lua_pushlightuserdata(inner_state, lua_touserdata(inner_state, 1));
LuaState::pushVariant(inner_state, arg2);
lua_pushcclosure(inner_state, luaUserdataFuncCall, 2);
Expand Down Expand Up @@ -430,7 +430,7 @@ void LuaState::createObjectMetatable() {
}

// If the functions is allowed and exists
if ((allowedFields.is_empty() || allowedFields.has(arg2)) && arg1.has_method(arg2)) {
if ((allowedFields.is_empty() || allowedFields.has(arg2)) && arg1.has_method(arg2.operator String())) {
lua_pushlightuserdata(inner_state, lua_touserdata(inner_state, 1));
LuaState::pushVariant(inner_state, arg2);
lua_pushcclosure(inner_state, luaUserdataFuncCall, 2);
Expand Down

0 comments on commit dd11bf9

Please sign in to comment.