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