Skip to content

Commit

Permalink
tools: port js2c.py to C++
Browse files Browse the repository at this point in the history
This makes it easier to use third-party dependencies in this tool
(e.g. adding compression using algorithms not available in Python).
It is also much faster - locally js2c.py takes ~1.5s to generate the
output whereas this version takes ~0.1s - and consumes less memory
(~110MB v.s. 66MB).

This also modifies the js2c.py a bit to simplify the output, making
it easier to compare with one generated by the C++ version. Locally
the output from the two are identical. We'll remove js2c.py in a
subsequent commit when the C++ version is used by default.

PR-URL: #46997
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
  • Loading branch information
joyeecheung authored and targos committed May 30, 2023
1 parent 4d4e506 commit 5325be1
Show file tree
Hide file tree
Showing 5 changed files with 857 additions and 18 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@ LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
test/fixtures/*.c \
test/js-native-api/*/*.cc \
test/node-api/*/*.cc \
tools/js2c.cc \
tools/icu/*.cc \
tools/icu/*.h \
tools/code_cache/*.cc \
Expand Down
29 changes: 29 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,35 @@
}],
]
}, # overlapped-checker
{
'target_name': 'node_js2c',
'type': 'executable',
'dependencies': [
'deps/simdutf/simdutf.gyp:simdutf',
],
'include_dirs': [
'tools'
],
'sources': [
'tools/js2c.cc',
'tools/executable_wrapper.h'
],
'conditions': [
[ 'node_shared_libuv=="false"', {
'dependencies': [ 'deps/uv/uv.gyp:libuv' ],
}],
[ 'debug_node=="true"', {
'cflags!': [ '-O3' ],
'cflags': [ '-g', '-O0' ],
'defines': [ 'DEBUG' ],
'xcode_settings': {
'OTHER_CFLAGS': [
'-g', '-O0'
],
},
}],
]
},
{
'target_name': 'node_mksnapshot',
'type': 'executable',
Expand Down
55 changes: 55 additions & 0 deletions tools/executable_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef TOOLS_EXECUTABLE_WRAPPER_H_
#define TOOLS_EXECUTABLE_WRAPPER_H_

// TODO(joyeecheung): reuse this in mksnapshot.
#include "uv.h"
#ifdef _WIN32
#include <windows.h>
#endif

namespace node {
#ifdef _WIN32
using argv_type = wchar_t*;
#define NODE_MAIN int wmain

void FixupMain(int argc, argv_type raw_argv[], char*** argv) {
// Convert argv to UTF8.
*argv = new char*[argc + 1];
for (int i = 0; i < argc; i++) {
// Compute the size of the required buffer
DWORD size = WideCharToMultiByte(
CP_UTF8, 0, raw_argv[i], -1, nullptr, 0, nullptr, nullptr);
if (size == 0) {
// This should never happen.
fprintf(stderr, "Could not convert arguments to utf8.");
exit(1);
}
// Do the actual conversion
(*argv)[i] = new char[size];
DWORD result = WideCharToMultiByte(
CP_UTF8, 0, raw_argv[i], -1, (*argv)[i], size, nullptr, nullptr);
if (result == 0) {
// This should never happen.
fprintf(stderr, "Could not convert arguments to utf8.");
exit(1);
}
}
(*argv)[argc] = nullptr;
}
#else

using argv_type = char*;
#define NODE_MAIN int main

void FixupMain(int argc, argv_type raw_argv[], char*** argv) {
*argv = uv_setup_args(argc, raw_argv);
// Disable stdio buffering, it interacts poorly with printf()
// calls elsewhere in the program (e.g., any logging from V8.)
setvbuf(stdout, nullptr, _IONBF, 0);
setvbuf(stderr, nullptr, _IONBF, 0);
}
#endif

} // namespace node

#endif // TOOLS_EXECUTABLE_WRAPPER_H_
Loading

0 comments on commit 5325be1

Please sign in to comment.