Skip to content

Commit 7dead84

Browse files
committed
src: add LoadEnvironment() variant taking a string
Allow passing a string as the main module rather than using the callback variant. PR-URL: #30467 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent c44edec commit 7dead84

10 files changed

+94
-2
lines changed

src/api/environment.cc

+35-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
422422
}
423423

424424
void LoadEnvironment(Environment* env) {
425-
USE(LoadEnvironment(env, nullptr, {}));
425+
USE(LoadEnvironment(env,
426+
StartExecutionCallback{},
427+
{}));
426428
}
427429

428430
MaybeLocal<Value> LoadEnvironment(
@@ -445,6 +447,38 @@ MaybeLocal<Value> LoadEnvironment(
445447
return StartExecution(env, cb);
446448
}
447449

450+
MaybeLocal<Value> LoadEnvironment(
451+
Environment* env,
452+
const char* main_script_source_utf8,
453+
std::unique_ptr<InspectorParentHandle> inspector_parent_handle) {
454+
CHECK_NOT_NULL(main_script_source_utf8);
455+
return LoadEnvironment(
456+
env,
457+
[&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> {
458+
// This is a slightly hacky way to convert UTF-8 to UTF-16.
459+
Local<String> str =
460+
String::NewFromUtf8(env->isolate(),
461+
main_script_source_utf8,
462+
v8::NewStringType::kNormal).ToLocalChecked();
463+
auto main_utf16 = std::make_unique<String::Value>(env->isolate(), str);
464+
465+
// TODO(addaleax): Avoid having a global table for all scripts.
466+
std::string name = "embedder_main_" + std::to_string(env->thread_id());
467+
native_module::NativeModuleEnv::Add(
468+
name.c_str(),
469+
UnionBytes(**main_utf16, main_utf16->length()));
470+
env->set_main_utf16(std::move(main_utf16));
471+
std::vector<Local<String>> params = {
472+
env->process_string(),
473+
env->require_string()};
474+
std::vector<Local<Value>> args = {
475+
env->process_object(),
476+
env->native_module_require()};
477+
return ExecuteBootstrapper(env, name.c_str(), &params, &args);
478+
},
479+
std::move(inspector_parent_handle));
480+
}
481+
448482
Environment* GetCurrentEnvironment(Local<Context> context) {
449483
return Environment::GetCurrent(context);
450484
}

src/env-inl.h

+5
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,11 @@ int64_t Environment::base_object_count() const {
12661266
return base_object_count_;
12671267
}
12681268

1269+
void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
1270+
CHECK(!main_utf16_);
1271+
main_utf16_ = std::move(str);
1272+
}
1273+
12691274
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
12701275
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
12711276
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)

src/env.h

+7
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,8 @@ class Environment : public MemoryRetainer {
12531253

12541254
#endif // HAVE_INSPECTOR
12551255

1256+
inline void set_main_utf16(std::unique_ptr<v8::String::Value>);
1257+
12561258
private:
12571259
template <typename Fn>
12581260
inline void CreateImmediate(Fn&& cb, bool ref);
@@ -1462,6 +1464,11 @@ class Environment : public MemoryRetainer {
14621464
#undef V
14631465

14641466
v8::Global<v8::Context> context_;
1467+
1468+
// Keeps the main script source alive is one was passed to LoadEnvironment().
1469+
// We should probably find a way to just use plain `v8::String`s created from
1470+
// the source passed to LoadEnvironment() directly instead.
1471+
std::unique_ptr<v8::String::Value> main_utf16_;
14651472
};
14661473

14671474
} // namespace node

src/node.h

+4
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
453453
Environment* env,
454454
StartExecutionCallback cb,
455455
std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {});
456+
NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
457+
Environment* env,
458+
const char* main_script_source_utf8,
459+
std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {});
456460
NODE_EXTERN void FreeEnvironment(Environment* env);
457461

