Skip to content

Commit

Permalink
Stop metatables from being overwritten/read from (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
RadiantUwU authored Nov 6, 2023
1 parent 6c31c63 commit a7c659a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 41 deletions.
8 changes: 0 additions & 8 deletions doc_classes/LuaObjectMetatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@
The less than (<) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are neither both numbers nor both strings. Moreover, the result of the call is always converted to a boolean.
</description>
</method>
<method name="__metatable" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<description>
If object does not have a metatable, returns nil. Otherwise, if the object's metatable has a __metatable field, returns the associated value. Otherwise, returns the metatable of the given object.
</description>
</method>
<method name="__mod" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
Expand Down
15 changes: 0 additions & 15 deletions src/classes/luaObjectMetatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ void LuaObjectMetatable::_bind_methods() {
GDVIRTUAL_BIND(__call, "obj", "lua", "args");
GDVIRTUAL_BIND(__gc, "obj", "lua");
GDVIRTUAL_BIND(__tostring, "obj", "lua");
GDVIRTUAL_BIND(__metatable, "obj", "lua");
GDVIRTUAL_BIND(__len, "obj", "lua");
GDVIRTUAL_BIND(__unm, "obj", "lua");
GDVIRTUAL_BIND(__add, "obj", "lua", "other");
Expand Down Expand Up @@ -65,12 +64,6 @@ String LuaObjectMetatable::__tostring(Object *obj, Ref<LuaAPI> api) {
return ret;
}

Variant LuaObjectMetatable::__metatable(Object *obj, Ref<LuaAPI> api) {
Variant ret;
VIRTUAL_CALL(__metatable, ret, obj, api);
return ret;
}

int LuaObjectMetatable::__len(Object *obj, Ref<LuaAPI> api) {
int ret = 0;
VIRTUAL_CALL(__len, ret, obj, api);
Expand Down Expand Up @@ -274,14 +267,6 @@ String LuaDefaultObjectMetatable::__tostring(Object *obj, Ref<LuaAPI> api) {
return String();
}

Variant LuaDefaultObjectMetatable::__metatable(Object *obj, Ref<LuaAPI> api) {
if (obj->has_method("__metatable")) {
return obj->call("__metatable", api);
}

return Variant();
}

Variant LuaDefaultObjectMetatable::__unm(Object *obj, Ref<LuaAPI> api) {
if (obj->has_method("__unm")) {
return obj->call("__unm", api);
Expand Down
3 changes: 0 additions & 3 deletions src/classes/luaObjectMetatable.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class LuaObjectMetatable : public RefCounted {
GDVIRTUAL3R(Variant, __call, Object *, Ref<LuaAPI>, Ref<LuaTuple>);
GDVIRTUAL2R(Ref<LuaError>, __gc, Object *, Ref<LuaAPI>);
GDVIRTUAL2R(String, __tostring, Object *, Ref<LuaAPI>);
GDVIRTUAL2R(Variant, __metatable, Object *, Ref<LuaAPI>);
GDVIRTUAL2R(int, __len, Object *, Ref<LuaAPI>);
GDVIRTUAL2R(Variant, __unm, Object *, Ref<LuaAPI>);
GDVIRTUAL3R(Variant, __add, Object *, Ref<LuaAPI>, Variant);
Expand Down Expand Up @@ -56,7 +55,6 @@ class LuaObjectMetatable : public RefCounted {
virtual Variant __call(Object *obj, Ref<LuaAPI> api, Ref<LuaTuple> args);
virtual Ref<LuaError> __gc(Object *obj, Ref<LuaAPI> api);
virtual String __tostring(Object *obj, Ref<LuaAPI> api);
virtual Variant __metatable(Object *obj, Ref<LuaAPI> api);
virtual int __len(Object *obj, Ref<LuaAPI> api);
virtual Variant __unm(Object *obj, Ref<LuaAPI> api);
virtual Variant __add(Object *obj, Ref<LuaAPI> api, Variant other);
Expand Down Expand Up @@ -94,7 +92,6 @@ class LuaDefaultObjectMetatable : public LuaObjectMetatable {
Variant __call(Object *obj, Ref<LuaAPI> api, Ref<LuaTuple> args) override;
Ref<LuaError> __gc(Object *obj, Ref<LuaAPI> api) override;
String __tostring(Object *obj, Ref<LuaAPI> api) override;
Variant __metatable(Object *obj, Ref<LuaAPI> api) override;
int __len(Object *obj, Ref<LuaAPI> api) override;
Variant __unm(Object *obj, Ref<LuaAPI> api) override;
Variant __add(Object *obj, Ref<LuaAPI> api, Variant other) override;
Expand Down
2 changes: 2 additions & 0 deletions src/luaState.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <classes/luaError.h>
#include <lua/lua.hpp>

#define METATABLE_DISCLAIMER "This metatable is protected."

class LuaAPI;

class LuaState {
Expand Down
51 changes: 36 additions & 15 deletions src/metatables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ void LuaState::createVector2Metatable() {
return 1;
});

lua_pushliteral(L, "__metatable");
lua_pushliteral(L, METATABLE_DISCLAIMER);
lua_settable(L, -3);

lua_pop(L, 1); // Stack is now unmodified
}

Expand Down Expand Up @@ -230,6 +234,10 @@ void LuaState::createVector3Metatable() {
return 1;
});

lua_pushliteral(L, "__metatable");
lua_pushliteral(L, METATABLE_DISCLAIMER);
lua_settable(L, -3);

lua_pop(L, 1); // Stack is now unmodified
}

Expand Down Expand Up @@ -260,6 +268,10 @@ void LuaState::createRect2Metatable() {
return 1;
});

lua_pushliteral(L, "__metatable");
lua_pushliteral(L, METATABLE_DISCLAIMER);
lua_settable(L, -3);

lua_pop(L, 1); // Stack is now unmodified
}

Expand Down Expand Up @@ -290,6 +302,10 @@ void LuaState::createPlaneMetatable() {
return 1;
});

lua_pushliteral(L, "__metatable");
lua_pushliteral(L, METATABLE_DISCLAIMER);
lua_settable(L, -3);

lua_pop(L, 1); // Stack is now unmodified
}

Expand Down Expand Up @@ -358,6 +374,10 @@ void LuaState::createColorMetatable() {
return 1;
});

