Fix Closure compiler build, python style.

Move copyToFS into utils.js library included with '--pre-js'.
This commit is contained in:
Fabio Alessandrelli 2020-05-08 16:55:01 +02:00
parent 7411e7fd37
commit d2eef39731
11 changed files with 79 additions and 50 deletions

View file

@ -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);

View file

@ -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) {

View file

@ -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",
)

View file

@ -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"])

View file

@ -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 */

View file

@ -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.

View file

@ -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) {

View file

@ -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+'});
}

View file

@ -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);