Skip to content

Commit 5325be1

Browse files
joyeecheungtargos
authored andcommitted
tools: port js2c.py to C++
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 <[email protected]>
1 parent 4d4e506 commit 5325be1

File tree

5 files changed

+857
-18
lines changed

5 files changed

+857
-18
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,7 @@ LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
14091409
test/fixtures/*.c \
14101410
test/js-native-api/*/*.cc \
14111411
test/node-api/*/*.cc \
1412+
tools/js2c.cc \
14121413
tools/icu/*.cc \
14131414
tools/icu/*.h \
14141415
tools/code_cache/*.cc \

node.gyp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,35 @@
11761176
}],
11771177
]
11781178
}, # overlapped-checker
1179+
{
1180+
'target_name': 'node_js2c',
1181+
'type': 'executable',
1182+
'dependencies': [
1183+
'deps/simdutf/simdutf.gyp:simdutf',
1184+
],
1185+
'include_dirs': [
1186+
'tools'
1187+
],
1188+
'sources': [
1189+
'tools/js2c.cc',
1190+
'tools/executable_wrapper.h'
1191+
],
1192+
'conditions': [
1193+
[ 'node_shared_libuv=="false"', {
1194+
'dependencies': [ 'deps/uv/uv.gyp:libuv' ],
1195+
}],
1196+
[ 'debug_node=="true"', {
1197+
'cflags!': [ '-O3' ],
1198+
'cflags': [ '-g', '-O0' ],
1199+
'defines': [ 'DEBUG' ],
1200+
'xcode_settings': {
1201+
'OTHER_CFLAGS': [
1202+
'-g', '-O0'
1203+
],
1204+
},
1205+
}],
1206+
]
1207+
},
11791208
{
11801209
'target_name': 'node_mksnapshot',
11811210
'type': 'executable',

tools/executable_wrapper.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef TOOLS_EXECUTABLE_WRAPPER_H_
2+
#define TOOLS_EXECUTABLE_WRAPPER_H_
3+
4+
// TODO(joyeecheung): reuse this in mksnapshot.
5+
#include "uv.h"
6+
#ifdef _WIN32
7+
#include <windows.h>
8+
#endif
9+
10+
namespace node {
11+
#ifdef _WIN32
12+
using argv_type = wchar_t*;
13+
#define NODE_MAIN int wmain
14+
15+
void FixupMain(int argc, argv_type raw_argv[], char*** argv) {
16+
// Convert argv to UTF8.
17+
*argv = new char*[argc + 1];
18+
for (int i = 0; i < argc; i++) {
19+
// Compute the size of the required buffer
20+
DWORD size = WideCharToMultiByte(
21+
CP_UTF8, 0, raw_argv[i], -1, nullptr, 0, nullptr, nullptr);
22+
if (size == 0) {
23+
// This should never happen.
24+
fprintf(stderr, "Could not convert arguments to utf8.");
25+
exit(1);
26+
}
27+
// Do the actual conversion
28+
(*argv)[i] = new char[size];
29+
DWORD result = WideCharToMultiByte(
30+
CP_UTF8, 0, raw_argv[i], -1, (*argv)[i], size, nullptr, nullptr);
31+
if (result == 0) {
32+
// This should never happen.
33+
fprintf(stderr, "Could not convert arguments to utf8.");
34+
exit(1);
35+
}
36+
}
37+
(*argv)[argc] = nullptr;
38+
}
39+
#else
40+
41+
using argv_type = char*;
42+
#define NODE_MAIN int main
43+
44+
void FixupMain(int argc, argv_type raw_argv[], char*** argv) {
45+
*argv = uv_setup_args(argc, raw_argv);
46+
// Disable stdio buffering, it interacts poorly with printf()
47+
// calls elsewhere in the program (e.g., any logging from V8.)
48+
setvbuf(stdout, nullptr, _IONBF, 0);
49+
setvbuf(stderr, nullptr, _IONBF, 0);
50+
}
51+
#endif
52+
53+
} // namespace node
54+
55+
#endif // TOOLS_EXECUTABLE_WRAPPER_H_

0 commit comments

Comments
 (0)