Skip to content
Merged
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
18 changes: 16 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ jobs:
- uses: actions/checkout@v6
- name: build
run: |
cmake -B build -DQJS_BUILD_WERROR=ON -DQJS_BUILD_EXAMPLES=ON -G "Visual Studio 17 2022" -A ${{matrix.config.arch}}
cmake -B build -DQJS_BUILD_WERROR=ON -DQJS_BUILD_EXAMPLES=ON -A ${{matrix.config.arch}}
cmake --build build --config ${{matrix.config.buildType}}
- name: stats
run: |
Expand Down Expand Up @@ -264,7 +264,7 @@ jobs:
- name: build
run: |
git submodule update --init --checkout --depth 1
cmake -B build -DQJS_BUILD_WERROR=ON -DQJS_BUILD_EXAMPLES=ON -G "Visual Studio 17 2022" -T ClangCL
cmake -B build -DQJS_BUILD_WERROR=ON -DQJS_BUILD_EXAMPLES=ON -T ClangCL
cmake --build build --config ${{matrix.buildType}}
- name: stats
run: |
Expand Down Expand Up @@ -721,9 +721,11 @@ jobs:
args: >-
"-Db_sanitize=address,undefined"
"-Db_lundef=false"
"-Db_vscrt=md"
extra_envs:
CC: clang-cl
CXX: clang-cl
ASAN_OPTIONS: report_globals=0:halt_on_error=1:abort_on_error=1:print_summary=1
platform:
- ubuntu-latest
- windows-latest
Expand Down Expand Up @@ -818,6 +820,18 @@ jobs:

- name: Checkout
uses: actions/checkout@v6
- name: Set up Clang sanitizer runtime
if: ${{ matrix.platform == 'windows-latest' && matrix.mode.name == 'clang-cl+sanitize' }}
run: |
$clangResourceDir = (& clang-cl --print-resource-dir).Trim()
$clangRuntimeDir = Join-Path $clangResourceDir "lib\windows"
$asanRuntime = Join-Path $clangRuntimeDir "clang_rt.asan_dynamic-x86_64.lib"
$asanThunk = Join-Path $clangRuntimeDir "clang_rt.asan_dynamic_runtime_thunk-x86_64.lib"
if (!(Test-Path $asanRuntime) -or !(Test-Path $asanThunk)) {
throw "Clang ASan runtime libraries not found in: $clangRuntimeDir"
}
"LIB=$clangRuntimeDir;$env:LIB" >> $env:GITHUB_ENV
$clangRuntimeDir >> $env:GITHUB_PATH
- name: Activate MSVC and Configure
if: ${{ matrix.platform == 'windows-latest' }}
env: ${{ matrix.mode.extra_envs }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.a
*.orig
*.so
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ This project is a _fork_ of the [original QuickJS project] by Fabrice Bellard an
Head over to the [project website] for instructions on how to get started and more
documentation.

## Zephyr

QuickJS can use Zephyr's libc and native synchronization primitives, including 64-bit Atomics on
targets without lock-free 64-bit instructions. The [Zephyr example](examples/zephyr/README.md)
builds and runs the engine directly on the `mps2/an385` QEMU board.

## Authors

[@bnoordhuis], [@saghul], and many more [contributors].
Expand Down
124 changes: 117 additions & 7 deletions cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#if !defined(_MSC_VER) && !defined(RQUICKJS_WASM_FREESTANDING)
#if defined(__ZEPHYR__)
#include <zephyr/posix/sys/time.h>
#elif !defined(_MSC_VER) && !defined(RQUICKJS_WASM_FREESTANDING)
#include <sys/time.h>
#endif
#if defined(__APPLE__)
Expand Down Expand Up @@ -66,7 +68,10 @@ uint64_t rquickjs_browser_hrtime_ns(void);
#include <windows.h>
#include <process.h> // _beginthread
#endif
#if !defined(_WIN32) && !defined(EMSCRIPTEN) && !defined(__wasi__) && !defined(__DJGPP) && !defined(RQUICKJS_WASM_FREESTANDING)
#if defined(__ZEPHYR__)
#include <errno.h>
#include <zephyr/posix/pthread.h>
#elif !defined(_WIN32) && !defined(EMSCRIPTEN) && !defined(__wasi__) && !defined(__DJGPP) && !defined(RQUICKJS_WASM_FREESTANDING)
#include <errno.h>
#include <pthread.h>
#endif
Expand All @@ -87,10 +92,16 @@ uint64_t rquickjs_browser_hrtime_ns(void);
# define __attribute__(x)
# define __attribute(x)
#else
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)
# ifndef likely
# define likely(x) __builtin_expect(!!(x), 1)
# endif
# ifndef unlikely
# define unlikely(x) __builtin_expect(!!(x), 0)
# endif
# define no_inline __attribute__((noinline))
# define __maybe_unused __attribute__((unused))
# ifndef __maybe_unused
# define __maybe_unused __attribute__((unused))
# endif
#endif

