@@ -130,7 +130,14 @@ String LuaScript::_get_class_icon_path() const {
130130}
131131
132132bool LuaScript::_has_method (const StringName &p_method) const {
133- return methods.has (p_method);
133+ if (metatable.is_valid ()) {
134+ Variant value = metatable->get_value (p_method);
135+ LuaFunction *method = Object::cast_to<LuaFunction>(value);
136+ return method != nullptr ;
137+ }
138+ else {
139+ return false ;
140+ }
134141}
135142
136143bool LuaScript::_has_static_method (const StringName &p_method) const {
@@ -171,7 +178,13 @@ ScriptLanguage *LuaScript::_get_language() const {
171178}
172179
173180bool LuaScript::_has_script_signal (const StringName &p_signal) const {
174- return signals.has (p_signal);
181+ if (metatable.is_valid ()) {
182+ Variant value = metatable->get_value (p_signal);
183+ return value.get_type () == Variant::Type::SIGNAL;
184+ }
185+ else {
186+ return false ;
187+ }
175188}
176189
177190TypedArray<Dictionary> LuaScript::_get_script_signal_list () const {
@@ -241,89 +254,8 @@ Variant LuaScript::_new(const Variant **args, GDExtensionInt arg_count, GDExtens
241254 return new_instance;
242255}
243256
244- bool LuaScript::_instance_set (LuaScriptInstance *instance, const StringName& p_name, const Variant& p_value) const {
245- if (const Ref<LuaFunction> *_set = methods.getptr (" _set" ); _set && _set->is_valid ()) {
246- Variant result = _set->ptr ()->call_method (instance, p_name, p_value);
247- if (LuaError *lua_error = Object::cast_to<LuaError>(result)) {
248- ERR_FAIL_V_MSG (false , lua_error->get_message ());
249- }
250- return true ;
251- }
252-
253- if (const LuaScriptProperty *lua_prop = properties.getptr (p_name)) {
254- if (lua_prop->setter .is_valid ()) {
255- lua_prop->setter ->call_method (instance, p_value);
256- }
257- else {
258- instance->properties [p_name] = p_value;
259- }
260- return true ;
261- }
262- else {
263- return false ;
264- }
265- }
266-
267- bool LuaScript::_instance_get (LuaScriptInstance *instance, const StringName& p_name, Variant& p_value) const {
268- if (const Ref<LuaFunction> *_get = methods.getptr (" _get" ); _get && _get->is_valid ()) {
269- p_value = _get->ptr ()->call_method (instance, p_name);
270- if (LuaError *lua_error = Object::cast_to<LuaError>(p_value)) {
271- ERR_FAIL_V_MSG (false , lua_error->get_message ());
272- }
273- if (p_value != Variant ()) {
274- return true ;
275- }
276- }
277-
278- if (const LuaScriptProperty *lua_prop = properties.getptr (p_name)) {
279- if (lua_prop->getter .is_valid ()) {
280- p_value = lua_prop->getter ->call_method (instance);
281- }
282- else {
283- p_value = instance->properties .get (p_name, lua_prop->default_value );
284- }
285- return true ;
286- }
287- else if (instance->properties .has (p_name)) {
288- p_value = instance->properties [p_name];
289- return true ;
290- }
291- else {
292- return false ;
293- }
294- }
295-
296- Variant LuaScript::_instance_call (LuaScriptInstance *instance, const StringName& p_name, const Variant **p_args, GDExtensionInt p_argument_count, GDExtensionCallError& r_error) const {
297- if (const Ref<LuaFunction> *method = methods.getptr (p_name)) {
298- return method->ptr ()->invoke_method (instance, p_args, p_argument_count, r_error);
299- }
300- else {
301- r_error.error = GDEXTENSION_CALL_ERROR_INVALID_METHOD;
302- }
303- return {};
304- }
305-
306- void LuaScript::_instance_notification (LuaScriptInstance *instance, int32_t what, GDExtensionBool reversed) const {
307- if (const Ref<LuaFunction> *method = methods.getptr (" _notification" )) {
308- Variant result = method->ptr ()->call_method (instance, what, reversed);
309- if (LuaError *lua_error = Object::cast_to<LuaError>(result)) {
310- ERR_FAIL_MSG (lua_error->get_message ());
311- }
312- }
313- }
314-
315- bool LuaScript::_instance_tostring (LuaScriptInstance *instance, String& str) const {
316- if (const Ref<LuaFunction> *method = methods.getptr (" __tostring" )) {
317- Variant result = method->ptr ()->call_method (instance);
318- if (LuaError *lua_error = Object::cast_to<LuaError>(result)) {
319- ERR_FAIL_V_MSG (false , lua_error->get_message ());
320- }
321- str = result;
322- return true ;
323- }
324- else {
325- return false ;
326- }
257+ Ref<LuaTable> LuaScript::get_metatable () {
258+ return metatable;
327259}
328260
329261void LuaScript::_bind_methods () {
@@ -341,53 +273,12 @@ void LuaScript::process_script_result(const Variant& result) {
341273 }
342274
343275 metatable.reference_ptr (table);
344- methods.clear ();
345- properties.clear ();
346- signals.clear ();
347- if (!metatable.is_valid ()) {
348- return ;
349- }
350276
351- for (Variant key : *table) {
352- if (key == " extends" ) {
353- StringName class_name = table->get_value (key);
354- if (!ClassDB::class_exists (class_name)) {
355- WARN_PRINT (String (" Specified base class '%s' does not exist. Unsetting 'extends'" ) % Array::make (class_name));
356- table->set_value (key, Variant ());
357- }
358- // skip, not considered property
359- continue ;
360- }
361-
362- // skip special keys, they are not considered properties
363- if (key.in (Array::make (" class_name" , " icon" , " tool" ))) {
364- continue ;
365- }
366-
367- // methods, signals and properties must have string key
368- if (key.get_type () != Variant::STRING && key.get_type () != Variant::STRING_NAME) {
369- continue ;
370- }
371-
372- Variant value = table->get_value (key);
373- switch (value.get_type ()) {
374- case Variant::SIGNAL:
375- signals[key] = value;
376- break ;
377-
378- case Variant::OBJECT:
379- if (LuaFunction *method = Object::cast_to<LuaFunction>(value)) {
380- methods[key].reference_ptr (method);
381- }
382- // fallthrough
383- default :
384- // TODO: add support for property metadata
385- properties[key] = LuaScriptProperty {
386- .type = value.get_type (),
387- .default_value = value,
388- };
389- break ;
390- }
277+ // Remove "extends" field if it's invalid
278+ Variant base_class = table->get_value (" extends" );
279+ if (base_class && !ClassDB::class_exists (base_class)) {
280+ WARN_PRINT (String (" Specified base class '%s' does not exist. Unsetting 'extends'" ) % Array::make (base_class));
281+ table->set_value (" extends" , Variant ());
391282 }
392283}
393284
0 commit comments