Skip to content

Commit 8b4675a

Browse files
authored
Emit async imports for variants (justjake#100)
* upgrade typescript * attempt to use nodenext tsc * revert enabling noUnused{Locals,Parameters} * fix imports to use real extensions * update mocha for ESM * awkward async import issue * npm run build * examples/website: update for newer NPM, typescript * fix behavior for webpack build * bump emsdk to 3.1.31 * fix function signature * cache another item * use EMSDK 3.1.32 native, 3.1.31 in Docker * add some extra default timeout * Fix breakage caused by upgrade * rebuild * More precise/strict compilation (drop support for node<16) * rebuild * add note about MINIMAL_RUNTIME * tested website * ? * pretty
1 parent 4adb7c7 commit 8b4675a

27 files changed

+53504
-21180
lines changed

Diff for: .github/workflows/main.yml

+11-6
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ jobs:
3030
if: matrix.platform == 'macos-latest'
3131

3232
- name: Setup Node.js environment
33-
uses: actions/setup-node@v1
33+
uses: actions/setup-node@v3
3434
with:
35-
# Version Spec of the version to use. Examples: 10.x, 10.15.1, >=10.15.0
36-
node-version: 12.x
35+
node-version: 16
3736

3837
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
39-
- uses: actions/checkout@v2
38+
- uses: actions/checkout@v3
4039

41-
- name: Cache dependencies
42-
uses: actions/cache@v2
40+
- name: Yarn cache
41+
uses: actions/cache@v3
4342
with:
4443
path: "**/.yarn/cache"
4544
key: ${{ hashFiles('**/yarn.lock') }}
@@ -49,6 +48,12 @@ jobs:
4948
env:
5049
YARN_ENABLE_SCRIPTS: 0
5150

51+
- name: EMSDK cache
52+
uses: actions/cache@v3
53+
with:
54+
path: "emsdk-cache"
55+
key: ${{ hashFiles('scripts/emcc.sh') }}
56+
5257
- name: Build
5358
run: yarn build
5459

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ npm-error.log
77

88
/build
99
/dist
10+
/emsdk-cache
1011

1112
# QuickJS build stuff
1213
/quickjs/test_fib.c
@@ -24,6 +25,5 @@ npm-error.log
2425
/quickjs/repl.c
2526
/quickjs/run-test262
2627

27-
2828
**/.yarn/*
2929
!/.yarn/releases

Diff for: .mocharc.cjs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict"
2+
3+
// https://mochajs.org/#configuring-mocha-nodejs
4+
5+
module.exports = {
6+
// https://typestrong.org/ts-node/docs/recipes/mocha/
7+
require: [
8+
// Specify "require" for CommonJS
9+
// "ts-node/register",
10+
"source-map-support/register",
11+
],
12+
// Specify "loader" for native ESM
13+
loader: "ts-node/esm",
14+
extensions: ["js", "ts", "tsx"],
15+
timeout: 5000,
16+
17+
"watch-files": [
18+
"ts/**/*",
19+
"build/**/*.js",
20+
// "build/**/*.ts",
21+
],
22+
}

Diff for: Makefile

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Tools
22
CC=clang
3-
EMSDK_VERSION=3.1.7
4-
EMSDK_DOCKER_IMAGE=emscripten/emsdk:$(EMSDK_VERSION)
5-
EMCC=EMSDK_VERSION=$(EMSDK_VERSION) EMSDK_DOCKER_IMAGE=$(EMSDK_DOCKER_IMAGE) EMSDK_DOCKER_CACHE=$(THIS_DIR)/$(BUILD_EMSDK_CACHE)/$(EMSDK_VERSION) scripts/emcc.sh
3+
EMSDK_VERSION=3.1.32
4+
EMSDK_DOCKER_IMAGE=emscripten/emsdk:3.1.31 # .32 not released to Docker hub.
5+
EMCC=EMSDK_VERSION=$(EMSDK_VERSION) EMSDK_DOCKER_IMAGE=$(EMSDK_DOCKER_IMAGE) EMSDK_DOCKER_CACHE=$(THIS_DIR)/emsdk-cache/$(EMSDK_VERSION) scripts/emcc.sh
66
GENERATE_TS=$(VARIANT_GENERATE_TS_ENV) npx ts-node generate.ts
77
PRETTIER=npx prettier
88
THIS_DIR := $(dir $(abspath $(firstword $(MAKEFILE_LIST))))
@@ -41,7 +41,6 @@ WRAPPER_ROOT=c
4141
BUILD_ROOT=build
4242
BUILD_WRAPPER=$(BUILD_ROOT)/wrapper
4343
BUILD_QUICKJS=$(BUILD_ROOT)/quickjs
44-
BUILD_EMSDK_CACHE=$(BUILD_ROOT)/emsdk-cache
4544
BUILD_TS=ts/generated
4645

