forked from cory/tildefriends
quickjs-2024-01-13.tar.xz
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4765 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
74
deps/quickjs/quickjs-libc.c
vendored
74
deps/quickjs/quickjs-libc.c
vendored
@@ -751,6 +751,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValueConst this_val,
|
||||
JSValue ret;
|
||||
JSValueConst options_obj;
|
||||
BOOL backtrace_barrier = FALSE;
|
||||
BOOL is_async = FALSE;
|
||||
int flags;
|
||||
|
||||
if (argc >= 2) {
|
||||
@@ -758,6 +759,9 @@ static JSValue js_evalScript(JSContext *ctx, JSValueConst 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]);
|
||||
@@ -770,6 +774,8 @@ static JSValue js_evalScript(JSContext *ctx, JSValueConst 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) {
|
||||
@@ -1970,6 +1976,13 @@ static int64_t get_time_ms(void)
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
|
||||
}
|
||||
|
||||
static int64_t get_time_ns(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
|
||||
}
|
||||
#else
|
||||
/* more portable, but does not work if the date is updated */
|
||||
static int64_t get_time_ms(void)
|
||||
@@ -1978,8 +1991,21 @@ static int64_t get_time_ms(void)
|
||||
gettimeofday(&tv, NULL);
|
||||
return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
|
||||
}
|
||||
|
||||
static int64_t get_time_ns(void)
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
return (int64_t)tv.tv_sec * 1000000000 + (tv.tv_usec * 1000);
|
||||
}
|
||||
#endif
|
||||
|
||||
static JSValue js_os_now(JSContext *ctx, JSValue this_val,
|
||||
int argc, JSValue *argv)
|
||||
{
|
||||
return JS_NewFloat64(ctx, (double)get_time_ns() / 1e6);
|
||||
}
|
||||
|
||||
static void unlink_timer(JSRuntime *rt, JSOSTimer *th)
|
||||
{
|
||||
if (th->link.prev) {
|
||||
@@ -2062,6 +2088,38 @@ 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 = get_time_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, JSValueConst func)
|
||||
{
|
||||
JSValue ret, func1;
|
||||
@@ -3030,6 +3088,13 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* getpid() -> pid */
|
||||
static JSValue js_os_getpid(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
return JS_NewInt32(ctx, getpid());
|
||||
}
|
||||
|
||||
/* waitpid(pid, block) -> [pid, status] */
|
||||
static JSValue js_os_waitpid(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
@@ -3274,6 +3339,7 @@ static void *worker_func(void *opaque)
|
||||
JSRuntime *rt;
|
||||
JSThreadState *ts;
|
||||
JSContext *ctx;
|
||||
JSValue promise;
|
||||
|
||||
rt = JS_NewRuntime();
|
||||
if (rt == NULL) {
|
||||
@@ -3300,8 +3366,11 @@ static void *worker_func(void *opaque)
|
||||
|
||||
js_std_add_helpers(ctx, -1, NULL);
|
||||
|
||||
if (!JS_RunModule(ctx, args->basename, args->filename))
|
||||
promise = JS_LoadModule(ctx, args->basename, args->filename);
|
||||
if (JS_IsException(promise))
|
||||
js_std_dump_error(ctx);
|
||||
/* XXX: check */
|
||||
JS_FreeValue(ctx, promise);
|
||||
free(args->filename);
|
||||
free(args->basename);
|
||||
free(args);
|
||||
@@ -3621,8 +3690,10 @@ static const JSCFunctionListEntry js_os_funcs[] = {
|
||||
OS_FLAG(SIGTTIN),
|
||||
OS_FLAG(SIGTTOU),
|
||||
#endif
|
||||
JS_CFUNC_DEF("now", 0, js_os_now ),
|
||||
JS_CFUNC_DEF("setTimeout", 2, js_os_setTimeout ),
|
||||
JS_CFUNC_DEF("clearTimeout", 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 ),
|
||||
@@ -3650,6 +3721,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
|
||||
JS_CFUNC_DEF("symlink", 2, js_os_symlink ),
|
||||
JS_CFUNC_DEF("readlink", 1, js_os_readlink ),
|
||||
JS_CFUNC_DEF("exec", 1, js_os_exec ),
|
||||
JS_CFUNC_DEF("getpid", 0, js_os_getpid ),
|
||||
JS_CFUNC_DEF("waitpid", 2, js_os_waitpid ),
|
||||
OS_FLAG(WNOHANG),
|
||||
JS_CFUNC_DEF("pipe", 0, js_os_pipe ),
|
||||
|
Reference in New Issue
Block a user