1 /*************************************************************************/
2 /*  script_language.h                                                    */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #ifndef SCRIPT_LANGUAGE_H
32 #define SCRIPT_LANGUAGE_H
33 
34 #include "core/io/multiplayer_api.h"
35 #include "core/map.h"
36 #include "core/pair.h"
37 #include "core/resource.h"
38 
39 class ScriptLanguage;
40 
41 typedef void (*ScriptEditRequestFunction)(const String &p_path);
42 
43 class ScriptServer {
44 	enum {
45 
46 		MAX_LANGUAGES = 16
47 	};
48 
49 	static ScriptLanguage *_languages[MAX_LANGUAGES];
50 	static int _language_count;
51 	static bool scripting_enabled;
52 	static bool reload_scripts_on_save;
53 	static bool languages_finished;
54 
55 	struct GlobalScriptClass {
56 		StringName language;
57 		String path;
58 		String base;
59 	};
60 
61 	static HashMap<StringName, GlobalScriptClass> global_classes;
62 
63 public:
64 	static ScriptEditRequestFunction edit_request_func;
65 
66 	static void set_scripting_enabled(bool p_enabled);
67 	static bool is_scripting_enabled();
get_language_count()68 	_FORCE_INLINE_ static int get_language_count() { return _language_count; }
69 	static ScriptLanguage *get_language(int p_idx);
70 	static void register_language(ScriptLanguage *p_language);
71 	static void unregister_language(ScriptLanguage *p_language);
72 
73 	static void set_reload_scripts_on_save(bool p_enable);
74 	static bool is_reload_scripts_on_save_enabled();
75 
76 	static void thread_enter();
77 	static void thread_exit();
78 
79 	static void global_classes_clear();
80 	static void add_global_class(const StringName &p_class, const StringName &p_base, const StringName &p_language, const String &p_path);
81 	static void remove_global_class(const StringName &p_class);
82 	static bool is_global_class(const StringName &p_class);
83 	static StringName get_global_class_language(const StringName &p_class);
84 	static String get_global_class_path(const String &p_class);
85 	static StringName get_global_class_base(const String &p_class);
86 	static StringName get_global_class_native_base(const String &p_class);
87 	static void get_global_class_list(List<StringName> *r_global_classes);
88 	static void save_global_classes();
89 
90 	static void init_languages();
91 	static void finish_languages();
92 
are_languages_finished()93 	static bool are_languages_finished() { return languages_finished; }
94 };
95 
96 class ScriptInstance;
97 class PlaceHolderScriptInstance;
98 
99 class Script : public Resource {
100 
101 	GDCLASS(Script, Resource);
102 	OBJ_SAVE_TYPE(Script);
103 
104 protected:
editor_can_reload_from_file()105 	virtual bool editor_can_reload_from_file() { return false; } // this is handled by editor better
106 	void _notification(int p_what);
107 	static void _bind_methods();
108 
109 	friend class PlaceHolderScriptInstance;
_placeholder_erased(PlaceHolderScriptInstance * p_placeholder)110 	virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {}
111 
112 	Variant _get_property_default_value(const StringName &p_property);
113 	Array _get_script_property_list();
114 	Array _get_script_method_list();
115 	Array _get_script_signal_list();
116 	Dictionary _get_script_constant_map();
117 
118 public:
119 	virtual bool can_instance() const = 0;
120 
121 	virtual Ref<Script> get_base_script() const = 0; //for script inheritance
122 
123 	virtual StringName get_instance_base_type() const = 0; // this may not work in all scripts, will return empty if so
124 	virtual ScriptInstance *instance_create(Object *p_this) = 0;
placeholder_instance_create(Object * p_this)125 	virtual PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) { return NULL; }
126 	virtual bool instance_has(const Object *p_this) const = 0;
127 
128 	virtual bool has_source_code() const = 0;
129 	virtual String get_source_code() const = 0;
130 	virtual void set_source_code(const String &p_code) = 0;
131 	virtual Error reload(bool p_keep_state = false) = 0;
132 
133 	virtual bool has_method(const StringName &p_method) const = 0;
134 	virtual MethodInfo get_method_info(const StringName &p_method) const = 0;
135 
136 	virtual bool is_tool() const = 0;
137 	virtual bool is_valid() const = 0;
138 
139 	virtual ScriptLanguage *get_language() const = 0;
140 
141 	virtual bool has_script_signal(const StringName &p_signal) const = 0;
142 	virtual void get_script_signal_list(List<MethodInfo> *r_signals) const = 0;
143 
144 	virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const = 0;
145 
update_exports()146 	virtual void update_exports() {} //editor tool
147 	virtual void get_script_method_list(List<MethodInfo> *p_list) const = 0;
148 	virtual void get_script_property_list(List<PropertyInfo> *p_list) const = 0;
149 
get_member_line(const StringName & p_member)150 	virtual int get_member_line(const StringName &p_member) const { return -1; }
151 
get_constants(Map<StringName,Variant> * p_constants)152 	virtual void get_constants(Map<StringName, Variant> *p_constants) {}
get_members(Set<StringName> * p_constants)153 	virtual void get_members(Set<StringName> *p_constants) {}
154 
is_placeholder_fallback_enabled()155 	virtual bool is_placeholder_fallback_enabled() const { return false; }
156 
Script()157 	Script() {}
158 };
159 
160 class ScriptInstance {
161 public:
162 	virtual bool set(const StringName &p_name, const Variant &p_value) = 0;
163 	virtual bool get(const StringName &p_name, Variant &r_ret) const = 0;
164 	virtual void get_property_list(List<PropertyInfo> *p_properties) const = 0;
165 	virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = NULL) const = 0;
166 
get_owner()167 	virtual Object *get_owner() { return NULL; }
168 	virtual void get_property_state(List<Pair<StringName, Variant> > &state);
169 
170 	virtual void get_method_list(List<MethodInfo> *p_list) const = 0;
171 	virtual bool has_method(const StringName &p_method) const = 0;
172 	virtual Variant call(const StringName &p_method, VARIANT_ARG_LIST);
173 	virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) = 0;
174 	virtual void call_multilevel(const StringName &p_method, VARIANT_ARG_LIST);
175 	virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount);
176 	virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount);
177 	virtual void notification(int p_notification) = 0;
to_string(bool * r_valid)178 	virtual String to_string(bool *r_valid) {
179 		if (r_valid)
180 			*r_valid = false;
181 		return String();
182 	}
183 
184 	//this is used by script languages that keep a reference counter of their own
185 	//you can make make Ref<> not die when it reaches zero, so deleting the reference
186 	//depends entirely from the script
187 
refcount_incremented()188 	virtual void refcount_incremented() {}
refcount_decremented()189 	virtual bool refcount_decremented() { return true; } //return true if it can die
190 
191 	virtual Ref<Script> get_script() const = 0;
192 
is_placeholder()193 	virtual bool is_placeholder() const { return false; }
194 
195 	virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid);
196 	virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid);
197 
198 	virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const = 0;
199 	virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const = 0;
200 
201 	virtual ScriptLanguage *get_language() = 0;
202 	virtual ~ScriptInstance();
203 };
204 
205 struct ScriptCodeCompletionOption {
206 	enum Kind {
207 		KIND_CLASS,
208 		KIND_FUNCTION,
209 		KIND_SIGNAL,
210 		KIND_VARIABLE,
211 		KIND_MEMBER,
212 		KIND_ENUM,
213 		KIND_CONSTANT,
214 		KIND_NODE_PATH,
215 		KIND_FILE_PATH,
216 		KIND_PLAIN_TEXT,
217 	};
218 	Kind kind;
219 	String display;
220 	String insert_text;
221 	RES icon;
222 
ScriptCodeCompletionOptionScriptCodeCompletionOption223 	ScriptCodeCompletionOption() {
224 		kind = KIND_PLAIN_TEXT;
225 	}
226 
ScriptCodeCompletionOptionScriptCodeCompletionOption227 	ScriptCodeCompletionOption(const String &p_text, Kind p_kind) {
228 		display = p_text;
229 		insert_text = p_text;
230 		kind = p_kind;
231 	}
232 };
233 
234 class ScriptCodeCompletionCache {
235 
236 	static ScriptCodeCompletionCache *singleton;
237 
238 public:
239 	virtual RES get_cached_resource(const String &p_path) = 0;
240 
get_singleton()241 	static ScriptCodeCompletionCache *get_singleton() { return singleton; }
242 
243 	ScriptCodeCompletionCache();
244 
~ScriptCodeCompletionCache()245 	virtual ~ScriptCodeCompletionCache() {}
246 };
247 
248 class ScriptLanguage {
249 public:
250 	virtual String get_name() const = 0;
251 
252 	/* LANGUAGE FUNCTIONS */
253 	virtual void init() = 0;
254 	virtual String get_type() const = 0;
255 	virtual String get_extension() const = 0;
256 	virtual Error execute_file(const String &p_path) = 0;
257 	virtual void finish() = 0;
258 
259 	/* EDITOR FUNCTIONS */
260 	struct Warning {
261 		int line;
262 		int code;
263 		String string_code;
264 		String message;
265 	};
266 
267 	virtual void get_reserved_words(List<String> *p_words) const = 0;
268 	virtual void get_comment_delimiters(List<String> *p_delimiters) const = 0;
269 	virtual void get_string_delimiters(List<String> *p_delimiters) const = 0;
270 	virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const = 0;
make_template(const String & p_class_name,const String & p_base_class_name,Ref<Script> & p_script)271 	virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {}
is_using_templates()272 	virtual bool is_using_templates() { return false; }
273 	virtual bool validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path = "", List<String> *r_functions = NULL, List<Warning> *r_warnings = NULL, Set<int> *r_safe_lines = NULL) const = 0;
validate_path(const String & p_path)274 	virtual String validate_path(const String &p_path) const { return ""; }
275 	virtual Script *create_script() const = 0;
276 	virtual bool has_named_classes() const = 0;
277 	virtual bool supports_builtin_mode() const = 0;
can_inherit_from_file()278 	virtual bool can_inherit_from_file() { return false; }
279 	virtual int find_function(const String &p_function, const String &p_code) const = 0;
280 	virtual String make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const = 0;
open_in_external_editor(const Ref<Script> & p_script,int p_line,int p_col)281 	virtual Error open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) { return ERR_UNAVAILABLE; }
overrides_external_editor()282 	virtual bool overrides_external_editor() { return false; }
283 
complete_code(const String & p_code,const String & p_path,Object * p_owner,List<ScriptCodeCompletionOption> * r_options,bool & r_force,String & r_call_hint)284 	virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_force, String &r_call_hint) { return ERR_UNAVAILABLE; }
285 
286 	struct LookupResult {
287 		enum Type {
288 			RESULT_SCRIPT_LOCATION,
289 			RESULT_CLASS,
290 			RESULT_CLASS_CONSTANT,
291 			RESULT_CLASS_PROPERTY,
292 			RESULT_CLASS_METHOD,
293 			RESULT_CLASS_ENUM,
294 			RESULT_CLASS_TBD_GLOBALSCOPE
295 		};
296 		Type type;
297 		Ref<Script> script;
298 		String class_name;
299 		String class_member;
300 		int location;
301 	};
302 
lookup_code(const String & p_code,const String & p_symbol,const String & p_path,Object * p_owner,LookupResult & r_result)303 	virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) { return ERR_UNAVAILABLE; }
304 
305 	virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const = 0;
306 	virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) = 0;
add_named_global_constant(const StringName & p_name,const Variant & p_value)307 	virtual void add_named_global_constant(const StringName &p_name, const Variant &p_value) {}
remove_named_global_constant(const StringName & p_name)308 	virtual void remove_named_global_constant(const StringName &p_name) {}
309 
310 	/* MULTITHREAD FUNCTIONS */
311 
312 	//some VMs need to be notified of thread creation/exiting to allocate a stack
thread_enter()313 	virtual void thread_enter() {}
thread_exit()314 	virtual void thread_exit() {}
315 
316 	/* DEBUGGER FUNCTIONS */
317 
318 	virtual String debug_get_error() const = 0;
319 	virtual int debug_get_stack_level_count() const = 0;
320 	virtual int debug_get_stack_level_line(int p_level) const = 0;
321 	virtual String debug_get_stack_level_function(int p_level) const = 0;
322 	virtual String debug_get_stack_level_source(int p_level) const = 0;
323 	virtual void debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) = 0;
324 	virtual void debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) = 0;
debug_get_stack_level_instance(int p_level)325 	virtual ScriptInstance *debug_get_stack_level_instance(int p_level) { return NULL; }
326 	virtual void debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) = 0;
327 	virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems = -1, int p_max_depth = -1) = 0;
328 
329 	struct StackInfo {
330 		String file;
331 		String func;
332 		int line;
333 	};
334 
debug_get_current_stack_info()335 	virtual Vector<StackInfo> debug_get_current_stack_info() { return Vector<StackInfo>(); }
336 
337 	virtual void reload_all_scripts() = 0;
338 	virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) = 0;
339 	/* LOADER FUNCTIONS */
340 
341 	virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
342 	virtual void get_public_functions(List<MethodInfo> *p_functions) const = 0;
343 	virtual void get_public_constants(List<Pair<String, Variant> > *p_constants) const = 0;
344 
345 	struct ProfilingInfo {
346 		StringName signature;
347 		uint64_t call_count;
348 		uint64_t total_time;
349 		uint64_t self_time;
350 	};
351 
352 	virtual void profiling_start() = 0;
353 	virtual void profiling_stop() = 0;
354 
355 	virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) = 0;
356 	virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) = 0;
357 
alloc_instance_binding_data(Object * p_object)358 	virtual void *alloc_instance_binding_data(Object *p_object) { return NULL; } //optional, not used by all languages
free_instance_binding_data(void * p_data)359 	virtual void free_instance_binding_data(void *p_data) {} //optional, not used by all languages
refcount_incremented_instance_binding(Object * p_object)360 	virtual void refcount_incremented_instance_binding(Object *p_object) {} //optional, not used by all languages
refcount_decremented_instance_binding(Object * p_object)361 	virtual bool refcount_decremented_instance_binding(Object *p_object) { return true; } //return true if it can die //optional, not used by all languages
362 
363 	virtual void frame();
364 
handles_global_class_type(const String & p_type)365 	virtual bool handles_global_class_type(const String &p_type) const { return false; }
366 	virtual String get_global_class_name(const String &p_path, String *r_base_type = NULL, String *r_icon_path = NULL) const { return String(); }
367 
~ScriptLanguage()368 	virtual ~ScriptLanguage() {}
369 };
370 
371 extern uint8_t script_encryption_key[32];
372 
373 class PlaceHolderScriptInstance : public ScriptInstance {
374 
375 	Object *owner;
376 	List<PropertyInfo> properties;
377 	Map<StringName, Variant> values;
378 	Map<StringName, Variant> constants;
379 	ScriptLanguage *language;
380 	Ref<Script> script;
381 
382 public:
383 	virtual bool set(const StringName &p_name, const Variant &p_value);
384 	virtual bool get(const StringName &p_name, Variant &r_ret) const;
385 	virtual void get_property_list(List<PropertyInfo> *p_properties) const;
386 	virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = NULL) const;
387 
388 	virtual void get_method_list(List<MethodInfo> *p_list) const;
389 	virtual bool has_method(const StringName &p_method) const;
call(const StringName & p_method,VARIANT_ARG_LIST)390 	virtual Variant call(const StringName &p_method, VARIANT_ARG_LIST) { return Variant(); }
call(const StringName & p_method,const Variant ** p_args,int p_argcount,Variant::CallError & r_error)391 	virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
392 		r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
393 		return Variant();
394 	}
395 	//virtual void call_multilevel(const StringName& p_method,VARIANT_ARG_LIST) { return Variant(); }
396 	//virtual void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) { return Variant(); }
notification(int p_notification)397 	virtual void notification(int p_notification) {}
398 
get_script()399 	virtual Ref<Script> get_script() const { return script; }
400 
get_language()401 	virtual ScriptLanguage *get_language() { return language; }
402 
get_owner()403 	Object *get_owner() { return owner; }
404 
405 	void update(const List<PropertyInfo> &p_properties, const Map<StringName, Variant> &p_values); //likely changed in editor
406 
is_placeholder()407 	virtual bool is_placeholder() const { return true; }
408 
409 	virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid = NULL);
410 	virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid = NULL);
411 
get_rpc_mode(const StringName & p_method)412 	virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const { return MultiplayerAPI::RPC_MODE_DISABLED; }
get_rset_mode(const StringName & p_variable)413 	virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const { return MultiplayerAPI::RPC_MODE_DISABLED; }
414 
415 	PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner);
416 	~PlaceHolderScriptInstance();
417 };
418 
419 class ScriptDebugger {
420 
421 	int lines_left;
422 	int depth;
423 
424 	static ScriptDebugger *singleton;
425 	Map<int, Set<StringName> > breakpoints;
426 
427 	ScriptLanguage *break_lang;
428 
429 public:
get_singleton()430 	_FORCE_INLINE_ static ScriptDebugger *get_singleton() { return singleton; }
431 	void set_lines_left(int p_left);
432 	int get_lines_left() const;
433 
434 	void set_depth(int p_depth);
435 	int get_depth() const;
436 
437 	String breakpoint_find_source(const String &p_source) const;
438 	void insert_breakpoint(int p_line, const StringName &p_source);
439 	void remove_breakpoint(int p_line, const StringName &p_source);
440 	bool is_breakpoint(int p_line, const StringName &p_source) const;
441 	bool is_breakpoint_line(int p_line) const;
442 	void clear_breakpoints();
get_breakpoints()443 	const Map<int, Set<StringName> > &get_breakpoints() const { return breakpoints; }
444 
445 	virtual void debug(ScriptLanguage *p_script, bool p_can_continue = true, bool p_is_error_breakpoint = false) = 0;
446 	virtual void idle_poll();
447 	virtual void line_poll();
448 
449 	void set_break_language(ScriptLanguage *p_lang);
450 	ScriptLanguage *get_break_language() const;
451 
452 	virtual void send_message(const String &p_message, const Array &p_args) = 0;
453 	virtual void send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, ErrorHandlerType p_type, const Vector<ScriptLanguage::StackInfo> &p_stack_info) = 0;
454 
is_remote()455 	virtual bool is_remote() const { return false; }
request_quit()456 	virtual void request_quit() {}
457 
set_multiplayer(Ref<MultiplayerAPI> p_multiplayer)458 	virtual void set_multiplayer(Ref<MultiplayerAPI> p_multiplayer) {}
459 
460 	virtual bool is_profiling() const = 0;
461 	virtual void add_profiling_frame_data(const StringName &p_name, const Array &p_data) = 0;
462 	virtual void profiling_start() = 0;
463 	virtual void profiling_end() = 0;
464 	virtual void profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time) = 0;
465 
466 	ScriptDebugger();
~ScriptDebugger()467 	virtual ~ScriptDebugger() { singleton = NULL; }
468 };
469 
470 #endif
471