Skip to content
File

Blob: src/workerd/api/workers-module.c++

3.4 KB
1// Copyright (c) 2017-2025 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5#include "workers-module.h"
6 
7#include <workerd/api/actor-state.h>
8#include <workerd/api/global-scope.h>
9#include <workerd/io/features.h>
10 
11namespace workerd::api {
12 
13jsg::Ref<WorkerEntrypoint> WorkerEntrypoint::constructor(
14 const v8::FunctionCallbackInfo<v8::Value>& args, jsg::JsObject ctx, jsg::JsObject env) {
15 // HACK: We take `FunctionCallbackInfo` mostly so that we can set properties directly on
16 // `This()`. There ought to be a better way to get access to `this` in a constructor.
17 // We *also* declare `ctx` and `env` params more explicitly just for the sake of type checking.
18 jsg::Lock& js = jsg::Lock::from(args.GetIsolate());
19 
20 jsg::JsObject self(args.This());
21 self.set(js, "ctx", jsg::JsValue(args[0]));
22 self.set(js, "env", jsg::JsValue(args[1]));
23 return js.alloc<WorkerEntrypoint>();
24}
25 
26jsg::Ref<DurableObjectBase> DurableObjectBase::constructor(
27 const v8::FunctionCallbackInfo<v8::Value>& args,
28 jsg::Ref<DurableObjectState> ctx,
29 jsg::JsObject env) {
30 // HACK: We take `FunctionCallbackInfo` mostly so that we can set properties directly on
31 // `This()`. There ought to be a better way to get access to `this` in a constructor.
32 // We *also* declare `ctx` and `env` params more explicitly just for the sake of type checking.
33 jsg::Lock& js = jsg::Lock::from(args.GetIsolate());
34 
35 jsg::JsObject self(args.This());
36 self.set(js, "ctx", jsg::JsValue(args[0]));
37 self.set(js, "env", jsg::JsValue(args[1]));
38 return js.alloc<DurableObjectBase>();
39}
40 
41jsg::Ref<WorkflowEntrypoint> WorkflowEntrypoint::constructor(
42 const v8::FunctionCallbackInfo<v8::Value>& args,
43 jsg::Ref<ExecutionContext> ctx,
44 jsg::JsObject env) {
45 // HACK: We take `FunctionCallbackInfo` mostly so that we can set properties directly on
46 // `This()`. There ought to be a better way to get access to `this` in a constructor.
47 // We *also* declare `ctx` and `env` params more explicitly just for the sake of type checking.
48 jsg::Lock& js = jsg::Lock::from(args.GetIsolate());
49 
50 jsg::JsObject self(args.This());
51 self.set(js, "ctx", jsg::JsValue(args[0]));
52 self.set(js, "env", jsg::JsValue(args[1]));
53 return js.alloc<WorkflowEntrypoint>();
54}
55 
56void EntrypointsModule::waitUntil(kj::Promise<void> promise) {
57 // No need to check if IoContext::hasCurrent since current() will throw
58 // if there is no active request.
59 IoContext::current().addWaitUntil(kj::mv(promise));
60}
61 
62jsg::Optional<jsg::Ref<CacheContext>> EntrypointsModule::getCtxCache(jsg::Lock& js) {
63 // Delegate to the embedding application's getCtxCacheProperty() hook, which returns a
64 // CacheContext (e.g. CachePurge in edgeworker) when cache is enabled for this pipeline.
65 // Returns kj::none when there is no active request or cache is not available.
66 if (IoContext::hasCurrent()) {
67 return Worker::Isolate::from(js).getApi().getCtxCacheProperty(js);
68 }
69 return kj::none;
70}
71 
72void EntrypointsModule::abortIsolate(jsg::Lock& js, jsg::Optional<kj::String> reason) {
73 auto& r = reason.orDefault(nullptr);
74 KJ_IF_SOME(context, IoContext::tryCurrent()) {
75 context.abortIsolate(r);
76 }
77 js.terminateExecutionNow();
78}
79 
80bool EntrypointsModule::getIsExperimental(jsg::Lock& js) {
81 return FeatureFlags::get(js).getWorkerdExperimental();
82}
83 
84} // namespace workerd::api