lua_pushliteral(L, "__metatable");
lua_pushliteral(L, METATABLE_DISCLAIMER);
lua_settable(L, -3);

lua_pop(L, 1); // Stack is now unmodified
}

Expand All @@ -377,6 +397,10 @@ void LuaState::createSignalMetatable() {
return 1;
});

lua_pushliteral(L, "__metatable");
lua_pushliteral(L, METATABLE_DISCLAIMER);
lua_settable(L, -3);

lua_pop(L, 1); // Stack is now unmodified
}

Expand Down Expand Up @@ -479,21 +503,6 @@ void LuaState::createObjectMetatable() {
return 0;
});

LUA_METAMETHOD_TEMPLATE(L, -1, "__metatable", {
Ref<LuaAPI> api = getAPI(inner_state);
Ref<LuaObjectMetatable> mt = arg1.get("lua_metatable");
if (!mt.is_valid()) {
mt = api->getObjectMetatable();
}

if (mt.is_valid()) {
LuaState::pushVariant(inner_state, mt->__metatable(arg1, api));
return 1;
}

return 0;
});

LUA_METAMETHOD_TEMPLATE(L, -1, "__len", {
Ref<LuaAPI> api = getAPI(inner_state);
Ref<LuaObjectMetatable> mt = arg1.get("lua_metatable");
Expand Down Expand Up @@ -783,6 +792,10 @@ void LuaState::createObjectMetatable() {
return 0;
});

lua_pushliteral(L, "__metatable");
lua_pushliteral(L, METATABLE_DISCLAIMER);
lua_settable(L, -3);

lua_pop(L, 1);
}

Expand All @@ -794,6 +807,10 @@ void LuaState::createCallableMetatable() {
lua_pushcfunction(L, luaCallableCall);
lua_settable(L, -3);

lua_pushliteral(L, "__metatable");
lua_pushliteral(L, METATABLE_DISCLAIMER);
lua_settable(L, -3);

lua_pop(L, 1);
}

Expand All @@ -814,5 +831,9 @@ void LuaState::createCallableExtraMetatable() {
lua_pushcfunction(L, LuaCallableExtra::call);
lua_settable(L, -3);

lua_pushliteral(L, "__metatable");
lua_pushliteral(L, METATABLE_DISCLAIMER);
lua_settable(L, -3);

lua_pop(L, 1);
}

0 comments on commit a7c659a

Please sign in to comment.