Merge pull request #43446 from reduz/create-variant-builtin-funcs

Create Variant built-in functions.
This commit is contained in:
Rémi Verschelde 2020-11-11 00:25:41 +01:00 committed by GitHub
commit 88d463cabc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 1574 additions and 822 deletions

View file

@ -455,3 +455,35 @@ Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int &
return err;
}
Error JSONParser::parse_string(const String &p_json_string) {
return JSON::parse(p_json_string, data, err_text, err_line);
}
String JSONParser::get_error_text() const {
return err_text;
}
int JSONParser::get_error_line() const {
return err_line;
}
Variant JSONParser::get_data() const {
return data;
}
Error JSONParser::decode_data(const Variant &p_data, const String &p_indent, bool p_sort_keys) {
string = JSON::print(p_data, p_indent, p_sort_keys);
data = p_data;
return OK;
}
String JSONParser::get_string() const {
return string;
}
void JSONParser::_bind_methods() {
ClassDB::bind_method(D_METHOD("parse_string", "json_string"), &JSONParser::parse_string);
ClassDB::bind_method(D_METHOD("get_error_text"), &JSONParser::get_error_text);
ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParser::get_error_line);
ClassDB::bind_method(D_METHOD("get_data"), &JSONParser::get_data);
ClassDB::bind_method(D_METHOD("decode_data", "data", "indent", "sort_keys"), &JSONParser::decode_data, DEFVAL(""), DEFVAL(true));
ClassDB::bind_method(D_METHOD("get_string"), &JSONParser::get_string);
}

View file

@ -31,8 +31,8 @@
#ifndef JSON_H
#define JSON_H
#include "core/object/reference.h"
#include "core/variant/variant.h"
class JSON {
enum TokenType {
TK_CURLY_BRACKET_OPEN,
@ -75,4 +75,25 @@ public:
static Error parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line);
};
class JSONParser : public Reference {
GDCLASS(JSONParser, Reference);
Variant data;
String string;
String err_text;
int err_line = 0;
protected:
static void _bind_methods();
public:
Error parse_string(const String &p_json_string);
String get_error_text() const;
int get_error_line() const;
Variant get_data() const;
Error decode_data(const Variant &p_data, const String &p_indent = "", bool p_sort_keys = true);
String get_string() const;
};
#endif // JSON_H

View file

