forked from godotjs/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecmascript.cpp
More file actions
332 lines (273 loc) · 9.1 KB
/
ecmascript.cpp
File metadata and controls
332 lines (273 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "ecmascript.h"
#include "core/engine.h"
#include "core/io/file_access_encrypted.h"
#include "ecmascript_instance.h"
#include "ecmascript_language.h"
#include "scene/resources/resource_format_text.h"
#define GET_CLASS_BINDER(cls) ECMAScriptLanguage::get_singleton()->binding->get_context_binder(cls->constructor.context)
ScriptLanguage *ECMAScript::get_language() const {
return ECMAScriptLanguage::get_singleton();
}
ECMAScript::ECMAScript() {
ecma_class = NULL;
}
ECMAScript::~ECMAScript() {
}
bool ECMAScript::can_instance() const {
#ifdef TOOLS_ENABLED
return is_valid() && (is_tool() || ScriptServer::is_scripting_enabled());
#else
return is_valid();
#endif
}
StringName ECMAScript::get_instance_base_type() const {
const ECMAClassInfo *cls = get_ecma_class();
if (cls) {
return cls->native_class->name;
} else {
static StringName empty;
return empty;
}
}
ScriptInstance *ECMAScript::instance_create(Object *p_this) {
const ECMAClassInfo *cls = get_ecma_class();
ERR_FAIL_NULL_V(cls, NULL);
if (!ClassDB::is_parent_class(p_this->get_class_name(), cls->native_class->name)) {
ERR_PRINTS("Script inherits from native type '" + String(cls->native_class->name) + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'");
ERR_FAIL_V(NULL);
}
Variant::CallError unchecked_error;
ECMAScriptGCHandler ecma_instance = GET_CLASS_BINDER(cls)->create_ecma_instance_for_godot_object(cls, p_this);
ERR_FAIL_NULL_V(ecma_instance.ecma_object, NULL);
ECMAScriptInstance *instance = memnew(ECMAScriptInstance);
instance->script = Ref<ECMAScript>(this);
instance->owner = p_this;
instance->ecma_object = ecma_instance;
instance->owner->set_script_instance(instance);
return instance;
}
PlaceHolderScriptInstance *ECMAScript::placeholder_instance_create(Object *p_this) {
#ifdef TOOLS_ENABLED
PlaceHolderScriptInstance *si = memnew(PlaceHolderScriptInstance(ECMAScriptLanguage::get_singleton(), Ref<Script>(this), p_this));
placeholders.insert(si);
update_exports();
return si;
#else
return NULL;
#endif
}
bool ECMAScript::is_placeholder_fallback_enabled() const {
#ifdef TOOLS_ENABLED
return Engine::get_singleton()->is_editor_hint() && false;
#else
return false;
#endif
}
Error ECMAScript::reload(bool p_keep_state) {
Error err = OK;
ECMAscriptScriptError ecma_err;
if (!bytecode.empty()) {
ecma_class = ECMAScriptLanguage::get_singleton()->binding->parse_ecma_class(bytecode, get_script_path(), &ecma_err);
} else {
ecma_class = ECMAScriptLanguage::get_singleton()->binding->parse_ecma_class(code, get_script_path(), &ecma_err);
}
if (!ecma_class) {
err = ERR_PARSE_ERROR;
ERR_PRINTS(ECMAScriptLanguage::get_singleton()->binding->error_to_string(ecma_err));
}
return err;
}
bool ECMAScript::instance_has(const Object *p_this) const {
return instances.has(const_cast<Object *>(p_this));
}
#ifdef TOOLS_ENABLED
void ECMAScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
placeholders.erase(p_placeholder);
}
#endif
bool ECMAScript::has_method(const StringName &p_method) const {
const ECMAClassInfo *cls = get_ecma_class();
if (!cls) return false;
return GET_CLASS_BINDER(cls)->has_method(cls->prototype, p_method);
}
MethodInfo ECMAScript::get_method_info(const StringName &p_method) const {
MethodInfo mi;
const ECMAClassInfo *cls = get_ecma_class();
ERR_FAIL_NULL_V(cls, mi);
if (has_method(p_method)) {
mi.name = p_method;
}
return mi;
}
bool ECMAScript::is_tool() const {
const ECMAClassInfo *cls = get_ecma_class();
if (!cls) return false;
return cls->tool;
}
void ECMAScript::get_script_method_list(List<MethodInfo> *p_list) const {
const ECMAClassInfo *cls = get_ecma_class();
// TODO
}
void ECMAScript::get_script_property_list(List<PropertyInfo> *p_list) const {
const ECMAClassInfo *cls = get_ecma_class();
if (!cls) return;
for (const StringName *name = cls->properties.next(NULL); name; name = cls->properties.next(name)) {
const ECMAProperyInfo &prop = cls->properties.get(*name);
PropertyInfo pi;
pi.name = *name;
pi.type = prop.type;
p_list->push_back(pi);
}
}
bool ECMAScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
const ECMAClassInfo *cls = get_ecma_class();
if (!cls)
return false;
if (const ECMAProperyInfo *pi = cls->properties.getptr(p_property)) {
r_value = pi->default_value;
return true;
}
return false;
}
void ECMAScript::update_exports() {
#ifdef TOOLS_ENABLED
const ECMAClassInfo *cls = get_ecma_class();
if (!cls) return;
List<PropertyInfo> props;
Map<StringName, Variant> values;
for (const StringName *name = cls->properties.next(NULL); name; name = cls->properties.next(name)) {
const ECMAProperyInfo epi = cls->properties.get(*name);
PropertyInfo pi;
pi.name = *name;
pi.type = epi.type;
props.push_back(pi);
values[*name] = epi.default_value;
}
for (Set<PlaceHolderScriptInstance *>::Element *E = placeholders.front(); E; E = E->next()) {
E->get()->update(props, values);
}
#endif
}
bool ECMAScript::has_script_signal(const StringName &p_signal) const {
const ECMAClassInfo *cls = get_ecma_class();
if (!cls) return false;
bool found = false;
if (cls->signals.has(p_signal)) {
found = true;
} else if (GET_CLASS_BINDER(cls)->has_signal(cls, p_signal)) {
found = true;
}
return found;
}
void ECMAScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
const ECMAClassInfo *cls = get_ecma_class();
if (!cls) return;
for (const StringName *name = cls->signals.next(NULL); name; name = cls->signals.next(name)) {
r_signals->push_back(cls->signals.get(*name));
}
}
bool ECMAScript::is_valid() const {
return get_ecma_class() != NULL;
}
void ECMAScript::_bind_methods() {
}
RES ResourceFormatLoaderECMAScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
Error err;
if (r_error)
*r_error = ERR_FILE_CANT_OPEN;
Ref<ECMAScript> script;
script.instance();
script->set_script_path(p_path);
if (p_path.ends_with(".js")) {
String code = FileAccess::get_file_as_string(p_path, &err);
ERR_FAIL_COND_V_MSG(err != OK, RES(), "Cannot load source code from file '" + p_path + "'.");
script->set_source_code(code);
} else if (p_path.ends_with(".jsc")) {
Error err;
script->bytecode = FileAccess::get_file_as_array(p_path, &err);
ERR_FAIL_COND_V_MSG(err != OK, RES(), "Cannot load bytecode from file '" + p_path + "'.");
} else if (p_path.ends_with(".jse")) {
FileAccess *fa = FileAccess::open(p_path, FileAccess::READ);
if (fa->is_open()) {
FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
Vector<uint8_t> key;
key.resize(32);
for (int i = 0; i < key.size(); i++) {
key.write[i] = script_encryption_key[i];
}
err = fae->open_and_parse(fa, key, FileAccessEncrypted::MODE_READ);
if (err == OK) {
Vector<uint8_t> encrypted_code;
encrypted_code.resize(fae->get_len());
fae->get_buffer(encrypted_code.ptrw(), encrypted_code.size());
String code;
if (code.parse_utf8((const char *)encrypted_code.ptr(), encrypted_code.size())) {
err = ERR_PARSE_ERROR;
} else {
script->set_source_code(code);
}
fa->close();
fae->close();
memdelete(fae);
} else {
fa->close();
fae->close();
memdelete(fae);
memdelete(fa);
}
} else {
err = ERR_CANT_OPEN;
}
}
err = script->reload();
if (OK != err) {
ERR_PRINTS("Cannot parse source code from file '" + p_path + "'.");
}
if (r_error)
*r_error = err;
return script;
}
void ResourceFormatLoaderECMAScript::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_front("js");
p_extensions->push_back("jsc");
p_extensions->push_back("jse");
}
void ResourceFormatLoaderECMAScript::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
get_recognized_extensions(p_extensions);
}
bool ResourceFormatLoaderECMAScript::handles_type(const String &p_type) const {
return p_type == "ECMAScript";
}
String ResourceFormatLoaderECMAScript::get_resource_type(const String &p_path) const {
String el = p_path.get_extension().to_lower();
if (el == "js" || el == "jsc" || el == "jse")
return "ECMAScript";
return "";
}
Error ResourceFormatSaverECMAScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
Ref<ECMAScript> script = p_resource;
ERR_FAIL_COND_V(script.is_null(), ERR_INVALID_PARAMETER);
String source = script->get_source_code();
Error err;
FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
ERR_FAIL_COND_V_MSG(err, err, "Cannot save ECMAScript file '" + p_path + "'.");
file->store_string(source);
if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
memdelete(file);
return ERR_CANT_CREATE;
}
file->close();
memdelete(file);
if (ScriptServer::is_reload_scripts_on_save_enabled()) {
script->reload();
}
return OK;
}
void ResourceFormatSaverECMAScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
if (Object::cast_to<ECMAScript>(*p_resource)) {
p_extensions->push_back("js");
}
}
bool ResourceFormatSaverECMAScript::recognize(const RES &p_resource) const {
return Object::cast_to<ECMAScript>(*p_resource) != NULL;
}