#ifndef offsetof
Expand Down Expand Up @@ -628,7 +639,13 @@ static inline int js_exepath(char* buffer, size_t* size);

#define JS_HAVE_THREADS 1

#if defined(_WIN32)
#if defined(__ZEPHYR__)
#define JS_ONCE_INIT ATOMIC_INIT(0)
typedef atomic_t js_once_t;
typedef struct k_mutex js_mutex_t;
typedef struct k_condvar js_cond_t;
typedef pthread_t js_thread_t;
#elif defined(_WIN32)
#define JS_ONCE_INIT INIT_ONCE_STATIC_INIT
typedef INIT_ONCE js_once_t;
typedef CRITICAL_SECTION js_mutex_t;
Expand Down Expand Up @@ -1726,7 +1743,100 @@ static inline int js_exepath(char* buffer, size_t* size_ptr) {
/*--- Cross-platform threading APIs. ----*/

#if JS_HAVE_THREADS
#if defined(_WIN32)
#if defined(__ZEPHYR__)

static inline void js_once(js_once_t *guard, void (*callback)(void)) {
if (atomic_cas(guard, 0, 1)) {
callback();
atomic_set(guard, 2);
} else {
while (atomic_get(guard) != 2)
k_yield();
}
}

static inline void js_mutex_init(js_mutex_t *mutex) {
if (k_mutex_init(mutex))
abort();
}

static inline void js_mutex_destroy(js_mutex_t *mutex) {
(void)mutex;
}

static inline void js_mutex_lock(js_mutex_t *mutex) {
if (k_mutex_lock(mutex, K_FOREVER))
abort();
}

static inline void js_mutex_unlock(js_mutex_t *mutex) {
if (k_mutex_unlock(mutex))
abort();
}

static inline void js_cond_init(js_cond_t *cond) {
if (k_condvar_init(cond))
abort();
}

static inline void js_cond_destroy(js_cond_t *cond) {
(void)cond;
}

static inline void js_cond_signal(js_cond_t *cond) {
if (k_condvar_signal(cond))
abort();
}

static inline void js_cond_broadcast(js_cond_t *cond) {
if (k_condvar_broadcast(cond) < 0)
abort();
}

static inline void js_cond_wait(js_cond_t *cond, js_mutex_t *mutex) {
if (k_condvar_wait(cond, mutex, K_FOREVER))
abort();
}

static inline int js_cond_timedwait(js_cond_t *cond, js_mutex_t *mutex,
uint64_t timeout) {
int ret = k_condvar_wait(cond, mutex, K_NSEC(timeout));
if (ret == 0)
return 0;
if (ret == -EAGAIN) {
if (k_mutex_lock(mutex, K_FOREVER))
abort();
return -1;
}
abort();
}

static inline int js_thread_create(js_thread_t *thrd, void (*start)(void *), void *arg,
int flags) {
union {
void (*x)(void *);
void *(*f)(void *);
} u = {start};
pthread_attr_t attr;
int ret = -1;

if (flags & ~JS_THREAD_CREATE_DETACHED || pthread_attr_init(&attr))
return -1;
if ((flags & JS_THREAD_CREATE_DETACHED) &&
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
goto fail;
if (!pthread_create(thrd, &attr, u.f, arg))
ret = 0;
fail:
pthread_attr_destroy(&attr);
return ret;
}

static inline int js_thread_join(js_thread_t thrd) {
return pthread_join(thrd, NULL) ? -1 : 0;
}

#elif defined(_WIN32)
typedef void (*js__once_cb)(void);

typedef struct {
Expand Down
16 changes: 16 additions & 0 deletions examples/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(quickjs_zephyr)

get_filename_component(QUICKJS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE)

target_sources(app PRIVATE
src/main.c
${QUICKJS_ROOT}/quickjs.c
${QUICKJS_ROOT}/dtoa.c
${QUICKJS_ROOT}/libregexp.c
${QUICKJS_ROOT}/libunicode.c
)
target_include_directories(app PRIVATE ${QUICKJS_ROOT})
target_compile_definitions(app PRIVATE _GNU_SOURCE)
17 changes: 17 additions & 0 deletions examples/zephyr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Zephyr example

This sample builds the QuickJS engine directly into a Zephyr application and validates the C API,
promises and pending jobs, SharedArrayBuffer, timed waits, and 8/16/32/64-bit Atomics.

Zephyr 4.4.2 and SDK 1.0.1 were used for the initial port. The `mps2/an385` QEMU board has enough
RAM and flash for the full engine.

```sh
source /Users/yorkie/zephyrproject/env.sh
cd /Users/yorkie/zephyrproject
west build -b mps2/an385 /Users/yorkie/workspace/jsar-project/quickjs/examples/zephyr \
-d build/quickjs-zephyr
west build -d build/quickjs-zephyr -t run
```

Exit QEMU with Ctrl+A, then X.
11 changes: 11 additions & 0 deletions examples/zephyr/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CONFIG_MAIN_STACK_SIZE=65536
CONFIG_HEAP_MEM_POOL_SIZE=2097152

CONFIG_POSIX_API=y
CONFIG_POSIX_THREAD_THREADS_MAX=4
CONFIG_MAX_PTHREAD_MUTEX_COUNT=8
CONFIG_MAX_PTHREAD_COND_COUNT=8
CONFIG_POSIX_THREAD_ATTR_STACKSIZE=y

CONFIG_LOG=y
CONFIG_LOG_BACKEND_RTT=n
108 changes: 108 additions & 0 deletions examples/zephyr/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <stdint.h>
#include <stdio.h>

#include <zephyr/kernel.h>

#include "quickjs.h"

static int report_exception(JSContext *ctx, const char *stage)
{
JSValue exception = JS_GetException(ctx);
const char *message = JS_ToCString(ctx, exception);

printk("QuickJS %s failed: %s\n", stage, message ? message : "unknown exception");
JS_FreeCString(ctx, message);
JS_FreeValue(ctx, exception);
return -1;
}

int main(void)
{
JSRuntime *runtime = JS_NewRuntime();
JSContext *context;
JSContext *job_context;
JSValue value;
int32_t result;

if (runtime == NULL) {
printk("QuickJS runtime allocation failed\n");
return 1;
}
JS_SetCanBlock(runtime, true);

context = JS_NewContext(runtime);
if (context == NULL) {
printk("QuickJS context allocation failed\n");
JS_FreeRuntime(runtime);
return 1;
}

value = JS_Eval(context,
"globalThis.answer = 0;"
"Promise.resolve(40).then(v => { globalThis.answer = v + 2; });",
sizeof("globalThis.answer = 0;"
"Promise.resolve(40).then(v => { globalThis.answer = v + 2; });") - 1,
"<zephyr-promise>", JS_EVAL_TYPE_GLOBAL);
if (JS_IsException(value)) {
JS_FreeValue(context, value);
report_exception(context, "promise evaluation");
goto fail;
}
JS_FreeValue(context, value);

while (JS_IsJobPending(runtime)) {
if (JS_ExecutePendingJob(runtime, &job_context) < 0) {
report_exception(job_context, "pending job");
goto fail;
}
}

static const char atomics_script[] =
"(() => {"
" const sab = new SharedArrayBuffer(32);"
" const u8 = new Uint8Array(sab);"
" const i16 = new Int16Array(sab);"
" const i32 = new Int32Array(sab);"
" const i64 = new BigInt64Array(sab);"
" if (Atomics.add(i32, 0, 40) !== 0) return -1;"
" if (Atomics.compareExchange(i32, 0, 40, 42) !== 40) return -2;"
" if (Atomics.wait(i32, 1, 0, 1) !== 'timed-out') return -3;"
" if (Atomics.exchange(u8, 16, 5) !== 0) return -4;"
" if (Atomics.or(u8, 16, 2) !== 5) return -5;"
" if (Atomics.store(i16, 9, 9) !== 9) return -6;"
" if (Atomics.xor(i16, 9, 5) !== 9) return -7;"
" if (Atomics.store(i64, 1, 7n) !== 7n) return -8;"
" if (Atomics.add(i64, 1, 2n) !== 7n) return -9;"
" if (Atomics.load(i64, 1) !== 9n) return -10;"
" return globalThis.answer;"
"})()";

value = JS_Eval(context, atomics_script, sizeof(atomics_script) - 1,
"<zephyr-atomics>", JS_EVAL_TYPE_GLOBAL);
if (JS_IsException(value)) {
JS_FreeValue(context, value);
report_exception(context, "Atomics evaluation");
goto fail;
}
if (JS_ToInt32(context, &result, value) < 0) {
JS_FreeValue(context, value);
report_exception(context, "result conversion");
goto fail;
}
JS_FreeValue(context, value);

printk("QuickJS Zephyr returned %ld\n", (long)result);
if (result != 42) {
goto fail;
}

printk("QuickJS Zephyr: PASS\n");
JS_FreeContext(context);
JS_FreeRuntime(runtime);
return 0;

fail:
JS_FreeContext(context);
JS_FreeRuntime(runtime);
return 1;
}
10 changes: 10 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ if host_system == 'windows'
stack_size = 8 * 1024 * 1024
if cc.get_argument_syntax() == 'msvc'
add_project_link_arguments(f'/STACK:@stack_size@', language: 'c')
if cc.get_id() == 'clang-cl' and get_option('b_sanitize').contains('address')
clang_rt_arch = host_machine.cpu_family() == 'x86' ? 'i386' : host_machine.cpu_family()
add_project_link_arguments(
f'clang_rt.asan_dynamic-@clang_rt_arch@.lib',
'/include:__asan_seh_interceptor',
f'/wholearchive:clang_rt.asan_dynamic_runtime_thunk-@clang_rt_arch@.lib',
'/incremental:no',
language: 'c',
)
endif
else
add_project_link_arguments(f'-Wl,--stack,@stack_size@', language: 'c')
endif
Expand Down
Loading
Loading