File
Blob: src/workerd/api/modules.c++
| 1 | #include "modules.h" |
| 2 | |
| 3 | #include <workerd/io/features.h> |
| 4 | #include <workerd/jsg/jsg.h> |
| 5 | #include <workerd/jsg/setup.h> |
| 6 | |
| 7 | namespace workerd::api { |
| 8 | |
| 9 | kj::Maybe<jsg::JsObject> EnvModule::getCurrentEnv(jsg::Lock& js) { |
| 10 | auto& key = jsg::IsolateBase::from(js.v8Isolate).getEnvAsyncContextKey(); |
| 11 | // Check async context first - withEnv() overrides take precedence over the disable flag. |
| 12 | KJ_IF_SOME(frame, jsg::AsyncContextFrame::current(js)) { |
| 13 | KJ_IF_SOME(value, frame.get(key)) { |
| 14 | auto handle = value.getHandle(js); |
| 15 | if (handle->IsObject()) { |
| 16 | return jsg::JsObject(handle.As<v8::Object>()); |
| 17 | } |
| 18 | if (FeatureFlags::get(js).getEnvModuleNullableSupport() && handle->IsNullOrUndefined()) { |
| 19 | return kj::none; |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | if (FeatureFlags::get(js).getDisableImportableEnv()) return kj::none; |
| 24 | |
| 25 | return js.getWorkerEnv().map([&](const jsg::V8Ref<v8::Object>& val) -> jsg::JsObject { |
| 26 | return jsg::JsObject(val.getHandle(js)); |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | jsg::JsRef<jsg::JsValue> EnvModule::withEnv( |
| 31 | jsg::Lock& js, jsg::Value newEnv, jsg::Function<jsg::JsRef<jsg::JsValue>()> fn) { |
| 32 | auto& key = jsg::IsolateBase::from(js.v8Isolate).getEnvAsyncContextKey(); |
| 33 | jsg::AsyncContextFrame::StorageScope storage(js, key, kj::mv(newEnv)); |
| 34 | return js.tryCatch([&]() mutable -> jsg::JsRef<jsg::JsValue> { return fn(js); }, |
| 35 | [&](jsg::Value&& exception) mutable -> jsg::JsRef<jsg::JsValue> { |
| 36 | js.throwException(kj::mv(exception)); |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | jsg::Ref<PythonPatchedEnv> EnvModule::pythonPatchEnv(jsg::Lock& js, jsg::Value newEnv) { |
| 41 | auto& key = jsg::IsolateBase::from(js.v8Isolate).getEnvAsyncContextKey(); |
| 42 | return jsg::alloc<PythonPatchedEnv>(js, key, kj::mv(newEnv)); |
| 43 | } |
| 44 | |
| 45 | kj::Maybe<jsg::JsObject> EnvModule::getCurrentExports(jsg::Lock& js) { |
| 46 | auto& key = jsg::IsolateBase::from(js.v8Isolate).getExportsAsyncContextKey(); |
| 47 | // Check async context first - withExports() overrides take precedence over the disable flags. |
| 48 | KJ_IF_SOME(frame, jsg::AsyncContextFrame::current(js)) { |
| 49 | KJ_IF_SOME(value, frame.get(key)) { |
| 50 | auto handle = value.getHandle(js); |
| 51 | if (handle->IsObject()) { |
| 52 | return jsg::JsObject(handle.As<v8::Object>()); |
| 53 | } |
| 54 | if (FeatureFlags::get(js).getEnvModuleNullableSupport() && handle->IsNullOrUndefined()) { |
| 55 | return kj::none; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | if (FeatureFlags::get(js).getDisableImportableEnv()) { |
| 60 | return kj::none; |
| 61 | } |
| 62 | |
| 63 | return js.getWorkerExports().map([&](const jsg::V8Ref<v8::Object>& val) -> jsg::JsObject { |
| 64 | return jsg::JsObject(val.getHandle(js)); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | jsg::JsRef<jsg::JsValue> EnvModule::withExports( |
| 69 | jsg::Lock& js, jsg::Value newExports, jsg::Function<jsg::JsRef<jsg::JsValue>()> fn) { |
| 70 | auto& key = jsg::IsolateBase::from(js.v8Isolate).getExportsAsyncContextKey(); |
| 71 | jsg::AsyncContextFrame::StorageScope storage(js, key, kj::mv(newExports)); |
| 72 | return js.tryCatch([&]() mutable -> jsg::JsRef<jsg::JsValue> { return fn(js); }, |
| 73 | [&](jsg::Value&& exception) mutable -> jsg::JsRef<jsg::JsValue> { |
| 74 | js.throwException(kj::mv(exception)); |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | jsg::JsRef<jsg::JsValue> EnvModule::withEnvAndExports(jsg::Lock& js, |
| 79 | jsg::Value newEnv, |
| 80 | jsg::Value newExports, |
| 81 | jsg::Function<jsg::JsRef<jsg::JsValue>()> fn) { |
| 82 | auto& envKey = jsg::IsolateBase::from(js.v8Isolate).getEnvAsyncContextKey(); |
| 83 | auto& exportsKey = jsg::IsolateBase::from(js.v8Isolate).getExportsAsyncContextKey(); |
| 84 | jsg::AsyncContextFrame::StorageScope envStorage(js, envKey, kj::mv(newEnv)); |
| 85 | jsg::AsyncContextFrame::StorageScope exportsStorage(js, exportsKey, kj::mv(newExports)); |
| 86 | return js.tryCatch([&]() mutable -> jsg::JsRef<jsg::JsValue> { return fn(js); }, |
| 87 | [&](jsg::Value&& exception) mutable -> jsg::JsRef<jsg::JsValue> { |
| 88 | js.throwException(kj::mv(exception)); |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | } // namespace workerd::api |