Skip to content
File

Blob: src/workerd/jsg/script.h

cpp33 lines
1#pragma once
2 
3#include <workerd/jsg/jsg.h>
4 
5namespace 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.
10class NonModuleScript final {
11 public:
12 NonModuleScript(jsg::Lock& js, v8::Local<v8::UnboundScript> script)
13 : unboundScript(js.v8Isolate, script) {}
14 
15 NonModuleScript(NonModuleScript&&) noexcept = 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