From 855fef26061dc3e3fc87ec34050caf40665a6201 Mon Sep 17 00:00:00 2001 From: Zia Date: Thu, 14 Mar 2024 01:14:46 +0330 Subject: [PATCH 1/5] win32 dll fix --- quickjs-libc.c | 62 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index b00dc16..e7fc2d8 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -466,8 +466,50 @@ typedef JSModuleDef *(JSInitModuleFunc)(JSContext *ctx, static JSModuleDef *js_module_loader_so(JSContext *ctx, const char *module_name) { - JS_ThrowReferenceError(ctx, "shared library modules are not supported yet"); - return NULL; + JSModuleDef *m; + HANDLE hd; + JSInitModuleFunc *init; + char *filename; + + if (!strchr(module_name, '/')) { + /* must add a '/' so that the DLL is not searched in the + system library paths */ + filename = js_malloc(ctx, strlen(module_name) + 2 + 1); + if (!filename) + return NULL; + strcpy(filename, "./"); + strcpy(filename + 2, module_name); + } else { + filename = (char *)module_name; + } + + /* C module */ + hd = LoadLibrary(filename); + if (filename != module_name) + js_free(ctx, filename); + if (!hd) { + JS_ThrowReferenceError(ctx, "could not load module filename '%s'('%s') as shared library", + module_name,filename); + goto fail; + } + + init = (JSInitModuleFunc*)GetProcAddress(hd,"js_init_module"); + if (!init) { + JS_ThrowReferenceError(ctx, "could not load module filename '%s': js_init_module not found", + module_name); + goto fail; + } + + m = init(ctx, module_name); + if (!m) { + JS_ThrowReferenceError(ctx, "could not load module filename '%s': initialization error", + module_name); + fail: + if (hd) + FreeLibrary(hd); + return NULL; + } + return m; } #else static JSModuleDef *js_module_loader_so(JSContext *ctx, @@ -495,15 +537,13 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx, if (filename != module_name) js_free(ctx, filename); if (!hd) { - JS_ThrowReferenceError(ctx, "could not load module filename '%s' as shared library", - module_name); - goto fail; + goto dlerror; } init = dlsym(hd, "js_init_module"); if (!init) { - JS_ThrowReferenceError(ctx, "could not load module filename '%s': js_init_module not found", - module_name); + dlerror: + JS_ThrowReferenceError(ctx, "could not load module filename '%s': %s", module_name, dlerror()); goto fail; } @@ -578,7 +618,13 @@ JSModuleDef *js_module_loader(JSContext *ctx, { JSModuleDef *m; - if (has_suffix(module_name, ".so")) { + if ( + #if !defined(_WIN32) + has_suffix(module_name, ".so") + #else + has_suffix(module_name, ".dll") + #endif + ) { m = js_module_loader_so(ctx, module_name); } else { size_t buf_len; From f058e2a5f35c62d1928afa530eda5c3dbad94250 Mon Sep 17 00:00:00 2001 From: Zia Date: Thu, 14 Mar 2024 01:19:41 +0330 Subject: [PATCH 2/5] new string allocator --- quickjs.c | 14 ++++++++++++++ quickjs.h | 1 + 2 files changed, 15 insertions(+) diff --git a/quickjs.c b/quickjs.c index ebf45a9..c01e2ca 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3874,6 +3874,20 @@ static JSValue string_buffer_end(StringBuffer *s) return JS_MKPTR(JS_TAG_STRING, str); } +JSValue JS_NewStringWLen(JSContext *ctx, size_t buf_len) +{ + JSString *str; + if (buf_len <= 0) { + return JS_AtomToString(ctx, JS_ATOM_empty_string); + } + str = js_alloc_string_rt(ctx->rt, buf_len, 0); + if (unlikely(!str)){ + JS_ThrowOutOfMemory(ctx); + return JS_EXCEPTION; + } + memset(str->u.str8, 0, buf_len+1); + return JS_MKPTR(JS_TAG_STRING, str); +} /* create a string from a UTF-8 buffer */ JSValue JS_NewStringLen(JSContext *ctx, const char *buf, size_t buf_len) { diff --git a/quickjs.h b/quickjs.h index a951e67..09ad07d 100644 --- a/quickjs.h +++ b/quickjs.h @@ -696,6 +696,7 @@ int JS_ToBigInt64(JSContext *ctx, int64_t *pres, JSValueConst val); /* same as JS_ToInt64() but allow BigInt */ int JS_ToInt64Ext(JSContext *ctx, int64_t *pres, JSValueConst val); +JSValue JS_NewStringWLen(JSContext *ctx, size_t len); JSValue JS_NewStringLen(JSContext *ctx, const char *str1, size_t len1); JSValue JS_NewString(JSContext *ctx, const char *str); JSValue JS_NewAtomString(JSContext *ctx, const char *str); From a4eff5077e138d27411907addb9597d1da226be6 Mon Sep 17 00:00:00 2001 From: Zia Date: Thu, 14 Mar 2024 01:20:11 +0330 Subject: [PATCH 3/5] duplicated gettimeofday --- quickjs.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/quickjs.c b/quickjs.c index c01e2ca..2a7f3e0 100644 --- a/quickjs.c +++ b/quickjs.c @@ -43243,11 +43243,10 @@ static uint64_t xorshift64star(uint64_t *pstate) return x * 0x2545F4914F6CDD1D; } +static int64_t date_now(void); static void js_random_init(JSContext *ctx) { - struct timeval tv; - gettimeofday(&tv, NULL); - ctx->random_state = ((int64_t)tv.tv_sec * 1000000) + tv.tv_usec; + ctx->random_state = date_now(); /* the state must be non zero */ if (ctx->random_state == 0) ctx->random_state = 1; From adcfe4cadba412955fb7caff8aca6eeb04c39ac1 Mon Sep 17 00:00:00 2001 From: Zia Date: Thu, 14 Mar 2024 01:20:53 +0330 Subject: [PATCH 4/5] class id argument type fix --- quickjs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 2a7f3e0..2423d40 100644 --- a/quickjs.c +++ b/quickjs.c @@ -4943,7 +4943,7 @@ static int JS_SetObjectData(JSContext *ctx, JSValueConst obj, JSValue val) return -1; } -JSValue JS_NewObjectClass(JSContext *ctx, int class_id) +JSValue JS_NewObjectClass(JSContext *ctx, JSClassID class_id) { return JS_NewObjectProtoClass(ctx, ctx->class_proto[class_id], class_id); } From 619be5b1a4d32a6356567f0d7a0ad5d36f44e874 Mon Sep 17 00:00:00 2001 From: Zia Date: Thu, 14 Mar 2024 01:24:16 +0330 Subject: [PATCH 5/5] raw access to string bytes --- quickjs.c | 12 ++++++++++++ quickjs.h | 1 + 2 files changed, 13 insertions(+) diff --git a/quickjs.c b/quickjs.c index 2423d40..76b990b 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3994,6 +3994,18 @@ JSValue JS_NewAtomString(JSContext *ctx, const char *str) return val; } +uint8_t *JS_ToCStringLenRaw(JSContext *ctx, size_t *plen, JSValueConst val) +{ + if (JS_VALUE_GET_TAG(val) != JS_TAG_STRING) + { + if(plen) *plen = 0; + return NULL; + } + JSString *str = JS_VALUE_GET_STRING(val); + if(plen) *plen = str->len; + return str->u.str8; +} + /* return (NULL, 0) if exception. */ /* return pointer into a JSString with a live ref_count */ /* cesu8 determines if non-BMP1 codepoints are encoded as 1 or 2 utf-8 sequences */ diff --git a/quickjs.h b/quickjs.h index 09ad07d..5f9f805 100644 --- a/quickjs.h +++ b/quickjs.h @@ -702,6 +702,7 @@ JSValue JS_NewString(JSContext *ctx, const char *str); JSValue JS_NewAtomString(JSContext *ctx, const char *str); JSValue JS_ToString(JSContext *ctx, JSValueConst val); JSValue JS_ToPropertyKey(JSContext *ctx, JSValueConst val); +uint8_t *JS_ToCStringLenRaw(JSContext *ctx, size_t *plen, JSValueConst val1); const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValueConst val1, JS_BOOL cesu8); static inline const char *JS_ToCStringLen(JSContext *ctx, size_t *plen, JSValueConst val1) {