Browse Source

Use wasm for dukweb.js

Also fix async load handling in dukweb: delay dukwebSetup() until
the emcc-compiled Duktape code is fully initialized.  This matters
with wasm compilation because loading is actually asynchronous.
pull/2244/head
Sami Vaarala 5 years ago
parent
commit
7ad4a51d8f
  1. 1
      .gitignore
  2. 14
      Makefile
  3. 3
      RELEASES.rst
  4. 16
      dukweb/dukweb.html
  5. 48
      dukweb/dukweb_extra.js
  6. 1
      website/buildsite.py

1
.gitignore

@ -77,6 +77,7 @@
/massif-*.out
/ms_print.*
/dukweb.js
/dukweb.wasm
/debugger/jquery-ui-1.11.2.zip
/debugger/jquery-ui-1.11.2/
/debugger/node_modules/

14
Makefile

@ -202,6 +202,7 @@ EMCC = emcc
#EMCCOPTS = -s TOTAL_MEMORY=2097152 -s TOTAL_STACK=524288 --memory-init-file 0
EMCCOPTS = -O2 -std=c99 -Wall --memory-init-file 0 -s WASM=0 -s POLYFILL_OLD_MATH_FUNCTIONS
EMCCOPTS_DUKVM = -O2 -std=c99 -Wall --memory-init-file 0 -DEMSCRIPTEN -s WASM=0
EMCCOPTS_DUKVM_WASM = -O2 -std=c99 -Wall --memory-init-file 0 -DEMSCRIPTEN -s WASM=1
EMCCOPTS_DUKWEB_EXPORT = -s EXPORTED_FUNCTIONS='["_main","_dukweb_is_open", "_dukweb_open","_dukweb_close","_dukweb_eval"]' -s 'EXTRA_EXPORTED_RUNTIME_METHODS=["ccall","cwrap"]'
EMDUKOPTS = -s TOTAL_MEMORY=268435456 -DDUK_CMDLINE_PRINTALERT_SUPPORT
EMDUKOPTS += -DEMSCRIPTEN # enable stdin workaround in duk_cmdline.c
@ -261,7 +262,7 @@ clean:
@rm -rf test262-*
@rm -rf lua-5.2.3
@rm -rf luajs
@rm -f dukweb.js
@rm -f dukweb.js dukweb.wasm
@rm -rf /tmp/dukweb-test/
@rm -f massif-*.out
@rm -f literal_intern_test
@ -574,9 +575,10 @@ emduk.js: prep/emduk
# and providing an eval() facility from both sides. This is a placeholder now
# and doesn't do anything useful yet.
dukweb.js: prep/dukweb
$(EMCC) $(EMCCOPTS_DUKVM) $(EMCCOPTS_DUKWEB_EXPORT) --post-js dukweb/dukweb_extra.js \
@rm -f dukweb.js dukweb.asm
$(EMCC) $(EMCCOPTS_DUKVM_WASM) $(EMCCOPTS_DUKWEB_EXPORT) --post-js dukweb/dukweb_extra.js \
-Iprep/dukweb prep/dukweb/duktape.c dukweb/dukweb.c -o dukweb.js
@wc dukweb.js
@wc dukweb.js dukweb.wasm
literal_intern_test: prep/nondebug misc/literal_intern_test.c
$(CC) -o $@ -std=c99 -O2 -fstrict-aliasing -Wall -Wextra \
-Iprep/nondebug prep/nondebug/duktape.c misc/literal_intern_test.c -lm
@ -660,11 +662,11 @@ configuretest:
# Dukweb.js test.
.PHONY: dukwebtest
dukwebtest: dukweb.js jquery-1.11.2.js
dukwebtest: dukweb.js dukweb.wasm jquery-1.11.2.js
@echo "### dukwebtest"
@rm -rf /tmp/dukweb-test/
mkdir /tmp/dukweb-test/
cp dukweb.js jquery-1.11.2.js dukweb/dukweb.html dukweb/dukweb.css /tmp/dukweb-test/
cp dukweb.js dukweb.wasm jquery-1.11.2.js dukweb/dukweb.html dukweb/dukweb.css /tmp/dukweb-test/
@echo "Now point your browser to: file:///tmp/dukweb-test/dukweb.html"
# Third party tests.
@ -1010,7 +1012,7 @@ dist-iso: dist-src
.PHONY: tidy-site
tidy-site:
for i in website/*/*.html; do echo "*** Checking $$i"; tidy -q -e -xml $$i; done
site: duktape-releases dukweb.js jquery-1.11.2.js lz-string
site: duktape-releases dukweb.js dukweb.wasm jquery-1.11.2.js lz-string
rm -rf site
mkdir site
-cd duktape-releases/; git pull --rebase # get binaries up-to-date, but allow errors for offline use

3
RELEASES.rst

@ -3597,3 +3597,6 @@ Planned
* Don't treat U+180E as whitespace e.g. for String trim() purposes,
(ES2016, requires Unicode 8.0.0 or higher) (GH-2236)
* Use wasm for dukweb.js compilation (including duktape.org site),
fix async loading of emcc-compiled code in dukweb.html (GH-2244)

16
dukweb/dukweb.html

@ -12,11 +12,11 @@
<div id="content-middle">
<p id="dukweb-intro">Works in Chrome/Chromium/Firefox. There is much to
improve in the Duktape/browser bindings and errors are not handled nicely
yet, so keep your Javascript Console open. Also note that this page takes
a few seconds to (re)load. You can embed code in the Dukweb link as a
fragment identifier: <a href="http://duktape.org/dukweb.html#print('Hello%20world!')%3B">http://duktape.org/dukweb.html#print('Hello%20world!')%3B</a>.</p>
<p id="dukweb-intro">Duktape compiled using emcc into JS + wasm (can
also be compiled into pure JS), should work in Chrome/Chromium/Firefox.
See Web Console for details and errors. This page takes a a few seconds
to (re)load. You can embed code in the Dukweb link as a fragment identifier:
<a href="http://duktape.org/dukweb.html#print('Hello%20world!')%3B">http://duktape.org/dukweb.html#print('Hello%20world!')%3B</a>.</p>
<div id="dukweb-input-wrapper">
<textarea id="dukweb-input">
@ -117,15 +117,15 @@ $(document).ready(function () {
alert(e);
}
try {
Duktape.getInitializedPromise().then(function () {
dukwebSetup();
} catch (e) {
}).catch(function (e) {
$('#dukweb-output')
.addClass('error')
.text(
'Dukweb.js initialization failed (perhaps your browser is not compatible?):\n\n' +
String(e.stack || e) + '\n\n');
}
});
});
//]]>

48
dukweb/dukweb_extra.js

@ -12,11 +12,15 @@
var Duktape = {};
Duktape.initializedPromise = new Promise(function (resolve, reject) {
Duktape.initializedPromiseFuncs = [ resolve, reject ];
});
/*
* Utilities
*/
Duktape.log = function() {};
Duktape.log = function log() {};
if (typeof console === 'object' && typeof console.log === 'function') {
Duktape.log = function() {
console.log.apply(console, arguments);
@ -28,16 +32,16 @@ if (typeof console === 'object' && typeof console.log === 'function') {
}
Duktape.log('Duktape (dukweb) initializing');
Duktape.logOwnProperties = function() {
Duktape.logOwnProperties = function logOwnProperties() {
Duktape.log('Own properties of Duktape:');
Object.getOwnPropertyNames(Duktape).forEach(function (k) {
var v = Duktape[k];
if (typeof v === 'function') { v = '[omitted]'; }
Duktape.log(' ' + k + ' = (' + typeof Duktape[k] + ') ' + String(v));
});
}
};
Duktape.hexVal = function(x, ndigits) {
Duktape.hexVal = function hexVal(x, ndigits) {
var nybbles = '0123456789abcdef';
var i;
var res = ''
@ -48,7 +52,7 @@ Duktape.hexVal = function(x, ndigits) {
return res;
};
Duktape.escapeTable = (function() {
Duktape.escapeTable = (function escapeTableInit() {
var i, n;
var res = {};
@ -64,7 +68,7 @@ Duktape.escapeTable = (function() {
})();
// Probably unnecessary, JSON.stringify() escapes strings nicely.
Duktape.escapeString = function(x) {
Duktape.escapeString = function escapeString(x) {
var res = [];
var i, n = x.length;
var c;
@ -74,14 +78,14 @@ Duktape.escapeString = function(x) {
res.push(esc[c] || '\\u' + Duktape.hexVal(String.charCodeAt(i)));
}
return res.join('');
}
};
/*
* Duktape.eval: run code inside Duktape, encode output value using JSON.
*/
// XXX: errors should probably be promoted to work better
Duktape.eval = function(code) {
Duktape.eval = function eval(code) {
// Code escape into a Javascript string
var escapedString = JSON.stringify(String(code));
//var escapedString = '"' + Duktape.escapeString(code) + '"';
@ -105,17 +109,22 @@ Duktape.eval = function(code) {
*/
// Expect web page to override this, e.g. to append to some buffer.
Duktape.printHandler = function(msg) {
Duktape.printHandler = function printHandler(msg) {
log(msg);
}
};
// Expect web page to override this, e.g. to use browser alert().
Duktape.alertHandler = function(msg) {
Duktape.alertHandler = function alertHandler(msg) {
log(msg);
}
};
// Get Promise for Dukweb init completion.
Duktape.getInitializedPromise = function getInitializedPromise() {
return Duktape.initializedPromise;
};
// Asynchronous init, triggered from C main().
Duktape.initialize = function initialize() {
Duktape.initializeRaw = function initializeRaw() {
console.log('Duktape.initialize() start');
/*
@ -207,4 +216,15 @@ Duktape.initialize = function initialize() {
//console.log('=== ' + Duktape.eval('Duktape.enc("jx", { env: Duktape.env, version: Duktape.version })') + ' ===');
console.log('Duktape.initialize() end');
}
};
Duktape.initialize = function initialize() {
try {
Duktape.initializeRaw();
Duktape.initializedPromiseFuncs[0](Duktape);
} catch (e) {
Duktape.initializedPromiseFuncs[1](e);
} finally {
Duktape.initializedPromiseFuncs[1](new Error('internal error'));
}
};

1
website/buildsite.py

@ -1167,6 +1167,7 @@ def main():
print 'Copying dukweb.js files'
for i in [ '../dukweb.js',
'../dukweb.wasm',
'../jquery-1.11.2.js',
'../dukweb/dukweb.css',
'../dukweb/dukweb.html' ]:

Loading…
Cancel
Save