summaryrefslogtreecommitdiff
path: root/modules/gdscript
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gdscript')
-rw-r--r--modules/gdscript/gd_compiler.cpp122
-rw-r--r--modules/gdscript/gd_compiler.h4
-rw-r--r--modules/gdscript/gd_editor.cpp70
-rw-r--r--modules/gdscript/gd_function.cpp1434
-rw-r--r--modules/gdscript/gd_function.h207
-rw-r--r--modules/gdscript/gd_functions.cpp93
-rw-r--r--modules/gdscript/gd_functions.h1
-rw-r--r--modules/gdscript/gd_parser.cpp8
-rw-r--r--modules/gdscript/gd_pretty_print.cpp34
-rw-r--r--modules/gdscript/gd_pretty_print.h40
-rw-r--r--modules/gdscript/gd_script.cpp1841
-rw-r--r--modules/gdscript/gd_script.h218
-rw-r--r--modules/gdscript/gd_tokenizer.cpp5
-rw-r--r--modules/gdscript/register_types.cpp39
14 files changed, 2366 insertions, 1750 deletions
diff --git a/modules/gdscript/gd_compiler.cpp b/modules/gdscript/gd_compiler.cpp
index d38f5f3e35..7481eac620 100644
--- a/modules/gdscript/gd_compiler.cpp
+++ b/modules/gdscript/gd_compiler.cpp
@@ -1290,8 +1290,8 @@ Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *
// gdfunc = &p_script->initializer;
//} else { //regular func
- p_script->member_functions[func_name]=GDFunction();
- gdfunc = &p_script->member_functions[func_name];
+ p_script->member_functions[func_name]=memnew(GDFunction);
+ gdfunc = p_script->member_functions[func_name];
//}
if (p_func)
@@ -1358,6 +1358,32 @@ Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *
gdfunc->_stack_size=codegen.stack_max;
gdfunc->_call_size=codegen.call_max;
gdfunc->name=func_name;
+#ifdef DEBUG_ENABLED
+ if (ScriptDebugger::get_singleton()){
+ String signature;
+ //path
+ if (p_script->get_path()!=String())
+ signature+=p_script->get_path();
+ //loc
+ if (p_func) {
+ signature+="::"+itos(p_func->body->line);
+ } else {
+ signature+="::0";
+ }
+
+ //funciton and class
+
+ if (p_class->name) {
+ signature+="::"+String(p_class->name)+"."+String(func_name);;
+ } else {
+ signature+="::"+String(func_name);
+ }
+
+
+
+ gdfunc->profile.signature=signature;
+ }
+#endif
gdfunc->_script=p_script;
gdfunc->source=source;
@@ -1388,18 +1414,27 @@ Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *
-Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDParser::ClassNode *p_class) {
+Error GDCompiler::_parse_class(GDScript *p_script, GDScript *p_owner, const GDParser::ClassNode *p_class, bool p_keep_state) {
+ Map<StringName,Ref<GDScript> > old_subclasses;
+
+ if (p_keep_state) {
+ old_subclasses=p_script->subclasses;
+ }
p_script->native=Ref<GDNativeClass>();
p_script->base=Ref<GDScript>();
p_script->_base=NULL;
p_script->members.clear();
p_script->constants.clear();
+ for (Map<StringName,GDFunction*>::Element *E=p_script->member_functions.front();E;E=E->next()) {
+ memdelete(E->get());
+ }
p_script->member_functions.clear();
p_script->member_indices.clear();
p_script->member_info.clear();
p_script->initializer=NULL;
+
p_script->subclasses.clear();
p_script->_owner=p_owner;
p_script->tool=p_class->tool;
@@ -1576,6 +1611,9 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars
p_script->member_default_values[name]=p_class->variables[i].default_value;
}
#endif
+ } else {
+
+ p_script->member_info[name]=PropertyInfo(Variant::NIL,name,PROPERTY_HINT_NONE,"",PROPERTY_USAGE_SCRIPT_VARIABLE);
}
//int new_idx = p_script->member_indices.size();
@@ -1633,9 +1671,15 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars
for(int i=0;i<p_class->subclasses.size();i++) {
StringName name = p_class->subclasses[i]->name;
- Ref<GDScript> subclass = memnew( GDScript );
+ Ref<GDScript> subclass;
+
+ if (old_subclasses.has(name)) {
+ subclass=old_subclasses[name];
+ } else {
+ subclass.instance();
+ }
- Error err = _parse_class(subclass.ptr(),p_script,p_class->subclasses[i]);
+ Error err = _parse_class(subclass.ptr(),p_script,p_class->subclasses[i],p_keep_state);
if (err)
return err;
@@ -1690,7 +1734,7 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars
for(int i=0;i<p_class->variables.size();i++) {
if (p_class->variables[i].setter) {
- const Map<StringName,GDFunction>::Element *E=p_script->get_member_functions().find(p_class->variables[i].setter);
+ const Map<StringName,GDFunction*>::Element *E=p_script->get_member_functions().find(p_class->variables[i].setter);
if (!E) {
_set_error("Setter function '"+String(p_class->variables[i].setter)+"' not found in class.",NULL);
err_line=p_class->variables[i].line;
@@ -1698,7 +1742,7 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars
return ERR_PARSE_ERROR;
}
- if (E->get().is_static()) {
+ if (E->get()->is_static()) {
_set_error("Setter function '"+String(p_class->variables[i].setter)+"' is static.",NULL);
err_line=p_class->variables[i].line;
@@ -1708,7 +1752,7 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars
}
if (p_class->variables[i].getter) {
- const Map<StringName,GDFunction>::Element *E=p_script->get_member_functions().find(p_class->variables[i].getter);
+ const Map<StringName,GDFunction*>::Element *E=p_script->get_member_functions().find(p_class->variables[i].getter);
if (!E) {
_set_error("Getter function '"+String(p_class->variables[i].getter)+"' not found in class.",NULL);
err_line=p_class->variables[i].line;
@@ -1716,7 +1760,7 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars
return ERR_PARSE_ERROR;
}
- if (E->get().is_static()) {
+ if (E->get()->is_static()) {
_set_error("Getter function '"+String(p_class->variables[i].getter)+"' is static.",NULL);
err_line=p_class->variables[i].line;
@@ -1726,13 +1770,67 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars
}
}
+
+ //validate instances if keeping state
+
+ if (p_keep_state) {
+
+ print_line("RELOAD KEEP "+p_script->path);
+ for (Set<Object*>::Element *E=p_script->instances.front();E;) {
+
+ Set<Object*>::Element *N = E->next();
+
+ ScriptInstance *si = E->get()->get_script_instance();
+ if (si->is_placeholder()) {
+#ifdef TOOLS_ENABLED
+ PlaceHolderScriptInstance *psi = static_cast<PlaceHolderScriptInstance*>(si);
+
+ if (p_script->is_tool()) {
+ //re-create as an instance
+ p_script->placeholders.erase(psi); //remove placeholder
+
+ GDInstance* instance = memnew( GDInstance );
+ instance->base_ref=E->get()->cast_to<Reference>();
+ instance->members.resize(p_script->member_indices.size());
+ instance->script=Ref<GDScript>(p_script);
+ instance->owner=E->get();
+
+ //needed for hot reloading
+ for(Map<StringName,GDScript::MemberInfo>::Element *E=p_script->member_indices.front();E;E=E->next()) {
+ instance->member_indices_cache[E->key()]=E->get().index;
+ }
+ instance->owner->set_script_instance(instance);
+
+
+ /* STEP 2, INITIALIZE AND CONSRTUCT */
+
+ Variant::CallError ce;
+ p_script->initializer->call(instance,NULL,0,ce);
+
+ if (ce.error!=Variant::CallError::CALL_OK) {
+ //well, tough luck, not goinna do anything here
+ }
+ }
+#endif
+ } else {
+
+ GDInstance *gi = static_cast<GDInstance*>(si);
+ gi->reload_members();
+ }
+
+ E=N;
+
+ }
+
+
+ }
#endif
p_script->valid=true;
return OK;
}
-Error GDCompiler::compile(const GDParser *p_parser,GDScript *p_script) {
+Error GDCompiler::compile(const GDParser *p_parser,GDScript *p_script,bool p_keep_state) {
err_line=-1;
err_column=-1;
@@ -1743,9 +1841,7 @@ Error GDCompiler::compile(const GDParser *p_parser,GDScript *p_script) {
source=p_script->get_path();
-
-
- Error err = _parse_class(p_script,NULL,static_cast<const GDParser::ClassNode*>(root));
+ Error err = _parse_class(p_script,NULL,static_cast<const GDParser::ClassNode*>(root),p_keep_state);
if (err)
return err;
diff --git a/modules/gdscript/gd_compiler.h b/modules/gdscript/gd_compiler.h
index 32e18c6dcf..7cf575e3d6 100644
--- a/modules/gdscript/gd_compiler.h
+++ b/modules/gdscript/gd_compiler.h
@@ -144,7 +144,7 @@ class GDCompiler {
int _parse_expression(CodeGen& codegen,const GDParser::Node *p_expression, int p_stack_level,bool p_root=false,bool p_initializer=false);
Error _parse_block(CodeGen& codegen,const GDParser::BlockNode *p_block,int p_stack_level=0,int p_break_addr=-1,int p_continue_addr=-1);
Error _parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func,bool p_for_ready=false);
- Error _parse_class(GDScript *p_script,GDScript *p_owner,const GDParser::ClassNode *p_class);
+ Error _parse_class(GDScript *p_script,GDScript *p_owner,const GDParser::ClassNode *p_class,bool p_keep_state);
int err_line;
int err_column;
StringName source;
@@ -152,7 +152,7 @@ class GDCompiler {
public:
- Error compile(const GDParser *p_parser,GDScript *p_script);
+ Error compile(const GDParser *p_parser, GDScript *p_script, bool p_keep_state=false);
String get_error() const;
int get_error_line() const;
diff --git a/modules/gdscript/gd_editor.cpp b/modules/gdscript/gd_editor.cpp
index ff19518ad5..d5bf6463c6 100644
--- a/modules/gdscript/gd_editor.cpp
+++ b/modules/gdscript/gd_editor.cpp
@@ -1024,7 +1024,7 @@ static bool _guess_identifier_type_in_block(GDCompletionContext& context,int p_l
}
-static bool _guess_identifier_from_assignment_in_function(GDCompletionContext& context,const StringName& p_identifier, const StringName& p_function,GDCompletionIdentifier &r_type) {
+static bool _guess_identifier_from_assignment_in_function(GDCompletionContext& context, int p_src_line, const StringName& p_identifier, const StringName& p_function,GDCompletionIdentifier &r_type) {
const GDParser::FunctionNode* func=NULL;
for(int i=0;i<context._class->functions.size();i++) {
@@ -1039,7 +1039,9 @@ static bool _guess_identifier_from_assignment_in_function(GDCompletionContext& c
for(int i=0;i<func->body->statements.size();i++) {
-
+ if (func->body->statements[i]->line == p_src_line) {
+ break;
+ }
if (func->body->statements[i]->type==GDParser::BlockNode::TYPE_OPERATOR) {
const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(func->body->statements[i]);
@@ -1160,11 +1162,11 @@ static bool _guess_identifier_type(GDCompletionContext& context,int p_line,const
}
//try to guess from assignment in construtor or _ready
- if (_guess_identifier_from_assignment_in_function(context,p_identifier,"_ready",r_type))
+ if (_guess_identifier_from_assignment_in_function(context,p_line+1,p_identifier,"_ready",r_type))
return true;
- if (_guess_identifier_from_assignment_in_function(context,p_identifier,"_enter_tree",r_type))
+ if (_guess_identifier_from_assignment_in_function(context,p_line+1,p_identifier,"_enter_tree",r_type))
return true;
- if (_guess_identifier_from_assignment_in_function(context,p_identifier,"_init",r_type))
+ if (_guess_identifier_from_assignment_in_function(context,p_line+1,p_identifier,"_init",r_type))
return true;
return false;
@@ -1310,9 +1312,9 @@ static void _find_identifiers_in_class(GDCompletionContext& context,bool p_stati
}
}
- for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
- if (!p_static || E->get().is_static()) {
- if (E->get().get_argument_count())
+ for (const Map<StringName,GDFunction*>::Element *E=script->get_member_functions().front();E;E=E->next()) {
+ if (!p_static || E->get()->is_static()) {
+ if (E->get()->get_argument_count())
result.insert(E->key().operator String()+"(");
else
result.insert(E->key().operator String()+"()");
@@ -1536,10 +1538,10 @@ static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const St
if (scr) {
while (scr) {
- for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
- if (E->get().is_static() && p_method==E->get().get_name()) {
+ for (const Map<StringName,GDFunction*>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
+ if (E->get()->is_static() && p_method==E->get()->get_name()) {
arghint="static func "+String(p_method)+"(";
- for(int i=0;i<E->get().get_argument_count();i++) {
+ for(int i=0;i<E->get()->get_argument_count();i++) {
if (i>0)
arghint+=", ";
else
@@ -1547,12 +1549,12 @@ static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const St
if (i==p_argidx) {
arghint+=String::chr(0xFFFF);
}
- arghint+="var "+E->get().get_argument_name(i);
- int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
+ arghint+="var "+E->get()->get_argument_name(i);
+ int deffrom = E->get()->get_argument_count()-E->get()->get_default_argument_count();
if (i>=deffrom) {
int defidx = deffrom-i;
- if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
- arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
+ if (defidx>=0 && defidx<E->get()->get_default_argument_count()) {
+ arghint+="="+E->get()->get_default_argument(defidx).get_construct_string();
}
}
if (i==p_argidx) {
@@ -1670,10 +1672,10 @@ static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const St
if (code=="") {
- for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
- if (p_method==E->get().get_name()) {
+ for (const Map<StringName,GDFunction*>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
+ if (p_method==E->get()->get_name()) {
arghint="func "+String(p_method)+"(";
- for(int i=0;i<E->get().get_argument_count();i++) {
+ for(int i=0;i<E->get()->get_argument_count();i++) {
if (i>0)
arghint+=", ";
else
@@ -1681,12 +1683,12 @@ static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const St
if (i==p_argidx) {
arghint+=String::chr(0xFFFF);
}
- arghint+="var "+E->get().get_argument_name(i);
- int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
+ arghint+="var "+E->get()->get_argument_name(i);
+ int deffrom = E->get()->get_argument_count()-E->get()->get_default_argument_count();
if (i>=deffrom) {
int defidx = deffrom-i;
- if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
- arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
+ if (defidx>=0 && defidx<E->get()->get_default_argument_count()) {
+ arghint+="="+E->get()->get_default_argument(defidx).get_construct_string();
}
}
if (i==p_argidx) {
@@ -1926,16 +1928,16 @@ static void _find_call_arguments(GDCompletionContext& context,const GDParser::No
if (script.is_valid()) {
- for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
+ for (const Map<StringName,GDFunction*>::Element *E=script->get_member_functions().front();E;E=E->next()) {
if (E->key()==id->name) {
- if (context.function && context.function->_static && !E->get().is_static())
+ if (context.function && context.function->_static && !E->get()->is_static())
continue;
arghint = "func "+id->name.operator String()+String("(");
- for(int i=0;i<E->get().get_argument_count();i++) {
+ for(int i=0;i<E->get()->get_argument_count();i++) {
if (i>0)
arghint+=", ";
else
@@ -1943,12 +1945,12 @@ static void _find_call_arguments(GDCompletionContext& context,const GDParser::No
if (i==p_argidx) {
arghint+=String::chr(0xFFFF);
}
- arghint+=E->get().get_argument_name(i);
- int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
+ arghint+=E->get()->get_argument_name(i);
+ int deffrom = E->get()->get_argument_count()-E->get()->get_default_argument_count();
if (i>=deffrom) {
int defidx = deffrom-i;
- if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
- arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
+ if (defidx>=0 && defidx<E->get()->get_default_argument_count()) {
+ arghint+="="+E->get()->get_default_argument(defidx).get_construct_string();
}
}
if (i==p_argidx) {
@@ -1956,7 +1958,7 @@ static void _find_call_arguments(GDCompletionContext& context,const GDParser::No
}
}
- if (E->get().get_argument_count()>0)
+ if (E->get()->get_argument_count()>0)
arghint+=" ";
arghint+=")";
return;
@@ -2178,8 +2180,8 @@ Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base
options.insert(E->key());
}
}
- for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
- if (E->get().is_static())
+ for (const Map<StringName,GDFunction*>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
+ if (E->get()->is_static())
options.insert(E->key());
}
@@ -2266,8 +2268,8 @@ Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base
options.insert(E->key());
}
}
- for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
- if (E->get().get_argument_count())
+ for (const Map<StringName,GDFunction*>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
+ if (E->get()->get_argument_count())
options.insert(String(E->key())+"()");
else
options.insert(String(E->key())+"(");
diff --git a/modules/gdscript/gd_function.cpp b/modules/gdscript/gd_function.cpp
new file mode 100644
index 0000000000..9d438998cb
--- /dev/null
+++ b/modules/gdscript/gd_function.cpp
@@ -0,0 +1,1434 @@
+#include "gd_function.h"
+#include "gd_script.h"
+#include "os/os.h"
+#include "gd_functions.h"
+
+Variant *GDFunction::_get_variant(int p_address,GDInstance *p_instance,GDScript *p_script,Variant &self, Variant *p_stack,String& r_error) const{
+
+ int address = p_address&ADDR_MASK;
+
+ //sequential table (jump table generated by compiler)
+ switch((p_address&ADDR_TYPE_MASK)>>ADDR_BITS) {
+
+ case ADDR_TYPE_SELF: {
+
+ if (!p_instance) {
+ r_error="Cannot access self without instance.";
+ return NULL;
+ }
+ return &self;
+ } break;
+ case ADDR_TYPE_CLASS: {
+
+ return &p_script->_static_ref;
+ } break;
+ case ADDR_TYPE_MEMBER: {
+ //member indexing is O(1)
+ if (!p_instance) {
+ r_error="Cannot access member without instance.";
+ return NULL;
+ }
+ return &p_instance->members[address];
+ } break;
+ case ADDR_TYPE_CLASS_CONSTANT: {
+
+ //todo change to index!
+ GDScript *o=p_script;
+ ERR_FAIL_INDEX_V(address,_global_names_count,NULL);
+ const StringName *sn = &_global_names_ptr[address];
+
+ while(o) {
+ GDScript *s=o;
+ while(s) {
+
+ Map<StringName,Variant>::Element *E=s->constants.find(*sn);
+ if (E) {
+ return &E->get();
+ }
+ s=s->_base;
+ }
+ o=o->_owner;
+ }
+
+
+ ERR_EXPLAIN("GDCompiler bug..");
+ ERR_FAIL_V(NULL);
+ } break;
+ case ADDR_TYPE_LOCAL_CONSTANT: {
+ ERR_FAIL_INDEX_V(address,_constant_count,NULL);
+ return &_constants_ptr[address];
+ } break;
+ case ADDR_TYPE_STACK:
+ case ADDR_TYPE_STACK_VARIABLE: {
+ ERR_FAIL_INDEX_V(address,_stack_size,NULL);
+ return &p_stack[address];
+ } break;
+ case ADDR_TYPE_GLOBAL: {
+
+
+ ERR_FAIL_INDEX_V(address,GDScriptLanguage::get_singleton()->get_global_array_size(),NULL);
+
+
+ return &GDScriptLanguage::get_singleton()->get_global_array()[address];
+ } break;
+ case ADDR_TYPE_NIL: {
+ return &nil;
+ } break;
+ }
+
+ ERR_EXPLAIN("Bad Code! (Addressing Mode)");
+ ERR_FAIL_V(NULL);
+ return NULL;
+}
+
+
+String GDFunction::_get_call_error(const Variant::CallError& p_err, const String& p_where,const Variant**argptrs) const {
+
+
+
+ String err_text;
+
+ if (p_err.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
+ int errorarg=p_err.argument;
+ err_text="Invalid type in "+p_where+". Cannot convert argument "+itos(errorarg+1)+" from "+Variant::get_type_name(argptrs[errorarg]->get_type())+" to "+Variant::get_type_name(p_err.expected)+".";
+ } else if (p_err.error==Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
+ err_text="Invalid call to "+p_where+". Expected "+itos(p_err.argument)+" arguments.";
+ } else if (p_err.error==Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
+ err_text="Invalid call to "+p_where+". Expected "+itos(p_err.argument)+" arguments.";
+ } else if (p_err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
+ err_text="Invalid call. Nonexistent "+p_where+".";
+ } else if (p_err.error==Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
+ err_text="Attempt to call "+p_where+" on a null instance.";
+ } else {
+ err_text="Bug, call error: #"+itos(p_err.error);
+ }
+
+ return err_text;
+
+}
+
+static String _get_var_type(const Variant* p_type) {
+
+ String basestr;
+
+ if (p_type->get_type()==Variant::OBJECT) {
+ Object *bobj = *p_type;
+ if (!bobj) {
+ basestr = "null instance";
+ } else {
+#ifdef DEBUG_ENABLED
+ if (ObjectDB::instance_validate(bobj)) {
+ if (bobj->get_script_instance())
+ basestr= bobj->get_type()+" ("+bobj->get_script_instance()->get_script()->get_path().get_file()+")";
+ else
+ basestr = bobj->get_type();
+ } else {
+ basestr="previously freed instance";
+ }
+
+#else
+ basestr="Object";
+#endif
+ }
+
+ } else {
+ basestr = Variant::get_type_name(p_type->get_type());
+ }
+
+ return basestr;
+
+}
+
+Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError& r_err, CallState *p_state) {
+
+
+ if (!_code_ptr) {
+
+ return Variant();
+ }
+
+ r_err.error=Variant::CallError::CALL_OK;
+
+ Variant self;
+ Variant retvalue;
+ Variant *stack = NULL;
+ Variant **call_args;
+ int defarg=0;
+
+#ifdef DEBUG_ENABLED
+
+ //GDScriptLanguage::get_singleton()->calls++;
+
+#endif
+
+ uint32_t alloca_size=0;
+ GDScript *_class;
+ int ip=0;
+ int line=_initial_line;
+
+
+
+ if (p_state) {
+ //use existing (supplied) state (yielded)
+ stack=(Variant*)p_state->stack.ptr();
+ call_args=(Variant**)&p_state->stack[sizeof(Variant)*p_state->stack_size];
+ line=p_state->line;
+ ip=p_state->ip;
+ alloca_size=p_state->stack.size();
+ _class=p_state->_class;
+ p_instance=p_state->instance;
+ defarg=p_state->defarg;
+ self=p_state->self;
+ //stack[p_state->result_pos]=p_state->result; //assign stack with result
+
+ } else {
+
+ if (p_argcount!=_argument_count) {
+
+ if (p_argcount>_argument_count) {
+
+ r_err.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
+ r_err.argument=_argument_count;
+
+
+ return Variant();
+ } else if (p_argcount < _argument_count - _default_arg_count) {
+
+ r_err.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
+ r_err.argument=_argument_count - _default_arg_count;
+ return Variant();
+ } else {
+
+ defarg=_argument_count-p_argcount;
+ }
+ }
+
+ alloca_size = sizeof(Variant*)*_call_size + sizeof(Variant)*_stack_size;
+
+ if (alloca_size) {
+
+ uint8_t *aptr = (uint8_t*)alloca(alloca_size);
+
+ if (_stack_size) {
+
+ stack=(Variant*)aptr;
+ for(int i=0;i<p_argcount;i++)
+ memnew_placement(&stack[i],Variant(*p_args[i]));
+ for(int i=p_argcount;i<_stack_size;i++)
+ memnew_placement(&stack[i],Variant);
+ } else {
+ stack=NULL;
+ }
+
+ if (_call_size) {
+
+ call_args = (Variant**)&aptr[sizeof(Variant)*_stack_size];
+ } else {
+
+ call_args=NULL;
+ }
+
+
+ } else {
+ stack=NULL;
+ call_args=NULL;
+ }
+
+ if (p_instance) {
+ if (p_instance->base_ref && static_cast<Reference*>(p_instance->owner)->is_referenced()) {
+
+ self=REF(static_cast<Reference*>(p_instance->owner));
+ } else {
+ self=p_instance->owner;
+ }
+ _class=p_instance->script.ptr();
+ } else {
+ _class=_script;
+ }
+ }
+
+ String err_text;
+
+#ifdef DEBUG_ENABLED
+
+ if (ScriptDebugger::get_singleton())
+ GDScriptLanguage::get_singleton()->enter_function(p_instance,this,stack,&ip,&line);
+
+#define CHECK_SPACE(m_space)\
+ ERR_BREAK((ip+m_space)>_code_size)
+
+#define GET_VARIANT_PTR(m_v,m_code_ofs) \
+ Variant *m_v; \
+ m_v = _get_variant(_code_ptr[ip+m_code_ofs],p_instance,_class,self,stack,err_text);\
+ if (!m_v)\
+ break;
+
+
+#else
+#define CHECK_SPACE(m_space)
+#define GET_VARIANT_PTR(m_v,m_code_ofs) \
+ Variant *m_v; \
+ m_v = _get_variant(_code_ptr[ip+m_code_ofs],p_instance,_class,self,stack,err_text);
+
+#endif
+
+
+#ifdef DEBUG_ENABLED
+
+ uint64_t function_start_time;
+ uint64_t function_call_time;
+
+ if (GDScriptLanguage::get_singleton()->profiling) {
+ function_start_time=OS::get_singleton()->get_ticks_usec();
+ function_call_time=0;
+ profile.call_count++;
+ profile.frame_call_count++;
+ }
+#endif
+ bool exit_ok=false;
+
+ while(ip<_code_size) {
+
+
+ int last_opcode=_code_ptr[ip];
+ switch(_code_ptr[ip]) {
+
+ case OPCODE_OPERATOR: {
+
+ CHECK_SPACE(5);
+
+ bool valid;
+ Variant::Operator op = (Variant::Operator)_code_ptr[ip+1];
+ ERR_BREAK(op>=Variant::OP_MAX);
+
+ GET_VARIANT_PTR(a,2);
+ GET_VARIANT_PTR(b,3);
+ GET_VARIANT_PTR(dst,4);
+
+#ifdef DEBUG_ENABLED
+ Variant ret;
+ Variant::evaluate(op,*a,*b,ret,valid);
+#else
+ Variant::evaluate(op,*a,*b,*dst,valid);
+#endif
+
+ if (!valid) {
+#ifdef DEBUG_ENABLED
+
+ if (ret.get_type()==Variant::STRING) {
+ //return a string when invalid with the error
+ err_text=ret;
+ err_text += " in operator '"+Variant::get_operator_name(op)+"'.";
+ } else {
+ err_text="Invalid operands '"+Variant::get_type_name(a->get_type())+"' and '"+Variant::get_type_name(b->get_type())+"' in operator '"+Variant::get_operator_name(op)+"'.";
+ }
+#endif
+ break;
+
+ }
+#ifdef DEBUG_ENABLED
+ *dst=ret;
+#endif
+
+ ip+=5;
+
+ } continue;
+ case OPCODE_EXTENDS_TEST: {
+
+ CHECK_SPACE(4);
+
+ GET_VARIANT_PTR(a,1);
+ GET_VARIANT_PTR(b,2);
+ GET_VARIANT_PTR(dst,3);
+
+#ifdef DEBUG_ENABLED
+
+ if (a->get_type()!=Variant::OBJECT || a->operator Object*()==NULL) {
+
+ err_text="Left operand of 'extends' is not an instance of anything.";
+ break;
+
+ }
+ if (b->get_type()!=Variant::OBJECT || b->operator Object*()==NULL) {
+
+ err_text="Right operand of 'extends' is not a class.";
+ break;
+
+ }
+#endif
+
+
+ Object *obj_A = *a;
+ Object *obj_B = *b;
+
+
+ GDScript *scr_B = obj_B->cast_to<GDScript>();
+
+ bool extends_ok=false;
+
+ if (scr_B) {
+ //if B is a script, the only valid condition is that A has an instance which inherits from the script
+ //in other situation, this shoul return false.
+
+ if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language()==GDScriptLanguage::get_singleton()) {
+
+ GDInstance *ins = static_cast<GDInstance*>(obj_A->get_script_instance());
+ GDScript *cmp = ins->script.ptr();
+ //bool found=false;
+ while(cmp) {
+
+ if (cmp==scr_B) {
+ //inherits from script, all ok
+ extends_ok=true;
+ break;
+
+ }
+
+ cmp=cmp->_base;
+ }
+
+ }
+
+
+ } else {
+
+ GDNativeClass *nc= obj_B->cast_to<GDNativeClass>();
+
+ if (!nc) {
+
+ err_text="Right operand of 'extends' is not a class (type: '"+obj_B->get_type()+"').";
+ break;
+ }
+
+ extends_ok=ObjectTypeDB::is_type(obj_A->get_type_name(),nc->get_name());
+ }
+
+ *dst=extends_ok;
+ ip+=4;
+
+ } continue;
+ case OPCODE_SET: {
+
+ CHECK_SPACE(3);
+
+ GET_VARIANT_PTR(dst,1);
+ GET_VARIANT_PTR(index,2);
+ GET_VARIANT_PTR(value,3);
+
+ bool valid;
+ dst->set(*index,*value,&valid);
+
+ if (!valid) {
+ String v = index->operator String();
+ if (v!="") {
+ v="'"+v+"'";
+ } else {
+ v="of type '"+_get_var_type(index)+"'";
+ }
+ err_text="Invalid set index "+v+" (on base: '"+_get_var_type(dst)+"').";
+ break;
+ }
+
+ ip+=4;
+ } continue;
+ case OPCODE_GET: {
+
+ CHECK_SPACE(3);
+
+ GET_VARIANT_PTR(src,1);
+ GET_VARIANT_PTR(index,2);
+ GET_VARIANT_PTR(dst,3);
+
+ bool valid;
+#ifdef DEBUG_ENABLED
+ //allow better error message in cases where src and dst are the same stack position
+ Variant ret = src->get(*index,&valid);
+#else
+ *dst = src->get(*index,&valid);
+
+#endif
+ if (!valid) {
+ String v = index->operator String();
+ if (v!="") {
+ v="'"+v+"'";
+ } else {
+ v="of type '"+_get_var_type(index)+"'";
+ }
+ err_text="Invalid get index "+v+" (on base: '"+_get_var_type(src)+"').";
+ break;
+ }
+#ifdef DEBUG_ENABLED
+ *dst=ret;
+#endif
+ ip+=4;
+ } continue;
+ case OPCODE_SET_NAMED: {
+
+ CHECK_SPACE(3);
+
+ GET_VARIANT_PTR(dst,1);
+ GET_VARIANT_PTR(value,3);
+
+ int indexname = _code_ptr[ip+2];
+
+ ERR_BREAK(indexname<0 || indexname>=_global_names_count);
+ const StringName *index = &_global_names_ptr[indexname];
+
+ bool valid;
+ dst->set_named(*index,*value,&valid);
+
+ if (!valid) {
+ String err_type;
+ err_text="Invalid set index '"+String(*index)+"' (on base: '"+_get_var_type(dst)+"').";
+ break;
+ }
+
+ ip+=4;
+ } continue;
+ case OPCODE_GET_NAMED: {
+
+
+ CHECK_SPACE(3);
+
+ GET_VARIANT_PTR(src,1);
+ GET_VARIANT_PTR(dst,3);
+
+ int indexname = _code_ptr[ip+2];
+
+ ERR_BREAK(indexname<0 || indexname>=_global_names_count);
+ const StringName *index = &_global_names_ptr[indexname];
+
+ bool valid;
+#ifdef DEBUG_ENABLED
+ //allow better error message in cases where src and dst are the same stack position
+ Variant ret = src->get_named(*index,&valid);
+
+#else
+ *dst = src->get_named(*index,&valid);
+#endif
+
+ if (!valid) {
+ if (src->has_method(*index)) {
+ err_text="Invalid get index '"+index->operator String()+"' (on base: '"+_get_var_type(src)+"'). Did you mean '."+index->operator String()+"()' ?";
+ } else {
+ err_text="Invalid get index '"+index->operator String()+"' (on base: '"+_get_var_type(src)+"').";
+ }
+ break;
+ }
+#ifdef DEBUG_ENABLED
+ *dst=ret;
+#endif
+ ip+=4;
+ } continue;
+ case OPCODE_ASSIGN: {
+
+ CHECK_SPACE(3);
+ GET_VARIANT_PTR(dst,1);
+ GET_VARIANT_PTR(src,2);
+
+ *dst = *src;
+
+ ip+=3;
+
+ } continue;
+ case OPCODE_ASSIGN_TRUE: {
+
+ CHECK_SPACE(2);
+ GET_VARIANT_PTR(dst,1);
+
+ *dst = true;
+
+ ip+=2;
+ } continue;
+ case OPCODE_ASSIGN_FALSE: {
+
+ CHECK_SPACE(2);
+ GET_VARIANT_PTR(dst,1);
+
+ *dst = false;
+
+ ip+=2;
+ } continue;
+ case OPCODE_CONSTRUCT: {
+
+ CHECK_SPACE(2);
+ Variant::Type t=Variant::Type(_code_ptr[ip+1]);
+ int argc=_code_ptr[ip+2];
+ CHECK_SPACE(argc+2);
+ Variant **argptrs = call_args;
+ for(int i=0;i<argc;i++) {
+ GET_VARIANT_PTR(v,3+i);
+ argptrs[i]=v;
+ }
+
+ GET_VARIANT_PTR(dst,3+argc);
+ Variant::CallError err;
+ *dst = Variant::construct(t,(const Variant**)argptrs,argc,err);
+
+ if (err.error!=Variant::CallError::CALL_OK) {
+
+ err_text=_get_call_error(err,"'"+Variant::get_type_name(t)+"' constructor",(const Variant**)argptrs);
+ break;
+ }
+
+ ip+=4+argc;
+ //construct a basic type
+ } continue;
+ case OPCODE_CONSTRUCT_ARRAY: {
+
+ CHECK_SPACE(1);
+ int argc=_code_ptr[ip+1];
+ Array array(true); //arrays are always shared
+ array.resize(argc);
+ CHECK_SPACE(argc+2);
+
+ for(int i=0;i<argc;i++) {
+ GET_VARIANT_PTR(v,2+i);
+ array[i]=*v;
+
+ }
+
+ GET_VARIANT_PTR(dst,2+argc);
+
+ *dst=array;
+
+ ip+=3+argc;
+
+ } continue;
+ case OPCODE_CONSTRUCT_DICTIONARY: {
+
+ CHECK_SPACE(1);
+ int argc=_code_ptr[ip+1];
+ Dictionary dict(true); //arrays are always shared
+
+ CHECK_SPACE(argc*2+2);
+
+ for(int i=0;i<argc;i++) {
+
+ GET_VARIANT_PTR(k,2+i*2+0);
+ GET_VARIANT_PTR(v,2+i*2+1);
+ dict[*k]=*v;
+
+ }
+
+ GET_VARIANT_PTR(dst,2+argc*2);
+
+ *dst=dict;
+
+ ip+=3+argc*2;
+
+ } continue;
+ case OPCODE_CALL_RETURN:
+ case OPCODE_CALL: {
+
+
+ CHECK_SPACE(4);
+ bool call_ret = _code_ptr[ip]==OPCODE_CALL_RETURN;
+
+ int argc=_code_ptr[ip+1];
+ GET_VARIANT_PTR(base,2);
+ int nameg=_code_ptr[ip+3];
+
+ ERR_BREAK(nameg<0 || nameg>=_global_names_count);
+ const StringName *methodname = &_global_names_ptr[nameg];
+
+ ERR_BREAK(argc<0);
+ ip+=4;
+ CHECK_SPACE(argc+1);
+ Variant **argptrs = call_args;
+
+ for(int i=0;i<argc;i++) {
+ GET_VARIANT_PTR(v,i);
+ argptrs[i]=v;
+ }
+
+#ifdef DEBUG_ENABLED
+ uint64_t call_time;
+
+ if (GDScriptLanguage::get_singleton()->profiling) {
+ call_time=OS::get_singleton()->get_ticks_usec();
+ }
+
+#endif
+ Variant::CallError err;
+ if (call_ret) {
+
+ GET_VARIANT_PTR(ret,argc);
+ *ret = base->call(*methodname,(const Variant**)argptrs,argc,err);
+ } else {
+
+ base->call(*methodname,(const Variant**)argptrs,argc,err);
+ }
+#ifdef DEBUG_ENABLED
+ if (GDScriptLanguage::get_singleton()->profiling) {
+ function_call_time+=OS::get_singleton()->get_ticks_usec() - call_time;
+ }
+#endif
+
+ if (err.error!=Variant::CallError::CALL_OK) {
+
+
+ String methodstr = *methodname;
+ String basestr = _get_var_type(base);
+
+ if (methodstr=="call") {
+ if (argc>=1) {
+ methodstr=String(*argptrs[0])+" (via call)";
+ if (err.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
+ err.argument-=1;
+ }
+ }
+ } if (methodstr=="free") {
+
+ if (err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
+
+ if (base->is_ref()) {
+ err_text="Attempted to free a reference.";
+ break;
+ } else if (base->get_type()==Variant::OBJECT) {
+
+ err_text="Attempted to free a locked object (calling or emitting).";
+ break;
+ }
+ }
+ }
+ err_text=_get_call_error(err,"function '"+methodstr+"' in base '"+basestr+"'",(const Variant**)argptrs);
+ break;
+ }
+
+ //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
+ ip+=argc+1;
+
+ } continue;
+ case OPCODE_CALL_BUILT_IN: {
+
+ CHECK_SPACE(4);
+
+ GDFunctions::Function func = GDFunctions::Function(_code_ptr[ip+1]);
+ int argc=_code_ptr[ip+2];
+ ERR_BREAK(argc<0);
+
+ ip+=3;
+ CHECK_SPACE(argc+1);
+ Variant **argptrs = call_args;
+
+ for(int i=0;i<argc;i++) {
+ GET_VARIANT_PTR(v,i);
+ argptrs[i]=v;
+ }
+
+ GET_VARIANT_PTR(dst,argc);
+
+ Variant::CallError err;
+
+ GDFunctions::call(func,(const Variant**)argptrs,argc,*dst,err);
+
+ if (err.error!=Variant::CallError::CALL_OK) {
+
+
+ String methodstr = GDFunctions::get_func_name(func);
+ if (dst->get_type()==Variant::STRING) {
+ //call provided error string
+ err_text="Error calling built-in function '"+methodstr+"': "+String(*dst);
+ } else {
+ err_text=_get_call_error(err,"built-in function '"+methodstr+"'",(const Variant**)argptrs);
+ }
+ break;
+ }
+ ip+=argc+1;
+
+ } continue;
+ case OPCODE_CALL_SELF: {
+
+
+ } break;
+ case OPCODE_CALL_SELF_BASE: {
+
+ CHECK_SPACE(2);
+ int self_fun = _code_ptr[ip+1];
+#ifdef DEBUG_ENABLED
+
+ if (self_fun<0 || self_fun>=_global_names_count) {
+
+ err_text="compiler bug, function name not found";
+ break;
+ }
+#endif
+ const StringName *methodname = &_global_names_ptr[self_fun];
+
+ int argc=_code_ptr[ip+2];
+
+ CHECK_SPACE(2+argc+1);
+
+ Variant **argptrs = call_args;
+
+ for(int i=0;i<argc;i++) {
+ GET_VARIANT_PTR(v,i+3);
+ argptrs[i]=v;
+ }
+
+ GET_VARIANT_PTR(dst,argc+3);
+
+ const GDScript *gds = _script;
+
+
+ const Map<StringName,GDFunction*>::Element *E=NULL;
+ while (gds->base.ptr()) {
+ gds=gds->base.ptr();
+ E=gds->member_functions.find(*methodname);
+ if (E)
+ break;
+ }
+
+ Variant::CallError err;
+
+ if (E) {
+
+ *dst=E->get()->call(p_instance,(const Variant**)argptrs,argc,err);
+ } else if (gds->native.ptr()) {
+
+ if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
+
+ MethodBind *mb = ObjectTypeDB::get_method(gds->native->get_name(),*methodname);
+ if (!mb) {
+ err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
+ } else {
+ *dst=mb->call(p_instance->owner,(const Variant**)argptrs,argc,err);
+ }
+ } else {
+ err.error=Variant::CallError::CALL_OK;
+ }
+ } else {
+
+ if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
+ err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
+ } else {
+ err.error=Variant::CallError::CALL_OK;
+ }
+ }
+
+
+ if (err.error!=Variant::CallError::CALL_OK) {
+
+
+ String methodstr = *methodname;
+ err_text=_get_call_error(err,"function '"+methodstr+"'",(const Variant**)argptrs);
+
+ break;
+ }
+
+ ip+=4+argc;
+
+ } continue;
+ case OPCODE_YIELD:
+ case OPCODE_YIELD_SIGNAL: {
+
+ int ipofs=1;
+ if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
+ CHECK_SPACE(4);
+ ipofs+=2;
+ } else {
+ CHECK_SPACE(2);
+
+ }
+
+ Ref<GDFunctionState> gdfs = memnew( GDFunctionState );
+ gdfs->function=this;
+
+ gdfs->state.stack.resize(alloca_size);
+ //copy variant stack
+ for(int i=0;i<_stack_size;i++) {
+ memnew_placement(&gdfs->state.stack[sizeof(Variant)*i],Variant(stack[i]));
+ }
+ gdfs->state.stack_size=_stack_size;
+ gdfs->state.self=self;
+ gdfs->state.alloca_size=alloca_size;
+ gdfs->state._class=_class;
+ gdfs->state.ip=ip+ipofs;
+ gdfs->state.line=line;
+ //gdfs->state.result_pos=ip+ipofs-1;
+ gdfs->state.defarg=defarg;
+ gdfs->state.instance=p_instance;
+ gdfs->function=this;
+
+ retvalue=gdfs;
+
+ if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
+ GET_VARIANT_PTR(argobj,1);
+ GET_VARIANT_PTR(argname,2);
+ //do the oneshot connect
+
+ if (argobj->get_type()!=Variant::OBJECT) {
+ err_text="First argument of yield() not of type object.";
+ break;
+ }
+ if (argname->get_type()!=Variant::STRING) {
+ err_text="Second argument of yield() not a string (for signal name).";
+ break;
+ }
+ Object *obj=argobj->operator Object *();
+ String signal = argname->operator String();
+#ifdef DEBUG_ENABLED
+
+ if (!obj) {
+ err_text="First argument of yield() is null.";
+ break;
+ }
+ if (ScriptDebugger::get_singleton()) {
+ if (!ObjectDB::instance_validate(obj)) {
+ err_text="First argument of yield() is a previously freed instance.";
+ break;
+ }
+ }
+ if (signal.length()==0) {
+
+ err_text="Second argument of yield() is an empty string (for signal name).";
+ break;
+ }
+
+#endif
+ Error err = obj->connect(signal,gdfs.ptr(),"_signal_callback",varray(gdfs),Object::CONNECT_ONESHOT);
+ if (err!=OK) {
+ err_text="Error connecting to signal: "+signal+" during yield().";
+ break;
+ }
+
+
+ }
+
+ exit_ok=true;
+
+ } break;
+ case OPCODE_YIELD_RESUME: {
+
+ CHECK_SPACE(2);
+ if (!p_state) {
+ err_text=("Invalid Resume (bug?)");
+ break;
+ }
+ GET_VARIANT_PTR(result,1);
+ *result=p_state->result;
+ ip+=2;
+
+ } continue;
+ case OPCODE_JUMP: {
+
+ CHECK_SPACE(2);
+ int to = _code_ptr[ip+1];
+
+ ERR_BREAK(to<0 || to>_code_size);
+ ip=to;
+
+ } continue;
+ case OPCODE_JUMP_IF: {
+
+ CHECK_SPACE(3);
+
+ GET_VARIANT_PTR(test,1);
+
+ bool valid;
+ bool result = test->booleanize(valid);
+#ifdef DEBUG_ENABLED
+ if (!valid) {
+
+ err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
+ break;
+ }
+#endif
+ if (result) {
+ int to = _code_ptr[ip+2];
+ ERR_BREAK(to<0 || to>_code_size);
+ ip=to;
+ continue;
+ }
+ ip+=3;
+ } continue;
+ case OPCODE_JUMP_IF_NOT: {
+
+ CHECK_SPACE(3);
+
+ GET_VARIANT_PTR(test,1);
+
+ bool valid;
+ bool result = test->booleanize(valid);
+#ifdef DEBUG_ENABLED
+ if (!valid) {
+
+ err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
+ break;
+ }
+#endif
+ if (!result) {
+ int to = _code_ptr[ip+2];
+ ERR_BREAK(to<0 || to>_code_size);
+ ip=to;
+ continue;
+ }
+ ip+=3;
+ } continue;
+ case OPCODE_JUMP_TO_DEF_ARGUMENT: {
+
+ CHECK_SPACE(2);
+ ip=_default_arg_ptr[defarg];
+
+ } continue;
+ case OPCODE_RETURN: {
+
+ CHECK_SPACE(2);
+ GET_VARIANT_PTR(r,1);
+ retvalue=*r;
+ exit_ok=true;
+
+ } break;
+ case OPCODE_ITERATE_BEGIN: {
+
+ CHECK_SPACE(8); //space for this an regular iterate
+
+ GET_VARIANT_PTR(counter,1);
+ GET_VARIANT_PTR(container,2);
+
+ bool valid;
+ if (!container->iter_init(*counter,valid)) {
+ if (!valid) {
+ err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"'.";
+ break;
+ }
+ int jumpto=_code_ptr[ip+3];
+ ERR_BREAK(jumpto<0 || jumpto>_code_size);
+ ip=jumpto;
+ continue;
+ }
+ GET_VARIANT_PTR(iterator,4);
+
+
+ *iterator=container->iter_get(*counter,valid);
+ if (!valid) {
+ err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"'.";
+ break;
+ }
+
+
+ ip+=5; //skip regular iterate which is always next
+
+ } continue;
+ case OPCODE_ITERATE: {
+
+ CHECK_SPACE(4);
+
+ GET_VARIANT_PTR(counter,1);
+ GET_VARIANT_PTR(container,2);
+
+ bool valid;
+ if (!container->iter_next(*counter,valid)) {
+ if (!valid) {
+ err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"' (type changed since first iteration?).";
+ break;
+ }
+ int jumpto=_code_ptr[ip+3];
+ ERR_BREAK(jumpto<0 || jumpto>_code_size);
+ ip=jumpto;
+ continue;
+ }
+ GET_VARIANT_PTR(iterator,4);
+
+ *iterator=container->iter_get(*counter,valid);
+ if (!valid) {
+ err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"' (but was obtained on first iteration?).";
+ break;
+ }
+
+ ip+=5; //loop again
+ } continue;
+ case OPCODE_ASSERT: {
+ CHECK_SPACE(2);
+ GET_VARIANT_PTR(test,1);
+
+#ifdef DEBUG_ENABLED
+ bool valid;
+ bool result = test->booleanize(valid);
+
+
+ if (!valid) {
+
+ err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
+ break;
+ }
+
+
+ if (!result) {
+
+ err_text="Assertion failed.";
+ break;
+ }
+
+#endif
+
+ ip+=2;
+ } continue;
+ case OPCODE_BREAKPOINT: {
+#ifdef DEBUG_ENABLED
+ if (ScriptDebugger::get_singleton()) {
+ GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement",true);
+ }
+#endif
+ ip+=1;
+ } continue;
+ case OPCODE_LINE: {
+ CHECK_SPACE(2);
+
+ line=_code_ptr[ip+1];
+ ip+=2;
+
+ if (ScriptDebugger::get_singleton()) {
+ // line
+ bool do_break=false;
+
+ if (ScriptDebugger::get_singleton()->get_lines_left()>0) {
+
+ if (ScriptDebugger::get_singleton()->get_depth()<=0)
+ ScriptDebugger::get_singleton()->set_lines_left( ScriptDebugger::get_singleton()->get_lines_left() -1 );
+ if (ScriptDebugger::get_singleton()->get_lines_left()<=0)
+ do_break=true;
+ }
+
+ if (ScriptDebugger::get_singleton()->is_breakpoint(line,source))
+ do_break=true;
+
+ if (do_break) {
+ GDScriptLanguage::get_singleton()->debug_break("Breakpoint",true);
+ }
+
+ ScriptDebugger::get_singleton()->line_poll();
+
+ }
+ } continue;
+ case OPCODE_END: {
+
+ exit_ok=true;
+ break;
+
+ } break;
+ default: {
+
+ err_text="Illegal opcode "+itos(_code_ptr[ip])+" at address "+itos(ip);
+ } break;
+
+ }
+
+ if (exit_ok)
+ break;
+ //error
+ // function, file, line, error, explanation
+ String err_file;
+ if (p_instance)
+ err_file=p_instance->script->path;
+ else if (_class)
+ err_file=_class->path;
+ if (err_file=="")
+ err_file="<built-in>";
+ String err_func = name;
+ if (p_instance && p_instance->script->name!="")
+ err_func=p_instance->script->name+"."+err_func;
+ int err_line=line;
+ if (err_text=="") {
+ err_text="Internal Script Error! - opcode #"+itos(last_opcode)+" (report please).";
+ }
+
+ if (!GDScriptLanguage::get_singleton()->debug_break(err_text,false)) {
+ // debugger break did not happen
+
+ _err_print_error(err_func.utf8().get_data(),err_file.utf8().get_data(),err_line,err_text.utf8().get_data(),ERR_HANDLER_SCRIPT);
+ }
+
+
+ break;
+ }
+
+#ifdef DEBUG_ENABLED
+ if (GDScriptLanguage::get_singleton()->profiling) {
+ uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
+ profile.total_time+=time_taken;
+ profile.self_time+=time_taken-function_call_time;
+ profile.frame_total_time+=time_taken;
+ profile.frame_self_time+=time_taken-function_call_time;
+ GDScriptLanguage::get_singleton()->script_frame_time+=time_taken-function_call_time;
+
+ }
+
+#endif
+ if (ScriptDebugger::get_singleton())
+ GDScriptLanguage::get_singleton()->exit_function();
+
+
+ if (_stack_size) {
+ //free stack
+ for(int i=0;i<_stack_size;i++)
+ stack[i].~Variant();
+ }
+
+ return retvalue;
+
+}
+
+const int* GDFunction::get_code() const {
+
+ return _code_ptr;
+}
+int GDFunction::get_code_size() const{
+
+ return _code_size;
+}
+
+Variant GDFunction::get_constant(int p_idx) const {
+
+ ERR_FAIL_INDEX_V(p_idx,constants.size(),"<errconst>");
+ return constants[p_idx];
+}
+
+StringName GDFunction::get_global_name(int p_idx) const {
+
+ ERR_FAIL_INDEX_V(p_idx,global_names.size(),"<errgname>");
+ return global_names[p_idx];
+}
+
+int GDFunction::get_default_argument_count() const {
+
+ return default_arguments.size();
+}
+int GDFunction::get_default_argument_addr(int p_arg) const{
+
+ ERR_FAIL_INDEX_V(p_arg,default_arguments.size(),-1);
+ return default_arguments[p_arg];
+}
+
+
+StringName GDFunction::get_name() const {
+
+ return name;
+}
+
+int GDFunction::get_max_stack_size() const {
+
+ return _stack_size;
+}
+
+struct _GDFKC {
+
+ int order;
+ List<int> pos;
+};
+
+struct _GDFKCS {
+
+ int order;
+ StringName id;
+ int pos;
+
+ bool operator<(const _GDFKCS &p_r) const {
+
+ return order<p_r.order;
+ }
+};
+
+void GDFunction::debug_get_stack_member_state(int p_line,List<Pair<StringName,int> > *r_stackvars) const {
+
+
+ int oc=0;
+ Map<StringName,_GDFKC> sdmap;
+ for( const List<StackDebug>::Element *E=stack_debug.front();E;E=E->next()) {
+
+ const StackDebug &sd=E->get();
+ if (sd.line>p_line)
+ break;
+
+ if (sd.added) {
+
+ if (!sdmap.has(sd.identifier)) {
+ _GDFKC d;
+ d.order=oc++;
+ d.pos.push_back(sd.pos);
+ sdmap[sd.identifier]=d;
+
+ } else {
+ sdmap[sd.identifier].pos.push_back(sd.pos);
+ }
+ } else {
+
+
+ ERR_CONTINUE(!sdmap.has(sd.identifier));
+
+ sdmap[sd.identifier].pos.pop_back();
+ if (sdmap[sd.identifier].pos.empty())
+ sdmap.erase(sd.identifier);
+ }
+
+ }
+
+
+ List<_GDFKCS> stackpositions;
+ for(Map<StringName,_GDFKC>::Element *E=sdmap.front();E;E=E->next() ) {
+
+ _GDFKCS spp;
+ spp.id=E->key();
+ spp.order=E->get().order;
+ spp.pos=E->get().pos.back()->get();
+ stackpositions.push_back(spp);
+ }
+
+ stackpositions.sort();
+
+ for(List<_GDFKCS>::Element *E=stackpositions.front();E;E=E->next()) {
+
+ Pair<StringName,int> p;
+ p.first=E->get().id;
+ p.second=E->get().pos;
+ r_stackvars->push_back(p);
+ }
+
+
+}
+
+#if 0
+void GDFunction::clear() {
+
+ name=StringName();
+ constants.clear();
+ _stack_size=0;
+ code.clear();
+ _constants_ptr=NULL;
+ _constant_count=0;
+ _global_names_ptr=NULL;
+ _global_names_count=0;
+ _code_ptr=NULL;
+ _code_size=0;
+
+}
+#endif
+GDFunction::GDFunction() : function_list(this) {
+
+ _stack_size=0;
+ _call_size=0;
+ name="<anonymous>";
+#ifdef DEBUG_ENABLED
+ _func_cname=NULL;
+
+ if (GDScriptLanguage::get_singleton()->lock) {
+ GDScriptLanguage::get_singleton()->lock->lock();
+ }
+ GDScriptLanguage::get_singleton()->function_list.add(&function_list);
+
+ if (GDScriptLanguage::get_singleton()->lock) {
+ GDScriptLanguage::get_singleton()->lock->unlock();
+ }
+
+ profile.call_count=0;
+ profile.self_time=0;
+ profile.total_time=0;
+ profile.frame_call_count=0;
+ profile.frame_self_time=0;
+ profile.frame_total_time=0;
+ profile.last_frame_call_count=0;
+ profile.last_frame_self_time=0;
+ profile.last_frame_total_time=0;
+
+#endif
+}
+
+GDFunction::~GDFunction() {
+#ifdef DEBUG_ENABLED
+ if (GDScriptLanguage::get_singleton()->lock) {
+ GDScriptLanguage::get_singleton()->lock->lock();
+ }
+ GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
+
+ if (GDScriptLanguage::get_singleton()->lock) {
+ GDScriptLanguage::get_singleton()->lock->unlock();
+ }
+#endif
+}
+
+/////////////////////
+
+
+Variant GDFunctionState::_signal_callback(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
+
+ Variant arg;
+ r_error.error=Variant::CallError::CALL_OK;
+
+ ERR_FAIL_COND_V(!function,Variant());
+
+ if (p_argcount==0) {
+ r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
+ r_error.argument=1;
+ return Variant();
+ } else if (p_argcount==1) {
+ //noooneee
+ } else if (p_argcount==2) {
+ arg=*p_args[0];
+ } else {
+ Array extra_args;
+ for(int i=0;i<p_argcount-1;i++) {
+ extra_args.push_back(*p_args[i]);
+ }
+ arg=extra_args;
+ }
+
+ Ref<GDFunctionState> self = *p_args[p_argcount-1];
+
+ if (self.is_null()) {
+ r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+ r_error.argument=p_argcount-1;
+ r_error.expected=Variant::OBJECT;
+ return Variant();
+ }
+
+ state.result=arg;
+ Variant ret = function->call(NULL,NULL,0,r_error,&state);
+ function=NULL; //cleaned up;
+ state.result=Variant();
+ return ret;
+}
+
+
+bool GDFunctionState::is_valid() const {
+
+ return function!=NULL;
+}
+
+Variant GDFunctionState::resume(const Variant& p_arg) {
+
+ ERR_FAIL_COND_V(!function,Variant());
+
+ state.result=p_arg;
+ Variant::CallError err;
+ Variant ret = function->call(NULL,NULL,0,err,&state);
+ function=NULL; //cleaned up;
+ state.result=Variant();
+ return ret;
+}
+
+
+void GDFunctionState::_bind_methods() {
+
+ ObjectTypeDB::bind_method(_MD("resume:Variant","arg"),&GDFunctionState::resume,DEFVAL(Variant()));
+ ObjectTypeDB::bind_method(_MD("is_valid"),&GDFunctionState::is_valid);
+ ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"_signal_callback",&GDFunctionState::_signal_callback,MethodInfo("_signal_callback"));
+
+}
+
+GDFunctionState::GDFunctionState() {
+
+ function=NULL;
+}
+
+GDFunctionState::~GDFunctionState() {
+
+ if (function!=NULL) {
+ //never called, deinitialize stack
+ for(int i=0;i<state.stack_size;i++) {
+ Variant *v=(Variant*)&state.stack[sizeof(Variant)*i];
+ v->~Variant();
+ }
+ }
+}
+
diff --git a/modules/gdscript/gd_function.h b/modules/gdscript/gd_function.h
new file mode 100644
index 0000000000..1f790eaadc
--- /dev/null
+++ b/modules/gdscript/gd_function.h
@@ -0,0 +1,207 @@
+#ifndef GD_FUNCTION_H
+#define GD_FUNCTION_H
+
+#include "self_list.h"
+#include "os/thread.h"
+#include "pair.h"
+#include "variant.h"
+#include "string_db.h"
+#include "reference.h"
+
+class GDInstance;
+class GDScript;
+
+
+class GDFunction {
+public:
+
+ enum Opcode {
+ OPCODE_OPERATOR,
+ OPCODE_EXTENDS_TEST,
+ OPCODE_SET,
+ OPCODE_GET,
+ OPCODE_SET_NAMED,
+ OPCODE_GET_NAMED,
+ OPCODE_ASSIGN,
+ OPCODE_ASSIGN_TRUE,
+ OPCODE_ASSIGN_FALSE,
+ OPCODE_CONSTRUCT, //only for basic types!!
+ OPCODE_CONSTRUCT_ARRAY,
+ OPCODE_CONSTRUCT_DICTIONARY,
+ OPCODE_CALL,
+ OPCODE_CALL_RETURN,
+ OPCODE_CALL_BUILT_IN,
+ OPCODE_CALL_SELF,
+ OPCODE_CALL_SELF_BASE,
+ OPCODE_YIELD,
+ OPCODE_YIELD_SIGNAL,
+ OPCODE_YIELD_RESUME,
+ OPCODE_JUMP,
+ OPCODE_JUMP_IF,
+ OPCODE_JUMP_IF_NOT,
+ OPCODE_JUMP_TO_DEF_ARGUMENT,
+ OPCODE_RETURN,
+ OPCODE_ITERATE_BEGIN,
+ OPCODE_ITERATE,
+ OPCODE_ASSERT,
+ OPCODE_BREAKPOINT,
+ OPCODE_LINE,
+ OPCODE_END
+ };
+
+ enum Address {
+ ADDR_BITS=24,
+ ADDR_MASK=((1<<ADDR_BITS)-1),
+ ADDR_TYPE_MASK=~ADDR_MASK,
+ ADDR_TYPE_SELF=0,
+ ADDR_TYPE_CLASS=1,
+ ADDR_TYPE_MEMBER=2,
+ ADDR_TYPE_CLASS_CONSTANT=3,
+ ADDR_TYPE_LOCAL_CONSTANT=4,
+ ADDR_TYPE_STACK=5,
+ ADDR_TYPE_STACK_VARIABLE=6,
+ ADDR_TYPE_GLOBAL=7,
+ ADDR_TYPE_NIL=8
+ };
+
+ struct StackDebug {
+
+ int line;
+ int pos;
+ bool added;
+ StringName identifier;
+ };
+
+private:
+friend class GDCompiler;
+
+ StringName source;
+
+ mutable Variant nil;
+ mutable Variant *_constants_ptr;
+ int _constant_count;
+ const StringName *_global_names_ptr;
+ int _global_names_count;
+ const int *_default_arg_ptr;
+ int _default_arg_count;
+ const int *_code_ptr;
+ int _code_size;
+ int _argument_count;
+ int _stack_size;
+ int _call_size;
+ int _initial_line;
+ bool _static;
+ GDScript *_script;
+
+ StringName name;
+ Vector<Variant> constants;
+ Vector<StringName> global_names;
+ Vector<int> default_arguments;
+ Vector<int> code;
+
+#ifdef TOOLS_ENABLED
+ Vector<StringName> arg_names;
+#endif
+
+ List<StackDebug> stack_debug;
+
+ _FORCE_INLINE_ Variant *_get_variant(int p_address,GDInstance *p_instance,GDScript *p_script,Variant &self,Variant *p_stack,String& r_error) const;
+ _FORCE_INLINE_ String _get_call_error(const Variant::CallError& p_err, const String& p_where,const Variant**argptrs) const;
+
+friend class GDScriptLanguage;
+
+ SelfList<GDFunction> function_list;
+#ifdef DEBUG_ENABLED
+ CharString func_cname;
+ const char*_func_cname;
+
+ struct Profile {
+ StringName signature;
+ uint64_t call_count;
+ uint64_t self_time;
+ uint64_t total_time;
+ uint64_t frame_call_count;
+ uint64_t frame_self_time;
+ uint64_t frame_total_time;
+ uint64_t last_frame_call_count;
+ uint64_t last_frame_self_time;
+ uint64_t last_frame_total_time;
+ } profile;
+
+#endif
+
+public:
+
+
+
+ struct CallState {
+
+ GDInstance *instance;
+ Vector<uint8_t> stack;
+ int stack_size;
+ Variant self;
+ uint32_t alloca_size;
+ GDScript *_class;
+ int ip;
+ int line;
+ int defarg;
+ Variant result;
+
+ };
+
+ _FORCE_INLINE_ bool is_static() const { return _static; }
+
+ const int* get_code() const; //used for debug
+ int get_code_size() const;
+ Variant get_constant(int p_idx) const;
+ StringName get_global_name(int p_idx) const;
+ StringName get_name() const;
+ int get_max_stack_size() const;
+ int get_default_argument_count() const;
+ int get_default_argument_addr(int p_idx) const;
+ GDScript *get_script() const { return _script; }
+
+ void debug_get_stack_member_state(int p_line,List<Pair<StringName,int> > *r_stackvars) const;
+
+ _FORCE_INLINE_ bool is_empty() const { return _code_size==0; }
+
+ int get_argument_count() const { return _argument_count; }
+ StringName get_argument_name(int p_idx) const {
+#ifdef TOOLS_ENABLED
+ ERR_FAIL_INDEX_V(p_idx,arg_names.size(),StringName());
+ return arg_names[p_idx];
+#endif
+ return StringName();
+
+ }
+ Variant get_default_argument(int p_idx) const {
+ ERR_FAIL_INDEX_V(p_idx,default_arguments.size(),Variant());
+ return default_arguments[p_idx];
+ }
+
+ Variant call(GDInstance *p_instance,const Variant **p_args, int p_argcount,Variant::CallError& r_err,CallState *p_state=NULL);
+
+ GDFunction();
+ ~GDFunction();
+};
+
+
+class GDFunctionState : public Reference {
+
+ OBJ_TYPE(GDFunctionState,Reference);
+friend class GDFunction;
+ GDFunction *function;
+ GDFunction::CallState state;
+ Variant _signal_callback(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
+protected:
+ static void _bind_methods();
+public:
+
+ bool is_valid() const;
+ Variant resume(const Variant& p_arg=Variant());
+ GDFunctionState();
+ ~GDFunctionState();
+};
+
+
+#endif // GD_FUNCTION_H
diff --git a/modules/gdscript/gd_functions.cpp b/modules/gdscript/gd_functions.cpp
index 1c05a71d01..ec66841662 100644
--- a/modules/gdscript/gd_functions.cpp
+++ b/modules/gdscript/gd_functions.cpp
@@ -87,6 +87,7 @@ const char *GDFunctions::get_func_name(Function p_func) {
"funcref",
"convert",
"typeof",
+ "type_exists",
"str",
"print",
"printt",
@@ -120,11 +121,13 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
if (p_arg_count<m_count) {\
r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;\
r_error.argument=m_count;\
+ r_ret=Variant();\
return;\
}\
if (p_arg_count>m_count) {\
r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;\
r_error.argument=m_count;\
+ r_ret=Variant();\
return;\
}
@@ -133,6 +136,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;\
r_error.argument=m_arg;\
r_error.expected=Variant::REAL;\
+ r_ret=Variant();\
return;\
}
@@ -244,6 +248,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::REAL;
+ r_ret=Variant();
}
} break;
case MATH_SIGN: {
@@ -261,6 +266,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::REAL;
+ r_ret=Variant();
}
} break;
case MATH_POW: {
@@ -442,6 +448,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::OBJECT;
+ r_ret=Variant();
return;
}
@@ -479,7 +486,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::OBJECT;
- r_ret=Variant();
+ r_ret=Variant();
return;
}
@@ -508,8 +515,11 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
int type=*p_args[1];
if (type<0 || type>=Variant::VARIANT_MAX) {
- ERR_PRINT("Invalid type argument to convert()");
- r_ret=Variant::NIL;
+ r_ret=RTR("Invalid type argument to convert(), use TYPE_* constants.");
+ r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+ r_error.argument=0;
+ r_error.expected=Variant::INT;
+ return;
} else {
@@ -523,6 +533,12 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_ret = p_args[0]->get_type();
} break;
+ case TYPE_EXISTS: {
+
+ VALIDATE_ARG_COUNT(1);
+ r_ret = ObjectTypeDB::type_exists(*p_args[0]);
+
+ } break;
case TEXT_STR: {
String str;
@@ -638,7 +654,8 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::STRING;
- r_ret=Variant();
+ r_ret="Parse error at line "+itos(line)+": "+errs;
+ return;
}
} break;
@@ -652,7 +669,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::NIL;
- r_ret=Variant();
+ r_ret="Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).";
return;
}
@@ -680,11 +697,10 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
ByteArray::Read r=varr.read();
Error err = decode_variant(ret,r.ptr(),varr.size(),NULL);
if (err!=OK) {
- ERR_PRINT("Not enough bytes for decoding..");
+ r_ret=RTR("Not enough bytes for decoding bytes, or invalid format.");
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::RAW_ARRAY;
- r_ret=Variant();
return;
}
@@ -701,6 +717,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.argument=1;
+ r_ret=Variant();
} break;
case 1: {
@@ -759,9 +776,9 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
int incr=*p_args[2];
if (incr==0) {
- ERR_EXPLAIN("step argument is zero!");
+ r_ret=RTR("step argument is zero!");
r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
- ERR_FAIL();
+ return;
}
Array arr(true);
@@ -812,6 +829,8 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
r_error.argument=3;
+ r_ret=Variant();
+
} break;
}
@@ -847,8 +866,8 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::DICTIONARY;
- ERR_PRINT("Not a script with an instance");
-
+ r_ret=RTR("Not a script with an instance");
+ return;
} else {
GDInstance *ins = static_cast<GDInstance*>(obj->get_script_instance());
@@ -858,7 +877,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::DICTIONARY;
- ERR_PRINT("Not based on a script");
+ r_ret=RTR("Not based on a script");
return;
}
@@ -879,8 +898,10 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::DICTIONARY;
- print_line("PATH: "+p->path);
- ERR_PRINT("Not based on a resource file");
+ r_ret=Variant();
+
+
+ r_ret=RTR("Not based on a resource file");
return;
}
@@ -926,6 +947,8 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::DICTIONARY;
+ r_ret=Variant();
+
return;
}
@@ -936,6 +959,8 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::OBJECT;
+ r_ret=RTR("Invalid instance dictionary format (missing @path)");
+
return;
}
@@ -945,6 +970,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::OBJECT;
+ r_ret=RTR("Invalid instance dictionary format (can't load script at @path)");
return;
}
@@ -955,6 +981,8 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::OBJECT;
+ r_ret=Variant();
+ r_ret=RTR("Invalid instance dictionary format (invalid script at @path)");
return;
}
@@ -971,20 +999,22 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::OBJECT;
+ r_ret=Variant();
+ r_ret=RTR("Invalid instance dictionary (invalid subclasses)");
return;
}
}
r_ret = gdscr->_new(NULL,0,r_error);
- GDInstance *ins = static_cast<GDInstance*>(static_cast<Object*>(r_ret)->get_script_instance());
- Ref<GDScript> gd_ref = ins->get_script();
+ GDInstance *ins = static_cast<GDInstance*>(static_cast<Object*>(r_ret)->get_script_instance());
+ Ref<GDScript> gd_ref = ins->get_script();
- for(Map<StringName,GDScript::MemberInfo>::Element *E = gd_ref->member_indices.front(); E; E = E->next()) {
- if(d.has(E->key())) {
- ins->members[E->get().index] = d[E->key()];
- }
- }
+ for(Map<StringName,GDScript::MemberInfo>::Element *E = gd_ref->member_indices.front(); E; E = E->next()) {
+ if(d.has(E->key())) {
+ ins->members[E->get().index] = d[E->key()];
+ }
+ }
} break;
case HASH: {
@@ -998,11 +1028,15 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
if (p_arg_count<3) {
r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.argument=3;
+ r_ret=Variant();
+
return;
}
if (p_arg_count>4) {
r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
r_error.argument=4;
+ r_ret=Variant();
+
return;
}
@@ -1010,11 +1044,11 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
- Color color(*p_args[0],*p_args[1],*p_args[2]);
+ Color color((float)*p_args[0]/255.0f,(float)*p_args[1]/255.0f,(float)*p_args[2]/255.0f);
if (p_arg_count==4) {
VALIDATE_ARG_NUM(3);
- color.a=*p_args[3];
+ color.a=(float)*p_args[3]/255.0f;
}
r_ret=color;
@@ -1036,6 +1070,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
if (p_args[0]->get_type()!=Variant::INT && p_args[0]->get_type()!=Variant::REAL) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
+ r_error.expected=Variant::INT;
r_ret=Variant();
break;
}
@@ -1098,6 +1133,7 @@ bool GDFunctions::is_deterministic(Function p_func) {
case LOGIC_NEAREST_PO2:
case TYPE_CONVERT:
case TYPE_OF:
+ case TYPE_EXISTS:
case TEXT_STR:
case COLOR8:
// enable for debug only, otherwise not desirable - case GEN_RANGE:
@@ -1281,12 +1317,12 @@ MethodInfo GDFunctions::get_info(Function p_func) {
return mi;
} break;
case MATH_SEED: {
- MethodInfo mi("seed",PropertyInfo(Variant::REAL,"seed"));
+ MethodInfo mi("seed",PropertyInfo(Variant::INT,"seed"));
mi.return_val.type=Variant::NIL;
return mi;
} break;
case MATH_RANDSEED: {
- MethodInfo mi("rand_seed",PropertyInfo(Variant::REAL,"seed"));
+ MethodInfo mi("rand_seed",PropertyInfo(Variant::INT,"seed"));
mi.return_val.type=Variant::ARRAY;
return mi;
} break;
@@ -1361,6 +1397,13 @@ MethodInfo GDFunctions::get_info(Function p_func) {
return mi;
} break;
+ case TYPE_EXISTS: {
+
+ MethodInfo mi("type_exists",PropertyInfo(Variant::STRING,"type"));
+ mi.return_val.type=Variant::BOOL;
+ return mi;
+
+ } break;
case TEXT_STR: {
MethodInfo mi("str",PropertyInfo(Variant::NIL,"what"),PropertyInfo(Variant::NIL,"..."));
diff --git a/modules/gdscript/gd_functions.h b/modules/gdscript/gd_functions.h
index 8c88472567..c78956fe20 100644
--- a/modules/gdscript/gd_functions.h
+++ b/modules/gdscript/gd_functions.h
@@ -81,6 +81,7 @@ public:
FUNC_FUNCREF,
TYPE_CONVERT,
TYPE_OF,
+ TYPE_EXISTS,
TEXT_STR,
TEXT_PRINT,
TEXT_PRINT_TABBED,
diff --git a/modules/gdscript/gd_parser.cpp b/modules/gdscript/gd_parser.cpp
index b713dc318f..ac96a2117c 100644
--- a/modules/gdscript/gd_parser.cpp
+++ b/modules/gdscript/gd_parser.cpp
@@ -86,7 +86,7 @@ bool GDParser::_enter_indent_block(BlockNode* p_block) {
while(true) {
if (tokenizer->get_token()!=GDTokenizer::TK_NEWLINE) {
- print_line("no newline");
+
return false; //wtf
} else if (tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE) {
@@ -730,7 +730,7 @@ GDParser::Node* GDParser::_parse_expression(Node *p_parent,bool p_static,bool p_
//find list [ or find dictionary {
- print_line("found bug?");
+ //print_line("found bug?");
_set_error("Error parsing expression, misplaced: "+String(tokenizer->get_token_name(tokenizer->get_token())));
return NULL; //nothing
@@ -2408,6 +2408,7 @@ void GDParser::_parse_class(ClassNode *p_class) {
return;
}
current_export.type=type;
+ current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
tokenizer->advance();
if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
// hint expected next!
@@ -2782,6 +2783,8 @@ void GDParser::_parse_class(ClassNode *p_class) {
current_export.type=Variant::OBJECT;
current_export.hint=PROPERTY_HINT_RESOURCE_TYPE;
+ current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
+
current_export.hint_string=identifier;
tokenizer->advance();
@@ -2901,6 +2904,7 @@ void GDParser::_parse_class(ClassNode *p_class) {
return;
}
member._export.type=cn->value.get_type();
+ member._export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
}
}
#ifdef TOOLS_ENABLED
diff --git a/modules/gdscript/gd_pretty_print.cpp b/modules/gdscript/gd_pretty_print.cpp
deleted file mode 100644
index cca3cd3984..0000000000
--- a/modules/gdscript/gd_pretty_print.cpp
+++ /dev/null
@@ -1,34 +0,0 @@
-/*************************************************************************/
-/* gd_pretty_print.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* http://www.godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2016 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. */
-/*************************************************************************/
-#include "gd_pretty_print.h"
-
-GDPrettyPrint::GDPrettyPrint() {
-
-
-}
diff --git a/modules/gdscript/gd_pretty_print.h b/modules/gdscript/gd_pretty_print.h
deleted file mode 100644
index 0106d873d9..0000000000
--- a/modules/gdscript/gd_pretty_print.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*************************************************************************/
-/* gd_pretty_print.h */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* http://www.godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2016 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. */
-/*************************************************************************/
-#ifndef GD_PRETTY_PRINT_H
-#define GD_PRETTY_PRINT_H
-
-
-
-
-class GDPrettyPrint {
-public:
- GDPrettyPrint();
-};
-
-#endif // GD_PRETTY_PRINT_H
diff --git a/modules/gdscript/gd_script.cpp b/modules/gdscript/gd_script.cpp
index beec314e44..026fd04869 100644
--- a/modules/gdscript/gd_script.cpp
+++ b/modules/gdscript/gd_script.cpp
@@ -32,1363 +32,7 @@
#include "gd_compiler.h"
#include "os/file_access.h"
#include "io/file_access_encrypted.h"
-
-
-
-
-Variant *GDFunction::_get_variant(int p_address,GDInstance *p_instance,GDScript *p_script,Variant &self, Variant *p_stack,String& r_error) const{
-
- int address = p_address&ADDR_MASK;
-
- //sequential table (jump table generated by compiler)
- switch((p_address&ADDR_TYPE_MASK)>>ADDR_BITS) {
-
- case ADDR_TYPE_SELF: {
-
- if (!p_instance) {
- r_error="Cannot access self without instance.";
- return NULL;
- }
- return &self;
- } break;
- case ADDR_TYPE_CLASS: {
-
- return &p_script->_static_ref;
- } break;
- case ADDR_TYPE_MEMBER: {
- //member indexing is O(1)
- if (!p_instance) {
- r_error="Cannot access member without instance.";
- return NULL;
- }
- return &p_instance->members[address];
- } break;
- case ADDR_TYPE_CLASS_CONSTANT: {
-
- //todo change to index!
- GDScript *o=p_script;
- ERR_FAIL_INDEX_V(address,_global_names_count,NULL);
- const StringName *sn = &_global_names_ptr[address];
-
- while(o) {
- GDScript *s=o;
- while(s) {
-
- Map<StringName,Variant>::Element *E=s->constants.find(*sn);
- if (E) {
- return &E->get();
- }
- s=s->_base;
- }
- o=o->_owner;
- }
-
-
- ERR_EXPLAIN("GDCompiler bug..");
- ERR_FAIL_V(NULL);
- } break;
- case ADDR_TYPE_LOCAL_CONSTANT: {
- ERR_FAIL_INDEX_V(address,_constant_count,NULL);
- return &_constants_ptr[address];
- } break;
- case ADDR_TYPE_STACK:
- case ADDR_TYPE_STACK_VARIABLE: {
- ERR_FAIL_INDEX_V(address,_stack_size,NULL);
- return &p_stack[address];
- } break;
- case ADDR_TYPE_GLOBAL: {
-
-
- ERR_FAIL_INDEX_V(address,GDScriptLanguage::get_singleton()->get_global_array_size(),NULL);
-
-
- return &GDScriptLanguage::get_singleton()->get_global_array()[address];
- } break;
- case ADDR_TYPE_NIL: {
- return &nil;
- } break;
- }
-
- ERR_EXPLAIN("Bad Code! (Addressing Mode)");
- ERR_FAIL_V(NULL);
- return NULL;
-}
-
-
-String GDFunction::_get_call_error(const Variant::CallError& p_err, const String& p_where,const Variant**argptrs) const {
-
-
-
- String err_text;
-
- if (p_err.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
- int errorarg=p_err.argument;
- err_text="Invalid type in "+p_where+". Cannot convert argument "+itos(errorarg+1)+" from "+Variant::get_type_name(argptrs[errorarg]->get_type())+" to "+Variant::get_type_name(p_err.expected)+".";
- } else if (p_err.error==Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
- err_text="Invalid call to "+p_where+". Expected "+itos(p_err.argument)+" arguments.";
- } else if (p_err.error==Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
- err_text="Invalid call to "+p_where+". Expected "+itos(p_err.argument)+" arguments.";
- } else if (p_err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
- err_text="Invalid call. Nonexistent "+p_where+".";
- } else if (p_err.error==Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
- err_text="Attempt to call "+p_where+" on a null instance.";
- } else {
- err_text="Bug, call error: #"+itos(p_err.error);
- }
-
- return err_text;
-
-}
-
-static String _get_var_type(const Variant* p_type) {
-
- String basestr;
-
- if (p_type->get_type()==Variant::OBJECT) {
- Object *bobj = *p_type;
- if (!bobj) {
- basestr = "null instance";
- } else {
-#ifdef DEBUG_ENABLED
- if (ObjectDB::instance_validate(bobj)) {
- if (bobj->get_script_instance())
- basestr= bobj->get_type()+" ("+bobj->get_script_instance()->get_script()->get_path().get_file()+")";
- else
- basestr = bobj->get_type();
- } else {
- basestr="previously freed instance";
- }
-
-#else
- basestr="Object";
-#endif
- }
-
- } else {
- basestr = Variant::get_type_name(p_type->get_type());
- }
-
- return basestr;
-
-}
-
-Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError& r_err, CallState *p_state) {
-
-
- if (!_code_ptr) {
-
- return Variant();
- }
-
- r_err.error=Variant::CallError::CALL_OK;
-
- Variant self;
- Variant retvalue;
- Variant *stack = NULL;
- Variant **call_args;
- int defarg=0;
-
-#ifdef DEBUG_ENABLED
-
- //GDScriptLanguage::get_singleton()->calls++;
-
-#endif
-
- uint32_t alloca_size=0;
- GDScript *_class;
- int ip=0;
- int line=_initial_line;
-
- if (p_state) {
- //use existing (supplied) state (yielded)
- stack=(Variant*)p_state->stack.ptr();
- call_args=(Variant**)&p_state->stack[sizeof(Variant)*p_state->stack_size];
- line=p_state->line;
- ip=p_state->ip;
- alloca_size=p_state->stack.size();
- _class=p_state->_class;
- p_instance=p_state->instance;
- defarg=p_state->defarg;
- self=p_state->self;
- //stack[p_state->result_pos]=p_state->result; //assign stack with result
-
- } else {
-
- if (p_argcount!=_argument_count) {
-
- if (p_argcount>_argument_count) {
-
- r_err.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
- r_err.argument=_argument_count;
-
-
- return Variant();
- } else if (p_argcount < _argument_count - _default_arg_count) {
-
- r_err.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
- r_err.argument=_argument_count - _default_arg_count;
- return Variant();
- } else {
-
- defarg=_argument_count-p_argcount;
- }
- }
-
- alloca_size = sizeof(Variant*)*_call_size + sizeof(Variant)*_stack_size;
-
- if (alloca_size) {
-
- uint8_t *aptr = (uint8_t*)alloca(alloca_size);
-
- if (_stack_size) {
-
- stack=(Variant*)aptr;
- for(int i=0;i<p_argcount;i++)
- memnew_placement(&stack[i],Variant(*p_args[i]));
- for(int i=p_argcount;i<_stack_size;i++)
- memnew_placement(&stack[i],Variant);
- } else {
- stack=NULL;
- }
-
- if (_call_size) {
-
- call_args = (Variant**)&aptr[sizeof(Variant)*_stack_size];
- } else {
-
- call_args=NULL;
- }
-
-
- } else {
- stack=NULL;
- call_args=NULL;
- }
-
- if (p_instance) {
- if (p_instance->base_ref && static_cast<Reference*>(p_instance->owner)->is_referenced()) {
-
- self=REF(static_cast<Reference*>(p_instance->owner));
- } else {
- self=p_instance->owner;
- }
- _class=p_instance->script.ptr();
- } else {
- _class=_script;
- }
- }
-
- String err_text;
-
-#ifdef DEBUG_ENABLED
-
- if (ScriptDebugger::get_singleton())
- GDScriptLanguage::get_singleton()->enter_function(p_instance,this,stack,&ip,&line);
-
-#define CHECK_SPACE(m_space)\
- ERR_BREAK((ip+m_space)>_code_size)
-
-#define GET_VARIANT_PTR(m_v,m_code_ofs) \
- Variant *m_v; \
- m_v = _get_variant(_code_ptr[ip+m_code_ofs],p_instance,_class,self,stack,err_text);\
- if (!m_v)\
- break;
-
-
-#else
-#define CHECK_SPACE(m_space)
-#define GET_VARIANT_PTR(m_v,m_code_ofs) \
- Variant *m_v; \
- m_v = _get_variant(_code_ptr[ip+m_code_ofs],p_instance,_class,self,stack,err_text);
-
-#endif
-
-
-
- bool exit_ok=false;
-
- while(ip<_code_size) {
-
-
- int last_opcode=_code_ptr[ip];
- switch(_code_ptr[ip]) {
-
- case OPCODE_OPERATOR: {
-
- CHECK_SPACE(5);
-
- bool valid;
- Variant::Operator op = (Variant::Operator)_code_ptr[ip+1];
- ERR_BREAK(op>=Variant::OP_MAX);
-
- GET_VARIANT_PTR(a,2);
- GET_VARIANT_PTR(b,3);
- GET_VARIANT_PTR(dst,4);
-
-#ifdef DEBUG_ENABLED
- Variant ret;
- Variant::evaluate(op,*a,*b,ret,valid);
-#else
- Variant::evaluate(op,*a,*b,*dst,valid);
-#endif
-
- if (!valid) {
-#ifdef DEBUG_ENABLED
-
- if (ret.get_type()==Variant::STRING) {
- //return a string when invalid with the error
- err_text=ret;
- err_text += " in operator '"+Variant::get_operator_name(op)+"'.";
- } else {
- err_text="Invalid operands '"+Variant::get_type_name(a->get_type())+"' and '"+Variant::get_type_name(b->get_type())+"' in operator '"+Variant::get_operator_name(op)+"'.";
- }
-#endif
- break;
-
- }
-#ifdef DEBUG_ENABLED
- *dst=ret;
-#endif
-
- ip+=5;
-
- } continue;
- case OPCODE_EXTENDS_TEST: {
-
- CHECK_SPACE(4);
-
- GET_VARIANT_PTR(a,1);
- GET_VARIANT_PTR(b,2);
- GET_VARIANT_PTR(dst,3);
-
-#ifdef DEBUG_ENABLED
-
- if (a->get_type()!=Variant::OBJECT || a->operator Object*()==NULL) {
-
- err_text="Left operand of 'extends' is not an instance of anything.";
- break;
-
- }
- if (b->get_type()!=Variant::OBJECT || b->operator Object*()==NULL) {
-
- err_text="Right operand of 'extends' is not a class.";
- break;
-
- }
-#endif
-
-
- Object *obj_A = *a;
- Object *obj_B = *b;
-
-
- GDScript *scr_B = obj_B->cast_to<GDScript>();
-
- bool extends_ok=false;
-
- if (scr_B) {
- //if B is a script, the only valid condition is that A has an instance which inherits from the script
- //in other situation, this shoul return false.
-
- if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language()==GDScriptLanguage::get_singleton()) {
-
- GDInstance *ins = static_cast<GDInstance*>(obj_A->get_script_instance());
- GDScript *cmp = ins->script.ptr();
- //bool found=false;
- while(cmp) {
-
- if (cmp==scr_B) {
- //inherits from script, all ok
- extends_ok=true;
- break;
-
- }
-
- cmp=cmp->_base;
- }
-
- }
-
-
- } else {
-
- GDNativeClass *nc= obj_B->cast_to<GDNativeClass>();
-
- if (!nc) {
-
- err_text="Right operand of 'extends' is not a class (type: '"+obj_B->get_type()+"').";
- break;
- }
-
- extends_ok=ObjectTypeDB::is_type(obj_A->get_type_name(),nc->get_name());
- }
-
- *dst=extends_ok;
- ip+=4;
-
- } continue;
- case OPCODE_SET: {
-
- CHECK_SPACE(3);
-
- GET_VARIANT_PTR(dst,1);
- GET_VARIANT_PTR(index,2);
- GET_VARIANT_PTR(value,3);
-
- bool valid;
- dst->set(*index,*value,&valid);
-
- if (!valid) {
- String v = index->operator String();
- if (v!="") {
- v="'"+v+"'";
- } else {
- v="of type '"+_get_var_type(index)+"'";
- }
- err_text="Invalid set index "+v+" (on base: '"+_get_var_type(dst)+"').";
- break;
- }
-
- ip+=4;
- } continue;
- case OPCODE_GET: {
-
- CHECK_SPACE(3);
-
- GET_VARIANT_PTR(src,1);
- GET_VARIANT_PTR(index,2);
- GET_VARIANT_PTR(dst,3);
-
- bool valid;
-#ifdef DEBUG_ENABLED
-//allow better error message in cases where src and dst are the same stack position
- Variant ret = src->get(*index,&valid);
-#else
- *dst = src->get(*index,&valid);
-
-#endif
- if (!valid) {
- String v = index->operator String();
- if (v!="") {
- v="'"+v+"'";
- } else {
- v="of type '"+_get_var_type(index)+"'";
- }
- err_text="Invalid get index "+v+" (on base: '"+_get_var_type(src)+"').";
- break;
- }
-#ifdef DEBUG_ENABLED
- *dst=ret;
-#endif
- ip+=4;
- } continue;
- case OPCODE_SET_NAMED: {
-
- CHECK_SPACE(3);
-
- GET_VARIANT_PTR(dst,1);
- GET_VARIANT_PTR(value,3);
-
- int indexname = _code_ptr[ip+2];
-
- ERR_BREAK(indexname<0 || indexname>=_global_names_count);
- const StringName *index = &_global_names_ptr[indexname];
-
- bool valid;
- dst->set_named(*index,*value,&valid);
-
- if (!valid) {
- String err_type;
- err_text="Invalid set index '"+String(*index)+"' (on base: '"+_get_var_type(dst)+"').";
- break;
- }
-
- ip+=4;
- } continue;
- case OPCODE_GET_NAMED: {
-
-
- CHECK_SPACE(3);
-
- GET_VARIANT_PTR(src,1);
- GET_VARIANT_PTR(dst,3);
-
- int indexname = _code_ptr[ip+2];
-
- ERR_BREAK(indexname<0 || indexname>=_global_names_count);
- const StringName *index = &_global_names_ptr[indexname];
-
- bool valid;
-#ifdef DEBUG_ENABLED
-//allow better error message in cases where src and dst are the same stack position
- Variant ret = src->get_named(*index,&valid);
-
-#else
- *dst = src->get_named(*index,&valid);
-#endif
-
- if (!valid) {
- if (src->has_method(*index)) {
- err_text="Invalid get index '"+index->operator String()+"' (on base: '"+_get_var_type(src)+"'). Did you mean '."+index->operator String()+"()' ?";
- } else {
- err_text="Invalid get index '"+index->operator String()+"' (on base: '"+_get_var_type(src)+"').";
- }
- break;
- }
-#ifdef DEBUG_ENABLED
- *dst=ret;
-#endif
- ip+=4;
- } continue;
- case OPCODE_ASSIGN: {
-
- CHECK_SPACE(3);
- GET_VARIANT_PTR(dst,1);
- GET_VARIANT_PTR(src,2);
-
- *dst = *src;
-
- ip+=3;
-
- } continue;
- case OPCODE_ASSIGN_TRUE: {
-
- CHECK_SPACE(2);
- GET_VARIANT_PTR(dst,1);
-
- *dst = true;
-
- ip+=2;
- } continue;
- case OPCODE_ASSIGN_FALSE: {
-
- CHECK_SPACE(2);
- GET_VARIANT_PTR(dst,1);
-
- *dst = false;
-
- ip+=2;
- } continue;
- case OPCODE_CONSTRUCT: {
-
- CHECK_SPACE(2);
- Variant::Type t=Variant::Type(_code_ptr[ip+1]);
- int argc=_code_ptr[ip+2];
- CHECK_SPACE(argc+2);
- Variant **argptrs = call_args;
- for(int i=0;i<argc;i++) {
- GET_VARIANT_PTR(v,3+i);
- argptrs[i]=v;
- }
-
- GET_VARIANT_PTR(dst,3+argc);
- Variant::CallError err;
- *dst = Variant::construct(t,(const Variant**)argptrs,argc,err);
-
- if (err.error!=Variant::CallError::CALL_OK) {
-
- err_text=_get_call_error(err,"'"+Variant::get_type_name(t)+"' constructor",(const Variant**)argptrs);
- break;
- }
-
- ip+=4+argc;
- //construct a basic type
- } continue;
- case OPCODE_CONSTRUCT_ARRAY: {
-
- CHECK_SPACE(1);
- int argc=_code_ptr[ip+1];
- Array array(true); //arrays are always shared
- array.resize(argc);
- CHECK_SPACE(argc+2);
-
- for(int i=0;i<argc;i++) {
- GET_VARIANT_PTR(v,2+i);
- array[i]=*v;
-
- }
-
- GET_VARIANT_PTR(dst,2+argc);
-
- *dst=array;
-
- ip+=3+argc;
-
- } continue;
- case OPCODE_CONSTRUCT_DICTIONARY: {
-
- CHECK_SPACE(1);
- int argc=_code_ptr[ip+1];
- Dictionary dict(true); //arrays are always shared
-
- CHECK_SPACE(argc*2+2);
-
- for(int i=0;i<argc;i++) {
-
- GET_VARIANT_PTR(k,2+i*2+0);
- GET_VARIANT_PTR(v,2+i*2+1);
- dict[*k]=*v;
-
- }
-
- GET_VARIANT_PTR(dst,2+argc*2);
-
- *dst=dict;
-
- ip+=3+argc*2;
-
- } continue;
- case OPCODE_CALL_RETURN:
- case OPCODE_CALL: {
-
-
- CHECK_SPACE(4);
- bool call_ret = _code_ptr[ip]==OPCODE_CALL_RETURN;
-
- int argc=_code_ptr[ip+1];
- GET_VARIANT_PTR(base,2);
- int nameg=_code_ptr[ip+3];
-
- ERR_BREAK(nameg<0 || nameg>=_global_names_count);
- const StringName *methodname = &_global_names_ptr[nameg];
-
- ERR_BREAK(argc<0);
- ip+=4;
- CHECK_SPACE(argc+1);
- Variant **argptrs = call_args;
-
- for(int i=0;i<argc;i++) {
- GET_VARIANT_PTR(v,i);
- argptrs[i]=v;
- }
-
- Variant::CallError err;
- if (call_ret) {
-
- GET_VARIANT_PTR(ret,argc);
- *ret = base->call(*methodname,(const Variant**)argptrs,argc,err);
- } else {
-
- base->call(*methodname,(const Variant**)argptrs,argc,err);
- }
-
- if (err.error!=Variant::CallError::CALL_OK) {
-
-
- String methodstr = *methodname;
- String basestr = _get_var_type(base);
-
- if (methodstr=="call") {
- if (argc>=1) {
- methodstr=String(*argptrs[0])+" (via call)";
- if (err.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
- err.argument-=1;
- }
- }
- } if (methodstr=="free") {
-
- if (err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
-
- if (base->is_ref()) {
- err_text="Attempted to free a reference.";
- break;
- } else if (base->get_type()==Variant::OBJECT) {
-
- err_text="Attempted to free a locked object (calling or emitting).";
- break;
- }
- }
- }
- err_text=_get_call_error(err,"function '"+methodstr+"' in base '"+basestr+"'",(const Variant**)argptrs);
- break;
- }
-
- //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
- ip+=argc+1;
-
- } continue;
- case OPCODE_CALL_BUILT_IN: {
-
- CHECK_SPACE(4);
-
- GDFunctions::Function func = GDFunctions::Function(_code_ptr[ip+1]);
- int argc=_code_ptr[ip+2];
- ERR_BREAK(argc<0);
-
- ip+=3;
- CHECK_SPACE(argc+1);
- Variant **argptrs = call_args;
-
- for(int i=0;i<argc;i++) {
- GET_VARIANT_PTR(v,i);
- argptrs[i]=v;
- }
-
- GET_VARIANT_PTR(dst,argc);
-
- Variant::CallError err;
-
- GDFunctions::call(func,(const Variant**)argptrs,argc,*dst,err);
-
- if (err.error!=Variant::CallError::CALL_OK) {
-
-
- String methodstr = GDFunctions::get_func_name(func);
- err_text=_get_call_error(err,"built-in function '"+methodstr+"'",(const Variant**)argptrs);
- break;
- }
- ip+=argc+1;
-
- } continue;
- case OPCODE_CALL_SELF: {
-
-
- } break;
- case OPCODE_CALL_SELF_BASE: {
-
- CHECK_SPACE(2);
- int self_fun = _code_ptr[ip+1];
-#ifdef DEBUG_ENABLED
-
- if (self_fun<0 || self_fun>=_global_names_count) {
-
- err_text="compiler bug, function name not found";
- break;
- }
-#endif
- const StringName *methodname = &_global_names_ptr[self_fun];
-
- int argc=_code_ptr[ip+2];
-
- CHECK_SPACE(2+argc+1);
-
- Variant **argptrs = call_args;
-
- for(int i=0;i<argc;i++) {
- GET_VARIANT_PTR(v,i+3);
- argptrs[i]=v;
- }
-
- GET_VARIANT_PTR(dst,argc+3);
-
- const GDScript *gds = _script;
-
-
- const Map<StringName,GDFunction>::Element *E=NULL;
- while (gds->base.ptr()) {
- gds=gds->base.ptr();
- E=gds->member_functions.find(*methodname);
- if (E)
- break;
- }
-
- Variant::CallError err;
-
- if (E) {
-
- *dst=((GDFunction*)&E->get())->call(p_instance,(const Variant**)argptrs,argc,err);
- } else if (gds->native.ptr()) {
-
- if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
-
- MethodBind *mb = ObjectTypeDB::get_method(gds->native->get_name(),*methodname);
- if (!mb) {
- err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
- } else {
- *dst=mb->call(p_instance->owner,(const Variant**)argptrs,argc,err);
- }
- } else {
- err.error=Variant::CallError::CALL_OK;
- }
- } else {
-
- if (*methodname!=GDScriptLanguage::get_singleton()->strings._init) {
- err.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
- } else {
- err.error=Variant::CallError::CALL_OK;
- }
- }
-
-
- if (err.error!=Variant::CallError::CALL_OK) {
-
-
- String methodstr = *methodname;
- err_text=_get_call_error(err,"function '"+methodstr+"'",(const Variant**)argptrs);
-
- break;
- }
-
- ip+=4+argc;
-
- } continue;
- case OPCODE_YIELD:
- case OPCODE_YIELD_SIGNAL: {
-
- int ipofs=1;
- if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
- CHECK_SPACE(4);
- ipofs+=2;
- } else {
- CHECK_SPACE(2);
-
- }
-
- Ref<GDFunctionState> gdfs = memnew( GDFunctionState );
- gdfs->function=this;
-
- gdfs->state.stack.resize(alloca_size);
- //copy variant stack
- for(int i=0;i<_stack_size;i++) {
- memnew_placement(&gdfs->state.stack[sizeof(Variant)*i],Variant(stack[i]));
- }
- gdfs->state.stack_size=_stack_size;
- gdfs->state.self=self;
- gdfs->state.alloca_size=alloca_size;
- gdfs->state._class=_class;
- gdfs->state.ip=ip+ipofs;
- gdfs->state.line=line;
- //gdfs->state.result_pos=ip+ipofs-1;
- gdfs->state.defarg=defarg;
- gdfs->state.instance=p_instance;
- gdfs->function=this;
-
- retvalue=gdfs;
-
- if (_code_ptr[ip]==OPCODE_YIELD_SIGNAL) {
- GET_VARIANT_PTR(argobj,1);
- GET_VARIANT_PTR(argname,2);
- //do the oneshot connect
-
- if (argobj->get_type()!=Variant::OBJECT) {
- err_text="First argument of yield() not of type object.";
- break;
- }
- if (argname->get_type()!=Variant::STRING) {
- err_text="Second argument of yield() not a string (for signal name).";
- break;
- }
- Object *obj=argobj->operator Object *();
- String signal = argname->operator String();
-#ifdef DEBUG_ENABLED
-
- if (!obj) {
- err_text="First argument of yield() is null.";
- break;
- }
- if (ScriptDebugger::get_singleton()) {
- if (!ObjectDB::instance_validate(obj)) {
- err_text="First argument of yield() is a previously freed instance.";
- break;
- }
- }
- if (signal.length()==0) {
-
- err_text="Second argument of yield() is an empty string (for signal name).";
- break;
- }
-
-#endif
- Error err = obj->connect(signal,gdfs.ptr(),"_signal_callback",varray(gdfs),Object::CONNECT_ONESHOT);
- if (err!=OK) {
- err_text="Error connecting to signal: "+signal+" during yield().";
- break;
- }
-
-
- }
-
- exit_ok=true;
-
- } break;
- case OPCODE_YIELD_RESUME: {
-
- CHECK_SPACE(2);
- if (!p_state) {
- err_text=("Invalid Resume (bug?)");
- break;
- }
- GET_VARIANT_PTR(result,1);
- *result=p_state->result;
- ip+=2;
-
- } continue;
- case OPCODE_JUMP: {
-
- CHECK_SPACE(2);
- int to = _code_ptr[ip+1];
-
- ERR_BREAK(to<0 || to>_code_size);
- ip=to;
-
- } continue;
- case OPCODE_JUMP_IF: {
-
- CHECK_SPACE(3);
-
- GET_VARIANT_PTR(test,1);
-
- bool valid;
- bool result = test->booleanize(valid);
-#ifdef DEBUG_ENABLED
- if (!valid) {
-
- err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
- break;
- }
-#endif
- if (result) {
- int to = _code_ptr[ip+2];
- ERR_BREAK(to<0 || to>_code_size);
- ip=to;
- continue;
- }
- ip+=3;
- } continue;
- case OPCODE_JUMP_IF_NOT: {
-
- CHECK_SPACE(3);
-
- GET_VARIANT_PTR(test,1);
-
- bool valid;
- bool result = test->booleanize(valid);
-#ifdef DEBUG_ENABLED
- if (!valid) {
-
- err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
- break;
- }
-#endif
- if (!result) {
- int to = _code_ptr[ip+2];
- ERR_BREAK(to<0 || to>_code_size);
- ip=to;
- continue;
- }
- ip+=3;
- } continue;
- case OPCODE_JUMP_TO_DEF_ARGUMENT: {
-
- CHECK_SPACE(2);
- ip=_default_arg_ptr[defarg];
-
- } continue;
- case OPCODE_RETURN: {
-
- CHECK_SPACE(2);
- GET_VARIANT_PTR(r,1);
- retvalue=*r;
- exit_ok=true;
-
- } break;
- case OPCODE_ITERATE_BEGIN: {
-
- CHECK_SPACE(8); //space for this an regular iterate
-
- GET_VARIANT_PTR(counter,1);
- GET_VARIANT_PTR(container,2);
-
- bool valid;
- if (!container->iter_init(*counter,valid)) {
- if (!valid) {
- err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"'.";
- break;
- }
- int jumpto=_code_ptr[ip+3];
- ERR_BREAK(jumpto<0 || jumpto>_code_size);
- ip=jumpto;
- continue;
- }
- GET_VARIANT_PTR(iterator,4);
-
-
- *iterator=container->iter_get(*counter,valid);
- if (!valid) {
- err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"'.";
- break;
- }
-
-
- ip+=5; //skip regular iterate which is always next
-
- } continue;
- case OPCODE_ITERATE: {
-
- CHECK_SPACE(4);
-
- GET_VARIANT_PTR(counter,1);
- GET_VARIANT_PTR(container,2);
-
- bool valid;
- if (!container->iter_next(*counter,valid)) {
- if (!valid) {
- err_text="Unable to iterate on object of type "+Variant::get_type_name(container->get_type())+"' (type changed since first iteration?).";
- break;
- }
- int jumpto=_code_ptr[ip+3];
- ERR_BREAK(jumpto<0 || jumpto>_code_size);
- ip=jumpto;
- continue;
- }
- GET_VARIANT_PTR(iterator,4);
-
- *iterator=container->iter_get(*counter,valid);
- if (!valid) {
- err_text="Unable to obtain iterator object of type "+Variant::get_type_name(container->get_type())+"' (but was obtained on first iteration?).";
- break;
- }
-
- ip+=5; //loop again
- } continue;
- case OPCODE_ASSERT: {
- CHECK_SPACE(2);
- GET_VARIANT_PTR(test,1);
-
-#ifdef DEBUG_ENABLED
- bool valid;
- bool result = test->booleanize(valid);
-
-
- if (!valid) {
-
- err_text="cannot evaluate conditional expression of type: "+Variant::get_type_name(test->get_type());
- break;
- }
-
-
- if (!result) {
-
- err_text="Assertion failed.";
- break;
- }
-
-#endif
-
- ip+=2;
- } continue;
- case OPCODE_BREAKPOINT: {
-#ifdef DEBUG_ENABLED
- if (ScriptDebugger::get_singleton()) {
- GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement",true);
- }
-#endif
- ip+=1;
- } continue;
- case OPCODE_LINE: {
- CHECK_SPACE(2);
-
- line=_code_ptr[ip+1];
- ip+=2;
-
- if (ScriptDebugger::get_singleton()) {
- // line
- bool do_break=false;
-
- if (ScriptDebugger::get_singleton()->get_lines_left()>0) {
-
- if (ScriptDebugger::get_singleton()->get_depth()<=0)
- ScriptDebugger::get_singleton()->set_lines_left( ScriptDebugger::get_singleton()->get_lines_left() -1 );
- if (ScriptDebugger::get_singleton()->get_lines_left()<=0)
- do_break=true;
- }
-
- if (ScriptDebugger::get_singleton()->is_breakpoint(line,source))
- do_break=true;
-
- if (do_break) {
- GDScriptLanguage::get_singleton()->debug_break("Breakpoint",true);
- }
-
- ScriptDebugger::get_singleton()->line_poll();
-
- }
- } continue;
- case OPCODE_END: {
-
- exit_ok=true;
- break;
-
- } break;
- default: {
-
- err_text="Illegal opcode "+itos(_code_ptr[ip])+" at address "+itos(ip);
- } break;
-
- }
-
- if (exit_ok)
- break;
- //error
- // function, file, line, error, explanation
- String err_file;
- if (p_instance)
- err_file=p_instance->script->path;
- else if (_class)
- err_file=_class->path;
- if (err_file=="")
- err_file="<built-in>";
- String err_func = name;
- if (p_instance && p_instance->script->name!="")
- err_func=p_instance->script->name+"."+err_func;
- int err_line=line;
- if (err_text=="") {
- err_text="Internal Script Error! - opcode #"+itos(last_opcode)+" (report please).";
- }
-
- if (!GDScriptLanguage::get_singleton()->debug_break(err_text,false)) {
- // debugger break did not happen
-
- _err_print_error(err_func.utf8().get_data(),err_file.utf8().get_data(),err_line,err_text.utf8().get_data(),ERR_HANDLER_SCRIPT);
- }
-
-
- break;
- }
-
- if (ScriptDebugger::get_singleton())
- GDScriptLanguage::get_singleton()->exit_function();
-
-
- if (_stack_size) {
- //free stack
- for(int i=0;i<_stack_size;i++)
- stack[i].~Variant();
- }
-
- return retvalue;
-
-}
-
-const int* GDFunction::get_code() const {
-
- return _code_ptr;
-}
-int GDFunction::get_code_size() const{
-
- return _code_size;
-}
-
-Variant GDFunction::get_constant(int p_idx) const {
-
- ERR_FAIL_INDEX_V(p_idx,constants.size(),"<errconst>");
- return constants[p_idx];
-}
-
-StringName GDFunction::get_global_name(int p_idx) const {
-
- ERR_FAIL_INDEX_V(p_idx,global_names.size(),"<errgname>");
- return global_names[p_idx];
-}
-
-int GDFunction::get_default_argument_count() const {
-
- return default_arguments.size();
-}
-int GDFunction::get_default_argument_addr(int p_arg) const{
-
- ERR_FAIL_INDEX_V(p_arg,default_arguments.size(),-1);
- return default_arguments[p_arg];
-}
-
-
-StringName GDFunction::get_name() const {
-
- return name;
-}
-
-int GDFunction::get_max_stack_size() const {
-
- return _stack_size;
-}
-
-struct _GDFKC {
-
- int order;
- List<int> pos;
-};
-
-struct _GDFKCS {
-
- int order;
- StringName id;
- int pos;
-
- bool operator<(const _GDFKCS &p_r) const {
-
- return order<p_r.order;
- }
-};
-
-void GDFunction::debug_get_stack_member_state(int p_line,List<Pair<StringName,int> > *r_stackvars) const {
-
-
- int oc=0;
- Map<StringName,_GDFKC> sdmap;
- for( const List<StackDebug>::Element *E=stack_debug.front();E;E=E->next()) {
-
- const StackDebug &sd=E->get();
- if (sd.line>p_line)
- break;
-
- if (sd.added) {
-
- if (!sdmap.has(sd.identifier)) {
- _GDFKC d;
- d.order=oc++;
- d.pos.push_back(sd.pos);
- sdmap[sd.identifier]=d;
-
- } else {
- sdmap[sd.identifier].pos.push_back(sd.pos);
- }
- } else {
-
-
- ERR_CONTINUE(!sdmap.has(sd.identifier));
-
- sdmap[sd.identifier].pos.pop_back();
- if (sdmap[sd.identifier].pos.empty())
- sdmap.erase(sd.identifier);
- }
-
- }
-
-
- List<_GDFKCS> stackpositions;
- for(Map<StringName,_GDFKC>::Element *E=sdmap.front();E;E=E->next() ) {
-
- _GDFKCS spp;
- spp.id=E->key();
- spp.order=E->get().order;
- spp.pos=E->get().pos.back()->get();
- stackpositions.push_back(spp);
- }
-
- stackpositions.sort();
-
- for(List<_GDFKCS>::Element *E=stackpositions.front();E;E=E->next()) {
-
- Pair<StringName,int> p;
- p.first=E->get().id;
- p.second=E->get().pos;
- r_stackvars->push_back(p);
- }
-
-
-}
-
-#if 0
-void GDFunction::clear() {
-
- name=StringName();
- constants.clear();
- _stack_size=0;
- code.clear();
- _constants_ptr=NULL;
- _constant_count=0;
- _global_names_ptr=NULL;
- _global_names_count=0;
- _code_ptr=NULL;
- _code_size=0;
-
-}
-#endif
-GDFunction::GDFunction() {
-
- _stack_size=0;
- _call_size=0;
- name="<anonymous>";
-#ifdef DEBUG_ENABLED
- _func_cname=NULL;
-#endif
-
-}
-
-/////////////////////
-
-
-Variant GDFunctionState::_signal_callback(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
-
- Variant arg;
- r_error.error=Variant::CallError::CALL_OK;
-
- ERR_FAIL_COND_V(!function,Variant());
-
- if (p_argcount==0) {
- r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
- r_error.argument=1;
- return Variant();
- } else if (p_argcount==1) {
- //noooneee
- } else if (p_argcount==2) {
- arg=*p_args[0];
- } else {
- Array extra_args;
- for(int i=0;i<p_argcount-1;i++) {
- extra_args.push_back(*p_args[i]);
- }
- arg=extra_args;
- }
-
- Ref<GDFunctionState> self = *p_args[p_argcount-1];
-
- if (self.is_null()) {
- r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
- r_error.argument=p_argcount-1;
- r_error.expected=Variant::OBJECT;
- return Variant();
- }
-
- state.result=arg;
- Variant ret = function->call(NULL,NULL,0,r_error,&state);
- function=NULL; //cleaned up;
- state.result=Variant();
- return ret;
-}
-
-
-bool GDFunctionState::is_valid() const {
-
- return function!=NULL;
-}
-
-Variant GDFunctionState::resume(const Variant& p_arg) {
-
- ERR_FAIL_COND_V(!function,Variant());
-
- state.result=p_arg;
- Variant::CallError err;
- Variant ret = function->call(NULL,NULL,0,err,&state);
- function=NULL; //cleaned up;
- state.result=Variant();
- return ret;
-}
-
-
-void GDFunctionState::_bind_methods() {
-
- ObjectTypeDB::bind_method(_MD("resume:Variant","arg"),&GDFunctionState::resume,DEFVAL(Variant()));
- ObjectTypeDB::bind_method(_MD("is_valid"),&GDFunctionState::is_valid);
- ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"_signal_callback",&GDFunctionState::_signal_callback,MethodInfo("_signal_callback"));
-
-}
-
-GDFunctionState::GDFunctionState() {
-
- function=NULL;
-}
-
-GDFunctionState::~GDFunctionState() {
-
- if (function!=NULL) {
- //never called, deinitialize stack
- for(int i=0;i<state.stack_size;i++) {
- Variant *v=(Variant*)&state.stack[sizeof(Variant)*i];
- v->~Variant();
- }
- }
-}
+#include "os/os.h"
///////////////////////////
@@ -1457,6 +101,12 @@ GDInstance* GDScript::_create_instance(const Variant** p_args,int p_argcount,Obj
instance->members.resize(member_indices.size());
instance->script=Ref<GDScript>(this);
instance->owner=p_owner;
+#ifdef DEBUG_ENABLED
+ //needed for hot reloading
+ for(Map<StringName,MemberInfo>::Element *E=member_indices.front();E;E=E->next()) {
+ instance->member_indices_cache[E->key()]=E->get().index;
+ }
+#endif
instance->owner->set_script_instance(instance);
/* STEP 2, INITIALIZE AND CONSRTUCT */
@@ -1856,10 +506,10 @@ void GDScript::_set_subclass_path(Ref<GDScript>& p_sc,const String& p_path) {
}
}
-Error GDScript::reload() {
+Error GDScript::reload(bool p_keep_state) {
- ERR_FAIL_COND_V(instances.size(),ERR_ALREADY_IN_USE);
+ ERR_FAIL_COND_V(!p_keep_state && instances.size(),ERR_ALREADY_IN_USE);
String basedir=path;
@@ -1887,7 +537,7 @@ Error GDScript::reload() {
bool can_run = ScriptServer::is_scripting_enabled() || parser.is_tool_script();
GDCompiler compiler;
- err = compiler.compile(&parser,this);
+ err = compiler.compile(&parser,this,p_keep_state);
if (err) {
@@ -1935,14 +585,14 @@ Variant GDScript::call(const StringName& p_method,const Variant** p_args,int p_a
GDScript *top=this;
while(top) {
- Map<StringName,GDFunction>::Element *E=top->member_functions.find(p_method);
+ Map<StringName,GDFunction*>::Element *E=top->member_functions.find(p_method);
if (E) {
- if (!E->get().is_static()) {
+ if (!E->get()->is_static()) {
WARN_PRINT(String("Can't call non-static function: '"+String(p_method)+"' in script.").utf8().get_data());
}
- return E->get().call(NULL,p_args,p_argcount,r_error);
+ return E->get()->call(NULL,p_args,p_argcount,r_error);
}
top=top->_base;
}
@@ -2127,7 +777,7 @@ Error GDScript::load_source_code(const String& p_path) {
}
-const Map<StringName,GDFunction>& GDScript::debug_get_member_functions() const {
+const Map<StringName,GDFunction*>& GDScript::debug_get_member_functions() const {
return member_functions;
}
@@ -2193,7 +843,7 @@ void GDScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
}
-GDScript::GDScript() {
+GDScript::GDScript() : script_list(this) {
_static_ref=this;
@@ -2207,6 +857,33 @@ GDScript::GDScript() {
source_changed_cache=false;
#endif
+#ifdef DEBUG_ENABLED
+ if (GDScriptLanguage::get_singleton()->lock) {
+ GDScriptLanguage::get_singleton()->lock->lock();
+ }
+ GDScriptLanguage::get_singleton()->script_list.add(&script_list);
+
+ if (GDScriptLanguage::get_singleton()->lock) {
+ GDScriptLanguage::get_singleton()->lock->unlock();
+ }
+#endif
+}
+
+GDScript::~GDScript() {
+ for (Map<StringName,GDFunction*>::Element *E=member_functions.front();E;E=E->next()) {
+ memdelete( E->get() );
+ }
+
+#ifdef DEBUG_ENABLED
+ if (GDScriptLanguage::get_singleton()->lock) {
+ GDScriptLanguage::get_singleton()->lock->lock();
+ }
+ GDScriptLanguage::get_singleton()->script_list.remove(&script_list);
+
+ if (GDScriptLanguage::get_singleton()->lock) {
+ GDScriptLanguage::get_singleton()->lock->unlock();
+ }
+#endif
}
@@ -2242,14 +919,14 @@ bool GDInstance::set(const StringName& p_name, const Variant& p_value) {
while(sptr) {
- Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._set);
+ Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._set);
if (E) {
Variant name=p_name;
const Variant *args[2]={&name,&p_value};
Variant::CallError err;
- Variant ret = E->get().call(this,(const Variant**)args,2,err);
+ Variant ret = E->get()->call(this,(const Variant**)args,2,err);
if (err.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::BOOL && ret.operator bool())
return true;
}
@@ -2292,14 +969,14 @@ bool GDInstance::get(const StringName& p_name, Variant &r_ret) const {
}
{
- const Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get);
+ const Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get);
if (E) {
Variant name=p_name;
const Variant *args[1]={&name};
Variant::CallError err;
- Variant ret = const_cast<GDFunction*>(&E->get())->call(const_cast<GDInstance*>(this),(const Variant**)args,1,err);
+ Variant ret = const_cast<GDFunction*>(E->get())->call(const_cast<GDInstance*>(this),(const Variant**)args,1,err);
if (err.error==Variant::CallError::CALL_OK && ret.get_type()!=Variant::NIL) {
r_ret=ret;
return true;
@@ -2341,12 +1018,12 @@ void GDInstance::get_property_list(List<PropertyInfo> *p_properties) const {
while(sptr) {
- const Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get_property_list);
+ const Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get_property_list);
if (E) {
Variant::CallError err;
- Variant ret = const_cast<GDFunction*>(&E->get())->call(const_cast<GDInstance*>(this),NULL,0,err);
+ Variant ret = const_cast<GDFunction*>(E->get())->call(const_cast<GDInstance*>(this),NULL,0,err);
if (err.error==Variant::CallError::CALL_OK) {
if (ret.get_type()!=Variant::ARRAY) {
@@ -2403,7 +1080,7 @@ void GDInstance::get_property_list(List<PropertyInfo> *p_properties) const {
if (sptr->member_functions.has("_get_property_list")) {
Variant::CallError err;
- GDFunction *f = const_cast<GDFunction*>(&sptr->member_functions["_get_property_list"]);
+ GDFunction *f = const_cast<GDFunction*>(sptr->member_functions["_get_property_list"]);
Variant plv = f->call(const_cast<GDInstance*>(this),NULL,0,err);
if (plv.get_type()!=Variant::ARRAY) {
@@ -2419,11 +1096,11 @@ void GDInstance::get_property_list(List<PropertyInfo> *p_properties) const {
PropertyInfo pinfo;
if (!p.has("name")) {
ERR_PRINT("_get_property_list: expected 'name' key of type string.")
- continue;
+ continue;
}
if (!p.has("type")) {
ERR_PRINT("_get_property_list: expected 'type' key of type integer.")
- continue;
+ continue;
}
pinfo.name=p["name"];
pinfo.type=Variant::Type(int(p["type"]));
@@ -2457,12 +1134,12 @@ void GDInstance::get_method_list(List<MethodInfo> *p_list) const {
const GDScript *sptr=script.ptr();
while(sptr) {
- for (Map<StringName,GDFunction>::Element *E = sptr->member_functions.front();E;E=E->next()) {
+ for (Map<StringName,GDFunction*>::Element *E = sptr->member_functions.front();E;E=E->next()) {
MethodInfo mi;
mi.name=E->key();
mi.flags|=METHOD_FLAG_FROM_SCRIPT;
- for(int i=0;i<E->get().get_argument_count();i++)
+ for(int i=0;i<E->get()->get_argument_count();i++)
mi.arguments.push_back(PropertyInfo(Variant::NIL,"arg"+itos(i)));
p_list->push_back(mi);
}
@@ -2475,7 +1152,7 @@ bool GDInstance::has_method(const StringName& p_method) const {
const GDScript *sptr=script.ptr();
while(sptr) {
- const Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
+ const Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
if (E)
return true;
sptr = sptr->_base;
@@ -2489,9 +1166,9 @@ Variant GDInstance::call(const StringName& p_method,const Variant** p_args,int p
GDScript *sptr=script.ptr();
while(sptr) {
- Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
+ Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
if (E) {
- return E->get().call(this,p_args,p_argcount,r_error);
+ return E->get()->call(this,p_args,p_argcount,r_error);
}
sptr = sptr->_base;
}
@@ -2505,9 +1182,9 @@ void GDInstance::call_multilevel(const StringName& p_method,const Variant** p_ar
Variant::CallError ce;
while(sptr) {
- Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
+ Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
if (E) {
- E->get().call(this,p_args,p_argcount,ce);
+ E->get()->call(this,p_args,p_argcount,ce);
}
sptr = sptr->_base;
}
@@ -2522,9 +1199,9 @@ void GDInstance::_ml_call_reversed(GDScript *sptr,const StringName& p_method,con
Variant::CallError ce;
- Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(p_method);
+ Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
if (E) {
- E->get().call(this,p_args,p_argcount,ce);
+ E->get()->call(this,p_args,p_argcount,ce);
}
}
@@ -2544,10 +1221,10 @@ void GDInstance::notification(int p_notification) {
GDScript *sptr=script.ptr();
while(sptr) {
- Map<StringName,GDFunction>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._notification);
+ Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._notification);
if (E) {
Variant::CallError err;
- E->get().call(this,args,1,err);
+ E->get()->call(this,args,1,err);
if (err.error!=Variant::CallError::CALL_OK) {
//print error about notification call
@@ -2568,6 +1245,37 @@ ScriptLanguage *GDInstance::get_language() {
return GDScriptLanguage::get_singleton();
}
+void GDInstance::reload_members() {
+
+#ifdef DEBUG_ENABLED
+
+ members.resize(script->member_indices.size()); //resize
+
+ Vector<Variant> new_members;
+ new_members.resize(script->member_indices.size());
+
+ //pass the values to the new indices
+ for(Map<StringName,GDScript::MemberInfo>::Element *E=script->member_indices.front();E;E=E->next()) {
+
+ if (member_indices_cache.has(E->key())) {
+ Variant value = members[member_indices_cache[E->key()]];
+ new_members[E->get().index]=value;
+ }
+
+ }
+
+ //apply
+ members=new_members;
+
+ //pass the values to the new indices
+ member_indices_cache.clear();
+ for(Map<StringName,GDScript::MemberInfo>::Element *E=script->member_indices.front();E;E=E->next()) {
+
+ member_indices_cache[E->key()]=E->get().index;
+ }
+
+#endif
+}
GDInstance::GDInstance() {
owner=NULL;
@@ -2672,11 +1380,320 @@ void GDScriptLanguage::finish() {
}
+void GDScriptLanguage::profiling_start() {
+
+#ifdef DEBUG_ENABLED
+ if (lock) {
+ lock->lock();
+ }
+
+ SelfList<GDFunction> *elem=function_list.first();
+ while(elem) {
+ elem->self()->profile.call_count=0;
+ elem->self()->profile.self_time=0;
+ elem->self()->profile.total_time=0;
+ elem->self()->profile.frame_call_count=0;
+ elem->self()->profile.frame_self_time=0;
+ elem->self()->profile.frame_total_time=0;
+ elem->self()->profile.last_frame_call_count=0;
+ elem->self()->profile.last_frame_self_time=0;
+ elem->self()->profile.last_frame_total_time=0;
+ elem=elem->next();
+ }
+
+ profiling=true;
+ if (lock) {
+ lock->unlock();
+ }
+
+#endif
+
+}
+
+void GDScriptLanguage::profiling_stop() {
+
+#ifdef DEBUG_ENABLED
+ if (lock) {
+ lock->lock();
+ }
+
+ profiling=false;
+ if (lock) {
+ lock->unlock();
+ }
+
+#endif
+}
+
+int GDScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr,int p_info_max) {
+
+ int current=0;
+#ifdef DEBUG_ENABLED
+ if (lock) {
+ lock->lock();
+ }
+
+
+ SelfList<GDFunction> *elem=function_list.first();
+ while(elem) {
+ if (current>=p_info_max)
+ break;
+ p_info_arr[current].call_count=elem->self()->profile.call_count;
+ p_info_arr[current].self_time=elem->self()->profile.self_time;
+ p_info_arr[current].total_time=elem->self()->profile.total_time;
+ p_info_arr[current].signature=elem->self()->profile.signature;
+ elem=elem->next();
+ current++;
+ }
+
+
+
+ if (lock) {
+ lock->unlock();
+ }
+
+
+#endif
+
+ return current;
+
+
+}
+
+int GDScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr,int p_info_max) {
+
+ int current=0;
+
+#ifdef DEBUG_ENABLED
+ if (lock) {
+ lock->lock();
+ }
+
+
+ SelfList<GDFunction> *elem=function_list.first();
+ while(elem) {
+ if (current>=p_info_max)
+ break;
+ if (elem->self()->profile.last_frame_call_count>0) {
+ p_info_arr[current].call_count=elem->self()->profile.last_frame_call_count;
+ p_info_arr[current].self_time=elem->self()->profile.last_frame_self_time;
+ p_info_arr[current].total_time=elem->self()->profile.last_frame_total_time;
+ p_info_arr[current].signature=elem->self()->profile.signature;
+ //print_line(String(elem->self()->profile.signature)+": "+itos(elem->self()->profile.last_frame_call_count));
+ current++;
+ }
+ elem=elem->next();
+
+ }
+
+
+ if (lock) {
+ lock->unlock();
+ }
+
+
+#endif
+
+ return current;
+
+}
+
+
+struct GDScriptDepSort {
+
+ //must support sorting so inheritance works properly (parent must be reloaded first)
+ bool operator()(const Ref<GDScript> &A, const Ref<GDScript>& B) const {
+
+ if (A==B)
+ return false; //shouldn't happen but..
+ const GDScript *I=B->get_base().ptr();
+ while(I) {
+ if (I==A.ptr()) {
+ // A is a base of B
+ return true;
+ }
+
+ I=I->get_base().ptr();
+ }
+
+ return false; //not a base
+ }
+};
+
+void GDScriptLanguage::reload_all_scripts() {
+
+
+
+#ifdef DEBUG_ENABLED
+ print_line("RELOAD ALL SCRIPTS");
+ if (lock) {
+ lock->lock();
+ }
+
+ List<Ref<GDScript> > scripts;
+
+ SelfList<GDScript> *elem=script_list.first();
+ while(elem) {
+ if (elem->self()->get_path().is_resource_file()) {
+ print_line("FOUND: "+elem->self()->get_path());
+ scripts.push_back(Ref<GDScript>(elem->self())); //cast to gdscript to avoid being erased by accident
+ }
+ elem=elem->next();
+ }
+
+ if (lock) {
+ lock->unlock();
+ }
+
+ //as scripts are going to be reloaded, must proceed without locking here
+
+ scripts.sort_custom<GDScriptDepSort>(); //update in inheritance dependency order
+
+ for(List<Ref<GDScript> >::Element *E=scripts.front();E;E=E->next()) {
+
+ print_line("RELOADING: "+E->get()->get_path());
+ E->get()->load_source_code(E->get()->get_path());
+ E->get()->reload(true);
+ }
+#endif
+}
+
+
+void GDScriptLanguage::reload_tool_script(const Ref<Script>& p_script,bool p_soft_reload) {
+
+
+#ifdef DEBUG_ENABLED
+
+ if (lock) {
+ lock->lock();
+ }
+
+ List<Ref<GDScript> > scripts;
+
+ SelfList<GDScript> *elem=script_list.first();
+ while(elem) {
+ if (elem->self()->get_path().is_resource_file()) {
+
+ scripts.push_back(Ref<GDScript>(elem->self())); //cast to gdscript to avoid being erased by accident
+ }
+ elem=elem->next();
+ }
+
+ if (lock) {
+ lock->unlock();
+ }
+
+ //when someone asks you why dynamically typed languages are easier to write....
+
+ Map< Ref<GDScript>, Map<ObjectID,List<Pair<StringName,Variant> > > > to_reload;
+
+ //as scripts are going to be reloaded, must proceed without locking here
+
+ scripts.sort_custom<GDScriptDepSort>(); //update in inheritance dependency order
+
+ for(List<Ref<GDScript> >::Element *E=scripts.front();E;E=E->next()) {
+
+ bool reload = E->get()==p_script || to_reload.has(E->get()->get_base());
+
+ if (!reload)
+ continue;
+
+ to_reload.insert(E->get(),Map<ObjectID,List<Pair<StringName,Variant> > >());
+
+ if (!p_soft_reload) {
+
+ //save state and remove script from instances
+ Map<ObjectID,List<Pair<StringName,Variant> > >& map = to_reload[E->get()];
+
+ while(E->get()->instances.front()) {
+ Object *obj = E->get()->instances.front()->get();
+ //save instance info
+ List<Pair<StringName,Variant> > state;
+ if (obj->get_script_instance()) {
+
+ obj->get_script_instance()->get_property_state(state);
+ map[obj->get_instance_ID()]=state;
+ obj->set_script(RefPtr());
+ }
+ }
+
+ //same thing for placeholders
+#ifdef TOOLS_ENABLED
+
+ while(E->get()->placeholders.size()) {
+
+ Object *obj = E->get()->placeholders.front()->get()->get_owner();
+ //save instance info
+ List<Pair<StringName,Variant> > state;
+ if (obj->get_script_instance()) {
+
+ obj->get_script_instance()->get_property_state(state);
+ map[obj->get_instance_ID()]=state;
+ obj->set_script(RefPtr());
+ }
+ }
+#endif
+
+ }
+ }
+
+ for(Map< Ref<GDScript>, Map<ObjectID,List<Pair<StringName,Variant> > > >::Element *E=to_reload.front();E;E=E->next()) {
+
+ Ref<GDScript> scr = E->key();
+ scr->reload(true);
+
+ //restore state if saved
+ for (Map<ObjectID,List<Pair<StringName,Variant> > >::Element *F=E->get().front();F;F=F->next()) {
+
+ Object *obj = ObjectDB::get_instance(F->key());
+ if (!obj)
+ continue;
+
+ obj->set_script(scr.get_ref_ptr());
+ if (!obj->get_script_instance())
+ continue;
+
+ for (List<Pair<StringName,Variant> >::Element *G=F->get().front();G;G=G->next()) {
+ obj->get_script_instance()->set(G->get().first,G->get().second);
+ }
+ }
+
+ //if instance states were saved, set them!
+ }
+
+
+#endif
+}
void GDScriptLanguage::frame() {
-// print_line("calls: "+itos(calls));
+ // print_line("calls: "+itos(calls));
calls=0;
+
+#ifdef DEBUG_ENABLED
+ if (profiling) {
+ if (lock) {
+ lock->lock();
+ }
+
+ SelfList<GDFunction> *elem=function_list.first();
+ while(elem) {
+ elem->self()->profile.last_frame_call_count=elem->self()->profile.frame_call_count;
+ elem->self()->profile.last_frame_self_time=elem->self()->profile.frame_self_time;
+ elem->self()->profile.last_frame_total_time=elem->self()->profile.frame_total_time;
+ elem->self()->profile.frame_call_count=0;
+ elem->self()->profile.frame_self_time=0;
+ elem->self()->profile.frame_total_time=0;
+ elem=elem->next();
+ }
+
+
+ if (lock) {
+ lock->unlock();
+ }
+ }
+
+#endif
}
/* EDITOR FUNCTIONS */
@@ -2724,7 +1741,7 @@ void GDScriptLanguage::get_reserved_words(List<String> *p_words) const {
"pass",
"return",
"while",
- 0};
+ 0};
const char **w=_reserved_words;
@@ -2756,30 +1773,43 @@ GDScriptLanguage::GDScriptLanguage() {
_debug_parse_err_line=-1;
_debug_parse_err_file="";
- _debug_call_stack_pos=0;
- int dmcs=GLOBAL_DEF("debug/script_max_call_stack",1024);
- if (ScriptDebugger::get_singleton()) {
- //debugging enabled!
+#ifdef NO_THREADS
+ lock=NULL;
+#else
+ lock = Mutex::create();
+#endif
+ profiling=false;
+ script_frame_time=0;
+
+ _debug_call_stack_pos=0;
+ int dmcs=GLOBAL_DEF("debug/script_max_call_stack",1024);
+ if (ScriptDebugger::get_singleton()) {
+ //debugging enabled!
- _debug_max_call_stack = dmcs;
- if (_debug_max_call_stack<1024)
- _debug_max_call_stack=1024;
- _call_stack = memnew_arr( CallLevel, _debug_max_call_stack+1 );
+ _debug_max_call_stack = dmcs;
+ if (_debug_max_call_stack<1024)
+ _debug_max_call_stack=1024;
+ _call_stack = memnew_arr( CallLevel, _debug_max_call_stack+1 );
- } else {
- _debug_max_call_stack=0;
- _call_stack=NULL;
- }
+ } else {
+ _debug_max_call_stack=0;
+ _call_stack=NULL;
+ }
}
GDScriptLanguage::~GDScriptLanguage() {
- if (_call_stack) {
- memdelete_arr(_call_stack);
- }
- singleton=NULL;
+
+ if (lock) {
+ memdelete(lock);
+ lock=NULL;
+ }
+ if (_call_stack) {
+ memdelete_arr(_call_stack);
+ }
+ singleton=NULL;
}
/*************** RESOURCE ***************/
@@ -2868,6 +1898,11 @@ Error ResourceFormatSaverGDScript::save(const String &p_path,const RES& p_resour
}
file->close();
memdelete(file);
+
+ if (ScriptServer::is_reload_scripts_on_save_enabled()) {
+ GDScriptLanguage::get_singleton()->reload_tool_script(p_resource,false);
+ }
+
return OK;
}
diff --git a/modules/gdscript/gd_script.h b/modules/gdscript/gd_script.h
index 663fc985a7..166e29ad70 100644
--- a/modules/gdscript/gd_script.h
+++ b/modules/gdscript/gd_script.h
@@ -32,185 +32,7 @@
#include "script_language.h"
#include "io/resource_loader.h"
#include "io/resource_saver.h"
-#include "os/thread.h"
-#include "pair.h"
-class GDInstance;
-class GDScript;
-
-
-
-class GDFunction {
-public:
-
- enum Opcode {
- OPCODE_OPERATOR,
- OPCODE_EXTENDS_TEST,
- OPCODE_SET,
- OPCODE_GET,
- OPCODE_SET_NAMED,
- OPCODE_GET_NAMED,
- OPCODE_ASSIGN,
- OPCODE_ASSIGN_TRUE,
- OPCODE_ASSIGN_FALSE,
- OPCODE_CONSTRUCT, //only for basic types!!
- OPCODE_CONSTRUCT_ARRAY,
- OPCODE_CONSTRUCT_DICTIONARY,
- OPCODE_CALL,
- OPCODE_CALL_RETURN,
- OPCODE_CALL_BUILT_IN,
- OPCODE_CALL_SELF,
- OPCODE_CALL_SELF_BASE,
- OPCODE_YIELD,
- OPCODE_YIELD_SIGNAL,
- OPCODE_YIELD_RESUME,
- OPCODE_JUMP,
- OPCODE_JUMP_IF,
- OPCODE_JUMP_IF_NOT,
- OPCODE_JUMP_TO_DEF_ARGUMENT,
- OPCODE_RETURN,
- OPCODE_ITERATE_BEGIN,
- OPCODE_ITERATE,
- OPCODE_ASSERT,
- OPCODE_BREAKPOINT,
- OPCODE_LINE,
- OPCODE_END
- };
-
- enum Address {
- ADDR_BITS=24,
- ADDR_MASK=((1<<ADDR_BITS)-1),
- ADDR_TYPE_MASK=~ADDR_MASK,
- ADDR_TYPE_SELF=0,
- ADDR_TYPE_CLASS=1,
- ADDR_TYPE_MEMBER=2,
- ADDR_TYPE_CLASS_CONSTANT=3,
- ADDR_TYPE_LOCAL_CONSTANT=4,
- ADDR_TYPE_STACK=5,
- ADDR_TYPE_STACK_VARIABLE=6,
- ADDR_TYPE_GLOBAL=7,
- ADDR_TYPE_NIL=8
- };
-
- struct StackDebug {
-
- int line;
- int pos;
- bool added;
- StringName identifier;
- };
-
-private:
-friend class GDCompiler;
-
- StringName source;
-
- mutable Variant nil;
- mutable Variant *_constants_ptr;
- int _constant_count;
- const StringName *_global_names_ptr;
- int _global_names_count;
- const int *_default_arg_ptr;
- int _default_arg_count;
- const int *_code_ptr;
- int _code_size;
- int _argument_count;
- int _stack_size;
- int _call_size;
- int _initial_line;
- bool _static;
- GDScript *_script;
-
- StringName name;
- Vector<Variant> constants;
- Vector<StringName> global_names;
- Vector<int> default_arguments;
- Vector<int> code;
-#ifdef DEBUG_ENABLED
- CharString func_cname;
- const char*_func_cname;
-#endif
-
-#ifdef TOOLS_ENABLED
- Vector<StringName> arg_names;
-#endif
-
- List<StackDebug> stack_debug;
-
- _FORCE_INLINE_ Variant *_get_variant(int p_address,GDInstance *p_instance,GDScript *p_script,Variant &self,Variant *p_stack,String& r_error) const;
- _FORCE_INLINE_ String _get_call_error(const Variant::CallError& p_err, const String& p_where,const Variant**argptrs) const;
-
-
-public:
-
- struct CallState {
-
- GDInstance *instance;
- Vector<uint8_t> stack;
- int stack_size;
- Variant self;
- uint32_t alloca_size;
- GDScript *_class;
- int ip;
- int line;
- int defarg;
- Variant result;
-
- };
-
- _FORCE_INLINE_ bool is_static() const { return _static; }
-
- const int* get_code() const; //used for debug
- int get_code_size() const;
- Variant get_constant(int p_idx) const;
- StringName get_global_name(int p_idx) const;
- StringName get_name() const;
- int get_max_stack_size() const;
- int get_default_argument_count() const;
- int get_default_argument_addr(int p_idx) const;
- GDScript *get_script() const { return _script; }
-
- void debug_get_stack_member_state(int p_line,List<Pair<StringName,int> > *r_stackvars) const;
-
- _FORCE_INLINE_ bool is_empty() const { return _code_size==0; }
-
- int get_argument_count() const { return _argument_count; }
- StringName get_argument_name(int p_idx) const {
-#ifdef TOOLS_ENABLED
- ERR_FAIL_INDEX_V(p_idx,arg_names.size(),StringName());
- return arg_names[p_idx];
-#endif
- return StringName();
-
- }
- Variant get_default_argument(int p_idx) const {
- ERR_FAIL_INDEX_V(p_idx,default_arguments.size(),Variant());
- return default_arguments[p_idx];
- }
-
- Variant call(GDInstance *p_instance,const Variant **p_args, int p_argcount,Variant::CallError& r_err,CallState *p_state=NULL);
-
- GDFunction();
-};
-
-
-class GDFunctionState : public Reference {
-
- OBJ_TYPE(GDFunctionState,Reference);
-friend class GDFunction;
- GDFunction *function;
- GDFunction::CallState state;
- Variant _signal_callback(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
-protected:
- static void _bind_methods();
-public:
-
- bool is_valid() const;
- Variant resume(const Variant& p_arg=Variant());
- GDFunctionState();
- ~GDFunctionState();
-};
-
-
+#include "gd_function.h"
class GDNativeClass : public Reference {
OBJ_TYPE(GDNativeClass,Reference);
@@ -258,7 +80,7 @@ friend class GDScriptLanguage;
Set<StringName> members; //members are just indices to the instanced script.
Map<StringName,Variant> constants;
- Map<StringName,GDFunction> member_functions;
+ Map<StringName,GDFunction*> member_functions;
Map<StringName,MemberInfo> member_indices; //members are just indices to the instanced script.
Map<StringName,Ref<GDScript> > subclasses;
Map<StringName,Vector<StringName> > _signals;
@@ -285,6 +107,7 @@ friend class GDScriptLanguage;
String source;
String path;
String name;
+ SelfList<GDScript> script_list;
GDInstance* _create_instance(const Variant** p_args,int p_argcount,Object *p_owner,bool p_isref,Variant::CallError &r_error);
@@ -317,7 +140,7 @@ public:
const Map<StringName,Ref<GDScript> >& get_subclasses() const { return subclasses; }
const Map<StringName,Variant >& get_constants() const { return constants; }
const Set<StringName>& get_members() const { return members; }
- const Map<StringName,GDFunction>& get_member_functions() const { return member_functions; }
+ const Map<StringName,GDFunction*>& get_member_functions() const { return member_functions; }
const Ref<GDNativeClass>& get_native() const { return native; }
virtual bool has_script_signal(const StringName& p_signal) const;
@@ -328,7 +151,7 @@ public:
Ref<GDScript> get_base() const;
const Map<StringName,MemberInfo>& debug_get_member_indices() const { return member_indices; }
- const Map<StringName,GDFunction>& debug_get_member_functions() const; //this is debug only
+ const Map<StringName,GDFunction*>& debug_get_member_functions() const; //this is debug only
StringName debug_get_member_by_index(int p_idx) const;
Variant _new(const Variant** p_args,int p_argcount,Variant::CallError& r_error);
@@ -343,7 +166,7 @@ public:
virtual void set_source_code(const String& p_code);
virtual void update_exports();
- virtual Error reload();
+ virtual Error reload(bool p_keep_state=false);
virtual String get_node_type() const;
void set_script_path(const String& p_path) { path=p_path; } //because subclasses need a path too...
@@ -357,18 +180,24 @@ public:
virtual ScriptLanguage *get_language() const;
GDScript();
+ ~GDScript();
};
class GDInstance : public ScriptInstance {
friend class GDScript;
friend class GDFunction;
friend class GDFunctions;
+friend class GDCompiler;
Object *owner;
Ref<GDScript> script;
+#ifdef DEBUG_ENABLED
+ Map<StringName,int> member_indices_cache; //used only for hot script reloading
+#endif
Vector<Variant> members;
bool base_ref;
+
void _ml_call_reversed(GDScript *sptr,const StringName& p_method,const Variant** p_args,int p_argcount);
public:
@@ -385,7 +214,7 @@ public:
virtual void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount);
virtual void call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount);
- Variant debug_get_member_by_index(int p_idx) const { return members[p_idx]; }
+ Variant debug_get_member_by_index(int p_idx) const { return members[p_idx]; }
virtual void notification(int p_notification);
@@ -395,6 +224,7 @@ public:
void set_path(const String& p_path);
+ void reload_members();
GDInstance();
~GDInstance();
@@ -431,8 +261,19 @@ class GDScriptLanguage : public ScriptLanguage {
void _add_global(const StringName& p_name,const Variant& p_value);
+ Mutex *lock;
+
+friend class GDScript;
+
+ SelfList<GDScript>::List script_list;
+friend class GDFunction;
+
+ SelfList<GDFunction>::List function_list;
+ bool profiling;
+ uint64_t script_frame_time;
public:
+
int calls;
bool debug_break(const String& p_error,bool p_allow_continue=true);
@@ -547,11 +388,20 @@ public:
virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems=-1,int p_max_depth=-1);
virtual String debug_parse_stack_level_expression(int p_level,const String& p_expression,int p_max_subitems=-1,int p_max_depth=-1);
+ virtual void reload_all_scripts();
+ virtual void reload_tool_script(const Ref<Script>& p_script,bool p_soft_reload);
+
virtual void frame();
virtual void get_public_functions(List<MethodInfo> *p_functions) const;
virtual void get_public_constants(List<Pair<String,Variant> > *p_constants) const;
+ virtual void profiling_start();
+ virtual void profiling_stop();
+
+ virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr,int p_info_max);
+ virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr,int p_info_max);
+
/* LOADER FUNCTIONS */
virtual void get_recognized_extensions(List<String> *p_extensions) const;
diff --git a/modules/gdscript/gd_tokenizer.cpp b/modules/gdscript/gd_tokenizer.cpp
index 56eacfd20e..8dd68cf95a 100644
--- a/modules/gdscript/gd_tokenizer.cpp
+++ b/modules/gdscript/gd_tokenizer.cpp
@@ -79,8 +79,8 @@ const char* GDTokenizer::token_names[TK_MAX]={
"for",
"do",
"while",
-"switch",
-"case",
+"switch (reserved)",
+"case (reserved)",
"break",
"continue",
"pass",
@@ -874,6 +874,7 @@ void GDTokenizerText::_advance() {
{TK_CF_WHILE,"while"},
{TK_CF_DO,"do"},
{TK_CF_SWITCH,"switch"},
+ {TK_CF_CASE,"case"},
{TK_CF_BREAK,"break"},
{TK_CF_CONTINUE,"continue"},
{TK_CF_RETURN,"return"},
diff --git a/modules/gdscript/register_types.cpp b/modules/gdscript/register_types.cpp
index 2aea494f39..6aa53f03ef 100644
--- a/modules/gdscript/register_types.cpp
+++ b/modules/gdscript/register_types.cpp
@@ -1,14 +1,31 @@
-/*************************************************/
-/* register_script_types.cpp */
-/*************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/*************************************************/
-/* Source code within this file is: */
-/* (c) 2007-2016 Juan Linietsky, Ariel Manzur */
-/* All Rights Reserved. */
-/*************************************************/
-
+/*************************************************************************/
+/* register_types.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* http://www.godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2016 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. */
+/*************************************************************************/
#include "register_types.h"
#include "gd_script.h"