2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2017-11-16 17:38:18 +00:00
|
|
|
/* gdscript_function.h */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
#ifndef GDSCRIPT_FUNCTION_H
|
|
|
|
#define GDSCRIPT_FUNCTION_H
|
2016-06-01 01:28:27 +00:00
|
|
|
|
|
|
|
#include "os/thread.h"
|
|
|
|
#include "pair.h"
|
|
|
|
#include "reference.h"
|
2016-08-19 19:48:08 +00:00
|
|
|
#include "script_language.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "self_list.h"
|
|
|
|
#include "string_db.h"
|
|
|
|
#include "variant.h"
|
2016-06-01 01:28:27 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
class GDScriptInstance;
|
2016-06-01 01:28:27 +00:00
|
|
|
class GDScript;
|
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
class GDScriptFunction {
|
2016-06-01 01:28:27 +00:00
|
|
|
public:
|
|
|
|
enum Opcode {
|
|
|
|
OPCODE_OPERATOR,
|
|
|
|
OPCODE_EXTENDS_TEST,
|
|
|
|
OPCODE_SET,
|
|
|
|
OPCODE_GET,
|
|
|
|
OPCODE_SET_NAMED,
|
|
|
|
OPCODE_GET_NAMED,
|
2017-01-04 20:37:45 +00:00
|
|
|
OPCODE_SET_MEMBER,
|
|
|
|
OPCODE_GET_MEMBER,
|
2016-06-01 01:28:27 +00:00
|
|
|
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 {
|
2017-03-05 15:44:50 +00:00
|
|
|
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,
|
2018-05-01 14:06:23 +00:00
|
|
|
ADDR_TYPE_NAMED_GLOBAL = 8,
|
|
|
|
ADDR_TYPE_NIL = 9
|
2016-06-01 01:28:27 +00:00
|
|
|
};
|
|
|
|
|
2016-08-19 19:48:08 +00:00
|
|
|
enum RPCMode {
|
|
|
|
RPC_DISABLED,
|
|
|
|
RPC_ENABLED,
|
|
|
|
RPC_SYNC,
|
|
|
|
RPC_SYNC_MASTER,
|
|
|
|
RPC_SYNC_SLAVE
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
struct StackDebug {
|
2016-06-01 01:28:27 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int line;
|
|
|
|
int pos;
|
|
|
|
bool added;
|
|
|
|
StringName identifier;
|
|
|
|
};
|
2016-06-01 01:28:27 +00:00
|
|
|
|
|
|
|
private:
|
2017-11-16 17:38:18 +00:00
|
|
|
friend class GDScriptCompiler;
|
2016-06-01 01:28:27 +00:00
|
|
|
|
|
|
|
StringName source;
|
|
|
|
|
|
|
|
mutable Variant nil;
|
|
|
|
mutable Variant *_constants_ptr;
|
|
|
|
int _constant_count;
|
|
|
|
const StringName *_global_names_ptr;
|
|
|
|
int _global_names_count;
|
2018-05-01 14:06:23 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
const StringName *_named_globals_ptr;
|
|
|
|
int _named_globals_count;
|
|
|
|
#endif
|
2016-06-01 01:28:27 +00:00
|
|
|
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;
|
2016-08-19 19:48:08 +00:00
|
|
|
ScriptInstance::RPCMode rpc_mode;
|
|
|
|
|
2016-06-01 01:28:27 +00:00
|
|
|
GDScript *_script;
|
|
|
|
|
|
|
|
StringName name;
|
|
|
|
Vector<Variant> constants;
|
|
|
|
Vector<StringName> global_names;
|
2018-05-01 14:06:23 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
Vector<StringName> named_globals;
|
|
|
|
#endif
|
2016-06-01 01:28:27 +00:00
|
|
|
Vector<int> default_arguments;
|
|
|
|
Vector<int> code;
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
Vector<StringName> arg_names;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
List<StackDebug> stack_debug;
|
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
_FORCE_INLINE_ Variant *_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant *p_stack, String &r_error) const;
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ String _get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const;
|
2016-06-01 01:28:27 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
friend class GDScriptLanguage;
|
2016-06-01 01:28:27 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
SelfList<GDScriptFunction> function_list;
|
2016-06-01 01:28:27 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
CharString func_cname;
|
2017-03-05 15:44:50 +00:00
|
|
|
const char *_func_cname;
|
2016-06-01 01:28:27 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
|
2016-06-30 00:06:16 +00:00
|
|
|
ObjectID instance_id; //by debug only
|
|
|
|
ObjectID script_id;
|
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
GDScriptInstance *instance;
|
2016-06-01 01:28:27 +00:00
|
|
|
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; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const int *get_code() const; //used for debug
|
2016-06-01 01:28:27 +00:00
|
|
|
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; }
|
2016-06-28 14:49:18 +00:00
|
|
|
StringName get_source() const { return source; }
|
2016-06-01 01:28:27 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const;
|
2016-06-01 01:28:27 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ bool is_empty() const { return _code_size == 0; }
|
2016-06-01 01:28:27 +00:00
|
|
|
|
|
|
|
int get_argument_count() const { return _argument_count; }
|
|
|
|
StringName get_argument_name(int p_idx) const {
|
|
|
|
#ifdef TOOLS_ENABLED
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, arg_names.size(), StringName());
|
2016-06-01 01:28:27 +00:00
|
|
|
return arg_names[p_idx];
|
2017-08-22 17:01:57 +00:00
|
|
|
#else
|
2016-06-01 01:28:27 +00:00
|
|
|
return StringName();
|
2017-08-22 17:01:57 +00:00
|
|
|
#endif
|
2016-06-01 01:28:27 +00:00
|
|
|
}
|
|
|
|
Variant get_default_argument(int p_idx) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), Variant());
|
2016-06-01 01:28:27 +00:00
|
|
|
return default_arguments[p_idx];
|
|
|
|
}
|
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state = NULL);
|
2016-06-01 01:28:27 +00:00
|
|
|
|
2016-08-19 19:48:08 +00:00
|
|
|
_FORCE_INLINE_ ScriptInstance::RPCMode get_rpc_mode() const { return rpc_mode; }
|
2017-11-16 17:38:18 +00:00
|
|
|
GDScriptFunction();
|
|
|
|
~GDScriptFunction();
|
2016-06-01 01:28:27 +00:00
|
|
|
};
|
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
class GDScriptFunctionState : public Reference {
|
2016-06-01 01:28:27 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
GDCLASS(GDScriptFunctionState, Reference);
|
|
|
|
friend class GDScriptFunction;
|
|
|
|
GDScriptFunction *function;
|
|
|
|
GDScriptFunction::CallState state;
|
2017-03-05 15:44:50 +00:00
|
|
|
Variant _signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
|
2018-03-14 15:42:13 +00:00
|
|
|
Ref<GDScriptFunctionState> previous_state;
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2016-06-01 01:28:27 +00:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
2017-05-17 12:47:17 +00:00
|
|
|
bool is_valid(bool p_extended_check = false) const;
|
2017-03-05 15:44:50 +00:00
|
|
|
Variant resume(const Variant &p_arg = Variant());
|
2017-11-16 17:38:18 +00:00
|
|
|
GDScriptFunctionState();
|
|
|
|
~GDScriptFunctionState();
|
2016-06-01 01:28:27 +00:00
|
|
|
};
|
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
#endif // GDSCRIPT_FUNCTION_H
|