From d2eef397312d1c142d95c2aef0ff3d5aeee6495a Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Fri, 8 May 2020 16:55:01 +0200 Subject: [PATCH] Fix Closure compiler build, python style. Move copyToFS into utils.js library included with '--pre-js'. --- modules/webrtc/webrtc_data_channel_js.cpp | 6 +-- modules/websocket/emws_client.cpp | 6 +-- platform/javascript/SCsub | 27 +++++------ platform/javascript/detect.py | 6 ++- .../javascript/display_server_javascript.cpp | 10 ++-- platform/javascript/engine/engine.js | 6 +-- platform/javascript/engine/utils.js | 18 ------- .../javascript/{ => native}/http_request.js | 0 .../javascript/{ => native}/id_handler.js | 0 platform/javascript/native/utils.js | 48 +++++++++++++++++++ platform/javascript/os_javascript.cpp | 2 +- 11 files changed, 79 insertions(+), 50 deletions(-) rename platform/javascript/{ => native}/http_request.js (100%) rename platform/javascript/{ => native}/id_handler.js (100%) create mode 100644 platform/javascript/native/utils.js diff --git a/modules/webrtc/webrtc_data_channel_js.cpp b/modules/webrtc/webrtc_data_channel_js.cpp index 1b360720a22..40c3f5801be 100644 --- a/modules/webrtc/webrtc_data_channel_js.cpp +++ b/modules/webrtc/webrtc_data_channel_js.cpp @@ -312,14 +312,14 @@ WebRTCDataChannelJS::WebRTCDataChannelJS(int js_id) { return; } var len = buffer.length*buffer.BYTES_PER_ELEMENT; - var out = Module._malloc(len); - Module.HEAPU8.set(buffer, out); + var out = _malloc(len); + HEAPU8.set(buffer, out); ccall("_emrtc_on_ch_message", "void", ["number", "number", "number", "number"], [c_ptr, out, len, is_string] ); - Module._free(out); + _free(out); } }, this, js_id); diff --git a/modules/websocket/emws_client.cpp b/modules/websocket/emws_client.cpp index bbe4d6dc5bf..bceb65c315c 100644 --- a/modules/websocket/emws_client.cpp +++ b/modules/websocket/emws_client.cpp @@ -142,14 +142,14 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, } var len = buffer.length*buffer.BYTES_PER_ELEMENT; - var out = Module._malloc(len); - Module.HEAPU8.set(buffer, out); + var out = _malloc(len); + HEAPU8.set(buffer, out); ccall("_esws_on_message", "void", ["number", "number", "number", "number"], [c_ptr, out, len, is_string] ); - Module._free(out); + _free(out); }); socket.addEventListener("error", function (event) { diff --git a/platform/javascript/SCsub b/platform/javascript/SCsub index 172e15473b1..dcf9a46bf9f 100644 --- a/platform/javascript/SCsub +++ b/platform/javascript/SCsub @@ -18,18 +18,19 @@ if env["threads_enabled"]: build = env.add_program(build_targets, javascript_files) js_libraries = [ - "http_request.js", + "native/http_request.js", ] for lib in js_libraries: env.Append(LINKFLAGS=["--js-library", env.File(lib).path]) env.Depends(build, js_libraries) -js_modules = [ - "id_handler.js", +js_pre = [ + "native/id_handler.js", + "native/utils.js", ] -for module in js_modules: - env.Append(LINKFLAGS=["--pre-js", env.File(module).path]) -env.Depends(build, js_modules) +for js in js_pre: + env.Append(LINKFLAGS=["--pre-js", env.File(js).path]) +env.Depends(build, js_pre) engine = [ "engine/preloader.js", @@ -49,16 +50,12 @@ js_wrapped = env.Textfile("#bin/godot", [env.File(f) for f in wrap_list], TEXTFI zip_dir = env.Dir("#bin/.javascript_zip") binary_name = "godot.tools" if env["tools"] else "godot" out_files = [ - zip_dir.File(binary_name + ".js"), - zip_dir.File(binary_name + ".wasm"), - zip_dir.File(binary_name + ".html") + zip_dir.File(binary_name + ".js"), + zip_dir.File(binary_name + ".wasm"), + zip_dir.File(binary_name + ".html"), ] html_file = "#misc/dist/html/full-size.html" -in_files = [ - js_wrapped, - build[1], - html_file -] +in_files = [js_wrapped, build[1], html_file] if env["threads_enabled"]: in_files.append(build[2]) out_files.append(zip_dir.File(binary_name + ".worker.js")) @@ -69,5 +66,5 @@ env.Zip( zip_files, ZIPROOT=zip_dir, ZIPSUFFIX="${PROGSUFFIX}${ZIPSUFFIX}", - ZIPCOMSTR="Archving $SOURCES as $TARGET" + ZIPCOMSTR="Archving $SOURCES as $TARGET", ) diff --git a/platform/javascript/detect.py b/platform/javascript/detect.py index 5a0f8503f9f..81287cead88 100644 --- a/platform/javascript/detect.py +++ b/platform/javascript/detect.py @@ -2,6 +2,7 @@ import os from emscripten_helpers import parse_config, run_closure_compiler, create_engine_file + def is_active(): return True @@ -16,6 +17,7 @@ def can_build(): def get_opts(): from SCons.Variables import BoolVariable + return [ # eval() can be a security concern, so it can be disabled. BoolVariable("javascript_eval", "Enable JavaScript eval interface", True), @@ -55,7 +57,7 @@ def configure(env): env.Append(CPPDEFINES=["DEBUG_ENABLED"]) # Retain function names for backtraces at the cost of file size. env.Append(LINKFLAGS=["--profiling-funcs"]) - else: # "debug" + else: # "debug" env.Append(CPPDEFINES=["DEBUG_ENABLED"]) env.Append(CCFLAGS=["-O1", "-g"]) env.Append(LINKFLAGS=["-O1", "-g"]) @@ -164,6 +166,6 @@ def configure(env): env.Append(LINKFLAGS=["-s", "OFFSCREEN_FRAMEBUFFER=1"]) # callMain for manual start, FS for preloading, PATH and ERRNO_CODES for BrowserFS. - env.Append(LINKFLAGS=["-s", "EXTRA_EXPORTED_RUNTIME_METHODS=['callMain', 'FS', 'PATH', 'ERRNO_CODES']"]) + env.Append(LINKFLAGS=["-s", "EXTRA_EXPORTED_RUNTIME_METHODS=['callMain', 'FS', 'PATH']"]) # Add code that allow exiting runtime. env.Append(LINKFLAGS=["-s", "EXIT_RUNTIME=1"]) diff --git a/platform/javascript/display_server_javascript.cpp b/platform/javascript/display_server_javascript.cpp index 13615bebe13..103bbf4eadf 100644 --- a/platform/javascript/display_server_javascript.cpp +++ b/platform/javascript/display_server_javascript.cpp @@ -820,16 +820,16 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive EM_ASM({ const canvas = Module['canvas']; var enc = new TextEncoder("utf-8"); - buffer = new Uint8Array(enc.encode(canvas.id)); - var len = buffer.length*buffer.BYTES_PER_ELEMENT; - var out = Module._malloc(len); - Module.HEAPU8.set(buffer, out); + var buffer = new Uint8Array(enc.encode(canvas.id)); + var len = buffer.byteLength; + var out = _malloc(len); + HEAPU8.set(buffer, out); ccall("_set_canvas_id", "void", ["number", "number"], [out, len] ); - Module._free(out); + _free(out); }); /* clang-format on */ diff --git a/platform/javascript/engine/engine.js b/platform/javascript/engine/engine.js index aecc6b9432e..d709422abb4 100644 --- a/platform/javascript/engine/engine.js +++ b/platform/javascript/engine/engine.js @@ -82,7 +82,7 @@ Function('return this')()['Engine'] = (function() { var me = this; return me.init().then(function() { if (!me.rtenv) { - reject(new Error('The engine must be initialized before it can be started')); + return Promise.reject(new Error('The engine must be initialized before it can be started')); } if (!(me.canvas instanceof HTMLCanvasElement)) { @@ -124,7 +124,7 @@ Function('return this')()['Engine'] = (function() { } return new Promise(function(resolve, reject) { preloader.preloadedFiles.forEach(function(file) { - Utils.copyToFS(me.rtenv['FS'], file.path, file.buffer); + me.rtenv['copyToFS'](file.path, file.buffer); }); preloader.preloadedFiles.length = 0; // Clear memory me.rtenv['callMain'](args); @@ -217,7 +217,7 @@ Function('return this')()['Engine'] = (function() { if (this.rtenv == null) { throw new Error("Engine must be inited before copying files"); } - Utils.copyToFS(this.rtenv['FS'], path, buffer); + this.rtenv['copyToFS'](path, buffer); } // Closure compiler exported engine methods. diff --git a/platform/javascript/engine/utils.js b/platform/javascript/engine/utils.js index fdff90a9236..0c97b38199f 100644 --- a/platform/javascript/engine/utils.js +++ b/platform/javascript/engine/utils.js @@ -27,24 +27,6 @@ var Utils = { return instantiateWasm; }, - copyToFS: function(fs, path, buffer) { - var p = path.lastIndexOf("/"); - var dir = "/"; - if (p > 0) { - dir = path.slice(0, path.lastIndexOf("/")); - } - try { - fs.stat(dir); - } catch (e) { - if (e.errno !== 44) { // 'ENOENT', see https://github.com/emscripten-core/emscripten/blob/master/system/lib/libc/musl/arch/emscripten/bits/errno.h - throw e; - } - fs['mkdirTree'](dir); - } - // With memory growth, canOwn should be false. - fs['writeFile'](path, new Uint8Array(buffer), {'flags': 'wx+'}); - }, - findCanvas: function() { var nodes = document.getElementsByTagName('canvas'); if (nodes.length && nodes[0] instanceof HTMLCanvasElement) { diff --git a/platform/javascript/http_request.js b/platform/javascript/native/http_request.js similarity index 100% rename from platform/javascript/http_request.js rename to platform/javascript/native/http_request.js diff --git a/platform/javascript/id_handler.js b/platform/javascript/native/id_handler.js similarity index 100% rename from platform/javascript/id_handler.js rename to platform/javascript/native/id_handler.js diff --git a/platform/javascript/native/utils.js b/platform/javascript/native/utils.js new file mode 100644 index 00000000000..d45c6c30321 --- /dev/null +++ b/platform/javascript/native/utils.js @@ -0,0 +1,48 @@ +/*************************************************************************/ +/* utils.js */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +Module['copyToFS'] = function(path, buffer) { + var p = path.lastIndexOf("/"); + var dir = "/"; + if (p > 0) { + dir = path.slice(0, path.lastIndexOf("/")); + } + try { + FS.stat(dir); + } catch (e) { + if (e.errno !== ERRNO_CODES.ENOENT) { // 'ENOENT', see https://github.com/emscripten-core/emscripten/blob/master/system/lib/libc/musl/arch/emscripten/bits/errno.h + throw e; + } + FS.mkdirTree(dir); + } + // With memory growth, canOwn should be false. + FS.writeFile(path, new Uint8Array(buffer), {'flags': 'wx+'}); +} + diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 4b41ed5d023..205644ce513 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -71,7 +71,7 @@ void OS_JavaScript::initialize() { char locale_ptr[16]; /* clang-format off */ EM_ASM({ - stringToUTF8(Module.locale, $0, 16); + stringToUTF8(Module['locale'], $0, 16); }, locale_ptr); /* clang-format on */ setenv("LANG", locale_ptr, true);