2016-04-11 21:22:14 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* javascript_eval.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-04-11 21:22:14 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-04-11 21:22:14 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2016-04-11 21:22:14 +00:00
|
|
|
#ifdef JAVASCRIPT_EVAL_ENABLED
|
|
|
|
|
2017-11-16 00:33:48 +00:00
|
|
|
#include "api/javascript_eval.h"
|
2016-04-11 21:22:14 +00:00
|
|
|
#include "emscripten.h"
|
|
|
|
|
2020-02-25 09:15:45 +00:00
|
|
|
extern "C" EMSCRIPTEN_KEEPALIVE uint8_t *resize_PackedByteArray_and_open_write(PackedByteArray *p_arr, VectorWriteProxy<uint8_t> *r_write, int p_len) {
|
2017-10-10 21:37:16 +00:00
|
|
|
|
|
|
|
p_arr->resize(p_len);
|
2020-02-25 09:15:45 +00:00
|
|
|
*r_write = p_arr->write;
|
|
|
|
return p_arr->ptrw();
|
2017-10-10 21:37:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) {
|
2016-04-11 21:22:14 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
union {
|
2017-10-10 21:37:16 +00:00
|
|
|
bool b;
|
2017-03-05 15:44:50 +00:00
|
|
|
double d;
|
|
|
|
char *s;
|
2017-11-25 14:38:02 +00:00
|
|
|
} js_data;
|
2017-10-10 21:37:16 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
PackedByteArray arr;
|
2020-02-25 09:15:45 +00:00
|
|
|
VectorWriteProxy<uint8_t> arr_write;
|
2017-10-10 21:37:16 +00:00
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2016-04-11 21:22:14 +00:00
|
|
|
Variant::Type return_type = static_cast<Variant::Type>(EM_ASM_INT({
|
|
|
|
|
2017-10-10 21:37:16 +00:00
|
|
|
const CODE = $0;
|
|
|
|
const USE_GLOBAL_EXEC_CONTEXT = $1;
|
|
|
|
const PTR = $2;
|
2017-11-25 14:38:02 +00:00
|
|
|
const BYTEARRAY_PTR = $3;
|
|
|
|
const BYTEARRAY_WRITE_PTR = $4;
|
2016-04-11 21:22:14 +00:00
|
|
|
var eval_ret;
|
|
|
|
try {
|
2017-10-10 21:37:16 +00:00
|
|
|
if (USE_GLOBAL_EXEC_CONTEXT) {
|
2016-04-11 21:22:14 +00:00
|
|
|
// indirect eval call grants global execution context
|
|
|
|
var global_eval = eval;
|
2017-10-10 21:37:16 +00:00
|
|
|
eval_ret = global_eval(UTF8ToString(CODE));
|
2017-01-15 19:00:04 +00:00
|
|
|
} else {
|
2017-10-10 21:37:16 +00:00
|
|
|
eval_ret = eval(UTF8ToString(CODE));
|
2016-04-11 21:22:14 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-02-24 03:40:06 +00:00
|
|
|
err(e);
|
2016-04-11 21:22:14 +00:00
|
|
|
eval_ret = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (typeof eval_ret) {
|
|
|
|
|
|
|
|
case 'boolean':
|
2017-10-10 21:37:16 +00:00
|
|
|
setValue(PTR, eval_ret, 'i32');
|
2016-04-11 21:22:14 +00:00
|
|
|
return 1; // BOOL
|
|
|
|
|
|
|
|
case 'number':
|
2017-10-10 21:37:16 +00:00
|
|
|
setValue(PTR, eval_ret, 'double');
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
return 3; // FLOAT
|
2016-04-11 21:22:14 +00:00
|
|
|
|
|
|
|
case 'string':
|
|
|
|
var array_len = lengthBytesUTF8(eval_ret)+1;
|
|
|
|
var array_ptr = _malloc(array_len);
|
|
|
|
try {
|
|
|
|
if (array_ptr===0) {
|
|
|
|
throw new Error('String allocation failed (probably out of memory)');
|
|
|
|
}
|
2017-10-10 21:37:16 +00:00
|
|
|
setValue(PTR, array_ptr , '*');
|
2016-04-11 21:22:14 +00:00
|
|
|
stringToUTF8(eval_ret, array_ptr, array_len);
|
|
|
|
return 4; // STRING
|
|
|
|
} catch (e) {
|
|
|
|
if (array_ptr!==0) {
|
|
|
|
_free(array_ptr)
|
|
|
|
}
|
2019-02-24 03:40:06 +00:00
|
|
|
err(e);
|
2016-04-11 21:22:14 +00:00
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'object':
|
|
|
|
if (eval_ret === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-10 21:37:16 +00:00
|
|
|
if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
|
|
|
|
eval_ret = new Uint8Array(eval_ret.buffer);
|
|
|
|
}
|
|
|
|
else if (eval_ret instanceof ArrayBuffer) {
|
|
|
|
eval_ret = new Uint8Array(eval_ret);
|
|
|
|
}
|
|
|
|
if (eval_ret instanceof Uint8Array) {
|
2020-02-17 21:06:54 +00:00
|
|
|
var bytes_ptr = ccall('resize_PackedByteArray_and_open_write', 'number', ['number', 'number' ,'number'], [BYTEARRAY_PTR, BYTEARRAY_WRITE_PTR, eval_ret.length]);
|
2017-10-10 21:37:16 +00:00
|
|
|
HEAPU8.set(eval_ret, bytes_ptr);
|
2020-02-17 21:06:54 +00:00
|
|
|
return 20; // PACKED_BYTE_ARRAY
|
2017-10-10 21:37:16 +00:00
|
|
|
}
|
2016-04-11 21:22:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0; // NIL
|
|
|
|
|
2017-11-25 14:38:02 +00:00
|
|
|
}, p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write));
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format on */
|
2016-04-11 21:22:14 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (return_type) {
|
2016-04-11 21:22:14 +00:00
|
|
|
case Variant::BOOL:
|
2017-11-25 14:38:02 +00:00
|
|
|
return js_data.b;
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
case Variant::FLOAT:
|
2017-11-25 14:38:02 +00:00
|
|
|
return js_data.d;
|
2017-03-05 15:44:50 +00:00
|
|
|
case Variant::STRING: {
|
2017-11-25 14:38:02 +00:00
|
|
|
String str = String::utf8(js_data.s);
|
2017-03-05 15:44:50 +00:00
|
|
|
/* clang-format off */
|
2017-11-25 14:38:02 +00:00
|
|
|
EM_ASM_({ _free($0); }, js_data.s);
|
2017-03-05 15:44:50 +00:00
|
|
|
/* clang-format on */
|
|
|
|
return str;
|
|
|
|
}
|
2020-02-17 21:06:54 +00:00
|
|
|
case Variant::PACKED_BYTE_ARRAY:
|
2020-02-25 09:15:45 +00:00
|
|
|
arr_write = VectorWriteProxy<uint8_t>();
|
2017-10-10 21:37:16 +00:00
|
|
|
return arr;
|
2018-10-02 00:30:27 +00:00
|
|
|
default:
|
|
|
|
return Variant();
|
2016-04-11 21:22:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // JAVASCRIPT_EVAL_ENABLED
|