@ -37,688 +37,6 @@
#include "core/os/os.h"
#include "core/variant/variant_parser.h"
const char *Expression::func_name[Expression::FUNC_MAX] = {
"sin",
"cos",
"tan",
"sinh",
"cosh",
"tanh",
"asin",
"acos",
"atan",
"atan2",
"sqrt",
"fmod",
"fposmod",
"posmod",
"floor",
"ceil",
"round",
"abs",
"sign",
"pow",
"log",
"exp",
"is_nan",
"is_inf",
"ease",
"step_decimals",
"stepify",
"lerp",
"lerp_angle",
"inverse_lerp",
"range_lerp",
"smoothstep",
"move_toward",
"dectime",
"randomize",
"randi",
"randf",
"randf_range",
"randi_range",
"seed",
"rand_seed",
"deg2rad",
"rad2deg",
"linear2db",
"db2linear",
"polar2cartesian",
"cartesian2polar",
"wrapi",
"wrapf",
"max",
"min",
"clamp",
"nearest_po2",
"weakref",
"convert",
"typeof",
"type_exists",
"char",
"ord",
"str",
"print",
"printerr",
"printraw",
"var2str",
"str2var",
"var2bytes",
"bytes2var",
"color_named",
};
Expression::BuiltinFunc Expression::find_function(const String &p_string) {
for (int i = 0; i < FUNC_MAX; i++) {
if (p_string == func_name[i]) {
return BuiltinFunc(i);
}
}
return FUNC_MAX;
}
String Expression::get_func_name(BuiltinFunc p_func) {
ERR_FAIL_INDEX_V(p_func, FUNC_MAX, String());
return func_name[p_func];
}
int Expression::get_func_argument_count(BuiltinFunc p_func) {
switch (p_func) {
case MATH_RANDOMIZE:
case MATH_RANDI:
case MATH_RANDF:
return 0;
case MATH_SIN:
case MATH_COS:
case MATH_TAN:
case MATH_SINH:
case MATH_COSH:
case MATH_TANH:
case MATH_ASIN:
case MATH_ACOS:
case MATH_ATAN:
case MATH_SQRT:
case MATH_FLOOR:
case MATH_CEIL:
case MATH_ROUND:
case MATH_ABS:
case MATH_SIGN:
case MATH_LOG:
case MATH_EXP:
case MATH_ISNAN:
case MATH_ISINF:
case MATH_STEP_DECIMALS:
case MATH_SEED:
case MATH_RANDSEED:
case MATH_DEG2RAD:
case MATH_RAD2DEG:
case MATH_LINEAR2DB:
case MATH_DB2LINEAR:
case LOGIC_NEAREST_PO2:
case OBJ_WEAKREF:
case TYPE_OF:
case TEXT_CHAR:
case TEXT_ORD:
case TEXT_STR:
case TEXT_PRINT:
case TEXT_PRINTERR:
case TEXT_PRINTRAW:
case VAR_TO_STR:
case STR_TO_VAR:
case TYPE_EXISTS:
return 1;
case VAR_TO_BYTES:
case BYTES_TO_VAR:
case MATH_ATAN2:
case MATH_FMOD:
case MATH_FPOSMOD:
case MATH_POSMOD:
case MATH_POW:
case MATH_EASE:
case MATH_STEPIFY:
case MATH_RANDF_RANGE:
case MATH_RANDI_RANGE:
case MATH_POLAR2CARTESIAN:
case MATH_CARTESIAN2POLAR:
case LOGIC_MAX:
case LOGIC_MIN:
case TYPE_CONVERT:
case COLORN:
return 2;
case MATH_LERP:
case MATH_LERP_ANGLE:
case MATH_INVERSE_LERP:
case MATH_SMOOTHSTEP:
case MATH_MOVE_TOWARD:
case MATH_DECTIME:
case MATH_WRAP:
case MATH_WRAPF:
case LOGIC_CLAMP:
return 3;
case MATH_RANGE_LERP:
return 5;
case FUNC_MAX: {
}
}
return 0;
}
#define VALIDATE_ARG_NUM(m_arg) \
if (!p_inputs[m_arg]->is_num()) { \
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \
r_error.argument = m_arg; \
r_error.expected = Variant::FLOAT; \
return; \
}
void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Callable::CallError &r_error, String &r_error_str) {
r_error.error = Callable::CallError::CALL_OK;
switch (p_func) {
case MATH_SIN: {
VALIDATE_ARG_NUM(0);
*r_return = Math::sin((double)*p_inputs[0]);
} break;
case MATH_COS: {
VALIDATE_ARG_NUM(0);
*r_return = Math::cos((double)*p_inputs[0]);
} break;
case MATH_TAN: {
VALIDATE_ARG_NUM(0);
*r_return = Math::tan((double)*p_inputs[0]);
} break;
case MATH_SINH: {
VALIDATE_ARG_NUM(0);
*r_return = Math::sinh((double)*p_inputs[0]);
} break;
case MATH_COSH: {
VALIDATE_ARG_NUM(0);
*r_return = Math::cosh((double)*p_inputs[0]);
} break;
case MATH_TANH: {
VALIDATE_ARG_NUM(0);
*r_return = Math::tanh((double)*p_inputs[0]);
} break;
case MATH_ASIN: {
VALIDATE_ARG_NUM(0);
*r_return = Math::asin((double)*p_inputs[0]);
} break;
case MATH_ACOS: {
VALIDATE_ARG_NUM(0);
*r_return = Math::acos((double)*p_inputs[0]);
} break;
case MATH_ATAN: {
VALIDATE_ARG_NUM(0);
*r_return = Math::atan((double)*p_inputs[0]);
} break;
case MATH_ATAN2: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::atan2((double)*p_inputs[0], (double)*p_inputs[1]);
} break;
case MATH_SQRT: {
VALIDATE_ARG_NUM(0);
*r_return = Math::sqrt((double)*p_inputs[0]);
} break;
case MATH_FMOD: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::fmod((double)*p_inputs[0], (double)*p_inputs[1]);
} break;
case MATH_FPOSMOD: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]);
} break;
case MATH_POSMOD: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::posmod((int)*p_inputs[0], (int)*p_inputs[1]);
} break;
case MATH_FLOOR: {
VALIDATE_ARG_NUM(0);
*r_return = Math::floor((double)*p_inputs[0]);
} break;
case MATH_CEIL: {
VALIDATE_ARG_NUM(0);
*r_return = Math::ceil((double)*p_inputs[0]);
} break;
case MATH_ROUND: {
VALIDATE_ARG_NUM(0);
*r_return = Math::round((double)*p_inputs[0]);
} break;
case MATH_ABS: {
if (p_inputs[0]->get_type() == Variant::INT) {
int64_t i = *p_inputs[0];
*r_return = ABS(i);
} else if (p_inputs[0]->get_type() == Variant::FLOAT) {
real_t r = *p_inputs[0];
*r_return = Math::abs(r);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::FLOAT;
}
} break;
case MATH_SIGN: {
if (p_inputs[0]->get_type() == Variant::INT) {
int64_t i = *p_inputs[0];
*r_return = i < 0 ? -1 : (i > 0 ? +1 : 0);
} else if (p_inputs[0]->get_type() == Variant::FLOAT) {
real_t r = *p_inputs[0];
*r_return = r < 0.0 ? -1.0 : (r > 0.0 ? +1.0 : 0.0);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::FLOAT;
}
} break;
case MATH_POW: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::pow((double)*p_inputs[0], (double)*p_inputs[1]);
} break;
case MATH_LOG: {
VALIDATE_ARG_NUM(0);
*r_return = Math::log((double)*p_inputs[0]);
} break;
case MATH_EXP: {
VALIDATE_ARG_NUM(0);
*r_return = Math::exp((double)*p_inputs[0]);
} break;
case MATH_ISNAN: {
VALIDATE_ARG_NUM(0);
*r_return = Math::is_nan((double)*p_inputs[0]);
} break;
case MATH_ISINF: {
VALIDATE_ARG_NUM(0);
*r_return = Math::is_inf((double)*p_inputs[0]);
} break;
case MATH_EASE: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::ease((double)*p_inputs[0], (double)*p_inputs[1]);
} break;
case MATH_STEP_DECIMALS: {
VALIDATE_ARG_NUM(0);
*r_return = Math::step_decimals((double)*p_inputs[0]);
} break;
case MATH_STEPIFY: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::stepify((double)*p_inputs[0], (double)*p_inputs[1]);
} break;
case MATH_LERP: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*r_return = Math::lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
} break;
case MATH_LERP_ANGLE: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*r_return = Math::lerp_angle((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
} break;
case MATH_INVERSE_LERP: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*r_return = Math::inverse_lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
} break;
case MATH_RANGE_LERP: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
VALIDATE_ARG_NUM(3);
VALIDATE_ARG_NUM(4);
*r_return = Math::range_lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2], (double)*p_inputs[3], (double)*p_inputs[4]);
} break;
case MATH_SMOOTHSTEP: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*r_return = Math::smoothstep((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
} break;
case MATH_MOVE_TOWARD: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*r_return = Math::move_toward((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
} break;
case MATH_DECTIME: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*r_return = Math::dectime((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
} break;
case MATH_RANDOMIZE: {
Math::randomize();
} break;
case MATH_RANDI: {
*r_return = Math::rand();
} break;
case MATH_RANDF: {
*r_return = Math::randf();
} break;
case MATH_RANDF_RANGE: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]);
} break;
case MATH_RANDI_RANGE: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::random((int)*p_inputs[0], (int)*p_inputs[1]);
} break;
case MATH_SEED: {
VALIDATE_ARG_NUM(0);
uint64_t seed = *p_inputs[0];
Math::seed(seed);
} break;
case MATH_RANDSEED: {
VALIDATE_ARG_NUM(0);
uint64_t seed = *p_inputs[0];
int ret = Math::rand_from_seed(&seed);
Array reta;
reta.push_back(ret);
reta.push_back(seed);
*r_return = reta;
} break;
case MATH_DEG2RAD: {
VALIDATE_ARG_NUM(0);
*r_return = Math::deg2rad((double)*p_inputs[0]);
} break;
case MATH_RAD2DEG: {
VALIDATE_ARG_NUM(0);
*r_return = Math::rad2deg((double)*p_inputs[0]);
} break;
case MATH_LINEAR2DB: {
VALIDATE_ARG_NUM(0);
*r_return = Math::linear2db((double)*p_inputs[0]);
} break;
case MATH_DB2LINEAR: {
VALIDATE_ARG_NUM(0);
*r_return = Math::db2linear((double)*p_inputs[0]);
} break;
case MATH_POLAR2CARTESIAN: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
double r = *p_inputs[0];
double th = *p_inputs[1];
*r_return = Vector2(r * Math::cos(th), r * Math::sin(th));
} break;
case MATH_CARTESIAN2POLAR: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
double x = *p_inputs[0];
double y = *p_inputs[1];
*r_return = Vector2(Math::sqrt(x * x + y * y), Math::atan2(y, x));
} break;
case MATH_WRAP: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*r_return = Math::wrapi((int64_t)*p_inputs[0], (int64_t)*p_inputs[1], (int64_t)*p_inputs[2]);
} break;
case MATH_WRAPF: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*r_return = Math::wrapf((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
} break;
case LOGIC_MAX: {
if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) {
int64_t a = *p_inputs[0];
int64_t b = *p_inputs[1];
*r_return = MAX(a, b);
} else {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
real_t a = *p_inputs[0];
real_t b = *p_inputs[1];
*r_return = MAX(a, b);
}
} break;
case LOGIC_MIN: {
if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) {
int64_t a = *p_inputs[0];
int64_t b = *p_inputs[1];
*r_return = MIN(a, b);
} else {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
real_t a = *p_inputs[0];
real_t b = *p_inputs[1];
*r_return = MIN(a, b);
}
} break;
case LOGIC_CLAMP: {
if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT && p_inputs[2]->get_type() == Variant::INT) {
int64_t a = *p_inputs[0];
int64_t b = *p_inputs[1];
int64_t c = *p_inputs[2];
*r_return = CLAMP(a, b, c);
} else {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
real_t a = *p_inputs[0];
real_t b = *p_inputs[1];
real_t c = *p_inputs[2];
*r_return = CLAMP(a, b, c);
}
} break;
case LOGIC_NEAREST_PO2: {
VALIDATE_ARG_NUM(0);
int64_t num = *p_inputs[0];
*r_return = next_power_of_2(num);
} break;
case OBJ_WEAKREF: {
if (p_inputs[0]->get_type() != Variant::OBJECT) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
return;
}
if (p_inputs[0]->is_ref()) {
REF r = *p_inputs[0];
if (!r.is_valid()) {
return;
}
Ref<WeakRef> wref = memnew(WeakRef);
wref->set_ref(r);
*r_return = wref;
} else {
Object *obj = *p_inputs[0];
if (!obj) {
return;
}
Ref<WeakRef> wref = memnew(WeakRef);
wref->set_obj(obj);
*r_return = wref;
}
} break;
case TYPE_CONVERT: {
VALIDATE_ARG_NUM(1);
int type = *p_inputs[1];
if (type < 0 || type >= Variant::VARIANT_MAX) {
r_error_str = RTR("Invalid type argument to convert(), use TYPE_* constants.");
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::INT;
return;
} else {
Variant::construct(Variant::Type(type), *r_return, p_inputs, 1, r_error);
}
} break;
case TYPE_OF: {
*r_return = p_inputs[0]->get_type();
} break;
case TYPE_EXISTS: {
*r_return = ClassDB::class_exists(*p_inputs[0]);
} break;
case TEXT_CHAR: {
char32_t result[2] = { *p_inputs[0], 0 };
*r_return = String(result);
} break;
case TEXT_ORD: {
if (p_inputs[0]->get_type() != Variant::STRING) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
return;
}
String str = *p_inputs[0];
if (str.length() != 1) {
r_error_str = RTR("Expected a string of length 1 (a character).");
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
return;
}
*r_return = str.get(0);
} break;
case TEXT_STR: {
String str = *p_inputs[0];
*r_return = str;
} break;
case TEXT_PRINT: {
String str = *p_inputs[0];
print_line(str);
} break;
case TEXT_PRINTERR: {
String str = *p_inputs[0];
print_error(str);
} break;
case TEXT_PRINTRAW: {
String str = *p_inputs[0];
OS::get_singleton()->print("%s", str.utf8().get_data());
} break;
case VAR_TO_STR: {
String vars;
VariantWriter::write_to_string(*p_inputs[0], vars);
*r_return = vars;
} break;
case STR_TO_VAR: {
if (p_inputs[0]->get_type() != Variant::STRING) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
return;
}
VariantParser::StreamString ss;
ss.s = *p_inputs[0];
String errs;
int line;
Error err = VariantParser::parse(&ss, *r_return, errs, line);
if (err != OK) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
*r_return = "Parse error at line " + itos(line) + ": " + errs;
return;
}
} break;
case VAR_TO_BYTES: {
PackedByteArray barr;
bool full_objects = *p_inputs[1];
int len;
Error err = encode_variant(*p_inputs[0], nullptr, len, full_objects);
if (err) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::NIL;
r_error_str = "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).";
return;
}
barr.resize(len);
{
uint8_t *w = barr.ptrw();
encode_variant(*p_inputs[0], w, len, full_objects);
}
*r_return = barr;
} break;
case BYTES_TO_VAR: {
if (p_inputs[0]->get_type() != Variant::PACKED_BYTE_ARRAY) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::PACKED_BYTE_ARRAY;
return;
}
PackedByteArray varr = *p_inputs[0];
bool allow_objects = *p_inputs[1];
Variant ret;
{
const uint8_t *r = varr.ptr();
Error err = decode_variant(ret, r, varr.size(), nullptr, allow_objects);
if (err != OK) {
r_error_str = RTR("Not enough bytes for decoding bytes, or invalid format.");
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::PACKED_BYTE_ARRAY;
return;
}
}
*r_return = ret;
} break;
case COLORN: {
VALIDATE_ARG_NUM(1);
Color color = Color::named(*p_inputs[0]);
color.a = *p_inputs[1];
*r_return = String(color);
} break;
default: {
}
}
}
////////
static bool _is_number(char32_t c) {
return (c >= '0' && c <= '9');
}
@ -1092,18 +410,9 @@ Error Expression::_get_token(Token &r_token) {
} else if (id == "self") {
r_token.type = TK_SELF;
} else {
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
if (id == Variant::get_type_name(Variant::Type(i))) {
r_token.type = TK_BASIC_TYPE;
r_token.value = i;
return OK;
}
}
BuiltinFunc bifunc = find_function(id);
if (bifunc != FUNC_MAX) {
if (Variant::has_builtin_func(id)) {
r_token.type = TK_BUILTIN_FUNC;
r_token.value = bifunc;
r_token.value = id;
return OK;
}
@ -1401,6 +710,8 @@ Expression::ENode *Expression::_parse_expression() {
case TK_BUILTIN_FUNC: {
//builtin function
StringName func = tk.value;
_get_token(tk);
if (tk.type != TK_PARENTHESIS_OPEN) {
_set_error("Expected '('");
@ -1408,7 +719,7 @@ Expression::ENode *Expression::_parse_expression() {
}
BuiltinFuncNode *bifunc = alloc_node<BuiltinFuncNode>();
bifunc->func = BuiltinFunc(int(tk.value));
bifunc->func = func;
while (true) {
int cofs = str_ofs;
@ -1436,9 +747,11 @@ Expression::ENode *Expression::_parse_expression() {
}
}
int expected_args = get_func_argument_count(bifunc->func);
if (bifunc->arguments.size() != expected_args) {
_set_error("Builtin func '" + get_func_name(bifunc->func) + "' expects " + itos(expected_args) + " arguments.");
if (!Variant::is_builtin_func_vararg(bifunc->func)) {
int expected_args = Variant::get_builtin_func_argument_count(bifunc->func);
if (expected_args != bifunc->arguments.size()) {
_set_error("Builtin func '" + String(bifunc->func) + "' expects " + itos(expected_args) + " arguments.");
}
}
expr = bifunc;
@ -2047,11 +1360,11 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression:
argp.write[i] = &arr[i];
}
r_ret = Variant(); //may not return anything
Callable::CallError ce;
exec_func(bifunc->func, (const Variant **)argp.ptr(), &r_ret, ce, r_error_str);
Variant::call_builtin_func(bifunc->func, &r_ret, (const Variant **)argp.ptr(), argp.size(), ce);
if (ce.error != Callable::CallError::CALL_OK) {
r_error_str = "Builtin Call Failed. " + r_error_str;
r_error_str = "Builtin Call Failed. " + Variant::get_call_error_text(bifunc->func, (const Variant **)argp.ptr(), argp.size(), ce);
return true;
}

View file

@ -36,87 +36,7 @@
class Expression : public Reference {
GDCLASS(Expression, Reference);
public:
enum BuiltinFunc {
MATH_SIN,
MATH_COS,
MATH_TAN,
MATH_SINH,
MATH_COSH,
MATH_TANH,
MATH_ASIN,
MATH_ACOS,
MATH_ATAN,
MATH_ATAN2,
MATH_SQRT,
MATH_FMOD,
MATH_FPOSMOD,
MATH_POSMOD,
MATH_FLOOR,
MATH_CEIL,
MATH_ROUND,
MATH_ABS,
MATH_SIGN,
MATH_POW,
MATH_LOG,
MATH_EXP,
MATH_ISNAN,
MATH_ISINF,
MATH_EASE,
MATH_STEP_DECIMALS,
MATH_STEPIFY,
MATH_LERP,
MATH_LERP_ANGLE,
MATH_INVERSE_LERP,
MATH_RANGE_LERP,
MATH_SMOOTHSTEP,
MATH_MOVE_TOWARD,
MATH_DECTIME,
MATH_RANDOMIZE,
MATH_RANDI,
MATH_RANDF,
MATH_RANDF_RANGE,
MATH_RANDI_RANGE,
MATH_SEED,
MATH_RANDSEED,
MATH_DEG2RAD,
MATH_RAD2DEG,
MATH_LINEAR2DB,
MATH_DB2LINEAR,
MATH_POLAR2CARTESIAN,
MATH_CARTESIAN2POLAR,
MATH_WRAP,
MATH_WRAPF,
LOGIC_MAX,
LOGIC_MIN,
LOGIC_CLAMP,
LOGIC_NEAREST_PO2,
OBJ_WEAKREF,
TYPE_CONVERT,
TYPE_OF,
TYPE_EXISTS,
TEXT_CHAR,
TEXT_ORD,
TEXT_STR,
TEXT_PRINT,
TEXT_PRINTERR,
TEXT_PRINTRAW,
VAR_TO_STR,
STR_TO_VAR,
VAR_TO_BYTES,
BYTES_TO_VAR,
COLORN,
FUNC_MAX
};
static int get_func_argument_count(BuiltinFunc p_func);
static String get_func_name(BuiltinFunc p_func);
static void exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Callable::CallError &r_error, String &r_error_str);
static BuiltinFunc find_function(const String &p_string);
private:
static const char *func_name[FUNC_MAX];
struct Input {
Variant::Type type = Variant::NIL;
String name;
@ -315,7 +235,7 @@ private:
};
struct BuiltinFuncNode : public ENode {
BuiltinFunc func;
StringName func;
Vector<ENode *> arguments;
BuiltinFuncNode() {
type = TYPE_BUILTIN_FUNC;

View file

@ -43,6 +43,7 @@
#include "core/io/dtls_server.h"
#include "core/io/http_client.h"
#include "core/io/image_loader.h"
#include "core/io/json.h"
#include "core/io/marshalls.h"
#include "core/io/multiplayer_api.h"
#include "core/io/networked_multiplayer_peer.h"
@ -197,6 +198,7 @@ void register_core_types() {
ClassDB::register_class<_Semaphore>();
ClassDB::register_class<XMLParser>();
ClassDB::register_class<JSONParser>();
ClassDB::register_class<ConfigFile>();

View file

@ -536,4 +536,24 @@ String RTRN(const String &p_text, const String &p_text_plural, int p_n, const St
bool is_symbol(char32_t c);
bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end);
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr) {
}
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str) {
arr.push_back(p_str);
}
template <class... P>
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str, P... p_args) {
arr.push_back(p_str);
sarray_add_str(arr, p_args...);
}
template <class... P>
_FORCE_INLINE_ Vector<String> sarray(P... p_args) {
Vector<String> arr;
sarray_add_str(arr, p_args...);
return arr;
}
#endif // USTRING_H

View file

@ -3435,6 +3435,30 @@ String Variant::get_construct_string() const {
return vars;
}
String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
String err_text;
if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
int errorarg = ce.argument;
if (p_argptrs) {
err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(ce.expected)) + ".";
} else {
err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected)) + ".";
}
} else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
} else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
} else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
err_text = "Method not found.";
} else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
err_text = "Instance is null";
} else if (ce.error == Callable::CallError::CALL_OK) {
return "Call OK";
}
return "'" + String(p_method) + "': " + err_text;
}
String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
String err_text;
@ -3525,10 +3549,12 @@ void Variant::register_types() {
_register_variant_methods();
_register_variant_setters_getters();
_register_variant_constructors();
_register_variant_builtin_funcs();
}
void Variant::unregister_types() {
_unregister_variant_operators();
_unregister_variant_methods();
_unregister_variant_setters_getters();
_unregister_variant_constructors();
_unregister_variant_builtin_funcs();
}

