summaryrefslogtreecommitdiff
path: root/quickjs-libc.c
diff options
context:
space:
mode:
Diffstat (limited to 'quickjs-libc.c')
-rw-r--r--quickjs-libc.c208
1 files changed, 104 insertions, 104 deletions
diff --git a/quickjs-libc.c b/quickjs-libc.c
index 6d50a90..47b5301 100644
--- a/quickjs-libc.c
+++ b/quickjs-libc.c
@@ -1,6 +1,6 @@
/*
* QuickJS C library
- *
+ *
* Copyright (c) 2017-2021 Fabrice Bellard
* Copyright (c) 2017-2021 Charlie Gordon
*
@@ -181,7 +181,7 @@ static JSValue js_printf_internal(JSContext *ctx,
break;
q = fmtbuf;
*q++ = *fmt++; /* copy '%' */
-
+
/* flags */
for(;;) {
c = *fmt;
@@ -235,14 +235,14 @@ static JSValue js_printf_internal(JSContext *ctx,
if (*fmt == 'l') {
mod = *fmt++;
}
-
+
/* type */
c = *fmt++;
if (q >= fmtbuf + sizeof(fmtbuf) - 1)
goto invalid;
*q++ = c;
*q = '\0';
-
+
switch (c) {
case 'c':
if (i >= argc)
@@ -264,7 +264,7 @@ static JSValue js_printf_internal(JSContext *ctx,
len = unicode_to_utf8(cbuf, int32_arg);
dbuf_put(&dbuf, cbuf, len);
break;
-
+
case 'd':
case 'i':
case 'o':
@@ -309,7 +309,7 @@ static JSValue js_printf_internal(JSContext *ctx,
dbuf_printf_fun(&dbuf, fmtbuf, string_arg);
JS_FreeCString(ctx, string_arg);
break;
-
+
case 'e':
case 'f':
case 'g':
@@ -324,11 +324,11 @@ static JSValue js_printf_internal(JSContext *ctx,
goto fail;
dbuf_printf_fun(&dbuf, fmtbuf, double_arg);
break;
-
+
case '%':
dbuf_putc(&dbuf, '%');
break;
-
+
default:
/* XXX: should support an extension mechanism */
invalid:
@@ -365,7 +365,7 @@ uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
uint8_t *buf;
size_t buf_len;
long lret;
-
+
f = fopen(filename, "rb");
if (!f)
return NULL;
@@ -412,7 +412,7 @@ static JSValue js_loadScript(JSContext *ctx, JSValueConst this_val,
const char *filename;
JSValue ret;
size_t buf_len;
-
+
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
return JS_EXCEPTION;
@@ -437,7 +437,7 @@ static JSValue js_std_loadFile(JSContext *ctx, JSValueConst this_val,
const char *filename;
JSValue ret;
size_t buf_len;
-
+
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
return JS_EXCEPTION;
@@ -469,7 +469,7 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx,
void *hd;
JSInitModuleFunc *init;
char *filename;
-
+
if (!strchr(module_name, '/')) {
/* must add a '/' so that the DLL is not searched in the
system library paths */
@@ -481,7 +481,7 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx,
} else {
filename = (char *)module_name;
}
-
+
/* C module */
hd = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
if (filename != module_name)
@@ -520,7 +520,7 @@ int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val,
JSValue meta_obj;
JSAtom module_name_atom;
const char *module_name;
-
+
assert(JS_VALUE_GET_TAG(func_val) == JS_TAG_MODULE);
m = JS_VALUE_GET_PTR(func_val);
@@ -551,7 +551,7 @@ int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val,
pstrcpy(buf, sizeof(buf), module_name);
}
JS_FreeCString(ctx, module_name);
-
+
meta_obj = JS_GetImportMeta(ctx, m);
if (JS_IsException(meta_obj))
return -1;
@@ -576,14 +576,14 @@ JSModuleDef *js_module_loader(JSContext *ctx,
size_t buf_len;
uint8_t *buf;
JSValue func_val;
-
+
buf = js_load_file(ctx, &buf_len, module_name);
if (!buf) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
module_name);
return NULL;
}
-
+
/* compile the module */
func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
@@ -754,7 +754,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValueConst this_val,
BOOL backtrace_barrier = FALSE;
BOOL is_async = FALSE;
int flags;
-
+
if (argc >= 2) {
options_obj = argv[1];
if (get_bool_option(ctx, &backtrace_barrier, options_obj,
@@ -772,7 +772,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValueConst this_val,
/* install the interrupt handler */
JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL);
}
- flags = JS_EVAL_TYPE_GLOBAL;
+ flags = JS_EVAL_TYPE_GLOBAL;
if (backtrace_barrier)
flags |= JS_EVAL_FLAG_BACKTRACE_BARRIER;
if (is_async)
@@ -878,7 +878,7 @@ static JSValue js_std_open(JSContext *ctx, JSValueConst this_val,
const char *filename, *mode = NULL;
FILE *f;
int err;
-
+
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
goto fail;
@@ -914,7 +914,7 @@ static JSValue js_std_popen(JSContext *ctx, JSValueConst this_val,
const char *filename, *mode = NULL;
FILE *f;
int err;
-
+
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
goto fail;
@@ -1028,7 +1028,7 @@ static JSValue js_std_file_puts(JSContext *ctx, JSValueConst this_val,
if (!f)
return JS_EXCEPTION;
}
-
+
for(i = 0; i < argc; i++) {
str = JS_ToCStringLen(ctx, &len, argv[i]);
if (!str)
@@ -1159,7 +1159,7 @@ static JSValue js_std_file_read_write(JSContext *ctx, JSValueConst this_val,
uint64_t pos, len;
size_t size, ret;
uint8_t *buf;
-
+
if (!f)
return JS_EXCEPTION;
if (JS_ToIndex(ctx, &pos, argv[1]))
@@ -1186,7 +1186,7 @@ static JSValue js_std_file_getline(JSContext *ctx, JSValueConst this_val,
int c;
DynBuf dbuf;
JSValue obj;
-
+
if (!f)
return JS_EXCEPTION;
@@ -1225,7 +1225,7 @@ static JSValue js_std_file_readAsString(JSContext *ctx, JSValueConst this_val,
uint64_t max_size64;
size_t max_size;
JSValueConst max_size_val;
-
+
if (!f)
return JS_EXCEPTION;
@@ -1289,7 +1289,7 @@ static int http_get_header_line(FILE *f, char *buf, size_t buf_size,
{
int c;
char *p;
-
+
p = buf;
for(;;) {
c = fgetc(f);
@@ -1325,21 +1325,21 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
DynBuf cmd_buf;
DynBuf data_buf_s, *data_buf = &data_buf_s;
DynBuf header_buf_s, *header_buf = &header_buf_s;
- char *buf;
+ char *buf;
size_t i, len;
int c, status;
JSValue response = JS_UNDEFINED, ret_obj;
JSValueConst options_obj;
FILE *f;
BOOL binary_flag, full_flag;
-
+
url = JS_ToCString(ctx, argv[0]);
if (!url)
return JS_EXCEPTION;
-
+
binary_flag = FALSE;
full_flag = FALSE;
-
+
if (argc >= 2) {
options_obj = argv[1];
@@ -1352,7 +1352,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
return JS_EXCEPTION;
}
}
-
+
js_std_dbuf_init(ctx, &cmd_buf);
dbuf_printf(&cmd_buf, "%s ''", URL_GET_PROGRAM);
len = strlen(url);
@@ -1378,7 +1378,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
js_std_dbuf_init(ctx, data_buf);
js_std_dbuf_init(ctx, header_buf);
-
+
buf = js_malloc(ctx, URL_GET_BUF_SIZE);
if (!buf)
goto fail;
@@ -1392,7 +1392,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
if (!full_flag && !(status >= 200 && status <= 299)) {
goto bad_header;
}
-
+
/* wait until there is an empty line */
for(;;) {
if (http_get_header_line(f, buf, URL_GET_BUF_SIZE, header_buf) < 0) {
@@ -1468,7 +1468,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
static JSClassDef js_std_file_class = {
"FILE",
.finalizer = js_std_file_finalizer,
-};
+};
static const JSCFunctionListEntry js_std_error_props[] = {
/* various errno values */
@@ -1500,7 +1500,7 @@ static const JSCFunctionListEntry js_std_funcs[] = {
JS_CFUNC_DEF("loadFile", 1, js_std_loadFile ),
JS_CFUNC_DEF("strerror", 1, js_std_strerror ),
JS_CFUNC_DEF("parseExtJSON", 1, js_std_parseExtJSON ),
-
+
/* FILE I/O */
JS_CFUNC_DEF("open", 2, js_std_open ),
JS_CFUNC_DEF("popen", 2, js_std_popen ),
@@ -1514,7 +1514,7 @@ static const JSCFunctionListEntry js_std_funcs[] = {
JS_PROP_INT32_DEF("SEEK_END", SEEK_END, JS_PROP_CONFIGURABLE ),
JS_OBJECT_DEF("Error", js_std_error_props, countof(js_std_error_props), JS_PROP_CONFIGURABLE),
};
-
+
static const JSCFunctionListEntry js_std_file_proto_funcs[] = {
JS_CFUNC_DEF("close", 0, js_std_file_close ),
JS_CFUNC_MAGIC_DEF("puts", 1, js_std_file_puts, 1 ),
@@ -1539,7 +1539,7 @@ static const JSCFunctionListEntry js_std_file_proto_funcs[] = {
static int js_std_init(JSContext *ctx, JSModuleDef *m)
{
JSValue proto;
-
+
/* FILE class */
/* the class ID is created once */
JS_NewClassID(&js_std_file_class_id);
@@ -1620,7 +1620,7 @@ static JSValue js_os_seek(JSContext *ctx, JSValueConst this_val,
int fd, whence;
int64_t pos, ret;
BOOL is_bigint;
-
+
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
is_bigint = JS_IsBigInt(ctx, argv[1]);
@@ -1645,7 +1645,7 @@ static JSValue js_os_read_write(JSContext *ctx, JSValueConst this_val,
size_t size;
ssize_t ret;
uint8_t *buf;
-
+
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
if (JS_ToIndex(ctx, &pos, argv[2]))
@@ -1685,7 +1685,7 @@ static JSValue js_os_ttyGetWinSize(JSContext *ctx, JSValueConst this_val,
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
handle = (HANDLE)_get_osfhandle(fd);
-
+
if (!GetConsoleScreenBufferInfo(handle, &info))
return JS_NULL;
obj = JS_NewArray(ctx);
@@ -1724,7 +1724,7 @@ static JSValue js_os_ttyGetWinSize(JSContext *ctx, JSValueConst this_val,
int fd;
struct winsize ws;
JSValue obj;
-
+
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
if (ioctl(fd, TIOCGWINSZ, &ws) == 0 &&
@@ -1753,10 +1753,10 @@ static JSValue js_os_ttySetRaw(JSContext *ctx, JSValueConst this_val,
{
struct termios tty;
int fd;
-
+
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
-
+
memset(&tty, 0, sizeof(tty));
tcgetattr(fd, &tty);
oldtty = tty;
@@ -1783,7 +1783,7 @@ static JSValue js_os_remove(JSContext *ctx, JSValueConst this_val,
{
const char *filename;
int ret;
-
+
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
return JS_EXCEPTION;
@@ -1809,7 +1809,7 @@ static JSValue js_os_rename(JSContext *ctx, JSValueConst this_val,
{
const char *oldpath, *newpath;
int ret;
-
+
oldpath = JS_ToCString(ctx, argv[0]);
if (!oldpath)
return JS_EXCEPTION;
@@ -1861,7 +1861,7 @@ static JSValue js_os_setReadHandler(JSContext *ctx, JSValueConst this_val,
JSOSRWHandler *rh;
int fd;
JSValueConst func;
-
+
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
func = argv[1];
@@ -1935,7 +1935,7 @@ static JSValue js_os_signal(JSContext *ctx, JSValueConst this_val,
if (!is_main_thread(rt))
return JS_ThrowTypeError(ctx, "signal handler can only be set in the main thread");
-
+
if (JS_ToUint32(ctx, &sig_num, argv[0]))
return JS_EXCEPTION;
if (sig_num >= 64)
@@ -2062,7 +2062,7 @@ static JSValue js_os_clearTimeout(JSContext *ctx, JSValueConst this_val,
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
JSOSTimer *th;
int timer_id;
-
+
if (JS_ToInt32(ctx, &timer_id, argv[0]))
return JS_EXCEPTION;
th = find_timer_by_id(ts, timer_id);
@@ -2127,12 +2127,12 @@ static int js_os_poll(JSContext *ctx)
int64_t cur_time, delay;
JSOSRWHandler *rh;
struct list_head *el;
-
+
/* XXX: handle signals if useful */
if (list_empty(&ts->os_rw_handlers) && list_empty(&ts->os_timers))
return -1; /* no more events */
-
+
/* XXX: only timers and basic console input are supported */
if (!list_empty(&ts->os_timers)) {
cur_time = get_time_ms();
@@ -2205,7 +2205,7 @@ static int handle_posted_message(JSRuntime *rt, JSContext *ctx,
struct list_head *el;
JSWorkerMessage *msg;
JSValue obj, data_obj, func, retval;
-
+
pthread_mutex_lock(&ps->mutex);
if (!list_empty(&ps->msg_queue)) {
el = ps->msg_queue.next;
@@ -2232,7 +2232,7 @@ static int handle_posted_message(JSRuntime *rt, JSContext *ctx,
JS_READ_OBJ_SAB | JS_READ_OBJ_REFERENCE);
js_free_message(msg);
-
+
if (JS_IsException(data_obj))
goto fail;
obj = JS_NewObject(ctx);
@@ -2285,7 +2285,7 @@ static int js_os_poll(JSContext *ctx)
unlikely(os_pending_signals != 0)) {
JSOSSignalHandler *sh;
uint64_t mask;
-
+
list_for_each(el, &ts->os_signal_handlers) {
sh = list_entry(el, JSOSSignalHandler, link);
mask = (uint64_t)1 << sh->sig_num;
@@ -2300,7 +2300,7 @@ static int js_os_poll(JSContext *ctx)
if (list_empty(&ts->os_rw_handlers) && list_empty(&ts->os_timers) &&
list_empty(&ts->port_list))
return -1; /* no more events */
-
+
if (!list_empty(&ts->os_timers)) {
cur_time = get_time_ms();
min_delay = 10000;
@@ -2326,7 +2326,7 @@ static int js_os_poll(JSContext *ctx)
} else {
tvp = NULL;
}
-
+
FD_ZERO(&rfds);
FD_ZERO(&wfds);
fd_max = -1;
@@ -2412,7 +2412,7 @@ static JSValue js_os_getcwd(JSContext *ctx, JSValueConst this_val,
{
char buf[PATH_MAX];
int err;
-
+
if (!getcwd(buf, sizeof(buf))) {
buf[0] = '\0';
err = errno;
@@ -2441,7 +2441,7 @@ static JSValue js_os_mkdir(JSContext *ctx, JSValueConst this_val,
{
int mode, ret;
const char *path;
-
+
if (argc >= 2) {
if (JS_ToInt32(ctx, &mode, argv[1]))
return JS_EXCEPTION;
@@ -2471,7 +2471,7 @@ static JSValue js_os_readdir(JSContext *ctx, JSValueConst this_val,
JSValue obj;
int err;
uint32_t len;
-
+
path = JS_ToCString(ctx, argv[0]);
if (!path)
return JS_EXCEPTION;
@@ -2619,7 +2619,7 @@ static JSValue js_os_utimes(JSContext *ctx, JSValueConst this_val,
const char *path;
int64_t atime, mtime;
int ret;
-
+
if (JS_ToInt64(ctx, &atime, argv[1]))
return JS_EXCEPTION;
if (JS_ToInt64(ctx, &mtime, argv[2]))
@@ -2652,7 +2652,7 @@ static JSValue js_os_sleep(JSContext *ctx, JSValueConst this_val,
{
int64_t delay;
int ret;
-
+
if (JS_ToInt64(ctx, &delay, argv[0]))
return JS_EXCEPTION;
if (delay < 0)
@@ -2716,7 +2716,7 @@ static JSValue js_os_symlink(JSContext *ctx, JSValueConst this_val,
{
const char *target, *linkpath;
int err;
-
+
target = JS_ToCString(ctx, argv[0]);
if (!target)
return JS_EXCEPTION;
@@ -2739,7 +2739,7 @@ static JSValue js_os_readlink(JSContext *ctx, JSValueConst this_val,
char buf[PATH_MAX];
int err;
ssize_t res;
-
+
path = JS_ToCString(ctx, argv[0]);
if (!path)
return JS_EXCEPTION;
@@ -2763,7 +2763,7 @@ static char **build_envp(JSContext *ctx, JSValueConst obj)
const char *key, *str;
JSValue val;
size_t key_len, str_len;
-
+
if (JS_GetOwnPropertyNames(ctx, &tab, &len, obj,
JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY) < 0)
return NULL;
@@ -2821,7 +2821,7 @@ static int my_execvpe(const char *filename, char **argv, char **envp)
char buf[PATH_MAX];
size_t filename_len, path_len;
BOOL eacces_error;
-
+
filename_len = strlen(filename);
if (filename_len == 0) {
errno = ENOENT;
@@ -2829,7 +2829,7 @@ static int my_execvpe(const char *filename, char **argv, char **envp)
}
if (strchr(filename, '/'))
return execve(filename, argv, envp);
-
+
path = getenv("PATH");
if (!path)
path = (char *)"/bin:/usr/bin";
@@ -2851,7 +2851,7 @@ static int my_execvpe(const char *filename, char **argv, char **envp)
buf[path_len] = '/';
memcpy(buf + path_len + 1, filename, filename_len);
buf[path_len + 1 + filename_len] = '\0';
-
+
execve(buf, argv, envp);
switch(errno) {
@@ -2884,7 +2884,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
static const char *std_name[3] = { "stdin", "stdout", "stderr" };
int std_fds[3];
uint32_t uid = -1, gid = -1;
-
+
val = JS_GetPropertyStr(ctx, args, "length");
if (JS_IsException(val))
return JS_EXCEPTION;
@@ -2913,7 +2913,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
for(i = 0; i < 3; i++)
std_fds[i] = i;
-
+
/* get the options, if any */
if (argc >= 2) {
options = argv[1];
@@ -2922,7 +2922,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
goto exception;
if (get_bool_option(ctx, &use_path, options, "usePath"))
goto exception;
-
+
val = JS_GetPropertyStr(ctx, options, "file");
if (JS_IsException(val))
goto exception;
@@ -2967,7 +2967,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
if (!envp)
goto exception;
}
-
+
val = JS_GetPropertyStr(ctx, options, "uid");
if (JS_IsException(val))
goto exception;
@@ -3081,7 +3081,7 @@ static JSValue js_os_waitpid(JSContext *ctx, JSValueConst this_val,
{
int pid, status, options, ret;
JSValue obj;
-
+
if (JS_ToInt32(ctx, &pid, argv[0]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &options, argv[1]))
@@ -3101,7 +3101,7 @@ static JSValue js_os_waitpid(JSContext *ctx, JSValueConst this_val,
JS_DefinePropertyValueUint32(ctx, obj, 1, JS_NewInt32(ctx, status),
JS_PROP_C_W_E);
return obj;
-}
+}
/* pipe() -> [read_fd, write_fd] or null if error */
static JSValue js_os_pipe(JSContext *ctx, JSValueConst this_val,
@@ -3109,7 +3109,7 @@ static JSValue js_os_pipe(JSContext *ctx, JSValueConst this_val,
{
int pipe_fds[2], ret;
JSValue obj;
-
+
ret = pipe(pipe_fds);
if (ret < 0)
return JS_NULL;
@@ -3128,7 +3128,7 @@ static JSValue js_os_kill(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int pid, sig, ret;
-
+
if (JS_ToInt32(ctx, &pid, argv[0]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &sig, argv[1]))
@@ -3142,7 +3142,7 @@ static JSValue js_os_dup(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd, ret;
-
+
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
ret = js_get_errno(dup(fd));
@@ -3154,7 +3154,7 @@ static JSValue js_os_dup2(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd, fd2, ret;
-
+
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &fd2, argv[1]))
@@ -3228,7 +3228,7 @@ static JSWorkerMessagePipe *js_new_message_pipe(void)
{
JSWorkerMessagePipe *ps;
int pipe_fds[2];
-
+
if (pipe(pipe_fds) < 0)
return NULL;
@@ -3269,10 +3269,10 @@ static void js_free_message_pipe(JSWorkerMessagePipe *ps)
struct list_head *el, *el1;
JSWorkerMessage *msg;
int ref_count;
-
+
if (!ps)
return;
-
+
ref_count = atomic_add_int(&ps->ref_count, -1);
assert(ref_count >= 0);
if (ref_count == 0) {
@@ -3311,7 +3311,7 @@ static void js_worker_finalizer(JSRuntime *rt, JSValue val)
static JSClassDef js_worker_class = {
"Worker",
.finalizer = js_worker_finalizer,
-};
+};
static void *worker_func(void *opaque)
{
@@ -3320,12 +3320,12 @@ static void *worker_func(void *opaque)
JSThreadState *ts;
JSContext *ctx;
JSValue val;
-
+
rt = JS_NewRuntime();
if (rt == NULL) {
fprintf(stderr, "JS_NewRuntime failure");
exit(1);
- }
+ }
js_std_init_handlers(rt);
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
@@ -3334,7 +3334,7 @@ static void *worker_func(void *opaque)
ts = JS_GetRuntimeOpaque(rt);
ts->recv_pipe = args->recv_pipe;
ts->send_pipe = args->send_pipe;
-
+
/* function pointer to avoid linking the whole JS_NewContext() if
not needed */
ctx = js_worker_new_context_func(rt);
@@ -3369,7 +3369,7 @@ static JSValue js_worker_ctor_internal(JSContext *ctx, JSValueConst new_target,
{
JSValue obj = JS_UNDEFINED, proto;
JSWorkerData *s;
-
+
/* create the object */
if (JS_IsUndefined(new_target)) {
proto = JS_GetClassProto(ctx, js_worker_class_id);
@@ -3406,7 +3406,7 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
int ret;
const char *filename = NULL, *basename;
JSAtom basename_atom;
-
+
/* XXX: in order to avoid problems with resource liberation, we
don't support creating workers inside workers */
if (!is_main_thread(rt))
@@ -3422,7 +3422,7 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
JS_FreeAtom(ctx, basename_atom);
if (!basename)
goto fail;
-
+
/* module name */
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
@@ -3447,7 +3447,7 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
args->send_pipe, args->recv_pipe);
if (JS_IsException(obj))
goto fail;
-
+
pthread_attr_init(&attr);
/* no join at the end */
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
@@ -3485,10 +3485,10 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
uint8_t *data;
JSWorkerMessage *msg;
uint8_t **sab_tab;
-
+
if (!worker)
return JS_EXCEPTION;
-
+
data = JS_WriteObject2(ctx, &data_len, argv[0],
JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_REFERENCE,
&sab_tab, &sab_tab_len);
@@ -3516,7 +3516,7 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
js_free(ctx, data);
js_free(ctx, sab_tab);
-
+
/* increment the SAB reference counts */
for(i = 0; i < msg->sab_tab_len; i++) {
js_sab_dup(NULL, msg->sab_tab[i]);
@@ -3548,7 +3548,7 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
js_free(ctx, data);
js_free(ctx, sab_tab);
return JS_EXCEPTION;
-
+
}
static JSValue js_worker_set_onmessage(JSContext *ctx, JSValueConst this_val,
@@ -3558,7 +3558,7 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValueConst this_val,
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, js_worker_class_id);
JSWorkerMessageHandler *port;
-
+
if (!worker)
return JS_EXCEPTION;
@@ -3714,7 +3714,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
static int js_os_init(JSContext *ctx, JSModuleDef *m)
{
os_poll_func = js_os_poll;
-
+
#ifdef USE_WORKER
{
JSRuntime *rt = JS_GetRuntime(ctx);
@@ -3725,20 +3725,20 @@ static int js_os_init(JSContext *ctx, JSModuleDef *m)
JS_NewClass(JS_GetRuntime(ctx), js_worker_class_id, &js_worker_class);
proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, proto, js_worker_proto_funcs, countof(js_worker_proto_funcs));
-
+
obj = JS_NewCFunction2(ctx, js_worker_ctor, "Worker", 1,
JS_CFUNC_constructor, 0);
JS_SetConstructor(ctx, obj, proto);
-
+
JS_SetClassProto(ctx, js_worker_class_id, proto);
-
+
/* set 'Worker.parent' if necessary */
if (ts->recv_pipe && ts->send_pipe) {
JS_DefinePropertyValueStr(ctx, obj, "parent",
js_worker_ctor_internal(ctx, JS_UNDEFINED, ts->recv_pipe, ts->send_pipe),
JS_PROP_C_W_E);
}
-
+
JS_SetModuleExport(ctx, m, "Worker", obj);
}
#endif /* USE_WORKER */
@@ -3803,12 +3803,12 @@ void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
}
JS_SetPropertyStr(ctx, global_obj, "scriptArgs", args);
}
-
+
JS_SetPropertyStr(ctx, global_obj, "print",
JS_NewCFunction(ctx, js_print, "print", 1));
JS_SetPropertyStr(ctx, global_obj, "__loadScript",
JS_NewCFunction(ctx, js_loadScript, "__loadScript", 1));
-
+
JS_FreeValue(ctx, global_obj);
}
@@ -3827,7 +3827,7 @@ void js_std_init_handlers(JSRuntime *rt)
init_list_head(&ts->os_timers);
init_list_head(&ts->port_list);
ts->next_timer_id = 1;
-
+
JS_SetRuntimeOpaque(rt, ts);
#ifdef USE_WORKER
@@ -3857,7 +3857,7 @@ void js_std_free_handlers(JSRuntime *rt)
JSOSSignalHandler *sh = list_entry(el, JSOSSignalHandler, link);
free_sh(rt, sh);
}
-
+
list_for_each_safe(el, el1, &ts->os_timers) {
JSOSTimer *th = list_entry(el, JSOSTimer, link);
free_timer(rt, th);
@@ -3876,7 +3876,7 @@ void js_std_free_handlers(JSRuntime *rt)
static void js_dump_obj(JSContext *ctx, FILE *f, JSValueConst val)
{
const char *str;
-
+
str = JS_ToCString(ctx, val);
if (str) {
fprintf(f, "%s\n", str);
@@ -3890,7 +3890,7 @@ static void js_std_dump_error1(JSContext *ctx, JSValueConst exception_val)
{
JSValue val;
BOOL is_error;
-
+
is_error = JS_IsError(ctx, exception_val);
js_dump_obj(ctx, stderr, exception_val);
if (is_error) {
@@ -3905,7 +3905,7 @@ static void js_std_dump_error1(JSContext *ctx, JSValueConst exception_val)
void js_std_dump_error(JSContext *ctx)
{
JSValue exception_val;
-
+
exception_val = JS_GetException(ctx);
js_std_dump_error1(ctx, exception_val);
JS_FreeValue(ctx, exception_val);