From 29a2588be2b89c5c523109545adc1e43213eac0e Mon Sep 17 00:00:00 2001 From: zeromake Date: Sat, 24 Aug 2024 12:55:18 +0800 Subject: [PATCH 01/18] feat(libc): windows native module support --- quickjs-libc.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index e9e276db7..7d71b06a6 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -58,6 +58,11 @@ #define rmdir _rmdir #define getcwd _getcwd #define chdir _chdir + +#ifndef NATIVE_MODULE_SUFFIX +#define NATIVE_MODULE_SUFFIX ".dll" +#endif + #else #include #if !defined(__wasi__) @@ -78,6 +83,10 @@ typedef sig_t sighandler_t; extern char **environ; #endif +#ifndef NATIVE_MODULE_SUFFIX +#define NATIVE_MODULE_SUFFIX ".so" +#endif + #endif /* _WIN32 */ #if !defined(_WIN32) && !defined(__wasi__) @@ -479,7 +488,66 @@ typedef JSModuleDef *(JSInitModuleFunc)(JSContext *ctx, const char *module_name); -#if defined(_WIN32) || defined(__wasi__) +#if defined(_WIN32) +static JSModuleDef *js_module_loader_so(JSContext *ctx, + const char *module_name) +{ + JSModuleDef *m; + HINSTANCE hd; + JSInitModuleFunc *init; + char *filename; + int len = strlen(module_name); + if (len > 2 && + ((module_name[0] >= 'A' && module_name[0] <= 'Z') || + (module_name[0] >= 'a' && module_name[0] <= 'z')) && + module_name[1] == ':') { + filename = (char *)module_name; + } else if (len > 2 && module_name[0] != '.' && (module_name[1]!= '/' || module_name[1] == '\\')) { + filename = js_malloc(ctx, strlen(module_name) + 2 + 1); + if (!filename) + return NULL; + strcpy(filename, "./"); + strcpy(filename + 2, module_name); + } + { + wchar_t wfilename[PATH_MAX + 1]; + size_t n; + int error = dirent_mbstowcs_s( + &n, + wfilename, + PATH_MAX + 1, + filename, + PATH_MAX + 1); + if (filename!= module_name) + js_free(ctx, filename); + if (error) { + JS_ThrowReferenceError(ctx, "could not convert '%s' to wide character string", module_name); + goto fail; + } + hd = LoadLibraryW(wfilename); + if (hd == NULL) { + JS_ThrowReferenceError(ctx, "could not load module filename '%s' as shared library: %s", + module_name, GetLastError()); + 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, GetLastError()); + 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 != NULL) FreeLibrary(hd); + return NULL; + } + return m; +} +#elif defined(__wasi__) static JSModuleDef *js_module_loader_so(JSContext *ctx, const char *module_name) { @@ -595,7 +663,7 @@ JSModuleDef *js_module_loader(JSContext *ctx, { JSModuleDef *m; - if (has_suffix(module_name, ".so")) { + if (has_suffix(module_name, NATIVE_MODULE_SUFFIX)) { m = js_module_loader_so(ctx, module_name); } else { size_t buf_len; From 94f37534a4a18951218d2de9d4eb964c0f6e3f0a Mon Sep 17 00:00:00 2001 From: zeromake Date: Sat, 24 Aug 2024 12:58:06 +0800 Subject: [PATCH 02/18] fix(libc): NATIVE_MODULE_SUFFIX -> QJS_NATIVE_MODULE_SUFFIX --- quickjs-libc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 7d71b06a6..589bad198 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -59,8 +59,8 @@ #define getcwd _getcwd #define chdir _chdir -#ifndef NATIVE_MODULE_SUFFIX -#define NATIVE_MODULE_SUFFIX ".dll" +#ifndef QJS_NATIVE_MODULE_SUFFIX +#define QJS_NATIVE_MODULE_SUFFIX ".dll" #endif #else @@ -83,8 +83,8 @@ typedef sig_t sighandler_t; extern char **environ; #endif -#ifndef NATIVE_MODULE_SUFFIX -#define NATIVE_MODULE_SUFFIX ".so" +#ifndef QJS_NATIVE_MODULE_SUFFIX +#define QJS_NATIVE_MODULE_SUFFIX ".so" #endif #endif /* _WIN32 */ @@ -663,7 +663,7 @@ JSModuleDef *js_module_loader(JSContext *ctx, { JSModuleDef *m; - if (has_suffix(module_name, NATIVE_MODULE_SUFFIX)) { + if (has_suffix(module_name, QJS_NATIVE_MODULE_SUFFIX)) { m = js_module_loader_so(ctx, module_name); } else { size_t buf_len; From 7685526732b958cc2a26a2892e6908144e6966c8 Mon Sep 17 00:00:00 2001 From: zeromake Date: Mon, 26 Aug 2024 19:43:22 +0800 Subject: [PATCH 03/18] fix: dirent_mbstowcs_s not has --- quickjs-libc.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 589bad198..df62526cd 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -510,36 +510,24 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx, strcpy(filename + 2, module_name); } { - wchar_t wfilename[PATH_MAX + 1]; - size_t n; - int error = dirent_mbstowcs_s( - &n, - wfilename, - PATH_MAX + 1, - filename, - PATH_MAX + 1); + hd = LoadLibraryA(filename); if (filename!= module_name) js_free(ctx, filename); - if (error) { - JS_ThrowReferenceError(ctx, "could not convert '%s' to wide character string", module_name); - goto fail; - } - hd = LoadLibraryW(wfilename); if (hd == NULL) { - JS_ThrowReferenceError(ctx, "could not load module filename '%s' as shared library: %s", + JS_ThrowReferenceError(ctx, "js_load_module '%s' error: %lu", module_name, GetLastError()); 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", + JS_ThrowReferenceError(ctx, "js_init_module '%s' not found: %lu", module_name, GetLastError()); goto fail; } m = init(ctx, module_name); if (!m) { - JS_ThrowReferenceError(ctx, "could not load module filename '%s': initialization error", + JS_ThrowReferenceError(ctx, "js_call_module '%s' initialization error", module_name); fail: if (hd != NULL) FreeLibrary(hd); From c0ef55ea1d37560f83f195463e0085a41d0c78bf Mon Sep 17 00:00:00 2001 From: zeromake Date: Mon, 26 Aug 2024 20:39:01 +0800 Subject: [PATCH 04/18] fix: filename init NULL --- quickjs-libc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index df62526cd..6c9839221 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -495,7 +495,7 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx, JSModuleDef *m; HINSTANCE hd; JSInitModuleFunc *init; - char *filename; + char *filename = NULL; int len = strlen(module_name); if (len > 2 && ((module_name[0] >= 'A' && module_name[0] <= 'Z') || @@ -519,7 +519,7 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx, goto fail; } } - init = (JSInitModuleFunc *)GetProcAddress(hd, "js_init_module"); + init = (JSInitModuleFunc *)(void *)GetProcAddress(hd, "js_init_module"); if (!init) { JS_ThrowReferenceError(ctx, "js_init_module '%s' not found: %lu", module_name, GetLastError()); From f87ab72668cf17660bbbaac4624bd546799491c1 Mon Sep 17 00:00:00 2001 From: zeromake Date: Wed, 28 Aug 2024 10:19:21 +0800 Subject: [PATCH 05/18] fix: codeview change --- quickjs-libc.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 6c9839221..86a730da2 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -496,28 +496,26 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx, HINSTANCE hd; JSInitModuleFunc *init; char *filename = NULL; - int len = strlen(module_name); - if (len > 2 && - ((module_name[0] >= 'A' && module_name[0] <= 'Z') || - (module_name[0] >= 'a' && module_name[0] <= 'z')) && - module_name[1] == ':') { + size_t len = strlen(module_name); + JS_BOOL is_absolute = len > 2 && ((module_name[0] >= 'A' && module_name[0] <= 'Z') || + (module_name[0] >= 'a' && module_name[0] <= 'z')) && module_name[1] == ':'; + JS_BOOL is_relative = len > 2 && module_name[0] != '.' && (module_name[1] == '/' || module_name[1] == '\\'); + if (is_absolute || is_relative) { filename = (char *)module_name; - } else if (len > 2 && module_name[0] != '.' && (module_name[1]!= '/' || module_name[1] == '\\')) { - filename = js_malloc(ctx, strlen(module_name) + 2 + 1); + } else { + filename = js_malloc(ctx, len + 2 + 1); if (!filename) return NULL; strcpy(filename, "./"); strcpy(filename + 2, module_name); } - { - hd = LoadLibraryA(filename); - if (filename!= module_name) - js_free(ctx, filename); - if (hd == NULL) { - JS_ThrowReferenceError(ctx, "js_load_module '%s' error: %lu", - module_name, GetLastError()); - goto fail; - } + hd = LoadLibraryA(filename); + if (filename != module_name) + js_free(ctx, filename); + if (hd == NULL) { + JS_ThrowReferenceError(ctx, "js_load_module '%s' error: %lu", + module_name, GetLastError()); + goto fail; } init = (JSInitModuleFunc *)(void *)GetProcAddress(hd, "js_init_module"); if (!init) { From efa5ef60333dde95e2462bf40c69a97bbe89608c Mon Sep 17 00:00:00 2001 From: zeromake Date: Tue, 3 Sep 2024 00:06:04 +0800 Subject: [PATCH 06/18] feat: BUILD_EXAMPLES support msvc --- CMakeLists.txt | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4de876f8..55474cd37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -275,7 +275,7 @@ target_link_libraries(function_source ${qjs_libs}) # Examples # -if(BUILD_EXAMPLES AND NOT WIN32) +if(BUILD_EXAMPLES) add_executable(hello gen/hello.c ) @@ -292,27 +292,27 @@ if(BUILD_EXAMPLES AND NOT WIN32) target_compile_definitions(hello_module PRIVATE ${qjs_defines}) target_link_libraries(hello_module ${qjs_libs}) - if(NOT WIN32) - add_library(fib MODULE examples/fib.c) - set_target_properties(fib PROPERTIES - PREFIX "" - C_VISIBILITY_PRESET default - ) - target_compile_definitions(fib PRIVATE JS_SHARED_LIBRARY) - if(APPLE) - target_link_options(fib PRIVATE -undefined dynamic_lookup) - endif() + add_library(fib MODULE examples/fib.c) + set_target_properties(fib PROPERTIES + PREFIX "" + C_VISIBILITY_PRESET default + ) + target_compile_definitions(fib PRIVATE JS_SHARED_LIBRARY) + target_link_libraries(fib ${qjs_libs}) + if(APPLE) + target_link_options(fib PRIVATE -undefined dynamic_lookup) + endif() - add_library(point MODULE examples/point.c) - set_target_properties(point PROPERTIES - PREFIX "" - C_VISIBILITY_PRESET default - ) - target_compile_definitions(point PRIVATE JS_SHARED_LIBRARY) - if(APPLE) - target_link_options(point PRIVATE -undefined dynamic_lookup) + add_library(point MODULE examples/point.c) + set_target_properties(point PROPERTIES + PREFIX "" + C_VISIBILITY_PRESET default + ) + target_compile_definitions(point PRIVATE JS_SHARED_LIBRARY) + target_link_libraries(point ${qjs_libs}) + if(APPLE) + target_link_options(point PRIVATE -undefined dynamic_lookup) endif() - endif() add_executable(test_fib examples/fib.c From c703ffb834fdea1e6055074a78e7db569ebe64bd Mon Sep 17 00:00:00 2001 From: zeromake Date: Wed, 4 Sep 2024 07:59:43 +0800 Subject: [PATCH 07/18] fix: == . Co-authored-by: Ben Noordhuis --- quickjs-libc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 86a730da2..8e765029a 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -499,7 +499,7 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx, size_t len = strlen(module_name); JS_BOOL is_absolute = len > 2 && ((module_name[0] >= 'A' && module_name[0] <= 'Z') || (module_name[0] >= 'a' && module_name[0] <= 'z')) && module_name[1] == ':'; - JS_BOOL is_relative = len > 2 && module_name[0] != '.' && (module_name[1] == '/' || module_name[1] == '\\'); + JS_BOOL is_relative = len > 2 && module_name[0] == '.' && (module_name[1] == '/' || module_name[1] == '\\'); if (is_absolute || is_relative) { filename = (char *)module_name; } else { From 76a058834e00ac6e9ea64f72176abe930a63f831 Mon Sep 17 00:00:00 2001 From: zeromake Date: Wed, 4 Sep 2024 08:00:20 +0800 Subject: [PATCH 08/18] fix: uintptr_t conv Co-authored-by: Ben Noordhuis --- quickjs-libc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 8e765029a..7a1c4d504 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -517,7 +517,7 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx, module_name, GetLastError()); goto fail; } - init = (JSInitModuleFunc *)(void *)GetProcAddress(hd, "js_init_module"); + init = (JSInitModuleFunc *)(uintptr_t)GetProcAddress(hd, "js_init_module"); if (!init) { JS_ThrowReferenceError(ctx, "js_init_module '%s' not found: %lu", module_name, GetLastError()); From 64ae115c8730ec992257514fdfd14a2873e7c76a Mon Sep 17 00:00:00 2001 From: zeromake Date: Wed, 4 Sep 2024 08:00:34 +0800 Subject: [PATCH 09/18] Update quickjs-libc.c Co-authored-by: Ben Noordhuis --- quickjs-libc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 7a1c4d504..bf26b8485 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -528,7 +528,8 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx, JS_ThrowReferenceError(ctx, "js_call_module '%s' initialization error", module_name); fail: - if (hd != NULL) FreeLibrary(hd); + if (hd != NULL) + FreeLibrary(hd); return NULL; } return m; From 096ada0a027da304788b2c1f4756bfbf2948e538 Mon Sep 17 00:00:00 2001 From: zeromake Date: Fri, 6 Sep 2024 10:00:26 +0800 Subject: [PATCH 10/18] fix: only win32 example link qjs --- CMakeLists.txt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 55474cd37..524e862e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -298,7 +298,9 @@ if(BUILD_EXAMPLES) C_VISIBILITY_PRESET default ) target_compile_definitions(fib PRIVATE JS_SHARED_LIBRARY) - target_link_libraries(fib ${qjs_libs}) + if(WIN32) + target_link_libraries(fib ${qjs_libs}) + endif() if(APPLE) target_link_options(fib PRIVATE -undefined dynamic_lookup) endif() @@ -309,7 +311,9 @@ if(BUILD_EXAMPLES) C_VISIBILITY_PRESET default ) target_compile_definitions(point PRIVATE JS_SHARED_LIBRARY) - target_link_libraries(point ${qjs_libs}) + if(WIN32) + target_link_libraries(point ${qjs_libs}) + endif() if(APPLE) target_link_options(point PRIVATE -undefined dynamic_lookup) endif() From c2391b091de558fbbb503f7338d952d9f6152045 Mon Sep 17 00:00:00 2001 From: zeromake Date: Fri, 6 Sep 2024 22:03:15 +0800 Subject: [PATCH 11/18] feat: support ci examples --- .github/workflows/ci.yml | 24 +++++++++++++++++++++--- CMakeLists.txt | 2 ++ examples/fib.c | 10 +++++++++- examples/point.c | 10 +++++++++- quickjs-libc.c | 13 ++++--------- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f849eefe4..876c128fd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -192,14 +192,20 @@ jobs: - uses: actions/checkout@v4 - name: build run: | - cmake -B build -G "Visual Studio 17 2022" -A ${{matrix.arch}} + cmake -B build -D BUILD_EXAMPLES=ON -D BUILD_QJS_LIBC=ON -G "Visual Studio 17 2022" -A ${{matrix.arch}} cmake --build build --config ${{matrix.buildType}} --target qjs_exe cmake --build build --config ${{matrix.buildType}} --target function_source + cmake --build build --config ${{matrix.buildType}} --target fib + cmake --build build --config ${{matrix.buildType}} --target point - name: stats run: | build\${{matrix.buildType}}\qjs.exe -qd - name: test run: | + cp build\${{matrix.buildType}}\fib.so examples\ + cp build\${{matrix.buildType}}\point.so examples\ + build\${{matrix.buildType}}\qjs.exe examples\test_fib.js + build\${{matrix.buildType}}\qjs.exe examples\test_point.js build\${{matrix.buildType}}\qjs.exe tests\test_bigint.js build\${{matrix.buildType}}\qjs.exe tests\test_bjson.js build\${{matrix.buildType}}\qjs.exe tests\test_closure.js @@ -221,14 +227,20 @@ jobs: - uses: actions/checkout@v4 - name: build run: | - cmake -B build -G "Visual Studio 17 2022" -T ClangCL + cmake -B build -D BUILD_EXAMPLES=ON -D BUILD_QJS_LIBC=ON -G "Visual Studio 17 2022" -T ClangCL cmake --build build --config ${{matrix.buildType}} --target qjs_exe cmake --build build --config ${{matrix.buildType}} --target function_source + cmake --build build --config ${{matrix.buildType}} --target fib + cmake --build build --config ${{matrix.buildType}} --target point - name: stats run: | build\${{matrix.buildType}}\qjs.exe -qd - name: test run: | + cp build\${{matrix.buildType}}\fib.so examples\ + cp build\${{matrix.buildType}}\point.so examples\ + build\${{matrix.buildType}}\qjs.exe examples\test_fib.js + build\${{matrix.buildType}}\qjs.exe examples\test_point.js build\${{matrix.buildType}}\qjs.exe tests\test_bigint.js build\${{matrix.buildType}}\qjs.exe tests\test_bjson.js build\${{matrix.buildType}}\qjs.exe tests\test_closure.js @@ -254,14 +266,20 @@ jobs: ninja.exe --version - name: build run: | - cmake -B build -DCMAKE_BUILD_TYPE=${{matrix.buildType}} -G "Ninja" + cmake -B build -D BUILD_EXAMPLES=ON -D BUILD_QJS_LIBC=ON -DCMAKE_BUILD_TYPE=${{matrix.buildType}} -G "Ninja" cmake --build build --target qjs_exe cmake --build build --target function_source + cmake --build build --target fib + cmake --build build --target point - name: stats run: | build\qjs.exe -qd - name: test run: | + cp build\fib.so examples\ + cp build\point.so examples\ + build\qjs.exe examples\test_fib.js + build\qjs.exe examples\test_point.js build\qjs.exe tests\test_bigint.js build\qjs.exe tests\test_bjson.js build\qjs.exe tests\test_closure.js diff --git a/CMakeLists.txt b/CMakeLists.txt index 524e862e3..fdb8782d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -300,6 +300,7 @@ if(BUILD_EXAMPLES) target_compile_definitions(fib PRIVATE JS_SHARED_LIBRARY) if(WIN32) target_link_libraries(fib ${qjs_libs}) + set_target_properties(fib PROPERTIES SUFFIX ".so") endif() if(APPLE) target_link_options(fib PRIVATE -undefined dynamic_lookup) @@ -313,6 +314,7 @@ if(BUILD_EXAMPLES) target_compile_definitions(point PRIVATE JS_SHARED_LIBRARY) if(WIN32) target_link_libraries(point ${qjs_libs}) + set_target_properties(point PROPERTIES SUFFIX ".so") endif() if(APPLE) target_link_options(point PRIVATE -undefined dynamic_lookup) diff --git a/examples/fib.c b/examples/fib.c index 0786378fa..b965acc5b 100644 --- a/examples/fib.c +++ b/examples/fib.c @@ -61,7 +61,15 @@ static int js_fib_init(JSContext *ctx, JSModuleDef *m) #define JS_INIT_MODULE js_init_module_fib #endif -JSModuleDef *JS_INIT_MODULE(JSContext *ctx, const char *module_name) +#ifndef JS_EXTERN +#ifdef _WIN32 +#define JS_EXTERN __declspec(dllexport) +#else +#define JS_EXTERN +#endif +#endif + +JS_EXTERN JSModuleDef *JS_INIT_MODULE(JSContext *ctx, const char *module_name) { JSModuleDef *m; m = JS_NewCModule(ctx, module_name, js_fib_init); diff --git a/examples/point.c b/examples/point.c index da22d6f5c..a82df1519 100644 --- a/examples/point.c +++ b/examples/point.c @@ -141,7 +141,15 @@ static int js_point_init(JSContext *ctx, JSModuleDef *m) return 0; } -JSModuleDef *js_init_module(JSContext *ctx, const char *module_name) +#ifndef JS_EXTERN +#ifdef _WIN32 +#define JS_EXTERN __declspec(dllexport) +#else +#define JS_EXTERN +#endif +#endif + +JS_EXTERN JSModuleDef *js_init_module(JSContext *ctx, const char *module_name) { JSModuleDef *m; m = JS_NewCModule(ctx, module_name, js_point_init); diff --git a/quickjs-libc.c b/quickjs-libc.c index bf26b8485..bfafc8083 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -58,11 +58,6 @@ #define rmdir _rmdir #define getcwd _getcwd #define chdir _chdir - -#ifndef QJS_NATIVE_MODULE_SUFFIX -#define QJS_NATIVE_MODULE_SUFFIX ".dll" -#endif - #else #include #if !defined(__wasi__) @@ -83,10 +78,6 @@ typedef sig_t sighandler_t; extern char **environ; #endif -#ifndef QJS_NATIVE_MODULE_SUFFIX -#define QJS_NATIVE_MODULE_SUFFIX ".so" -#endif - #endif /* _WIN32 */ #if !defined(_WIN32) && !defined(__wasi__) @@ -103,6 +94,10 @@ extern char **environ; #include "list.h" #include "quickjs-libc.h" +#ifndef QJS_NATIVE_MODULE_SUFFIX +#define QJS_NATIVE_MODULE_SUFFIX ".so" +#endif + /* TODO: - add socket calls */ From fedc1bc8ede6d18f2f78c394c66eee6564745f3a Mon Sep 17 00:00:00 2001 From: zeromake Date: Mon, 9 Sep 2024 15:26:52 +0800 Subject: [PATCH 12/18] feat: add support dynamic import native module --- CMakeLists.txt | 2 -- examples/test_fib.js | 4 +++- examples/test_point.js | 5 ++++- quickjs-libc.c | 6 ++++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fdb8782d6..524e862e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -300,7 +300,6 @@ if(BUILD_EXAMPLES) target_compile_definitions(fib PRIVATE JS_SHARED_LIBRARY) if(WIN32) target_link_libraries(fib ${qjs_libs}) - set_target_properties(fib PROPERTIES SUFFIX ".so") endif() if(APPLE) target_link_options(fib PRIVATE -undefined dynamic_lookup) @@ -314,7 +313,6 @@ if(BUILD_EXAMPLES) target_compile_definitions(point PRIVATE JS_SHARED_LIBRARY) if(WIN32) target_link_libraries(point ${qjs_libs}) - set_target_properties(point PROPERTIES SUFFIX ".so") endif() if(APPLE) target_link_options(point PRIVATE -undefined dynamic_lookup) diff --git a/examples/test_fib.js b/examples/test_fib.js index 70d26bd8c..4bdf9dc9c 100644 --- a/examples/test_fib.js +++ b/examples/test_fib.js @@ -1,6 +1,8 @@ /* example of JS module importing a C module */ +import * as os from "os"; -import { fib } from "./fib.so"; +const isWin = os.platform === 'win32'; +const { fib } = await import(`./fib.${isWin ? 'dll' : 'so'}`); console.log("Hello World"); console.log("fib(10)=", fib(10)); diff --git a/examples/test_point.js b/examples/test_point.js index 0659bc350..7400420c7 100644 --- a/examples/test_point.js +++ b/examples/test_point.js @@ -1,5 +1,8 @@ /* example of JS module importing a C module */ -import { Point } from "./point.so"; +import * as os from "os"; + +const isWin = os.platform === 'win32'; +const { Point } = await import(`./point.${isWin ? 'dll' : 'so'}`); function assert(b, str) { diff --git a/quickjs-libc.c b/quickjs-libc.c index bfafc8083..e1d909546 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -94,9 +94,15 @@ extern char **environ; #include "list.h" #include "quickjs-libc.h" +#ifdef _WIN32 +#ifndef QJS_NATIVE_MODULE_SUFFIX +#define QJS_NATIVE_MODULE_SUFFIX ".dll" +#endif +#else #ifndef QJS_NATIVE_MODULE_SUFFIX #define QJS_NATIVE_MODULE_SUFFIX ".so" #endif +#endif /* TODO: - add socket calls From 306704c02d8262a7cb8a723aa0a9d75b243a8651 Mon Sep 17 00:00:00 2001 From: zeromake Date: Mon, 9 Sep 2024 15:30:24 +0800 Subject: [PATCH 13/18] fix: use win32 shared file is dll --- .github/workflows/ci.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 876c128fd..36a159470 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -202,8 +202,8 @@ jobs: build\${{matrix.buildType}}\qjs.exe -qd - name: test run: | - cp build\${{matrix.buildType}}\fib.so examples\ - cp build\${{matrix.buildType}}\point.so examples\ + cp build\${{matrix.buildType}}\fib.dll examples\ + cp build\${{matrix.buildType}}\point.dll examples\ build\${{matrix.buildType}}\qjs.exe examples\test_fib.js build\${{matrix.buildType}}\qjs.exe examples\test_point.js build\${{matrix.buildType}}\qjs.exe tests\test_bigint.js @@ -237,8 +237,8 @@ jobs: build\${{matrix.buildType}}\qjs.exe -qd - name: test run: | - cp build\${{matrix.buildType}}\fib.so examples\ - cp build\${{matrix.buildType}}\point.so examples\ + cp build\${{matrix.buildType}}\fib.dll examples\ + cp build\${{matrix.buildType}}\point.dll examples\ build\${{matrix.buildType}}\qjs.exe examples\test_fib.js build\${{matrix.buildType}}\qjs.exe examples\test_point.js build\${{matrix.buildType}}\qjs.exe tests\test_bigint.js @@ -276,8 +276,8 @@ jobs: build\qjs.exe -qd - name: test run: | - cp build\fib.so examples\ - cp build\point.so examples\ + cp build\fib.dll examples\ + cp build\point.dll examples\ build\qjs.exe examples\test_fib.js build\qjs.exe examples\test_point.js build\qjs.exe tests\test_bigint.js From 8e465ca96a5d5e0e2954308912794d445058ac61 Mon Sep 17 00:00:00 2001 From: zeromake Date: Mon, 9 Sep 2024 18:09:46 +0800 Subject: [PATCH 14/18] fix: update codegen --- gen/test_fib.c | 64 +++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/gen/test_fib.c b/gen/test_fib.c index 53c431bf1..9a41851ac 100644 --- a/gen/test_fib.c +++ b/gen/test_fib.c @@ -2,30 +2,46 @@ #include "quickjs-libc.h" -const uint32_t qjsc_test_fib_size = 167; +const uint32_t qjsc_test_fib_size = 293; -const uint8_t qjsc_test_fib[167] = { - 0x0d, 0x07, 0x28, 0x65, 0x78, 0x61, 0x6d, 0x70, +const uint8_t qjsc_test_fib[293] = { + 0x0d, 0x0d, 0x28, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2f, 0x74, 0x65, 0x73, 0x74, - 0x5f, 0x66, 0x69, 0x62, 0x2e, 0x6a, 0x73, 0x10, - 0x2e, 0x2f, 0x66, 0x69, 0x62, 0x2e, 0x73, 0x6f, - 0x06, 0x66, 0x69, 0x62, 0x0e, 0x63, 0x6f, 0x6e, - 0x73, 0x6f, 0x6c, 0x65, 0x06, 0x6c, 0x6f, 0x67, - 0x16, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, - 0x6f, 0x72, 0x6c, 0x64, 0x10, 0x66, 0x69, 0x62, - 0x28, 0x31, 0x30, 0x29, 0x3d, 0x0d, 0xb2, 0x03, - 0x01, 0xb4, 0x03, 0x00, 0x00, 0x01, 0x00, 0xb6, - 0x03, 0x00, 0x00, 0x0c, 0x20, 0xfa, 0x01, 0x9e, - 0x01, 0x00, 0x00, 0x00, 0x05, 0x01, 0x00, 0x32, - 0x00, 0xb6, 0x03, 0x00, 0x0c, 0x08, 0xe9, 0x02, - 0x29, 0x38, 0xdc, 0x00, 0x00, 0x00, 0x42, 0xdd, - 0x00, 0x00, 0x00, 0x04, 0xde, 0x00, 0x00, 0x00, - 0x24, 0x01, 0x00, 0x0e, 0x38, 0xdc, 0x00, 0x00, - 0x00, 0x42, 0xdd, 0x00, 0x00, 0x00, 0x04, 0xdf, - 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0xbb, 0x0a, - 0xee, 0x24, 0x02, 0x00, 0x0e, 0x06, 0x2e, 0xb2, - 0x03, 0x01, 0x01, 0x0a, 0x01, 0x01, 0x00, 0x04, - 0x0a, 0x02, 0x62, 0x00, 0x4d, 0x30, 0x00, + 0x5f, 0x66, 0x69, 0x62, 0x2e, 0x6a, 0x73, 0x04, + 0x6f, 0x73, 0x0a, 0x69, 0x73, 0x57, 0x69, 0x6e, + 0x06, 0x66, 0x69, 0x62, 0x10, 0x70, 0x6c, 0x61, + 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x0a, 0x77, 0x69, + 0x6e, 0x33, 0x32, 0x0c, 0x2e, 0x2f, 0x66, 0x69, + 0x62, 0x2e, 0x06, 0x64, 0x6c, 0x6c, 0x04, 0x73, + 0x6f, 0x0e, 0x63, 0x6f, 0x6e, 0x73, 0x6f, 0x6c, + 0x65, 0x06, 0x6c, 0x6f, 0x67, 0x16, 0x48, 0x65, + 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, + 0x64, 0x10, 0x66, 0x69, 0x62, 0x28, 0x31, 0x30, + 0x29, 0x3d, 0x0d, 0xb2, 0x03, 0x01, 0xb4, 0x03, + 0x00, 0x00, 0x01, 0x00, 0xf8, 0x01, 0x00, 0x01, + 0x0c, 0x20, 0xfa, 0x01, 0x9e, 0x01, 0x00, 0x00, + 0x00, 0x05, 0x03, 0x00, 0x73, 0x00, 0xb4, 0x03, + 0x00, 0x0d, 0xb6, 0x03, 0x00, 0x0d, 0xb8, 0x03, + 0x01, 0x0d, 0x08, 0xe9, 0x02, 0x29, 0x65, 0x00, + 0x00, 0x41, 0xdd, 0x00, 0x00, 0x00, 0x04, 0xde, + 0x00, 0x00, 0x00, 0xae, 0xe1, 0x06, 0x11, 0xf1, + 0xea, 0x0b, 0x70, 0x42, 0xdc, 0x00, 0x00, 0x00, + 0xe2, 0x0e, 0xeb, 0x24, 0x0e, 0x04, 0xdf, 0x00, + 0x00, 0x00, 0x42, 0x5b, 0x00, 0x00, 0x00, 0x65, + 0x01, 0x00, 0xe9, 0x08, 0x04, 0xe0, 0x00, 0x00, + 0x00, 0xeb, 0x06, 0x04, 0xe1, 0x00, 0x00, 0x00, + 0x24, 0x01, 0x00, 0x35, 0x8b, 0xeb, 0xd4, 0x38, + 0xe2, 0x00, 0x00, 0x00, 0x42, 0xe3, 0x00, 0x00, + 0x00, 0x04, 0xe4, 0x00, 0x00, 0x00, 0x24, 0x01, + 0x00, 0x0e, 0x38, 0xe2, 0x00, 0x00, 0x00, 0x42, + 0xe3, 0x00, 0x00, 0x00, 0x04, 0xe5, 0x00, 0x00, + 0x00, 0x65, 0x02, 0x00, 0xbb, 0x0a, 0xee, 0x24, + 0x02, 0x00, 0x0e, 0x06, 0x2e, 0xb2, 0x03, 0x01, + 0x01, 0x22, 0x01, 0x01, 0x00, 0x04, 0x08, 0x1e, + 0x2a, 0x18, 0x1b, 0x08, 0x0d, 0x3b, 0x1b, 0x0c, + 0x07, 0x04, 0x25, 0x08, 0x0c, 0x04, 0x07, 0x10, + 0x43, 0x2c, 0x25, 0x10, 0x25, 0x04, 0x27, 0x6b, + 0x62, 0x00, 0x4d, 0x30, 0x00, }; static JSContext *JS_NewCustomContext(JSRuntime *rt) @@ -34,8 +50,8 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt) if (!ctx) return NULL; { - extern JSModuleDef *js_init_module_fib(JSContext *ctx, const char *name); - js_init_module_fib(ctx, "examples/fib.so"); + extern JSModuleDef *js_init_module_os(JSContext *ctx, const char *name); + js_init_module_os(ctx, "os"); } return ctx; } From e3b79ba38f6faef697ab44993ebc40af292f9e09 Mon Sep 17 00:00:00 2001 From: zeromake Date: Mon, 9 Sep 2024 18:28:58 +0800 Subject: [PATCH 15/18] fix: del BUILD_QJS_LIBC=ON --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 36a159470..38efba2ca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -192,7 +192,7 @@ jobs: - uses: actions/checkout@v4 - name: build run: | - cmake -B build -D BUILD_EXAMPLES=ON -D BUILD_QJS_LIBC=ON -G "Visual Studio 17 2022" -A ${{matrix.arch}} + cmake -B build -D BUILD_EXAMPLES=ON -G "Visual Studio 17 2022" -A ${{matrix.arch}} cmake --build build --config ${{matrix.buildType}} --target qjs_exe cmake --build build --config ${{matrix.buildType}} --target function_source cmake --build build --config ${{matrix.buildType}} --target fib @@ -227,7 +227,7 @@ jobs: - uses: actions/checkout@v4 - name: build run: | - cmake -B build -D BUILD_EXAMPLES=ON -D BUILD_QJS_LIBC=ON -G "Visual Studio 17 2022" -T ClangCL + cmake -B build -D BUILD_EXAMPLES=ON -G "Visual Studio 17 2022" -T ClangCL cmake --build build --config ${{matrix.buildType}} --target qjs_exe cmake --build build --config ${{matrix.buildType}} --target function_source cmake --build build --config ${{matrix.buildType}} --target fib @@ -266,7 +266,7 @@ jobs: ninja.exe --version - name: build run: | - cmake -B build -D BUILD_EXAMPLES=ON -D BUILD_QJS_LIBC=ON -DCMAKE_BUILD_TYPE=${{matrix.buildType}} -G "Ninja" + cmake -B build -D BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=${{matrix.buildType}} -G "Ninja" cmake --build build --target qjs_exe cmake --build build --target function_source cmake --build build --target fib From a9239349a095ac3b074ab0cf2ba5f897648f1f25 Mon Sep 17 00:00:00 2001 From: zeromake Date: Tue, 10 Sep 2024 09:41:26 +0800 Subject: [PATCH 16/18] fix: code view change --- .github/workflows/ci.yml | 6 +++--- CMakeLists.txt | 4 ++-- examples/fib.c | 8 -------- examples/point.c | 8 -------- quickjs-libc.c | 4 +--- quickjs.h | 5 ++++- 6 files changed, 10 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 38efba2ca..36f7dbc28 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -192,7 +192,7 @@ jobs: - uses: actions/checkout@v4 - name: build run: | - cmake -B build -D BUILD_EXAMPLES=ON -G "Visual Studio 17 2022" -A ${{matrix.arch}} + cmake -B build -DBUILD_EXAMPLES=ON -G "Visual Studio 17 2022" -A ${{matrix.arch}} cmake --build build --config ${{matrix.buildType}} --target qjs_exe cmake --build build --config ${{matrix.buildType}} --target function_source cmake --build build --config ${{matrix.buildType}} --target fib @@ -227,7 +227,7 @@ jobs: - uses: actions/checkout@v4 - name: build run: | - cmake -B build -D BUILD_EXAMPLES=ON -G "Visual Studio 17 2022" -T ClangCL + cmake -B build -DBUILD_EXAMPLES=ON -G "Visual Studio 17 2022" -T ClangCL cmake --build build --config ${{matrix.buildType}} --target qjs_exe cmake --build build --config ${{matrix.buildType}} --target function_source cmake --build build --config ${{matrix.buildType}} --target fib @@ -266,7 +266,7 @@ jobs: ninja.exe --version - name: build run: | - cmake -B build -D BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=${{matrix.buildType}} -G "Ninja" + cmake -B build -DBUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=${{matrix.buildType}} -G "Ninja" cmake --build build --target qjs_exe cmake --build build --target function_source cmake --build build --target fib diff --git a/CMakeLists.txt b/CMakeLists.txt index 31aa9fbf3..faa31aeb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -294,7 +294,7 @@ if(BUILD_EXAMPLES) ) target_compile_definitions(fib PRIVATE JS_SHARED_LIBRARY) if(WIN32) - target_link_libraries(fib ${qjs_libs}) + target_link_libraries(fib qjs) endif() if(APPLE) target_link_options(fib PRIVATE -undefined dynamic_lookup) @@ -307,7 +307,7 @@ if(BUILD_EXAMPLES) ) target_compile_definitions(point PRIVATE JS_SHARED_LIBRARY) if(WIN32) - target_link_libraries(point ${qjs_libs}) + target_link_libraries(point qjs) endif() if(APPLE) target_link_options(point PRIVATE -undefined dynamic_lookup) diff --git a/examples/fib.c b/examples/fib.c index b965acc5b..f67eebfa4 100644 --- a/examples/fib.c +++ b/examples/fib.c @@ -61,14 +61,6 @@ static int js_fib_init(JSContext *ctx, JSModuleDef *m) #define JS_INIT_MODULE js_init_module_fib #endif -#ifndef JS_EXTERN -#ifdef _WIN32 -#define JS_EXTERN __declspec(dllexport) -#else -#define JS_EXTERN -#endif -#endif - JS_EXTERN JSModuleDef *JS_INIT_MODULE(JSContext *ctx, const char *module_name) { JSModuleDef *m; diff --git a/examples/point.c b/examples/point.c index a82df1519..b2f18d6de 100644 --- a/examples/point.c +++ b/examples/point.c @@ -141,14 +141,6 @@ static int js_point_init(JSContext *ctx, JSModuleDef *m) return 0; } -#ifndef JS_EXTERN -#ifdef _WIN32 -#define JS_EXTERN __declspec(dllexport) -#else -#define JS_EXTERN -#endif -#endif - JS_EXTERN JSModuleDef *js_init_module(JSContext *ctx, const char *module_name) { JSModuleDef *m; diff --git a/quickjs-libc.c b/quickjs-libc.c index e1d909546..e6afd7661 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -94,12 +94,10 @@ extern char **environ; #include "list.h" #include "quickjs-libc.h" -#ifdef _WIN32 #ifndef QJS_NATIVE_MODULE_SUFFIX +#ifdef _WIN32 #define QJS_NATIVE_MODULE_SUFFIX ".dll" -#endif #else -#ifndef QJS_NATIVE_MODULE_SUFFIX #define QJS_NATIVE_MODULE_SUFFIX ".so" #endif #endif diff --git a/quickjs.h b/quickjs.h index 9fb40a7f6..8fb391fd7 100644 --- a/quickjs.h +++ b/quickjs.h @@ -40,6 +40,10 @@ extern "C" { #define js_force_inline inline __attribute__((always_inline)) #define __js_printf_like(f, a) __attribute__((format(printf, f, a))) #define JS_EXTERN __attribute__((visibility("default"))) +#elif defined(_WIN32) +#define js_force_inline inline +#define __js_printf_like(a, b) +#define JS_EXTERN __declspec(dllexport) #else #define js_force_inline inline #define __js_printf_like(a, b) @@ -1023,7 +1027,6 @@ JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m, JS_EXTERN const char* JS_GetVersion(void); -#undef JS_EXTERN #undef js_force_inline #undef __js_printf_like From 4727d01b56633d61d7b1fae016c3cef35c144dea Mon Sep 17 00:00:00 2001 From: zeromake Date: Tue, 10 Sep 2024 15:58:09 +0800 Subject: [PATCH 17/18] fix: target_link_libraries use ${qjs_libs} --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37b52cfe4..671b9b813 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -297,7 +297,7 @@ if(BUILD_EXAMPLES) ) target_compile_definitions(fib PRIVATE JS_SHARED_LIBRARY) if(WIN32) - target_link_libraries(fib qjs) + target_link_libraries(fib ${qjs_libs}) endif() if(APPLE) target_link_options(fib PRIVATE -undefined dynamic_lookup) @@ -310,7 +310,7 @@ if(BUILD_EXAMPLES) ) target_compile_definitions(point PRIVATE JS_SHARED_LIBRARY) if(WIN32) - target_link_libraries(point qjs) + target_link_libraries(point ${qjs_libs}) endif() if(APPLE) target_link_options(point PRIVATE -undefined dynamic_lookup) From 6094d3d4a10c1f4ef90b7b58b13cfe59f70a7fad Mon Sep 17 00:00:00 2001 From: zeromake Date: Tue, 10 Sep 2024 22:16:43 +0800 Subject: [PATCH 18/18] fix: examples use self JS_EXTERN --- examples/fib.c | 8 ++++++++ examples/point.c | 8 ++++++++ quickjs.h | 5 +---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/fib.c b/examples/fib.c index f67eebfa4..b965acc5b 100644 --- a/examples/fib.c +++ b/examples/fib.c @@ -61,6 +61,14 @@ static int js_fib_init(JSContext *ctx, JSModuleDef *m) #define JS_INIT_MODULE js_init_module_fib #endif +#ifndef JS_EXTERN +#ifdef _WIN32 +#define JS_EXTERN __declspec(dllexport) +#else +#define JS_EXTERN +#endif +#endif + JS_EXTERN JSModuleDef *JS_INIT_MODULE(JSContext *ctx, const char *module_name) { JSModuleDef *m; diff --git a/examples/point.c b/examples/point.c index b2f18d6de..a82df1519 100644 --- a/examples/point.c +++ b/examples/point.c @@ -141,6 +141,14 @@ static int js_point_init(JSContext *ctx, JSModuleDef *m) return 0; } +#ifndef JS_EXTERN +#ifdef _WIN32 +#define JS_EXTERN __declspec(dllexport) +#else +#define JS_EXTERN +#endif +#endif + JS_EXTERN JSModuleDef *js_init_module(JSContext *ctx, const char *module_name) { JSModuleDef *m; diff --git a/quickjs.h b/quickjs.h index 8fb391fd7..9fb40a7f6 100644 --- a/quickjs.h +++ b/quickjs.h @@ -40,10 +40,6 @@ extern "C" { #define js_force_inline inline __attribute__((always_inline)) #define __js_printf_like(f, a) __attribute__((format(printf, f, a))) #define JS_EXTERN __attribute__((visibility("default"))) -#elif defined(_WIN32) -#define js_force_inline inline -#define __js_printf_like(a, b) -#define JS_EXTERN __declspec(dllexport) #else #define js_force_inline inline #define __js_printf_like(a, b) @@ -1027,6 +1023,7 @@ JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m, JS_EXTERN const char* JS_GetVersion(void); +#undef JS_EXTERN #undef js_force_inline #undef __js_printf_like