From 810da56440b67f28d688e1fba166bf8be83958f4 Mon Sep 17 00:00:00 2001 From: Vladimir Morozov Date: Mon, 22 Apr 2024 10:12:59 -0700 Subject: [PATCH 1/2] test,doc: enable running embedtest for Windows --- Makefile | 1 + doc/api/embedding.md | 2 +- node.gyp | 1 + test/embedding/embedtest.cc | 11 ++++++--- test/embedding/utf8_args.c | 47 +++++++++++++++++++++++++++++++++++++ test/embedding/utf8_args.h | 20 ++++++++++++++++ vcbuild.bat | 4 ++++ 7 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 test/embedding/utf8_args.c create mode 100644 test/embedding/utf8_args.h diff --git a/Makefile b/Makefile index f2009fff0c2f2b..670dbd8bd26173 100644 --- a/Makefile +++ b/Makefile @@ -1450,6 +1450,7 @@ FORMAT_CPP_FILES += $(LINT_CPP_FILES) # C source codes. FORMAT_CPP_FILES += $(wildcard \ benchmark/napi/*/*.c \ + test/embedding/*.c \ test/js-native-api/*.h \ test/js-native-api/*/*.c \ test/js-native-api/*/*.h \ diff --git a/doc/api/embedding.md b/doc/api/embedding.md index 9f831b342c2705..d4ae090c255f97 100644 --- a/doc/api/embedding.md +++ b/doc/api/embedding.md @@ -90,7 +90,7 @@ to as `node::Environment`. Each `node::Environment` is associated with: * A number of `v8::Context`s, but exactly one main `v8::Context`. * One `node::IsolateData` instance that contains information that could be shared by multiple `node::Environment`s that use the same `v8::Isolate`. - Currently, no testing if performed for this scenario. + Currently, no testing is performed for this scenario. In order to set up a `v8::Isolate`, an `v8::ArrayBuffer::Allocator` needs to be provided. One possible choice is the default Node.js allocator, which diff --git a/node.gyp b/node.gyp index d4f25b3ef2d11a..b028f5145b2472 100644 --- a/node.gyp +++ b/node.gyp @@ -1258,6 +1258,7 @@ 'sources': [ 'src/node_snapshot_stub.cc', 'test/embedding/embedtest.cc', + 'test/embedding/utf8_args.c', ], 'conditions': [ diff --git a/test/embedding/embedtest.cc b/test/embedding/embedtest.cc index 741ee6fcad44f9..9cba6133030682 100644 --- a/test/embedding/embedtest.cc +++ b/test/embedding/embedtest.cc @@ -1,9 +1,10 @@ #ifdef NDEBUG #undef NDEBUG #endif +#include #include "node.h" +#include "utf8_args.h" #include "uv.h" -#include #include @@ -28,6 +29,8 @@ static int RunNodeInstance(MultiIsolatePlatform* platform, const std::vector& exec_args); int main(int argc, char** argv) { + GetUtf8CommandLineArgs(&argc, &argv); + argv = uv_setup_args(argc, argv); std::vector args(argv, argv + argc); std::shared_ptr result = @@ -54,6 +57,8 @@ int main(int argc, char** argv) { V8::DisposePlatform(); node::TearDownOncePerProcess(); + + FreeUtf8CommandLineArgs(argc, argv); return ret; } @@ -106,7 +111,7 @@ int RunNodeInstance(MultiIsolatePlatform* platform, } if (!snapshot_blob_path.empty() && !is_building_snapshot) { - FILE* fp = fopen(snapshot_blob_path.c_str(), "r"); + FILE* fp = fopen(snapshot_blob_path.c_str(), "rb"); assert(fp != nullptr); if (snapshot_as_file) { snapshot = node::EmbedderSnapshotData::FromFile(fp); @@ -204,7 +209,7 @@ int RunNodeInstance(MultiIsolatePlatform* platform, snapshot = setup->CreateSnapshot(); assert(snapshot); - FILE* fp = fopen(snapshot_blob_path.c_str(), "w"); + FILE* fp = fopen(snapshot_blob_path.c_str(), "wb"); assert(fp != nullptr); if (snapshot_as_file) { snapshot->ToFile(fp); diff --git a/test/embedding/utf8_args.c b/test/embedding/utf8_args.c new file mode 100644 index 00000000000000..ef0004a042d2ca --- /dev/null +++ b/test/embedding/utf8_args.c @@ -0,0 +1,47 @@ +#include "utf8_args.h" + +#ifdef _WIN32 + +#include +#include +#include + +void GetUtf8CommandLineArgs(int* argc, char*** argv) { + int wargc; + wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &wargc); + if (wargv == NULL) { + *argc = 0; + *argv = NULL; + return; + } + + *argc = wargc; + *argv = (char**)malloc(wargc * sizeof(char*)); + for (int i = 0; i < wargc; ++i) { + int len = + WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL); + (*argv)[i] = malloc(len); + WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, (*argv)[i], len, NULL, NULL); + } + + LocalFree(wargv); +} + +void FreeUtf8CommandLineArgs(int argc, char** argv) { + for (int i = 0; i < argc; ++i) { + free(argv[i]); + } + free(argv); +} + +#else + +void GetUtf8CommandLineArgs(int* /*argc*/, char*** /*argv*/) { + // Do nothing on non-Windows platforms. +} + +void FreeUtf8CommandLineArgs(int /*argc*/, char** /*argv*/) { + // Do nothing on non-Windows platforms. +} + +#endif diff --git a/test/embedding/utf8_args.h b/test/embedding/utf8_args.h new file mode 100644 index 00000000000000..53cd6d18dcd255 --- /dev/null +++ b/test/embedding/utf8_args.h @@ -0,0 +1,20 @@ +#ifndef TEST_EMBEDDING_UTF8_ARGS_H_ +#define TEST_EMBEDDING_UTF8_ARGS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +// Windows does not support UTF-8 command line arguments. +// We must get them using an alternative way. +// This function does nothing on non-Windows platforms. +void GetUtf8CommandLineArgs(int* argc, char*** argv); + +// Free the memory allocated by GetUtf8CommandLineArgs. +void FreeUtf8CommandLineArgs(int argc, char** argv); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // TEST_EMBEDDING_UTF8_ARGS_H_ diff --git a/vcbuild.bat b/vcbuild.bat index d618930059646c..8a72e50cb494fe 100644 --- a/vcbuild.bat +++ b/vcbuild.bat @@ -333,6 +333,7 @@ if "%target%"=="Build" ( if defined cctest set target="Build" ) if "%target%"=="node" if exist "%config%\cctest.exe" del "%config%\cctest.exe" +if "%target%"=="node" if exist "%config%\embedtest.exe" del "%config%\embedtest.exe" if defined msbuild_args set "extra_msbuild_args=%extra_msbuild_args% %msbuild_args%" @rem Setup env variables to use multiprocessor build set UseMultiToolTask=True @@ -675,6 +676,9 @@ if not exist "%config%\cctest.exe" echo cctest.exe not found. Run "vcbuild test" echo running 'cctest %cctest_args%' "%config%\cctest" %cctest_args% if %errorlevel% neq 0 set exit_code=%errorlevel% +echo running '%node_exe% test\embedding\test-embedding.js' +"%node_exe%" test\embedding\test-embedding.js +if %errorlevel% neq 0 set exit_code=%errorlevel% :run-test-py echo running 'python tools\test.py %test_args%' python tools\test.py %test_args% From 91b5225d7946e4f858803cd3036e6301c4ce4ba9 Mon Sep 17 00:00:00 2001 From: Vladimir Morozov Date: Tue, 23 Apr 2024 14:30:34 -0700 Subject: [PATCH 2/2] use existing executable_wrapper.h --- Makefile | 1 - node.gyp | 2 +- test/embedding/embedtest.cc | 11 ++++----- test/embedding/utf8_args.c | 47 ------------------------------------- test/embedding/utf8_args.h | 20 ---------------- 5 files changed, 5 insertions(+), 76 deletions(-) delete mode 100644 test/embedding/utf8_args.c delete mode 100644 test/embedding/utf8_args.h diff --git a/Makefile b/Makefile index 670dbd8bd26173..f2009fff0c2f2b 100644 --- a/Makefile +++ b/Makefile @@ -1450,7 +1450,6 @@ FORMAT_CPP_FILES += $(LINT_CPP_FILES) # C source codes. FORMAT_CPP_FILES += $(wildcard \ benchmark/napi/*/*.c \ - test/embedding/*.c \ test/js-native-api/*.h \ test/js-native-api/*/*.c \ test/js-native-api/*/*.h \ diff --git a/node.gyp b/node.gyp index b028f5145b2472..8478201831c997 100644 --- a/node.gyp +++ b/node.gyp @@ -1248,6 +1248,7 @@ 'include_dirs': [ 'src', + 'tools', 'tools/msvs/genfiles', 'deps/v8/include', 'deps/cares/include', @@ -1258,7 +1259,6 @@ 'sources': [ 'src/node_snapshot_stub.cc', 'test/embedding/embedtest.cc', - 'test/embedding/utf8_args.c', ], 'conditions': [ diff --git a/test/embedding/embedtest.cc b/test/embedding/embedtest.cc index 9cba6133030682..559a31a96af008 100644 --- a/test/embedding/embedtest.cc +++ b/test/embedding/embedtest.cc @@ -2,9 +2,8 @@ #undef NDEBUG #endif #include +#include "executable_wrapper.h" #include "node.h" -#include "utf8_args.h" -#include "uv.h" #include @@ -28,10 +27,10 @@ static int RunNodeInstance(MultiIsolatePlatform* platform, const std::vector& args, const std::vector& exec_args); -int main(int argc, char** argv) { - GetUtf8CommandLineArgs(&argc, &argv); +NODE_MAIN(int argc, node::argv_type raw_argv[]) { + char** argv = nullptr; + node::FixupMain(argc, raw_argv, &argv); - argv = uv_setup_args(argc, argv); std::vector args(argv, argv + argc); std::shared_ptr result = node::InitializeOncePerProcess( @@ -57,8 +56,6 @@ int main(int argc, char** argv) { V8::DisposePlatform(); node::TearDownOncePerProcess(); - - FreeUtf8CommandLineArgs(argc, argv); return ret; } diff --git a/test/embedding/utf8_args.c b/test/embedding/utf8_args.c deleted file mode 100644 index ef0004a042d2ca..00000000000000 --- a/test/embedding/utf8_args.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "utf8_args.h" - -#ifdef _WIN32 - -#include -#include -#include - -void GetUtf8CommandLineArgs(int* argc, char*** argv) { - int wargc; - wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &wargc); - if (wargv == NULL) { - *argc = 0; - *argv = NULL; - return; - } - - *argc = wargc; - *argv = (char**)malloc(wargc * sizeof(char*)); - for (int i = 0; i < wargc; ++i) { - int len = - WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL); - (*argv)[i] = malloc(len); - WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, (*argv)[i], len, NULL, NULL); - } - - LocalFree(wargv); -} - -void FreeUtf8CommandLineArgs(int argc, char** argv) { - for (int i = 0; i < argc; ++i) { - free(argv[i]); - } - free(argv); -} - -#else - -void GetUtf8CommandLineArgs(int* /*argc*/, char*** /*argv*/) { - // Do nothing on non-Windows platforms. -} - -void FreeUtf8CommandLineArgs(int /*argc*/, char** /*argv*/) { - // Do nothing on non-Windows platforms. -} - -#endif diff --git a/test/embedding/utf8_args.h b/test/embedding/utf8_args.h deleted file mode 100644 index 53cd6d18dcd255..00000000000000 --- a/test/embedding/utf8_args.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef TEST_EMBEDDING_UTF8_ARGS_H_ -#define TEST_EMBEDDING_UTF8_ARGS_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -// Windows does not support UTF-8 command line arguments. -// We must get them using an alternative way. -// This function does nothing on non-Windows platforms. -void GetUtf8CommandLineArgs(int* argc, char*** argv); - -// Free the memory allocated by GetUtf8CommandLineArgs. -void FreeUtf8CommandLineArgs(int argc, char** argv); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TEST_EMBEDDING_UTF8_ARGS_H_