Skip to content

Commit

Permalink
src: add support for externally shared js builtins
Browse files Browse the repository at this point in the history
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 <mdawson@devrus.com>
  • Loading branch information
mhdawson committed Aug 24, 2022
1 parent 7900f65 commit 85ad117
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
28 changes: 27 additions & 1 deletion configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
with open ('tools/icu/icu_versions.json') as f:
icu_versions = json.load(f)

sharable_builtins = {'cjs_module_lexer/lexer': 'deps/cjs-module-lexer/lexer.js',
'cjs_module_lexer/dist/lexer': 'deps/cjs-module-lexer/dist/lexer.js',
'undici/undici': 'deps/undici/undici.js'
}

# create option groups
shared_optgroup = parser.add_argument_group("Shared libraries",
"Flags that allows you to control whether you want to build against "
Expand All @@ -70,6 +75,9 @@
"library you want to build against.")
http2_optgroup = parser.add_argument_group("HTTP2",
"Flags that allows you to control HTTP2 features in Node.js")
shared_builtin_optgroup = parser.add_argument_group("Shared builtins",
"Flags that allows you to control whether you want to build against "
"internal builtins or shared files.")

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

parser.add_argument_group(shared_optgroup)

for builtin in sharable_builtins:
builtin_id = 'shared_builtin_' + builtin + '_path'
shared_builtin_optgroup.add_argument('--shared-builtin-' + builtin + '-path',
action='store',
dest='node_shared_builtin_' + builtin.replace('/', '_') + '_path',
help='Path to shared file for ' + builtin + ' builtin. '
'Will be used instead of bundled version at runtime')

parser.add_argument_group(shared_builtin_optgroup)

static_optgroup.add_argument('--static-zoslib-gyp',
action='store',
dest='static_zoslib_gyp',
Expand Down Expand Up @@ -1400,7 +1418,6 @@ def configure_library(lib, output, pkgname=None):
elif pkg_libs:
output['libraries'] += pkg_libs.split()


def configure_v8(o):
o['variables']['v8_enable_webassembly'] = 1
o['variables']['v8_enable_javascript_promise_hooks'] = 1
Expand Down Expand Up @@ -1951,6 +1968,15 @@ def make_bin_override():
configure_inspector(output)
configure_section_file(output)

# configure sharable builtins
output['variables']['node_builtin_sharable_builtins'] = []
for builtin in sharable_builtins:
builtin_id = 'node_shared_builtin_' + builtin.replace('/', '_') + '_path'
if getattr(options, builtin_id):
output['defines'] += [builtin_id.upper() + '=' + getattr(options, builtin_id)]
else:
output['variables']['node_builtin_sharable_builtins'] += [sharable_builtins[builtin]]

# Forward OSS-Fuzz settings
output['variables']['ossfuzz'] = b(options.ossfuzz)

Expand Down
4 changes: 1 addition & 3 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@
'deps/v8/tools/tickprocessor-driver.mjs',
'deps/acorn/acorn/dist/acorn.js',
'deps/acorn/acorn-walk/dist/walk.js',
'deps/cjs-module-lexer/lexer.js',
'deps/cjs-module-lexer/dist/lexer.js',
'deps/undici/undici.js',
'<@(node_builtin_sharable_builtins)',
],
'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)',
'conditions': [
Expand Down
46 changes: 37 additions & 9 deletions src/node_builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,47 @@ static std::string OnDiskFileName(const char* id) {

MaybeLocal<String> BuiltinLoader::LoadBuiltinSource(Isolate* isolate,
const char* id) {
std::string filename;
#ifdef NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH
#define NODE_CHECK_FILE_NAME
if (strncmp(id, "cjs_module_lexer/lexer", strlen(id)) == 0 ) {
filename = STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH);
}
#endif // NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH

#ifdef NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH
#define NODE_CHECK_FILE_NAME
if (strncmp(id, "cjs_module_lexer/dist/lexer", strlen(id)) == 0 ) {
filename = STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH);
}
#endif // NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH

#ifdef NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH
#define NODE_CHECK_FILE_NAME
if (strncmp(id, "undici/undici", strlen(id)) == 0 ) {
filename = STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH);
}
#endif // NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH

#ifdef NODE_CHECK_FILE_NAME
if (filename.empty()) {
#endif // NODE_CHECK_FILE_NAME
#ifdef NODE_BUILTIN_MODULES_PATH
if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
#endif // NODE_BUILTIN_MODULES_PATH
const auto source_it = source_.find(id);
if (UNLIKELY(source_it == source_.end())) {
fprintf(stderr, "Cannot find native builtin: \"%s\".\n", id);
ABORT();
}
return source_it->second.ToStringChecked(isolate);
const auto source_it = source_.find(id);
if (UNLIKELY(source_it == source_.end())) {
fprintf(stderr, "Cannot find native builtin: \"%s\".\n", id);
ABORT();
}
return source_it->second.ToStringChecked(isolate);
#ifdef NODE_BUILTIN_MODULES_PATH
}
filename = OnDiskFileName(id);
#endif // NODE_BUILTIN_MODULES_PATH
#ifdef NODE_CHECK_FILE_NAME
}
std::string filename = OnDiskFileName(id);
#endif // NODE_CHECK_FILE_NAME

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

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

0 comments on commit 85ad117

Please sign in to comment.