Merge pull request #65239 from Geequlim/js2web

Rename JavaScript singleton to JavaScriptBridge
This commit is contained in:
Fabio Alessandrelli 2022-09-07 09:29:42 +02:00 committed by GitHub
commit fffdbb38e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 63 deletions

View file

@ -1275,8 +1275,8 @@
The [JavaClassWrapper] singleton. The [JavaClassWrapper] singleton.
[b]Note:[/b] Only implemented on Android. [b]Note:[/b] Only implemented on Android.
</member> </member>
<member name="JavaScript" type="JavaScript" setter="" getter=""> <member name="JavaScriptBridge" type="JavaScriptBridge" setter="" getter="">
The [JavaScript] singleton. The [JavaScriptBridge] singleton.
[b]Note:[/b] Only implemented on the Web platform. [b]Note:[/b] Only implemented on the Web platform.
</member> </member>
<member name="Marshalls" type="Marshalls" setter="" getter=""> <member name="Marshalls" type="Marshalls" setter="" getter="">

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<class name="JavaScript" inherits="Object" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> <class name="JavaScriptBridge" inherits="Object" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description> <brief_description>
Singleton that connects the engine with the browser's JavaScript context in Web export. Singleton that connects the engine with the browser's JavaScript context in Web export.
</brief_description> </brief_description>
<description> <description>
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. 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 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. [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.
</description> </description>
<tutorials> <tutorials>
<link title="Exporting for the Web: Calling JavaScript from script">$DOCS_URL/tutorials/export/exporting_for_web.html#calling-javascript-from-script</link> <link title="Exporting for the Web: Calling JavaScript from script">$DOCS_URL/tutorials/export/exporting_for_web.html#calling-javascript-from-script</link>

View file

@ -1,27 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<class name="JavaScriptObject" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> <class name="JavaScriptObject" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description> <brief_description>
A wrapper class for native JavaScript objects. A wrapper class for web native JavaScript objects.
</brief_description> </brief_description>
<description> <description>
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: Example:
[codeblock] [codeblock]
extends Node extends Node
var _my_js_callback = JavaScript.create_callback(self, "myCallback") # This reference must be kept var _my_js_callback = JavaScriptBridge.create_callback(self, "myCallback") # This reference must be kept
var console = JavaScript.get_interface("console") var console = JavaScriptBridge.get_interface("console")
func _init(): 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] 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 uint8arr[1] = 255
prints(uint8arr[1], uint8arr.byteLength) # prints 255 10 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 ]" 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) # Equivalent of JavaScriptBridge: Array.from(uint8arr).forEach(myCallback)
JavaScript.get_interface("Array").from(uint8arr).forEach(_my_js_callback) JavaScriptBridge.get_interface("Array").from(uint8arr).forEach(_my_js_callback)
func myCallback(args): func myCallback(args):
# Will be called with the parameters passed to the "forEach" callback # Will be called with the parameters passed to the "forEach" callback

View file