458462
// This may return nullptr if context is not associated with a Node instance.

src/node_native_module.cc

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ bool NativeModuleLoader::Exists(const char* id) {
3333
return source_.find(id) != source_.end();
3434
}
3535

36+
bool NativeModuleLoader::Add(const char* id, const UnionBytes& source) {
37+
if (Exists(id)) {
38+
return false;
39+
}
40+
source_.emplace(id, source);
41+
return true;
42+
}
43+
3644
Local<Object> NativeModuleLoader::GetSourceObject(Local<Context> context) {
3745
Isolate* isolate = context->GetIsolate();
3846
Local<Object> out = Object::New(isolate);

src/node_native_module.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class NativeModuleLoader {
4747
UnionBytes GetConfig(); // Return data for config.gypi
4848

4949
bool Exists(const char* id);
50+
bool Add(const char* id, const UnionBytes& source);
51+
5052
v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context);
5153
v8::Local<v8::String> GetConfigString(v8::Isolate* isolate);
5254
std::vector<std::string> GetModuleIds();

src/node_native_module_env.cc

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Local<Set> ToJsSet(Local<Context> context, const std::set<std::string>& in) {
3333
return out;
3434
}
3535

36+
bool NativeModuleEnv::Add(const char* id, const UnionBytes& source) {
37+
return NativeModuleLoader::GetInstance()->Add(id, source);
38+
}
39+
3640
bool NativeModuleEnv::Exists(const char* id) {
3741
return NativeModuleLoader::GetInstance()->Exists(id);
3842
}

src/node_native_module_env.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class NativeModuleEnv {
2929
// Returns config.gypi as a JSON string
3030
static v8::Local<v8::String> GetConfigString(v8::Isolate* isolate);
3131
static bool Exists(const char* id);
32+
static bool Add(const char* id, const UnionBytes& source);
3233

3334
// Loads data into NativeModuleLoader::.instance.code_cache_
3435
// Generated by mkcodecache as node_code_cache.cc when

src/node_worker.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ void Worker::Run() {
323323
CreateEnvMessagePort(env_.get());
324324
Debug(this, "Created message port for worker %llu", thread_id_.id);
325325
if (LoadEnvironment(env_.get(),
326-
nullptr,
326+
StartExecutionCallback{},
327327
std::move(inspector_parent_handle_))
328328
.IsEmpty()) {
329329
return;

test/cctest/test_environment.cc

+27
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ TEST_F(EnvironmentTest, LoadEnvironmentWithCallback) {
8080
CHECK(called_cb);
8181
}
8282

83+
TEST_F(EnvironmentTest, LoadEnvironmentWithSource) {
84+
const v8::HandleScope handle_scope(isolate_);
85+
const Argv argv;
86+
Env env {handle_scope, argv};
87+
88+
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
89+
v8::Local<v8::Value> main_ret =
90+
node::LoadEnvironment(*env,
91+
"return { process, require };").ToLocalChecked();
92+
93+
CHECK(main_ret->IsObject());
94+
CHECK(main_ret.As<v8::Object>()->Get(
95+
context,
96+
v8::String::NewFromOneByte(
97+
isolate_,
98+
reinterpret_cast<const uint8_t*>("process"),
99+
v8::NewStringType::kNormal).ToLocalChecked())
100+
.ToLocalChecked()->IsObject());
101+
CHECK(main_ret.As<v8::Object>()->Get(
102+
context,
103+
v8::String::NewFromOneByte(
104+
isolate_,
105+
reinterpret_cast<const uint8_t*>("require"),
106+
v8::NewStringType::kNormal).ToLocalChecked())
107+
.ToLocalChecked()->IsFunction());
108+
}
109+
83110
TEST_F(EnvironmentTest, AtExitWithEnvironment) {
84111
const v8::HandleScope handle_scope(isolate_);
85112
const Argv argv;

0 commit comments

Comments
 (0)