Compare commits

..

2 commits

Author SHA1 Message Date
KaruroChori
2674e97fb5 Add getter for GC threshold 2024-06-04 17:40:07 +02:00
Saúl Ibarra Corretgé
4093f9a318 Use 0 for disabling automatic GC
Similarly to other limits such as memory or stack limit, 0 means
unlimited.
2024-06-04 17:36:36 +02:00
18 changed files with 2927 additions and 3594 deletions

View file

@ -71,26 +71,6 @@ jobs:
- name: test
run: |
make test
linux-riscv64:
runs-on: ubuntu-latest
defaults:
run:
shell: alpine.sh {0}
steps:
- uses: actions/checkout@v3
- uses: jirutka/setup-alpine@v1
with:
arch: riscv64
packages: "build-base make cmake"
- name: build
run: |
make
- name: stats
run: |
make stats
- name: test
run: |
make test
linux-s390x:
runs-on: ubuntu-latest
defaults:
@ -356,13 +336,12 @@ jobs:
strategy:
fail-fast: false
matrix:
arch: [x64, Win32]
buildType: [Debug, Release]
steps:
- uses: actions/checkout@v4
- name: build
run: |
cmake -B build -G "Visual Studio 17 2022" -A ${{matrix.arch}}
cmake -B build -G "Visual Studio 17 2022"
cmake --build build --config ${{matrix.buildType}} --target qjs_exe
cmake --build build --config ${{matrix.buildType}} --target function_source
- name: stats

View file

