Skip to content
This repository was archived by the owner on Dec 29, 2024. It is now read-only.

Commit a7c659a

Browse files
authored
Stop metatables from being overwritten/read from (#184)
1 parent 6c31c63 commit a7c659a

5 files changed

Lines changed: 38 additions & 41 deletions

File tree

doc_classes/LuaObjectMetatable.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,6 @@
140140
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.
141141
</description>
142142
</method>
143-
<method name="__metatable" qualifiers="virtual">
144-
<return type="Variant" />
145-
<param index="0" name="obj" type="Object" />
146-
<param index="1" name="lua" type="LuaAPI" />
147-
<description>
148-
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.
149-
</description>
150-
</method>
151143
<method name="__mod" qualifiers="virtual">
152144
<return type="Variant" />
153145
<param index="0" name="obj" type="Object" />

src/classes/luaObjectMetatable.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ void LuaObjectMetatable::_bind_methods() {
1313
GDVIRTUAL_BIND(__call, "obj", "lua", "args");
1414
GDVIRTUAL_BIND(__gc, "obj", "lua");
1515
GDVIRTUAL_BIND(__tostring, "obj", "lua");
16-
GDVIRTUAL_BIND(__metatable, "obj", "lua");
1716
GDVIRTUAL_BIND(__len, "obj", "lua");
1817
GDVIRTUAL_BIND(__unm, "obj", "lua");
1918
GDVIRTUAL_BIND(__add, "obj", "lua", "other");
@@ -65,12 +64,6 @@ String LuaObjectMetatable::__tostring(Object *obj, Ref<LuaAPI> api) {
6564
return ret;
6665
}
6766

68-
Variant LuaObjectMetatable::__metatable(Object *obj, Ref<LuaAPI> api) {
69-
Variant ret;
70-
VIRTUAL_CALL(__metatable, ret, obj, api);
71-
return ret;
72-
}
73-
7467
int LuaObjectMetatable::__len(Object *obj, Ref<LuaAPI> api) {
7568
int ret = 0;
7669
VIRTUAL_CALL(__len, ret, obj, api);
@@ -274,14 +267,6 @@ String LuaDefaultObjectMetatable::__tostring(Object *obj, Ref<LuaAPI> api) {
274267
return String();
275268
}
276269

277-
Variant LuaDefaultObjectMetatable::__metatable(Object *obj, Ref<LuaAPI> api) {
278-
if (obj->has_method("__metatable")) {
279-
return obj->call("__metatable", api);
280-
}
281-
282-
return Variant();
283-
}
284-
285270
Variant LuaDefaultObjectMetatable::__unm(Object *obj, Ref<LuaAPI> api) {
286271
if (obj->has_method("__unm")) {
287272
return obj->call("__unm", api);

src/classes/luaObjectMetatable.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class LuaObjectMetatable : public RefCounted {
2828
GDVIRTUAL3R(Variant, __call, Object *, Ref<LuaAPI>, Ref<LuaTuple>);
2929
GDVIRTUAL2R(Ref<LuaError>, __gc, Object *, Ref<LuaAPI>);
3030
GDVIRTUAL2R(String, __tostring, Object *, Ref<LuaAPI>);
31-
GDVIRTUAL2R(Variant, __metatable, Object *, Ref<LuaAPI>);
3231
GDVIRTUAL2R(int, __len, Object *, Ref<LuaAPI>);
3332
GDVIRTUAL2R(Variant, __unm, Object *, Ref<LuaAPI>);
3433
GDVIRTUAL3R(Variant, __add, Object *, Ref<LuaAPI>, Variant);
@@ -56,7 +55,6 @@ class LuaObjectMetatable : public RefCounted {
5655
virtual Variant __call(Object *obj, Ref<LuaAPI> api, Ref<LuaTuple> args);
5756
virtual Ref<LuaError> __gc(Object *obj, Ref<LuaAPI> api);
5857
virtual String __tostring(Object *obj, Ref<LuaAPI> api);
59-
virtual Variant __metatable(Object *obj, Ref<LuaAPI> api);
6058
virtual int __len(Object *obj, Ref<LuaAPI> api);
6159
virtual Variant __unm(Object *obj, Ref<LuaAPI> api);
6260
virtual Variant __add(Object *obj, Ref<LuaAPI> api, Variant other);
@@ -94,7 +92,6 @@ class LuaDefaultObjectMetatable : public LuaObjectMetatable {
9492
Variant __call(Object *obj, Ref<LuaAPI> api, Ref<LuaTuple> args) override;
9593
Ref<LuaError> __gc(Object *obj, Ref<LuaAPI> api) override;
9694
String __tostring(Object *obj, Ref<LuaAPI> api) override;
97-
Variant __metatable(Object *obj, Ref<LuaAPI> api) override;
9895
int __len(Object *obj, Ref<LuaAPI> api) override;
9996
Variant __unm(Object *obj, Ref<LuaAPI> api) override;
10097
Variant __add(Object *obj, Ref<LuaAPI> api, Variant other) override;

src/luaState.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <classes/luaError.h>
1414
#include <lua/lua.hpp>
1515

16+
#define METATABLE_DISCLAIMER "This metatable is protected."
17+
1618
class LuaAPI;
1719

1820
class LuaState {

src/metatables.cpp

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ void LuaState::createVector2Metatable() {
162162
return 1;
163163
});
164164

165+
lua_pushliteral(L, "__metatable");
166+
lua_pushliteral(L, METATABLE_DISCLAIMER);
167+
lua_settable(L, -3);
168+
165169
lua_pop(L, 1); // Stack is now unmodified
166170
}
167171

@@ -230,6 +234,10 @@ void LuaState::createVector3Metatable() {
230234
return 1;
231235
});
232236

237+
lua_pushliteral(L, "__metatable");
238+
lua_pushliteral(L, METATABLE_DISCLAIMER);
239+
lua_settable(L, -3);
240+
233241
lua_pop(L, 1); // Stack is now unmodified
234242
}
235243

@@ -260,6 +268,10 @@ void LuaState::createRect2Metatable() {
260268
return 1;
261269
});
262270

271+
lua_pushliteral(L, "__metatable");
272+
lua_pushliteral(L, METATABLE_DISCLAIMER);
273+
lua_settable(L, -3);
274+
263275
lua_pop(L, 1); // Stack is now unmodified
264276
}
265277

@@ -290,6 +302,10 @@ void LuaState::createPlaneMetatable() {
290302
return 1;
291303
});
292304

305+
lua_pushliteral(L, "__metatable");
306+
lua_pushliteral(L, METATABLE_DISCLAIMER);
307+
lua_settable(L, -3);
308+
293309
lua_pop(L, 1); // Stack is now unmodified
294310
}
295311

@@ -358,6 +374,10 @@ void LuaState::createColorMetatable() {
358374
return 1;
359375
});
360376

