Skip to content

Commit

Permalink
test,doc: enable running embedtest for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoroz committed Apr 22, 2024
1 parent 01c281f commit 39a78d8
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,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 \
Expand Down
2 changes: 1 addition & 1 deletion doc/api/embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@
'sources': [
'src/node_snapshot_stub.cc',
'test/embedding/embedtest.cc',
'test/embedding/utf8_args.c',
],

'conditions': [
Expand Down
11 changes: 8 additions & 3 deletions test/embedding/embedtest.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include "node.h"
#include "utf8_args.h"
#include "uv.h"
#include <assert.h>

#include <algorithm>

Expand All @@ -28,6 +29,8 @@ static int RunNodeInstance(MultiIsolatePlatform* platform,
const std::vector<std::string>& exec_args);

int main(int argc, char** argv) {
GetUtf8CommandLineArgs(&argc, &argv);

argv = uv_setup_args(argc, argv);
std::vector<std::string> args(argv, argv + argc);
std::unique_ptr<node::InitializationResult> result =
Expand All @@ -54,6 +57,8 @@ int main(int argc, char** argv) {
V8::DisposePlatform();

node::TearDownOncePerProcess();

FreeUtf8CommandLineArgs(argc, argv);
return ret;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions test/embedding/utf8_args.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "utf8_args.h"

#ifdef _WIN32

#include <malloc.h>
#include <shellapi.h>
#include <windows.h>

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
20 changes: 20 additions & 0 deletions test/embedding/utf8_args.h
Original file line number Diff line number Diff line change
@@ -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_
4 changes: 4 additions & 0 deletions vcbuild.bat
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,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
Expand Down Expand Up @@ -633,6 +634,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%
Expand Down

0 comments on commit 39a78d8

Please sign in to comment.