View file

@ -267,6 +267,8 @@ private:
static void _unregister_variant_setters_getters();
static void _register_variant_constructors();
static void _unregister_variant_constructors();
static void _register_variant_builtin_funcs();
static void _unregister_variant_builtin_funcs();
public:
_FORCE_INLINE_ Type get_type() const {
@ -523,6 +525,7 @@ public:
Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
Variant call(const StringName &p_method, const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant());
static String get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
static String get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
@ -619,6 +622,32 @@ public:
void get_property_list(List<PropertyInfo> *p_list) const;
static void call_builtin_func(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
static bool has_builtin_func(const StringName &p_name);
typedef void (*BuiltinFunctionValidatedCall)(Variant *r_ret, const Variant **p_args, int p_argcount);
typedef void (*BuiltinFunctionPTRCall)(void *r_ret, const void **p_args, int p_argcount);
static BuiltinFunctionValidatedCall get_builtin_validated_caller(const StringName &p_name);
static BuiltinFunctionPTRCall get_builtin_ptr_caller(const StringName &p_name);
enum BuiltInFunctionType {
BUILTIN_FUNC_TYPE_MATH,
BUILTIN_FUNC_TYPE_RANDOM,
BUILTIN_FUNC_TYPE_UTILITY,
};
static BuiltInFunctionType get_builtin_func_type(const StringName &p_name);
static int get_builtin_func_argument_count(const StringName &p_name);
static Variant::Type get_builtin_func_argument_type(const StringName &p_name, int p_arg);
static String get_builtin_func_argument_name(const StringName &p_name, int p_arg);
static bool has_builtin_func_return_value(const StringName &p_name);
static Variant::Type get_builtin_func_return_type(const StringName &p_name);
static bool is_builtin_func_vararg(const StringName &p_name);
static void get_builtin_function_list(List<StringName> *r_functions);
//argsVariant call()
bool operator==(const Variant &p_variant) const;

File diff suppressed because it is too large Load diff

View file

@ -38,26 +38,6 @@
#include "core/os/os.h"
#include "core/templates/oa_hash_map.h"
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr) {
}
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str) {
arr.push_back(p_str);
}
template <class... P>
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str, P... p_args) {
arr.push_back(p_str);
sarray_add_str(arr, p_args...);
}
template <class... P>
_FORCE_INLINE_ Vector<String> sarray(P... p_args) {
Vector<String> arr;
sarray_add_str(arr, p_args...);
return arr;
}
typedef void (*VariantFunc)(Variant &r_ret, Variant &p_self, const Variant **p_args);
typedef void (*VariantConstructFunc)(Variant &r_ret, const Variant **p_args);

