Skip to content

Commit 3342bba

Browse files
committed
Add example in Makefile showing how we could compile a worker-only wrapper
This is slow and somewhat error prone since it results in recompiling the .wasm files, so I am leaving it out for now. Also including some unused changes to test-worker.js that I added when trying to get that test running again.
1 parent b3d2c37 commit 3342bba

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

Makefile

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Note: Last tested with version 1.38.15 of Emscripten
1+
# Note: Last built with version 1.38.30 of Emscripten
22

33
# TODO: Emit a file showing which version of emcc and SQLite was used to compile the emitted output.
44
# TODO: Make it easier to use a newer version of Sqlite.
@@ -41,13 +41,13 @@ all: optimized debug worker
4141
.PHONY: debug
4242
debug: dist/sql-asm-debug.js dist/sql-wasm-debug.js
4343

44-
dist/sql-asm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) out/api.js exported_functions exported_runtime_methods
44+
dist/sql-asm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) out/api.js exported_functions exported_runtime_methods
4545
$(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) -s WASM=0 $(BITCODE_FILES) --pre-js out/api.js -o $@
4646
mv $@ out/tmp-raw.js
4747
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
4848
rm out/tmp-raw.js
4949

50-
dist/sql-wasm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) out/api.js exported_functions exported_runtime_methods
50+
dist/sql-wasm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) out/api.js exported_functions exported_runtime_methods
5151
$(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) $(EMFLAGS_WASM) $(BITCODE_FILES) --pre-js out/api.js -o $@
5252
mv $@ out/tmp-raw.js
5353
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
@@ -94,6 +94,24 @@ dist/worker.sql-wasm.js: dist/sql-wasm.js out/worker.js
9494
dist/worker.sql-wasm-debug.js: dist/sql-wasm-debug.js out/worker.js
9595
cat $^ > $@
9696

97+
# Building it this way gets us a wrapper that _knows_ it's in worker mode, which is nice.
98+
# However, since we can't tell emcc that we don't need the wasm generated, and just want the wrapper, we have to pay to have the .wasm generated
99+
# even though we would have already generated it with our sql-wasm.js target above.
100+
# This would be made easier if this is implemented: https://github.com/emscripten-core/emscripten/issues/8506
101+
# dist/worker.sql-wasm.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) out/api.js out/worker.js exported_functions exported_runtime_methods dist/sql-wasm-debug.wasm
102+
# $(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) -s ENVIRONMENT=worker -s $(EMFLAGS_WASM) $(BITCODE_FILES) --pre-js out/api.js -o out/sql-wasm.js
103+
# mv out/sql-wasm.js out/tmp-raw.js
104+
# cat src/shell-pre.js out/tmp-raw.js src/shell-post.js out/worker.js > $@
105+
# #mv out/sql-wasm.wasm dist/sql-wasm.wasm
106+
# rm out/tmp-raw.js
107+
108+
# dist/worker.sql-wasm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) out/api.js out/worker.js exported_functions exported_runtime_methods dist/sql-wasm-debug.wasm
109+
# $(EMCC) -s ENVIRONMENT=worker $(EMFLAGS) $(EMFLAGS_DEBUG) -s ENVIRONMENT=worker -s WASM_BINARY_FILE=sql-wasm-foo.debug $(EMFLAGS_WASM) $(BITCODE_FILES) --pre-js out/api.js -o out/sql-wasm-debug.js
110+
# mv out/sql-wasm-debug.js out/tmp-raw.js
111+
# cat src/shell-pre.js out/tmp-raw.js src/shell-post.js out/worker.js > $@
112+
# #mv out/sql-wasm-debug.wasm dist/sql-wasm-debug.wasm
113+
# rm out/tmp-raw.js
114+
97115
out/api.js: src/output-pre.js src/api.coffee src/exports.coffee src/api-data.coffee src/output-post.js
98116
cat src/api.coffee src/exports.coffee src/api-data.coffee | coffee --bare --compile --stdio > $@
99117
cat src/output-pre.js $@ src/output-post.js > out/api-wrapped.js

src/worker.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Since this is only included in web worker builds, I'm not sure if we need to detect this.
2+
# In fact, it seems like we might want to throw an error if importScripts isn't available.
13
if typeof importScripts is 'function' # Detect webworker context
24
db = null
35

test/test_worker.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
// TODO: Instead of using tiny-worker, we could use the new Node 11 workers via
33
// node --experimental-worker test/all.js
44
// Then we could do this:
5-
// const { Worker } = require('worker_threads');
5+
//const { Worker } = require('worker_threads');
66
// But it turns out that the worker_threads interface is just different enough not to work.
77
var Worker = require("tiny-worker");
88
var path = require("path");
99

10-
1110
exports.test = function(notUsed, assert, done) {
1211

13-
console.error("Skipping: This test is 'expected' to fail because of the way emscripten detects ENVIRONMENT_IS_WORKER.");
12+
// We keep running into issues trying to simulate the worker environment.
13+
// We really need headless testing of some sort
14+
console.error("Skipping: This test is 'expected' to fail because tiny-worker and workerjs don't simulate the environment well enough");
1415
done();
1516
return;
1617

@@ -26,9 +27,10 @@ exports.test = function(notUsed, assert, done) {
2627
//
2728

2829
var target = process.argv[2];
29-
var file = target ? "sql-"+target : "sql";
30+
var file = target ? "sql-"+target : "sql-wasm";
3031
// If we use tiny-worker, we need to pass in this new cwd as the root of the file being loaded:
31-
var worker = new Worker(path.join(__dirname, "../dist/worker."+file+".js"), null, { cwd: path.join(__dirname, "../dist/") });
32+
const filename = "../dist/worker."+file+".js";
33+
var worker = new Worker(path.join(__dirname, filename), null, { cwd: path.join(__dirname, "../dist/") });
3234

3335
// The following tests are continually overwriting worker.onmessage so that they
3436

@@ -105,17 +107,12 @@ if (!Array.from) {
105107
}
106108

107109
if (module == require.main) {
108-
const target_file = process.argv[2];
109-
const sql_loader = require('./load_sql_lib');
110-
sql_loader(target_file).then((sql)=>{
111-
require('test').run({
112-
'test worker': function(assert, done){
113-
exports.test(sql, assert, done);
114-
}
115-
});
116-
})
117-
.catch((e)=>{
118-
console.error(e);
119-
assert.fail(e);
110+
process.on('unhandledRejection', r => console.log(r));
111+
112+
require('test').run({
113+
'test worker': function(assert, done){
114+
exports.test(null, assert, done);
115+
}
120116
});
117+
121118
}

0 commit comments

Comments
 (0)