Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ jobs:
wasmtime run build/qjs -qd
echo "console.log('hello wasi!');" > t.js
wasmtime run --dir . build/qjs t.js
- name: build wasi reactor
run: |
cmake -B build -DCMAKE_TOOLCHAIN_FILE=/opt/wasi-sdk/share/cmake/wasi-sdk.cmake -DQJS_WASI_REACTOR=ON
make -C build qjs_wasi
ls -lh build/qjs.wasm
cygwin:
runs-on: windows-latest
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,18 @@ jobs:
cmake -B build -DCMAKE_TOOLCHAIN_FILE=/opt/wasi-sdk/share/cmake/wasi-sdk.cmake
make -C build qjs_exe
mv build/qjs build/qjs-wasi.wasm
- name: build wasi reactor
run: |
cmake -B build -DCMAKE_TOOLCHAIN_FILE=/opt/wasi-sdk/share/cmake/wasi-sdk.cmake -DQJS_WASI_REACTOR=ON
make -C build qjs_wasi
mv build/qjs.wasm build/qjs-wasi-reactor.wasm
- name: upload
uses: actions/upload-artifact@v6
with:
name: qjs-wasi
path: build/qjs-wasi.wasm
path: |
build/qjs-wasi.wasm
build/qjs-wasi-reactor.wasm

upload-to-release:
needs: [linux, macos, windows, wasi, check_meson_version]
Expand Down
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,36 @@ target_link_libraries(qjs_exe qjs)
if(NOT WIN32)
set_target_properties(qjs_exe PROPERTIES ENABLE_EXPORTS TRUE)
endif()

# WASI Reactor
#

if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
option(QJS_WASI_REACTOR "Build WASI reactor (exports library functions, no _start)" OFF)
if(QJS_WASI_REACTOR)
add_executable(qjs_wasi
quickjs-libc.c
qjs-wasi-reactor.c
)
set_target_properties(qjs_wasi PROPERTIES
OUTPUT_NAME "qjs"
SUFFIX ".wasm"
)
target_compile_definitions(qjs_wasi PRIVATE ${qjs_defines})
target_link_libraries(qjs_wasi qjs)
target_link_options(qjs_wasi PRIVATE
-mexec-model=reactor
# Export all symbols with default visibility (JS_EXTERN functions)
-Wl,--export-dynamic
# Memory management (libc symbols need explicit export)
-Wl,--export=malloc
-Wl,--export=free
-Wl,--export=realloc
-Wl,--export=calloc
)
endif()
endif()

if(QJS_BUILD_CLI_WITH_MIMALLOC OR QJS_BUILD_CLI_WITH_STATIC_MIMALLOC)
find_package(mimalloc REQUIRED)
# Upstream mimalloc doesn't provide a way to know if both libraries are supported.
Expand Down
208 changes: 208 additions & 0 deletions qjs-wasi-reactor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/*
* QuickJS WASI Reactor Mode
*
* In reactor mode, QuickJS exports functions that can be called repeatedly
* by the host instead of running main() once and blocking in the event loop.
*
* Copyright (c) 2017-2021 Fabrice Bellard
* Copyright (c) 2017-2021 Charlie Gordon
*
* 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.
*/

/* Include qjs.c to get access to static functions like eval_buf, eval_file,
* parse_limit, and JS_NewCustomContext */
#include "qjs.c"

static JSRuntime *reactor_rt = NULL;
static JSContext *reactor_ctx = NULL;

int qjs_init_argv(int argc, char **argv);

__attribute__((export_name("qjs_init")))
int qjs_init(void)
{
static char *empty_argv[] = { "qjs", NULL };
return qjs_init_argv(1, empty_argv);
}