377+
lua_pushliteral(L, "__metatable");
378+
lua_pushliteral(L, METATABLE_DISCLAIMER);
379+
lua_settable(L, -3);
380+
361381
lua_pop(L, 1); // Stack is now unmodified
362382
}
363383

@@ -377,6 +397,10 @@ void LuaState::createSignalMetatable() {
377397
return 1;
378398
});
379399

400+
lua_pushliteral(L, "__metatable");
401+
lua_pushliteral(L, METATABLE_DISCLAIMER);
402+
lua_settable(L, -3);
403+
380404
lua_pop(L, 1); // Stack is now unmodified
381405
}
382406

@@ -479,21 +503,6 @@ void LuaState::createObjectMetatable() {
479503
return 0;
480504
});
481505

482-
LUA_METAMETHOD_TEMPLATE(L, -1, "__metatable", {
483-
Ref<LuaAPI> api = getAPI(inner_state);
484-
Ref<LuaObjectMetatable> mt = arg1.get("lua_metatable");
485-
if (!mt.is_valid()) {
486-
mt = api->getObjectMetatable();
487-
}
488-
489-
if (mt.is_valid()) {
490-
LuaState::pushVariant(inner_state, mt->__metatable(arg1, api));
491-
return 1;
492-
}
493-
494-
return 0;
495-
});
496-
497506
LUA_METAMETHOD_TEMPLATE(L, -1, "__len", {
498507
Ref<LuaAPI> api = getAPI(inner_state);
499508
Ref<LuaObjectMetatable> mt = arg1.get("lua_metatable");
@@ -783,6 +792,10 @@ void LuaState::createObjectMetatable() {
783792
return 0;
784793
});
785794

795+
lua_pushliteral(L, "__metatable");
796+
lua_pushliteral(L, METATABLE_DISCLAIMER);
797+
lua_settable(L, -3);
798+
786799
lua_pop(L, 1);
787800
}
788801

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

810+
lua_pushliteral(L, "__metatable");
811+
lua_pushliteral(L, METATABLE_DISCLAIMER);
812+
lua_settable(L, -3);
813+
797814
lua_pop(L, 1);
798815
}
799816

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

834+
lua_pushliteral(L, "__metatable");
835+
lua_pushliteral(L, METATABLE_DISCLAIMER);
836+
lua_settable(L, -3);
837+
817838
lua_pop(L, 1);
818839
}

0 commit comments

Comments
 (0)