Skip to content

Commit

Permalink
Minor improvements to the NonModuleScript API
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Jan 6, 2025
1 parent 7da0d6f commit f652333
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/workerd/api/unsafe.c++
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jsg::JsValue UnsafeEval::eval(jsg::Lock& js, kj::String script, jsg::Optional<kj
js.setAllowEval(true);
KJ_DEFER(js.setAllowEval(false));
auto compiled = jsg::NonModuleScript::compile(js, script, getName(name, EVAL_STR));
return jsg::JsValue(compiled.runAndReturn(js.v8Context()));
return compiled.runAndReturn(js);
}

UnsafeEval::UnsafeEvalFunction UnsafeEval::newFunction(jsg::Lock& js,
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ Worker::Worker(kj::Own<const Script> scriptParam,
KJ_CASE_ONEOF(unboundScript, jsg::NonModuleScript) {
auto limitScope =
script->isolate->getLimitEnforcer().enterStartupJs(lock, limitErrorOrTime);
unboundScript.run(lock.v8Context());
unboundScript.run(lock);
}
KJ_CASE_ONEOF(mainModule, kj::Path) {
KJ_IF_SOME(ns,
Expand Down
14 changes: 6 additions & 8 deletions src/workerd/jsg/script.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace workerd::jsg {

v8::Local<v8::Value> NonModuleScript::runAndReturn(v8::Local<v8::Context> context) const {
auto isolate = context->GetIsolate();
auto boundScript = unboundScript.getHandle(isolate)->BindToCurrentContext();
return check(boundScript->Run(context));
jsg::JsValue NonModuleScript::runAndReturn(jsg::Lock& js) const {
auto boundScript = unboundScript.Get(js.v8Isolate)->BindToCurrentContext();
return jsg::JsValue(check(boundScript->Run(js.v8Context())));
}

void NonModuleScript::run(v8::Local<v8::Context> context) const {
auto isolate = context->GetIsolate();
auto boundScript = unboundScript.getHandle(isolate)->BindToCurrentContext();
check(boundScript->Run(context));
void NonModuleScript::run(jsg::Lock& js) const {
auto boundScript = unboundScript.Get(js.v8Isolate)->BindToCurrentContext();
check(boundScript->Run(js.v8Context()));
}

NonModuleScript NonModuleScript::compile(jsg::Lock& js, kj::StringPtr code, kj::StringPtr name) {
Expand Down
8 changes: 4 additions & 4 deletions src/workerd/jsg/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace workerd::jsg {
// jsg::NonModuleScript wraps a v8::UnboundScript.
// An unbound script is a script that has been compiled but is not
// yet bound to a specific context.
class NonModuleScript {
class NonModuleScript final {
public:
NonModuleScript(jsg::Lock& js, v8::Local<v8::UnboundScript> script)
: unboundScript(js.v8Isolate, script) {}
Expand All @@ -18,15 +18,15 @@ class NonModuleScript {

// Running the script will create a v8::Script instance bound to the given
// context then will run it to completion.
void run(v8::Local<v8::Context> context) const;
void run(jsg::Lock& js) const;

v8::Local<v8::Value> runAndReturn(v8::Local<v8::Context> context) const;
jsg::JsValue runAndReturn(jsg::Lock& js) const;

static jsg::NonModuleScript compile(
jsg::Lock& js, kj::StringPtr code, kj::StringPtr name = "worker.js");

private:
jsg::V8Ref<v8::UnboundScript> unboundScript;
v8::Global<v8::UnboundScript> unboundScript;
};

} // namespace workerd::jsg

0 comments on commit f652333

Please sign in to comment.