__attribute__((export_name("qjs_init_argv")))
int qjs_init_argv(int argc, char **argv)
{
int optind = 1;
char *expr = NULL;
int module = -1;
int load_std = 0;
char *include_list[32];
int i, include_count = 0;
int64_t memory_limit = -1;
int64_t stack_size = -1;

if (reactor_rt)
return -1; /* already initialized */

/* Parse options (subset of main()) */
while (optind < argc && *argv[optind] == '-') {
char *arg = argv[optind] + 1;
const char *longopt = "";
char *optarg = NULL;
if (!*arg)
break;
optind++;
if (*arg == '-') {
longopt = arg + 1;
optarg = strchr(longopt, '=');
if (optarg)
*optarg++ = '\0';
arg += strlen(arg);
if (!*longopt)
break;
}
for (; *arg || *longopt; longopt = "") {
char opt = *arg;
if (opt) {
arg++;
if (!optarg && *arg)
optarg = arg;
}
if (opt == 'e' || !strcmp(longopt, "eval")) {
if (!optarg) {
if (optind >= argc)
return -1;
optarg = argv[optind++];
}
expr = optarg;
break;
}
if (opt == 'I' || !strcmp(longopt, "include")) {
if (optind >= argc || include_count >= countof(include_list))
return -1;
include_list[include_count++] = argv[optind++];
continue;
}
if (opt == 'm' || !strcmp(longopt, "module")) {
module = 1;
continue;
}
if (!strcmp(longopt, "std")) {
load_std = 1;
continue;
}
if (!strcmp(longopt, "memory-limit")) {
if (!optarg) {
if (optind >= argc)
return -1;
optarg = argv[optind++];
}
memory_limit = parse_limit(optarg);
break;
}
if (!strcmp(longopt, "stack-size")) {
if (!optarg) {
if (optind >= argc)
return -1;
optarg = argv[optind++];
}
stack_size = parse_limit(optarg);
break;
}
break; /* ignore unknown options */
}
}

reactor_rt = JS_NewRuntime();
if (!reactor_rt)
return -1;
if (memory_limit >= 0)
JS_SetMemoryLimit(reactor_rt, (size_t)memory_limit);
if (stack_size >= 0)
JS_SetMaxStackSize(reactor_rt, (size_t)stack_size);

js_std_set_worker_new_context_func(JS_NewCustomContext);
js_std_init_handlers(reactor_rt);

reactor_ctx = JS_NewCustomContext(reactor_rt);
if (!reactor_ctx) {
js_std_free_handlers(reactor_rt);
JS_FreeRuntime(reactor_rt);
reactor_rt = NULL;
return -1;
}

JS_SetModuleLoaderFunc2(reactor_rt, NULL, js_module_loader,
js_module_check_attributes, NULL);
JS_SetHostPromiseRejectionTracker(reactor_rt, js_std_promise_rejection_tracker, NULL);
js_std_add_helpers(reactor_ctx, argc - optind, argv + optind);

if (load_std) {
const char *str =
"import * as bjson from 'qjs:bjson';\n"
"import * as std from 'qjs:std';\n"
"import * as os from 'qjs:os';\n"
"globalThis.bjson = bjson;\n"
"globalThis.std = std;\n"
"globalThis.os = os;\n";
if (eval_buf(reactor_ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE))
goto fail;
}

for (i = 0; i < include_count; i++) {
if (eval_file(reactor_ctx, include_list[i], 0))
goto fail;
}

if (expr) {
if (eval_buf(reactor_ctx, expr, strlen(expr), "<cmdline>",
module == 1 ? JS_EVAL_TYPE_MODULE : 0))
goto fail;
} else if (optind < argc) {
if (eval_file(reactor_ctx, argv[optind], module))
goto fail;
}

return 0;

fail:
js_std_free_handlers(reactor_rt);
JS_FreeContext(reactor_ctx);
JS_FreeRuntime(reactor_rt);
reactor_rt = NULL;
reactor_ctx = NULL;
return -1;
}

__attribute__((export_name("qjs_get_context")))
JSContext *qjs_get_context(void)
{
return reactor_ctx;
}

__attribute__((export_name("qjs_destroy")))
void qjs_destroy(void)
{
if (reactor_ctx) {
js_std_free_handlers(reactor_rt);
JS_FreeContext(reactor_ctx);
reactor_ctx = NULL;
}
if (reactor_rt) {
JS_FreeRuntime(reactor_rt);
reactor_rt = NULL;
}
}
Loading