2017-03-05 22:47:28 +08:00
|
|
|
/**************************************************************************/
|
2017-11-17 01:38:18 +08:00
|
|
|
/* gdscript_function.h */
|
2017-03-05 22:47:28 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2017-11-17 01:38:18 +08:00
|
|
|
#ifndef GDSCRIPT_FUNCTION_H
|
|
|
|
#define GDSCRIPT_FUNCTION_H
|
2016-06-01 09:28:27 +08:00
|
|
|
|
2023-06-13 22:56:21 +08:00
|
|
|
#include "gdscript_utility_functions.h"
|
|
|
|
|
2021-06-05 00:03:15 +08:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/object/script_language.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/thread.h"
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/string/string_name.h"
|
|
|
|
#include "core/templates/pair.h"
|
|
|
|
#include "core/templates/self_list.h"
|
|
|
|
#include "core/variant/variant.h"
|
2016-06-01 09:28:27 +08:00
|
|
|
|
2017-11-17 01:38:18 +08:00
|
|
|
class GDScriptInstance;
|
2016-06-01 09:28:27 +08:00
|
|
|
class GDScript;
|
|
|
|
|
2021-03-09 23:32:35 +08:00
|
|
|
class GDScriptDataType {
|
|
|
|
public:
|
2023-09-15 02:31:07 +08:00
|
|
|
Vector<GDScriptDataType> container_element_types;
|
|
|
|
|
2020-05-12 23:01:17 +08:00
|
|
|
enum Kind {
|
2019-01-30 09:12:41 +08:00
|
|
|
UNINITIALIZED,
|
2018-05-30 10:16:54 +08:00
|
|
|
BUILTIN,
|
|
|
|
NATIVE,
|
|
|
|
SCRIPT,
|
2019-01-30 09:12:41 +08:00
|
|
|
GDSCRIPT,
|
2020-05-12 23:01:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Kind kind = UNINITIALIZED;
|
|
|
|
|
|
|
|
bool has_type = false;
|
|
|
|
Variant::Type builtin_type = Variant::NIL;
|
2018-05-30 10:16:54 +08:00
|
|
|
StringName native_type;
|
2020-09-10 07:26:07 +08:00
|
|
|
Script *script_type = nullptr;
|
|
|
|
Ref<Script> script_type_ref;
|
2018-05-30 10:16:54 +08:00
|
|
|
|
2019-03-04 19:25:59 +08:00
|
|
|
bool is_type(const Variant &p_variant, bool p_allow_implicit_conversion = false) const {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!has_type) {
|
2018-05-30 10:16:56 +08:00
|
|
|
return true; // Can't type check
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-05-30 10:16:56 +08:00
|
|
|
|
|
|
|
switch (kind) {
|
2019-01-30 09:12:41 +08:00
|
|
|
case UNINITIALIZED:
|
|
|
|
break;
|
2018-05-30 10:16:56 +08:00
|
|
|
case BUILTIN: {
|
|
|
|
Variant::Type var_type = p_variant.get_type();
|
2019-03-04 19:25:59 +08:00
|
|
|
bool valid = builtin_type == var_type;
|
2023-09-15 02:31:07 +08:00
|
|
|
if (valid && builtin_type == Variant::ARRAY && has_container_element_type(0)) {
|
2021-03-09 23:32:35 +08:00
|
|
|
Array array = p_variant;
|
|
|
|
if (array.is_typed()) {
|
2024-03-29 02:57:56 +08:00
|
|
|
const GDScriptDataType &elem_type = container_element_types[0];
|
2021-03-09 23:32:35 +08:00
|
|
|
Variant::Type array_builtin_type = (Variant::Type)array.get_typed_builtin();
|
|
|
|
StringName array_native_type = array.get_typed_class_name();
|
|
|
|
Ref<Script> array_script_type_ref = array.get_typed_script();
|
|
|
|
|
|
|
|
if (array_script_type_ref.is_valid()) {
|
2024-03-29 02:57:56 +08:00
|
|
|
valid = (elem_type.kind == SCRIPT || elem_type.kind == GDSCRIPT) && elem_type.script_type == array_script_type_ref.ptr();
|
2021-03-09 23:32:35 +08:00
|
|
|
} else if (array_native_type != StringName()) {
|
2024-03-29 02:57:56 +08:00
|
|
|
valid = elem_type.kind == NATIVE && elem_type.native_type == array_native_type;
|
2021-03-09 23:32:35 +08:00
|
|
|
} else {
|
2024-03-29 02:57:56 +08:00
|
|
|
valid = elem_type.kind == BUILTIN && elem_type.builtin_type == array_builtin_type;
|
2021-03-09 23:32:35 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
} else if (!valid && p_allow_implicit_conversion) {
|
2019-03-04 19:25:59 +08:00
|
|
|
valid = Variant::can_convert_strict(var_type, builtin_type);
|
|
|
|
}
|
|
|
|
return valid;
|
2018-05-30 10:16:56 +08:00
|
|
|
} break;
|
|
|
|
case NATIVE: {
|
|
|
|
if (p_variant.get_type() == Variant::NIL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (p_variant.get_type() != Variant::OBJECT) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-16 21:53:34 +08:00
|
|
|
|
2023-02-20 10:24:39 +08:00
|
|
|
bool was_freed = false;
|
|
|
|
Object *obj = p_variant.get_validated_object_with_check(was_freed);
|
2020-02-14 03:03:10 +08:00
|
|
|
if (!obj) {
|
2023-02-20 10:24:39 +08:00
|
|
|
return !was_freed;
|
2020-01-16 21:53:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ClassDB::is_parent_class(obj->get_class_name(), native_type)) {
|
2021-08-17 21:06:54 +08:00
|
|
|
return false;
|
2018-05-30 10:16:56 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} break;
|
|
|
|
case SCRIPT:
|
|
|
|
case GDSCRIPT: {
|
|
|
|
if (p_variant.get_type() == Variant::NIL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (p_variant.get_type() != Variant::OBJECT) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-16 21:53:34 +08:00
|
|
|
|
2023-02-20 10:24:39 +08:00
|
|
|
bool was_freed = false;
|
|
|
|
Object *obj = p_variant.get_validated_object_with_check(was_freed);
|
2020-02-14 03:03:10 +08:00
|
|
|
if (!obj) {
|
2023-02-20 10:24:39 +08:00
|
|
|
return !was_freed;
|
2020-01-16 21:53:34 +08:00
|
|
|
}
|
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
Ref<Script> base = obj && obj->get_script_instance() ? obj->get_script_instance()->get_script() : nullptr;
|
2018-05-30 10:16:56 +08:00
|
|
|
bool valid = false;
|
|
|
|
while (base.is_valid()) {
|
|
|
|
if (base == script_type) {
|
|
|
|
valid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
base = base->get_base_script();
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-29 02:57:56 +08:00
|
|
|
bool can_contain_object() const {
|
|
|
|
if (has_type && kind == BUILTIN) {
|
|
|
|
switch (builtin_type) {
|
|
|
|
case Variant::ARRAY:
|
|
|
|
if (has_container_element_type(0)) {
|
|
|
|
return container_element_types[0].can_contain_object();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case Variant::DICTIONARY:
|
|
|
|
case Variant::NIL:
|
|
|
|
case Variant::OBJECT:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-15 02:31:07 +08:00
|
|
|
void set_container_element_type(int p_index, const GDScriptDataType &p_element_type) {
|
|
|
|
ERR_FAIL_COND(p_index < 0);
|
|
|
|
while (p_index >= container_element_types.size()) {
|
|
|
|
container_element_types.push_back(GDScriptDataType());
|
|
|
|
}
|
|
|
|
container_element_types.write[p_index] = GDScriptDataType(p_element_type);
|
2021-03-09 23:32:35 +08:00
|
|
|
}
|
|
|
|
|
2023-09-15 02:31:07 +08:00
|
|
|
GDScriptDataType get_container_element_type(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, container_element_types.size(), GDScriptDataType());
|
|
|
|
return container_element_types[p_index];
|
2021-03-09 23:32:35 +08:00
|
|
|
}
|
|
|
|
|
2023-09-15 02:31:07 +08:00
|
|
|
GDScriptDataType get_container_element_type_or_variant(int p_index) const {
|
|
|
|
if (p_index < 0 || p_index >= container_element_types.size()) {
|
|
|
|
return GDScriptDataType();
|
|
|
|
}
|
|
|
|
return container_element_types[p_index];
|
2021-03-09 23:32:35 +08:00
|
|
|
}
|
|
|
|
|
2023-09-15 02:31:07 +08:00
|
|
|
bool has_container_element_type(int p_index) const {
|
|
|
|
return p_index >= 0 && p_index < container_element_types.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_container_element_types() const {
|
|
|
|
return !container_element_types.is_empty();
|
2021-03-09 23:32:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GDScriptDataType() = default;
|
|
|
|
|
2021-11-30 22:19:26 +08:00
|
|
|
void operator=(const GDScriptDataType &p_other) {
|
2021-03-09 23:32:35 +08:00
|
|
|
kind = p_other.kind;
|
|
|
|
has_type = p_other.has_type;
|
|
|
|
builtin_type = p_other.builtin_type;
|
|
|
|
native_type = p_other.native_type;
|
|
|
|
script_type = p_other.script_type;
|
|
|
|
script_type_ref = p_other.script_type_ref;
|
2023-09-15 02:31:07 +08:00
|
|
|
container_element_types = p_other.container_element_types;
|
2021-03-09 23:32:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GDScriptDataType(const GDScriptDataType &p_other) {
|
|
|
|
*this = p_other;
|
|
|
|
}
|
|
|
|
|
2023-09-15 02:31:07 +08:00
|
|
|
~GDScriptDataType() {}
|
2018-05-30 10:16:54 +08:00
|
|
|
};
|
|
|
|
|
2017-11-17 01:38:18 +08:00
|
|
|
class GDScriptFunction {
|
2016-06-01 09:28:27 +08:00
|
|
|
public:
|
|
|
|
enum Opcode {
|
|
|
|
OPCODE_OPERATOR,
|
2020-11-14 03:47:45 +08:00
|
|
|
OPCODE_OPERATOR_VALIDATED,
|
2023-02-17 07:16:24 +08:00
|
|
|
OPCODE_TYPE_TEST_BUILTIN,
|
|
|
|
OPCODE_TYPE_TEST_ARRAY,
|
|
|
|
OPCODE_TYPE_TEST_NATIVE,
|
|
|
|
OPCODE_TYPE_TEST_SCRIPT,
|
2020-11-16 23:59:53 +08:00
|
|
|
OPCODE_SET_KEYED,
|
|
|
|
OPCODE_SET_KEYED_VALIDATED,
|
|
|
|
OPCODE_SET_INDEXED_VALIDATED,
|
|
|
|
OPCODE_GET_KEYED,
|
|
|
|
OPCODE_GET_KEYED_VALIDATED,
|
|
|
|
OPCODE_GET_INDEXED_VALIDATED,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_SET_NAMED,
|
2020-11-16 23:59:53 +08:00
|
|
|
OPCODE_SET_NAMED_VALIDATED,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_GET_NAMED,
|
2020-11-16 23:59:53 +08:00
|
|
|
OPCODE_GET_NAMED_VALIDATED,
|
2017-01-05 04:37:45 +08:00
|
|
|
OPCODE_SET_MEMBER,
|
|
|
|
OPCODE_GET_MEMBER,
|
2023-05-16 18:03:53 +08:00
|
|
|
OPCODE_SET_STATIC_VARIABLE, // Only for GDScript.
|
|
|
|
OPCODE_GET_STATIC_VARIABLE, // Only for GDScript.
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_ASSIGN,
|
2024-03-29 02:57:56 +08:00
|
|
|
OPCODE_ASSIGN_NULL,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_ASSIGN_TRUE,
|
|
|
|
OPCODE_ASSIGN_FALSE,
|
2018-05-30 10:16:56 +08:00
|
|
|
OPCODE_ASSIGN_TYPED_BUILTIN,
|
2021-03-09 23:32:35 +08:00
|
|
|
OPCODE_ASSIGN_TYPED_ARRAY,
|
2018-05-30 10:16:56 +08:00
|
|
|
OPCODE_ASSIGN_TYPED_NATIVE,
|
|
|
|
OPCODE_ASSIGN_TYPED_SCRIPT,
|
|
|
|
OPCODE_CAST_TO_BUILTIN,
|
|
|
|
OPCODE_CAST_TO_NATIVE,
|
|
|
|
OPCODE_CAST_TO_SCRIPT,
|
2020-11-18 22:37:08 +08:00
|
|
|
OPCODE_CONSTRUCT, // Only for basic types!
|
|
|
|
OPCODE_CONSTRUCT_VALIDATED, // Only for basic types!
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_CONSTRUCT_ARRAY,
|
2021-03-09 23:32:35 +08:00
|
|
|
OPCODE_CONSTRUCT_TYPED_ARRAY,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_CONSTRUCT_DICTIONARY,
|
|
|
|
OPCODE_CALL,
|
|
|
|
OPCODE_CALL_RETURN,
|
2020-05-02 06:14:56 +08:00
|
|
|
OPCODE_CALL_ASYNC,
|
2020-11-26 22:56:32 +08:00
|
|
|
OPCODE_CALL_UTILITY,
|
|
|
|
OPCODE_CALL_UTILITY_VALIDATED,
|
|
|
|
OPCODE_CALL_GDSCRIPT_UTILITY,
|
2020-11-18 21:32:28 +08:00
|
|
|
OPCODE_CALL_BUILTIN_TYPE_VALIDATED,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_CALL_SELF_BASE,
|
2020-11-17 21:44:52 +08:00
|
|
|
OPCODE_CALL_METHOD_BIND,
|
|
|
|
OPCODE_CALL_METHOD_BIND_RET,
|
2021-05-16 22:48:53 +08:00
|
|
|
OPCODE_CALL_BUILTIN_STATIC,
|
2022-04-07 01:14:38 +08:00
|
|
|
OPCODE_CALL_NATIVE_STATIC,
|
2024-04-26 08:05:53 +08:00
|
|
|
OPCODE_CALL_NATIVE_STATIC_VALIDATED_RETURN,
|
|
|
|
OPCODE_CALL_NATIVE_STATIC_VALIDATED_NO_RETURN,
|
2023-07-25 23:42:07 +08:00
|
|
|
OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN,
|
|
|
|
OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN,
|
2020-05-02 06:14:56 +08:00
|
|
|
OPCODE_AWAIT,
|
|
|
|
OPCODE_AWAIT_RESUME,
|
2021-03-28 22:03:13 +08:00
|
|
|
OPCODE_CREATE_LAMBDA,
|
2022-04-21 01:22:22 +08:00
|
|
|
OPCODE_CREATE_SELF_LAMBDA,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_JUMP,
|
|
|
|
OPCODE_JUMP_IF,
|
|
|
|
OPCODE_JUMP_IF_NOT,
|
|
|
|
OPCODE_JUMP_TO_DEF_ARGUMENT,
|
2022-06-27 23:09:51 +08:00
|
|
|
OPCODE_JUMP_IF_SHARED,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_RETURN,
|
2021-04-02 21:34:44 +08:00
|
|
|
OPCODE_RETURN_TYPED_BUILTIN,
|
|
|
|
OPCODE_RETURN_TYPED_ARRAY,
|
|
|
|
OPCODE_RETURN_TYPED_NATIVE,
|
|
|
|
OPCODE_RETURN_TYPED_SCRIPT,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_ITERATE_BEGIN,
|
2020-10-07 20:36:48 +08:00
|
|
|
OPCODE_ITERATE_BEGIN_INT,
|
|
|
|
OPCODE_ITERATE_BEGIN_FLOAT,
|
|
|
|
OPCODE_ITERATE_BEGIN_VECTOR2,
|
|
|
|
OPCODE_ITERATE_BEGIN_VECTOR2I,
|
|
|
|
OPCODE_ITERATE_BEGIN_VECTOR3,
|
|
|
|
OPCODE_ITERATE_BEGIN_VECTOR3I,
|
|
|
|
OPCODE_ITERATE_BEGIN_STRING,
|
|
|
|
OPCODE_ITERATE_BEGIN_DICTIONARY,
|
|
|
|
OPCODE_ITERATE_BEGIN_ARRAY,
|
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY,
|
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY,
|
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY,
|
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY,
|
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY,
|
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY,
|
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY,
|
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY,
|
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY,
|
2024-04-08 22:51:34 +08:00
|
|
|
OPCODE_ITERATE_BEGIN_PACKED_VECTOR4_ARRAY,
|
2020-10-07 20:36:48 +08:00
|
|
|
OPCODE_ITERATE_BEGIN_OBJECT,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_ITERATE,
|
2020-10-07 20:36:48 +08:00
|
|
|
OPCODE_ITERATE_INT,
|
|
|
|
OPCODE_ITERATE_FLOAT,
|
|
|
|
OPCODE_ITERATE_VECTOR2,
|
|
|
|
OPCODE_ITERATE_VECTOR2I,
|
|
|
|
OPCODE_ITERATE_VECTOR3,
|
|
|
|
OPCODE_ITERATE_VECTOR3I,
|
|
|
|
OPCODE_ITERATE_STRING,
|
|
|
|
OPCODE_ITERATE_DICTIONARY,
|
|
|
|
OPCODE_ITERATE_ARRAY,
|
|
|
|
OPCODE_ITERATE_PACKED_BYTE_ARRAY,
|
|
|
|
OPCODE_ITERATE_PACKED_INT32_ARRAY,
|
|
|
|
OPCODE_ITERATE_PACKED_INT64_ARRAY,
|
|
|
|
OPCODE_ITERATE_PACKED_FLOAT32_ARRAY,
|
|
|
|
OPCODE_ITERATE_PACKED_FLOAT64_ARRAY,
|
|
|
|
OPCODE_ITERATE_PACKED_STRING_ARRAY,
|
|
|
|
OPCODE_ITERATE_PACKED_VECTOR2_ARRAY,
|
|
|
|
OPCODE_ITERATE_PACKED_VECTOR3_ARRAY,
|
|
|
|
OPCODE_ITERATE_PACKED_COLOR_ARRAY,
|
2024-04-08 22:51:34 +08:00
|
|
|
OPCODE_ITERATE_PACKED_VECTOR4_ARRAY,
|
2020-10-07 20:36:48 +08:00
|
|
|
OPCODE_ITERATE_OBJECT,
|
2021-09-02 03:01:39 +08:00
|
|
|
OPCODE_STORE_GLOBAL,
|
2021-04-08 22:55:24 +08:00
|
|
|
OPCODE_STORE_NAMED_GLOBAL,
|
2021-04-16 23:04:08 +08:00
|
|
|
OPCODE_TYPE_ADJUST_BOOL,
|
|
|
|
OPCODE_TYPE_ADJUST_INT,
|
|
|
|
OPCODE_TYPE_ADJUST_FLOAT,
|
|
|
|
OPCODE_TYPE_ADJUST_STRING,
|
|
|
|
OPCODE_TYPE_ADJUST_VECTOR2,
|
|
|
|
OPCODE_TYPE_ADJUST_VECTOR2I,
|
|
|
|
OPCODE_TYPE_ADJUST_RECT2,
|
|
|
|
OPCODE_TYPE_ADJUST_RECT2I,
|
|
|
|
OPCODE_TYPE_ADJUST_VECTOR3,
|
|
|
|
OPCODE_TYPE_ADJUST_VECTOR3I,
|
|
|
|
OPCODE_TYPE_ADJUST_TRANSFORM2D,
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-20 07:11:13 +08:00
|
|
|
OPCODE_TYPE_ADJUST_VECTOR4,
|
|
|
|
OPCODE_TYPE_ADJUST_VECTOR4I,
|
2021-04-16 23:04:08 +08:00
|
|
|
OPCODE_TYPE_ADJUST_PLANE,
|
2021-01-20 15:02:02 +08:00
|
|
|
OPCODE_TYPE_ADJUST_QUATERNION,
|
2021-04-16 23:04:08 +08:00
|
|
|
OPCODE_TYPE_ADJUST_AABB,
|
|
|
|
OPCODE_TYPE_ADJUST_BASIS,
|
2022-04-08 08:47:11 +08:00
|
|
|
OPCODE_TYPE_ADJUST_TRANSFORM3D,
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-20 07:11:13 +08:00
|
|
|
OPCODE_TYPE_ADJUST_PROJECTION,
|
2021-04-16 23:04:08 +08:00
|
|
|
OPCODE_TYPE_ADJUST_COLOR,
|
|
|
|
OPCODE_TYPE_ADJUST_STRING_NAME,
|
|
|
|
OPCODE_TYPE_ADJUST_NODE_PATH,
|
|
|
|
OPCODE_TYPE_ADJUST_RID,
|
|
|
|
OPCODE_TYPE_ADJUST_OBJECT,
|
|
|
|
OPCODE_TYPE_ADJUST_CALLABLE,
|
|
|
|
OPCODE_TYPE_ADJUST_SIGNAL,
|
|
|
|
OPCODE_TYPE_ADJUST_DICTIONARY,
|
|
|
|
OPCODE_TYPE_ADJUST_ARRAY,
|
|
|
|
OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY,
|
|
|
|
OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY,
|
|
|
|
OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY,
|
|
|
|
OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY,
|
|
|
|
OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY,
|
|
|
|
OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY,
|
|
|
|
OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY,
|
|
|
|
OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY,
|
|
|
|
OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY,
|
2024-04-08 22:51:34 +08:00
|
|
|
OPCODE_TYPE_ADJUST_PACKED_VECTOR4_ARRAY,
|
2016-06-01 09:28:27 +08:00
|
|
|
OPCODE_ASSERT,
|
|
|
|
OPCODE_BREAKPOINT,
|
|
|
|
OPCODE_LINE,
|
|
|
|
OPCODE_END
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Address {
|
|
|
|
ADDR_BITS = 24,
|
|
|
|
ADDR_MASK = ((1 << ADDR_BITS) - 1),
|
|
|
|
ADDR_TYPE_MASK = ~ADDR_MASK,
|
2021-04-08 22:55:24 +08:00
|
|
|
ADDR_TYPE_STACK = 0,
|
|
|
|
ADDR_TYPE_CONSTANT = 1,
|
2016-06-01 09:28:27 +08:00
|
|
|
ADDR_TYPE_MEMBER = 2,
|
2023-05-16 18:03:53 +08:00
|
|
|
ADDR_TYPE_MAX = 3,
|
2021-04-08 22:55:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum FixedAddresses {
|
|
|
|
ADDR_STACK_SELF = 0,
|
|
|
|
ADDR_STACK_CLASS = 1,
|
|
|
|
ADDR_STACK_NIL = 2,
|
2023-05-16 18:03:53 +08:00
|
|
|
FIXED_ADDRESSES_MAX = 3,
|
2021-04-08 22:55:24 +08:00
|
|
|
ADDR_SELF = ADDR_STACK_SELF | (ADDR_TYPE_STACK << ADDR_BITS),
|
|
|
|
ADDR_CLASS = ADDR_STACK_CLASS | (ADDR_TYPE_STACK << ADDR_BITS),
|
|
|
|
ADDR_NIL = ADDR_STACK_NIL | (ADDR_TYPE_STACK << ADDR_BITS),
|
2016-06-01 09:28:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct StackDebug {
|
|
|
|
int line;
|
|
|
|
int pos;
|
|
|
|
bool added;
|
|
|
|
StringName identifier;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2022-10-10 00:41:28 +08:00
|
|
|
friend class GDScript;
|
2017-11-17 01:38:18 +08:00
|
|
|
friend class GDScriptCompiler;
|
2020-07-29 02:17:49 +08:00
|
|
|
friend class GDScriptByteCodeGenerator;
|
2023-08-29 00:20:10 +08:00
|
|
|
friend class GDScriptLanguage;
|
2016-06-01 09:28:27 +08:00
|
|
|
|
2023-08-29 00:20:10 +08:00
|
|
|
StringName name;
|
2016-06-01 09:28:27 +08:00
|
|
|
StringName source;
|
2023-08-29 00:20:10 +08:00
|
|
|
bool _static = false;
|
|
|
|
Vector<GDScriptDataType> argument_types;
|
|
|
|
GDScriptDataType return_type;
|
|
|
|
MethodInfo method_info;
|
|
|
|
Variant rpc_config;
|
2016-06-01 09:28:27 +08:00
|
|
|
|
2023-08-29 00:20:10 +08:00
|
|
|
GDScript *_script = nullptr;
|
|
|
|
int _initial_line = 0;
|
2020-11-13 21:31:14 +08:00
|
|
|
int _argument_count = 0;
|
|
|
|
int _stack_size = 0;
|
|
|
|
int _instruction_args_size = 0;
|
2020-11-17 21:44:52 +08:00
|
|
|
|
2023-08-29 00:20:10 +08:00
|
|
|
SelfList<GDScriptFunction> function_list{ this };
|
|
|
|
mutable Variant nil;
|
|
|
|
HashMap<int, Variant::Type> temporary_slots;
|
|
|
|
List<StackDebug> stack_debug;
|
2016-06-01 09:28:27 +08:00
|
|
|
|
2023-08-29 00:20:10 +08:00
|
|
|
Vector<int> code;
|
|
|
|
Vector<int> default_arguments;
|
2016-06-01 09:28:27 +08:00
|
|
|
Vector<Variant> constants;
|
|
|
|
Vector<StringName> global_names;
|
2020-11-14 03:47:45 +08:00
|
|
|
Vector<Variant::ValidatedOperatorEvaluator> operator_funcs;
|
2020-11-16 23:59:53 +08:00
|
|
|
Vector<Variant::ValidatedSetter> setters;
|
|
|
|
Vector<Variant::ValidatedGetter> getters;
|
|
|
|
Vector<Variant::ValidatedKeyedSetter> keyed_setters;
|
|
|
|
Vector<Variant::ValidatedKeyedGetter> keyed_getters;
|
|
|
|
Vector<Variant::ValidatedIndexedSetter> indexed_setters;
|
|
|
|
Vector<Variant::ValidatedIndexedGetter> indexed_getters;
|
2020-11-18 21:32:28 +08:00
|
|
|
Vector<Variant::ValidatedBuiltInMethod> builtin_methods;
|
2020-11-18 22:37:08 +08:00
|
|
|
Vector<Variant::ValidatedConstructor> constructors;
|
2020-11-26 22:56:32 +08:00
|
|
|
Vector<Variant::ValidatedUtilityFunction> utilities;
|
|
|
|
Vector<GDScriptUtilityFunctions::FunctionPtr> gds_utilities;
|
2020-11-17 21:44:52 +08:00
|
|
|
Vector<MethodBind *> methods;
|
2021-03-28 22:03:13 +08:00
|
|
|
Vector<GDScriptFunction *> lambdas;
|
2016-06-01 09:28:27 +08:00
|
|
|
|
2023-08-29 00:20:10 +08:00
|
|
|
int _code_size = 0;
|
|
|
|
int _default_arg_count = 0;
|
|
|
|
int _constant_count = 0;
|
|
|
|
int _global_names_count = 0;
|
|
|
|
int _operator_funcs_count = 0;
|
|
|
|
int _setters_count = 0;
|
|
|
|
int _getters_count = 0;
|
|
|
|
int _keyed_setters_count = 0;
|
|
|
|
int _keyed_getters_count = 0;
|
|
|
|
int _indexed_setters_count = 0;
|
|
|
|
int _indexed_getters_count = 0;
|
|
|
|
int _builtin_methods_count = 0;
|
|
|
|
int _constructors_count = 0;
|
|
|
|
int _utilities_count = 0;
|
|
|
|
int _gds_utilities_count = 0;
|
|
|
|
int _methods_count = 0;
|
|
|
|
int _lambdas_count = 0;
|
2021-05-17 21:59:43 +08:00
|
|
|
|
2023-08-29 00:20:10 +08:00
|
|
|
int *_code_ptr = nullptr;
|
|
|
|
const int *_default_arg_ptr = nullptr;
|
|
|
|
mutable Variant *_constants_ptr = nullptr;
|
|
|
|
const StringName *_global_names_ptr = nullptr;
|
|
|
|
const Variant::ValidatedOperatorEvaluator *_operator_funcs_ptr = nullptr;
|
|
|
|
const Variant::ValidatedSetter *_setters_ptr = nullptr;
|
|
|
|
const Variant::ValidatedGetter *_getters_ptr = nullptr;
|
|
|
|
const Variant::ValidatedKeyedSetter *_keyed_setters_ptr = nullptr;
|
|
|
|
const Variant::ValidatedKeyedGetter *_keyed_getters_ptr = nullptr;
|
|
|
|
const Variant::ValidatedIndexedSetter *_indexed_setters_ptr = nullptr;
|
|
|
|
const Variant::ValidatedIndexedGetter *_indexed_getters_ptr = nullptr;
|
|
|
|
const Variant::ValidatedBuiltInMethod *_builtin_methods_ptr = nullptr;
|
|
|
|
const Variant::ValidatedConstructor *_constructors_ptr = nullptr;
|
|
|
|
const Variant::ValidatedUtilityFunction *_utilities_ptr = nullptr;
|
|
|
|
const GDScriptUtilityFunctions::FunctionPtr *_gds_utilities_ptr = nullptr;
|
|
|
|
MethodBind **_methods_ptr = nullptr;
|
|
|
|
GDScriptFunction **_lambdas_ptr = nullptr;
|
2016-06-01 09:28:27 +08:00
|
|
|
|
2022-12-29 21:47:53 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
2023-08-29 00:20:10 +08:00
|
|
|
CharString func_cname;
|
|
|
|
const char *_func_cname = nullptr;
|
|
|
|
|
2022-12-29 21:47:53 +08:00
|
|
|
Vector<String> operator_names;
|
|
|
|
Vector<String> setter_names;
|
|
|
|
Vector<String> getter_names;
|
|
|
|
Vector<String> builtin_methods_names;
|
|
|
|
Vector<String> constructors_names;
|
|
|
|
Vector<String> utilities_names;
|
|
|
|
Vector<String> gds_utilities_names;
|
2016-06-01 09:28:27 +08:00
|
|
|
|
|
|
|
struct Profile {
|
|
|
|
StringName signature;
|
2023-04-29 23:20:38 +08:00
|
|
|
SafeNumeric<uint64_t> call_count;
|
|
|
|
SafeNumeric<uint64_t> self_time;
|
|
|
|
SafeNumeric<uint64_t> total_time;
|
|
|
|
SafeNumeric<uint64_t> frame_call_count;
|
|
|
|
SafeNumeric<uint64_t> frame_self_time;
|
|
|
|
SafeNumeric<uint64_t> frame_total_time;
|
2020-11-13 21:31:14 +08:00
|
|
|
uint64_t last_frame_call_count = 0;
|
|
|
|
uint64_t last_frame_self_time = 0;
|
|
|
|
uint64_t last_frame_total_time = 0;
|
2023-03-05 20:37:11 +08:00
|
|
|
typedef struct NativeProfile {
|
|
|
|
uint64_t call_count;
|
|
|
|
uint64_t total_time;
|
|
|
|
String signature;
|
|
|
|
} NativeProfile;
|
|
|
|
HashMap<String, NativeProfile> native_calls;
|
|
|
|
HashMap<String, NativeProfile> last_native_calls;
|
2016-06-01 09:28:27 +08:00
|
|
|
} profile;
|
|
|
|
#endif
|
|
|
|
|
2023-08-29 00:20:10 +08:00
|
|
|
_FORCE_INLINE_ String _get_call_error(const Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const;
|
|
|
|
Variant _get_default_variant_for_data_type(const GDScriptDataType &p_data_type);
|
|
|
|
|
2016-06-01 09:28:27 +08:00
|
|
|
public:
|
2023-02-08 04:09:40 +08:00
|
|
|
static constexpr int MAX_CALL_DEPTH = 2048; // Limit to try to avoid crash because of a stack overflow.
|
|
|
|
|
2016-06-01 09:28:27 +08:00
|
|
|
struct CallState {
|
2021-02-08 17:57:18 +08:00
|
|
|
GDScript *script = nullptr;
|
|
|
|
GDScriptInstance *instance = nullptr;
|
2020-04-29 19:38:00 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-05-05 18:53:05 +08:00
|
|
|
StringName function_name;
|
2020-04-29 19:38:00 +08:00
|
|
|
String script_path;
|
|
|
|
#endif
|
2016-06-01 09:28:27 +08:00
|
|
|
Vector<uint8_t> stack;
|
2021-02-08 17:57:18 +08:00
|
|
|
int stack_size = 0;
|
|
|
|
uint32_t alloca_size = 0;
|
|
|
|
int ip = 0;
|
|
|
|
int line = 0;
|
|
|
|
int defarg = 0;
|
2016-06-01 09:28:27 +08:00
|
|
|
Variant result;
|
|
|
|
};
|
|
|
|
|
2023-08-29 00:20:10 +08:00
|
|
|
_FORCE_INLINE_ StringName get_name() const { return name; }
|
|
|
|
_FORCE_INLINE_ StringName get_source() const { return source; }
|
|
|
|
_FORCE_INLINE_ GDScript *get_script() const { return _script; }
|
2016-06-01 09:28:27 +08:00
|
|
|
_FORCE_INLINE_ bool is_static() const { return _static; }
|
2023-08-29 00:20:10 +08:00
|
|
|
_FORCE_INLINE_ MethodInfo get_method_info() const { return method_info; }
|
2024-01-28 22:16:09 +08:00
|
|
|
_FORCE_INLINE_ int get_argument_count() const { return _argument_count; }
|
2023-08-29 00:20:10 +08:00
|
|
|
_FORCE_INLINE_ Variant get_rpc_config() const { return rpc_config; }
|
|
|
|
_FORCE_INLINE_ int get_max_stack_size() const { return _stack_size; }
|
2016-06-01 09:28:27 +08:00
|
|
|
|
|
|
|
Variant get_constant(int p_idx) const;
|
|
|
|
StringName get_global_name(int p_idx) const;
|
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state = nullptr);
|
2023-08-29 00:20:10 +08:00
|
|
|
void debug_get_stack_member_state(int p_line, List<Pair<StringName, int>> *r_stackvars) const;
|
2016-06-01 09:28:27 +08:00
|
|
|
|
2020-08-17 21:00:42 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
2023-03-05 20:37:11 +08:00
|
|
|
void _profile_native_call(uint64_t p_t_taken, const String &p_function_name, const String &p_instance_class_name = String());
|
2020-08-17 21:00:42 +08:00
|
|
|
void disassemble(const Vector<String> &p_code_lines) const;
|
|
|
|
#endif
|
|
|
|
|
2017-11-17 01:38:18 +08:00
|
|
|
GDScriptFunction();
|
|
|
|
~GDScriptFunction();
|
2016-06-01 09:28:27 +08:00
|
|
|
};
|
|
|
|
|
2021-06-05 00:03:15 +08:00
|
|
|
class GDScriptFunctionState : public RefCounted {
|
|
|
|
GDCLASS(GDScriptFunctionState, RefCounted);
|
2017-11-17 01:38:18 +08:00
|
|
|
friend class GDScriptFunction;
|
2021-02-08 17:57:18 +08:00
|
|
|
GDScriptFunction *function = nullptr;
|
2017-11-17 01:38:18 +08:00
|
|
|
GDScriptFunction::CallState state;
|
2020-02-20 03:27:19 +08:00
|
|
|
Variant _signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
|
2018-06-28 23:40:11 +08:00
|
|
|
Ref<GDScriptFunctionState> first_state;
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2020-05-05 18:53:05 +08:00
|
|
|
SelfList<GDScriptFunctionState> scripts_list;
|
|
|
|
SelfList<GDScriptFunctionState> instances_list;
|
|
|
|
|
2016-06-01 09:28:27 +08:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
public:
|
2017-05-17 20:47:17 +08:00
|
|
|
bool is_valid(bool p_extended_check = false) const;
|
2016-06-01 09:28:27 +08:00
|
|
|
Variant resume(const Variant &p_arg = Variant());
|
2020-05-05 18:53:05 +08:00
|
|
|
|
|
|
|
void _clear_stack();
|
2022-09-16 23:42:06 +08:00
|
|
|
void _clear_connections();
|
2020-05-05 18:53:05 +08:00
|
|
|
|
2017-11-17 01:38:18 +08:00
|
|
|
GDScriptFunctionState();
|
|
|
|
~GDScriptFunctionState();
|
2016-06-01 09:28:27 +08:00
|
|
|
};
|
|
|
|
|
2017-11-17 01:38:18 +08:00
|
|
|
#endif // GDSCRIPT_FUNCTION_H
|