Debian Codegraph @

platform/web/js/libs/library_godot_javascript_singleton.js

raw symbols SCIP
1 /**************************************************************************/
2 /* library_godot_javascript_singleton.js */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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 const GodotJSWrapper = {
32
33 $GodotJSWrapper__deps: ['$GodotRuntime', '$IDHandler'],
34 $GodotJSWrapper__postset: 'GodotJSWrapper.proxies = new Map();',
35 $GodotJSWrapper: {
36 proxies: null,
37 cb_ret: null,
38
39 MyProxy: function (val) {
40 const id = IDHandler.add(this);
41 GodotJSWrapper.proxies.set(val, id);
42 let refs = 1;
43 this.ref = function () {
44 refs++;
45 };
46 this.unref = function () {
47 refs--;
48 if (refs === 0) {
49 IDHandler.remove(id);
50 GodotJSWrapper.proxies.delete(val);
51 }
52 };
53 this.get_val = function () {
54 return val;
55 };
56 this.get_id = function () {
57 return id;
58 };
59 },
60
61 get_proxied: function (val) {
62 const id = GodotJSWrapper.proxies.get(val);
63 if (id === undefined) {
64 const proxy = new GodotJSWrapper.MyProxy(val);
65 return proxy.get_id();
66 }
67 IDHandler.get(id).ref();
68 return id;
69 },
70
71 get_proxied_value: function (id) {
72 const proxy = IDHandler.get(id);
73 if (proxy === undefined) {
74 return undefined;
75 }
76 return proxy.get_val();
77 },
78
79 variant2js: function (type, val) {
80 switch (type) {
81 case 0:
82 return null;
83 case 1:
84 return Boolean(GodotRuntime.getHeapValue(val, 'i64'));
85 case 2: {
86 // `heap_value` may be a bigint.
87 const heap_value = GodotRuntime.getHeapValue(val, 'i64');
88 return heap_value >= Number.MIN_SAFE_INTEGER && heap_value <= Number.MAX_SAFE_INTEGER
89 ? Number(heap_value)
90 : heap_value;
91 }
92 case 3:
93 return Number(GodotRuntime.getHeapValue(val, 'double'));
94 case 4:
95 return GodotRuntime.parseString(GodotRuntime.getHeapValue(val, '*'));
96 case 24: // OBJECT
97 return GodotJSWrapper.get_proxied_value(GodotRuntime.getHeapValue(val, 'i64'));
98 default:
99 return undefined;
100 }
101 },
102
103 js2variant: function (p_val, p_exchange) {
104 if (p_val === undefined || p_val === null) {
105 return 0; // NIL
106 }
107 const type = typeof (p_val);
108 if (type === 'boolean') {
109 GodotRuntime.setHeapValue(p_exchange, p_val, 'i64');
110 return 1; // BOOL
111 } else if (type === 'number') {
112 if (Number.isInteger(p_val)) {
113 GodotRuntime.setHeapValue(p_exchange, p_val, 'i64');
114 return 2; // INT
115 }
116 GodotRuntime.setHeapValue(p_exchange, p_val, 'double');
117 return 3; // FLOAT
118 } else if (type === 'bigint') {
119 GodotRuntime.setHeapValue(p_exchange, p_val, 'i64');
120 return 2; // INT
121 } else if (type === 'string') {
122 const c_str = GodotRuntime.allocString(p_val);
123 GodotRuntime.setHeapValue(p_exchange, c_str, '*');
124 return 4; // STRING
125 }
126 const id = GodotJSWrapper.get_proxied(p_val);
127 GodotRuntime.setHeapValue(p_exchange, id, 'i64');
128 return 24; // OBJECT
129 },
130
131 isBuffer: function (obj) {
132 return obj instanceof ArrayBuffer || ArrayBuffer.isView(obj);
133 },
134 },
135
136 godot_js_wrapper_interface_get__proxy: 'sync',
137 godot_js_wrapper_interface_get__sig: 'ii',
138 godot_js_wrapper_interface_get: function (p_name) {
139 const name = GodotRuntime.parseString(p_name);
140 if (typeof (window[name]) !== 'undefined') {
141 return GodotJSWrapper.get_proxied(window[name]);
142 }
143 return 0;
144 },
145
146 godot_js_wrapper_object_get__proxy: 'sync',
147 godot_js_wrapper_object_get__sig: 'iiii',
148 godot_js_wrapper_object_get: function (p_id, p_exchange, p_prop) {
149 const obj = GodotJSWrapper.get_proxied_value(p_id);
150 if (obj === undefined) {
151 return 0;
152 }
153 if (p_prop) {
154 const prop = GodotRuntime.parseString(p_prop);
155 try {
156 return GodotJSWrapper.js2variant(obj[prop], p_exchange);
157 } catch (e) {
158 GodotRuntime.error(`Error getting variable ${prop} on object`, obj);
159 return 0; // NIL
160 }
161 }
162 return GodotJSWrapper.js2variant(obj, p_exchange);
163 },
164
165 godot_js_wrapper_object_set__proxy: 'sync',
166 godot_js_wrapper_object_set__sig: 'viiii',
167 godot_js_wrapper_object_set: function (p_id, p_name, p_type, p_exchange) {
168 const obj = GodotJSWrapper.get_proxied_value(p_id);
169 if (obj === undefined) {
170 return;
171 }
172 const name = GodotRuntime.parseString(p_name);
173 try {
174 obj[name] = GodotJSWrapper.variant2js(p_type, p_exchange);
175 } catch (e) {
176 GodotRuntime.error(`Error setting variable ${name} on object`, obj);
177 }
178 },
179
180 godot_js_wrapper_object_call__proxy: 'sync',
181 godot_js_wrapper_object_call__sig: 'iiiiiiiii',
182 godot_js_wrapper_object_call: function (p_id, p_method, p_args, p_argc, p_convert_callback, p_exchange, p_lock, p_free_lock_callback) {
183 const obj = GodotJSWrapper.get_proxied_value(p_id);
184 if (obj === undefined) {
185 return -1;
186 }
187 const method = GodotRuntime.parseString(p_method);
188 const convert = GodotRuntime.get_func(p_convert_callback);
189 const freeLock = GodotRuntime.get_func(p_free_lock_callback);
190 const args = new Array(p_argc);
191 for (let i = 0; i < p_argc; i++) {
192 const type = convert(p_args, i, p_exchange, p_lock);
193 const lock = GodotRuntime.getHeapValue(p_lock, '*');
194 args[i] = GodotJSWrapper.variant2js(type, p_exchange);
195 if (lock) {
196 freeLock(p_lock, type);
197 }
198 }
199 try {
200 const res = obj[method](...args);
201 return GodotJSWrapper.js2variant(res, p_exchange);
202 } catch (e) {
203 GodotRuntime.error(`Error calling method ${method} on:`, obj, 'error:', e);
204 return -1;
205 }
206 },
207
208 godot_js_wrapper_object_unref__proxy: 'sync',
209 godot_js_wrapper_object_unref__sig: 'vi',
210 godot_js_wrapper_object_unref: function (p_id) {
211 const proxy = IDHandler.get(p_id);
212 if (proxy !== undefined) {
213 proxy.unref();
214 }
215 },
216
217 godot_js_wrapper_create_cb__proxy: 'sync',
218 godot_js_wrapper_create_cb__sig: 'iii',
219 godot_js_wrapper_create_cb: function (p_ref, p_func) {
220 const func = GodotRuntime.get_func(p_func);
221 let id = 0;
222 const cb = function () {
223 if (!GodotJSWrapper.get_proxied_value(id)) {
224 return undefined;
225 }
226 // The callback will store the returned value in this variable via
227 // "godot_js_wrapper_object_set_cb_ret" upon calling the user function.
228 // This is safe! JavaScript is single threaded (and using it in threads is not a good idea anyway).
229 GodotJSWrapper.cb_ret = null;
230 const args = Array.from(arguments);
231 const argsProxy = new GodotJSWrapper.MyProxy(args);
232 func(p_ref, argsProxy.get_id(), args.length);
233 argsProxy.unref();
234 const ret = GodotJSWrapper.cb_ret;
235 GodotJSWrapper.cb_ret = null;
236 return ret;
237 };
238 id = GodotJSWrapper.get_proxied(cb);
239 return id;
240 },
241
242 godot_js_wrapper_object_set_cb_ret__proxy: 'sync',
243 godot_js_wrapper_object_set_cb_ret__sig: 'vii',
244 godot_js_wrapper_object_set_cb_ret: function (p_val_type, p_val_ex) {
245 GodotJSWrapper.cb_ret = GodotJSWrapper.variant2js(p_val_type, p_val_ex);
246 },
247
248 godot_js_wrapper_object_getvar__proxy: 'sync',
249 godot_js_wrapper_object_getvar__sig: 'iiii',
250 godot_js_wrapper_object_getvar: function (p_id, p_type, p_exchange) {
251 const obj = GodotJSWrapper.get_proxied_value(p_id);
252 if (obj === undefined) {
253 return -1;
254 }
255 const prop = GodotJSWrapper.variant2js(p_type, p_exchange);
256 if (prop === undefined || prop === null) {
257 return -1;
258 }
259 try {
260 return GodotJSWrapper.js2variant(obj[prop], p_exchange);
261 } catch (e) {
262 GodotRuntime.error(`Error getting variable ${prop} on object`, obj, e);
263 return -1;
264 }
265 },
266
267 godot_js_wrapper_object_setvar__proxy: 'sync',
268 godot_js_wrapper_object_setvar__sig: 'iiiiii',
269 godot_js_wrapper_object_setvar: function (p_id, p_key_type, p_key_ex, p_val_type, p_val_ex) {
270 const obj = GodotJSWrapper.get_proxied_value(p_id);
271 if (obj === undefined) {
272 return -1;
273 }
274 const key = GodotJSWrapper.variant2js(p_key_type, p_key_ex);
275 try {
276 obj[key] = GodotJSWrapper.variant2js(p_val_type, p_val_ex);
277 return 0;
278 } catch (e) {
279 GodotRuntime.error(`Error setting variable ${key} on object`, obj);
280 return -1;
281 }
282 },
283
284 godot_js_wrapper_create_object__proxy: 'sync',
285 godot_js_wrapper_create_object__sig: 'iiiiiiii',
286 godot_js_wrapper_create_object: function (p_object, p_args, p_argc, p_convert_callback, p_exchange, p_lock, p_free_lock_callback) {
287 const name = GodotRuntime.parseString(p_object);
288 if (typeof (window[name]) === 'undefined') {
289 return -1;
290 }
291 const convert = GodotRuntime.get_func(p_convert_callback);
292 const freeLock = GodotRuntime.get_func(p_free_lock_callback);
293 const args = new Array(p_argc);
294 for (let i = 0; i < p_argc; i++) {
295 const type = convert(p_args, i, p_exchange, p_lock);
296 const lock = GodotRuntime.getHeapValue(p_lock, '*');
297 args[i] = GodotJSWrapper.variant2js(type, p_exchange);
298 if (lock) {
299 freeLock(p_lock, type);
300 }
301 }
302 try {
303 const res = new window[name](...args);
304 return GodotJSWrapper.js2variant(res, p_exchange);
305 } catch (e) {
306 GodotRuntime.error(`Error calling constructor ${name} with args:`, args, 'error:', e);
307 return -1;
308 }
309 },
310
311 godot_js_wrapper_object_is_buffer__proxy: 'sync',
312 godot_js_wrapper_object_is_buffer__sig: 'ii',
313 godot_js_wrapper_object_is_buffer: function (p_id) {
314 const obj = GodotJSWrapper.get_proxied_value(p_id);
315 return GodotJSWrapper.isBuffer(obj)
316 ? 1
317 : 0;
318 },
319
320 godot_js_wrapper_object_transfer_buffer__proxy: 'sync',
321 godot_js_wrapper_object_transfer_buffer__sig: 'viiii',
322 godot_js_wrapper_object_transfer_buffer: function (p_id, p_byte_arr, p_byte_arr_write, p_callback) {
323 let obj = GodotJSWrapper.get_proxied_value(p_id);
324 if (!GodotJSWrapper.isBuffer(obj)) {
325 return;
326 }
327
328 if (ArrayBuffer.isView(obj) && !(obj instanceof Uint8Array)) {
329 obj = new Uint8Array(obj.buffer);
330 } else if (obj instanceof ArrayBuffer) {
331 obj = new Uint8Array(obj);
332 }
333
334 const resizePackedByteArrayAndOpenWrite = GodotRuntime.get_func(p_callback);
335 const bytesPtr = resizePackedByteArrayAndOpenWrite(p_byte_arr, p_byte_arr_write, obj.length);
336 HEAPU8.set(obj, bytesPtr);
337 },
338 };
339
340 autoAddDeps(GodotJSWrapper, '$GodotJSWrapper');
341 mergeInto(LibraryManager.library, GodotJSWrapper);
342
343 const GodotEval = {
344 godot_js_eval__deps: ['$GodotRuntime'],
345 godot_js_eval__sig: 'iiiiiii',
346 godot_js_eval: function (p_js, p_use_global_ctx, p_union_ptr, p_byte_arr, p_byte_arr_write, p_callback) {
347 const js_code = GodotRuntime.parseString(p_js);
348 let eval_ret = null;
349 try {
350 if (p_use_global_ctx) {
351 // indirect eval call grants global execution context
352 const global_eval = eval; // eslint-disable-line no-eval
353 eval_ret = global_eval(js_code);
354 } else {
355 eval_ret = eval(js_code); // eslint-disable-line no-eval
356 }
357 } catch (e) {
358 GodotRuntime.error(e);
359 }
360
361 switch (typeof eval_ret) {
362 case 'boolean':
363 GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'i32');
364 return 1; // BOOL
365
366 case 'number':
367 GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'double');
368 return 3; // FLOAT
369
370 case 'string':
371 GodotRuntime.setHeapValue(p_union_ptr, GodotRuntime.allocString(eval_ret), '*');
372 return 4; // STRING
373
374 case 'object':
375 if (eval_ret === null) {
376 break;
377 }
378
379 if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
380 eval_ret = new Uint8Array(eval_ret.buffer);
381 } else if (eval_ret instanceof ArrayBuffer) {
382 eval_ret = new Uint8Array(eval_ret);
383 }
384 if (eval_ret instanceof Uint8Array) {
385 const func = GodotRuntime.get_func(p_callback);
386 const bytes_ptr = func(p_byte_arr, p_byte_arr_write, eval_ret.length);
387 HEAPU8.set(eval_ret, bytes_ptr);
388 return 29; // PACKED_BYTE_ARRAY
389 }
390 break;
391
392 // no default
393 }
394 return 0; // NIL
395 },
396 };
397
398 mergeInto(LibraryManager.library, GodotEval);