@ -6,7 +6,7 @@ web_files = [
"audio_driver_web.cpp", "audio_driver_web.cpp",
"display_server_web.cpp", "display_server_web.cpp",
"http_client_web.cpp", "http_client_web.cpp",
"javascript_singleton.cpp", "javascript_bridge_singleton.cpp",
"web_main.cpp", "web_main.cpp",
"os_web.cpp", "os_web.cpp",
"api/web_tools_editor_plugin.cpp", "api/web_tools_editor_plugin.cpp",

View file

@ -30,66 +30,66 @@
#include "api.h" #include "api.h"
#include "core/config/engine.h" #include "core/config/engine.h"
#include "javascript_singleton.h" #include "javascript_bridge_singleton.h"
#include "web_tools_editor_plugin.h" #include "web_tools_editor_plugin.h"
static JavaScript *javascript_singleton; static JavaScriptBridge *javascript_bridge_singleton;
void register_web_api() { void register_web_api() {
WebToolsEditorPlugin::initialize(); WebToolsEditorPlugin::initialize();
GDREGISTER_ABSTRACT_CLASS(JavaScriptObject); GDREGISTER_ABSTRACT_CLASS(JavaScriptObject);
GDREGISTER_ABSTRACT_CLASS(JavaScript); GDREGISTER_ABSTRACT_CLASS(JavaScriptBridge);
javascript_singleton = memnew(JavaScript); javascript_bridge_singleton = memnew(JavaScriptBridge);
Engine::get_singleton()->add_singleton(Engine::Singleton("JavaScript", javascript_singleton)); Engine::get_singleton()->add_singleton(Engine::Singleton("JavaScriptBridge", javascript_bridge_singleton));
} }
void unregister_web_api() { 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; return singleton;
} }
JavaScript::JavaScript() { JavaScriptBridge::JavaScriptBridge() {
ERR_FAIL_COND_MSG(singleton != nullptr, "JavaScript singleton already exist."); ERR_FAIL_COND_MSG(singleton != nullptr, "JavaScriptBridge singleton already exist.");
singleton = this; singleton = this;
} }
JavaScript::~JavaScript() {} JavaScriptBridge::~JavaScriptBridge() {}
void JavaScript::_bind_methods() { void JavaScriptBridge::_bind_methods() {
ClassDB::bind_method(D_METHOD("eval", "code", "use_global_execution_context"), &JavaScript::eval, DEFVAL(false)); ClassDB::bind_method(D_METHOD("eval", "code", "use_global_execution_context"), &JavaScriptBridge::eval, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_interface", "interface"), &JavaScript::get_interface); ClassDB::bind_method(D_METHOD("get_interface", "interface"), &JavaScriptBridge::get_interface);
ClassDB::bind_method(D_METHOD("create_callback", "callable"), &JavaScript::create_callback); ClassDB::bind_method(D_METHOD("create_callback", "callable"), &JavaScriptBridge::create_callback);
{ {
MethodInfo mi; MethodInfo mi;
mi.name = "create_object"; mi.name = "create_object";
mi.arguments.push_back(PropertyInfo(Variant::STRING, "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("download_buffer", "buffer", "name", "mime"), &JavaScriptBridge::download_buffer, DEFVAL("application/octet-stream"));
ClassDB::bind_method(D_METHOD("pwa_needs_update"), &JavaScript::pwa_needs_update); ClassDB::bind_method(D_METHOD("pwa_needs_update"), &JavaScriptBridge::pwa_needs_update);
ClassDB::bind_method(D_METHOD("pwa_update"), &JavaScript::pwa_update); ClassDB::bind_method(D_METHOD("pwa_update"), &JavaScriptBridge::pwa_update);
ADD_SIGNAL(MethodInfo("pwa_update_available")); ADD_SIGNAL(MethodInfo("pwa_update_available"));
} }
#if !defined(WEB_ENABLED) || !defined(JAVASCRIPT_EVAL_ENABLED) #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(); return Variant();
} }
Ref<JavaScriptObject> JavaScript::get_interface(const String &p_interface) { Ref<JavaScriptObject> JavaScriptBridge::get_interface(const String &p_interface) {
return Ref<JavaScriptObject>(); return Ref<JavaScriptObject>();
} }
Ref<JavaScriptObject> JavaScript::create_callback(const Callable &p_callable) { Ref<JavaScriptObject> JavaScriptBridge::create_callback(const Callable &p_callable) {
return Ref<JavaScriptObject>(); return Ref<JavaScriptObject>();
} }
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) { if (p_argcount < 1) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.argument = 0; r_error.argument = 0;
@ -105,12 +105,12 @@ Variant JavaScript::_create_object_bind(const Variant **p_args, int p_argcount,
} }
#endif #endif
#if !defined(WEB_ENABLED) #if !defined(WEB_ENABLED)
bool JavaScript::pwa_needs_update() const { bool JavaScriptBridge::pwa_needs_update() const {
return false; return false;
} }
Error JavaScript::pwa_update() { Error JavaScriptBridge::pwa_update() {
return ERR_UNAVAILABLE; return ERR_UNAVAILABLE;
} }
void JavaScript::download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime) { void JavaScriptBridge::download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime) {
} }
#endif #endif

View file

@ -1,5 +1,5 @@
/*************************************************************************/ /*************************************************************************/
/* javascript_singleton.h */ /* javascript_bridge_singleton.h */
/*************************************************************************/ /*************************************************************************/
/* This file is part of: */ /* This file is part of: */
/* GODOT ENGINE */ /* GODOT ENGINE */
@ -28,8 +28,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
#ifndef JAVASCRIPT_SINGLETON_H #ifndef JAVASCRIPT_BRIDGE_SINGLETON_H
#define JAVASCRIPT_SINGLETON_H #define JAVASCRIPT_BRIDGE_SINGLETON_H
#include "core/object/class_db.h" #include "core/object/class_db.h"
#include "core/object/ref_counted.h" #include "core/object/ref_counted.h"
@ -44,11 +44,11 @@ protected:
virtual void _get_property_list(List<PropertyInfo> *p_list) const {} virtual void _get_property_list(List<PropertyInfo> *p_list) const {}
}; };
class JavaScript : public Object { class JavaScriptBridge : public Object {
private: private:
GDCLASS(JavaScript, Object); GDCLASS(JavaScriptBridge, Object);
static JavaScript *singleton; static JavaScriptBridge *singleton;
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -62,9 +62,9 @@ public:
bool pwa_needs_update() const; bool pwa_needs_update() const;
Error pwa_update(); Error pwa_update();
static JavaScript *get_singleton(); static JavaScriptBridge *get_singleton();
JavaScript(); JavaScriptBridge();
~JavaScript(); ~JavaScriptBridge();
}; };
#endif // JAVASCRIPT_SINGLETON_H #endif // JAVASCRIPT_BRIDGE_SINGLETON_H

