-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move NonModuleScript out of modules.h/modules.c++ (#3296)
- Loading branch information
Showing
7 changed files
with
64 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "script.h" | ||
|
||
namespace workerd::jsg { | ||
|
||
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(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) { | ||
// Create a dummy script origin for it to appear in Sources panel. | ||
auto isolate = js.v8Isolate; | ||
v8::ScriptOrigin origin(js.str(name)); | ||
v8::ScriptCompiler::Source source(js.str(code), origin); | ||
return NonModuleScript(js, check(v8::ScriptCompiler::CompileUnboundScript(isolate, &source))); | ||
} | ||
|
||
} // namespace workerd::jsg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <workerd/jsg/jsg.h> | ||
|
||
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 final { | ||
public: | ||
NonModuleScript(jsg::Lock& js, v8::Local<v8::UnboundScript> script) | ||
: unboundScript(js.v8Isolate, script) {} | ||
|
||
NonModuleScript(NonModuleScript&&) = default; | ||
NonModuleScript& operator=(NonModuleScript&&) = default; | ||
KJ_DISALLOW_COPY(NonModuleScript); | ||
|
||
// Running the script will create a v8::Script instance bound to the given | ||
// context then will run it to completion. | ||
void run(jsg::Lock& js) const; | ||
|
||
jsg::JsValue runAndReturn(jsg::Lock& js) const; | ||
|
||
static jsg::NonModuleScript compile( | ||
jsg::Lock& js, kj::StringPtr code, kj::StringPtr name = "worker.js"); | ||
|
||
private: | ||
v8::Global<v8::UnboundScript> unboundScript; | ||
}; | ||
|
||
} // namespace workerd::jsg |