Skip to content

Commit d5d4ce7

Browse files
committed
add: support import dll
1 parent 56ca7e4 commit d5d4ce7

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

src/quickjs-libc.c

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,57 @@ typedef JSModuleDef *(JSInitModuleFunc)(JSContext *ctx,
374374

375375

376376
#if defined(_WIN32)
377+
#include <Windows.h>
377378
static JSModuleDef *js_module_loader_so(JSContext *ctx,
378379
const char *module_name)
379380
{
380-
JS_ThrowReferenceError(ctx, "shared library modules are not supported yet");
381-
return NULL;
381+
JSModuleDef *m;
382+
HINSTANCE h;
383+
JSInitModuleFunc *init;
384+
char *filename;
385+
386+
if (!strchr(module_name, '/')) {
387+
/* must add a '/' so that the DLL is not searched in the
388+
system library paths */
389+
filename = js_malloc(ctx, strlen(module_name) + 2 + 1);
390+
if (!filename)
391+
return NULL;
392+
strcpy(filename, "./");
393+
strcpy(filename + 2, module_name);
394+
} else {
395+
filename = (char *)module_name;
396+
}
397+
398+
filename = (char *)module_name;
399+
400+
/* C module */
401+
h=LoadLibraryA(filename);
402+
403+
if (filename != module_name)
404+
js_free(ctx, filename);
405+
if (!h) {
406+
JS_ThrowReferenceError(ctx, "could not load module filename '%s' as shared library, ERR code: %d",
407+
module_name, (int)GetLastError());
408+
goto fail;
409+
}
410+
init = (JSInitModuleFunc*)GetProcAddress(h,"js_init_module");
411+
// init = dlsym(hd, "js_init_module");
412+
if (!init) {
413+
JS_ThrowReferenceError(ctx, "could not load module filename '%s': js_init_module not found",
414+
module_name);
415+
goto fail;
416+
}
417+
418+
m = init(ctx, module_name);
419+
if (!m) {
420+
JS_ThrowReferenceError(ctx, "could not load module filename '%s': initialization error",
421+
module_name);
422+
fail:
423+
if (h)
424+
FreeLibrary(h);
425+
return NULL;
426+
}
427+
return m;
382428
}
383429
#else
384430
static JSModuleDef *js_module_loader_so(JSContext *ctx,
@@ -489,7 +535,7 @@ JSModuleDef *js_module_loader(JSContext *ctx,
489535
{
490536
JSModuleDef *m;
491537

492-
if (has_suffix(module_name, ".so")) {
538+
if (has_suffix(module_name, ".dll") || has_suffix(module_name, ".so")) {
493539
m = js_module_loader_so(ctx, module_name);
494540
} else {
495541
size_t buf_len;

0 commit comments

Comments
 (0)