View file

@ -1,5 +1,5 @@
/*************************************************************************/ /*************************************************************************/
/* javascript_singleton.cpp */ /* javascript_bridge_singleton.cpp */
/*************************************************************************/ /*************************************************************************/
/* This file is part of: */ /* This file is part of: */
/* GODOT ENGINE */ /* GODOT ENGINE */
@ -28,7 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
#include "api/javascript_singleton.h" #include "api/javascript_bridge_singleton.h"
#include "emscripten.h" #include "emscripten.h"
#include "os_web.h" #include "os_web.h"
@ -62,7 +62,7 @@ extern int godot_js_wrapper_create_object(const char *p_method, void **p_args, i
class JavaScriptObjectImpl : public JavaScriptObject { class JavaScriptObjectImpl : public JavaScriptObject {
private: private:
friend class JavaScript; friend class JavaScriptBridge;
int _js_id = 0; int _js_id = 0;
Callable _callable; Callable _callable;
@ -272,20 +272,20 @@ void JavaScriptObjectImpl::_callback(void *p_ref, int p_args_id, int p_argc) {
} }
} }
Ref<JavaScriptObject> JavaScript::create_callback(const Callable &p_callable) { Ref<JavaScriptObject> JavaScriptBridge::create_callback(const Callable &p_callable) {
Ref<JavaScriptObjectImpl> out = memnew(JavaScriptObjectImpl); Ref<JavaScriptObjectImpl> out = memnew(JavaScriptObjectImpl);
out->_callable = p_callable; out->_callable = p_callable;
out->_js_id = godot_js_wrapper_create_cb(out.ptr(), JavaScriptObjectImpl::_callback); out->_js_id = godot_js_wrapper_create_cb(out.ptr(), JavaScriptObjectImpl::_callback);
return out; return out;
} }
Ref<JavaScriptObject> JavaScript::get_interface(const String &p_interface) { Ref<JavaScriptObject> JavaScriptBridge::get_interface(const String &p_interface) {
int js_id = godot_js_wrapper_interface_get(p_interface.utf8().get_data()); int js_id = godot_js_wrapper_interface_get(p_interface.utf8().get_data());
ERR_FAIL_COND_V_MSG(!js_id, Ref<JavaScriptObject>(), "No interface '" + p_interface + "' registered."); ERR_FAIL_COND_V_MSG(!js_id, Ref<JavaScriptObject>(), "No interface '" + p_interface + "' registered.");
return Ref<JavaScriptObject>(memnew(JavaScriptObjectImpl(js_id))); return Ref<JavaScriptObject>(memnew(JavaScriptObjectImpl(js_id)));
} }
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) { if (p_argcount < 1) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.argument = 0; r_error.argument = 0;
@ -328,7 +328,7 @@ void *resize_PackedByteArray_and_open_write(void *p_arr, void *r_write, int p_le
return arr->ptrw(); return arr->ptrw();
} }
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) {
union js_eval_ret js_data; union js_eval_ret js_data;
PackedByteArray arr; PackedByteArray arr;
VectorWriteProxy<uint8_t> arr_write; VectorWriteProxy<uint8_t> arr_write;
@ -354,13 +354,13 @@ Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) {
} }
#endif // JAVASCRIPT_EVAL_ENABLED #endif // JAVASCRIPT_EVAL_ENABLED
void JavaScript::download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime) { void JavaScriptBridge::download_buffer(Vector<uint8_t> 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()); 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 { bool JavaScriptBridge::pwa_needs_update() const {
return OS_Web::get_singleton()->pwa_needs_update(); return OS_Web::get_singleton()->pwa_needs_update();
} }
Error JavaScript::pwa_update() { Error JavaScriptBridge::pwa_update() {
return OS_Web::get_singleton()->pwa_update(); return OS_Web::get_singleton()->pwa_update();
} }

View file

@ -45,7 +45,7 @@
#include <emscripten.h> #include <emscripten.h>
#include <stdlib.h> #include <stdlib.h>
#include "api/javascript_singleton.h" #include "api/javascript_bridge_singleton.h"
#include "godot_js.h" #include "godot_js.h"
void OS_Web::alert(const String &p_alert, const String &p_title) { 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()) { if (OS_Web::get_singleton()) {
OS_Web::get_singleton()->pwa_is_waiting = true; OS_Web::get_singleton()->pwa_is_waiting = true;
} }
if (JavaScript::get_singleton()) { if (JavaScriptBridge::get_singleton()) {
JavaScript::get_singleton()->emit_signal("pwa_update_available"); JavaScriptBridge::get_singleton()->emit_signal("pwa_update_available");
} }
} }