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
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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-10-23 16:33:20 +00:00
|
|
|
extern "C" {
|
|
|
|
union js_eval_ret {
|
|
|
|
uint32_t b;
|
|
|
|
double d;
|
|
|
|
char *s;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern int godot_js_eval(const char *p_js, int p_use_global_ctx, union js_eval_ret *p_union_ptr, void *p_byte_arr, void *p_byte_arr_write, void *(*p_callback)(void *p_ptr, void *p_ptr2, int p_len));
|
2017-10-10 21:37:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 16:33:20 +00:00
|
|
|
void *resize_PackedByteArray_and_open_write(void *p_arr, void *r_write, int p_len) {
|
|
|
|
PackedByteArray *arr = (PackedByteArray *)p_arr;
|
|
|
|
VectorWriteProxy<uint8_t> *write = (VectorWriteProxy<uint8_t> *)r_write;
|
|
|
|
arr->resize(p_len);
|
|
|
|
*write = arr->write;
|
|
|
|
return arr->ptrw();
|
|
|
|
}
|
2017-10-10 21:37:16 +00:00
|
|
|
|
2020-10-23 16:33:20 +00:00
|
|
|
Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) {
|
|
|
|
union js_eval_ret js_data;
|
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
|
|
|
|
2020-10-23 16:33:20 +00:00
|
|
|
Variant::Type return_type = static_cast<Variant::Type>(godot_js_eval(p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write, resize_PackedByteArray_and_open_write));
|
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);
|
2020-10-23 16:33:20 +00:00
|
|
|
free(js_data.s); // Must free the string allocated in JS.
|
2017-03-05 15:44:50 +00:00
|
|
|
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
|