Skip to content

Commit 85ad117

Browse files
committed
src: add support for externally shared js builtins
Refs: nodejs#44000 - add infra to support externally shared js builtins in support of distos that want to externalize deps that include JS/WASM instead of native code - add support for externalizing - cjs_module_lexer/lexer - cjs_module_lexer/dist/lexer - undici/undici Signed-off-by: Michael Dawson <[email protected]>
1 parent 7900f65 commit 85ad117

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed

configure.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
with open ('tools/icu/icu_versions.json') as f:
5858
icu_versions = json.load(f)
5959

60+
sharable_builtins = {'cjs_module_lexer/lexer': 'deps/cjs-module-lexer/lexer.js',
61+
'cjs_module_lexer/dist/lexer': 'deps/cjs-module-lexer/dist/lexer.js',
62+
'undici/undici': 'deps/undici/undici.js'
63+
}
64+
6065
# create option groups
6166
shared_optgroup = parser.add_argument_group("Shared libraries",
6267
"Flags that allows you to control whether you want to build against "
@@ -70,6 +75,9 @@
7075
"library you want to build against.")
7176
http2_optgroup = parser.add_argument_group("HTTP2",
7277
"Flags that allows you to control HTTP2 features in Node.js")
78+
shared_builtin_optgroup = parser.add_argument_group("Shared builtins",
79+
"Flags that allows you to control whether you want to build against "
80+
"internal builtins or shared files.")
7381

7482
# Options should be in alphabetical order but keep --prefix at the top,
7583
# that's arguably the one people will be looking for most.
@@ -422,6 +430,16 @@
422430

423431
parser.add_argument_group(shared_optgroup)
424432

433+
for builtin in sharable_builtins:
434+
builtin_id = 'shared_builtin_' + builtin + '_path'
435+
shared_builtin_optgroup.add_argument('--shared-builtin-' + builtin + '-path',
436+
action='store',
437+
dest='node_shared_builtin_' + builtin.replace('/', '_') + '_path',
438+
help='Path to shared file for ' + builtin + ' builtin. '
439+
'Will be used instead of bundled version at runtime')
440+
441+
parser.add_argument_group(shared_builtin_optgroup)
442+
425443
static_optgroup.add_argument('--static-zoslib-gyp',
426444
action='store',
427445
dest='static_zoslib_gyp',
@@ -1400,7 +1418,6 @@ def configure_library(lib, output, pkgname=None):
14001418
elif pkg_libs:
14011419
output['libraries'] += pkg_libs.split()
14021420

1403-
14041421
def configure_v8(o):
14051422
o['variables']['v8_enable_webassembly'] = 1
14061423
o['variables']['v8_enable_javascript_promise_hooks'] = 1
@@ -1951,6 +1968,15 @@ def make_bin_override():
19511968
configure_inspector(output)
19521969
configure_section_file(output)
19531970

1971+
# configure sharable builtins
1972+
output['variables']['node_builtin_sharable_builtins'] = []
1973+
for builtin in sharable_builtins:
1974+
builtin_id = 'node_shared_builtin_' + builtin.replace('/', '_') + '_path'
1975+
if getattr(options, builtin_id):
1976+
output['defines'] += [builtin_id.upper() + '=' + getattr(options, builtin_id)]
1977+
else:
1978+
output['variables']['node_builtin_sharable_builtins'] += [sharable_builtins[builtin]]
1979+
19541980
# Forward OSS-Fuzz settings
19551981
output['variables']['ossfuzz'] = b(options.ossfuzz)
19561982

node.gyp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@
4848
'deps/v8/tools/tickprocessor-driver.mjs',
4949
'deps/acorn/acorn/dist/acorn.js',
5050
'deps/acorn/acorn-walk/dist/walk.js',
51-
'deps/cjs-module-lexer/lexer.js',
52-
'deps/cjs-module-lexer/dist/lexer.js',
53-
'deps/undici/undici.js',
51+
'<@(node_builtin_sharable_builtins)',
5452
],
5553
'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)',
5654
'conditions': [

src/node_builtins.cc

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,47 @@ static std::string OnDiskFileName(const char* id) {
190190

191191
MaybeLocal<String> BuiltinLoader::LoadBuiltinSource(Isolate* isolate,
192192
const char* id) {
193+
std::string filename;
194+
#ifdef NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH
195+
#define NODE_CHECK_FILE_NAME
196+
if (strncmp(id, "cjs_module_lexer/lexer", strlen(id)) == 0 ) {
197+
filename = STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH);
198+
}
199+
#endif // NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH
200+
201+
#ifdef NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH
202+
#define NODE_CHECK_FILE_NAME
203+
if (strncmp(id, "cjs_module_lexer/dist/lexer", strlen(id)) == 0 ) {
204+
filename = STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH);
205+
}
206+
#endif // NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH
207+
208+
#ifdef NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH
209+
#define NODE_CHECK_FILE_NAME
210+
if (strncmp(id, "undici/undici", strlen(id)) == 0 ) {
211+
filename = STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH);
212+
}
213+
#endif // NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH
214+
215+
#ifdef NODE_CHECK_FILE_NAME
216+
if (filename.empty()) {
217+
#endif // NODE_CHECK_FILE_NAME
193218
#ifdef NODE_BUILTIN_MODULES_PATH
194-
if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
219+
if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
195220
#endif // NODE_BUILTIN_MODULES_PATH
196-
const auto source_it = source_.find(id);
197-
if (UNLIKELY(source_it == source_.end())) {
198-
fprintf(stderr, "Cannot find native builtin: \"%s\".\n", id);
199-
ABORT();
200-
}
201-
return source_it->second.ToStringChecked(isolate);
221+
const auto source_it = source_.find(id);
222+
if (UNLIKELY(source_it == source_.end())) {
223+
fprintf(stderr, "Cannot find native builtin: \"%s\".\n", id);
224+
ABORT();
225+
}
226+
return source_it->second.ToStringChecked(isolate);
202227
#ifdef NODE_BUILTIN_MODULES_PATH
228+
}
229+
filename = OnDiskFileName(id);
230+
#endif // NODE_BUILTIN_MODULES_PATH
231+
#ifdef NODE_CHECK_FILE_NAME
203232
}
204-
std::string filename = OnDiskFileName(id);
233+
#endif // NODE_CHECK_FILE_NAME
205234

206235
std::string contents;
207236
int r = ReadFileSync(&contents, filename.c_str());
@@ -216,7 +245,6 @@ MaybeLocal<String> BuiltinLoader::LoadBuiltinSource(Isolate* isolate,
216245
}
217246
return String::NewFromUtf8(
218247
isolate, contents.c_str(), v8::NewStringType::kNormal, contents.length());
219-
#endif // NODE_BUILTIN_MODULES_PATH
220248
}
221249

222250
// Returns Local<Function> of the compiled module if return_code_cache

0 commit comments

Comments
 (0)