@ -34,34 +34,6 @@ jobs:
with:
name: qjs
path: build/*-linux-aarch64
linux-riscv64:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
- uses: jirutka/setup-alpine@v1
with:
arch: riscv64
packages: "build-base make cmake"
- name: build
shell: alpine.sh {0}
run: |
mkdir build
cd build
cmake -DBUILD_STATIC_QJS_EXE=ON ..
cd ..
cmake --build build --target qjs_exe -j$(getconf _NPROCESSORS_ONLN)
cmake --build build --target qjsc -j$(getconf _NPROCESSORS_ONLN)
mv build/qjs build/qjs-linux-riscv64
mv build/qjsc build/qjsc-linux-riscv64
- name: check
shell: alpine.sh {0}
run: |
file build/*-linux-riscv64
- name: upload
uses: actions/upload-artifact@v3
with:
name: qjs
path: build/*-linux-riscv64
linux-x86:
runs-on: ubuntu-20.04
steps:
@ -225,7 +197,7 @@ jobs:
path: build/qjs-wasi.wasm
upload-to-release:
needs: [linux-aarch64, linux-riscv64, linux-x86, linux-x86_64, macos, windows-x86, windows-x86_64, wasi]
needs: [linux-x86, linux-x86_64, macos, windows-x86, windows-x86_64, wasi]
runs-on: ubuntu-20.04
steps:
- name: get assets

View file

@ -35,6 +35,7 @@ xcheck_add_c_compiler_flag(-Wno-implicit-fallthrough)
xcheck_add_c_compiler_flag(-Wno-sign-compare)
xcheck_add_c_compiler_flag(-Wno-missing-field-initializers)
xcheck_add_c_compiler_flag(-Wno-unused-parameter)
xcheck_add_c_compiler_flag(-Wno-unused-variable)
xcheck_add_c_compiler_flag(-Wno-unused-but-set-variable)
xcheck_add_c_compiler_flag(-Wno-array-bounds)
xcheck_add_c_compiler_flag(-Wno-format-truncation)

View file

@ -203,16 +203,9 @@ static inline int clz32(unsigned int a)
static inline int clz64(uint64_t a)
{
#if defined(_MSC_VER) && !defined(__clang__)
#if INTPTR_MAX == INT64_MAX
unsigned long index;
_BitScanReverse64(&index, a);
return 63 - index;
#else
if (a >> 32)
return clz32((unsigned)(a >> 32));
else
return clz32((unsigned)a) + 32;
#endif
#else
return __builtin_clzll(a);
#endif

View file

@ -330,11 +330,6 @@ optional properties:
@item backtrace_barrier
Boolean (default = false). If true, error backtraces do not list the
stack frames below the evalScript.
@item async
Boolean (default = false). If true, @code{await} is accepted in the
script and a promise is returned. The promise is resolved with an
object whose @code{value} property holds the value returned by the
script.
@end table
@item loadScript(filename)
@ -722,12 +717,6 @@ write_fd]} or null in case of error.
@item sleep(delay_ms)
Sleep during @code{delay_ms} milliseconds.
@item sleepAsync(delay_ms)
Asynchronouse sleep during @code{delay_ms} milliseconds. Returns a promise. Example:
@example
await os.sleepAsync(500);
@end example
@item setTimeout(func, delay)
Call the function @code{func} after @code{delay} ms. Return a handle
to the timer.

View file

@ -2,53 +2,53 @@
#include "quickjs-libc.h"
const uint32_t qjsc_fib_module_size = 311;
const uint32_t qjsc_fib_module_size = 310;
const uint8_t qjsc_fib_module[311] = {
const uint8_t qjsc_fib_module[310] = {
0x0c, 0x03, 0x2c, 0x65, 0x78, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x73, 0x2f, 0x66, 0x69, 0x62, 0x5f,
0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x2e, 0x6a,
0x73, 0x06, 0x66, 0x69, 0x62, 0x02, 0x6e, 0x0d,
0xb2, 0x03, 0x00, 0x01, 0x00, 0x00, 0xb4, 0x03,
0x00, 0x00, 0x00, 0x0c, 0x20, 0xfa, 0x01, 0x9e,
0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x09,
0x00, 0xb4, 0x03, 0x00, 0x01, 0x0c, 0x43, 0xfa,
0x01, 0xb4, 0x03, 0x01, 0x00, 0x01, 0x04, 0x01,
0x00, 0x1a, 0x01, 0xb6, 0x03, 0x00, 0x01, 0x00,
0xb4, 0x03, 0x00, 0x00, 0xd0, 0xb3, 0xa7, 0xe9,
0x03, 0xb3, 0x28, 0xd0, 0xb4, 0xac, 0xe9, 0x03,
0xb4, 0x28, 0xdc, 0xd0, 0xb4, 0x9e, 0xee, 0xdc,
0xd0, 0xb5, 0x9e, 0xee, 0x9d, 0x28, 0xb2, 0x03,
0x02, 0x08, 0x20, 0x04, 0x00, 0x07, 0x06, 0x07,
0x06, 0x12, 0x09, 0x08, 0x07, 0x07, 0x10, 0x07,
0x06, 0x07, 0x06, 0x12, 0x13, 0x08, 0x07, 0x08,
0x16, 0x0c, 0x0c, 0x07, 0x04, 0x0c, 0x0a, 0x0c,
0x0c, 0x07, 0x04, 0x8d, 0x01, 0x66, 0x75, 0x6e,
0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x66, 0x69,
0x62, 0x28, 0x6e, 0x29, 0x0a, 0x7b, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x6e,
0x20, 0x3c, 0x3d, 0x20, 0x30, 0x29, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x72,
0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x30, 0x3b,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x65, 0x6c, 0x73,
0x65, 0x20, 0x69, 0x66, 0x20, 0x28, 0x6e, 0x20,
0x3d, 0x3d, 0x20, 0x31, 0x29, 0x0a, 0x20, 0x20,
0x00, 0x00, 0x0c, 0x20, 0xfa, 0x01, 0x9e, 0x01,
0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x09, 0x00,
0xb4, 0x03, 0x00, 0x01, 0x0c, 0x43, 0xfa, 0x01,
0xb4, 0x03, 0x01, 0x00, 0x01, 0x04, 0x01, 0x00,
0x1a, 0x01, 0xb6, 0x03, 0x00, 0x01, 0x00, 0xb4,
0x03, 0x00, 0x00, 0xd0, 0xb3, 0xa7, 0xe9, 0x03,
0xb3, 0x28, 0xd0, 0xb4, 0xac, 0xe9, 0x03, 0xb4,
0x28, 0xdc, 0xd0, 0xb4, 0x9e, 0xee, 0xdc, 0xd0,
0xb5, 0x9e, 0xee, 0x9d, 0x28, 0xb2, 0x03, 0x02,
0x08, 0x20, 0x04, 0x00, 0x07, 0x06, 0x07, 0x06,
0x12, 0x09, 0x08, 0x07, 0x07, 0x10, 0x07, 0x06,
0x07, 0x06, 0x12, 0x13, 0x08, 0x07, 0x08, 0x16,
0x0c, 0x0c, 0x07, 0x04, 0x0c, 0x0a, 0x0c, 0x0c,
0x07, 0x04, 0x8d, 0x01, 0x66, 0x75, 0x6e, 0x63,
0x74, 0x69, 0x6f, 0x6e, 0x20, 0x66, 0x69, 0x62,
0x28, 0x6e, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x6e, 0x20,
0x3c, 0x3d, 0x20, 0x30, 0x29, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65,
0x74, 0x75, 0x72, 0x6e, 0x20, 0x31, 0x3b, 0x0a,
0x74, 0x75, 0x72, 0x6e, 0x20, 0x30, 0x3b, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x65, 0x6c, 0x73, 0x65,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20,
0x66, 0x69, 0x62, 0x28, 0x6e, 0x20, 0x2d, 0x20,
0x31, 0x29, 0x20, 0x2b, 0x20, 0x66, 0x69, 0x62,
0x28, 0x6e, 0x20, 0x2d, 0x20, 0x32, 0x29, 0x3b,
0x0a, 0x7d, 0x08, 0xe9, 0x05, 0xbe, 0x00, 0xe0,
0x29, 0x06, 0x2e, 0xb2, 0x03, 0x01, 0x01, 0x06,
0x01, 0x01, 0x00, 0x07, 0x14, 0x02, 0x00,
0x20, 0x69, 0x66, 0x20, 0x28, 0x6e, 0x20, 0x3d,
0x3d, 0x20, 0x31, 0x29, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74,
0x75, 0x72, 0x6e, 0x20, 0x31, 0x3b, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x66,
0x69, 0x62, 0x28, 0x6e, 0x20, 0x2d, 0x20, 0x31,
0x29, 0x20, 0x2b, 0x20, 0x66, 0x69, 0x62, 0x28,
0x6e, 0x20, 0x2d, 0x20, 0x32, 0x29, 0x3b, 0x0a,
0x7d, 0x08, 0xe9, 0x05, 0xbe, 0x00, 0xe0, 0x29,
0x06, 0x2e, 0xb2, 0x03, 0x01, 0x01, 0x06, 0x01,
0x01, 0x00, 0x07, 0x14, 0x02, 0x00,
};
const uint32_t qjsc_hello_module_size = 178;
const uint32_t qjsc_hello_module_size = 177;
const uint8_t qjsc_hello_module[178] = {
const uint8_t qjsc_hello_module[177] = {
0x0c, 0x07, 0x30, 0x65, 0x78, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x73, 0x2f, 0x68, 0x65, 0x6c, 0x6c,
0x6f, 0x5f, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65,
@ -60,18 +60,18 @@ const uint8_t qjsc_hello_module[178] = {
0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x10,
0x66, 0x69, 0x62, 0x28, 0x31, 0x30, 0x29, 0x3d,
0x0d, 0xb2, 0x03, 0x01, 0xb4, 0x03, 0x00, 0x00,
0x01, 0x00, 0xb6, 0x03, 0x00, 0x00, 0x0c, 0x20,
0xfa, 0x01, 0x9e, 0x01, 0x00, 0x00, 0x00, 0x05,
0x01, 0x00, 0x32, 0x00, 0xb6, 0x03, 0x00, 0x0c,
0x08, 0xe9, 0x02, 0x29, 0x38, 0xdc, 0x00, 0x00,
0x00, 0x42, 0xdd, 0x00, 0x00, 0x00, 0x04, 0xde,
0x00, 0x00, 0x00, 0x24, 0x01, 0x00, 0x0e, 0x38,
0xdc, 0x00, 0x00, 0x00, 0x42, 0xdd, 0x00, 0x00,
0x00, 0x04, 0xdf, 0x00, 0x00, 0x00, 0x65, 0x00,
0x00, 0xbb, 0x0a, 0xee, 0x24, 0x02, 0x00, 0x0e,
0x06, 0x2e, 0xb2, 0x03, 0x01, 0x01, 0x0a, 0x01,
0x01, 0x00, 0x04, 0x0a, 0x02, 0x62, 0x00, 0x4d,
0x30, 0x00,
0x01, 0x00, 0xb6, 0x03, 0x00, 0x0c, 0x20, 0xfa,
0x01, 0x9e, 0x01, 0x00, 0x00, 0x00, 0x05, 0x01,
0x00, 0x32, 0x00, 0xb6, 0x03, 0x00, 0x0c, 0x08,
0xe9, 0x02, 0x29, 0x38, 0xdc, 0x00, 0x00, 0x00,
0x42, 0xdd, 0x00, 0x00, 0x00, 0x04, 0xde, 0x00,
0x00, 0x00, 0x24, 0x01, 0x00, 0x0e, 0x38, 0xdc,
0x00, 0x00, 0x00, 0x42, 0xdd, 0x00, 0x00, 0x00,
0x04, 0xdf, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00,
0xbb, 0x0a, 0xee, 0x24, 0x02, 0x00, 0x0e, 0x06,
0x2e, 0xb2, 0x03, 0x01, 0x01, 0x0a, 0x01, 0x01,
0x00, 0x04, 0x0a, 0x02, 0x62, 0x00, 0x4d, 0x30,
0x00,
};
static JSContext *JS_NewCustomContext(JSRuntime *rt)

5118
gen/repl.c

File diff suppressed because it is too large Load diff

View file

@ -2,9 +2,9 @@
#include "quickjs-libc.h"
const uint32_t qjsc_test_fib_size = 167;
const uint32_t qjsc_test_fib_size = 166;
const uint8_t qjsc_test_fib[167] = {
const uint8_t qjsc_test_fib[166] = {
0x0c, 0x07, 0x28, 0x65, 0x78, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x73, 0x2f, 0x74, 0x65, 0x73, 0x74,
0x5f, 0x66, 0x69, 0x62, 0x2e, 0x6a, 0x73, 0x10,
@ -15,17 +15,17 @@ const uint8_t qjsc_test_fib[167] = {
0x6f, 0x72, 0x6c, 0x64, 0x10, 0x66, 0x69, 0x62,
0x28, 0x31, 0x30, 0x29, 0x3d, 0x0d, 0xb2, 0x03,
0x01, 0xb4, 0x03, 0x00, 0x00, 0x01, 0x00, 0xb6,
0x03, 0x00, 0x00, 0x0c, 0x20, 0xfa, 0x01, 0x9e,
0x01, 0x00, 0x00, 0x00, 0x05, 0x01, 0x00, 0x32,
0x00, 0xb6, 0x03, 0x00, 0x0c, 0x08, 0xe9, 0x02,
0x29, 0x38, 0xdc, 0x00, 0x00, 0x00, 0x42, 0xdd,
0x00, 0x00, 0x00, 0x04, 0xde, 0x00, 0x00, 0x00,
0x24, 0x01, 0x00, 0x0e, 0x38, 0xdc, 0x00, 0x00,
0x00, 0x42, 0xdd, 0x00, 0x00, 0x00, 0x04, 0xdf,
0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0xbb, 0x0a,
0xee, 0x24, 0x02, 0x00, 0x0e, 0x06, 0x2e, 0xb2,
0x03, 0x01, 0x01, 0x0a, 0x01, 0x01, 0x00, 0x04,
0x0a, 0x02, 0x62, 0x00, 0x4d, 0x30, 0x00,
0x03, 0x00, 0x0c, 0x20, 0xfa, 0x01, 0x9e, 0x01,
0x00, 0x00, 0x00, 0x05, 0x01, 0x00, 0x32, 0x00,
0xb6, 0x03, 0x00, 0x0c, 0x08, 0xe9, 0x02, 0x29,
0x38, 0xdc, 0x00, 0x00, 0x00, 0x42, 0xdd, 0x00,
0x00, 0x00, 0x04, 0xde, 0x00, 0x00, 0x00, 0x24,
0x01, 0x00, 0x0e, 0x38, 0xdc, 0x00, 0x00, 0x00,
0x42, 0xdd, 0x00, 0x00, 0x00, 0x04, 0xdf, 0x00,
0x00, 0x00, 0x65, 0x00, 0x00, 0xbb, 0x0a, 0xee,
0x24, 0x02, 0x00, 0x0e, 0x06, 0x2e, 0xb2, 0x03,
0x01, 0x01, 0x0a, 0x01, 0x01, 0x00, 0x04, 0x0a,
0x02, 0x62, 0x00, 0x4d, 0x30, 0x00,
};
static JSContext *JS_NewCustomContext(JSRuntime *rt)

View file

@ -53,7 +53,7 @@ int lre_exec(uint8_t **capture,
int lre_parse_escape(const uint8_t **pp, int allow_utf16);
LRE_BOOL lre_is_space(int c);
void lre_byte_swap(uint8_t *buf, size_t len, LRE_BOOL is_byte_swapped);
void lre_byte_swap(uint8_t *buf, size_t len, BOOL is_byte_swapped);
/* must be provided by the user */
LRE_BOOL lre_check_stack_overflow(void *opaque, size_t alloca_size);

