Skip to content

Commit b9697c3

Browse files
authored
Move NonModuleScript out of modules.h/modules.c++ (#3296)
1 parent 1a12caa commit b9697c3

File tree

7 files changed

+64
-46
lines changed

7 files changed

+64
-46
lines changed

src/workerd/api/unsafe.c++

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "unsafe.h"
22

3+
#include <workerd/jsg/script.h>
4+
35
namespace workerd::api {
46

57
namespace {
@@ -18,8 +20,8 @@ inline kj::StringPtr getName(jsg::Optional<kj::String>& name, kj::StringPtr def)
1820
jsg::JsValue UnsafeEval::eval(jsg::Lock& js, kj::String script, jsg::Optional<kj::String> name) {
1921
js.setAllowEval(true);
2022
KJ_DEFER(js.setAllowEval(false));
21-
auto compiled = jsg::NonModuleScript::compile(script, js, getName(name, EVAL_STR));
22-
return jsg::JsValue(compiled.runAndReturn(js.v8Context()));
23+
auto compiled = jsg::NonModuleScript::compile(js, script, getName(name, EVAL_STR));
24+
return compiled.runAndReturn(js);
2325
}
2426

2527
UnsafeEval::UnsafeEvalFunction UnsafeEval::newFunction(jsg::Lock& js,

src/workerd/io/worker.c++

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <workerd/jsg/inspector.h>
1919
#include <workerd/jsg/jsg.h>
2020
#include <workerd/jsg/modules-new.h>
21+
#include <workerd/jsg/script.h>
2122
#include <workerd/jsg/util.h>
2223
#include <workerd/util/batch-queue.h>
2324
#include <workerd/util/color-util.h>
@@ -1344,7 +1345,7 @@ Worker::Script::Script(kj::Own<const Isolate> isolateParam,
13441345
auto limitScope =
13451346
isolate->getLimitEnforcer().enterStartupJs(lock, limitErrorOrTime);
13461347
impl->unboundScriptOrMainModule =
1347-
jsg::NonModuleScript::compile(script.mainScript, lock, script.mainScriptName);
1348+
jsg::NonModuleScript::compile(lock, script.mainScript, script.mainScriptName);
13481349
}
13491350

13501351
break;
@@ -1661,7 +1662,7 @@ Worker::Worker(kj::Own<const Script> scriptParam,
16611662
KJ_CASE_ONEOF(unboundScript, jsg::NonModuleScript) {
16621663
auto limitScope =
16631664
script->isolate->getLimitEnforcer().enterStartupJs(lock, limitErrorOrTime);
1664-
unboundScript.run(lock.v8Context());
1665+
unboundScript.run(lock);
16651666
}
16661667
KJ_CASE_ONEOF(mainModule, kj::Path) {
16671668
KJ_IF_SOME(ns,

src/workerd/jsg/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ wd_cc_library(
2121
"modules-new.c++",
2222
"promise.c++",
2323
"resource.c++",
24+
"script.c++",
2425
"ser.c++",
2526
"setup.c++",
2627
"util.c++",
@@ -44,6 +45,7 @@ wd_cc_library(
4445
"modules-new.h",
4546
"promise.h",
4647
"resource.h",
48+
"script.h",
4749
"ser.h",
4850
"setup.h",
4951
"struct.h",

src/workerd/jsg/modules.c++

-20
Original file line numberDiff line numberDiff line change
@@ -280,26 +280,6 @@ v8::Local<v8::Value> CommonJsModuleContext::require(jsg::Lock& js, kj::String sp
280280
return ModuleRegistry::requireImpl(js, info, options);
281281
}
282282

283-
v8::Local<v8::Value> NonModuleScript::runAndReturn(v8::Local<v8::Context> context) const {
284-
auto isolate = context->GetIsolate();
285-
auto boundScript = unboundScript.Get(isolate)->BindToCurrentContext();
286-
return check(boundScript->Run(context));
287-
}
288-
289-
void NonModuleScript::run(v8::Local<v8::Context> context) const {
290-
auto isolate = context->GetIsolate();
291-
auto boundScript = unboundScript.Get(isolate)->BindToCurrentContext();
292-
check(boundScript->Run(context));
293-
}
294-
295-
NonModuleScript NonModuleScript::compile(kj::StringPtr code, jsg::Lock& js, kj::StringPtr name) {
296-
// Create a dummy script origin for it to appear in Sources panel.
297-
auto isolate = js.v8Isolate;
298-
v8::ScriptOrigin origin(v8StrIntern(isolate, name));
299-
v8::ScriptCompiler::Source source(v8Str(isolate, code), origin);
300-
return NonModuleScript(js, check(v8::ScriptCompiler::CompileUnboundScript(isolate, &source)));
301-
}
302-
303283
void instantiateModule(
304284
jsg::Lock& js, v8::Local<v8::Module>& module, InstantiateModuleOptions options) {
305285
KJ_ASSERT(!module.IsEmpty());

src/workerd/jsg/modules.h

-22
Original file line numberDiff line numberDiff line change
@@ -171,28 +171,6 @@ class NodeJsModuleContext: public jsg::Object {
171171
jsg::Value exports;
172172
};
173173

174-
// jsg::NonModuleScript wraps a v8::UnboundScript.
175-
class NonModuleScript {
176-
public:
177-
NonModuleScript(jsg::Lock& js, v8::Local<v8::UnboundScript> script)
178-
: unboundScript(js.v8Isolate, script) {}
179-
180-
NonModuleScript(NonModuleScript&&) = default;
181-
NonModuleScript& operator=(NonModuleScript&&) = default;
182-
183-
// Running the script will create a v8::Script instance bound to the given
184-
// context then will run it to completion.
185-
void run(v8::Local<v8::Context> context) const;
186-
187-
v8::Local<v8::Value> runAndReturn(v8::Local<v8::Context> context) const;
188-
189-
static jsg::NonModuleScript compile(
190-
kj::StringPtr code, jsg::Lock& js, kj::StringPtr name = "worker.js");
191-
192-
private:
193-
v8::Global<v8::UnboundScript> unboundScript;
194-
};
195-
196174
enum class InstantiateModuleOptions {
197175
// Allows pending top-level await in the module when evaluated. Will cause
198176
// the microtask queue to be drained once in an attempt to resolve those.

src/workerd/jsg/script.c++

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "script.h"
2+
3+
namespace workerd::jsg {
4+
5+
jsg::JsValue NonModuleScript::runAndReturn(jsg::Lock& js) const {
6+
auto boundScript = unboundScript.Get(js.v8Isolate)->BindToCurrentContext();
7+
return jsg::JsValue(check(boundScript->Run(js.v8Context())));
8+
}
9+
10+
void NonModuleScript::run(jsg::Lock& js) const {
11+
auto boundScript = unboundScript.Get(js.v8Isolate)->BindToCurrentContext();
12+
check(boundScript->Run(js.v8Context()));
13+
}
14+
15+
NonModuleScript NonModuleScript::compile(jsg::Lock& js, kj::StringPtr code, kj::StringPtr name) {
16+
// Create a dummy script origin for it to appear in Sources panel.
17+
auto isolate = js.v8Isolate;
18+
v8::ScriptOrigin origin(js.str(name));
19+
v8::ScriptCompiler::Source source(js.str(code), origin);
20+
return NonModuleScript(js, check(v8::ScriptCompiler::CompileUnboundScript(isolate, &source)));
21+
}
22+
23+
} // namespace workerd::jsg

src/workerd/jsg/script.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <workerd/jsg/jsg.h>
4+
5+
namespace workerd::jsg {
6+
7+
// jsg::NonModuleScript wraps a v8::UnboundScript.
8+
// An unbound script is a script that has been compiled but is not
9+
// yet bound to a specific context.
10+
class NonModuleScript final {
11+
public:
12+
NonModuleScript(jsg::Lock& js, v8::Local<v8::UnboundScript> script)
13+
: unboundScript(js.v8Isolate, script) {}
14+
15+
NonModuleScript(NonModuleScript&&) = default;
16+
NonModuleScript& operator=(NonModuleScript&&) = default;
17+
KJ_DISALLOW_COPY(NonModuleScript);
18+
19+
// Running the script will create a v8::Script instance bound to the given
20+
// context then will run it to completion.
21+
void run(jsg::Lock& js) const;
22+
23+
jsg::JsValue runAndReturn(jsg::Lock& js) const;
24+
25+
static jsg::NonModuleScript compile(
26+
jsg::Lock& js, kj::StringPtr code, kj::StringPtr name = "worker.js");
27+
28+
private:
29+
v8::Global<v8::UnboundScript> unboundScript;
30+
};
31+
32+
} // namespace workerd::jsg

0 commit comments

Comments
 (0)