1/**************************************************************************/
2/* gdscript_vm.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "gdscript.h"
32#include "gdscript_function.h"
33#include "gdscript_lambda_callable.h"
34
35#include "core/core_string_names.h"
36#include "core/os/os.h"
37
38#ifdef DEBUG_ENABLED
39static String _get_element_type(Variant::Type builtin_type, const StringName &native_type, const Ref<Script> &script_type) {
40 if (script_type.is_valid() && script_type->is_valid()) {
41 return GDScript::debug_get_script_name(script_type);
42 } else if (native_type != StringName()) {
43 return native_type.operator String();
44 } else {
45 return Variant::get_type_name(builtin_type);
46 }
47}
48
49static String _get_var_type(const Variant *p_var) {
50 String basestr;
51
52 if (p_var->get_type() == Variant::OBJECT) {
53 bool was_freed;
54 Object *bobj = p_var->get_validated_object_with_check(was_freed);
55 if (!bobj) {
56 if (was_freed) {
57 basestr = "previously freed";
58 } else {
59 basestr = "null instance";
60 }
61 } else {
62 if (bobj->is_class_ptr(GDScriptNativeClass::get_class_ptr_static())) {
63 basestr = Object::cast_to<GDScriptNativeClass>(bobj)->get_name();
64 } else {
65 basestr = bobj->get_class();
66 if (bobj->get_script_instance()) {
67 basestr += " (" + GDScript::debug_get_script_name(bobj->get_script_instance()->get_script()) + ")";
68 }
69 }
70 }
71
72 } else {
73 if (p_var->get_type() == Variant::ARRAY) {
74 basestr = "Array";
75 const Array *p_array = VariantInternal::get_array(p_var);
76 Variant::Type builtin_type = (Variant::Type)p_array->get_typed_builtin();
77 if (builtin_type != Variant::NIL) {
78 basestr += "[" + _get_element_type(builtin_type, p_array->get_typed_class_name(), p_array->get_typed_script()) + "]";
79 }
80 } else {
81 basestr = Variant::get_type_name(p_var->get_type());
82 }
83 }
84
85 return basestr;
86}
87#endif // DEBUG_ENABLED
88
89Variant GDScriptFunction::_get_default_variant_for_data_type(const GDScriptDataType &p_data_type) {
90 if (p_data_type.kind == GDScriptDataType::BUILTIN) {
91 if (p_data_type.builtin_type == Variant::ARRAY) {
92 Array array;
93 // Typed array.
94 if (p_data_type.has_container_element_type()) {
95 const GDScriptDataType &element_type = p_data_type.get_container_element_type();
96 array.set_typed(element_type.builtin_type, element_type.native_type, element_type.script_type);
97 }
98
99 return array;
100 } else {
101 Callable::CallError ce;
102 Variant variant;
103 Variant::construct(p_data_type.builtin_type, variant, nullptr, 0, ce);
104
105 ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, Variant());
106
107 return variant;
108 }
109 }
110
111 return Variant();
112}
113
114String GDScriptFunction::_get_call_error(const Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const {
115 String err_text;
116
117 if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
118 int errorarg = p_err.argument;
119 ERR_FAIL_COND_V_MSG(errorarg < 0 || argptrs[errorarg] == nullptr, "GDScript bug (please report): Invalid CallError argument index or null pointer.", "Invalid CallError argument index or null pointer.");
120 // Handle the Object to Object case separately as we don't have further class details.
121#ifdef DEBUG_ENABLED
122 if (p_err.expected == Variant::OBJECT && argptrs[errorarg]->get_type() == p_err.expected) {
123 err_text = "Invalid type in " + p_where + ". The Object-derived class of argument " + itos(errorarg + 1) + " (" + _get_var_type(argptrs[errorarg]) + ") is not a subclass of the expected argument class.";
124 } else if (p_err.expected == Variant::ARRAY && argptrs[errorarg]->get_type() == p_err.expected) {
125 err_text = "Invalid type in " + p_where + ". The array of argument " + itos(errorarg + 1) + " (" + _get_var_type(argptrs[errorarg]) + ") does not have the same element type as the expected typed array argument.";
126 } else
127#endif // DEBUG_ENABLED
128 {
129 err_text = "Invalid type in " + p_where + ". Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(p_err.expected)) + ".";
130 }
131 } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
132 err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.expected) + " arguments.";
133 } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
134 err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.expected) + " arguments.";
135 } else if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
136 err_text = "Invalid call. Nonexistent " + p_where + ".";
137 } else if (p_err.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
138 err_text = "Attempt to call " + p_where + " on a null instance.";
139 } else if (p_err.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
140 err_text = "Attempt to call " + p_where + " on a const instance.";
141 } else {
142 err_text = "Bug, call error: #" + itos(p_err.error);
143 }
144
145 return err_text;
146}
147
148void (*type_init_function_table[])(Variant *) = {
149 nullptr, // NIL (shouldn't be called).
150 &VariantInitializer<bool>::init, // BOOL.
151 &VariantInitializer<int64_t>::init, // INT.
152 &VariantInitializer<double>::init, // FLOAT.
153 &VariantInitializer<String>::init, // STRING.
154 &VariantInitializer<Vector2>::init, // VECTOR2.
155 &VariantInitializer<Vector2i>::init, // VECTOR2I.
156 &VariantInitializer<Rect2>::init, // RECT2.
157 &VariantInitializer<Rect2i>::init, // RECT2I.
158 &VariantInitializer<Vector3>::init, // VECTOR3.
159 &VariantInitializer<Vector3i>::init, // VECTOR3I.
160 &VariantInitializer<Transform2D>::init, // TRANSFORM2D.
161 &VariantInitializer<Vector4>::init, // VECTOR4.
162 &VariantInitializer<Vector4i>::init, // VECTOR4I.
163 &VariantInitializer<Plane>::init, // PLANE.
164 &VariantInitializer<Quaternion>::init, // QUATERNION.
165 &VariantInitializer<AABB>::init, // AABB.
166 &VariantInitializer<Basis>::init, // BASIS.
167 &VariantInitializer<Transform3D>::init, // TRANSFORM3D.
168 &VariantInitializer<Projection>::init, // PROJECTION.
169 &VariantInitializer<Color>::init, // COLOR.
170 &VariantInitializer<StringName>::init, // STRING_NAME.
171 &VariantInitializer<NodePath>::init, // NODE_PATH.
172 &VariantInitializer<RID>::init, // RID.
173 &VariantInitializer<Object *>::init, // OBJECT.
174 &VariantInitializer<Callable>::init, // CALLABLE.
175 &VariantInitializer<Signal>::init, // SIGNAL.
176 &VariantInitializer<Dictionary>::init, // DICTIONARY.
177 &VariantInitializer<Array>::init, // ARRAY.
178 &VariantInitializer<PackedByteArray>::init, // PACKED_BYTE_ARRAY.
179 &VariantInitializer<PackedInt32Array>::init, // PACKED_INT32_ARRAY.
180 &VariantInitializer<PackedInt64Array>::init, // PACKED_INT64_ARRAY.
181 &VariantInitializer<PackedFloat32Array>::init, // PACKED_FLOAT32_ARRAY.
182 &VariantInitializer<PackedFloat64Array>::init, // PACKED_FLOAT64_ARRAY.
183 &VariantInitializer<PackedStringArray>::init, // PACKED_STRING_ARRAY.
184 &VariantInitializer<PackedVector2Array>::init, // PACKED_VECTOR2_ARRAY.
185 &VariantInitializer<PackedVector3Array>::init, // PACKED_VECTOR3_ARRAY.
186 &VariantInitializer<PackedColorArray>::init, // PACKED_COLOR_ARRAY.
187};
188
189#if defined(__GNUC__)
190#define OPCODES_TABLE \
191 static const void *switch_table_ops[] = { \
192 &&OPCODE_OPERATOR, \
193 &&OPCODE_OPERATOR_VALIDATED, \
194 &&OPCODE_TYPE_TEST_BUILTIN, \
195 &&OPCODE_TYPE_TEST_ARRAY, \
196 &&OPCODE_TYPE_TEST_NATIVE, \
197 &&OPCODE_TYPE_TEST_SCRIPT, \
198 &&OPCODE_SET_KEYED, \
199 &&OPCODE_SET_KEYED_VALIDATED, \
200 &&OPCODE_SET_INDEXED_VALIDATED, \
201 &&OPCODE_GET_KEYED, \
202 &&OPCODE_GET_KEYED_VALIDATED, \
203 &&OPCODE_GET_INDEXED_VALIDATED, \
204 &&OPCODE_SET_NAMED, \
205 &&OPCODE_SET_NAMED_VALIDATED, \
206 &&OPCODE_GET_NAMED, \
207 &&OPCODE_GET_NAMED_VALIDATED, \
208 &&OPCODE_SET_MEMBER, \
209 &&OPCODE_GET_MEMBER, \
210 &&OPCODE_SET_STATIC_VARIABLE, \
211 &&OPCODE_GET_STATIC_VARIABLE, \
212 &&OPCODE_ASSIGN, \
213 &&OPCODE_ASSIGN_TRUE, \
214 &&OPCODE_ASSIGN_FALSE, \
215 &&OPCODE_ASSIGN_TYPED_BUILTIN, \
216 &&OPCODE_ASSIGN_TYPED_ARRAY, \
217 &&OPCODE_ASSIGN_TYPED_NATIVE, \
218 &&OPCODE_ASSIGN_TYPED_SCRIPT, \
219 &&OPCODE_CAST_TO_BUILTIN, \
220 &&OPCODE_CAST_TO_NATIVE, \
221 &&OPCODE_CAST_TO_SCRIPT, \
222 &&OPCODE_CONSTRUCT, \
223 &&OPCODE_CONSTRUCT_VALIDATED, \
224 &&OPCODE_CONSTRUCT_ARRAY, \
225 &&OPCODE_CONSTRUCT_TYPED_ARRAY, \
226 &&OPCODE_CONSTRUCT_DICTIONARY, \
227 &&OPCODE_CALL, \
228 &&OPCODE_CALL_RETURN, \
229 &&OPCODE_CALL_ASYNC, \
230 &&OPCODE_CALL_UTILITY, \
231 &&OPCODE_CALL_UTILITY_VALIDATED, \
232 &&OPCODE_CALL_GDSCRIPT_UTILITY, \
233 &&OPCODE_CALL_BUILTIN_TYPE_VALIDATED, \
234 &&OPCODE_CALL_SELF_BASE, \
235 &&OPCODE_CALL_METHOD_BIND, \
236 &&OPCODE_CALL_METHOD_BIND_RET, \
237 &&OPCODE_CALL_BUILTIN_STATIC, \
238 &&OPCODE_CALL_NATIVE_STATIC, \
239 &&OPCODE_CALL_PTRCALL_NO_RETURN, \
240 &&OPCODE_CALL_PTRCALL_BOOL, \
241 &&OPCODE_CALL_PTRCALL_INT, \
242 &&OPCODE_CALL_PTRCALL_FLOAT, \
243 &&OPCODE_CALL_PTRCALL_STRING, \
244 &&OPCODE_CALL_PTRCALL_VECTOR2, \
245 &&OPCODE_CALL_PTRCALL_VECTOR2I, \
246 &&OPCODE_CALL_PTRCALL_RECT2, \
247 &&OPCODE_CALL_PTRCALL_RECT2I, \
248 &&OPCODE_CALL_PTRCALL_VECTOR3, \
249 &&OPCODE_CALL_PTRCALL_VECTOR3I, \
250 &&OPCODE_CALL_PTRCALL_TRANSFORM2D, \
251 &&OPCODE_CALL_PTRCALL_VECTOR4, \
252 &&OPCODE_CALL_PTRCALL_VECTOR4I, \
253 &&OPCODE_CALL_PTRCALL_PLANE, \
254 &&OPCODE_CALL_PTRCALL_QUATERNION, \
255 &&OPCODE_CALL_PTRCALL_AABB, \
256 &&OPCODE_CALL_PTRCALL_BASIS, \
257 &&OPCODE_CALL_PTRCALL_TRANSFORM3D, \
258 &&OPCODE_CALL_PTRCALL_PROJECTION, \
259 &&OPCODE_CALL_PTRCALL_COLOR, \
260 &&OPCODE_CALL_PTRCALL_STRING_NAME, \
261 &&OPCODE_CALL_PTRCALL_NODE_PATH, \
262 &&OPCODE_CALL_PTRCALL_RID, \
263 &&OPCODE_CALL_PTRCALL_OBJECT, \
264 &&OPCODE_CALL_PTRCALL_CALLABLE, \
265 &&OPCODE_CALL_PTRCALL_SIGNAL, \
266 &&OPCODE_CALL_PTRCALL_DICTIONARY, \
267 &&OPCODE_CALL_PTRCALL_ARRAY, \
268 &&OPCODE_CALL_PTRCALL_PACKED_BYTE_ARRAY, \
269 &&OPCODE_CALL_PTRCALL_PACKED_INT32_ARRAY, \
270 &&OPCODE_CALL_PTRCALL_PACKED_INT64_ARRAY, \
271 &&OPCODE_CALL_PTRCALL_PACKED_FLOAT32_ARRAY, \
272 &&OPCODE_CALL_PTRCALL_PACKED_FLOAT64_ARRAY, \
273 &&OPCODE_CALL_PTRCALL_PACKED_STRING_ARRAY, \
274 &&OPCODE_CALL_PTRCALL_PACKED_VECTOR2_ARRAY, \
275 &&OPCODE_CALL_PTRCALL_PACKED_VECTOR3_ARRAY, \
276 &&OPCODE_CALL_PTRCALL_PACKED_COLOR_ARRAY, \
277 &&OPCODE_AWAIT, \
278 &&OPCODE_AWAIT_RESUME, \
279 &&OPCODE_CREATE_LAMBDA, \
280 &&OPCODE_CREATE_SELF_LAMBDA, \
281 &&OPCODE_JUMP, \
282 &&OPCODE_JUMP_IF, \
283 &&OPCODE_JUMP_IF_NOT, \
284 &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
285 &&OPCODE_JUMP_IF_SHARED, \
286 &&OPCODE_RETURN, \
287 &&OPCODE_RETURN_TYPED_BUILTIN, \
288 &&OPCODE_RETURN_TYPED_ARRAY, \
289 &&OPCODE_RETURN_TYPED_NATIVE, \
290 &&OPCODE_RETURN_TYPED_SCRIPT, \
291 &&OPCODE_ITERATE_BEGIN, \
292 &&OPCODE_ITERATE_BEGIN_INT, \
293 &&OPCODE_ITERATE_BEGIN_FLOAT, \
294 &&OPCODE_ITERATE_BEGIN_VECTOR2, \
295 &&OPCODE_ITERATE_BEGIN_VECTOR2I, \
296 &&OPCODE_ITERATE_BEGIN_VECTOR3, \
297 &&OPCODE_ITERATE_BEGIN_VECTOR3I, \
298 &&OPCODE_ITERATE_BEGIN_STRING, \
299 &&OPCODE_ITERATE_BEGIN_DICTIONARY, \
300 &&OPCODE_ITERATE_BEGIN_ARRAY, \
301 &&OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY, \
302 &&OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY, \
303 &&OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY, \
304 &&OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY, \
305 &&OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY, \
306 &&OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY, \
307 &&OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY, \
308 &&OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY, \
309 &&OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY, \
310 &&OPCODE_ITERATE_BEGIN_OBJECT, \
311 &&OPCODE_ITERATE, \
312 &&OPCODE_ITERATE_INT, \
313 &&OPCODE_ITERATE_FLOAT, \
314 &&OPCODE_ITERATE_VECTOR2, \
315 &&OPCODE_ITERATE_VECTOR2I, \
316 &&OPCODE_ITERATE_VECTOR3, \
317 &&OPCODE_ITERATE_VECTOR3I, \
318 &&OPCODE_ITERATE_STRING, \
319 &&OPCODE_ITERATE_DICTIONARY, \
320 &&OPCODE_ITERATE_ARRAY, \
321 &&OPCODE_ITERATE_PACKED_BYTE_ARRAY, \
322 &&OPCODE_ITERATE_PACKED_INT32_ARRAY, \
323 &&OPCODE_ITERATE_PACKED_INT64_ARRAY, \
324 &&OPCODE_ITERATE_PACKED_FLOAT32_ARRAY, \
325 &&OPCODE_ITERATE_PACKED_FLOAT64_ARRAY, \
326 &&OPCODE_ITERATE_PACKED_STRING_ARRAY, \
327 &&OPCODE_ITERATE_PACKED_VECTOR2_ARRAY, \
328 &&OPCODE_ITERATE_PACKED_VECTOR3_ARRAY, \
329 &&OPCODE_ITERATE_PACKED_COLOR_ARRAY, \
330 &&OPCODE_ITERATE_OBJECT, \
331 &&OPCODE_STORE_GLOBAL, \
332 &&OPCODE_STORE_NAMED_GLOBAL, \
333 &&OPCODE_TYPE_ADJUST_BOOL, \
334 &&OPCODE_TYPE_ADJUST_INT, \
335 &&OPCODE_TYPE_ADJUST_FLOAT, \
336 &&OPCODE_TYPE_ADJUST_STRING, \
337 &&OPCODE_TYPE_ADJUST_VECTOR2, \
338 &&OPCODE_TYPE_ADJUST_VECTOR2I, \
339 &&OPCODE_TYPE_ADJUST_RECT2, \
340 &&OPCODE_TYPE_ADJUST_RECT2I, \
341 &&OPCODE_TYPE_ADJUST_VECTOR3, \
342 &&OPCODE_TYPE_ADJUST_VECTOR3I, \
343 &&OPCODE_TYPE_ADJUST_TRANSFORM2D, \
344 &&OPCODE_TYPE_ADJUST_VECTOR4, \
345 &&OPCODE_TYPE_ADJUST_VECTOR4I, \
346 &&OPCODE_TYPE_ADJUST_PLANE, \
347 &&OPCODE_TYPE_ADJUST_QUATERNION, \
348 &&OPCODE_TYPE_ADJUST_AABB, \
349 &&OPCODE_TYPE_ADJUST_BASIS, \
350 &&OPCODE_TYPE_ADJUST_TRANSFORM3D, \
351 &&OPCODE_TYPE_ADJUST_PROJECTION, \
352 &&OPCODE_TYPE_ADJUST_COLOR, \
353 &&OPCODE_TYPE_ADJUST_STRING_NAME, \
354 &&OPCODE_TYPE_ADJUST_NODE_PATH, \
355 &&OPCODE_TYPE_ADJUST_RID, \
356 &&OPCODE_TYPE_ADJUST_OBJECT, \
357 &&OPCODE_TYPE_ADJUST_CALLABLE, \
358 &&OPCODE_TYPE_ADJUST_SIGNAL, \
359 &&OPCODE_TYPE_ADJUST_DICTIONARY, \
360 &&OPCODE_TYPE_ADJUST_ARRAY, \
361 &&OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY, \
362 &&OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY, \
363 &&OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY, \
364 &&OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY, \
365 &&OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY, \
366 &&OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY, \
367 &&OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY, \
368 &&OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY, \
369 &&OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY, \
370 &&OPCODE_ASSERT, \
371 &&OPCODE_BREAKPOINT, \
372 &&OPCODE_LINE, \
373 &&OPCODE_END \
374 }; \
375 static_assert((sizeof(switch_table_ops) / sizeof(switch_table_ops[0]) == (OPCODE_END + 1)), "Opcodes in jump table aren't the same as opcodes in enum.");
376
377#define OPCODE(m_op) \
378 m_op:
379#define OPCODE_WHILE(m_test)
380#define OPCODES_END \
381 OPSEXIT:
382#define OPCODES_OUT \
383 OPSOUT:
384#define OPCODE_SWITCH(m_test) goto *switch_table_ops[m_test];
385#ifdef DEBUG_ENABLED
386#define DISPATCH_OPCODE \
387 last_opcode = _code_ptr[ip]; \
388 goto *switch_table_ops[last_opcode]
389#else
390#define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]]
391#endif
392#define OPCODE_BREAK goto OPSEXIT
393#define OPCODE_OUT goto OPSOUT
394#else
395#define OPCODES_TABLE
396#define OPCODE(m_op) case m_op:
397#define OPCODE_WHILE(m_test) while (m_test)
398#define OPCODES_END
399#define OPCODES_OUT
400#define DISPATCH_OPCODE continue
401#define OPCODE_SWITCH(m_test) switch (m_test)
402#define OPCODE_BREAK break
403#define OPCODE_OUT break
404#endif
405
406// Helpers for VariantInternal methods in macros.
407#define OP_GET_BOOL get_bool
408#define OP_GET_INT get_int
409#define OP_GET_FLOAT get_float
410#define OP_GET_VECTOR2 get_vector2
411#define OP_GET_VECTOR2I get_vector2i
412#define OP_GET_VECTOR3 get_vector3
413#define OP_GET_VECTOR3I get_vector3i
414#define OP_GET_RECT2 get_rect2
415#define OP_GET_VECTOR4 get_vector4
416#define OP_GET_VECTOR4I get_vector4i
417#define OP_GET_RECT2I get_rect2i
418#define OP_GET_QUATERNION get_quaternion
419#define OP_GET_COLOR get_color
420#define OP_GET_STRING get_string
421#define OP_GET_STRING_NAME get_string_name
422#define OP_GET_NODE_PATH get_node_path
423#define OP_GET_CALLABLE get_callable
424#define OP_GET_SIGNAL get_signal
425#define OP_GET_ARRAY get_array
426#define OP_GET_DICTIONARY get_dictionary
427#define OP_GET_PACKED_BYTE_ARRAY get_byte_array
428#define OP_GET_PACKED_INT32_ARRAY get_int32_array
429#define OP_GET_PACKED_INT64_ARRAY get_int64_array
430#define OP_GET_PACKED_FLOAT32_ARRAY get_float32_array
431#define OP_GET_PACKED_FLOAT64_ARRAY get_float64_array
432#define OP_GET_PACKED_STRING_ARRAY get_string_array
433#define OP_GET_PACKED_VECTOR2_ARRAY get_vector2_array
434#define OP_GET_PACKED_VECTOR3_ARRAY get_vector3_array
435#define OP_GET_PACKED_COLOR_ARRAY get_color_array
436#define OP_GET_TRANSFORM3D get_transform
437#define OP_GET_TRANSFORM2D get_transform2d
438#define OP_GET_PROJECTION get_projection
439#define OP_GET_PLANE get_plane
440#define OP_GET_AABB get_aabb
441#define OP_GET_BASIS get_basis
442#define OP_GET_RID get_rid
443
444#define METHOD_CALL_ON_NULL_VALUE_ERROR(method_pointer) "Cannot call method '" + (method_pointer)->get_name() + "' on a null value."
445#define METHOD_CALL_ON_FREED_INSTANCE_ERROR(method_pointer) "Cannot call method '" + (method_pointer)->get_name() + "' on a previously freed instance."
446
447Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state) {
448 OPCODES_TABLE;
449
450 if (!_code_ptr) {
451 return _get_default_variant_for_data_type(return_type);
452 }
453
454 r_err.error = Callable::CallError::CALL_OK;
455
456 static thread_local int call_depth = 0;
457 if (unlikely(++call_depth > MAX_CALL_DEPTH)) {
458 call_depth--;
459#ifdef DEBUG_ENABLED
460 String err_file;
461 if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && !p_instance->script->path.is_empty()) {
462 err_file = p_instance->script->path;
463 } else if (_script) {
464 err_file = _script->path;
465 }
466 if (err_file.is_empty()) {
467 err_file = "<built-in>";
468 }
469 String err_func = name;
470 if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->local_name != StringName()) {
471 err_func = p_instance->script->local_name.operator String() + "." + err_func;
472 }
473 int err_line = _initial_line;
474 const char *err_text = "Stack overflow. Check for infinite recursion in your script.";
475 if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
476 // Debugger break did not happen.
477 _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text, false, ERR_HANDLER_SCRIPT);
478 }
479#endif
480 return _get_default_variant_for_data_type(return_type);
481 }
482
483 Variant retvalue;
484 Variant *stack = nullptr;
485 Variant **instruction_args = nullptr;
486 const void **call_args_ptr = nullptr;
487 int defarg = 0;
488
489#ifdef DEBUG_ENABLED
490
491 //GDScriptLanguage::get_singleton()->calls++;
492
493#endif
494
495 uint32_t alloca_size = 0;
496 GDScript *script;
497 int ip = 0;
498 int line = _initial_line;
499
500 if (p_state) {
501 //use existing (supplied) state (awaited)
502 stack = (Variant *)p_state->stack.ptr();
503 instruction_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
504 line = p_state->line;
505 ip = p_state->ip;
506 alloca_size = p_state->stack.size();
507 script = p_state->script;
508 p_instance = p_state->instance;
509 defarg = p_state->defarg;
510
511 } else {
512 if (p_argcount != _argument_count) {
513 if (p_argcount > _argument_count) {
514 r_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
515 r_err.expected = _argument_count;
516
517 call_depth--;
518 return _get_default_variant_for_data_type(return_type);
519 } else if (p_argcount < _argument_count - _default_arg_count) {
520 r_err.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
521 r_err.expected = _argument_count - _default_arg_count;
522 call_depth--;
523 return _get_default_variant_for_data_type(return_type);
524 } else {
525 defarg = _argument_count - p_argcount;
526 }
527 }
528
529 // Add 3 here for self, class, and nil.
530 alloca_size = sizeof(Variant *) * 3 + sizeof(Variant *) * _instruction_args_size + sizeof(Variant) * _stack_size;
531
532 uint8_t *aptr = (uint8_t *)alloca(alloca_size);
533 stack = (Variant *)aptr;
534
535 for (int i = 0; i < p_argcount; i++) {
536 if (!argument_types[i].has_type) {
537 memnew_placement(&stack[i + 3], Variant(*p_args[i]));
538 continue;
539 }
540 // If types already match, don't call Variant::construct(). Constructors of some types
541 // (e.g. packed arrays) do copies, whereas they pass by reference when inside a Variant.
542 if (argument_types[i].is_type(*p_args[i], false)) {
543 memnew_placement(&stack[i + 3], Variant(*p_args[i]));
544 continue;
545 }
546 if (!argument_types[i].is_type(*p_args[i], true)) {
547 r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
548 r_err.argument = i;
549 r_err.expected = argument_types[i].builtin_type;
550 call_depth--;
551 return _get_default_variant_for_data_type(return_type);
552 }
553 if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
554 Variant arg;
555 Variant::construct(argument_types[i].builtin_type, arg, &p_args[i], 1, r_err);
556 memnew_placement(&stack[i + 3], Variant(arg));
557 } else {
558 memnew_placement(&stack[i + 3], Variant(*p_args[i]));
559 }
560 }
561 for (int i = p_argcount + 3; i < _stack_size; i++) {
562 memnew_placement(&stack[i], Variant);
563 }
564
565 if (_instruction_args_size) {
566 instruction_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
567 } else {
568 instruction_args = nullptr;
569 }
570
571 for (const KeyValue<int, Variant::Type> &E : temporary_slots) {
572 type_init_function_table[E.value](&stack[E.key]);
573 }
574 }
575
576 if (_ptrcall_args_size) {
577 call_args_ptr = (const void **)alloca(_ptrcall_args_size * sizeof(void *));
578 } else {
579 call_args_ptr = nullptr;
580 }
581
582 if (p_instance) {
583 memnew_placement(&stack[ADDR_STACK_SELF], Variant(p_instance->owner));
584 script = p_instance->script.ptr();
585 } else {
586 memnew_placement(&stack[ADDR_STACK_SELF], Variant);
587 script = _script;
588 }
589 memnew_placement(&stack[ADDR_STACK_CLASS], Variant(script));
590 memnew_placement(&stack[ADDR_STACK_NIL], Variant);
591
592 String err_text;
593
594#ifdef DEBUG_ENABLED
595
596 if (EngineDebugger::is_active()) {
597 GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
598 }
599
600#define GD_ERR_BREAK(m_cond) \
601 { \
602 if (unlikely(m_cond)) { \
603 _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
604 OPCODE_BREAK; \
605 } \
606 }
607
608#define CHECK_SPACE(m_space) \
609 GD_ERR_BREAK((ip + m_space) > _code_size)
610
611#define GET_VARIANT_PTR(m_v, m_code_ofs) \
612 Variant *m_v; \
613 { \
614 int address = _code_ptr[ip + 1 + (m_code_ofs)]; \
615 int address_type = (address & ADDR_TYPE_MASK) >> ADDR_BITS; \
616 if (unlikely(address_type < 0 || address_type >= ADDR_TYPE_MAX)) { \
617 err_text = "Bad address type."; \
618 OPCODE_BREAK; \
619 } \
620 int address_index = address & ADDR_MASK; \
621 if (unlikely(address_index < 0 || address_index >= variant_address_limits[address_type])) { \
622 if (address_type == ADDR_TYPE_MEMBER && !p_instance) { \
623 err_text = "Cannot access member without instance."; \
624 } else { \
625 err_text = "Bad address index."; \
626 } \
627 OPCODE_BREAK; \
628 } \
629 m_v = &variant_addresses[address_type][address_index]; \
630 if (unlikely(!m_v)) \
631 OPCODE_BREAK; \
632 }
633
634#else
635#define GD_ERR_BREAK(m_cond)
636#define CHECK_SPACE(m_space)
637
638#define GET_VARIANT_PTR(m_v, m_code_ofs) \
639 Variant *m_v; \
640 { \
641 int address = _code_ptr[ip + 1 + (m_code_ofs)]; \
642 m_v = &variant_addresses[(address & ADDR_TYPE_MASK) >> ADDR_BITS][address & ADDR_MASK]; \
643 if (unlikely(!m_v)) \
644 OPCODE_BREAK; \
645 }
646
647#endif
648
649#define LOAD_INSTRUCTION_ARGS \
650 int instr_arg_count = _code_ptr[ip + 1]; \
651 for (int i = 0; i < instr_arg_count; i++) { \
652 GET_VARIANT_PTR(v, i + 1); \
653 instruction_args[i] = v; \
654 } \
655 ip += 1; // Offset to skip instruction argcount.
656
657#define GET_INSTRUCTION_ARG(m_v, m_idx) \
658 Variant *m_v = instruction_args[m_idx]
659
660#ifdef DEBUG_ENABLED
661 uint64_t function_start_time = 0;
662 uint64_t function_call_time = 0;
663
664 if (GDScriptLanguage::get_singleton()->profiling) {
665 function_start_time = OS::get_singleton()->get_ticks_usec();
666 function_call_time = 0;
667 profile.call_count.increment();
668 profile.frame_call_count.increment();
669 }
670 bool exit_ok = false;
671 bool awaited = false;
672#endif
673
674#ifdef DEBUG_ENABLED
675 int variant_address_limits[ADDR_TYPE_MAX] = { _stack_size, _constant_count, p_instance ? p_instance->members.size() : 0 };
676#endif
677
678 Variant *variant_addresses[ADDR_TYPE_MAX] = { stack, _constants_ptr, p_instance ? p_instance->members.ptrw() : nullptr };
679
680#ifdef DEBUG_ENABLED
681 OPCODE_WHILE(ip < _code_size) {
682 int last_opcode = _code_ptr[ip];
683#else
684 OPCODE_WHILE(true) {
685#endif
686
687 OPCODE_SWITCH(_code_ptr[ip]) {
688 OPCODE(OPCODE_OPERATOR) {
689 constexpr int _pointer_size = sizeof(Variant::ValidatedOperatorEvaluator) / sizeof(*_code_ptr);
690 CHECK_SPACE(7 + _pointer_size);
691
692 bool valid;
693 Variant::Operator op = (Variant::Operator)_code_ptr[ip + 4];
694 GD_ERR_BREAK(op >= Variant::OP_MAX);
695
696 GET_VARIANT_PTR(a, 0);
697 GET_VARIANT_PTR(b, 1);
698 GET_VARIANT_PTR(dst, 2);
699 // Compute signatures (types of operands) so it can be optimized when matching.
700 uint32_t op_signature = _code_ptr[ip + 5];
701 uint32_t actual_signature = (a->get_type() << 8) | (b->get_type());
702
703 // Check if this is the first run. If so, store the current signature for the optimized path.
704 if (unlikely(op_signature == 0)) {
705 static Mutex initializer_mutex;
706 initializer_mutex.lock();
707 Variant::Type a_type = (Variant::Type)((actual_signature >> 8) & 0xFF);
708 Variant::Type b_type = (Variant::Type)(actual_signature & 0xFF);
709
710 Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(op, a_type, b_type);
711
712 if (unlikely(!op_func)) {
713#ifdef DEBUG_ENABLED
714 err_text = "Invalid operands '" + Variant::get_type_name(a->get_type()) + "' and '" + Variant::get_type_name(b->get_type()) + "' in operator '" + Variant::get_operator_name(op) + "'.";
715#endif
716 initializer_mutex.unlock();
717 OPCODE_BREAK;
718 } else {
719 Variant::Type ret_type = Variant::get_operator_return_type(op, a_type, b_type);
720 VariantInternal::initialize(dst, ret_type);
721 op_func(a, b, dst);
722
723 // Check again in case another thread already set it.
724 if (_code_ptr[ip + 5] == 0) {
725 _code_ptr[ip + 5] = actual_signature;
726 _code_ptr[ip + 6] = static_cast<int>(ret_type);
727 Variant::ValidatedOperatorEvaluator *tmp = reinterpret_cast<Variant::ValidatedOperatorEvaluator *>(&_code_ptr[ip + 7]);
728 *tmp = op_func;
729 }
730 }
731 initializer_mutex.unlock();
732 } else if (likely(op_signature == actual_signature)) {
733 // If the signature matches, we can use the optimized path.
734 Variant::Type ret_type = static_cast<Variant::Type>(_code_ptr[ip + 6]);
735 Variant::ValidatedOperatorEvaluator op_func = *reinterpret_cast<Variant::ValidatedOperatorEvaluator *>(&_code_ptr[ip + 7]);
736
737 // Make sure the return value has the correct type.
738 VariantInternal::initialize(dst, ret_type);
739 op_func(a, b, dst);
740 } else {
741 // If the signature doesn't match, we have to use the slow path.
742#ifdef DEBUG_ENABLED
743
744 Variant ret;
745 Variant::evaluate(op, *a, *b, ret, valid);
746#else
747 Variant::evaluate(op, *a, *b, *dst, valid);
748#endif
749#ifdef DEBUG_ENABLED
750 if (!valid) {
751 if (ret.get_type() == Variant::STRING) {
752 //return a string when invalid with the error
753 err_text = ret;
754 err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
755 } else {
756 err_text = "Invalid operands '" + Variant::get_type_name(a->get_type()) + "' and '" + Variant::get_type_name(b->get_type()) + "' in operator '" + Variant::get_operator_name(op) + "'.";
757 }
758 OPCODE_BREAK;
759 }
760 *dst = ret;
761#endif
762 }
763 ip += 7 + _pointer_size;
764 }
765 DISPATCH_OPCODE;
766
767 OPCODE(OPCODE_OPERATOR_VALIDATED) {
768 CHECK_SPACE(5);
769
770 int operator_idx = _code_ptr[ip + 4];
771 GD_ERR_BREAK(operator_idx < 0 || operator_idx >= _operator_funcs_count);
772 Variant::ValidatedOperatorEvaluator operator_func = _operator_funcs_ptr[operator_idx];
773
774 GET_VARIANT_PTR(a, 0);
775 GET_VARIANT_PTR(b, 1);
776 GET_VARIANT_PTR(dst, 2);
777
778 operator_func(a, b, dst);
779
780 ip += 5;
781 }
782 DISPATCH_OPCODE;
783
784 OPCODE(OPCODE_TYPE_TEST_BUILTIN) {
785 CHECK_SPACE(4);
786
787 GET_VARIANT_PTR(dst, 0);
788 GET_VARIANT_PTR(value, 1);
789
790 Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 3];
791 GD_ERR_BREAK(builtin_type < 0 || builtin_type >= Variant::VARIANT_MAX);
792
793 *dst = value->get_type() == builtin_type;
794 ip += 4;
795 }
796 DISPATCH_OPCODE;
797
798 OPCODE(OPCODE_TYPE_TEST_ARRAY) {
799 CHECK_SPACE(6);
800
801 GET_VARIANT_PTR(dst, 0);
802 GET_VARIANT_PTR(value, 1);
803
804 GET_VARIANT_PTR(script_type, 2);
805 Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 4];
806 int native_type_idx = _code_ptr[ip + 5];
807 GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
808 const StringName native_type = _global_names_ptr[native_type_idx];
809
810 bool result = false;
811 if (value->get_type() == Variant::ARRAY) {
812 Array *array = VariantInternal::get_array(value);
813 result = array->get_typed_builtin() == ((uint32_t)builtin_type) && array->get_typed_class_name() == native_type && array->get_typed_script() == *script_type;
814 }
815
816 *dst = result;
817 ip += 6;
818 }
819 DISPATCH_OPCODE;
820
821 OPCODE(OPCODE_TYPE_TEST_NATIVE) {
822 CHECK_SPACE(4);
823
824 GET_VARIANT_PTR(dst, 0);
825 GET_VARIANT_PTR(value, 1);
826
827 int native_type_idx = _code_ptr[ip + 3];
828 GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
829 const StringName native_type = _global_names_ptr[native_type_idx];
830
831 bool was_freed = false;
832 Object *object = value->get_validated_object_with_check(was_freed);
833 if (was_freed) {
834 err_text = "Left operand of 'is' is a previously freed instance.";
835 OPCODE_BREAK;
836 }
837
838 *dst = object && ClassDB::is_parent_class(object->get_class_name(), native_type);
839 ip += 4;
840 }
841 DISPATCH_OPCODE;
842
843 OPCODE(OPCODE_TYPE_TEST_SCRIPT) {
844 CHECK_SPACE(4);
845
846 GET_VARIANT_PTR(dst, 0);
847 GET_VARIANT_PTR(value, 1);
848
849 GET_VARIANT_PTR(type, 2);
850 Script *script_type = Object::cast_to<Script>(type->operator Object *());
851 GD_ERR_BREAK(!script_type);
852
853 bool was_freed = false;
854 Object *object = value->get_validated_object_with_check(was_freed);
855 if (was_freed) {
856 err_text = "Left operand of 'is' is a previously freed instance.";
857 OPCODE_BREAK;
858 }
859
860 bool result = false;
861 if (object && object->get_script_instance()) {
862 Script *script_ptr = object->get_script_instance()->get_script().ptr();
863 while (script_ptr) {
864 if (script_ptr == script_type) {
865 result = true;
866 break;
867 }
868 script_ptr = script_ptr->get_base_script().ptr();
869 }
870 }
871
872 *dst = result;
873 ip += 4;
874 }
875 DISPATCH_OPCODE;
876
877 OPCODE(OPCODE_SET_KEYED) {
878 CHECK_SPACE(3);
879
880 GET_VARIANT_PTR(dst, 0);
881 GET_VARIANT_PTR(index, 1);
882 GET_VARIANT_PTR(value, 2);
883
884 bool valid;
885 dst->set(*index, *value, &valid);
886
887#ifdef DEBUG_ENABLED
888 if (!valid) {
889 Object *obj = dst->get_validated_object();
890 String v = index->operator String();
891 bool read_only_property = false;
892 if (obj) {
893 read_only_property = ClassDB::has_property(obj->get_class_name(), v) && (ClassDB::get_property_setter(obj->get_class_name(), v) == StringName());
894 }
895 if (read_only_property) {
896 err_text = vformat(R"(Cannot set value into property "%s" (on base "%s") because it is read-only.)", v, _get_var_type(dst));
897 } else {
898 if (!v.is_empty()) {
899 v = "'" + v + "'";
900 } else {
901 v = "of type '" + _get_var_type(index) + "'";
902 }
903 err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
904 }
905 OPCODE_BREAK;
906 }
907#endif
908 ip += 4;
909 }
910 DISPATCH_OPCODE;
911
912 OPCODE(OPCODE_SET_KEYED_VALIDATED) {
913 CHECK_SPACE(4);
914
915 GET_VARIANT_PTR(dst, 0);
916 GET_VARIANT_PTR(index, 1);
917 GET_VARIANT_PTR(value, 2);
918
919 int index_setter = _code_ptr[ip + 4];
920 GD_ERR_BREAK(index_setter < 0 || index_setter >= _keyed_setters_count);
921 const Variant::ValidatedKeyedSetter setter = _keyed_setters_ptr[index_setter];
922
923 bool valid;
924 setter(dst, index, value, &valid);
925
926#ifdef DEBUG_ENABLED
927 if (!valid) {
928 String v = index->operator String();
929 if (!v.is_empty()) {
930 v = "'" + v + "'";
931 } else {
932 v = "of type '" + _get_var_type(index) + "'";
933 }
934 err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
935 OPCODE_BREAK;
936 }
937#endif
938 ip += 5;
939 }
940 DISPATCH_OPCODE;
941
942 OPCODE(OPCODE_SET_INDEXED_VALIDATED) {
943 CHECK_SPACE(4);
944
945 GET_VARIANT_PTR(dst, 0);
946 GET_VARIANT_PTR(index, 1);
947 GET_VARIANT_PTR(value, 2);
948
949 int index_setter = _code_ptr[ip + 4];
950 GD_ERR_BREAK(index_setter < 0 || index_setter >= _indexed_setters_count);
951 const Variant::ValidatedIndexedSetter setter = _indexed_setters_ptr[index_setter];
952
953 int64_t int_index = *VariantInternal::get_int(index);
954
955 bool oob;
956 setter(dst, int_index, value, &oob);
957
958#ifdef DEBUG_ENABLED
959 if (oob) {
960 String v = index->operator String();
961 if (!v.is_empty()) {
962 v = "'" + v + "'";
963 } else {
964 v = "of type '" + _get_var_type(index) + "'";
965 }
966 err_text = "Out of bounds set index " + v + " (on base: '" + _get_var_type(dst) + "')";
967 OPCODE_BREAK;
968 }
969#endif
970 ip += 5;
971 }
972 DISPATCH_OPCODE;
973
974 OPCODE(OPCODE_GET_KEYED) {
975 CHECK_SPACE(3);
976
977 GET_VARIANT_PTR(src, 0);
978 GET_VARIANT_PTR(index, 1);
979 GET_VARIANT_PTR(dst, 2);
980
981 bool valid;
982#ifdef DEBUG_ENABLED
983 // Allow better error message in cases where src and dst are the same stack position.
984 Variant ret = src->get(*index, &valid);
985#else
986 *dst = src->get(*index, &valid);
987
988#endif
989#ifdef DEBUG_ENABLED
990 if (!valid) {
991 String v = index->operator String();
992 if (!v.is_empty()) {
993 v = "'" + v + "'";
994 } else {
995 v = "of type '" + _get_var_type(index) + "'";
996 }
997 err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
998 OPCODE_BREAK;
999 }
1000 *dst = ret;
1001#endif
1002 ip += 4;
1003 }
1004 DISPATCH_OPCODE;
1005
1006 OPCODE(OPCODE_GET_KEYED_VALIDATED) {
1007 CHECK_SPACE(4);
1008
1009 GET_VARIANT_PTR(src, 0);
1010 GET_VARIANT_PTR(key, 1);
1011 GET_VARIANT_PTR(dst, 2);
1012
1013 int index_getter = _code_ptr[ip + 4];
1014 GD_ERR_BREAK(index_getter < 0 || index_getter >= _keyed_getters_count);
1015 const Variant::ValidatedKeyedGetter getter = _keyed_getters_ptr[index_getter];
1016
1017 bool valid;
1018#ifdef DEBUG_ENABLED
1019 // Allow better error message in cases where src and dst are the same stack position.
1020 Variant ret;
1021 getter(src, key, &ret, &valid);
1022#else
1023 getter(src, key, dst, &valid);
1024#endif
1025#ifdef DEBUG_ENABLED
1026 if (!valid) {
1027 String v = key->operator String();
1028 if (!v.is_empty()) {
1029 v = "'" + v + "'";
1030 } else {
1031 v = "of type '" + _get_var_type(key) + "'";
1032 }
1033 err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
1034 OPCODE_BREAK;
1035 }
1036 *dst = ret;
1037#endif
1038 ip += 5;
1039 }
1040 DISPATCH_OPCODE;
1041
1042 OPCODE(OPCODE_GET_INDEXED_VALIDATED) {
1043 CHECK_SPACE(4);
1044
1045 GET_VARIANT_PTR(src, 0);
1046 GET_VARIANT_PTR(index, 1);
1047 GET_VARIANT_PTR(dst, 2);
1048
1049 int index_getter = _code_ptr[ip + 4];
1050 GD_ERR_BREAK(index_getter < 0 || index_getter >= _indexed_getters_count);
1051 const Variant::ValidatedIndexedGetter getter = _indexed_getters_ptr[index_getter];
1052
1053 int64_t int_index = *VariantInternal::get_int(index);
1054
1055 bool oob;
1056 getter(src, int_index, dst, &oob);
1057
1058#ifdef DEBUG_ENABLED
1059 if (oob) {
1060 String v = index->operator String();
1061 if (!v.is_empty()) {
1062 v = "'" + v + "'";
1063 } else {
1064 v = "of type '" + _get_var_type(index) + "'";
1065 }
1066 err_text = "Out of bounds get index " + v + " (on base: '" + _get_var_type(src) + "')";
1067 OPCODE_BREAK;
1068 }
1069#endif
1070 ip += 5;
1071 }
1072 DISPATCH_OPCODE;
1073
1074 OPCODE(OPCODE_SET_NAMED) {
1075 CHECK_SPACE(3);
1076
1077 GET_VARIANT_PTR(dst, 0);
1078 GET_VARIANT_PTR(value, 1);
1079
1080 int indexname = _code_ptr[ip + 3];
1081
1082 GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
1083 const StringName *index = &_global_names_ptr[indexname];
1084
1085 bool valid;
1086 dst->set_named(*index, *value, valid);
1087
1088#ifdef DEBUG_ENABLED
1089 if (!valid) {
1090 Object *obj = dst->get_validated_object();
1091 bool read_only_property = false;
1092 if (obj) {
1093 read_only_property = ClassDB::has_property(obj->get_class_name(), *index) && (ClassDB::get_property_setter(obj->get_class_name(), *index) == StringName());
1094 }
1095 if (read_only_property) {
1096 err_text = vformat(R"(Cannot set value into property "%s" (on base "%s") because it is read-only.)", String(*index), _get_var_type(dst));
1097 } else {
1098 err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
1099 }
1100 OPCODE_BREAK;
1101 }
1102#endif
1103 ip += 4;
1104 }
1105 DISPATCH_OPCODE;
1106
1107 OPCODE(OPCODE_SET_NAMED_VALIDATED) {
1108 CHECK_SPACE(3);
1109
1110 GET_VARIANT_PTR(dst, 0);
1111 GET_VARIANT_PTR(value, 1);
1112
1113 int index_setter = _code_ptr[ip + 3];
1114 GD_ERR_BREAK(index_setter < 0 || index_setter >= _setters_count);
1115 const Variant::ValidatedSetter setter = _setters_ptr[index_setter];
1116
1117 setter(dst, value);
1118 ip += 4;
1119 }
1120 DISPATCH_OPCODE;
1121
1122 OPCODE(OPCODE_GET_NAMED) {
1123 CHECK_SPACE(4);
1124
1125 GET_VARIANT_PTR(src, 0);
1126 GET_VARIANT_PTR(dst, 1);
1127
1128 int indexname = _code_ptr[ip + 3];
1129
1130 GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
1131 const StringName *index = &_global_names_ptr[indexname];
1132
1133 bool valid;
1134#ifdef DEBUG_ENABLED
1135 //allow better error message in cases where src and dst are the same stack position
1136 Variant ret = src->get_named(*index, valid);
1137
1138#else
1139 *dst = src->get_named(*index, valid);
1140#endif
1141#ifdef DEBUG_ENABLED
1142 if (!valid) {
1143 err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
1144 OPCODE_BREAK;
1145 }
1146 *dst = ret;
1147#endif
1148 ip += 4;
1149 }
1150 DISPATCH_OPCODE;
1151
1152 OPCODE(OPCODE_GET_NAMED_VALIDATED) {
1153 CHECK_SPACE(3);
1154
1155 GET_VARIANT_PTR(src, 0);
1156 GET_VARIANT_PTR(dst, 1);
1157
1158 int index_getter = _code_ptr[ip + 3];
1159 GD_ERR_BREAK(index_getter < 0 || index_getter >= _getters_count);
1160 const Variant::ValidatedGetter getter = _getters_ptr[index_getter];
1161
1162 getter(src, dst);
1163 ip += 4;
1164 }
1165 DISPATCH_OPCODE;
1166
1167 OPCODE(OPCODE_SET_MEMBER) {
1168 CHECK_SPACE(3);
1169 GET_VARIANT_PTR(src, 0);
1170 int indexname = _code_ptr[ip + 2];
1171 GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
1172 const StringName *index = &_global_names_ptr[indexname];
1173
1174 bool valid;
1175#ifndef DEBUG_ENABLED
1176 ClassDB::set_property(p_instance->owner, *index, *src, &valid);
1177#else
1178 bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
1179 if (!ok) {
1180 err_text = "Internal error setting property: " + String(*index);
1181 OPCODE_BREAK;
1182 } else if (!valid) {
1183 err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
1184 OPCODE_BREAK;
1185 }
1186#endif
1187 ip += 3;
1188 }
1189 DISPATCH_OPCODE;
1190
1191 OPCODE(OPCODE_GET_MEMBER) {
1192 CHECK_SPACE(3);
1193 GET_VARIANT_PTR(dst, 0);
1194 int indexname = _code_ptr[ip + 2];
1195 GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
1196 const StringName *index = &_global_names_ptr[indexname];
1197#ifndef DEBUG_ENABLED
1198 ClassDB::get_property(p_instance->owner, *index, *dst);
1199#else
1200 bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
1201 if (!ok) {
1202 err_text = "Internal error getting property: " + String(*index);
1203 OPCODE_BREAK;
1204 }
1205#endif
1206 ip += 3;
1207 }
1208 DISPATCH_OPCODE;
1209
1210 OPCODE(OPCODE_SET_STATIC_VARIABLE) {
1211 CHECK_SPACE(4);
1212
1213 GET_VARIANT_PTR(value, 0);
1214
1215 GET_VARIANT_PTR(_class, 1);
1216 GDScript *gdscript = Object::cast_to<GDScript>(_class->operator Object *());
1217 GD_ERR_BREAK(!gdscript);
1218
1219 int index = _code_ptr[ip + 3];
1220 GD_ERR_BREAK(index < 0 || index >= gdscript->static_variables.size());
1221
1222 gdscript->static_variables.write[index] = *value;
1223
1224 ip += 4;
1225 }
1226 DISPATCH_OPCODE;
1227
1228 OPCODE(OPCODE_GET_STATIC_VARIABLE) {
1229 CHECK_SPACE(4);
1230
1231 GET_VARIANT_PTR(target, 0);
1232
1233 GET_VARIANT_PTR(_class, 1);
1234 GDScript *gdscript = Object::cast_to<GDScript>(_class->operator Object *());
1235 GD_ERR_BREAK(!gdscript);
1236
1237 int index = _code_ptr[ip + 3];
1238 GD_ERR_BREAK(index < 0 || index >= gdscript->static_variables.size());
1239
1240 *target = gdscript->static_variables[index];
1241
1242 ip += 4;
1243 }
1244 DISPATCH_OPCODE;
1245
1246 OPCODE(OPCODE_ASSIGN) {
1247 CHECK_SPACE(3);
1248 GET_VARIANT_PTR(dst, 0);
1249 GET_VARIANT_PTR(src, 1);
1250
1251 *dst = *src;
1252
1253 ip += 3;
1254 }
1255 DISPATCH_OPCODE;
1256
1257 OPCODE(OPCODE_ASSIGN_TRUE) {
1258 CHECK_SPACE(2);
1259 GET_VARIANT_PTR(dst, 0);
1260
1261 *dst = true;
1262
1263 ip += 2;
1264 }
1265 DISPATCH_OPCODE;
1266
1267 OPCODE(OPCODE_ASSIGN_FALSE) {
1268 CHECK_SPACE(2);
1269 GET_VARIANT_PTR(dst, 0);
1270
1271 *dst = false;
1272
1273 ip += 2;
1274 }
1275 DISPATCH_OPCODE;
1276
1277 OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
1278 CHECK_SPACE(4);
1279 GET_VARIANT_PTR(dst, 0);
1280 GET_VARIANT_PTR(src, 1);
1281
1282 Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
1283 GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
1284
1285 if (src->get_type() != var_type) {
1286#ifdef DEBUG_ENABLED
1287 if (Variant::can_convert_strict(src->get_type(), var_type)) {
1288#endif // DEBUG_ENABLED
1289 Callable::CallError ce;
1290 Variant::construct(var_type, *dst, const_cast<const Variant **>(&src), 1, ce);
1291 } else {
1292#ifdef DEBUG_ENABLED
1293 err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
1294 "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
1295 OPCODE_BREAK;
1296 }
1297 } else {
1298#endif // DEBUG_ENABLED
1299 *dst = *src;
1300 }
1301
1302 ip += 4;
1303 }
1304 DISPATCH_OPCODE;
1305
1306 OPCODE(OPCODE_ASSIGN_TYPED_ARRAY) {
1307 CHECK_SPACE(6);
1308 GET_VARIANT_PTR(dst, 0);
1309 GET_VARIANT_PTR(src, 1);
1310
1311 GET_VARIANT_PTR(script_type, 2);
1312 Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 4];
1313 int native_type_idx = _code_ptr[ip + 5];
1314 GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
1315 const StringName native_type = _global_names_ptr[native_type_idx];
1316
1317 if (src->get_type() != Variant::ARRAY) {
1318#ifdef DEBUG_ENABLED
1319 err_text = vformat(R"(Trying to assign a value of type "%s" to a variable of type "Array[%s]".)",
1320 _get_var_type(src), _get_element_type(builtin_type, native_type, *script_type));
1321#endif // DEBUG_ENABLED
1322 OPCODE_BREAK;
1323 }
1324
1325 Array *array = VariantInternal::get_array(src);
1326
1327 if (array->get_typed_builtin() != ((uint32_t)builtin_type) || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) {
1328#ifdef DEBUG_ENABLED
1329 err_text = vformat(R"(Trying to assign an array of type "%s" to a variable of type "Array[%s]".)",
1330 _get_var_type(src), _get_element_type(builtin_type, native_type, *script_type));
1331#endif // DEBUG_ENABLED
1332 OPCODE_BREAK;
1333 }
1334
1335 *dst = *src;
1336
1337 ip += 6;
1338 }
1339 DISPATCH_OPCODE;
1340
1341 OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
1342 CHECK_SPACE(4);
1343 GET_VARIANT_PTR(dst, 0);
1344 GET_VARIANT_PTR(src, 1);
1345
1346#ifdef DEBUG_ENABLED
1347 GET_VARIANT_PTR(type, 2);
1348 GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
1349 GD_ERR_BREAK(!nc);
1350 if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
1351 err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
1352 "' to a variable of type '" + nc->get_name() + "'.";
1353 OPCODE_BREAK;
1354 }
1355
1356 if (src->get_type() == Variant::OBJECT) {
1357 bool was_freed = false;
1358 Object *src_obj = src->get_validated_object_with_check(was_freed);
1359 if (!src_obj && was_freed) {
1360 err_text = "Trying to assign invalid previously freed instance.";
1361 OPCODE_BREAK;
1362 }
1363
1364 if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
1365 err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
1366 "' to a variable of type '" + nc->get_name() + "'.";
1367 OPCODE_BREAK;
1368 }
1369 }
1370#endif // DEBUG_ENABLED
1371 *dst = *src;
1372
1373 ip += 4;
1374 }
1375 DISPATCH_OPCODE;
1376
1377 OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
1378 CHECK_SPACE(4);
1379 GET_VARIANT_PTR(dst, 0);
1380 GET_VARIANT_PTR(src, 1);
1381
1382#ifdef DEBUG_ENABLED
1383 GET_VARIANT_PTR(type, 2);
1384 Script *base_type = Object::cast_to<Script>(type->operator Object *());
1385
1386 GD_ERR_BREAK(!base_type);
1387
1388 if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
1389 err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
1390 OPCODE_BREAK;
1391 }
1392
1393 if (src->get_type() == Variant::OBJECT) {
1394 bool was_freed = false;
1395 Object *val_obj = src->get_validated_object_with_check(was_freed);
1396 if (!val_obj && was_freed) {
1397 err_text = "Trying to assign invalid previously freed instance.";
1398 OPCODE_BREAK;
1399 }
1400
1401 if (val_obj) { // src is not null
1402 ScriptInstance *scr_inst = val_obj->get_script_instance();
1403 if (!scr_inst) {
1404 err_text = "Trying to assign value of type '" + val_obj->get_class_name() +
1405 "' to a variable of type '" + base_type->get_path().get_file() + "'.";
1406 OPCODE_BREAK;
1407 }
1408
1409 Script *src_type = scr_inst->get_script().ptr();
1410 bool valid = false;
1411
1412 while (src_type) {
1413 if (src_type == base_type) {
1414 valid = true;
1415 break;
1416 }
1417 src_type = src_type->get_base_script().ptr();
1418 }
1419
1420 if (!valid) {
1421 err_text = "Trying to assign value of type '" + val_obj->get_script_instance()->get_script()->get_path().get_file() +
1422 "' to a variable of type '" + base_type->get_path().get_file() + "'.";
1423 OPCODE_BREAK;
1424 }
1425 }
1426 }
1427#endif // DEBUG_ENABLED
1428
1429 *dst = *src;
1430
1431 ip += 4;
1432 }
1433 DISPATCH_OPCODE;
1434
1435 OPCODE(OPCODE_CAST_TO_BUILTIN) {
1436 CHECK_SPACE(4);
1437 GET_VARIANT_PTR(src, 0);
1438 GET_VARIANT_PTR(dst, 1);
1439 Variant::Type to_type = (Variant::Type)_code_ptr[ip + 3];
1440
1441 GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
1442
1443#ifdef DEBUG_ENABLED
1444 if (src->operator Object *() && !src->get_validated_object()) {
1445 err_text = "Trying to cast a freed object.";
1446 OPCODE_BREAK;
1447 }
1448#endif
1449
1450 Callable::CallError err;
1451 Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);
1452
1453#ifdef DEBUG_ENABLED
1454 if (err.error != Callable::CallError::CALL_OK) {
1455 err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
1456 OPCODE_BREAK;
1457 }
1458#endif
1459
1460 ip += 4;
1461 }
1462 DISPATCH_OPCODE;
1463
1464 OPCODE(OPCODE_CAST_TO_NATIVE) {
1465 CHECK_SPACE(4);
1466 GET_VARIANT_PTR(src, 0);
1467 GET_VARIANT_PTR(dst, 1);
1468 GET_VARIANT_PTR(to_type, 2);
1469
1470 GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
1471 GD_ERR_BREAK(!nc);
1472
1473#ifdef DEBUG_ENABLED
1474 if (src->operator Object *() && !src->get_validated_object()) {
1475 err_text = "Trying to cast a freed object.";
1476 OPCODE_BREAK;
1477 }
1478 if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
1479 err_text = "Invalid cast: can't convert a non-object value to an object type.";
1480 OPCODE_BREAK;
1481 }
1482#endif
1483 Object *src_obj = src->operator Object *();
1484
1485 if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
1486 *dst = Variant(); // invalid cast, assign NULL
1487 } else {
1488 *dst = *src;
1489 }
1490
1491 ip += 4;
1492 }
1493 DISPATCH_OPCODE;
1494
1495 OPCODE(OPCODE_CAST_TO_SCRIPT) {
1496 CHECK_SPACE(4);
1497 GET_VARIANT_PTR(src, 0);
1498 GET_VARIANT_PTR(dst, 1);
1499 GET_VARIANT_PTR(to_type, 2);
1500
1501 Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
1502
1503 GD_ERR_BREAK(!base_type);
1504
1505#ifdef DEBUG_ENABLED
1506 if (src->operator Object *() && !src->get_validated_object()) {
1507 err_text = "Trying to cast a freed object.";
1508 OPCODE_BREAK;
1509 }
1510 if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
1511 err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
1512 OPCODE_BREAK;
1513 }
1514#endif
1515
1516 bool valid = false;
1517
1518 if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
1519 ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
1520
1521 if (scr_inst) {
1522 Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
1523
1524 while (src_type) {
1525 if (src_type == base_type) {
1526 valid = true;
1527 break;
1528 }
1529 src_type = src_type->get_base_script().ptr();
1530 }
1531 }
1532 }
1533
1534 if (valid) {
1535 *dst = *src; // Valid cast, copy the source object
1536 } else {
1537 *dst = Variant(); // invalid cast, assign NULL
1538 }
1539
1540 ip += 4;
1541 }
1542 DISPATCH_OPCODE;
1543
1544 OPCODE(OPCODE_CONSTRUCT) {
1545 LOAD_INSTRUCTION_ARGS
1546 CHECK_SPACE(2 + instr_arg_count);
1547
1548 ip += instr_arg_count;
1549
1550 int argc = _code_ptr[ip + 1];
1551
1552 Variant::Type t = Variant::Type(_code_ptr[ip + 2]);
1553
1554 Variant **argptrs = instruction_args;
1555
1556 GET_INSTRUCTION_ARG(dst, argc);
1557
1558 Callable::CallError err;
1559 Variant::construct(t, *dst, (const Variant **)argptrs, argc, err);
1560
1561#ifdef DEBUG_ENABLED
1562 if (err.error != Callable::CallError::CALL_OK) {
1563 err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
1564 OPCODE_BREAK;
1565 }
1566#endif
1567
1568 ip += 3;
1569 }
1570 DISPATCH_OPCODE;
1571
1572 OPCODE(OPCODE_CONSTRUCT_VALIDATED) {
1573 LOAD_INSTRUCTION_ARGS
1574 CHECK_SPACE(2 + instr_arg_count);
1575 ip += instr_arg_count;
1576
1577 int argc = _code_ptr[ip + 1];
1578
1579 int constructor_idx = _code_ptr[ip + 2];
1580 GD_ERR_BREAK(constructor_idx < 0 || constructor_idx >= _constructors_count);
1581 Variant::ValidatedConstructor constructor = _constructors_ptr[constructor_idx];
1582
1583 Variant **argptrs = instruction_args;
1584
1585 GET_INSTRUCTION_ARG(dst, argc);
1586
1587 constructor(dst, (const Variant **)argptrs);
1588
1589 ip += 3;
1590 }
1591 DISPATCH_OPCODE;
1592
1593 OPCODE(OPCODE_CONSTRUCT_ARRAY) {
1594 LOAD_INSTRUCTION_ARGS
1595 CHECK_SPACE(1 + instr_arg_count);
1596 ip += instr_arg_count;
1597
1598 int argc = _code_ptr[ip + 1];
1599 Array array;
1600 array.resize(argc);
1601
1602 for (int i = 0; i < argc; i++) {
1603 array[i] = *(instruction_args[i]);
1604 }
1605
1606 GET_INSTRUCTION_ARG(dst, argc);
1607 *dst = Variant(); // Clear potential previous typed array.
1608
1609 *dst = array;
1610
1611 ip += 2;
1612 }
1613 DISPATCH_OPCODE;
1614
1615 OPCODE(OPCODE_CONSTRUCT_TYPED_ARRAY) {
1616 LOAD_INSTRUCTION_ARGS
1617 CHECK_SPACE(3 + instr_arg_count);
1618 ip += instr_arg_count;
1619
1620 int argc = _code_ptr[ip + 1];
1621
1622 GET_INSTRUCTION_ARG(script_type, argc + 1);
1623 Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 2];
1624 int native_type_idx = _code_ptr[ip + 3];
1625 GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
1626 const StringName native_type = _global_names_ptr[native_type_idx];
1627
1628 Array array;
1629 array.resize(argc);
1630 for (int i = 0; i < argc; i++) {
1631 array[i] = *(instruction_args[i]);
1632 }
1633
1634 GET_INSTRUCTION_ARG(dst, argc);
1635 *dst = Variant(); // Clear potential previous typed array.
1636
1637 *dst = Array(array, builtin_type, native_type, *script_type);
1638
1639 ip += 4;
1640 }
1641 DISPATCH_OPCODE;
1642
1643 OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
1644 LOAD_INSTRUCTION_ARGS
1645 CHECK_SPACE(2 + instr_arg_count);
1646
1647 ip += instr_arg_count;
1648
1649 int argc = _code_ptr[ip + 1];
1650 Dictionary dict;
1651
1652 for (int i = 0; i < argc; i++) {
1653 GET_INSTRUCTION_ARG(k, i * 2 + 0);
1654 GET_INSTRUCTION_ARG(v, i * 2 + 1);
1655 dict[*k] = *v;
1656 }
1657
1658 GET_INSTRUCTION_ARG(dst, argc * 2);
1659
1660 *dst = dict;
1661
1662 ip += 2;
1663 }
1664 DISPATCH_OPCODE;
1665
1666 OPCODE(OPCODE_CALL_ASYNC)
1667 OPCODE(OPCODE_CALL_RETURN)
1668 OPCODE(OPCODE_CALL) {
1669 bool call_ret = (_code_ptr[ip]) != OPCODE_CALL;
1670#ifdef DEBUG_ENABLED
1671 bool call_async = (_code_ptr[ip]) == OPCODE_CALL_ASYNC;
1672#endif
1673 LOAD_INSTRUCTION_ARGS
1674 CHECK_SPACE(3 + instr_arg_count);
1675
1676 ip += instr_arg_count;
1677
1678 int argc = _code_ptr[ip + 1];
1679 GD_ERR_BREAK(argc < 0);
1680
1681 int methodname_idx = _code_ptr[ip + 2];
1682 GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
1683 const StringName *methodname = &_global_names_ptr[methodname_idx];
1684
1685 GET_INSTRUCTION_ARG(base, argc);
1686 Variant **argptrs = instruction_args;
1687
1688#ifdef DEBUG_ENABLED
1689 uint64_t call_time = 0;
1690
1691 if (GDScriptLanguage::get_singleton()->profiling) {
1692 call_time = OS::get_singleton()->get_ticks_usec();
1693 }
1694
1695#endif
1696 Callable::CallError err;
1697 if (call_ret) {
1698 GET_INSTRUCTION_ARG(ret, argc + 1);
1699#ifdef DEBUG_ENABLED
1700 Variant::Type base_type = base->get_type();
1701 Object *base_obj = base->get_validated_object();
1702 StringName base_class = base_obj ? base_obj->get_class_name() : StringName();
1703#endif
1704 base->callp(*methodname, (const Variant **)argptrs, argc, *ret, err);
1705#ifdef DEBUG_ENABLED
1706 if (ret->get_type() == Variant::NIL) {
1707 if (base_type == Variant::OBJECT) {
1708 if (base_obj) {
1709 MethodBind *method = ClassDB::get_method(base_class, *methodname);
1710 if (*methodname == CoreStringNames::get_singleton()->_free || (method && !method->has_return())) {
1711 err_text = R"(Trying to get a return value of a method that returns "void")";
1712 OPCODE_BREAK;
1713 }
1714 }
1715 } else if (Variant::has_builtin_method(base_type, *methodname) && !Variant::has_builtin_method_return_value(base_type, *methodname)) {
1716 err_text = R"(Trying to get a return value of a method that returns "void")";
1717 OPCODE_BREAK;
1718 }
1719 }
1720
1721 if (!call_async && ret->get_type() == Variant::OBJECT) {
1722 // Check if getting a function state without await.
1723 bool was_freed = false;
1724 Object *obj = ret->get_validated_object_with_check(was_freed);
1725
1726 if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
1727 err_text = R"(Trying to call an async function without "await".)";
1728 OPCODE_BREAK;
1729 }
1730 }
1731#endif
1732 } else {
1733 Variant ret;
1734 base->callp(*methodname, (const Variant **)argptrs, argc, ret, err);
1735 }
1736#ifdef DEBUG_ENABLED
1737 if (GDScriptLanguage::get_singleton()->profiling) {
1738 function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
1739 }
1740
1741 if (err.error != Callable::CallError::CALL_OK) {
1742 String methodstr = *methodname;
1743 String basestr = _get_var_type(base);
1744 bool is_callable = false;
1745
1746 if (methodstr == "call") {
1747 if (argc >= 1 && base->get_type() != Variant::CALLABLE) {
1748 methodstr = String(*argptrs[0]) + " (via call)";
1749 if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
1750 err.argument += 1;
1751 }
1752 } else {
1753 methodstr = base->operator String() + " (Callable)";
1754 is_callable = true;
1755 }
1756 } else if (methodstr == "free") {
1757 if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
1758 if (base->is_ref_counted()) {
1759 err_text = "Attempted to free a reference.";
1760 OPCODE_BREAK;
1761 } else if (base->get_type() == Variant::OBJECT) {
1762 err_text = "Attempted to free a locked object (calling or emitting).";
1763 OPCODE_BREAK;
1764 }
1765 }
1766 } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
1767 if (argc >= 1) {
1768 methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
1769 if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
1770 err.argument += 1;
1771 }
1772 }
1773 }
1774 err_text = _get_call_error(err, "function '" + methodstr + (is_callable ? "" : "' in base '" + basestr) + "'", (const Variant **)argptrs);
1775 OPCODE_BREAK;
1776 }
1777#endif
1778
1779 ip += 3;
1780 }
1781 DISPATCH_OPCODE;
1782
1783 OPCODE(OPCODE_CALL_METHOD_BIND)
1784 OPCODE(OPCODE_CALL_METHOD_BIND_RET) {
1785 bool call_ret = (_code_ptr[ip]) == OPCODE_CALL_METHOD_BIND_RET;
1786 LOAD_INSTRUCTION_ARGS
1787 CHECK_SPACE(3 + instr_arg_count);
1788
1789 ip += instr_arg_count;
1790
1791 int argc = _code_ptr[ip + 1];
1792 GD_ERR_BREAK(argc < 0);
1793 GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
1794 MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
1795
1796 GET_INSTRUCTION_ARG(base, argc);
1797
1798#ifdef DEBUG_ENABLED
1799 bool freed = false;
1800 Object *base_obj = base->get_validated_object_with_check(freed);
1801 if (freed) {
1802 err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method);
1803 OPCODE_BREAK;
1804 } else if (!base_obj) {
1805 err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method);
1806 OPCODE_BREAK;
1807 }
1808#else
1809 Object *base_obj = base->operator Object *();
1810#endif
1811 Variant **argptrs = instruction_args;
1812
1813#ifdef DEBUG_ENABLED
1814 uint64_t call_time = 0;
1815
1816 if (GDScriptLanguage::get_singleton()->profiling) {
1817 call_time = OS::get_singleton()->get_ticks_usec();
1818 }
1819#endif
1820
1821 Callable::CallError err;
1822 if (call_ret) {
1823 GET_INSTRUCTION_ARG(ret, argc + 1);
1824 *ret = method->call(base_obj, (const Variant **)argptrs, argc, err);
1825 } else {
1826 method->call(base_obj, (const Variant **)argptrs, argc, err);
1827 }
1828
1829#ifdef DEBUG_ENABLED
1830 if (GDScriptLanguage::get_singleton()->profiling) {
1831 function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
1832 }
1833
1834 if (err.error != Callable::CallError::CALL_OK) {
1835 String methodstr = method->get_name();
1836 String basestr = _get_var_type(base);
1837
1838 if (methodstr == "call") {
1839 if (argc >= 1) {
1840 methodstr = String(*argptrs[0]) + " (via call)";
1841 if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
1842 err.argument += 1;
1843 }
1844 }
1845 } else if (methodstr == "free") {
1846 if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
1847 if (base->is_ref_counted()) {
1848 err_text = "Attempted to free a reference.";
1849 OPCODE_BREAK;
1850 } else if (base->get_type() == Variant::OBJECT) {
1851 err_text = "Attempted to free a locked object (calling or emitting).";
1852 OPCODE_BREAK;
1853 }
1854 }
1855 }
1856 err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
1857 OPCODE_BREAK;
1858 }
1859#endif
1860 ip += 3;
1861 }
1862 DISPATCH_OPCODE;
1863
1864 OPCODE(OPCODE_CALL_BUILTIN_STATIC) {
1865 LOAD_INSTRUCTION_ARGS
1866 CHECK_SPACE(4 + instr_arg_count);
1867
1868 ip += instr_arg_count;
1869
1870 GD_ERR_BREAK(_code_ptr[ip + 1] < 0 || _code_ptr[ip + 1] >= Variant::VARIANT_MAX);
1871 Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 1];
1872
1873 int methodname_idx = _code_ptr[ip + 2];
1874 GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
1875 const StringName *methodname = &_global_names_ptr[methodname_idx];
1876
1877 int argc = _code_ptr[ip + 3];
1878 GD_ERR_BREAK(argc < 0);
1879
1880 GET_INSTRUCTION_ARG(ret, argc);
1881
1882 const Variant **argptrs = const_cast<const Variant **>(instruction_args);
1883
1884#ifdef DEBUG_ENABLED
1885 uint64_t call_time = 0;
1886
1887 if (GDScriptLanguage::get_singleton()->profiling) {
1888 call_time = OS::get_singleton()->get_ticks_usec();
1889 }
1890#endif
1891
1892 Callable::CallError err;
1893 Variant::call_static(builtin_type, *methodname, argptrs, argc, *ret, err);
1894
1895#ifdef DEBUG_ENABLED
1896 if (GDScriptLanguage::get_singleton()->profiling) {
1897 function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
1898 }
1899
1900 if (err.error != Callable::CallError::CALL_OK) {
1901 err_text = _get_call_error(err, "static function '" + methodname->operator String() + "' in type '" + Variant::get_type_name(builtin_type) + "'", argptrs);
1902 OPCODE_BREAK;
1903 }
1904#endif
1905
1906 ip += 4;
1907 }
1908 DISPATCH_OPCODE;
1909
1910 OPCODE(OPCODE_CALL_NATIVE_STATIC) {
1911 LOAD_INSTRUCTION_ARGS
1912 CHECK_SPACE(3 + instr_arg_count);
1913
1914 ip += instr_arg_count;
1915
1916 GD_ERR_BREAK(_code_ptr[ip + 1] < 0 || _code_ptr[ip + 1] >= _methods_count);
1917 MethodBind *method = _methods_ptr[_code_ptr[ip + 1]];
1918
1919 int argc = _code_ptr[ip + 2];
1920 GD_ERR_BREAK(argc < 0);
1921
1922 GET_INSTRUCTION_ARG(ret, argc);
1923
1924 const Variant **argptrs = const_cast<const Variant **>(instruction_args);
1925
1926#ifdef DEBUG_ENABLED
1927 uint64_t call_time = 0;
1928
1929 if (GDScriptLanguage::get_singleton()->profiling) {
1930 call_time = OS::get_singleton()->get_ticks_usec();
1931 }
1932#endif
1933
1934 Callable::CallError err;
1935 *ret = method->call(nullptr, argptrs, argc, err);
1936
1937#ifdef DEBUG_ENABLED
1938 if (GDScriptLanguage::get_singleton()->profiling) {
1939 function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
1940 }
1941
1942 if (err.error != Callable::CallError::CALL_OK) {
1943 err_text = _get_call_error(err, "static function '" + method->get_name().operator String() + "' in type '" + method->get_instance_class().operator String() + "'", argptrs);
1944 OPCODE_BREAK;
1945 }
1946#endif
1947
1948 ip += 3;
1949 }
1950 DISPATCH_OPCODE;
1951
1952#ifdef DEBUG_ENABLED
1953#define OPCODE_CALL_PTR(m_type) \
1954 OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
1955 LOAD_INSTRUCTION_ARGS \
1956 CHECK_SPACE(3 + instr_arg_count); \
1957 ip += instr_arg_count; \
1958 int argc = _code_ptr[ip + 1]; \
1959 GD_ERR_BREAK(argc < 0); \
1960 GET_INSTRUCTION_ARG(base, argc); \
1961 GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count); \
1962 MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
1963 bool freed = false; \
1964 Object *base_obj = base->get_validated_object_with_check(freed); \
1965 if (freed) { \
1966 err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method); \
1967 OPCODE_BREAK; \
1968 } else if (!base_obj) { \
1969 err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method); \
1970 OPCODE_BREAK; \
1971 } \
1972 const void **argptrs = call_args_ptr; \
1973 for (int i = 0; i < argc; i++) { \
1974 GET_INSTRUCTION_ARG(v, i); \
1975 argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
1976 } \
1977 uint64_t call_time = 0; \
1978 if (GDScriptLanguage::get_singleton()->profiling) { \
1979 call_time = OS::get_singleton()->get_ticks_usec(); \
1980 } \
1981 GET_INSTRUCTION_ARG(ret, argc + 1); \
1982 VariantInternal::initialize(ret, Variant::m_type); \
1983 void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
1984 method->ptrcall(base_obj, argptrs, ret_opaque); \
1985 if (GDScriptLanguage::get_singleton()->profiling) { \
1986 function_call_time += OS::get_singleton()->get_ticks_usec() - call_time; \
1987 } \
1988 ip += 3; \
1989 } \
1990 DISPATCH_OPCODE
1991#else
1992#define OPCODE_CALL_PTR(m_type) \
1993 OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
1994 LOAD_INSTRUCTION_ARGS \
1995 CHECK_SPACE(3 + instr_arg_count); \
1996 ip += instr_arg_count; \
1997 int argc = _code_ptr[ip + 1]; \
1998 GET_INSTRUCTION_ARG(base, argc); \
1999 MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
2000 Object *base_obj = *VariantInternal::get_object(base); \
2001 const void **argptrs = call_args_ptr; \
2002 for (int i = 0; i < argc; i++) { \
2003 GET_INSTRUCTION_ARG(v, i); \
2004 argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
2005 } \
2006 GET_INSTRUCTION_ARG(ret, argc + 1); \
2007 VariantInternal::initialize(ret, Variant::m_type); \
2008 void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
2009 method->ptrcall(base_obj, argptrs, ret_opaque); \
2010 ip += 3; \
2011 } \
2012 DISPATCH_OPCODE
2013#endif
2014
2015 OPCODE_CALL_PTR(BOOL);
2016 OPCODE_CALL_PTR(INT);
2017 OPCODE_CALL_PTR(FLOAT);
2018 OPCODE_CALL_PTR(STRING);
2019 OPCODE_CALL_PTR(VECTOR2);
2020 OPCODE_CALL_PTR(VECTOR2I);
2021 OPCODE_CALL_PTR(RECT2);
2022 OPCODE_CALL_PTR(RECT2I);
2023 OPCODE_CALL_PTR(VECTOR3);
2024 OPCODE_CALL_PTR(VECTOR3I);
2025 OPCODE_CALL_PTR(TRANSFORM2D);
2026 OPCODE_CALL_PTR(VECTOR4);
2027 OPCODE_CALL_PTR(VECTOR4I);
2028 OPCODE_CALL_PTR(PLANE);
2029 OPCODE_CALL_PTR(QUATERNION);
2030 OPCODE_CALL_PTR(AABB);
2031 OPCODE_CALL_PTR(BASIS);
2032 OPCODE_CALL_PTR(TRANSFORM3D);
2033 OPCODE_CALL_PTR(PROJECTION);
2034 OPCODE_CALL_PTR(COLOR);
2035 OPCODE_CALL_PTR(STRING_NAME);
2036 OPCODE_CALL_PTR(NODE_PATH);
2037 OPCODE_CALL_PTR(RID);
2038 OPCODE_CALL_PTR(CALLABLE);
2039 OPCODE_CALL_PTR(SIGNAL);
2040 OPCODE_CALL_PTR(DICTIONARY);
2041 OPCODE_CALL_PTR(ARRAY);
2042 OPCODE_CALL_PTR(PACKED_BYTE_ARRAY);
2043 OPCODE_CALL_PTR(PACKED_INT32_ARRAY);
2044 OPCODE_CALL_PTR(PACKED_INT64_ARRAY);
2045 OPCODE_CALL_PTR(PACKED_FLOAT32_ARRAY);
2046 OPCODE_CALL_PTR(PACKED_FLOAT64_ARRAY);
2047 OPCODE_CALL_PTR(PACKED_STRING_ARRAY);
2048 OPCODE_CALL_PTR(PACKED_VECTOR2_ARRAY);
2049 OPCODE_CALL_PTR(PACKED_VECTOR3_ARRAY);
2050 OPCODE_CALL_PTR(PACKED_COLOR_ARRAY);
2051 OPCODE(OPCODE_CALL_PTRCALL_OBJECT) {
2052 LOAD_INSTRUCTION_ARGS
2053 CHECK_SPACE(3 + instr_arg_count);
2054
2055 ip += instr_arg_count;
2056
2057 int argc = _code_ptr[ip + 1];
2058 GD_ERR_BREAK(argc < 0);
2059
2060 GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
2061 MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
2062
2063 GET_INSTRUCTION_ARG(base, argc);
2064#ifdef DEBUG_ENABLED
2065 bool freed = false;
2066 Object *base_obj = base->get_validated_object_with_check(freed);
2067 if (freed) {
2068 err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method);
2069 OPCODE_BREAK;
2070 } else if (!base_obj) {
2071 err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method);
2072 OPCODE_BREAK;
2073 }
2074#else
2075 Object *base_obj = *VariantInternal::get_object(base);
2076#endif
2077
2078 const void **argptrs = call_args_ptr;
2079
2080 for (int i = 0; i < argc; i++) {
2081 GET_INSTRUCTION_ARG(v, i);
2082 argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
2083 }
2084#ifdef DEBUG_ENABLED
2085 uint64_t call_time = 0;
2086
2087 if (GDScriptLanguage::get_singleton()->profiling) {
2088 call_time = OS::get_singleton()->get_ticks_usec();
2089 }
2090#endif
2091
2092 GET_INSTRUCTION_ARG(ret, argc + 1);
2093 VariantInternal::initialize(ret, Variant::OBJECT);
2094 Object **ret_opaque = VariantInternal::get_object(ret);
2095 method->ptrcall(base_obj, argptrs, ret_opaque);
2096 if (method->is_return_type_raw_object_ptr()) {
2097 // The Variant has to participate in the ref count since the method returns a raw Object *.
2098 VariantInternal::object_assign(ret, *ret_opaque);
2099 } else {
2100 // The method, in case it returns something, returns an already encapsulated object.
2101 VariantInternal::update_object_id(ret);
2102 }
2103
2104#ifdef DEBUG_ENABLED
2105 if (GDScriptLanguage::get_singleton()->profiling) {
2106 function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
2107 }
2108#endif
2109 ip += 3;
2110 }
2111 DISPATCH_OPCODE;
2112 OPCODE(OPCODE_CALL_PTRCALL_NO_RETURN) {
2113 LOAD_INSTRUCTION_ARGS
2114 CHECK_SPACE(3 + instr_arg_count);
2115
2116 ip += instr_arg_count;
2117
2118 int argc = _code_ptr[ip + 1];
2119 GD_ERR_BREAK(argc < 0);
2120
2121 GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
2122 MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
2123
2124 GET_INSTRUCTION_ARG(base, argc);
2125#ifdef DEBUG_ENABLED
2126 bool freed = false;
2127 Object *base_obj = base->get_validated_object_with_check(freed);
2128 if (freed) {
2129 err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method);
2130 OPCODE_BREAK;
2131 } else if (!base_obj) {
2132 err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method);
2133 OPCODE_BREAK;
2134 }
2135#else
2136 Object *base_obj = *VariantInternal::get_object(base);
2137#endif
2138 const void **argptrs = call_args_ptr;
2139
2140 for (int i = 0; i < argc; i++) {
2141 GET_INSTRUCTION_ARG(v, i);
2142 argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
2143 }
2144#ifdef DEBUG_ENABLED
2145 uint64_t call_time = 0;
2146
2147 if (GDScriptLanguage::get_singleton()->profiling) {
2148 call_time = OS::get_singleton()->get_ticks_usec();
2149 }
2150#endif
2151
2152 GET_INSTRUCTION_ARG(ret, argc + 1);
2153 VariantInternal::initialize(ret, Variant::NIL);
2154 method->ptrcall(base_obj, argptrs, nullptr);
2155
2156#ifdef DEBUG_ENABLED
2157 if (GDScriptLanguage::get_singleton()->profiling) {
2158 function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
2159 }
2160#endif
2161 ip += 3;
2162 }
2163 DISPATCH_OPCODE;
2164
2165 OPCODE(OPCODE_CALL_BUILTIN_TYPE_VALIDATED) {
2166 LOAD_INSTRUCTION_ARGS
2167
2168 CHECK_SPACE(3 + instr_arg_count);
2169
2170 ip += instr_arg_count;
2171
2172 int argc = _code_ptr[ip + 1];
2173 GD_ERR_BREAK(argc < 0);
2174
2175 GET_INSTRUCTION_ARG(base, argc);
2176
2177 GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _builtin_methods_count);
2178 Variant::ValidatedBuiltInMethod method = _builtin_methods_ptr[_code_ptr[ip + 2]];
2179 Variant **argptrs = instruction_args;
2180
2181#ifdef DEBUG_ENABLED
2182 uint64_t call_time = 0;
2183 if (GDScriptLanguage::get_singleton()->profiling) {
2184 call_time = OS::get_singleton()->get_ticks_usec();
2185 }
2186#endif
2187
2188 GET_INSTRUCTION_ARG(ret, argc + 1);
2189 method(base, (const Variant **)argptrs, argc, ret);
2190
2191#ifdef DEBUG_ENABLED
2192 if (GDScriptLanguage::get_singleton()->profiling) {
2193 function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
2194 }
2195#endif
2196
2197 ip += 3;
2198 }
2199 DISPATCH_OPCODE;
2200
2201 OPCODE(OPCODE_CALL_UTILITY) {
2202 LOAD_INSTRUCTION_ARGS
2203 CHECK_SPACE(3 + instr_arg_count);
2204
2205 ip += instr_arg_count;
2206
2207 int argc = _code_ptr[ip + 1];
2208 GD_ERR_BREAK(argc < 0);
2209
2210 GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _global_names_count);
2211 StringName function = _global_names_ptr[_code_ptr[ip + 2]];
2212
2213 Variant **argptrs = instruction_args;
2214
2215 GET_INSTRUCTION_ARG(dst, argc);
2216
2217 Callable::CallError err;
2218 Variant::call_utility_function(function, dst, (const Variant **)argptrs, argc, err);
2219
2220#ifdef DEBUG_ENABLED
2221 if (err.error != Callable::CallError::CALL_OK) {
2222 String methodstr = function;
2223 if (dst->get_type() == Variant::STRING) {
2224 // Call provided error string.
2225 err_text = "Error calling utility function '" + methodstr + "': " + String(*dst);
2226 } else {
2227 err_text = _get_call_error(err, "utility function '" + methodstr + "'", (const Variant **)argptrs);
2228 }
2229 OPCODE_BREAK;
2230 }
2231#endif
2232 ip += 3;
2233 }
2234 DISPATCH_OPCODE;
2235
2236 OPCODE(OPCODE_CALL_UTILITY_VALIDATED) {
2237 LOAD_INSTRUCTION_ARGS
2238 CHECK_SPACE(3 + instr_arg_count);
2239
2240 ip += instr_arg_count;
2241
2242 int argc = _code_ptr[ip + 1];
2243 GD_ERR_BREAK(argc < 0);
2244
2245 GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _utilities_count);
2246 Variant::ValidatedUtilityFunction function = _utilities_ptr[_code_ptr[ip + 2]];
2247
2248 Variant **argptrs = instruction_args;
2249
2250 GET_INSTRUCTION_ARG(dst, argc);
2251
2252 function(dst, (const Variant **)argptrs, argc);
2253
2254 ip += 3;
2255 }
2256 DISPATCH_OPCODE;
2257
2258 OPCODE(OPCODE_CALL_GDSCRIPT_UTILITY) {
2259 LOAD_INSTRUCTION_ARGS
2260 CHECK_SPACE(3 + instr_arg_count);
2261
2262 ip += instr_arg_count;
2263
2264 int argc = _code_ptr[ip + 1];
2265 GD_ERR_BREAK(argc < 0);
2266
2267 GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _gds_utilities_count);
2268 GDScriptUtilityFunctions::FunctionPtr function = _gds_utilities_ptr[_code_ptr[ip + 2]];
2269
2270 Variant **argptrs = instruction_args;
2271
2272 GET_INSTRUCTION_ARG(dst, argc);
2273
2274 Callable::CallError err;
2275 function(dst, (const Variant **)argptrs, argc, err);
2276
2277#ifdef DEBUG_ENABLED
2278 if (err.error != Callable::CallError::CALL_OK) {
2279 // TODO: Add this information in debug.
2280 String methodstr = "<unknown function>";
2281 if (dst->get_type() == Variant::STRING) {
2282 // Call provided error string.
2283 err_text = "Error calling GDScript utility function '" + methodstr + "': " + String(*dst);
2284 } else {
2285 err_text = _get_call_error(err, "GDScript utility function '" + methodstr + "'", (const Variant **)argptrs);
2286 }
2287 OPCODE_BREAK;
2288 }
2289#endif
2290 ip += 3;
2291 }
2292 DISPATCH_OPCODE;
2293
2294 OPCODE(OPCODE_CALL_SELF_BASE) {
2295 LOAD_INSTRUCTION_ARGS
2296 CHECK_SPACE(3 + instr_arg_count);
2297
2298 ip += instr_arg_count;
2299
2300 int argc = _code_ptr[ip + 1];
2301 GD_ERR_BREAK(argc < 0);
2302
2303 int self_fun = _code_ptr[ip + 2];
2304#ifdef DEBUG_ENABLED
2305 if (self_fun < 0 || self_fun >= _global_names_count) {
2306 err_text = "compiler bug, function name not found";
2307 OPCODE_BREAK;
2308 }
2309#endif
2310 const StringName *methodname = &_global_names_ptr[self_fun];
2311
2312 Variant **argptrs = instruction_args;
2313
2314 GET_INSTRUCTION_ARG(dst, argc);
2315
2316 const GDScript *gds = _script;
2317
2318 HashMap<StringName, GDScriptFunction *>::ConstIterator E;
2319 while (gds->base.ptr()) {
2320 gds = gds->base.ptr();
2321 E = gds->member_functions.find(*methodname);
2322 if (E) {
2323 break;
2324 }
2325 }
2326
2327 Callable::CallError err;
2328
2329 if (E) {
2330 *dst = E->value->call(p_instance, (const Variant **)argptrs, argc, err);
2331 } else if (gds->native.ptr()) {
2332 if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
2333 MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
2334 if (!mb) {
2335 err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
2336 } else {
2337 *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
2338 }
2339 } else {
2340 err.error = Callable::CallError::CALL_OK;
2341 }
2342 } else {
2343 if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
2344 err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
2345 } else {
2346 err.error = Callable::CallError::CALL_OK;
2347 }
2348 }
2349
2350 if (err.error != Callable::CallError::CALL_OK) {
2351 String methodstr = *methodname;
2352 err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
2353
2354 OPCODE_BREAK;
2355 }
2356
2357 ip += 3;
2358 }
2359 DISPATCH_OPCODE;
2360
2361 OPCODE(OPCODE_AWAIT) {
2362 CHECK_SPACE(2);
2363
2364 // Do the one-shot connect.
2365 GET_VARIANT_PTR(argobj, 0);
2366
2367 Signal sig;
2368 bool is_signal = true;
2369
2370 {
2371 Variant result = *argobj;
2372
2373 if (argobj->get_type() == Variant::OBJECT) {
2374 bool was_freed = false;
2375 Object *obj = argobj->get_validated_object_with_check(was_freed);
2376
2377 if (was_freed) {
2378 err_text = "Trying to await on a freed object.";
2379 OPCODE_BREAK;
2380 }
2381
2382 // Is this even possible to be null at this point?
2383 if (obj) {
2384 if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
2385 result = Signal(obj, "completed");
2386 }
2387 }
2388 }
2389
2390 if (result.get_type() != Variant::SIGNAL) {
2391 // Not async, return immediately using the target from OPCODE_AWAIT_RESUME.
2392 GET_VARIANT_PTR(target, 2);
2393 *target = result;
2394 ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
2395 is_signal = false;
2396 } else {
2397 sig = result;
2398 }
2399 }
2400
2401 if (is_signal) {
2402 Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
2403 gdfs->function = this;
2404
2405 gdfs->state.stack.resize(alloca_size);
2406
2407 // First 3 stack addresses are special, so we just skip them here.
2408 for (int i = 3; i < _stack_size; i++) {
2409 memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
2410 }
2411 gdfs->state.stack_size = _stack_size;
2412 gdfs->state.alloca_size = alloca_size;
2413 gdfs->state.ip = ip + 2;
2414 gdfs->state.line = line;
2415 gdfs->state.script = _script;
2416 {
2417 MutexLock lock(GDScriptLanguage::get_singleton()->mutex);
2418 _script->pending_func_states.add(&gdfs->scripts_list);
2419 if (p_instance) {
2420 gdfs->state.instance = p_instance;
2421 p_instance->pending_func_states.add(&gdfs->instances_list);
2422 } else {
2423 gdfs->state.instance = nullptr;
2424 }
2425 }
2426#ifdef DEBUG_ENABLED
2427 gdfs->state.function_name = name;
2428 gdfs->state.script_path = _script->get_script_path();
2429#endif
2430 gdfs->state.defarg = defarg;
2431 gdfs->function = this;
2432
2433 retvalue = gdfs;
2434
2435 Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback").bind(retvalue), Object::CONNECT_ONE_SHOT);
2436 if (err != OK) {
2437 err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
2438 OPCODE_BREAK;
2439 }
2440
2441#ifdef DEBUG_ENABLED
2442 exit_ok = true;
2443 awaited = true;
2444#endif
2445 OPCODE_BREAK;
2446 }
2447 }
2448 DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
2449
2450 OPCODE(OPCODE_AWAIT_RESUME) {
2451 CHECK_SPACE(2);
2452#ifdef DEBUG_ENABLED
2453 if (!p_state) {
2454 err_text = ("Invalid Resume (bug?)");
2455 OPCODE_BREAK;
2456 }
2457#endif
2458 GET_VARIANT_PTR(result, 0);
2459 *result = p_state->result;
2460 ip += 2;
2461 }
2462 DISPATCH_OPCODE;
2463
2464 OPCODE(OPCODE_CREATE_LAMBDA) {
2465 LOAD_INSTRUCTION_ARGS
2466 CHECK_SPACE(2 + instr_arg_count);
2467
2468 ip += instr_arg_count;
2469
2470 int captures_count = _code_ptr[ip + 1];
2471 GD_ERR_BREAK(captures_count < 0);
2472
2473 int lambda_index = _code_ptr[ip + 2];
2474 GD_ERR_BREAK(lambda_index < 0 || lambda_index >= _lambdas_count);
2475 GDScriptFunction *lambda = _lambdas_ptr[lambda_index];
2476
2477 Vector<Variant> captures;
2478 captures.resize(captures_count);
2479 for (int i = 0; i < captures_count; i++) {
2480 GET_INSTRUCTION_ARG(arg, i);
2481 captures.write[i] = *arg;
2482 }
2483
2484 GDScriptLambdaCallable *callable = memnew(GDScriptLambdaCallable(Ref<GDScript>(script), lambda, captures));
2485
2486 GET_INSTRUCTION_ARG(result, captures_count);
2487 *result = Callable(callable);
2488
2489 ip += 3;
2490 }
2491 DISPATCH_OPCODE;
2492
2493 OPCODE(OPCODE_CREATE_SELF_LAMBDA) {
2494 LOAD_INSTRUCTION_ARGS
2495 CHECK_SPACE(2 + instr_arg_count);
2496
2497 GD_ERR_BREAK(p_instance == nullptr);
2498
2499 ip += instr_arg_count;
2500
2501 int captures_count = _code_ptr[ip + 1];
2502 GD_ERR_BREAK(captures_count < 0);
2503
2504 int lambda_index = _code_ptr[ip + 2];
2505 GD_ERR_BREAK(lambda_index < 0 || lambda_index >= _lambdas_count);
2506 GDScriptFunction *lambda = _lambdas_ptr[lambda_index];
2507
2508 Vector<Variant> captures;
2509 captures.resize(captures_count);
2510 for (int i = 0; i < captures_count; i++) {
2511 GET_INSTRUCTION_ARG(arg, i);
2512 captures.write[i] = *arg;
2513 }
2514
2515 GDScriptLambdaSelfCallable *callable;
2516 if (Object::cast_to<RefCounted>(p_instance->owner)) {
2517 callable = memnew(GDScriptLambdaSelfCallable(Ref<RefCounted>(Object::cast_to<RefCounted>(p_instance->owner)), lambda, captures));
2518 } else {
2519 callable = memnew(GDScriptLambdaSelfCallable(p_instance->owner, lambda, captures));
2520 }
2521
2522 GET_INSTRUCTION_ARG(result, captures_count);
2523 *result = Callable(callable);
2524
2525 ip += 3;
2526 }
2527 DISPATCH_OPCODE;
2528
2529 OPCODE(OPCODE_JUMP) {
2530 CHECK_SPACE(2);
2531 int to = _code_ptr[ip + 1];
2532
2533 GD_ERR_BREAK(to < 0 || to > _code_size);
2534 ip = to;
2535 }
2536 DISPATCH_OPCODE;
2537
2538 OPCODE(OPCODE_JUMP_IF) {
2539 CHECK_SPACE(3);
2540
2541 GET_VARIANT_PTR(test, 0);
2542
2543 bool result = test->booleanize();
2544
2545 if (result) {
2546 int to = _code_ptr[ip + 2];
2547 GD_ERR_BREAK(to < 0 || to > _code_size);
2548 ip = to;
2549 } else {
2550 ip += 3;
2551 }
2552 }
2553 DISPATCH_OPCODE;
2554
2555 OPCODE(OPCODE_JUMP_IF_NOT) {
2556 CHECK_SPACE(3);
2557
2558 GET_VARIANT_PTR(test, 0);
2559
2560 bool result = test->booleanize();
2561
2562 if (!result) {
2563 int to = _code_ptr[ip + 2];
2564 GD_ERR_BREAK(to < 0 || to > _code_size);
2565 ip = to;
2566 } else {
2567 ip += 3;
2568 }
2569 }
2570 DISPATCH_OPCODE;
2571
2572 OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
2573 CHECK_SPACE(2);
2574 ip = _default_arg_ptr[defarg];
2575 }
2576 DISPATCH_OPCODE;
2577
2578 OPCODE(OPCODE_JUMP_IF_SHARED) {
2579 CHECK_SPACE(3);
2580
2581 GET_VARIANT_PTR(val, 0);
2582
2583 if (val->is_shared()) {
2584 int to = _code_ptr[ip + 2];
2585 GD_ERR_BREAK(to < 0 || to > _code_size);
2586 ip = to;
2587 } else {
2588 ip += 3;
2589 }
2590 }
2591 DISPATCH_OPCODE;
2592
2593 OPCODE(OPCODE_RETURN) {
2594 CHECK_SPACE(2);
2595 GET_VARIANT_PTR(r, 0);
2596 retvalue = *r;
2597#ifdef DEBUG_ENABLED
2598 exit_ok = true;
2599#endif
2600 OPCODE_BREAK;
2601 }
2602
2603 OPCODE(OPCODE_RETURN_TYPED_BUILTIN) {
2604 CHECK_SPACE(3);
2605 GET_VARIANT_PTR(r, 0);
2606
2607 Variant::Type ret_type = (Variant::Type)_code_ptr[ip + 2];
2608 GD_ERR_BREAK(ret_type < 0 || ret_type >= Variant::VARIANT_MAX);
2609
2610 if (r->get_type() != ret_type) {
2611 if (Variant::can_convert_strict(r->get_type(), ret_type)) {
2612 Callable::CallError ce;
2613 Variant::construct(ret_type, retvalue, const_cast<const Variant **>(&r), 1, ce);
2614 } else {
2615#ifdef DEBUG_ENABLED
2616 err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
2617 Variant::get_type_name(r->get_type()), Variant::get_type_name(ret_type));
2618#endif // DEBUG_ENABLED
2619
2620 // Construct a base type anyway so type constraints are met.
2621 Callable::CallError ce;
2622 Variant::construct(ret_type, retvalue, nullptr, 0, ce);
2623 OPCODE_BREAK;
2624 }
2625 } else {
2626 retvalue = *r;
2627 }
2628#ifdef DEBUG_ENABLED
2629 exit_ok = true;
2630#endif // DEBUG_ENABLED
2631 OPCODE_BREAK;
2632 }
2633
2634 OPCODE(OPCODE_RETURN_TYPED_ARRAY) {
2635 CHECK_SPACE(5);
2636 GET_VARIANT_PTR(r, 0);
2637
2638 GET_VARIANT_PTR(script_type, 1);
2639 Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 3];
2640 int native_type_idx = _code_ptr[ip + 4];
2641 GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
2642 const StringName native_type = _global_names_ptr[native_type_idx];
2643
2644 if (r->get_type() != Variant::ARRAY) {
2645#ifdef DEBUG_ENABLED
2646 err_text = vformat(R"(Trying to return a value of type "%s" where expected return type is "Array[%s]".)",
2647 _get_var_type(r), _get_element_type(builtin_type, native_type, *script_type));
2648#endif // DEBUG_ENABLED
2649 OPCODE_BREAK;
2650 }
2651
2652 Array *array = VariantInternal::get_array(r);
2653
2654 if (array->get_typed_builtin() != ((uint32_t)builtin_type) || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) {
2655#ifdef DEBUG_ENABLED
2656 err_text = vformat(R"(Trying to return an array of type "%s" where expected return type is "Array[%s]".)",
2657 _get_var_type(r), _get_element_type(builtin_type, native_type, *script_type));
2658#endif // DEBUG_ENABLED
2659 OPCODE_BREAK;
2660 }
2661
2662 retvalue = *array;
2663
2664#ifdef DEBUG_ENABLED
2665 exit_ok = true;
2666#endif // DEBUG_ENABLED
2667 OPCODE_BREAK;
2668 }
2669
2670 OPCODE(OPCODE_RETURN_TYPED_NATIVE) {
2671 CHECK_SPACE(3);
2672 GET_VARIANT_PTR(r, 0);
2673
2674 GET_VARIANT_PTR(type, 1);
2675 GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
2676 GD_ERR_BREAK(!nc);
2677
2678 if (r->get_type() != Variant::OBJECT && r->get_type() != Variant::NIL) {
2679 err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
2680 Variant::get_type_name(r->get_type()), nc->get_name());
2681 OPCODE_BREAK;
2682 }
2683
2684#ifdef DEBUG_ENABLED
2685 bool freed = false;
2686 Object *ret_obj = r->get_validated_object_with_check(freed);
2687
2688 if (freed) {
2689 err_text = "Trying to return a previously freed instance.";
2690 OPCODE_BREAK;
2691 }
2692#else
2693 Object *ret_obj = r->operator Object *();
2694#endif // DEBUG_ENABLED
2695 if (ret_obj && !ClassDB::is_parent_class(ret_obj->get_class_name(), nc->get_name())) {
2696#ifdef DEBUG_ENABLED
2697 err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
2698 ret_obj->get_class_name(), nc->get_name());
2699#endif // DEBUG_ENABLED
2700 OPCODE_BREAK;
2701 }
2702 retvalue = *r;
2703
2704#ifdef DEBUG_ENABLED
2705 exit_ok = true;
2706#endif // DEBUG_ENABLED
2707 OPCODE_BREAK;
2708 }
2709
2710 OPCODE(OPCODE_RETURN_TYPED_SCRIPT) {
2711 CHECK_SPACE(3);
2712 GET_VARIANT_PTR(r, 0);
2713
2714 GET_VARIANT_PTR(type, 1);
2715 Script *base_type = Object::cast_to<Script>(type->operator Object *());
2716 GD_ERR_BREAK(!base_type);
2717
2718 if (r->get_type() != Variant::OBJECT && r->get_type() != Variant::NIL) {
2719#ifdef DEBUG_ENABLED
2720 err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
2721 Variant::get_type_name(r->get_type()), GDScript::debug_get_script_name(Ref<Script>(base_type)));
2722#endif // DEBUG_ENABLED
2723 OPCODE_BREAK;
2724 }
2725
2726#ifdef DEBUG_ENABLED
2727 bool freed = false;
2728 Object *ret_obj = r->get_validated_object_with_check(freed);
2729
2730 if (freed) {
2731 err_text = "Trying to return a previously freed instance.";
2732 OPCODE_BREAK;
2733 }
2734#else
2735 Object *ret_obj = r->operator Object *();
2736#endif // DEBUG_ENABLED
2737
2738 if (ret_obj) {
2739 ScriptInstance *ret_inst = ret_obj->get_script_instance();
2740 if (!ret_inst) {
2741#ifdef DEBUG_ENABLED
2742 err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
2743 ret_obj->get_class_name(), GDScript::debug_get_script_name(Ref<GDScript>(base_type)));
2744#endif // DEBUG_ENABLED
2745 OPCODE_BREAK;
2746 }
2747
2748 Script *ret_type = ret_obj->get_script_instance()->get_script().ptr();
2749 bool valid = false;
2750
2751 while (ret_type) {
2752 if (ret_type == base_type) {
2753 valid = true;
2754 break;
2755 }
2756 ret_type = ret_type->get_base_script().ptr();
2757 }
2758
2759 if (!valid) {
2760#ifdef DEBUG_ENABLED
2761 err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
2762 GDScript::debug_get_script_name(ret_obj->get_script_instance()->get_script()), GDScript::debug_get_script_name(Ref<GDScript>(base_type)));
2763#endif // DEBUG_ENABLED
2764 OPCODE_BREAK;
2765 }
2766 }
2767 retvalue = *r;
2768
2769#ifdef DEBUG_ENABLED
2770 exit_ok = true;
2771#endif // DEBUG_ENABLED
2772 OPCODE_BREAK;
2773 }
2774
2775 OPCODE(OPCODE_ITERATE_BEGIN) {
2776 CHECK_SPACE(8); // Space for this and a regular iterate.
2777
2778 GET_VARIANT_PTR(counter, 0);
2779 GET_VARIANT_PTR(container, 1);
2780
2781 bool valid;
2782 if (!container->iter_init(*counter, valid)) {
2783#ifdef DEBUG_ENABLED
2784 if (!valid) {
2785 err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
2786 OPCODE_BREAK;
2787 }
2788#endif
2789 int jumpto = _code_ptr[ip + 4];
2790 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
2791 ip = jumpto;
2792 } else {
2793 GET_VARIANT_PTR(iterator, 2);
2794
2795 *iterator = container->iter_get(*counter, valid);
2796#ifdef DEBUG_ENABLED
2797 if (!valid) {
2798 err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
2799 OPCODE_BREAK;
2800 }
2801#endif
2802 ip += 5; // Skip regular iterate which is always next.
2803 }
2804 }
2805 DISPATCH_OPCODE;
2806
2807 OPCODE(OPCODE_ITERATE_BEGIN_INT) {
2808 CHECK_SPACE(8); // Check space for iterate instruction too.
2809
2810 GET_VARIANT_PTR(counter, 0);
2811 GET_VARIANT_PTR(container, 1);
2812
2813 int64_t size = *VariantInternal::get_int(container);
2814
2815 VariantInternal::initialize(counter, Variant::INT);
2816 *VariantInternal::get_int(counter) = 0;
2817
2818 if (size > 0) {
2819 GET_VARIANT_PTR(iterator, 2);
2820 VariantInternal::initialize(iterator, Variant::INT);
2821 *VariantInternal::get_int(iterator) = 0;
2822
2823 // Skip regular iterate.
2824 ip += 5;
2825 } else {
2826 // Jump to end of loop.
2827 int jumpto = _code_ptr[ip + 4];
2828 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
2829 ip = jumpto;
2830 }
2831 }
2832 DISPATCH_OPCODE;
2833
2834 OPCODE(OPCODE_ITERATE_BEGIN_FLOAT) {
2835 CHECK_SPACE(8); // Check space for iterate instruction too.
2836
2837 GET_VARIANT_PTR(counter, 0);
2838 GET_VARIANT_PTR(container, 1);
2839
2840 double size = *VariantInternal::get_float(container);
2841
2842 VariantInternal::initialize(counter, Variant::FLOAT);
2843 *VariantInternal::get_float(counter) = 0.0;
2844
2845 if (size > 0) {
2846 GET_VARIANT_PTR(iterator, 2);
2847 VariantInternal::initialize(iterator, Variant::FLOAT);
2848 *VariantInternal::get_float(iterator) = 0;
2849
2850 // Skip regular iterate.
2851 ip += 5;
2852 } else {
2853 // Jump to end of loop.
2854 int jumpto = _code_ptr[ip + 4];
2855 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
2856 ip = jumpto;
2857 }
2858 }
2859 DISPATCH_OPCODE;
2860
2861 OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2) {
2862 CHECK_SPACE(8); // Check space for iterate instruction too.
2863
2864 GET_VARIANT_PTR(counter, 0);
2865 GET_VARIANT_PTR(container, 1);
2866
2867 Vector2 *bounds = VariantInternal::get_vector2(container);
2868
2869 VariantInternal::initialize(counter, Variant::FLOAT);
2870 *VariantInternal::get_float(counter) = bounds->x;
2871
2872 if (bounds->x < bounds->y) {
2873 GET_VARIANT_PTR(iterator, 2);
2874 VariantInternal::initialize(iterator, Variant::FLOAT);
2875 *VariantInternal::get_float(iterator) = bounds->x;
2876
2877 // Skip regular iterate.
2878 ip += 5;
2879 } else {
2880 // Jump to end of loop.
2881 int jumpto = _code_ptr[ip + 4];
2882 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
2883 ip = jumpto;
2884 }
2885 }
2886 DISPATCH_OPCODE;
2887
2888 OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2I) {
2889 CHECK_SPACE(8); // Check space for iterate instruction too.
2890
2891 GET_VARIANT_PTR(counter, 0);
2892 GET_VARIANT_PTR(container, 1);
2893
2894 Vector2i *bounds = VariantInternal::get_vector2i(container);
2895
2896 VariantInternal::initialize(counter, Variant::FLOAT);
2897 *VariantInternal::get_int(counter) = bounds->x;
2898
2899 if (bounds->x < bounds->y) {
2900 GET_VARIANT_PTR(iterator, 2);
2901 VariantInternal::initialize(iterator, Variant::INT);
2902 *VariantInternal::get_int(iterator) = bounds->x;
2903
2904 // Skip regular iterate.
2905 ip += 5;
2906 } else {
2907 // Jump to end of loop.
2908 int jumpto = _code_ptr[ip + 4];
2909 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
2910 ip = jumpto;
2911 }
2912 }
2913 DISPATCH_OPCODE;
2914
2915 OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3) {
2916 CHECK_SPACE(8); // Check space for iterate instruction too.
2917
2918 GET_VARIANT_PTR(counter, 0);
2919 GET_VARIANT_PTR(container, 1);
2920
2921 Vector3 *bounds = VariantInternal::get_vector3(container);
2922 double from = bounds->x;
2923 double to = bounds->y;
2924 double step = bounds->z;
2925
2926 VariantInternal::initialize(counter, Variant::FLOAT);
2927 *VariantInternal::get_float(counter) = from;
2928
2929 bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
2930
2931 if (do_continue) {
2932 GET_VARIANT_PTR(iterator, 2);
2933 VariantInternal::initialize(iterator, Variant::FLOAT);
2934 *VariantInternal::get_float(iterator) = from;
2935
2936 // Skip regular iterate.
2937 ip += 5;
2938 } else {
2939 // Jump to end of loop.
2940 int jumpto = _code_ptr[ip + 4];
2941 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
2942 ip = jumpto;
2943 }
2944 }
2945 DISPATCH_OPCODE;
2946
2947 OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3I) {
2948 CHECK_SPACE(8); // Check space for iterate instruction too.
2949
2950 GET_VARIANT_PTR(counter, 0);
2951 GET_VARIANT_PTR(container, 1);
2952
2953 Vector3i *bounds = VariantInternal::get_vector3i(container);
2954 int64_t from = bounds->x;
2955 int64_t to = bounds->y;
2956 int64_t step = bounds->z;
2957
2958 VariantInternal::initialize(counter, Variant::INT);
2959 *VariantInternal::get_int(counter) = from;
2960
2961 bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
2962
2963 if (do_continue) {
2964 GET_VARIANT_PTR(iterator, 2);
2965 VariantInternal::initialize(iterator, Variant::INT);
2966 *VariantInternal::get_int(iterator) = from;
2967
2968 // Skip regular iterate.
2969 ip += 5;
2970 } else {
2971 // Jump to end of loop.
2972 int jumpto = _code_ptr[ip + 4];
2973 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
2974 ip = jumpto;
2975 }
2976 }
2977 DISPATCH_OPCODE;
2978
2979 OPCODE(OPCODE_ITERATE_BEGIN_STRING) {
2980 CHECK_SPACE(8); // Check space for iterate instruction too.
2981
2982 GET_VARIANT_PTR(counter, 0);
2983 GET_VARIANT_PTR(container, 1);
2984
2985 String *str = VariantInternal::get_string(container);
2986
2987 VariantInternal::initialize(counter, Variant::INT);
2988 *VariantInternal::get_int(counter) = 0;
2989
2990 if (!str->is_empty()) {
2991 GET_VARIANT_PTR(iterator, 2);
2992 VariantInternal::initialize(iterator, Variant::STRING);
2993 *VariantInternal::get_string(iterator) = str->substr(0, 1);
2994
2995 // Skip regular iterate.
2996 ip += 5;
2997 } else {
2998 // Jump to end of loop.
2999 int jumpto = _code_ptr[ip + 4];
3000 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3001 ip = jumpto;
3002 }
3003 }
3004 DISPATCH_OPCODE;
3005
3006 OPCODE(OPCODE_ITERATE_BEGIN_DICTIONARY) {
3007 CHECK_SPACE(8); // Check space for iterate instruction too.
3008
3009 GET_VARIANT_PTR(counter, 0);
3010 GET_VARIANT_PTR(container, 1);
3011
3012 Dictionary *dict = VariantInternal::get_dictionary(container);
3013 const Variant *next = dict->next(nullptr);
3014
3015 if (!dict->is_empty()) {
3016 GET_VARIANT_PTR(iterator, 2);
3017 *counter = *next;
3018 *iterator = *next;
3019
3020 // Skip regular iterate.
3021 ip += 5;
3022 } else {
3023 // Jump to end of loop.
3024 int jumpto = _code_ptr[ip + 4];
3025 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3026 ip = jumpto;
3027 }
3028 }
3029 DISPATCH_OPCODE;
3030
3031 OPCODE(OPCODE_ITERATE_BEGIN_ARRAY) {
3032 CHECK_SPACE(8); // Check space for iterate instruction too.
3033
3034 GET_VARIANT_PTR(counter, 0);
3035 GET_VARIANT_PTR(container, 1);
3036
3037 Array *array = VariantInternal::get_array(container);
3038
3039 VariantInternal::initialize(counter, Variant::INT);
3040 *VariantInternal::get_int(counter) = 0;
3041
3042 if (!array->is_empty()) {
3043 GET_VARIANT_PTR(iterator, 2);
3044 *iterator = array->get(0);
3045
3046 // Skip regular iterate.
3047 ip += 5;
3048 } else {
3049 // Jump to end of loop.
3050 int jumpto = _code_ptr[ip + 4];
3051 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3052 ip = jumpto;
3053 }
3054 }
3055 DISPATCH_OPCODE;
3056
3057#define OPCODE_ITERATE_BEGIN_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_var_ret_type, m_ret_type, m_ret_get_func) \
3058 OPCODE(OPCODE_ITERATE_BEGIN_PACKED_##m_var_type##_ARRAY) { \
3059 CHECK_SPACE(8); \
3060 GET_VARIANT_PTR(counter, 0); \
3061 GET_VARIANT_PTR(container, 1); \
3062 Vector<m_elem_type> *array = VariantInternal::m_get_func(container); \
3063 VariantInternal::initialize(counter, Variant::INT); \
3064 *VariantInternal::get_int(counter) = 0; \
3065 if (!array->is_empty()) { \
3066 GET_VARIANT_PTR(iterator, 2); \
3067 VariantInternal::initialize(iterator, Variant::m_var_ret_type); \
3068 m_ret_type *it = VariantInternal::m_ret_get_func(iterator); \
3069 *it = array->get(0); \
3070 ip += 5; \
3071 } else { \
3072 int jumpto = _code_ptr[ip + 4]; \
3073 GD_ERR_BREAK(jumpto<0 || jumpto> _code_size); \
3074 ip = jumpto; \
3075 } \
3076 } \
3077 DISPATCH_OPCODE
3078
3079 OPCODE_ITERATE_BEGIN_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, INT, int64_t, get_int);
3080 OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT32, int32_t, get_int32_array, INT, int64_t, get_int);
3081 OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT64, int64_t, get_int64_array, INT, int64_t, get_int);
3082 OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT32, float, get_float32_array, FLOAT, double, get_float);
3083 OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT64, double, get_float64_array, FLOAT, double, get_float);
3084 OPCODE_ITERATE_BEGIN_PACKED_ARRAY(STRING, String, get_string_array, STRING, String, get_string);
3085 OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, VECTOR2, Vector2, get_vector2);
3086 OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, VECTOR3, Vector3, get_vector3);
3087 OPCODE_ITERATE_BEGIN_PACKED_ARRAY(COLOR, Color, get_color_array, COLOR, Color, get_color);
3088
3089 OPCODE(OPCODE_ITERATE_BEGIN_OBJECT) {
3090 CHECK_SPACE(4);
3091
3092 GET_VARIANT_PTR(counter, 0);
3093 GET_VARIANT_PTR(container, 1);
3094
3095#ifdef DEBUG_ENABLED
3096 bool freed = false;
3097 Object *obj = container->get_validated_object_with_check(freed);
3098 if (freed) {
3099 err_text = "Trying to iterate on a previously freed object.";
3100 OPCODE_BREAK;
3101 } else if (!obj) {
3102 err_text = "Trying to iterate on a null value.";
3103 OPCODE_BREAK;
3104 }
3105#else
3106 Object *obj = *VariantInternal::get_object(container);
3107#endif
3108 Array ref;
3109 ref.push_back(*counter);
3110 Variant vref;
3111 VariantInternal::initialize(&vref, Variant::ARRAY);
3112 *VariantInternal::get_array(&vref) = ref;
3113
3114 Variant **args = instruction_args; // Overriding an instruction argument, but we don't need access to that anymore.
3115 args[0] = &vref;
3116
3117 Callable::CallError ce;
3118 Variant has_next = obj->callp(CoreStringNames::get_singleton()->_iter_init, (const Variant **)args, 1, ce);
3119
3120#ifdef DEBUG_ENABLED
3121 if (ce.error != Callable::CallError::CALL_OK) {
3122 err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
3123 OPCODE_BREAK;
3124 }
3125#endif
3126 if (!has_next.booleanize()) {
3127 int jumpto = _code_ptr[ip + 4];
3128 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3129 ip = jumpto;
3130 } else {
3131 GET_VARIANT_PTR(iterator, 2);
3132 *iterator = obj->callp(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce);
3133#ifdef DEBUG_ENABLED
3134 if (ce.error != Callable::CallError::CALL_OK) {
3135 err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
3136 OPCODE_BREAK;
3137 }
3138#endif
3139
3140 ip += 5; // Loop again.
3141 }
3142 }
3143 DISPATCH_OPCODE;
3144
3145 OPCODE(OPCODE_ITERATE) {
3146 CHECK_SPACE(4);
3147
3148 GET_VARIANT_PTR(counter, 0);
3149 GET_VARIANT_PTR(container, 1);
3150
3151 bool valid;
3152 if (!container->iter_next(*counter, valid)) {
3153#ifdef DEBUG_ENABLED
3154 if (!valid) {
3155 err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
3156 OPCODE_BREAK;
3157 }
3158#endif
3159 int jumpto = _code_ptr[ip + 4];
3160 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3161 ip = jumpto;
3162 } else {
3163 GET_VARIANT_PTR(iterator, 2);
3164
3165 *iterator = container->iter_get(*counter, valid);
3166#ifdef DEBUG_ENABLED
3167 if (!valid) {
3168 err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
3169 OPCODE_BREAK;
3170 }
3171#endif
3172 ip += 5; //loop again
3173 }
3174 }
3175 DISPATCH_OPCODE;
3176
3177 OPCODE(OPCODE_ITERATE_INT) {
3178 CHECK_SPACE(4);
3179
3180 GET_VARIANT_PTR(counter, 0);
3181 GET_VARIANT_PTR(container, 1);
3182
3183 int64_t size = *VariantInternal::get_int(container);
3184 int64_t *count = VariantInternal::get_int(counter);
3185
3186 (*count)++;
3187
3188 if (*count >= size) {
3189 int jumpto = _code_ptr[ip + 4];
3190 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3191 ip = jumpto;
3192 } else {
3193 GET_VARIANT_PTR(iterator, 2);
3194 *VariantInternal::get_int(iterator) = *count;
3195
3196 ip += 5; // Loop again.
3197 }
3198 }
3199 DISPATCH_OPCODE;
3200
3201 OPCODE(OPCODE_ITERATE_FLOAT) {
3202 CHECK_SPACE(4);
3203
3204 GET_VARIANT_PTR(counter, 0);
3205 GET_VARIANT_PTR(container, 1);
3206
3207 double size = *VariantInternal::get_float(container);
3208 double *count = VariantInternal::get_float(counter);
3209
3210 (*count)++;
3211
3212 if (*count >= size) {
3213 int jumpto = _code_ptr[ip + 4];
3214 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3215 ip = jumpto;
3216 } else {
3217 GET_VARIANT_PTR(iterator, 2);
3218 *VariantInternal::get_float(iterator) = *count;
3219
3220 ip += 5; // Loop again.
3221 }
3222 }
3223 DISPATCH_OPCODE;
3224
3225 OPCODE(OPCODE_ITERATE_VECTOR2) {
3226 CHECK_SPACE(4);
3227
3228 GET_VARIANT_PTR(counter, 0);
3229 GET_VARIANT_PTR(container, 1);
3230
3231 const Vector2 *bounds = VariantInternal::get_vector2((const Variant *)container);
3232 double *count = VariantInternal::get_float(counter);
3233
3234 (*count)++;
3235
3236 if (*count >= bounds->y) {
3237 int jumpto = _code_ptr[ip + 4];
3238 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3239 ip = jumpto;
3240 } else {
3241 GET_VARIANT_PTR(iterator, 2);
3242 *VariantInternal::get_float(iterator) = *count;
3243
3244 ip += 5; // Loop again.
3245 }
3246 }
3247 DISPATCH_OPCODE;
3248
3249 OPCODE(OPCODE_ITERATE_VECTOR2I) {
3250 CHECK_SPACE(4);
3251
3252 GET_VARIANT_PTR(counter, 0);
3253 GET_VARIANT_PTR(container, 1);
3254
3255 const Vector2i *bounds = VariantInternal::get_vector2i((const Variant *)container);
3256 int64_t *count = VariantInternal::get_int(counter);
3257
3258 (*count)++;
3259
3260 if (*count >= bounds->y) {
3261 int jumpto = _code_ptr[ip + 4];
3262 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3263 ip = jumpto;
3264 } else {
3265 GET_VARIANT_PTR(iterator, 2);
3266 *VariantInternal::get_int(iterator) = *count;
3267
3268 ip += 5; // Loop again.
3269 }
3270 }
3271 DISPATCH_OPCODE;
3272
3273 OPCODE(OPCODE_ITERATE_VECTOR3) {
3274 CHECK_SPACE(4);
3275
3276 GET_VARIANT_PTR(counter, 0);
3277 GET_VARIANT_PTR(container, 1);
3278
3279 const Vector3 *bounds = VariantInternal::get_vector3((const Variant *)container);
3280 double *count = VariantInternal::get_float(counter);
3281
3282 *count += bounds->z;
3283
3284 if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
3285 int jumpto = _code_ptr[ip + 4];
3286 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3287 ip = jumpto;
3288 } else {
3289 GET_VARIANT_PTR(iterator, 2);
3290 *VariantInternal::get_float(iterator) = *count;
3291
3292 ip += 5; // Loop again.
3293 }
3294 }
3295 DISPATCH_OPCODE;
3296
3297 OPCODE(OPCODE_ITERATE_VECTOR3I) {
3298 CHECK_SPACE(4);
3299
3300 GET_VARIANT_PTR(counter, 0);
3301 GET_VARIANT_PTR(container, 1);
3302
3303 const Vector3i *bounds = VariantInternal::get_vector3i((const Variant *)container);
3304 int64_t *count = VariantInternal::get_int(counter);
3305
3306 *count += bounds->z;
3307
3308 if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
3309 int jumpto = _code_ptr[ip + 4];
3310 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3311 ip = jumpto;
3312 } else {
3313 GET_VARIANT_PTR(iterator, 2);
3314 *VariantInternal::get_int(iterator) = *count;
3315
3316 ip += 5; // Loop again.
3317 }
3318 }
3319 DISPATCH_OPCODE;
3320
3321 OPCODE(OPCODE_ITERATE_STRING) {
3322 CHECK_SPACE(4);
3323
3324 GET_VARIANT_PTR(counter, 0);
3325 GET_VARIANT_PTR(container, 1);
3326
3327 const String *str = VariantInternal::get_string((const Variant *)container);
3328 int64_t *idx = VariantInternal::get_int(counter);
3329 (*idx)++;
3330
3331 if (*idx >= str->length()) {
3332 int jumpto = _code_ptr[ip + 4];
3333 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3334 ip = jumpto;
3335 } else {
3336 GET_VARIANT_PTR(iterator, 2);
3337 *VariantInternal::get_string(iterator) = str->substr(*idx, 1);
3338
3339 ip += 5; // Loop again.
3340 }
3341 }
3342 DISPATCH_OPCODE;
3343
3344 OPCODE(OPCODE_ITERATE_DICTIONARY) {
3345 CHECK_SPACE(4);
3346
3347 GET_VARIANT_PTR(counter, 0);
3348 GET_VARIANT_PTR(container, 1);
3349
3350 const Dictionary *dict = VariantInternal::get_dictionary((const Variant *)container);
3351 const Variant *next = dict->next(counter);
3352
3353 if (!next) {
3354 int jumpto = _code_ptr[ip + 4];
3355 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3356 ip = jumpto;
3357 } else {
3358 GET_VARIANT_PTR(iterator, 2);
3359 *counter = *next;
3360 *iterator = *next;
3361
3362 ip += 5; // Loop again.
3363 }
3364 }
3365 DISPATCH_OPCODE;
3366
3367 OPCODE(OPCODE_ITERATE_ARRAY) {
3368 CHECK_SPACE(4);
3369
3370 GET_VARIANT_PTR(counter, 0);
3371 GET_VARIANT_PTR(container, 1);
3372
3373 const Array *array = VariantInternal::get_array((const Variant *)container);
3374 int64_t *idx = VariantInternal::get_int(counter);
3375 (*idx)++;
3376
3377 if (*idx >= array->size()) {
3378 int jumpto = _code_ptr[ip + 4];
3379 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3380 ip = jumpto;
3381 } else {
3382 GET_VARIANT_PTR(iterator, 2);
3383 *iterator = array->get(*idx);
3384
3385 ip += 5; // Loop again.
3386 }
3387 }
3388 DISPATCH_OPCODE;
3389
3390#define OPCODE_ITERATE_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_ret_get_func) \
3391 OPCODE(OPCODE_ITERATE_PACKED_##m_var_type##_ARRAY) { \
3392 CHECK_SPACE(4); \
3393 GET_VARIANT_PTR(counter, 0); \
3394 GET_VARIANT_PTR(container, 1); \
3395 const Vector<m_elem_type> *array = VariantInternal::m_get_func((const Variant *)container); \
3396 int64_t *idx = VariantInternal::get_int(counter); \
3397 (*idx)++; \
3398 if (*idx >= array->size()) { \
3399 int jumpto = _code_ptr[ip + 4]; \
3400 GD_ERR_BREAK(jumpto<0 || jumpto> _code_size); \
3401 ip = jumpto; \
3402 } else { \
3403 GET_VARIANT_PTR(iterator, 2); \
3404 *VariantInternal::m_ret_get_func(iterator) = array->get(*idx); \
3405 ip += 5; \
3406 } \
3407 } \
3408 DISPATCH_OPCODE
3409
3410 OPCODE_ITERATE_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, get_int);
3411 OPCODE_ITERATE_PACKED_ARRAY(INT32, int32_t, get_int32_array, get_int);
3412 OPCODE_ITERATE_PACKED_ARRAY(INT64, int64_t, get_int64_array, get_int);
3413 OPCODE_ITERATE_PACKED_ARRAY(FLOAT32, float, get_float32_array, get_float);
3414 OPCODE_ITERATE_PACKED_ARRAY(FLOAT64, double, get_float64_array, get_float);
3415 OPCODE_ITERATE_PACKED_ARRAY(STRING, String, get_string_array, get_string);
3416 OPCODE_ITERATE_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, get_vector2);
3417 OPCODE_ITERATE_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, get_vector3);
3418 OPCODE_ITERATE_PACKED_ARRAY(COLOR, Color, get_color_array, get_color);
3419
3420 OPCODE(OPCODE_ITERATE_OBJECT) {
3421 CHECK_SPACE(4);
3422
3423 GET_VARIANT_PTR(counter, 0);
3424 GET_VARIANT_PTR(container, 1);
3425
3426#ifdef DEBUG_ENABLED
3427 bool freed = false;
3428 Object *obj = container->get_validated_object_with_check(freed);
3429 if (freed) {
3430 err_text = "Trying to iterate on a previously freed object.";
3431 OPCODE_BREAK;
3432 } else if (!obj) {
3433 err_text = "Trying to iterate on a null value.";
3434 OPCODE_BREAK;
3435 }
3436#else
3437 Object *obj = *VariantInternal::get_object(container);
3438#endif
3439 Array ref;
3440 ref.push_back(*counter);
3441 Variant vref;
3442 VariantInternal::initialize(&vref, Variant::ARRAY);
3443 *VariantInternal::get_array(&vref) = ref;
3444
3445 Variant **args = instruction_args; // Overriding an instruction argument, but we don't need access to that anymore.
3446 args[0] = &vref;
3447
3448 Callable::CallError ce;
3449 Variant has_next = obj->callp(CoreStringNames::get_singleton()->_iter_next, (const Variant **)args, 1, ce);
3450
3451#ifdef DEBUG_ENABLED
3452 if (ce.error != Callable::CallError::CALL_OK) {
3453 err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
3454 OPCODE_BREAK;
3455 }
3456#endif
3457 if (!has_next.booleanize()) {
3458 int jumpto = _code_ptr[ip + 4];
3459 GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
3460 ip = jumpto;
3461 } else {
3462 GET_VARIANT_PTR(iterator, 2);
3463 *iterator = obj->callp(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce);
3464#ifdef DEBUG_ENABLED
3465 if (ce.error != Callable::CallError::CALL_OK) {
3466 err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
3467 OPCODE_BREAK;
3468 }
3469#endif
3470
3471 ip += 5; // Loop again.
3472 }
3473 }
3474 DISPATCH_OPCODE;
3475
3476 OPCODE(OPCODE_STORE_GLOBAL) {
3477 CHECK_SPACE(3);
3478 int global_idx = _code_ptr[ip + 2];
3479 GD_ERR_BREAK(global_idx < 0 || global_idx >= GDScriptLanguage::get_singleton()->get_global_array_size());
3480
3481 GET_VARIANT_PTR(dst, 0);
3482 *dst = GDScriptLanguage::get_singleton()->get_global_array()[global_idx];
3483
3484 ip += 3;
3485 }
3486 DISPATCH_OPCODE;
3487
3488 OPCODE(OPCODE_STORE_NAMED_GLOBAL) {
3489 CHECK_SPACE(3);
3490 int globalname_idx = _code_ptr[ip + 2];
3491 GD_ERR_BREAK(globalname_idx < 0 || globalname_idx >= _global_names_count);
3492 const StringName *globalname = &_global_names_ptr[globalname_idx];
3493 GD_ERR_BREAK(!GDScriptLanguage::get_singleton()->get_named_globals_map().has(*globalname));
3494
3495 GET_VARIANT_PTR(dst, 0);
3496 *dst = GDScriptLanguage::get_singleton()->get_named_globals_map()[*globalname];
3497
3498 ip += 3;
3499 }
3500 DISPATCH_OPCODE;
3501
3502#define OPCODE_TYPE_ADJUST(m_v_type, m_c_type) \
3503 OPCODE(OPCODE_TYPE_ADJUST_##m_v_type) { \
3504 CHECK_SPACE(2); \
3505 GET_VARIANT_PTR(arg, 0); \
3506 VariantTypeAdjust<m_c_type>::adjust(arg); \
3507 ip += 2; \
3508 } \
3509 DISPATCH_OPCODE
3510
3511 OPCODE_TYPE_ADJUST(BOOL, bool);
3512 OPCODE_TYPE_ADJUST(INT, int64_t);
3513 OPCODE_TYPE_ADJUST(FLOAT, double);
3514 OPCODE_TYPE_ADJUST(STRING, String);
3515 OPCODE_TYPE_ADJUST(VECTOR2, Vector2);
3516 OPCODE_TYPE_ADJUST(VECTOR2I, Vector2i);
3517 OPCODE_TYPE_ADJUST(RECT2, Rect2);
3518 OPCODE_TYPE_ADJUST(RECT2I, Rect2i);
3519 OPCODE_TYPE_ADJUST(VECTOR3, Vector3);
3520 OPCODE_TYPE_ADJUST(VECTOR3I, Vector3i);
3521 OPCODE_TYPE_ADJUST(TRANSFORM2D, Transform2D);
3522 OPCODE_TYPE_ADJUST(VECTOR4, Vector4);
3523 OPCODE_TYPE_ADJUST(VECTOR4I, Vector4i);
3524 OPCODE_TYPE_ADJUST(PLANE, Plane);
3525 OPCODE_TYPE_ADJUST(QUATERNION, Quaternion);
3526 OPCODE_TYPE_ADJUST(AABB, AABB);
3527 OPCODE_TYPE_ADJUST(BASIS, Basis);
3528 OPCODE_TYPE_ADJUST(TRANSFORM3D, Transform3D);
3529 OPCODE_TYPE_ADJUST(PROJECTION, Projection);
3530 OPCODE_TYPE_ADJUST(COLOR, Color);
3531 OPCODE_TYPE_ADJUST(STRING_NAME, StringName);
3532 OPCODE_TYPE_ADJUST(NODE_PATH, NodePath);
3533 OPCODE_TYPE_ADJUST(RID, RID);
3534 OPCODE_TYPE_ADJUST(OBJECT, Object *);
3535 OPCODE_TYPE_ADJUST(CALLABLE, Callable);
3536 OPCODE_TYPE_ADJUST(SIGNAL, Signal);
3537 OPCODE_TYPE_ADJUST(DICTIONARY, Dictionary);
3538 OPCODE_TYPE_ADJUST(ARRAY, Array);
3539 OPCODE_TYPE_ADJUST(PACKED_BYTE_ARRAY, PackedByteArray);
3540 OPCODE_TYPE_ADJUST(PACKED_INT32_ARRAY, PackedInt32Array);
3541 OPCODE_TYPE_ADJUST(PACKED_INT64_ARRAY, PackedInt64Array);
3542 OPCODE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY, PackedFloat32Array);
3543 OPCODE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY, PackedFloat64Array);
3544 OPCODE_TYPE_ADJUST(PACKED_STRING_ARRAY, PackedStringArray);
3545 OPCODE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY, PackedVector2Array);
3546 OPCODE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY, PackedVector3Array);
3547 OPCODE_TYPE_ADJUST(PACKED_COLOR_ARRAY, PackedColorArray);
3548
3549 OPCODE(OPCODE_ASSERT) {
3550 CHECK_SPACE(3);
3551
3552#ifdef DEBUG_ENABLED
3553 GET_VARIANT_PTR(test, 0);
3554 bool result = test->booleanize();
3555
3556 if (!result) {
3557 String message_str;
3558 if (_code_ptr[ip + 2] != 0) {
3559 GET_VARIANT_PTR(message, 1);
3560 Variant message_var = *message;
3561 if (message->get_type() != Variant::NIL) {
3562 message_str = message_var;
3563 }
3564 }
3565 if (message_str.is_empty()) {
3566 err_text = "Assertion failed.";
3567 } else {
3568 err_text = "Assertion failed: " + message_str;
3569 }
3570 OPCODE_BREAK;
3571 }
3572
3573#endif
3574 ip += 3;
3575 }
3576 DISPATCH_OPCODE;
3577
3578 OPCODE(OPCODE_BREAKPOINT) {
3579#ifdef DEBUG_ENABLED
3580 if (EngineDebugger::is_active()) {
3581 GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
3582 }
3583#endif
3584 ip += 1;
3585 }
3586 DISPATCH_OPCODE;
3587
3588 OPCODE(OPCODE_LINE) {
3589 CHECK_SPACE(2);
3590
3591 line = _code_ptr[ip + 1];
3592 ip += 2;
3593
3594 if (EngineDebugger::is_active()) {
3595 // line
3596 bool do_break = false;
3597
3598 if (unlikely(EngineDebugger::get_script_debugger()->get_lines_left() > 0)) {
3599 if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
3600 EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
3601 }
3602 if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
3603 do_break = true;
3604 }
3605 }
3606
3607 if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
3608 do_break = true;
3609 }
3610
3611 if (unlikely(do_break)) {
3612 GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
3613 }
3614
3615 EngineDebugger::get_singleton()->line_poll();
3616 }
3617 }
3618 DISPATCH_OPCODE;
3619
3620 OPCODE(OPCODE_END) {
3621#ifdef DEBUG_ENABLED
3622 exit_ok = true;
3623#endif
3624 OPCODE_BREAK;
3625 }
3626
3627#if 0 // Enable for debugging.
3628 default: {
3629 err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
3630 OPCODE_BREAK;
3631 }
3632#endif
3633 }
3634
3635 OPCODES_END
3636#ifdef DEBUG_ENABLED
3637 if (exit_ok) {
3638 OPCODE_OUT;
3639 }
3640 //error
3641 // function, file, line, error, explanation
3642 String err_file;
3643 bool instance_valid_with_script = p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid();
3644 if (instance_valid_with_script && !get_script()->path.is_empty()) {
3645 err_file = get_script()->path;
3646 } else if (script) {
3647 err_file = script->path;
3648 }
3649 if (err_file.is_empty()) {
3650 err_file = "<built-in>";
3651 }
3652 String err_func = name;
3653 if (instance_valid_with_script && p_instance->script->local_name != StringName()) {
3654 err_func = p_instance->script->local_name.operator String() + "." + err_func;
3655 }
3656 int err_line = line;
3657 if (err_text.is_empty()) {
3658 err_text = "Internal script error! Opcode: " + itos(last_opcode) + " (please report).";
3659 }
3660
3661 if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
3662 // debugger break did not happen
3663
3664 _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), false, ERR_HANDLER_SCRIPT);
3665 }
3666
3667 // Get a default return type in case of failure
3668 retvalue = _get_default_variant_for_data_type(return_type);
3669#endif
3670
3671 OPCODE_OUT;
3672 }
3673
3674 OPCODES_OUT
3675#ifdef DEBUG_ENABLED
3676 if (GDScriptLanguage::get_singleton()->profiling) {
3677 uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
3678 profile.total_time.add(time_taken);
3679 profile.self_time.add(time_taken - function_call_time);
3680 profile.frame_total_time.add(time_taken);
3681 profile.frame_self_time.add(time_taken - function_call_time);
3682 if (Thread::get_caller_id() == Thread::get_main_id()) {
3683 GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
3684 }
3685 }
3686
3687 // Check if this is not the last time it was interrupted by `await` or if it's the first time executing.
3688 // If that is the case then we exit the function as normal. Otherwise we postpone it until the last `await` is completed.
3689 // This ensures the call stack can be properly shown when using `await`, showing what resumed the function.
3690 if (!p_state || awaited) {
3691 if (EngineDebugger::is_active()) {
3692 GDScriptLanguage::get_singleton()->exit_function();
3693 }
3694#endif
3695
3696 // Free stack, except reserved addresses.
3697 for (int i = FIXED_ADDRESSES_MAX; i < _stack_size; i++) {
3698 stack[i].~Variant();
3699 }
3700#ifdef DEBUG_ENABLED
3701 }
3702#endif
3703
3704 // Always free reserved addresses, since they are never copied.
3705 for (int i = 0; i < FIXED_ADDRESSES_MAX; i++) {
3706 stack[i].~Variant();
3707 }
3708
3709 call_depth--;
3710
3711 return retvalue;
3712}
3713