View file

@ -39,26 +39,6 @@
#include "core/templates/local_vector.h"
#include "core/templates/oa_hash_map.h"
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr) {
}
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str) {
arr.push_back(p_str);
}
template <class... P>
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str, P... p_args) {
arr.push_back(p_str);
sarray_add_str(arr, p_args...);
}
template <class... P>
_FORCE_INLINE_ Vector<String> sarray(P... p_args) {
Vector<String> arr;
sarray_add_str(arr, p_args...);
return arr;
}
template <class T, class... P>
class VariantConstructor {
template <size_t... Is>

View file

@ -736,6 +736,43 @@ void DocData::generate(bool p_basic_types) {
}
c.properties.push_back(pd);
}
List<StringName> builtin_funcs;
Variant::get_builtin_function_list(&builtin_funcs);
builtin_funcs.sort_custom<StringName::AlphCompare>();
for (List<StringName>::Element *E = builtin_funcs.front(); E; E = E->next()) {
MethodDoc md;
md.name = E->get();
//return
if (Variant::has_builtin_func_return_value(E->get())) {
PropertyInfo pi;
pi.type = Variant::get_builtin_func_return_type(E->get());
if (pi.type == Variant::NIL) {
pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
}
DocData::ArgumentDoc ad;
argument_doc_from_arginfo(ad, pi);
md.return_type = ad.type;
}
if (Variant::is_builtin_func_vararg(E->get())) {
md.qualifiers = "vararg";
} else {
for (int i = 0; i < Variant::get_builtin_func_argument_count(E->get()); i++) {
PropertyInfo pi;
pi.type = Variant::get_builtin_func_argument_type(E->get(), i);
pi.name = Variant::get_builtin_func_argument_name(E->get(), i);
if (pi.type == Variant::NIL) {
pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
}
DocData::ArgumentDoc ad;
argument_doc_from_arginfo(ad, pi);
md.arguments.push_back(ad);
}
}
c.methods.push_back(md);
}
}
// Built-in script reference.