From 48f9069f3a443204fb20d5da468093265d03553c Mon Sep 17 00:00:00 2001 From: Geequlim Date: Fri, 2 Sep 2022 20:40:41 +0800 Subject: Rename JavaScript singleton to JavaScriptBridge --- doc/classes/@GlobalScope.xml | 4 +- doc/classes/JavaScript.xml | 79 ------ doc/classes/JavaScriptBridge.xml | 79 ++++++ doc/classes/JavaScriptObject.xml | 16 +- platform/web/SCsub | 2 +- platform/web/api/api.cpp | 52 ++-- platform/web/api/javascript_bridge_singleton.h | 70 +++++ platform/web/api/javascript_singleton.h | 70 ----- platform/web/javascript_bridge_singleton.cpp | 366 +++++++++++++++++++++++++ platform/web/javascript_singleton.cpp | 366 ------------------------- platform/web/os_web.cpp | 6 +- 11 files changed, 555 insertions(+), 555 deletions(-) delete mode 100644 doc/classes/JavaScript.xml create mode 100644 doc/classes/JavaScriptBridge.xml create mode 100644 platform/web/api/javascript_bridge_singleton.h delete mode 100644 platform/web/api/javascript_singleton.h create mode 100644 platform/web/javascript_bridge_singleton.cpp delete mode 100644 platform/web/javascript_singleton.cpp diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index 71b0901b3d..e06385de62 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -1275,8 +1275,8 @@ The [JavaClassWrapper] singleton. [b]Note:[/b] Only implemented on Android. - - The [JavaScript] singleton. + + The [JavaScriptBridge] singleton. [b]Note:[/b] Only implemented on the Web platform. diff --git a/doc/classes/JavaScript.xml b/doc/classes/JavaScript.xml deleted file mode 100644 index c838422e85..0000000000 --- a/doc/classes/JavaScript.xml +++ /dev/null @@ -1,79 +0,0 @@ - - - - Singleton that connects the engine with the browser's JavaScript context in Web export. - - - The JavaScript singleton is implemented only in the Web export. It's used to access the browser's JavaScript context. This allows interaction with embedding pages or calling third-party JavaScript APIs. - [b]Note:[/b] This singleton can be disabled at build-time to improve security. By default, the JavaScript singleton is enabled. Official export templates also have the JavaScript singleton enabled. See [url=$DOCS_URL/development/compiling/compiling_for_web.html]Compiling for the Web[/url] in the documentation for more information. - - - $DOCS_URL/tutorials/export/exporting_for_web.html#calling-javascript-from-script - - - - - - - Creates a reference to a [Callable] that can be used as a callback by JavaScript. The reference must be kept until the callback happens, or it won't be called at all. See [JavaScriptObject] for usage. - - - - - - - Creates a new JavaScript object using the [code]new[/code] constructor. The [param object] must a valid property of the JavaScript [code]window[/code]. See [JavaScriptObject] for usage. - - - - - - - - - Prompts the user to download a file containing the specified [param buffer]. The file will have the given [param name] and [param mime] type. - [b]Note:[/b] The browser may override the [url=https://en.wikipedia.org/wiki/Media_type]MIME type[/url] provided based on the file [param name]'s extension. - [b]Note:[/b] Browsers might block the download if [method download_buffer] is not being called from a user interaction (e.g. button click). - [b]Note:[/b] Browsers might ask the user for permission or block the download if multiple download requests are made in a quick succession. - - - - - - - - Execute the string [param code] as JavaScript code within the browser window. This is a call to the actual global JavaScript function [code]eval()[/code]. - If [param use_global_execution_context] is [code]true[/code], the code will be evaluated in the global execution context. Otherwise, it is evaluated in the execution context of a function within the engine's runtime environment. - - - - - - - Returns an interface to a JavaScript object that can be used by scripts. The [param interface] must be a valid property of the JavaScript [code]window[/code]. The callback must accept a single [Array] argument, which will contain the JavaScript [code]arguments[/code]. See [JavaScriptObject] for usage. - - - - - - Returns [code]true[/code] if a new version of the progressive web app is waiting to be activated. - [b]Note:[/b] Only relevant when exported as a Progressive Web App. - - - - - - Performs the live update of the progressive web app. Forcing the new version to be installed and the page to be reloaded. - [b]Note:[/b] Your application will be [b]reloaded in all browser tabs[/b]. - [b]Note:[/b] Only relevant when exported as a Progressive Web App and [method pwa_needs_update] returns [code]true[/code]. - - - - - - - Emitted when an update for this progressive web app has been detected but is waiting to be activated because a previous version is active. See [method pwa_update] to force the update to take place immediately. - - - - diff --git a/doc/classes/JavaScriptBridge.xml b/doc/classes/JavaScriptBridge.xml new file mode 100644 index 0000000000..5e36b5cc80 --- /dev/null +++ b/doc/classes/JavaScriptBridge.xml @@ -0,0 +1,79 @@ + + + + Singleton that connects the engine with the browser's JavaScript context in Web export. + + + The JavaScriptBridge singleton is implemented only in the Web export. It's used to access the browser's JavaScript context. This allows interaction with embedding pages or calling third-party JavaScript APIs. + [b]Note:[/b] This singleton can be disabled at build-time to improve security. By default, the JavaScriptBridge singleton is enabled. Official export templates also have the JavaScriptBridge singleton enabled. See [url=$DOCS_URL/development/compiling/compiling_for_web.html]Compiling for the Web[/url] in the documentation for more information. + + + $DOCS_URL/tutorials/export/exporting_for_web.html#calling-javascript-from-script + + + + + + + Creates a reference to a [Callable] that can be used as a callback by JavaScript. The reference must be kept until the callback happens, or it won't be called at all. See [JavaScriptObject] for usage. + + + + + + + Creates a new JavaScript object using the [code]new[/code] constructor. The [param object] must a valid property of the JavaScript [code]window[/code]. See [JavaScriptObject] for usage. + + + + + + + + + Prompts the user to download a file containing the specified [param buffer]. The file will have the given [param name] and [param mime] type. + [b]Note:[/b] The browser may override the [url=https://en.wikipedia.org/wiki/Media_type]MIME type[/url] provided based on the file [param name]'s extension. + [b]Note:[/b] Browsers might block the download if [method download_buffer] is not being called from a user interaction (e.g. button click). + [b]Note:[/b] Browsers might ask the user for permission or block the download if multiple download requests are made in a quick succession. + + + + + + + + Execute the string [param code] as JavaScript code within the browser window. This is a call to the actual global JavaScript function [code]eval()[/code]. + If [param use_global_execution_context] is [code]true[/code], the code will be evaluated in the global execution context. Otherwise, it is evaluated in the execution context of a function within the engine's runtime environment. + + + + + + + Returns an interface to a JavaScript object that can be used by scripts. The [param interface] must be a valid property of the JavaScript [code]window[/code]. The callback must accept a single [Array] argument, which will contain the JavaScript [code]arguments[/code]. See [JavaScriptObject] for usage. + + + + + + Returns [code]true[/code] if a new version of the progressive web app is waiting to be activated. + [b]Note:[/b] Only relevant when exported as a Progressive Web App. + + + + + + Performs the live update of the progressive web app. Forcing the new version to be installed and the page to be reloaded. + [b]Note:[/b] Your application will be [b]reloaded in all browser tabs[/b]. + [b]Note:[/b] Only relevant when exported as a Progressive Web App and [method pwa_needs_update] returns [code]true[/code]. + + + + + + + Emitted when an update for this progressive web app has been detected but is waiting to be activated because a previous version is active. See [method pwa_update] to force the update to take place immediately. + + + + diff --git a/doc/classes/JavaScriptObject.xml b/doc/classes/JavaScriptObject.xml index e279acf5a4..26792bd19e 100644 --- a/doc/classes/JavaScriptObject.xml +++ b/doc/classes/JavaScriptObject.xml @@ -1,27 +1,27 @@ - A wrapper class for native JavaScript objects. + A wrapper class for web native JavaScript objects. - JavaScriptObject is used to interact with JavaScript objects retrieved or created via [method JavaScript.get_interface], [method JavaScript.create_object], or [method JavaScript.create_callback]. + JavaScriptObject is used to interact with JavaScript objects retrieved or created via [method JavaScriptBridge.get_interface], [method JavaScriptBridge.create_object], or [method JavaScriptBridge.create_callback]. Example: [codeblock] extends Node - var _my_js_callback = JavaScript.create_callback(self, "myCallback") # This reference must be kept - var console = JavaScript.get_interface("console") + var _my_js_callback = JavaScriptBridge.create_callback(self, "myCallback") # This reference must be kept + var console = JavaScriptBridge.get_interface("console") func _init(): - var buf = JavaScript.create_object("ArrayBuffer", 10) # new ArrayBuffer(10) + var buf = JavaScriptBridge.create_object("ArrayBuffer", 10) # new ArrayBuffer(10) print(buf) # prints [JavaScriptObject:OBJECT_ID] - var uint8arr = JavaScript.create_object("Uint8Array", buf) # new Uint8Array(buf) + var uint8arr = JavaScriptBridge.create_object("Uint8Array", buf) # new Uint8Array(buf) uint8arr[1] = 255 prints(uint8arr[1], uint8arr.byteLength) # prints 255 10 console.log(uint8arr) # prints in browser console "Uint8Array(10) [ 0, 255, 0, 0, 0, 0, 0, 0, 0, 0 ]" - # Equivalent of JavaScript: Array.from(uint8arr).forEach(myCallback) - JavaScript.get_interface("Array").from(uint8arr).forEach(_my_js_callback) + # Equivalent of JavaScriptBridge: Array.from(uint8arr).forEach(myCallback) + JavaScriptBridge.get_interface("Array").from(uint8arr).forEach(_my_js_callback) func myCallback(args): # Will be called with the parameters passed to the "forEach" callback diff --git a/platform/web/SCsub b/platform/web/SCsub index ae9d628857..e8d0181ede 100644 --- a/platform/web/SCsub +++ b/platform/web/SCsub @@ -6,7 +6,7 @@ web_files = [ "audio_driver_web.cpp", "display_server_web.cpp", "http_client_web.cpp", - "javascript_singleton.cpp", + "javascript_bridge_singleton.cpp", "web_main.cpp", "os_web.cpp", "api/web_tools_editor_plugin.cpp", diff --git a/platform/web/api/api.cpp b/platform/web/api/api.cpp index a724b0456d..e637f2aef2 100644 --- a/platform/web/api/api.cpp +++ b/platform/web/api/api.cpp @@ -30,66 +30,66 @@ #include "api.h" #include "core/config/engine.h" -#include "javascript_singleton.h" +#include "javascript_bridge_singleton.h" #include "web_tools_editor_plugin.h" -static JavaScript *javascript_singleton; +static JavaScriptBridge *javascript_bridge_singleton; void register_web_api() { WebToolsEditorPlugin::initialize(); GDREGISTER_ABSTRACT_CLASS(JavaScriptObject); - GDREGISTER_ABSTRACT_CLASS(JavaScript); - javascript_singleton = memnew(JavaScript); - Engine::get_singleton()->add_singleton(Engine::Singleton("JavaScript", javascript_singleton)); + GDREGISTER_ABSTRACT_CLASS(JavaScriptBridge); + javascript_bridge_singleton = memnew(JavaScriptBridge); + Engine::get_singleton()->add_singleton(Engine::Singleton("JavaScriptBridge", javascript_bridge_singleton)); } void unregister_web_api() { - memdelete(javascript_singleton); + memdelete(javascript_bridge_singleton); } -JavaScript *JavaScript::singleton = nullptr; +JavaScriptBridge *JavaScriptBridge::singleton = nullptr; -JavaScript *JavaScript::get_singleton() { +JavaScriptBridge *JavaScriptBridge::get_singleton() { return singleton; } -JavaScript::JavaScript() { - ERR_FAIL_COND_MSG(singleton != nullptr, "JavaScript singleton already exist."); +JavaScriptBridge::JavaScriptBridge() { + ERR_FAIL_COND_MSG(singleton != nullptr, "JavaScriptBridge singleton already exist."); singleton = this; } -JavaScript::~JavaScript() {} +JavaScriptBridge::~JavaScriptBridge() {} -void JavaScript::_bind_methods() { - ClassDB::bind_method(D_METHOD("eval", "code", "use_global_execution_context"), &JavaScript::eval, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("get_interface", "interface"), &JavaScript::get_interface); - ClassDB::bind_method(D_METHOD("create_callback", "callable"), &JavaScript::create_callback); +void JavaScriptBridge::_bind_methods() { + ClassDB::bind_method(D_METHOD("eval", "code", "use_global_execution_context"), &JavaScriptBridge::eval, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("get_interface", "interface"), &JavaScriptBridge::get_interface); + ClassDB::bind_method(D_METHOD("create_callback", "callable"), &JavaScriptBridge::create_callback); { MethodInfo mi; mi.name = "create_object"; mi.arguments.push_back(PropertyInfo(Variant::STRING, "object")); - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "create_object", &JavaScript::_create_object_bind, mi); + ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "create_object", &JavaScriptBridge::_create_object_bind, mi); } - ClassDB::bind_method(D_METHOD("download_buffer", "buffer", "name", "mime"), &JavaScript::download_buffer, DEFVAL("application/octet-stream")); - ClassDB::bind_method(D_METHOD("pwa_needs_update"), &JavaScript::pwa_needs_update); - ClassDB::bind_method(D_METHOD("pwa_update"), &JavaScript::pwa_update); + ClassDB::bind_method(D_METHOD("download_buffer", "buffer", "name", "mime"), &JavaScriptBridge::download_buffer, DEFVAL("application/octet-stream")); + ClassDB::bind_method(D_METHOD("pwa_needs_update"), &JavaScriptBridge::pwa_needs_update); + ClassDB::bind_method(D_METHOD("pwa_update"), &JavaScriptBridge::pwa_update); ADD_SIGNAL(MethodInfo("pwa_update_available")); } #if !defined(WEB_ENABLED) || !defined(JAVASCRIPT_EVAL_ENABLED) -Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) { +Variant JavaScriptBridge::eval(const String &p_code, bool p_use_global_exec_context) { return Variant(); } -Ref JavaScript::get_interface(const String &p_interface) { +Ref JavaScriptBridge::get_interface(const String &p_interface) { return Ref(); } -Ref JavaScript::create_callback(const Callable &p_callable) { +Ref JavaScriptBridge::create_callback(const Callable &p_callable) { return Ref(); } -Variant JavaScript::_create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { +Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { if (p_argcount < 1) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 0; @@ -105,12 +105,12 @@ Variant JavaScript::_create_object_bind(const Variant **p_args, int p_argcount, } #endif #if !defined(WEB_ENABLED) -bool JavaScript::pwa_needs_update() const { +bool JavaScriptBridge::pwa_needs_update() const { return false; } -Error JavaScript::pwa_update() { +Error JavaScriptBridge::pwa_update() { return ERR_UNAVAILABLE; } -void JavaScript::download_buffer(Vector p_arr, const String &p_name, const String &p_mime) { +void JavaScriptBridge::download_buffer(Vector p_arr, const String &p_name, const String &p_mime) { } #endif diff --git a/platform/web/api/javascript_bridge_singleton.h b/platform/web/api/javascript_bridge_singleton.h new file mode 100644 index 0000000000..1e7b5a1699 --- /dev/null +++ b/platform/web/api/javascript_bridge_singleton.h @@ -0,0 +1,70 @@ +/*************************************************************************/ +/* javascript_bridge_singleton.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#ifndef JAVASCRIPT_BRIDGE_SINGLETON_H +#define JAVASCRIPT_BRIDGE_SINGLETON_H + +#include "core/object/class_db.h" +#include "core/object/ref_counted.h" + +class JavaScriptObject : public RefCounted { +private: + GDCLASS(JavaScriptObject, RefCounted); + +protected: + virtual bool _set(const StringName &p_name, const Variant &p_value) { return false; } + virtual bool _get(const StringName &p_name, Variant &r_ret) const { return false; } + virtual void _get_property_list(List *p_list) const {} +}; + +class JavaScriptBridge : public Object { +private: + GDCLASS(JavaScriptBridge, Object); + + static JavaScriptBridge *singleton; + +protected: + static void _bind_methods(); + +public: + Variant eval(const String &p_code, bool p_use_global_exec_context = false); + Ref get_interface(const String &p_interface); + Ref create_callback(const Callable &p_callable); + Variant _create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error); + void download_buffer(Vector p_arr, const String &p_name, const String &p_mime = "application/octet-stream"); + bool pwa_needs_update() const; + Error pwa_update(); + + static JavaScriptBridge *get_singleton(); + JavaScriptBridge(); + ~JavaScriptBridge(); +}; + +#endif // JAVASCRIPT_BRIDGE_SINGLETON_H diff --git a/platform/web/api/javascript_singleton.h b/platform/web/api/javascript_singleton.h deleted file mode 100644 index e93b0a18a1..0000000000 --- a/platform/web/api/javascript_singleton.h +++ /dev/null @@ -1,70 +0,0 @@ -/*************************************************************************/ -/* javascript_singleton.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ - -#ifndef JAVASCRIPT_SINGLETON_H -#define JAVASCRIPT_SINGLETON_H - -#include "core/object/class_db.h" -#include "core/object/ref_counted.h" - -class JavaScriptObject : public RefCounted { -private: - GDCLASS(JavaScriptObject, RefCounted); - -protected: - virtual bool _set(const StringName &p_name, const Variant &p_value) { return false; } - virtual bool _get(const StringName &p_name, Variant &r_ret) const { return false; } - virtual void _get_property_list(List *p_list) const {} -}; - -class JavaScript : public Object { -private: - GDCLASS(JavaScript, Object); - - static JavaScript *singleton; - -protected: - static void _bind_methods(); - -public: - Variant eval(const String &p_code, bool p_use_global_exec_context = false); - Ref get_interface(const String &p_interface); - Ref create_callback(const Callable &p_callable); - Variant _create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error); - void download_buffer(Vector p_arr, const String &p_name, const String &p_mime = "application/octet-stream"); - bool pwa_needs_update() const; - Error pwa_update(); - - static JavaScript *get_singleton(); - JavaScript(); - ~JavaScript(); -}; - -#endif // JAVASCRIPT_SINGLETON_H diff --git a/platform/web/javascript_bridge_singleton.cpp b/platform/web/javascript_bridge_singleton.cpp new file mode 100644 index 0000000000..69cd0cece1 --- /dev/null +++ b/platform/web/javascript_bridge_singleton.cpp @@ -0,0 +1,366 @@ +/*************************************************************************/ +/* javascript_bridge_singleton.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#include "api/javascript_bridge_singleton.h" + +#include "emscripten.h" +#include "os_web.h" + +extern "C" { +extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime); +} + +#ifdef JAVASCRIPT_EVAL_ENABLED + +extern "C" { +typedef union { + int64_t i; + double r; + void *p; +} godot_js_wrapper_ex; + +typedef int (*GodotJSWrapperVariant2JSCallback)(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock); +typedef void (*GodotJSWrapperFreeLockCallback)(void **p_lock, int p_type); +extern int godot_js_wrapper_interface_get(const char *p_name); +extern int godot_js_wrapper_object_call(int p_id, const char *p_method, void **p_args, int p_argc, GodotJSWrapperVariant2JSCallback p_variant2js_callback, godot_js_wrapper_ex *p_cb_rval, void **p_lock, GodotJSWrapperFreeLockCallback p_lock_callback); +extern int godot_js_wrapper_object_get(int p_id, godot_js_wrapper_ex *p_val, const char *p_prop); +extern int godot_js_wrapper_object_getvar(int p_id, int p_type, godot_js_wrapper_ex *p_val); +extern int godot_js_wrapper_object_setvar(int p_id, int p_key_type, godot_js_wrapper_ex *p_key_ex, int p_val_type, godot_js_wrapper_ex *p_val_ex); +extern void godot_js_wrapper_object_set(int p_id, const char *p_name, int p_type, godot_js_wrapper_ex *p_val); +extern void godot_js_wrapper_object_unref(int p_id); +extern int godot_js_wrapper_create_cb(void *p_ref, void (*p_callback)(void *p_ref, int p_arg_id, int p_argc)); +extern void godot_js_wrapper_object_set_cb_ret(int p_type, godot_js_wrapper_ex *p_val); +extern int godot_js_wrapper_create_object(const char *p_method, void **p_args, int p_argc, GodotJSWrapperVariant2JSCallback p_variant2js_callback, godot_js_wrapper_ex *p_cb_rval, void **p_lock, GodotJSWrapperFreeLockCallback p_lock_callback); +}; + +class JavaScriptObjectImpl : public JavaScriptObject { +private: + friend class JavaScriptBridge; + + int _js_id = 0; + Callable _callable; + + static int _variant2js(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock); + static void _free_lock(void **p_lock, int p_type); + static Variant _js2variant(int p_type, godot_js_wrapper_ex *p_val); + static void *_alloc_variants(int p_size); + static void _callback(void *p_ref, int p_arg_id, int p_argc); + +protected: + bool _set(const StringName &p_name, const Variant &p_value) override; + bool _get(const StringName &p_name, Variant &r_ret) const override; + void _get_property_list(List *p_list) const override; + +public: + Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override; + void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr) override; + Variant callp(const StringName &p_method, const Variant **p_args, int p_argc, Callable::CallError &r_error) override; + JavaScriptObjectImpl() {} + JavaScriptObjectImpl(int p_id) { _js_id = p_id; } + ~JavaScriptObjectImpl() { + if (_js_id) { + godot_js_wrapper_object_unref(_js_id); + } + } +}; + +bool JavaScriptObjectImpl::_set(const StringName &p_name, const Variant &p_value) { + ERR_FAIL_COND_V_MSG(!_js_id, false, "Invalid JS instance"); + const String name = p_name; + godot_js_wrapper_ex exchange; + void *lock = nullptr; + const Variant *v = &p_value; + int type = _variant2js((const void **)&v, 0, &exchange, &lock); + godot_js_wrapper_object_set(_js_id, name.utf8().get_data(), type, &exchange); + if (lock) { + _free_lock(&lock, type); + } + return true; +} + +bool JavaScriptObjectImpl::_get(const StringName &p_name, Variant &r_ret) const { + ERR_FAIL_COND_V_MSG(!_js_id, false, "Invalid JS instance"); + const String name = p_name; + godot_js_wrapper_ex exchange; + int type = godot_js_wrapper_object_get(_js_id, &exchange, name.utf8().get_data()); + r_ret = _js2variant(type, &exchange); + return true; +} + +Variant JavaScriptObjectImpl::getvar(const Variant &p_key, bool *r_valid) const { + if (r_valid) { + *r_valid = false; + } + godot_js_wrapper_ex exchange; + void *lock = nullptr; + const Variant *v = &p_key; + int prop_type = _variant2js((const void **)&v, 0, &exchange, &lock); + int type = godot_js_wrapper_object_getvar(_js_id, prop_type, &exchange); + if (lock) { + _free_lock(&lock, prop_type); + } + if (type < 0) { + return Variant(); + } + if (r_valid) { + *r_valid = true; + } + return _js2variant(type, &exchange); +} + +void JavaScriptObjectImpl::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) { + if (r_valid) { + *r_valid = false; + } + godot_js_wrapper_ex kex, vex; + void *klock = nullptr; + void *vlock = nullptr; + const Variant *kv = &p_key; + const Variant *vv = &p_value; + int ktype = _variant2js((const void **)&kv, 0, &kex, &klock); + int vtype = _variant2js((const void **)&vv, 0, &vex, &vlock); + int ret = godot_js_wrapper_object_setvar(_js_id, ktype, &kex, vtype, &vex); + if (klock) { + _free_lock(&klock, ktype); + } + if (vlock) { + _free_lock(&vlock, vtype); + } + if (ret == 0 && r_valid) { + *r_valid = true; + } +} + +void JavaScriptObjectImpl::_get_property_list(List *p_list) const { +} + +void JavaScriptObjectImpl::_free_lock(void **p_lock, int p_type) { + ERR_FAIL_COND_MSG(*p_lock == nullptr, "No lock to free!"); + const Variant::Type type = (Variant::Type)p_type; + switch (type) { + case Variant::STRING: { + CharString *cs = (CharString *)(*p_lock); + memdelete(cs); + *p_lock = nullptr; + } break; + default: + ERR_FAIL_MSG("Unknown lock type to free. Likely a bug."); + } +} + +Variant JavaScriptObjectImpl::_js2variant(int p_type, godot_js_wrapper_ex *p_val) { + Variant::Type type = (Variant::Type)p_type; + switch (type) { + case Variant::BOOL: + return Variant((bool)p_val->i); + case Variant::INT: + return p_val->i; + case Variant::FLOAT: + return p_val->r; + case Variant::STRING: { + String out = String::utf8((const char *)p_val->p); + free(p_val->p); + return out; + } + case Variant::OBJECT: { + return memnew(JavaScriptObjectImpl(p_val->i)); + } + default: + return Variant(); + } +} + +int JavaScriptObjectImpl::_variant2js(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock) { + const Variant **args = (const Variant **)p_args; + const Variant *v = args[p_pos]; + Variant::Type type = v->get_type(); + switch (type) { + case Variant::BOOL: + r_val->i = v->operator bool() ? 1 : 0; + break; + case Variant::INT: { + const int64_t tmp = v->operator int64_t(); + if (tmp >= 1LL << 31) { + r_val->r = (double)tmp; + return Variant::FLOAT; + } + r_val->i = v->operator int64_t(); + } break; + case Variant::FLOAT: + r_val->r = v->operator real_t(); + break; + case Variant::STRING: { + CharString *cs = memnew(CharString(v->operator String().utf8())); + r_val->p = (void *)cs->get_data(); + *p_lock = (void *)cs; + } break; + case Variant::OBJECT: { + JavaScriptObject *js_obj = Object::cast_to(v->operator Object *()); + r_val->i = js_obj != nullptr ? ((JavaScriptObjectImpl *)js_obj)->_js_id : 0; + } break; + default: + break; + } + return type; +} + +Variant JavaScriptObjectImpl::callp(const StringName &p_method, const Variant **p_args, int p_argc, Callable::CallError &r_error) { + godot_js_wrapper_ex exchange; + const String method = p_method; + void *lock = nullptr; + const int type = godot_js_wrapper_object_call(_js_id, method.utf8().get_data(), (void **)p_args, p_argc, &_variant2js, &exchange, &lock, &_free_lock); + r_error.error = Callable::CallError::CALL_OK; + if (type < 0) { + r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL; + return Variant(); + } + return _js2variant(type, &exchange); +} + +void JavaScriptObjectImpl::_callback(void *p_ref, int p_args_id, int p_argc) { + const JavaScriptObjectImpl *obj = (JavaScriptObjectImpl *)p_ref; + ERR_FAIL_COND_MSG(obj->_callable.is_null(), "JavaScript callback failed."); + Vector argp; + Array arg_arr; + for (int i = 0; i < p_argc; i++) { + godot_js_wrapper_ex exchange; + exchange.i = i; + int type = godot_js_wrapper_object_getvar(p_args_id, Variant::INT, &exchange); + arg_arr.push_back(_js2variant(type, &exchange)); + } + Variant arg = arg_arr; + const Variant *argv[1] = { &arg }; + Callable::CallError err; + Variant ret; + obj->_callable.callp(argv, 1, ret, err); + + // Set return value + godot_js_wrapper_ex exchange; + void *lock = nullptr; + const Variant *v = &ret; + int type = _variant2js((const void **)&v, 0, &exchange, &lock); + godot_js_wrapper_object_set_cb_ret(type, &exchange); + if (lock) { + _free_lock(&lock, type); + } +} + +Ref JavaScriptBridge::create_callback(const Callable &p_callable) { + Ref out = memnew(JavaScriptObjectImpl); + out->_callable = p_callable; + out->_js_id = godot_js_wrapper_create_cb(out.ptr(), JavaScriptObjectImpl::_callback); + return out; +} + +Ref JavaScriptBridge::get_interface(const String &p_interface) { + int js_id = godot_js_wrapper_interface_get(p_interface.utf8().get_data()); + ERR_FAIL_COND_V_MSG(!js_id, Ref(), "No interface '" + p_interface + "' registered."); + return Ref(memnew(JavaScriptObjectImpl(js_id))); +} + +Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { + if (p_argcount < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 0; + return Ref(); + } + if (p_args[0]->get_type() != Variant::STRING) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::STRING; + return Ref(); + } + godot_js_wrapper_ex exchange; + const String object = *p_args[0]; + void *lock = nullptr; + const Variant **args = p_argcount > 1 ? &p_args[1] : nullptr; + const int type = godot_js_wrapper_create_object(object.utf8().get_data(), (void **)args, p_argcount - 1, &JavaScriptObjectImpl::_variant2js, &exchange, &lock, &JavaScriptObjectImpl::_free_lock); + r_error.error = Callable::CallError::CALL_OK; + if (type < 0) { + r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL; + return Ref(); + } + return JavaScriptObjectImpl::_js2variant(type, &exchange); +} + +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)); +} + +void *resize_PackedByteArray_and_open_write(void *p_arr, void *r_write, int p_len) { + PackedByteArray *arr = (PackedByteArray *)p_arr; + VectorWriteProxy *write = (VectorWriteProxy *)r_write; + arr->resize(p_len); + *write = arr->write; + return arr->ptrw(); +} + +Variant JavaScriptBridge::eval(const String &p_code, bool p_use_global_exec_context) { + union js_eval_ret js_data; + PackedByteArray arr; + VectorWriteProxy arr_write; + + Variant::Type return_type = static_cast(godot_js_eval(p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write, resize_PackedByteArray_and_open_write)); + + switch (return_type) { + case Variant::BOOL: + return js_data.b; + case Variant::FLOAT: + return js_data.d; + case Variant::STRING: { + String str = String::utf8(js_data.s); + free(js_data.s); // Must free the string allocated in JS. + return str; + } + case Variant::PACKED_BYTE_ARRAY: + arr_write = VectorWriteProxy(); + return arr; + default: + return Variant(); + } +} +#endif // JAVASCRIPT_EVAL_ENABLED + +void JavaScriptBridge::download_buffer(Vector p_arr, const String &p_name, const String &p_mime) { + godot_js_os_download_buffer(p_arr.ptr(), p_arr.size(), p_name.utf8().get_data(), p_mime.utf8().get_data()); +} + +bool JavaScriptBridge::pwa_needs_update() const { + return OS_Web::get_singleton()->pwa_needs_update(); +} +Error JavaScriptBridge::pwa_update() { + return OS_Web::get_singleton()->pwa_update(); +} diff --git a/platform/web/javascript_singleton.cpp b/platform/web/javascript_singleton.cpp deleted file mode 100644 index 36ab4db452..0000000000 --- a/platform/web/javascript_singleton.cpp +++ /dev/null @@ -1,366 +0,0 @@ -/*************************************************************************/ -/* javascript_singleton.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ - -#include "api/javascript_singleton.h" - -#include "emscripten.h" -#include "os_web.h" - -extern "C" { -extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime); -} - -#ifdef JAVASCRIPT_EVAL_ENABLED - -extern "C" { -typedef union { - int64_t i; - double r; - void *p; -} godot_js_wrapper_ex; - -typedef int (*GodotJSWrapperVariant2JSCallback)(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock); -typedef void (*GodotJSWrapperFreeLockCallback)(void **p_lock, int p_type); -extern int godot_js_wrapper_interface_get(const char *p_name); -extern int godot_js_wrapper_object_call(int p_id, const char *p_method, void **p_args, int p_argc, GodotJSWrapperVariant2JSCallback p_variant2js_callback, godot_js_wrapper_ex *p_cb_rval, void **p_lock, GodotJSWrapperFreeLockCallback p_lock_callback); -extern int godot_js_wrapper_object_get(int p_id, godot_js_wrapper_ex *p_val, const char *p_prop); -extern int godot_js_wrapper_object_getvar(int p_id, int p_type, godot_js_wrapper_ex *p_val); -extern int godot_js_wrapper_object_setvar(int p_id, int p_key_type, godot_js_wrapper_ex *p_key_ex, int p_val_type, godot_js_wrapper_ex *p_val_ex); -extern void godot_js_wrapper_object_set(int p_id, const char *p_name, int p_type, godot_js_wrapper_ex *p_val); -extern void godot_js_wrapper_object_unref(int p_id); -extern int godot_js_wrapper_create_cb(void *p_ref, void (*p_callback)(void *p_ref, int p_arg_id, int p_argc)); -extern void godot_js_wrapper_object_set_cb_ret(int p_type, godot_js_wrapper_ex *p_val); -extern int godot_js_wrapper_create_object(const char *p_method, void **p_args, int p_argc, GodotJSWrapperVariant2JSCallback p_variant2js_callback, godot_js_wrapper_ex *p_cb_rval, void **p_lock, GodotJSWrapperFreeLockCallback p_lock_callback); -}; - -class JavaScriptObjectImpl : public JavaScriptObject { -private: - friend class JavaScript; - - int _js_id = 0; - Callable _callable; - - static int _variant2js(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock); - static void _free_lock(void **p_lock, int p_type); - static Variant _js2variant(int p_type, godot_js_wrapper_ex *p_val); - static void *_alloc_variants(int p_size); - static void _callback(void *p_ref, int p_arg_id, int p_argc); - -protected: - bool _set(const StringName &p_name, const Variant &p_value) override; - bool _get(const StringName &p_name, Variant &r_ret) const override; - void _get_property_list(List *p_list) const override; - -public: - Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override; - void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr) override; - Variant callp(const StringName &p_method, const Variant **p_args, int p_argc, Callable::CallError &r_error) override; - JavaScriptObjectImpl() {} - JavaScriptObjectImpl(int p_id) { _js_id = p_id; } - ~JavaScriptObjectImpl() { - if (_js_id) { - godot_js_wrapper_object_unref(_js_id); - } - } -}; - -bool JavaScriptObjectImpl::_set(const StringName &p_name, const Variant &p_value) { - ERR_FAIL_COND_V_MSG(!_js_id, false, "Invalid JS instance"); - const String name = p_name; - godot_js_wrapper_ex exchange; - void *lock = nullptr; - const Variant *v = &p_value; - int type = _variant2js((const void **)&v, 0, &exchange, &lock); - godot_js_wrapper_object_set(_js_id, name.utf8().get_data(), type, &exchange); - if (lock) { - _free_lock(&lock, type); - } - return true; -} - -bool JavaScriptObjectImpl::_get(const StringName &p_name, Variant &r_ret) const { - ERR_FAIL_COND_V_MSG(!_js_id, false, "Invalid JS instance"); - const String name = p_name; - godot_js_wrapper_ex exchange; - int type = godot_js_wrapper_object_get(_js_id, &exchange, name.utf8().get_data()); - r_ret = _js2variant(type, &exchange); - return true; -} - -Variant JavaScriptObjectImpl::getvar(const Variant &p_key, bool *r_valid) const { - if (r_valid) { - *r_valid = false; - } - godot_js_wrapper_ex exchange; - void *lock = nullptr; - const Variant *v = &p_key; - int prop_type = _variant2js((const void **)&v, 0, &exchange, &lock); - int type = godot_js_wrapper_object_getvar(_js_id, prop_type, &exchange); - if (lock) { - _free_lock(&lock, prop_type); - } - if (type < 0) { - return Variant(); - } - if (r_valid) { - *r_valid = true; - } - return _js2variant(type, &exchange); -} - -void JavaScriptObjectImpl::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) { - if (r_valid) { - *r_valid = false; - } - godot_js_wrapper_ex kex, vex; - void *klock = nullptr; - void *vlock = nullptr; - const Variant *kv = &p_key; - const Variant *vv = &p_value; - int ktype = _variant2js((const void **)&kv, 0, &kex, &klock); - int vtype = _variant2js((const void **)&vv, 0, &vex, &vlock); - int ret = godot_js_wrapper_object_setvar(_js_id, ktype, &kex, vtype, &vex); - if (klock) { - _free_lock(&klock, ktype); - } - if (vlock) { - _free_lock(&vlock, vtype); - } - if (ret == 0 && r_valid) { - *r_valid = true; - } -} - -void JavaScriptObjectImpl::_get_property_list(List *p_list) const { -} - -void JavaScriptObjectImpl::_free_lock(void **p_lock, int p_type) { - ERR_FAIL_COND_MSG(*p_lock == nullptr, "No lock to free!"); - const Variant::Type type = (Variant::Type)p_type; - switch (type) { - case Variant::STRING: { - CharString *cs = (CharString *)(*p_lock); - memdelete(cs); - *p_lock = nullptr; - } break; - default: - ERR_FAIL_MSG("Unknown lock type to free. Likely a bug."); - } -} - -Variant JavaScriptObjectImpl::_js2variant(int p_type, godot_js_wrapper_ex *p_val) { - Variant::Type type = (Variant::Type)p_type; - switch (type) { - case Variant::BOOL: - return Variant((bool)p_val->i); - case Variant::INT: - return p_val->i; - case Variant::FLOAT: - return p_val->r; - case Variant::STRING: { - String out = String::utf8((const char *)p_val->p); - free(p_val->p); - return out; - } - case Variant::OBJECT: { - return memnew(JavaScriptObjectImpl(p_val->i)); - } - default: - return Variant(); - } -} - -int JavaScriptObjectImpl::_variant2js(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock) { - const Variant **args = (const Variant **)p_args; - const Variant *v = args[p_pos]; - Variant::Type type = v->get_type(); - switch (type) { - case Variant::BOOL: - r_val->i = v->operator bool() ? 1 : 0; - break; - case Variant::INT: { - const int64_t tmp = v->operator int64_t(); - if (tmp >= 1LL << 31) { - r_val->r = (double)tmp; - return Variant::FLOAT; - } - r_val->i = v->operator int64_t(); - } break; - case Variant::FLOAT: - r_val->r = v->operator real_t(); - break; - case Variant::STRING: { - CharString *cs = memnew(CharString(v->operator String().utf8())); - r_val->p = (void *)cs->get_data(); - *p_lock = (void *)cs; - } break; - case Variant::OBJECT: { - JavaScriptObject *js_obj = Object::cast_to(v->operator Object *()); - r_val->i = js_obj != nullptr ? ((JavaScriptObjectImpl *)js_obj)->_js_id : 0; - } break; - default: - break; - } - return type; -} - -Variant JavaScriptObjectImpl::callp(const StringName &p_method, const Variant **p_args, int p_argc, Callable::CallError &r_error) { - godot_js_wrapper_ex exchange; - const String method = p_method; - void *lock = nullptr; - const int type = godot_js_wrapper_object_call(_js_id, method.utf8().get_data(), (void **)p_args, p_argc, &_variant2js, &exchange, &lock, &_free_lock); - r_error.error = Callable::CallError::CALL_OK; - if (type < 0) { - r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL; - return Variant(); - } - return _js2variant(type, &exchange); -} - -void JavaScriptObjectImpl::_callback(void *p_ref, int p_args_id, int p_argc) { - const JavaScriptObjectImpl *obj = (JavaScriptObjectImpl *)p_ref; - ERR_FAIL_COND_MSG(obj->_callable.is_null(), "JavaScript callback failed."); - Vector argp; - Array arg_arr; - for (int i = 0; i < p_argc; i++) { - godot_js_wrapper_ex exchange; - exchange.i = i; - int type = godot_js_wrapper_object_getvar(p_args_id, Variant::INT, &exchange); - arg_arr.push_back(_js2variant(type, &exchange)); - } - Variant arg = arg_arr; - const Variant *argv[1] = { &arg }; - Callable::CallError err; - Variant ret; - obj->_callable.callp(argv, 1, ret, err); - - // Set return value - godot_js_wrapper_ex exchange; - void *lock = nullptr; - const Variant *v = &ret; - int type = _variant2js((const void **)&v, 0, &exchange, &lock); - godot_js_wrapper_object_set_cb_ret(type, &exchange); - if (lock) { - _free_lock(&lock, type); - } -} - -Ref JavaScript::create_callback(const Callable &p_callable) { - Ref out = memnew(JavaScriptObjectImpl); - out->_callable = p_callable; - out->_js_id = godot_js_wrapper_create_cb(out.ptr(), JavaScriptObjectImpl::_callback); - return out; -} - -Ref JavaScript::get_interface(const String &p_interface) { - int js_id = godot_js_wrapper_interface_get(p_interface.utf8().get_data()); - ERR_FAIL_COND_V_MSG(!js_id, Ref(), "No interface '" + p_interface + "' registered."); - return Ref(memnew(JavaScriptObjectImpl(js_id))); -} - -Variant JavaScript::_create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 0; - return Ref(); - } - if (p_args[0]->get_type() != Variant::STRING) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::STRING; - return Ref(); - } - godot_js_wrapper_ex exchange; - const String object = *p_args[0]; - void *lock = nullptr; - const Variant **args = p_argcount > 1 ? &p_args[1] : nullptr; - const int type = godot_js_wrapper_create_object(object.utf8().get_data(), (void **)args, p_argcount - 1, &JavaScriptObjectImpl::_variant2js, &exchange, &lock, &JavaScriptObjectImpl::_free_lock); - r_error.error = Callable::CallError::CALL_OK; - if (type < 0) { - r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL; - return Ref(); - } - return JavaScriptObjectImpl::_js2variant(type, &exchange); -} - -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)); -} - -void *resize_PackedByteArray_and_open_write(void *p_arr, void *r_write, int p_len) { - PackedByteArray *arr = (PackedByteArray *)p_arr; - VectorWriteProxy *write = (VectorWriteProxy *)r_write; - arr->resize(p_len); - *write = arr->write; - return arr->ptrw(); -} - -Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) { - union js_eval_ret js_data; - PackedByteArray arr; - VectorWriteProxy arr_write; - - Variant::Type return_type = static_cast(godot_js_eval(p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write, resize_PackedByteArray_and_open_write)); - - switch (return_type) { - case Variant::BOOL: - return js_data.b; - case Variant::FLOAT: - return js_data.d; - case Variant::STRING: { - String str = String::utf8(js_data.s); - free(js_data.s); // Must free the string allocated in JS. - return str; - } - case Variant::PACKED_BYTE_ARRAY: - arr_write = VectorWriteProxy(); - return arr; - default: - return Variant(); - } -} -#endif // JAVASCRIPT_EVAL_ENABLED - -void JavaScript::download_buffer(Vector p_arr, const String &p_name, const String &p_mime) { - godot_js_os_download_buffer(p_arr.ptr(), p_arr.size(), p_name.utf8().get_data(), p_mime.utf8().get_data()); -} - -bool JavaScript::pwa_needs_update() const { - return OS_Web::get_singleton()->pwa_needs_update(); -} -Error JavaScript::pwa_update() { - return OS_Web::get_singleton()->pwa_update(); -} diff --git a/platform/web/os_web.cpp b/platform/web/os_web.cpp index f9714f25e7..ebe56924df 100644 --- a/platform/web/os_web.cpp +++ b/platform/web/os_web.cpp @@ -45,7 +45,7 @@ #include #include -#include "api/javascript_singleton.h" +#include "api/javascript_bridge_singleton.h" #include "godot_js.h" void OS_Web::alert(const String &p_alert, const String &p_title) { @@ -199,8 +199,8 @@ void OS_Web::update_pwa_state_callback() { if (OS_Web::get_singleton()) { OS_Web::get_singleton()->pwa_is_waiting = true; } - if (JavaScript::get_singleton()) { - JavaScript::get_singleton()->emit_signal("pwa_update_available"); + if (JavaScriptBridge::get_singleton()) { + JavaScriptBridge::get_singleton()->emit_signal("pwa_update_available"); } } -- cgit v1.2.3