/*************************************************************************/ /* gd_script.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_SCRIPT_H #define GD_SCRIPT_H #include "script_language.h" #include "io/resource_loader.h" #include "io/resource_saver.h" #include "os/thread.h" #include "pair.h" #include "self_list.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< constants; Vector global_names; Vector default_arguments; Vector code; #ifdef TOOLS_ENABLED Vector arg_names; #endif List 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 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 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 > *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(); }; class GDNativeClass : public Reference { OBJ_TYPE(GDNativeClass,Reference); StringName name; protected: bool _get(const StringName& p_name,Variant &r_ret) const; static void _bind_methods(); public: _FORCE_INLINE_ const StringName& get_name() const { return name; } Variant _new(); Object *instance(); GDNativeClass(const StringName& p_name); }; class GDScript : public Script { OBJ_TYPE(GDScript,Script); bool tool; bool valid; struct MemberInfo { int index; StringName setter; StringName getter; }; friend class GDInstance; friend class GDFunction; friend class GDCompiler; friend class GDFunctions; friend class GDScriptLanguage; Variant _static_ref; //used for static call Ref native; Ref base; GDScript *_base; //fast pointer access GDScript *_owner; //for subclasses Set members; //members are just indices to the instanced script. Map constants; Map member_functions; Map member_indices; //members are just indices to the instanced script. Map > subclasses; Map > _signals; #ifdef TOOLS_ENABLED Map member_default_values; List members_cache; Map member_default_values_cache; Ref base_cache; Set inheriters_cache; bool source_changed_cache; void _update_exports_values(Map& values, List &propnames); #endif Map member_info; GDFunction *initializer; //direct pointer to _init , faster to locate int subclass_count; Set instances; //exported members String source; String path; String name; GDInstance* _create_instance(const Variant** p_args,int p_argcount,Object *p_owner,bool p_isref,Variant::CallError &r_error); void _set_subclass_path(Ref& p_sc,const String& p_path); #ifdef TOOLS_ENABLED Set placeholders; //void _update_placeholder(PlaceHolderScriptInstance *p_placeholder); virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder); #endif bool _update_exports(); protected: bool _get(const StringName& p_name,Variant &r_ret) const; bool _set(const StringName& p_name, const Variant& p_value); void _get_property_list(List *p_properties) const; Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error); // void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount); static void _bind_methods(); public: bool is_valid() const { return valid; } const Map >& get_subclasses() const { return subclasses; } const Map& get_constants() const { return constants; } const Set& get_members() const { return members; } const Map& get_member_functions() const { return member_functions; } const Ref& get_native() const { return native; } virtual bool has_script_signal(const StringName& p_signal) const; virtual void get_script_signal_list(List *r_signals) const; bool is_tool() const { return tool; } Ref get_base() const; const Map& debug_get_member_indices() const { return member_indices; } const Map& 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); virtual bool can_instance() const; virtual StringName get_instance_base_type() const; // this may not work in all scripts, will return empty if so virtual ScriptInstance* instance_create(Object *p_this); virtual bool instance_has(const Object *p_this) const; virtual bool has_source_code() const; virtual String get_source_code() const; virtual void set_source_code(const String& p_code); virtual void update_exports(); virtual Error reload(); virtual String get_node_type() const; void set_script_path(const String& p_path) { path=p_path; } //because subclasses need a path too... Error load_source_code(const String& p_path); Error load_byte_code(const String& p_path); Vector get_as_byte_code() const; bool get_property_default_value(const StringName& p_property,Variant& r_value) const; virtual ScriptLanguage *get_language() const; GDScript(); ~GDScript(); }; class GDInstance : public ScriptInstance { friend class GDScript; friend class GDFunction; friend class GDFunctions; Object *owner; Ref script; Vector members; bool base_ref; void _ml_call_reversed(GDScript *sptr,const StringName& p_method,const Variant** p_args,int p_argcount); public: virtual bool set(const StringName& p_name, const Variant& p_value); virtual bool get(const StringName& p_name, Variant &r_ret) const; virtual void get_property_list(List *p_properties) const; virtual Variant::Type get_property_type(const StringName& p_name,bool *r_is_valid=NULL) const; virtual void get_method_list(List *p_list) const; virtual bool has_method(const StringName& p_method) const; virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error); 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]; } virtual void notification(int p_notification); virtual Ref