View file

@ -24,7 +24,6 @@
#ifndef LIBUNICODE_H
#define LIBUNICODE_H
#include <stddef.h>
#include <inttypes.h>
#define LRE_BOOL int /* for documentation purposes */

1
qjs.c
View file

@ -58,7 +58,6 @@ static int eval_buf(JSContext *ctx, const void *buf, int buf_len,
js_module_set_import_meta(ctx, val, TRUE, TRUE);
val = JS_EvalFunction(ctx, val);
}
val = js_std_await(ctx, val);
} else {
val = JS_Eval(ctx, buf, buf_len, filename, eval_flags);
}

View file

@ -778,7 +778,6 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
JSValue ret;
JSValue options_obj;
BOOL backtrace_barrier = FALSE;
BOOL is_async = FALSE;
int flags;
if (argc >= 2) {
@ -786,9 +785,6 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
if (get_bool_option(ctx, &backtrace_barrier, options_obj,
"backtrace_barrier"))
return JS_EXCEPTION;
if (get_bool_option(ctx, &is_async, options_obj,
"async"))
return JS_EXCEPTION;
}
str = JS_ToCStringLen(ctx, &len, argv[0]);
@ -801,8 +797,6 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
flags = JS_EVAL_TYPE_GLOBAL;
if (backtrace_barrier)
flags |= JS_EVAL_FLAG_BACKTRACE_BARRIER;
if (is_async)
flags |= JS_EVAL_FLAG_ASYNC;
ret = JS_Eval(ctx, str, len, "<evalScript>", flags);
JS_FreeCString(ctx, str);
if (!ts->recv_pipe && --ts->eval_script_recurse == 0) {
@ -2120,38 +2114,6 @@ static JSClassDef js_os_timer_class = {
.gc_mark = js_os_timer_mark,
};
/* return a promise */
static JSValue js_os_sleepAsync(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
int64_t delay;
JSOSTimer *th;
JSValue promise, resolving_funcs[2];
if (JS_ToInt64(ctx, &delay, argv[0]))
return JS_EXCEPTION;
promise = JS_NewPromiseCapability(ctx, resolving_funcs);
if (JS_IsException(promise))
return JS_EXCEPTION;
th = js_mallocz(ctx, sizeof(*th));
if (!th) {
JS_FreeValue(ctx, promise);
JS_FreeValue(ctx, resolving_funcs[0]);
JS_FreeValue(ctx, resolving_funcs[1]);
return JS_EXCEPTION;
}
th->has_object = FALSE;
th->timeout = js__hrtime_ms() + delay;
th->func = JS_DupValue(ctx, resolving_funcs[0]);
list_add_tail(&th->link, &ts->os_timers);
JS_FreeValue(ctx, resolving_funcs[0]);
JS_FreeValue(ctx, resolving_funcs[1]);
return promise;
}
static void call_handler(JSContext *ctx, JSValue func)
{
JSValue ret, func1;
@ -3367,7 +3329,6 @@ static void *worker_func(void *opaque)
JSRuntime *rt;
JSThreadState *ts;
JSContext *ctx;
JSValue val;
rt = JS_NewRuntime();
if (rt == NULL) {
@ -3394,14 +3355,11 @@ static void *worker_func(void *opaque)
js_std_add_helpers(ctx, -1, NULL);
val = JS_LoadModule(ctx, args->basename, args->filename);
if (!JS_RunModule(ctx, args->basename, args->filename))
js_std_dump_error(ctx);
free(args->filename);
free(args->basename);
free(args);
val = js_std_await(ctx, val);
if (JS_IsException(val))
js_std_dump_error(ctx);
JS_FreeValue(ctx, val);
js_std_loop(ctx);
@ -3744,7 +3702,6 @@ static const JSCFunctionListEntry js_os_funcs[] = {
// per spec: both functions can cancel timeouts and intervals
JS_CFUNC_DEF("clearTimeout", 1, js_os_clearTimeout ),
JS_CFUNC_DEF("clearInterval", 1, js_os_clearTimeout ),
JS_CFUNC_DEF("sleepAsync", 1, js_os_sleepAsync ),
JS_PROP_STRING_DEF("platform", OS_PLATFORM, 0 ),
JS_CFUNC_DEF("getcwd", 0, js_os_getcwd ),
JS_CFUNC_DEF("chdir", 0, js_os_chdir ),
@ -4023,42 +3980,6 @@ void js_std_loop(JSContext *ctx)
}
}
/* Wait for a promise and execute pending jobs while waiting for
it. Return the promise result or JS_EXCEPTION in case of promise
rejection. */
JSValue js_std_await(JSContext *ctx, JSValue obj)
{
JSValue ret;
int state;
for(;;) {
state = JS_PromiseState(ctx, obj);
if (state == JS_PROMISE_FULFILLED) {
ret = JS_PromiseResult(ctx, obj);
JS_FreeValue(ctx, obj);
break;
} else if (state == JS_PROMISE_REJECTED) {
ret = JS_Throw(ctx, JS_PromiseResult(ctx, obj));
JS_FreeValue(ctx, obj);
break;
} else if (state == JS_PROMISE_PENDING) {
JSContext *ctx1;
int err;
err = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
if (err < 0) {
js_std_dump_error(ctx1);
}
if (os_poll_func)
os_poll_func(ctx);
} else {
/* not a promise */
ret = obj;
break;
}
}
return ret;
}
void js_std_eval_binary(JSContext *ctx, const uint8_t *buf, size_t buf_len,
int load_only)
{
@ -4077,11 +3998,8 @@ void js_std_eval_binary(JSContext *ctx, const uint8_t *buf, size_t buf_len,
goto exception;
}
js_module_set_import_meta(ctx, obj, FALSE, TRUE);
val = JS_EvalFunction(ctx, obj);
val = js_std_await(ctx, val);
} else {
val = JS_EvalFunction(ctx, obj);
}
val = JS_EvalFunction(ctx, obj);
if (JS_IsException(val)) {
exception:
js_std_dump_error(ctx);

View file

@ -37,7 +37,6 @@ JSModuleDef *js_init_module_std(JSContext *ctx, const char *module_name);
JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name);
void js_std_add_helpers(JSContext *ctx, int argc, char **argv);
void js_std_loop(JSContext *ctx);
JSValue js_std_await(JSContext *ctx, JSValue obj);
void js_std_init_handlers(JSRuntime *rt);
void js_std_free_handlers(JSRuntime *rt);
void js_std_dump_error(JSContext *ctx);

846
quickjs.c

File diff suppressed because it is too large Load diff

View file

@ -30,7 +30,6 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#ifdef __cplusplus
extern "C" {
@ -183,7 +182,7 @@ typedef struct JSValue {
#define JS_TAG_IS_FLOAT64(tag) ((unsigned)(tag) == JS_TAG_FLOAT64)
#define JS_NAN (JSValue){ (JSValueUnion){ .float64 = JS_FLOAT64_NAN }, JS_TAG_FLOAT64 }
#define JS_NAN (JSValue){ .u.float64 = JS_FLOAT64_NAN, JS_TAG_FLOAT64 }
static inline JSValue __JS_NewFloat64(double d)
{
@ -275,9 +274,6 @@ static inline JS_BOOL JS_VALUE_IS_NAN(JSValue v)
#define JS_EVAL_FLAG_COMPILE_ONLY (1 << 5)
/* don't include the stack frames before this eval in the Error() backtraces */
#define JS_EVAL_FLAG_BACKTRACE_BARRIER (1 << 6)
/* allow top-level await in normal script. JS_Eval() returns a
promise. Only allowed with JS_EVAL_TYPE_GLOBAL */
#define JS_EVAL_FLAG_ASYNC (1 << 7)
typedef JSValue JSCFunction(JSContext *ctx, JSValue this_val, int argc, JSValue *argv);
typedef JSValue JSCFunctionMagic(JSContext *ctx, JSValue this_val, int argc, JSValue *argv, int magic);
@ -306,6 +302,7 @@ JS_EXTERN void JS_SetRuntimeInfo(JSRuntime *rt, const char *info);
JS_EXTERN void JS_SetMemoryLimit(JSRuntime *rt, size_t limit);
JS_EXTERN void JS_SetDumpFlags(JSRuntime *rt, uint64_t flags);
JS_EXTERN size_t JS_GetGCThreshold(JSRuntime *rt);
/* use 0 to disable automatic GC */
JS_EXTERN void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold);
/* use 0 to disable maximum stack size check */
JS_EXTERN void JS_SetMaxStackSize(JSRuntime *rt, size_t stack_size);
@ -643,7 +640,6 @@ JS_EXTERN int JS_ToIndex(JSContext *ctx, uint64_t *plen, JSValue val);
JS_EXTERN int JS_ToFloat64(JSContext *ctx, double *pres, JSValue val);
/* return an exception if 'val' is a Number */
JS_EXTERN int JS_ToBigInt64(JSContext *ctx, int64_t *pres, JSValue val);
JS_EXTERN int JS_ToBigUint64(JSContext *ctx, uint64_t *pres, JSValue val);
/* same as JS_ToInt64() but allow BigInt */
JS_EXTERN int JS_ToInt64Ext(JSContext *ctx, int64_t *pres, JSValue val);
@ -786,15 +782,7 @@ typedef struct {
} JSSharedArrayBufferFunctions;
JS_EXTERN void JS_SetSharedArrayBufferFunctions(JSRuntime *rt, const JSSharedArrayBufferFunctions *sf);
typedef enum JSPromiseStateEnum {
JS_PROMISE_PENDING,
JS_PROMISE_FULFILLED,
JS_PROMISE_REJECTED,
} JSPromiseStateEnum;
JS_EXTERN JSValue JS_NewPromiseCapability(JSContext *ctx, JSValue *resolving_funcs);
JS_EXTERN JSPromiseStateEnum JS_PromiseState(JSContext *ctx, JSValue promise);
JS_EXTERN JSValue JS_PromiseResult(JSContext *ctx, JSValue promise);
JS_EXTERN JSValue JS_NewSymbol(JSContext *ctx, const char *description, JS_BOOL is_global);
@ -866,7 +854,7 @@ JS_EXTERN int JS_ResolveModule(JSContext *ctx, JSValue obj);
/* only exported for os.Worker() */
JS_EXTERN JSAtom JS_GetScriptOrModuleName(JSContext *ctx, int n_stack_levels);
/* only exported for os.Worker() */
JS_EXTERN JSValue JS_LoadModule(JSContext *ctx, const char *basename,
JS_EXTERN JSModuleDef *JS_RunModule(JSContext *ctx, const char *basename,
const char *filename);
/* C function definition */
@ -970,21 +958,21 @@ typedef struct JSCFunctionListEntry {
#define JS_DEF_ALIAS 9
/* Note: c++ does not like nested designators */
#define JS_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define JS_CFUNC_DEF2(name, length, func1, prop_flags) { name, prop_flags, JS_DEF_CFUNC, 0, { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define JS_CFUNC_MAGIC_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, { .func = { length, JS_CFUNC_generic_magic, { .generic_magic = func1 } } } }
#define JS_CFUNC_SPECIAL_DEF(name, length, cproto, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, { .func = { length, JS_CFUNC_ ## cproto, { .cproto = func1 } } } }
#define JS_ITERATOR_NEXT_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, { .func = { length, JS_CFUNC_iterator_next, { .iterator_next = func1 } } } }
#define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
#define JS_CGETSET_MAGIC_DEF(name, fgetter, fsetter, magic) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, magic, { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } }
#define JS_PROP_STRING_DEF(name, cstr, prop_flags) { name, prop_flags, JS_DEF_PROP_STRING, 0, { .str = cstr } }
#define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, { .i32 = val } }
#define JS_PROP_INT64_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT64, 0, { .i64 = val } }
#define JS_PROP_DOUBLE_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, { .f64 = val } }
#define JS_PROP_UNDEFINED_DEF(name, prop_flags) { name, prop_flags, JS_DEF_PROP_UNDEFINED, 0, { .i32 = 0 } }
#define JS_OBJECT_DEF(name, tab, len, prop_flags) { name, prop_flags, JS_DEF_OBJECT, 0, { .prop_list = { tab, len } } }
#define JS_ALIAS_DEF(name, from) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, -1 } } }
#define JS_ALIAS_BASE_DEF(name, from, base) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, base } } }
#define JS_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define JS_CFUNC_DEF2(name, length, func1, prop_flags) { name, prop_flags, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define JS_CFUNC_MAGIC_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_generic_magic, { .generic_magic = func1 } } } }
#define JS_CFUNC_SPECIAL_DEF(name, length, cproto, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_ ## cproto, { .cproto = func1 } } } }
#define JS_ITERATOR_NEXT_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_iterator_next, { .iterator_next = func1 } } } }
#define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
#define JS_CGETSET_MAGIC_DEF(name, fgetter, fsetter, magic) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, magic, .u = { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } }
#define JS_PROP_STRING_DEF(name, cstr, prop_flags) { name, prop_flags, JS_DEF_PROP_STRING, 0, .u = { .str = cstr } }
#define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, .u = { .i32 = val } }
#define JS_PROP_INT64_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT64, 0, .u = { .i64 = val } }
#define JS_PROP_DOUBLE_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, .u = { .f64 = val } }
#define JS_PROP_UNDEFINED_DEF(name, prop_flags) { name, prop_flags, JS_DEF_PROP_UNDEFINED, 0, .u = { .i32 = 0 } }
#define JS_OBJECT_DEF(name, tab, len, prop_flags) { name, prop_flags, JS_DEF_OBJECT, 0, .u = { .prop_list = { tab, len } } }
#define JS_ALIAS_DEF(name, from) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, .u = { .alias = { from, -1 } } }
#define JS_ALIAS_BASE_DEF(name, from, base) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, .u = { .alias = { from, base } } }
JS_EXTERN void JS_SetPropertyFunctionList(JSContext *ctx, JSValue obj,
const JSCFunctionListEntry *tab,
@ -1006,10 +994,25 @@ JS_EXTERN int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *exp
JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
const JSCFunctionListEntry *tab, int len);
/* Promise */
#define JS_INVALID_PROMISE_STATE (-1)
typedef enum JSPromiseStateEnum {
JS_PROMISE_PENDING,
JS_PROMISE_FULFILLED,
JS_PROMISE_REJECTED,
} JSPromiseStateEnum;
/* Returns JSPromiseReactionEnum for the promise or JS_INVALID_PROMISE_STATE if the value is not a promise. */
JS_EXTERN JSPromiseStateEnum JS_PromiseState(JSContext *ctx, JSValue promise);
/* Return the result of the promise if the promise's state is in the FULFILLED or REJECTED state. Otherwise returns JS_UNDEFINED. */
JS_EXTERN JSValue JS_PromiseResult(JSContext *ctx, JSValue promise);
/* Version */
#define QJS_VERSION_MAJOR 0
#define QJS_VERSION_MINOR 6
#define QJS_VERSION_MINOR 5
#define QJS_VERSION_PATCH 0
#define QJS_VERSION_SUFFIX "dev"

112
repl.js
View file

@ -129,7 +129,6 @@ import * as os from "os";
var plen = 0;
var ps1 = "qjs > ";
var ps2 = " ... ";
var eval_start_time;
var eval_time = 0;
var mexpr = "";
var level = 0;
@ -839,8 +838,10 @@ import * as os from "os";
prompt += ps2;
} else {
if (show_time) {
var t = eval_time / 1000;
prompt += t.toFixed(6) + " ";
var t = Math.round(eval_time) + " ";
eval_time = 0;
t = dupstr("0", 5 - t.length) + t;
prompt += t.substring(0, t.length - 4) + "." + t.substring(t.length - 4);
}
plen = prompt.length;
prompt += ps1;
@ -1506,76 +1507,20 @@ import * as os from "os";
"quit": () => { exit(0); },
}, null);
function cmd_start() {
std.puts('QuickJS-ng - Type ".help" for help\n');
cmd_readline_start();
}
function cmd_readline_start() {
readline_start(dupstr(" ", level), readline_handle_cmd);
}
function readline_handle_cmd(expr) {
if (!handle_cmd(expr)) {
cmd_readline_start();
}
}
/* return true if async termination */
function handle_cmd(expr) {
if (!expr)
return false;
if (mexpr) {
expr = mexpr + '\n' + expr;
} else {
if (handle_directive(expr))
return false;
}
var colorstate = colorize_js(expr);
pstate = colorstate[0];
level = colorstate[1];
if (pstate) {
mexpr = expr;
return false;
}
mexpr = "";
eval_and_print_start(expr, true);
return true;
}
function eval_and_print_start(expr, is_async) {
function eval_and_print(expr) {
var result;
try {
if (use_strict)
expr = '"use strict"; void 0;' + expr;
eval_start_time = os.now();
var now = Date.now();
/* eval as a script */
result = std.evalScript(expr, { backtrace_barrier: true, async: is_async });
if (is_async) {
/* result is a promise */
result.then(print_eval_result, print_eval_error);
} else {
print_eval_result({ value: result });
}
} catch (error) {
print_eval_error(error);
}
}
function print_eval_result(result) {
result = result.value;
eval_time = os.now() - eval_start_time;
result = std.evalScript(expr, { backtrace_barrier: true });
eval_time = Date.now() - now;
print(result);
/* set the last result */
g._ = result;
handle_cmd_end();
}
function print_eval_error(error) {
} catch (error) {
std.puts(colors[styles.error]);
if (error instanceof Error) {
console.log(error);
@ -1587,17 +1532,46 @@ import * as os from "os";
console.log(error);
}
std.puts(colors.none);
handle_cmd_end();
}
}
function handle_cmd_end() {
function cmd_start() {
std.puts('QuickJS-ng - Type ".help" for help\n');
cmd_readline_start();
}
function cmd_readline_start() {
readline_start(dupstr(" ", level), readline_handle_cmd);
}
function readline_handle_cmd(expr) {
handle_cmd(expr);
cmd_readline_start();
}
function handle_cmd(expr) {
if (!expr)
return;
if (mexpr) {
expr = mexpr + '\n' + expr;
} else {
if (handle_directive(expr))
return;
}
var colorstate = colorize_js(expr);
pstate = colorstate[0];
level = colorstate[1];
if (pstate) {
mexpr = expr;
return;
}
mexpr = "";
eval_and_print(expr);
level = 0;
/* run the garbage collector after each command */
std.gc();
cmd_readline_start();
}
function colorize_js(str) {

View file

@ -1198,7 +1198,7 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
{
JSValue res_val, exception_val;
int ret, error_line, pos, pos_line;
BOOL is_error, has_error_line, ret_promise;
BOOL is_error, has_error_line;
const char *error_name;
pos = skip_comments(buf, 1, &pos_line);
@ -1207,19 +1207,12 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
exception_val = JS_UNDEFINED;
error_name = NULL;
/* a module evaluation returns a promise */
ret_promise = ((eval_flags & JS_EVAL_TYPE_MODULE) != 0);
async_done = 0; /* counter of "Test262:AsyncTestComplete" messages */
res_val = JS_Eval(ctx, buf, buf_len, filename, eval_flags);
if ((is_async || ret_promise) && !JS_IsException(res_val)) {
JSValue promise = JS_UNDEFINED;
if (ret_promise) {
promise = res_val;
} else {
if (is_async && !JS_IsException(res_val)) {
JS_FreeValue(ctx, res_val);
}
for(;;) {
JSContext *ctx1;
ret = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
@ -1227,23 +1220,34 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
res_val = JS_EXCEPTION;
break;
} else if (ret == 0) {
if (is_async) {
/* test if the test called $DONE() once */
if (async_done != 1) {
res_val = JS_ThrowTypeError(ctx, "$DONE() not called");
} else {
res_val = JS_UNDEFINED;
}
} else {
/* check that the returned promise is fulfilled */
JSPromiseStateEnum state = JS_PromiseState(ctx, promise);
if (state == JS_PROMISE_FULFILLED)
break;
}
}
} else if ((eval_flags & JS_EVAL_TYPE_MODULE) &&
!JS_IsUndefined(res_val) &&
!JS_IsException(res_val)) {
JSValue promise = res_val;
for(;;) {
JSContext *ctx1;
ret = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
if (ret < 0) {
res_val = JS_EXCEPTION;
break;
}
if (ret == 0) {
JSPromiseStateEnum s = JS_PromiseState(ctx, promise);
if (s == JS_PROMISE_FULFILLED)
res_val = JS_UNDEFINED;
else if (state == JS_PROMISE_REJECTED)
else if (s == JS_PROMISE_REJECTED)
res_val = JS_Throw(ctx, JS_PromiseResult(ctx, promise));
else
res_val = JS_ThrowTypeError(ctx, "promise is pending");
}
res_val = JS_EXCEPTION;
break;
}
}
@ -1882,13 +1886,8 @@ int run_test262_harness_test(const char *filename, BOOL is_module)
if (JS_IsException(res_val)) {
js_std_dump_error(ctx);
ret_code = 1;
} else {
JSValue promise = JS_UNDEFINED;
if (is_module) {
promise = res_val;
} else {
JS_FreeValue(ctx, res_val);
}
for(;;) {
JSContext *ctx1;
ret = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
@ -1899,16 +1898,6 @@ int run_test262_harness_test(const char *filename, BOOL is_module)
break;
}
}
/* dump the error if the module returned an error. */
if (is_module) {
JSPromiseStateEnum state = JS_PromiseState(ctx, promise);
if (state == JS_PROMISE_REJECTED) {
JS_Throw(ctx, JS_PromiseResult(ctx, promise));
js_std_dump_error(ctx);
ret_code = 1;
}
}
JS_FreeValue(ctx, promise);
}
free(buf);
#ifdef CONFIG_AGENT

View file

@ -94,20 +94,14 @@ static inline int clz32(unsigned int a)
static inline int clz64(uint64_t a)
{
#if defined(_MSC_VER) && !defined(__clang__)
#if INTPTR_MAX == INT64_MAX
unsigned long index;
_BitScanReverse64(&index, a);
return 63 - index;
#else
if (a >> 32)
return clz32((unsigned)(a >> 32));
else
return clz32((unsigned)a) + 32;
#endif
#else
return __builtin_clzll(a);
#endif
}
// prototypes for final functions
extern char const digits36[36];
size_t u32toa(char buf[minimum_length(11)], uint32_t n);