From 855fef26061dc3e3fc87ec34050caf40665a6201 Mon Sep 17 00:00:00 2001 From: Zia Date: Thu, 14 Mar 2024 01:14:46 +0330 Subject: [PATCH] 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;