Skip to content

Commit

Permalink
Merge pull request #36927 from ThakeeNathees/export-var-type-reduce-i…
Browse files Browse the repository at this point in the history
…mplimented

Fix: export var type reduce() implemented
  • Loading branch information
akien-mga authored Apr 27, 2020
2 parents f7e2ff5 + cd48720 commit f61587b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,38 @@ GDScriptParser::Node *GDScriptParser::_parse_and_reduce_expression(Node *p_paren
return expr;
}

bool GDScriptParser::_reduce_export_var_type(Variant &p_value, int p_line) {

if (p_value.get_type() == Variant::ARRAY) {
Array arr = p_value;
for (int i = 0; i < arr.size(); i++) {
if (!_reduce_export_var_type(arr[i], p_line)) return false;
}
return true;
}

if (p_value.get_type() == Variant::DICTIONARY) {
Dictionary dict = p_value;
for (int i = 0; i < dict.size(); i++) {
Variant value = dict.get_value_at_index(i);
if (!_reduce_export_var_type(value, p_line)) return false;
}
return true;
}

// validate type
DataType type = _type_from_variant(p_value);
if (type.kind == DataType::BUILTIN) {
return true;
} else if (type.kind == DataType::NATIVE) {
if (ClassDB::is_parent_class(type.native_type, "Resource")) {
return true;
}
}
_set_error("Invalid export type. Only built-in and native resource types can be exported.", p_line);
return false;
}

bool GDScriptParser::_recover_from_completion() {

if (!completion_found) {
Expand Down Expand Up @@ -4895,6 +4927,9 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
_set_error("Can't accept a null constant expression for inferring export type.");
return;
}

if (!_reduce_export_var_type(cn->value, member.line)) return;

member._export.type = cn->value.get_type();
member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
if (cn->value.get_type() == Variant::OBJECT) {
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ class GDScriptParser {
Node *_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign = false, bool p_parsing_constant = false);
Node *_reduce_expression(Node *p_node, bool p_to_const = false);
Node *_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const = false, bool p_allow_assign = false);
bool _reduce_export_var_type(Variant &p_value, int p_line = 0);

PatternNode *_parse_pattern(bool p_static);
void _parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static);
Expand Down

0 comments on commit f61587b

Please sign in to comment.