1 /*************************************************************************/
2 /*  script_language.cpp                                                  */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #include "script_language.h"
31 
32 ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
33 int ScriptServer::_language_count = 0;
34 
35 bool ScriptServer::scripting_enabled = true;
36 bool ScriptServer::reload_scripts_on_save = false;
37 
_notification(int p_what)38 void Script::_notification(int p_what) {
39 
40 	if (p_what == NOTIFICATION_POSTINITIALIZE) {
41 
42 		if (ScriptDebugger::get_singleton())
43 			ScriptDebugger::get_singleton()->set_break_language(get_language());
44 	}
45 }
46 
_bind_methods()47 void Script::_bind_methods() {
48 
49 	ObjectTypeDB::bind_method(_MD("can_instance"), &Script::can_instance);
50 	//ObjectTypeDB::bind_method(_MD("instance_create","base_object"),&Script::instance_create);
51 	ObjectTypeDB::bind_method(_MD("instance_has", "base_object"), &Script::instance_has);
52 	ObjectTypeDB::bind_method(_MD("has_source_code"), &Script::has_source_code);
53 	ObjectTypeDB::bind_method(_MD("get_source_code"), &Script::get_source_code);
54 	ObjectTypeDB::bind_method(_MD("set_source_code", "source"), &Script::set_source_code);
55 	ObjectTypeDB::bind_method(_MD("reload", "keep_state"), &Script::reload, DEFVAL(false));
56 }
57 
set_scripting_enabled(bool p_enabled)58 void ScriptServer::set_scripting_enabled(bool p_enabled) {
59 
60 	scripting_enabled = p_enabled;
61 }
62 
is_scripting_enabled()63 bool ScriptServer::is_scripting_enabled() {
64 
65 	return scripting_enabled;
66 }
67 
get_language_count()68 int ScriptServer::get_language_count() {
69 
70 	return _language_count;
71 }
72 
get_language(int p_idx)73 ScriptLanguage *ScriptServer::get_language(int p_idx) {
74 
75 	ERR_FAIL_INDEX_V(p_idx, _language_count, NULL);
76 
77 	return _languages[p_idx];
78 }
79 
register_language(ScriptLanguage * p_language)80 void ScriptServer::register_language(ScriptLanguage *p_language) {
81 
82 	ERR_FAIL_COND(_language_count >= MAX_LANGUAGES);
83 	_languages[_language_count++] = p_language;
84 }
85 
unregister_language(ScriptLanguage * p_language)86 void ScriptServer::unregister_language(ScriptLanguage *p_language) {
87 
88 	for (int i = 0; i < _language_count; i++) {
89 		if (_languages[i] == p_language) {
90 			_language_count--;
91 			if (i < _language_count) {
92 				SWAP(_languages[i], _languages[_language_count]);
93 			}
94 			return;
95 		}
96 	}
97 }
98 
init_languages()99 void ScriptServer::init_languages() {
100 
101 	for (int i = 0; i < _language_count; i++) {
102 		_languages[i]->init();
103 	}
104 }
105 
set_reload_scripts_on_save(bool p_enable)106 void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
107 
108 	reload_scripts_on_save = p_enable;
109 }
110 
is_reload_scripts_on_save_enabled()111 bool ScriptServer::is_reload_scripts_on_save_enabled() {
112 
113 	return reload_scripts_on_save;
114 }
115 
thread_enter()116 void ScriptServer::thread_enter() {
117 
118 	for (int i = 0; i < _language_count; i++) {
119 		_languages[i]->thread_enter();
120 	}
121 }
122 
thread_exit()123 void ScriptServer::thread_exit() {
124 
125 	for (int i = 0; i < _language_count; i++) {
126 		_languages[i]->thread_exit();
127 	}
128 }
129 
get_property_state(List<Pair<StringName,Variant>> & state)130 void ScriptInstance::get_property_state(List<Pair<StringName, Variant> > &state) {
131 
132 	List<PropertyInfo> pinfo;
133 	get_property_list(&pinfo);
134 	for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
135 
136 		if (E->get().usage & PROPERTY_USAGE_STORAGE) {
137 			Pair<StringName, Variant> p;
138 			p.first = E->get().name;
139 			if (get(p.first, p.second))
140 				state.push_back(p);
141 		}
142 	}
143 }
144 
call(const StringName & p_method,VARIANT_ARG_DECLARE)145 Variant ScriptInstance::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
146 
147 	VARIANT_ARGPTRS;
148 	int argc = 0;
149 	for (int i = 0; i < VARIANT_ARG_MAX; i++) {
150 		if (argptr[i]->get_type() == Variant::NIL)
151 			break;
152 		argc++;
153 	}
154 
155 	Variant::CallError error;
156 	return call(p_method, argptr, argc, error);
157 }
158 
call_multilevel(const StringName & p_method,const Variant ** p_args,int p_argcount)159 void ScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
160 	Variant::CallError ce;
161 	call(p_method, p_args, p_argcount, ce); // script may not support multilevel calls
162 }
163 
call_multilevel_reversed(const StringName & p_method,const Variant ** p_args,int p_argcount)164 void ScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
165 	Variant::CallError ce;
166 	call(p_method, p_args, p_argcount, ce); // script may not support multilevel calls
167 }
168 
call_multilevel(const StringName & p_method,VARIANT_ARG_DECLARE)169 void ScriptInstance::call_multilevel(const StringName &p_method, VARIANT_ARG_DECLARE) {
170 
171 	VARIANT_ARGPTRS;
172 	int argc = 0;
173 	for (int i = 0; i < VARIANT_ARG_MAX; i++) {
174 		if (argptr[i]->get_type() == Variant::NIL)
175 			break;
176 		argc++;
177 	}
178 
179 	Variant::CallError error;
180 	call_multilevel(p_method, argptr, argc);
181 }
182 
~ScriptInstance()183 ScriptInstance::~ScriptInstance() {
184 }
185 
186 ScriptCodeCompletionCache *ScriptCodeCompletionCache::singleton = NULL;
ScriptCodeCompletionCache()187 ScriptCodeCompletionCache::ScriptCodeCompletionCache() {
188 	singleton = this;
189 }
190 
frame()191 void ScriptLanguage::frame() {
192 }
193 
194 ScriptDebugger *ScriptDebugger::singleton = NULL;
195 
set_lines_left(int p_left)196 void ScriptDebugger::set_lines_left(int p_left) {
197 
198 	lines_left = p_left;
199 }
200 
get_lines_left() const201 int ScriptDebugger::get_lines_left() const {
202 
203 	return lines_left;
204 }
205 
set_depth(int p_depth)206 void ScriptDebugger::set_depth(int p_depth) {
207 
208 	depth = p_depth;
209 }
210 
get_depth() const211 int ScriptDebugger::get_depth() const {
212 
213 	return depth;
214 }
215 
insert_breakpoint(int p_line,const StringName & p_source)216 void ScriptDebugger::insert_breakpoint(int p_line, const StringName &p_source) {
217 
218 	if (!breakpoints.has(p_line))
219 		breakpoints[p_line] = Set<StringName>();
220 	breakpoints[p_line].insert(p_source);
221 }
222 
remove_breakpoint(int p_line,const StringName & p_source)223 void ScriptDebugger::remove_breakpoint(int p_line, const StringName &p_source) {
224 
225 	if (!breakpoints.has(p_line))
226 		return;
227 
228 	breakpoints[p_line].erase(p_source);
229 	if (breakpoints[p_line].size() == 0)
230 		breakpoints.erase(p_line);
231 }
is_breakpoint(int p_line,const StringName & p_source) const232 bool ScriptDebugger::is_breakpoint(int p_line, const StringName &p_source) const {
233 
234 	if (!breakpoints.has(p_line))
235 		return false;
236 	return breakpoints[p_line].has(p_source);
237 }
is_breakpoint_line(int p_line) const238 bool ScriptDebugger::is_breakpoint_line(int p_line) const {
239 
240 	return breakpoints.has(p_line);
241 }
242 
breakpoint_find_source(const String & p_source) const243 String ScriptDebugger::breakpoint_find_source(const String &p_source) const {
244 
245 	return p_source;
246 }
247 
clear_breakpoints()248 void ScriptDebugger::clear_breakpoints() {
249 
250 	breakpoints.clear();
251 }
252 
idle_poll()253 void ScriptDebugger::idle_poll() {
254 }
255 
line_poll()256 void ScriptDebugger::line_poll() {
257 }
258 
set_break_language(ScriptLanguage * p_lang)259 void ScriptDebugger::set_break_language(ScriptLanguage *p_lang) {
260 
261 	break_lang = p_lang;
262 }
263 
get_break_language() const264 ScriptLanguage *ScriptDebugger::get_break_language() const {
265 
266 	return break_lang;
267 }
268 
ScriptDebugger()269 ScriptDebugger::ScriptDebugger() {
270 
271 	singleton = this;
272 	lines_left = -1;
273 	depth = -1;
274 	break_lang = NULL;
275 }
276 
set(const StringName & p_name,const Variant & p_value)277 bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) {
278 
279 	if (values.has(p_name)) {
280 		Variant defval;
281 		if (script->get_property_default_value(p_name, defval)) {
282 			if (defval == p_value) {
283 				values.erase(p_name);
284 				return true;
285 			}
286 		}
287 		values[p_name] = p_value;
288 		return true;
289 	} else {
290 		Variant defval;
291 		if (script->get_property_default_value(p_name, defval)) {
292 			if (defval != p_value) {
293 				values[p_name] = p_value;
294 			}
295 			return true;
296 		}
297 	}
298 	return false;
299 }
get(const StringName & p_name,Variant & r_ret) const300 bool PlaceHolderScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
301 
302 	if (values.has(p_name)) {
303 		r_ret = values[p_name];
304 		return true;
305 	}
306 
307 	Variant defval;
308 	if (script->get_property_default_value(p_name, defval)) {
309 		r_ret = defval;
310 		return true;
311 	}
312 	return false;
313 }
314 
get_property_list(List<PropertyInfo> * p_properties) const315 void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
316 
317 	for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
318 		PropertyInfo pinfo = E->get();
319 		if (!values.has(pinfo.name)) {
320 			pinfo.usage |= PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE;
321 		}
322 		p_properties->push_back(E->get());
323 	}
324 }
325 
get_property_type(const StringName & p_name,bool * r_is_valid) const326 Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
327 
328 	if (values.has(p_name)) {
329 		if (r_is_valid)
330 			*r_is_valid = true;
331 		return values[p_name].get_type();
332 	}
333 	if (r_is_valid)
334 		*r_is_valid = false;
335 
336 	return Variant::NIL;
337 }
338 
update(const List<PropertyInfo> & p_properties,const Map<StringName,Variant> & p_values)339 void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties, const Map<StringName, Variant> &p_values) {
340 
341 	Set<StringName> new_values;
342 	for (const List<PropertyInfo>::Element *E = p_properties.front(); E; E = E->next()) {
343 
344 		StringName n = E->get().name;
345 		new_values.insert(n);
346 
347 		if (!values.has(n) || values[n].get_type() != E->get().type) {
348 
349 			if (p_values.has(n))
350 				values[n] = p_values[n];
351 		}
352 	}
353 
354 	properties = p_properties;
355 	List<StringName> to_remove;
356 
357 	for (Map<StringName, Variant>::Element *E = values.front(); E; E = E->next()) {
358 
359 		if (!new_values.has(E->key()))
360 			to_remove.push_back(E->key());
361 
362 		Variant defval;
363 		if (script->get_property_default_value(E->key(), defval)) {
364 			//remove because it's the same as the default value
365 			if (defval == E->get()) {
366 				to_remove.push_back(E->key());
367 			}
368 		}
369 	}
370 
371 	while (to_remove.size()) {
372 
373 		values.erase(to_remove.front()->get());
374 		to_remove.pop_front();
375 	}
376 
377 	if (owner && owner->get_script_instance() == this) {
378 
379 		owner->_change_notify();
380 	}
381 	//change notify
382 }
383 
PlaceHolderScriptInstance(ScriptLanguage * p_language,Ref<Script> p_script,Object * p_owner)384 PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner) {
385 
386 	language = p_language;
387 	script = p_script;
388 	owner = p_owner;
389 }
390 
~PlaceHolderScriptInstance()391 PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
392 
393 	if (script.is_valid()) {
394 		script->_placeholder_erased(this);
395 	}
396 }
397