4746
# QuickJS
@@ -51,25 +50,44 @@ QUICKJS_DEFINES:=-D_GNU_SOURCE -DCONFIG_VERSION=\"$(QUICKJS_CONFIG_VERSION)\" -D
5150
VARIANT_QUICKJS_OBJS=$(patsubst %.o, $(BUILD_QUICKJS)/%.$(VARIANT).o, $(QUICKJS_OBJS))
5251

5352
# quickjs-emscripten
53+
WRAPPER_DEFINES+=-Wcast-function-type # Likewise, warns about some quickjs casts we don't control.
5454
EMCC_EXPORTED_FUNCS+=-s EXPORTED_FUNCTIONS=@$(BUILD_WRAPPER)/symbols.json
5555
EMCC_EXPORTED_FUNCS_ASYNCIFY+=-s EXPORTED_FUNCTIONS=@$(BUILD_WRAPPER)/symbols.asyncify.json
5656

5757
# Emscripten options
5858
CFLAGS_WASM+=-s WASM=1
5959
CFLAGS_WASM+=-s [email protected]
60-
CFLAGS_WASM+=-s NODEJS_CATCH_EXIT=0
6160
CFLAGS_WASM+=-s MODULARIZE=1
6261
CFLAGS_WASM+=-s EXPORT_NAME=QuickJSRaw
6362
CFLAGS_WASM+=-s INVOKE_RUN=0
6463
CFLAGS_WASM+=-s ALLOW_MEMORY_GROWTH=1
6564
CFLAGS_WASM+=-s ALLOW_TABLE_GROWTH=1
65+
CFLAGS_WASM+=-s STACK_SIZE=5MB
66+
# CFLAGS_WASM+=-s MINIMAL_RUNTIME=1 # Appears to break MODULARIZE
67+
CFLAGS_WASM+=-s SUPPORT_ERRNO=0
68+
69+
# Emscripten options - like STRICT
70+
# https://github.com/emscripten-core/emscripten/blob/fa339b76424ca9fbe5cf15faea0295d2ac8d58cc/src/settings.js#L1095-L1109
71+
# CFLAGS_WASM+=-s STRICT_JS=1 # Doesn't work with MODULARIZE
72+
CFLAGS_WASM+=-s IGNORE_MISSING_MAIN=0 --no-entry
73+
CFLAGS_WASM+=-s AUTO_JS_LIBRARIES=0
74+
CFLAGS_WASM+=-s -lccall.js
75+
CFLAGS_WASM+=-s AUTO_NATIVE_LIBRARIES=0
76+
CFLAGS_WASM+=-s AUTO_ARCHIVE_INDEXES=0
77+
CFLAGS_WASM+=-s DEFAULT_TO_CXX=0
78+
CFLAGS_WASM+=-s ALLOW_UNIMPLEMENTED_SYSCALLS=0
79+
80+
# Emscripten options - NodeJS
81+
CFLAGS_WASM+=-s MIN_NODE_VERSION=160000
82+
CFLAGS_WASM+=-s NODEJS_CATCH_EXIT=0
6683

6784
# Empscripten options for asyncify variant
6885
# https://emscripten.org/docs/porting/asyncify.html
6986
CFLAGS_WASM_ASYNCIFY+=-s ASYNCIFY=1
7087
CFLAGS_WASM_ASYNCIFY+=-DQTS_ASYNCIFY=1
7188
CFLAGS_WASM_ASYNCIFY+=-s ASYNCIFY_REMOVE=@$(BUILD_WRAPPER)/asyncify-remove.json
7289
CFLAGS_WASM_ASYNCIFY+=-s ASYNCIFY_IMPORTS=@$(BUILD_WRAPPER)/asyncify-imports.json
90+
CFLAGS_WASM_ASYNCIFY+=-lasync.js
7391
GENERATE_TS_ENV_ASYNCIFY+=ASYNCIFY=true
7492

7593
# Release options
@@ -82,8 +100,10 @@ CFLAGS_WASM_RELEASE+=-s FILESYSTEM=0
82100

83101
# Debug options
84102
GENERATE_TS_ENV_DEBUG+=DEBUG=true
103+
85104
CFLAGS_DEBUG+=-O0
86105
CFLAGS_DEBUG+=-DQTS_DEBUG_MODE
106+
87107
CFLAGS_WASM_DEBUG+=-gsource-map
88108
CFLAGS_WASM_DEBUG+=-s ASSERTIONS=1
89109

