Skip to content

Commit

Permalink
Add updateConfiguration function to jsg Isolates
Browse files Browse the repository at this point in the history
This can be used to update the given configuration at runtime. Note that
while some jsg structs are lazily using the configuration, others can
use it at construction and will have the original configuration value.
  • Loading branch information
danlapid committed Nov 7, 2024
1 parent 8bf3af4 commit 23172a2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/workerd/jsg/promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,11 @@ class PromiseWrapper {
// std::nullptr_t). The getConfig allows us to handle any case using reasonable defaults.
PromiseWrapper(const auto& config): config(getConfig(config)) {}

template <typename MetaConfiguration>
void updateConfiguration(MetaConfiguration&& configuration) {
config = getConfig(kj::fwd<MetaConfiguration>(configuration));
}

template <typename T>
static constexpr const char* getName(Promise<T>*) {
return "Promise";
Expand Down Expand Up @@ -668,7 +673,7 @@ class PromiseWrapper {
}

private:
const JsgConfig config;
JsgConfig config;

static bool isThenable(v8::Local<v8::Context> context, v8::Local<v8::Value> handle) {
if (handle->IsObject()) {
Expand Down
5 changes: 5 additions & 0 deletions src/workerd/jsg/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,11 @@ class ResourceWrapper {
ResourceWrapper(MetaConfiguration&& configuration)
: configuration(kj::fwd<MetaConfiguration>(configuration)) {}

template <typename MetaConfiguration>
void updateConfiguration(MetaConfiguration&& config) {
configuration = kj::fwd<MetaConfiguration>(config);
}

inline void initTypeWrapper() {
TypeWrapper& wrapper = static_cast<TypeWrapper&>(*this);
wrapper.resourceTypeMap.insert(typeid(T),
Expand Down
5 changes: 5 additions & 0 deletions src/workerd/jsg/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ class Isolate: public IsolateBase {
dropWrappers(kj::mv(wrapper));
}

template <typename MetaConfiguration>
void updateConfiguration(MetaConfiguration&& configuration) {
wrapper->updateConfiguration(kj::fwd<MetaConfiguration>(configuration));
}

kj::Exception unwrapException(
v8::Local<v8::Context> context, v8::Local<v8::Value> exception) override {
return wrapper->template unwrap<kj::Exception>(
Expand Down
18 changes: 18 additions & 0 deletions src/workerd/jsg/type-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ class TypeWrapperBase<Self, T, JsgKind::STRUCT>

inline void initTypeWrapper() {}

template <typename MetaConfiguration>
void updateConfiguration(MetaConfiguration&& configuration) {}

void unwrap() = delete; // StructWrapper only implements tryUnwrap(), not unwrap()
};

Expand Down Expand Up @@ -274,6 +277,9 @@ class TypeWrapperBase<Self, TypeWrapperExtension<Extension>, JsgKind::EXTENSION>
void unwrap() = delete; // extensions only implement tryUnwrap(), not unwrap()

inline void initTypeWrapper() {}

template <typename MetaConfiguration>
void updateConfiguration(MetaConfiguration&& configuration) {}
};

// Specialization of TypeWrapperBase for InjectConfiguration.
Expand All @@ -298,6 +304,11 @@ class TypeWrapperBase<Self, InjectConfiguration<Configuration>, JsgKind::EXTENSI

inline void initTypeWrapper() {}

template <typename MetaConfiguration>
void updateConfiguration(MetaConfiguration&& config) {
configuration = kj::fwd<MetaConfiguration>(config);
}

private:
Configuration configuration;
};
Expand Down Expand Up @@ -411,6 +422,13 @@ class TypeWrapper: public DynamicResourceTypeMap<Self>,
(TypeWrapperBase<Self, T>::initTypeWrapper(), ...);
}

template <typename MetaConfiguration>
void updateConfiguration(MetaConfiguration&& configuration) {
(TypeWrapperBase<Self, T>::updateConfiguration(kj::fwd<MetaConfiguration>(configuration)), ...);
MaybeWrapper<Self>::updateConfiguration(kj::fwd<MetaConfiguration>(configuration));
PromiseWrapper<Self>::updateConfiguration(kj::fwd<MetaConfiguration>(configuration));
}

static TypeWrapper& from(v8::Isolate* isolate) {
return *reinterpret_cast<TypeWrapper*>(isolate->GetData(1));
}
Expand Down
7 changes: 6 additions & 1 deletion src/workerd/jsg/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,11 @@ class MaybeWrapper {
// The getConfig allows us to handle any case using reasonable defaults.
MaybeWrapper(const auto& config): config(getConfig(config)) {}

template <typename MetaConfiguration>
void updateConfiguration(MetaConfiguration&& configuration) {
config = getConfig(kj::fwd<MetaConfiguration>(configuration));
}

template <typename U>
static constexpr decltype(auto) getName(kj::Maybe<U>*) {
return TypeWrapper::getName((kj::Decay<U>*)nullptr);
Expand Down Expand Up @@ -623,7 +628,7 @@ class MaybeWrapper {
}

private:
const JsgConfig config;
JsgConfig config;
};

// =======================================================================================
Expand Down

0 comments on commit 23172a2

Please sign in to comment.