forked from godotjs/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecmascript_language.cpp
314 lines (286 loc) · 7.65 KB
/
ecmascript_language.cpp
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
#include "ecmascript_language.h"
#include "core/class_db.h"
#include "core/os/file_access.h"
ECMAScriptLanguage *ECMAScriptLanguage::singleton = NULL;
void ECMAScriptLanguage::init() {
ERR_FAIL_NULL(main_binder);
main_binder->initialize();
}
void ECMAScriptLanguage::finish() {
ERR_FAIL_NULL(main_binder);
main_binder->uninitialize();
main_binder->language_finalize();
}
Error ECMAScriptLanguage::execute_file(const String &p_path) {
ERR_FAIL_NULL_V(main_binder, ERR_BUG);
Error err;
String code = FileAccess::get_file_as_string(p_path, &err);
if (err == OK) {
ECMAScriptGCHandler eval_ret;
err = main_binder->eval_string(code, ECMAScriptBinder::EVAL_TYPE_GLOBAL, p_path, eval_ret);
}
return err;
}
void ECMAScriptLanguage::get_reserved_words(List<String> *p_words) const {
static const char *_reserved_words[] = {
"null",
"false",
"true",
"if",
"else",
"return",
"var",
"this",
"delete",
"void",
"typeof",
"new",
"in",
"instanceof",
"do",
"while",
"for",
"break",
"continue",
"switch",
"case",
"default",
"throw",
"try",
"catch",
"finally",
"function",
"debugger",
"with",
"class",
"const",
"enum",
"export",
"extends",
"import",
"super",
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield",
"await",
"prototype",
"constructor",
"get",
"set",
"of",
"__proto__",
"undefined",
"number",
"boolean",
"string",
"object",
"symbol",
"arguments",
"join",
"global",
"as",
"from",
"default",
"*",
"then",
"resolve",
"reject",
"promise",
"proxy",
"revoke",
"async",
"globalThis",
"Object",
"Array",
"Error",
"Number",
"String",
"Boolean",
"Symbol",
"Arguments",
"Math",
"JSON",
"Date",
"Function",
"GeneratorFunction",
"ForInIterator",
"RegExp",
"ArrayBuffer",
"SharedArrayBuffer",
"Uint8ClampedArray"
"Int8Array",
"Uint8Array",
"Int16Array",
"Uint16Array",
"Int32Array",
"Uint32Array",
"BigInt64Array",
"BigUint64Array",
"Float32Array",
"Float64Array",
"DataView",
"Map",
"Set",
"WeakMap",
"WeakSet",
"Generator",
"Proxy",
"Promise",
0
};
const char **w = _reserved_words;
while (*w) {
p_words->push_back(*w);
w++;
}
}
void ECMAScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
p_delimiters->push_back("//"); // single-line comment
p_delimiters->push_back("/* */"); // delimited comment
}
void ECMAScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
p_delimiters->push_back("' '");
p_delimiters->push_back("\" \"");
p_delimiters->push_back("` `");
}
Ref<Script> ECMAScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
String script_template = "export default class %CLASS% extends " GODOT_OBJECT_NAME ".%BASE% {\n"
" \n"
" // Declare member variables here. Examples:\n"
" a = 2;\n"
" b = \"text\";\n"
" \n"
" constructor() {\n"
" super();\n"
" }\n"
" \n"
" // Called when the node enters the scene tree for the first time.\n"
" _ready() {\n"
" \n"
" }\n"
" \n"
" // Called every frame. 'delta' is the elapsed time since the previous frame.\n"
" _process(delta) {\n"
" \n"
" }\n"
"}\n";
script_template = script_template.replace("%BASE%", p_base_class_name).replace("%CLASS%", p_class_name);
Ref<ECMAScript> script;
script.instance();
script->set_source_code(script_template);
script->set_name(p_class_name);
script->set_script_path(p_class_name);
return script;
}
void ECMAScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
String src = p_script->get_source_code();
src = src.replace("%BASE%", p_base_class_name).replace("%CLASS%", p_class_name);
p_script->set_source_code(src);
}
bool ECMAScriptLanguage::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, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
ECMAscriptScriptError script_error;
bool ret = main_binder->validate(p_script, p_path, &script_error);
if (!ret) {
r_test_error = main_binder->error_to_string(script_error);
r_line_error = script_error.line;
r_col_error = script_error.column;
}
return ret;
}
Script *ECMAScriptLanguage::create_script() const {
return memnew(ECMAScript);
}
void ECMAScriptLanguage::reload_all_scripts() {
#ifdef TOOLS_ENABLED
for (Set<Ref<ECMAScript> >::Element *E = scripts.front(); E; E = E->next()) {
reload_script(E->get(), true);
}
#endif
}
void ECMAScriptLanguage::reload_script(const Ref<Script> &p_script, bool p_soft_reload) {
Ref<ECMAScript> s = p_script;
if (s.is_valid()) {
Error err = OK;
Ref<ECMAScriptModule> module = ResourceFormatLoaderECMAScriptModule::load_static(s->get_script_path(), "", &err);
ERR_FAIL_COND_MSG(err != OK, ("Cannot load script file '" + s->get_script_path() + "'."));
s->set_source_code(module->get_source_code());
err = s->reload(p_soft_reload);
ERR_FAIL_COND_MSG(err != OK, "Parse source code from file '" + s->get_script_path() + "' failed.");
}
}
void ECMAScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back(EXT_JSMODULE);
p_extensions->push_back(EXT_JSCLASS);
p_extensions->push_back(EXT_JSON);
p_extensions->push_back(EXT_JSMODULE_ENCRYPTED);
p_extensions->push_back(EXT_JSMODULE_BYTECODE);
p_extensions->push_back(EXT_JSCLASS_ENCRYPTED);
p_extensions->push_back(EXT_JSCLASS_BYTECODE);
}
void *ECMAScriptLanguage::alloc_instance_binding_data(Object *p_object) {
if (ECMAScriptBinder *binder = get_thread_binder(Thread::get_caller_id())) {
return binder->alloc_object_binding_data(p_object);
}
return NULL;
}
void ECMAScriptLanguage::free_instance_binding_data(void *p_data) {
if (ECMAScriptBinder *binder = get_thread_binder(Thread::get_caller_id())) {
return binder->free_object_binding_data(p_data);
}
}
void ECMAScriptLanguage::refcount_incremented_instance_binding(Object *p_object) {
if (ECMAScriptBinder *binder = get_thread_binder(Thread::get_caller_id())) {
binder->godot_refcount_incremented(static_cast<Reference *>(p_object));
}
}
bool ECMAScriptLanguage::refcount_decremented_instance_binding(Object *p_object) {
if (ECMAScriptBinder *binder = get_thread_binder(Thread::get_caller_id())) {
return binder->godot_refcount_decremented(static_cast<Reference *>(p_object));
}
return true;
}
void ECMAScriptLanguage::frame() {
main_binder->frame();
}
String ECMAScriptLanguage::globalize_relative_path(const String &p_relative, const String &p_base_dir) {
String file = p_relative;
if (file.begins_with(".")) {
String base_dir = p_base_dir;
while (base_dir.ends_with(".")) {
if (base_dir.ends_with("..")) {
base_dir = base_dir.get_base_dir().get_base_dir();
} else {
base_dir = base_dir.get_base_dir();
}
}
String file_path = file;
while (file_path.begins_with(".")) {
if (file_path.begins_with("../")) {
base_dir = base_dir.get_base_dir();
file_path = file_path.substr(3);
} else if (file_path.begins_with("./")) {
file_path = file_path.substr(2);
} else {
file_path = file_path.get_basename();
break;
}
}
if (!base_dir.ends_with("/")) base_dir += "/";
file = base_dir + file_path;
}
return file;
}
ECMAScriptLanguage::ECMAScriptLanguage() {
ERR_FAIL_COND(singleton);
singleton = this;
main_binder = memnew(QuickJSBinder);
}
ECMAScriptLanguage::~ECMAScriptLanguage() {
memdelete(main_binder);
}