@@ -202,15 +222,15 @@ WASM_SYMBOLS=$(BUILD_WRAPPER)/symbols.json $(BUILD_WRAPPER)/asyncify-remove.json
202222

203223
$(BUILD_TS)/emscripten-module.$(VARIANT).js: $(BUILD_WRAPPER)/interface.$(VARIANT).o $(VARIANT_QUICKJS_OBJS) $(WASM_SYMBOLS) | scripts/emcc.sh
204224
$(MKDIRP)
205-
$(EMCC) $(VARIANT_CFLAGS) $(EMCC_EXPORTED_FUNCS) -o $@ $< $(VARIANT_QUICKJS_OBJS)
225+
$(EMCC) $(VARIANT_CFLAGS) $(WRAPPER_DEFINES) $(EMCC_EXPORTED_FUNCS) -o $@ $< $(VARIANT_QUICKJS_OBJS)
206226

207227
$(BUILD_TS)/emscripten-module.$(VARIANT).d.ts: ts/types-generated/emscripten-module.$(SYNC).d.ts
208228
echo '// Generated from $<' > $@
209229
cat $< >> $@
210230

211231
$(BUILD_WRAPPER)/%.WASM_$(RELEASE)_$(SYNC).o: $(WRAPPER_ROOT)/%.c $(WASM_SYMBOLS) | scripts/emcc.sh
212232
$(MKDIRP)
213-
$(EMCC) $(VARIANT_CFLAGS) $(CFLAGS_SORTED_FUNCS) -c -o $@ $<
233+
$(EMCC) $(VARIANT_CFLAGS) $(CFLAGS_SORTED_FUNCS) $(WRAPPER_DEFINES) -c -o $@ $<
214234

215235
$(BUILD_QUICKJS)/%.WASM_$(RELEASE)_$(SYNC).o: $(QUICKJS_ROOT)/%.c $(WASM_SYMBOLS) | scripts/emcc.sh
216236
$(MKDIRP)

Diff for: README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,11 @@ the Javascript parts of the library without setting up the Emscripten toolchain.
567567

568568
Intermediate object files from QuickJS end up in ./build/quickjs/.
569569

570-
This project uses `emscripten 3.1.7` via Docker. You will need a working `docker`
571-
install to build the Emscripten artifacts.
570+
This project uses `emscripten 3.1.32`.
571+
572+
- On ARM64, you should install `emscripten` on your machine. For example on macOS, `brew install emscripten`.
573+
- If _the correct version of emcc_ is not in your PATH, compilation falls back to using Docker.
574+
On ARM64, this is 10-50x slower than native compilation, but it's just fine on x64.
572575

573576
Related NPM scripts:
574577

@@ -583,8 +586,7 @@ Related NPM scripts:
583586
The ./ts directory contains Typescript types and wraps the generated Emscripten
584587
FFI in a more usable interface.
585588

586-
You'll need `node` and `npm` or `yarn`. Install dependencies with `npm install`
587-
or `yarn install`.
589+
You'll need `node` and `yarn`. Install dependencies with `yarn install`.
588590

589591
- `yarn build` produces ./dist.
590592
- `yarn test` runs the tests.

Diff for: c/interface.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ EM_JS(MaybeAsync(JSValue *), qts_host_call_function, (JSContext * ctx, JSValueCo
563563
#endif
564564

565565
// Function: QuickJS -> C
566-
JSValue qts_call_function(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, uint32_t magic) {
566+
JSValue qts_call_function(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic) {
567567
JSValue *result_ptr = qts_host_call_function(ctx, &this_val, argc, argv, magic);
568568
if (result_ptr == NULL) {
569569
return JS_UNDEFINED;

Diff for: examples/typescript-smoketest/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: examples/typescript-smoketest/yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44

55
"quickjs-emscripten@file:../../build/quickjs-emscripten.tgz":
6-
"integrity" "sha512-U+tgZEDv7qE8v68mp9amKnv7Am3bXL8OSDQdtFUKx75LCJYr/UdZGNy8NFvKHFBu0dtvm1L+bKu4wlcV0j4eQg=="
6+
"integrity" "sha512-zsmoyZgRQM3p+//njVseFyF1EPGQouNZD3mUzu7bVdiuejLG528XlZn5sEiPbrhCWq8VMo2XW8Nm01ZRTw8Bgg=="
77
"resolved" "file:../../build/quickjs-emscripten.tgz"
8-
"version" "0.21.1"
8+
"version" "0.21.2"
99

1010
1111
"integrity" "sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA=="

0 commit comments

Comments
 (0)