Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_function.h
10277 views
1
/**************************************************************************/
2
/* gdscript_function.h */
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
#pragma once
32
33
#include "gdscript_utility_functions.h"
34
35
#include "core/object/ref_counted.h"
36
#include "core/object/script_language.h"
37
#include "core/os/thread.h"
38
#include "core/string/string_name.h"
39
#include "core/templates/pair.h"
40
#include "core/templates/self_list.h"
41
#include "core/variant/variant.h"
42
43
class GDScriptInstance;
44
class GDScript;
45
46
class GDScriptDataType {
47
public:
48
Vector<GDScriptDataType> container_element_types;
49
50
enum Kind {
51
UNINITIALIZED,
52
BUILTIN,
53
NATIVE,
54
SCRIPT,
55
GDSCRIPT,
56
};
57
58
Kind kind = UNINITIALIZED;
59
60
bool has_type = false;
61
Variant::Type builtin_type = Variant::NIL;
62
StringName native_type;
63
Script *script_type = nullptr;
64
Ref<Script> script_type_ref;
65
66
bool is_type(const Variant &p_variant, bool p_allow_implicit_conversion = false) const {
67
if (!has_type) {
68
return true; // Can't type check
69
}
70
71
switch (kind) {
72
case UNINITIALIZED:
73
break;
74
case BUILTIN: {
75
Variant::Type var_type = p_variant.get_type();
76
bool valid = builtin_type == var_type;
77
if (valid && builtin_type == Variant::ARRAY && has_container_element_type(0)) {
78
Array array = p_variant;
79
if (array.is_typed()) {
80
const GDScriptDataType &elem_type = container_element_types[0];
81
Variant::Type array_builtin_type = (Variant::Type)array.get_typed_builtin();
82
StringName array_native_type = array.get_typed_class_name();
83
Ref<Script> array_script_type_ref = array.get_typed_script();
84
85
if (array_script_type_ref.is_valid()) {
86
valid = (elem_type.kind == SCRIPT || elem_type.kind == GDSCRIPT) && elem_type.script_type == array_script_type_ref.ptr();
87
} else if (array_native_type != StringName()) {
88
valid = elem_type.kind == NATIVE && elem_type.native_type == array_native_type;
89
} else {
90
valid = elem_type.kind == BUILTIN && elem_type.builtin_type == array_builtin_type;
91
}
92
} else {
93
valid = false;
94
}
95
} else if (valid && builtin_type == Variant::DICTIONARY && has_container_element_types()) {
96
Dictionary dictionary = p_variant;
97
if (dictionary.is_typed()) {
98
if (dictionary.is_typed_key()) {
99
GDScriptDataType key = get_container_element_type_or_variant(0);
100
Variant::Type key_builtin_type = (Variant::Type)dictionary.get_typed_key_builtin();
101
StringName key_native_type = dictionary.get_typed_key_class_name();
102
Ref<Script> key_script_type_ref = dictionary.get_typed_key_script();
103
104
if (key_script_type_ref.is_valid()) {
105
valid = (key.kind == SCRIPT || key.kind == GDSCRIPT) && key.script_type == key_script_type_ref.ptr();
106
} else if (key_native_type != StringName()) {
107
valid = key.kind == NATIVE && key.native_type == key_native_type;
108
} else {
109
valid = key.kind == BUILTIN && key.builtin_type == key_builtin_type;
110
}
111
}
112
113
if (valid && dictionary.is_typed_value()) {
114
GDScriptDataType value = get_container_element_type_or_variant(1);
115
Variant::Type value_builtin_type = (Variant::Type)dictionary.get_typed_value_builtin();
116
StringName value_native_type = dictionary.get_typed_value_class_name();
117
Ref<Script> value_script_type_ref = dictionary.get_typed_value_script();
118
119
if (value_script_type_ref.is_valid()) {
120
valid = (value.kind == SCRIPT || value.kind == GDSCRIPT) && value.script_type == value_script_type_ref.ptr();
121
} else if (value_native_type != StringName()) {
122
valid = value.kind == NATIVE && value.native_type == value_native_type;
123
} else {
124
valid = value.kind == BUILTIN && value.builtin_type == value_builtin_type;
125
}
126
}
127
} else {
128
valid = false;
129
}
130
} else if (!valid && p_allow_implicit_conversion) {
131
valid = Variant::can_convert_strict(var_type, builtin_type);
132
}
133
return valid;
134
} break;
135
case NATIVE: {
136
if (p_variant.get_type() == Variant::NIL) {
137
return true;
138
}
139
if (p_variant.get_type() != Variant::OBJECT) {
140
return false;
141
}
142
143
bool was_freed = false;
144
Object *obj = p_variant.get_validated_object_with_check(was_freed);
145
if (!obj) {
146
return !was_freed;
147
}
148
149
if (!ClassDB::is_parent_class(obj->get_class_name(), native_type)) {
150
return false;
151
}
152
return true;
153
} break;
154
case SCRIPT:
155
case GDSCRIPT: {
156
if (p_variant.get_type() == Variant::NIL) {
157
return true;
158
}
159
if (p_variant.get_type() != Variant::OBJECT) {
160
return false;
161
}
162
163
bool was_freed = false;
164
Object *obj = p_variant.get_validated_object_with_check(was_freed);
165
if (!obj) {
166
return !was_freed;
167
}
168
169
Ref<Script> base = obj && obj->get_script_instance() ? obj->get_script_instance()->get_script() : nullptr;
170
bool valid = false;
171
while (base.is_valid()) {
172
if (base == script_type) {
173
valid = true;
174
break;
175
}
176
base = base->get_base_script();
177
}
178
return valid;
179
} break;
180
}
181
return false;
182
}
183
184
bool can_contain_object() const {
185
if (has_type && kind == BUILTIN) {
186
switch (builtin_type) {
187
case Variant::ARRAY:
188
if (has_container_element_type(0)) {
189
return container_element_types[0].can_contain_object();
190
}
191
return true;
192
case Variant::DICTIONARY:
193
if (has_container_element_types()) {
194
return get_container_element_type_or_variant(0).can_contain_object() || get_container_element_type_or_variant(1).can_contain_object();
195
}
196
return true;
197
case Variant::NIL:
198
case Variant::OBJECT:
199
return true;
200
default:
201
return false;
202
}
203
}
204
return true;
205
}
206
207
void set_container_element_type(int p_index, const GDScriptDataType &p_element_type) {
208
ERR_FAIL_COND(p_index < 0);
209
while (p_index >= container_element_types.size()) {
210
container_element_types.push_back(GDScriptDataType());
211
}
212
container_element_types.write[p_index] = GDScriptDataType(p_element_type);
213
}
214
215
GDScriptDataType get_container_element_type(int p_index) const {
216
ERR_FAIL_INDEX_V(p_index, container_element_types.size(), GDScriptDataType());
217
return container_element_types[p_index];
218
}
219
220
GDScriptDataType get_container_element_type_or_variant(int p_index) const {
221
if (p_index < 0 || p_index >= container_element_types.size()) {
222
return GDScriptDataType();
223
}
224
return container_element_types[p_index];
225
}
226
227
bool has_container_element_type(int p_index) const {
228
return p_index >= 0 && p_index < container_element_types.size();
229
}
230
231
bool has_container_element_types() const {
232
return !container_element_types.is_empty();
233
}
234
235
GDScriptDataType() = default;
236
237
bool operator==(const GDScriptDataType &p_other) const {
238
return kind == p_other.kind &&
239
has_type == p_other.has_type &&
240
builtin_type == p_other.builtin_type &&
241
native_type == p_other.native_type &&
242
(script_type == p_other.script_type || script_type_ref == p_other.script_type_ref) &&
243
container_element_types == p_other.container_element_types;
244
}
245
246
bool operator!=(const GDScriptDataType &p_other) const {
247
return !(*this == p_other);
248
}
249
250
void operator=(const GDScriptDataType &p_other) {
251
kind = p_other.kind;
252
has_type = p_other.has_type;
253
builtin_type = p_other.builtin_type;
254
native_type = p_other.native_type;
255
script_type = p_other.script_type;
256
script_type_ref = p_other.script_type_ref;
257
container_element_types = p_other.container_element_types;
258
}
259
260
GDScriptDataType(const GDScriptDataType &p_other) {
261
*this = p_other;
262
}
263
264
~GDScriptDataType() {}
265
};
266
267
class GDScriptFunction {
268
public:
269
enum Opcode {
270
OPCODE_OPERATOR,
271
OPCODE_OPERATOR_VALIDATED,
272
OPCODE_TYPE_TEST_BUILTIN,
273
OPCODE_TYPE_TEST_ARRAY,
274
OPCODE_TYPE_TEST_DICTIONARY,
275
OPCODE_TYPE_TEST_NATIVE,
276
OPCODE_TYPE_TEST_SCRIPT,
277
OPCODE_SET_KEYED,
278
OPCODE_SET_KEYED_VALIDATED,
279
OPCODE_SET_INDEXED_VALIDATED,
280
OPCODE_GET_KEYED,
281
OPCODE_GET_KEYED_VALIDATED,
282
OPCODE_GET_INDEXED_VALIDATED,
283
OPCODE_SET_NAMED,
284
OPCODE_SET_NAMED_VALIDATED,
285
OPCODE_GET_NAMED,
286
OPCODE_GET_NAMED_VALIDATED,
287
OPCODE_SET_MEMBER,
288
OPCODE_GET_MEMBER,
289
OPCODE_SET_STATIC_VARIABLE, // Only for GDScript.
290
OPCODE_GET_STATIC_VARIABLE, // Only for GDScript.
291
OPCODE_ASSIGN,
292
OPCODE_ASSIGN_NULL,
293
OPCODE_ASSIGN_TRUE,
294
OPCODE_ASSIGN_FALSE,
295
OPCODE_ASSIGN_TYPED_BUILTIN,
296
OPCODE_ASSIGN_TYPED_ARRAY,
297
OPCODE_ASSIGN_TYPED_DICTIONARY,
298
OPCODE_ASSIGN_TYPED_NATIVE,
299
OPCODE_ASSIGN_TYPED_SCRIPT,
300
OPCODE_CAST_TO_BUILTIN,
301
OPCODE_CAST_TO_NATIVE,
302
OPCODE_CAST_TO_SCRIPT,
303
OPCODE_CONSTRUCT, // Only for basic types!
304
OPCODE_CONSTRUCT_VALIDATED, // Only for basic types!
305
OPCODE_CONSTRUCT_ARRAY,
306
OPCODE_CONSTRUCT_TYPED_ARRAY,
307
OPCODE_CONSTRUCT_DICTIONARY,
308
OPCODE_CONSTRUCT_TYPED_DICTIONARY,
309
OPCODE_CALL,
310
OPCODE_CALL_RETURN,
311
OPCODE_CALL_ASYNC,
312
OPCODE_CALL_UTILITY,
313
OPCODE_CALL_UTILITY_VALIDATED,
314
OPCODE_CALL_GDSCRIPT_UTILITY,
315
OPCODE_CALL_BUILTIN_TYPE_VALIDATED,
316
OPCODE_CALL_SELF_BASE,
317
OPCODE_CALL_METHOD_BIND,
318
OPCODE_CALL_METHOD_BIND_RET,
319
OPCODE_CALL_BUILTIN_STATIC,
320
OPCODE_CALL_NATIVE_STATIC,
321
OPCODE_CALL_NATIVE_STATIC_VALIDATED_RETURN,
322
OPCODE_CALL_NATIVE_STATIC_VALIDATED_NO_RETURN,
323
OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN,
324
OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN,
325
OPCODE_AWAIT,
326
OPCODE_AWAIT_RESUME,
327
OPCODE_CREATE_LAMBDA,
328
OPCODE_CREATE_SELF_LAMBDA,
329
OPCODE_JUMP,
330
OPCODE_JUMP_IF,
331
OPCODE_JUMP_IF_NOT,
332
OPCODE_JUMP_TO_DEF_ARGUMENT,
333
OPCODE_JUMP_IF_SHARED,
334
OPCODE_RETURN,
335
OPCODE_RETURN_TYPED_BUILTIN,
336
OPCODE_RETURN_TYPED_ARRAY,
337
OPCODE_RETURN_TYPED_DICTIONARY,
338
OPCODE_RETURN_TYPED_NATIVE,
339
OPCODE_RETURN_TYPED_SCRIPT,
340
OPCODE_ITERATE_BEGIN,
341
OPCODE_ITERATE_BEGIN_INT,
342
OPCODE_ITERATE_BEGIN_FLOAT,
343
OPCODE_ITERATE_BEGIN_VECTOR2,
344
OPCODE_ITERATE_BEGIN_VECTOR2I,
345
OPCODE_ITERATE_BEGIN_VECTOR3,
346
OPCODE_ITERATE_BEGIN_VECTOR3I,
347
OPCODE_ITERATE_BEGIN_STRING,
348
OPCODE_ITERATE_BEGIN_DICTIONARY,
349
OPCODE_ITERATE_BEGIN_ARRAY,
350
OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY,
351
OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY,
352
OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY,
353
OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY,
354
OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY,
355
OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY,
356
OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY,
357
OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY,
358
OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY,
359
OPCODE_ITERATE_BEGIN_PACKED_VECTOR4_ARRAY,
360
OPCODE_ITERATE_BEGIN_OBJECT,
361
OPCODE_ITERATE_BEGIN_RANGE,
362
OPCODE_ITERATE,
363
OPCODE_ITERATE_INT,
364
OPCODE_ITERATE_FLOAT,
365
OPCODE_ITERATE_VECTOR2,
366
OPCODE_ITERATE_VECTOR2I,
367
OPCODE_ITERATE_VECTOR3,
368
OPCODE_ITERATE_VECTOR3I,
369
OPCODE_ITERATE_STRING,
370
OPCODE_ITERATE_DICTIONARY,
371
OPCODE_ITERATE_ARRAY,
372
OPCODE_ITERATE_PACKED_BYTE_ARRAY,
373
OPCODE_ITERATE_PACKED_INT32_ARRAY,
374
OPCODE_ITERATE_PACKED_INT64_ARRAY,
375
OPCODE_ITERATE_PACKED_FLOAT32_ARRAY,
376
OPCODE_ITERATE_PACKED_FLOAT64_ARRAY,
377
OPCODE_ITERATE_PACKED_STRING_ARRAY,
378
OPCODE_ITERATE_PACKED_VECTOR2_ARRAY,
379
OPCODE_ITERATE_PACKED_VECTOR3_ARRAY,
380
OPCODE_ITERATE_PACKED_COLOR_ARRAY,
381
OPCODE_ITERATE_PACKED_VECTOR4_ARRAY,
382
OPCODE_ITERATE_OBJECT,
383
OPCODE_ITERATE_RANGE,
384
OPCODE_STORE_GLOBAL,
385
OPCODE_STORE_NAMED_GLOBAL,
386
OPCODE_TYPE_ADJUST_BOOL,
387
OPCODE_TYPE_ADJUST_INT,
388
OPCODE_TYPE_ADJUST_FLOAT,
389
OPCODE_TYPE_ADJUST_STRING,
390
OPCODE_TYPE_ADJUST_VECTOR2,
391
OPCODE_TYPE_ADJUST_VECTOR2I,
392
OPCODE_TYPE_ADJUST_RECT2,
393
OPCODE_TYPE_ADJUST_RECT2I,
394
OPCODE_TYPE_ADJUST_VECTOR3,
395
OPCODE_TYPE_ADJUST_VECTOR3I,
396
OPCODE_TYPE_ADJUST_TRANSFORM2D,
397
OPCODE_TYPE_ADJUST_VECTOR4,
398
OPCODE_TYPE_ADJUST_VECTOR4I,
399
OPCODE_TYPE_ADJUST_PLANE,
400
OPCODE_TYPE_ADJUST_QUATERNION,
401
OPCODE_TYPE_ADJUST_AABB,
402
OPCODE_TYPE_ADJUST_BASIS,
403
OPCODE_TYPE_ADJUST_TRANSFORM3D,
404
OPCODE_TYPE_ADJUST_PROJECTION,
405
OPCODE_TYPE_ADJUST_COLOR,
406
OPCODE_TYPE_ADJUST_STRING_NAME,
407
OPCODE_TYPE_ADJUST_NODE_PATH,
408
OPCODE_TYPE_ADJUST_RID,
409
OPCODE_TYPE_ADJUST_OBJECT,
410
OPCODE_TYPE_ADJUST_CALLABLE,
411
OPCODE_TYPE_ADJUST_SIGNAL,
412
OPCODE_TYPE_ADJUST_DICTIONARY,
413
OPCODE_TYPE_ADJUST_ARRAY,
414
OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY,
415
OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY,
416
OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY,
417
OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY,
418
OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY,
419
OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY,
420
OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY,
421
OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY,
422
OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY,
423
OPCODE_TYPE_ADJUST_PACKED_VECTOR4_ARRAY,
424
OPCODE_ASSERT,
425
OPCODE_BREAKPOINT,
426
OPCODE_LINE,
427
OPCODE_END
428
};
429
430
enum Address {
431
ADDR_BITS = 24,
432
ADDR_MASK = ((1 << ADDR_BITS) - 1),
433
ADDR_TYPE_MASK = ~ADDR_MASK,
434
ADDR_TYPE_STACK = 0,
435
ADDR_TYPE_CONSTANT = 1,
436
ADDR_TYPE_MEMBER = 2,
437
ADDR_TYPE_MAX = 3,
438
};
439
440
enum FixedAddresses {
441
ADDR_STACK_SELF = 0,
442
ADDR_STACK_CLASS = 1,
443
ADDR_STACK_NIL = 2,
444
FIXED_ADDRESSES_MAX = 3,
445
ADDR_SELF = ADDR_STACK_SELF | (ADDR_TYPE_STACK << ADDR_BITS),
446
ADDR_CLASS = ADDR_STACK_CLASS | (ADDR_TYPE_STACK << ADDR_BITS),
447
ADDR_NIL = ADDR_STACK_NIL | (ADDR_TYPE_STACK << ADDR_BITS),
448
};
449
450
struct StackDebug {
451
int line;
452
int pos;
453
bool added;
454
StringName identifier;
455
};
456
457
private:
458
friend class GDScript;
459
friend class GDScriptCompiler;
460
friend class GDScriptByteCodeGenerator;
461
friend class GDScriptLanguage;
462
463
StringName name;
464
StringName source;
465
bool _static = false;
466
Vector<GDScriptDataType> argument_types;
467
GDScriptDataType return_type;
468
MethodInfo method_info;
469
Variant rpc_config;
470
471
GDScript *_script = nullptr;
472
int _initial_line = 0;
473
int _argument_count = 0;
474
int _vararg_index = -1;
475
int _stack_size = 0;
476
int _instruction_args_size = 0;
477
478
SelfList<GDScriptFunction> function_list{ this };
479
mutable Variant nil;
480
HashMap<int, Variant::Type> temporary_slots;
481
List<StackDebug> stack_debug;
482
483
Vector<int> code;
484
Vector<int> default_arguments;
485
Vector<Variant> constants;
486
Vector<StringName> global_names;
487
Vector<Variant::ValidatedOperatorEvaluator> operator_funcs;
488
Vector<Variant::ValidatedSetter> setters;
489
Vector<Variant::ValidatedGetter> getters;
490
Vector<Variant::ValidatedKeyedSetter> keyed_setters;
491
Vector<Variant::ValidatedKeyedGetter> keyed_getters;
492
Vector<Variant::ValidatedIndexedSetter> indexed_setters;
493
Vector<Variant::ValidatedIndexedGetter> indexed_getters;
494
Vector<Variant::ValidatedBuiltInMethod> builtin_methods;
495
Vector<Variant::ValidatedConstructor> constructors;
496
Vector<Variant::ValidatedUtilityFunction> utilities;
497
Vector<GDScriptUtilityFunctions::FunctionPtr> gds_utilities;
498
Vector<MethodBind *> methods;
499
Vector<GDScriptFunction *> lambdas;
500
501
int _code_size = 0;
502
int _default_arg_count = 0;
503
int _constant_count = 0;
504
int _global_names_count = 0;
505
int _operator_funcs_count = 0;
506
int _setters_count = 0;
507
int _getters_count = 0;
508
int _keyed_setters_count = 0;
509
int _keyed_getters_count = 0;
510
int _indexed_setters_count = 0;
511
int _indexed_getters_count = 0;
512
int _builtin_methods_count = 0;
513
int _constructors_count = 0;
514
int _utilities_count = 0;
515
int _gds_utilities_count = 0;
516
int _methods_count = 0;
517
int _lambdas_count = 0;
518
519
int *_code_ptr = nullptr;
520
const int *_default_arg_ptr = nullptr;
521
mutable Variant *_constants_ptr = nullptr;
522
const StringName *_global_names_ptr = nullptr;
523
const Variant::ValidatedOperatorEvaluator *_operator_funcs_ptr = nullptr;
524
const Variant::ValidatedSetter *_setters_ptr = nullptr;
525
const Variant::ValidatedGetter *_getters_ptr = nullptr;
526
const Variant::ValidatedKeyedSetter *_keyed_setters_ptr = nullptr;
527
const Variant::ValidatedKeyedGetter *_keyed_getters_ptr = nullptr;
528
const Variant::ValidatedIndexedSetter *_indexed_setters_ptr = nullptr;
529
const Variant::ValidatedIndexedGetter *_indexed_getters_ptr = nullptr;
530
const Variant::ValidatedBuiltInMethod *_builtin_methods_ptr = nullptr;
531
const Variant::ValidatedConstructor *_constructors_ptr = nullptr;
532
const Variant::ValidatedUtilityFunction *_utilities_ptr = nullptr;
533
const GDScriptUtilityFunctions::FunctionPtr *_gds_utilities_ptr = nullptr;
534
MethodBind **_methods_ptr = nullptr;
535
GDScriptFunction **_lambdas_ptr = nullptr;
536
537
#ifdef DEBUG_ENABLED
538
CharString func_cname;
539
const char *_func_cname = nullptr;
540
541
Vector<String> operator_names;
542
Vector<String> setter_names;
543
Vector<String> getter_names;
544
Vector<String> builtin_methods_names;
545
Vector<String> constructors_names;
546
Vector<String> utilities_names;
547
Vector<String> gds_utilities_names;
548
549
struct Profile {
550
StringName signature;
551
SafeNumeric<uint64_t> call_count;
552
SafeNumeric<uint64_t> self_time;
553
SafeNumeric<uint64_t> total_time;
554
SafeNumeric<uint64_t> frame_call_count;
555
SafeNumeric<uint64_t> frame_self_time;
556
SafeNumeric<uint64_t> frame_total_time;
557
uint64_t last_frame_call_count = 0;
558
uint64_t last_frame_self_time = 0;
559
uint64_t last_frame_total_time = 0;
560
typedef struct NativeProfile {
561
uint64_t call_count;
562
uint64_t total_time;
563
String signature;
564
} NativeProfile;
565
HashMap<String, NativeProfile> native_calls;
566
HashMap<String, NativeProfile> last_native_calls;
567
} profile;
568
#endif
569
570
String _get_call_error(const String &p_where, const Variant **p_argptrs, int p_argcount, const Variant &p_ret, const Callable::CallError &p_err) const;
571
String _get_callable_call_error(const String &p_where, const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Variant &p_ret, const Callable::CallError &p_err) const;
572
Variant _get_default_variant_for_data_type(const GDScriptDataType &p_data_type);
573
574
public:
575
static constexpr int MAX_CALL_DEPTH = 2048; // Limit to try to avoid crash because of a stack overflow.
576
577
struct CallState {
578
Signal completed;
579
GDScript *script = nullptr;
580
GDScriptInstance *instance = nullptr;
581
#ifdef DEBUG_ENABLED
582
StringName function_name;
583
String script_path;
584
#endif
585
Vector<uint8_t> stack;
586
int stack_size = 0;
587
int ip = 0;
588
int line = 0;
589
int defarg = 0;
590
Variant result;
591
};
592
593
_FORCE_INLINE_ StringName get_name() const { return name; }
594
_FORCE_INLINE_ StringName get_source() const { return source; }
595
_FORCE_INLINE_ GDScript *get_script() const { return _script; }
596
_FORCE_INLINE_ bool is_static() const { return _static; }
597
_FORCE_INLINE_ bool is_vararg() const { return _vararg_index >= 0; }
598
_FORCE_INLINE_ MethodInfo get_method_info() const { return method_info; }
599
_FORCE_INLINE_ int get_argument_count() const { return _argument_count; }
600
_FORCE_INLINE_ Variant get_rpc_config() const { return rpc_config; }
601
_FORCE_INLINE_ int get_max_stack_size() const { return _stack_size; }
602
603
Variant get_constant(int p_idx) const;
604
StringName get_global_name(int p_idx) const;
605
606
Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state = nullptr);
607
void debug_get_stack_member_state(int p_line, List<Pair<StringName, int>> *r_stackvars) const;
608
609
#ifdef DEBUG_ENABLED
610
void _profile_native_call(uint64_t p_t_taken, const String &p_function_name, const String &p_instance_class_name = String());
611
void disassemble(const Vector<String> &p_code_lines) const;
612
#endif
613
614
GDScriptFunction();
615
~GDScriptFunction();
616
};
617
618
class GDScriptFunctionState : public RefCounted {
619
GDCLASS(GDScriptFunctionState, RefCounted);
620
friend class GDScriptFunction;
621
GDScriptFunction *function = nullptr;
622
GDScriptFunction::CallState state;
623
Variant _signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
624
Ref<GDScriptFunctionState> first_state;
625
626
SelfList<GDScriptFunctionState> scripts_list;
627
SelfList<GDScriptFunctionState> instances_list;
628
629
protected:
630
static void _bind_methods();
631
632
public:
633
bool is_valid(bool p_extended_check = false) const;
634
Variant resume(const Variant &p_arg = Variant());
635
636
#ifdef DEBUG_ENABLED
637
// Returns a human-readable representation of the function.
638
String get_readable_function() {
639
return state.function_name;
640
}
641
#endif
642
643
void _clear_stack();
644
void _clear_connections();
645
646
GDScriptFunctionState();
647
~GDScriptFunctionState();
648
};
649
650