Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSG Completion: more jsg consistency improvements #1004

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/workerd/api/crypto.c++
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,6 @@ jsg::Promise<jsg::Ref<CryptoKey>> SubtleCrypto::unwrapKey(jsg::Lock& js, kj::Str
const jsg::TypeHandler<JsonWebKey>& jwkHandler) {
auto operation = __func__;
return js.evalNow([&]() -> jsg::Ref<CryptoKey> {
auto isolate = js.v8Isolate;
auto normalizedAlgorithm = interpretAlgorithmParam(kj::mv(unwrapAlgorithm));
auto normalizedUnwrapAlgorithm = interpretAlgorithmParam(kj::mv(unwrappedKeyAlgorithm));

Expand All @@ -522,7 +521,7 @@ jsg::Promise<jsg::Ref<CryptoKey>> SubtleCrypto::unwrapKey(jsg::Lock& js, kj::Str
ImportKeyData importData;

if (format == "jwk") {
auto jwkDict = js.parseJson(jsg::v8Str(isolate, bytes.asChars()));
auto jwkDict = js.parseJson(bytes.asChars());

importData = JSG_REQUIRE_NONNULL(jwkHandler.tryUnwrap(js, jwkDict.getHandle(js)),
DOMDataError, "Missing \"kty\" field or corrupt JSON unwrapping key?");
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/global-scope.c++
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void handleDefaultBotManagement(jsg::Lock& js, jsg::Value& cf) {
// pull the exact same value.
auto defaultBm = jsg::check(context->Global()->GetPrivate(context, sym));
if (defaultBm->IsUndefined()) {
auto bm = js.parseJson(kDefaultBotManagementValue);
auto bm = js.parseJson(kj::StringPtr(kDefaultBotManagementValue));
KJ_DASSERT(bm.getHandle(js)->IsObject());
js.recursivelyFreeze(bm);
defaultBm = bm.getHandle(js);
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/gpu/gpu-buffer.c++
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GPUBuffer::GPUBuffer(jsg::Lock& js, wgpu::Buffer b, wgpu::BufferDescriptor desc,
wgpu::Device device, kj::Own<AsyncRunner> async)
: buffer_(kj::mv(b)), device_(kj::mv(device)), desc_(kj::mv(desc)),
async_(kj::mv(async)),
detachKey_(jsg::V8Ref(js.v8Isolate, v8::Object::New(js.v8Isolate))) {
detachKey_(js.v8Ref(v8::Object::New(js.v8Isolate))) {

if (desc.mappedAtCreation) {
state_ = State::MappedAtCreation;
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/api/kv.c++
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ jsg::Promise<KvNamespace::GetWithMetadataResult> KvNamespace::getWithMetadata(
auto cacheStatus = response.headers->get(context.getHeaderIds().cfCacheStatus)
.map([&](kj::StringPtr cs) {
auto value = jsg::v8StrIntern(js.v8Isolate, cs);
return jsg::Value(js.v8Isolate, kj::mv(value));
return js.v8Ref(kj::mv(value).As<v8::Value>());
});

if (response.statusCode == 404 || response.statusCode == 410) {
Expand Down Expand Up @@ -279,7 +279,7 @@ jsg::Promise<jsg::Value> KvNamespace::list(jsg::Lock& js, jsg::Optional<ListOpti

kj::Maybe<jsg::Value> cacheStatus = [&]()-> kj::Maybe<jsg::Value> {
KJ_IF_MAYBE(cs, response.headers->get(context.getHeaderIds().cfCacheStatus)) {
return jsg::Value(js.v8Isolate, jsg::v8StrIntern(js.v8Isolate, *cs));
return js.v8Ref(jsg::v8StrIntern(js.v8Isolate, *cs).As<v8::Value>());
}
return nullptr;
}();
Expand Down
16 changes: 8 additions & 8 deletions src/workerd/api/queue.c++
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ jsg::Value deserialize(jsg::Lock& js, kj::Array<kj::byte> body, kj::Maybe<kj::St
auto type = contentType.orDefault(IncomingQueueMessage::ContentType::V8);

if (type == IncomingQueueMessage::ContentType::TEXT) {
return jsg::Value(js.v8Isolate, jsg::v8Str(js.v8Isolate, body.asChars()));
return js.v8Ref(jsg::v8Str(js.v8Isolate, body.asChars()).As<v8::Value>());
} else if (type == IncomingQueueMessage::ContentType::BYTES) {
return jsg::Value(js.v8Isolate, js.wrapBytes(kj::mv(body)));
return js.v8Ref(js.wrapBytes(kj::mv(body)).As<v8::Value>());
} else if (type == IncomingQueueMessage::ContentType::JSON) {
return js.parseJson(jsg::v8Str(js.v8Isolate, body.asChars()));
return js.parseJson(body.asChars());
} else if (type == IncomingQueueMessage::ContentType::V8) {
return jsg::Value(js.v8Isolate, jsg::Deserializer(js, body.asPtr()).readValue());
return js.v8Ref(jsg::Deserializer(js, body.asPtr()).readValue());
} else {
JSG_FAIL_REQUIRE(TypeError, kj::str("Unsupported queue message content type: ", type));
}
Expand All @@ -291,14 +291,14 @@ jsg::Value deserialize(jsg::Lock& js, rpc::QueueMessage::Reader message) {
}

if (type == IncomingQueueMessage::ContentType::TEXT) {
return jsg::Value(js.v8Isolate, jsg::v8Str(js.v8Isolate, message.getData().asChars()));
return js.v8Ref(jsg::v8Str(js.v8Isolate, message.getData().asChars()).As<v8::Value>());
} else if (type == IncomingQueueMessage::ContentType::BYTES) {
kj::Array<kj::byte> bytes = kj::heapArray(message.getData().asBytes());
return jsg::Value(js.v8Isolate, js.wrapBytes(kj::mv(bytes)));
return js.v8Ref(js.wrapBytes(kj::mv(bytes)).As<v8::Value>());
} else if (type == IncomingQueueMessage::ContentType::JSON) {
return js.parseJson(jsg::v8Str(js.v8Isolate, message.getData().asChars()));
return js.parseJson(message.getData().asChars());
} else if (type == IncomingQueueMessage::ContentType::V8) {
return jsg::Value(js.v8Isolate, jsg::Deserializer(js, message.getData()).readValue());
return js.v8Ref(jsg::Deserializer(js, message.getData()).readValue());
} else {
JSG_FAIL_REQUIRE(TypeError, kj::str("Unsupported queue message content type: ", type));
}
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/r2-admin.c++
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jsg::Promise<R2Admin::ListResult> R2Admin::list(jsg::Lock& js,
}

ListResult result {
.buckets = jsg::Value(js.v8Isolate, kj::mv(buckets)),
.buckets = js.v8Ref(buckets.As<v8::Value>()),
.truncated = responseBuilder.getTruncated(),
};
if (responseBuilder.hasCursor()) {
Expand Down
6 changes: 3 additions & 3 deletions src/workerd/jsg/jsg.c++
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ Lock::~Lock() noexcept(false) {
v8Isolate->SetData(2, previousData);
}

Value Lock::parseJson(kj::StringPtr text) {
Value Lock::parseJson(kj::ArrayPtr<const char> data) {
return withinHandleScope([&] {
return jsg::Value(v8Isolate, jsg::check(v8::JSON::Parse(v8Context(), v8Str(v8Isolate, text))));
return v8Ref(jsg::check(v8::JSON::Parse(v8Context(), v8Str(v8Isolate, data))));
});
}

Value Lock::parseJson(v8::Local<v8::String> text) {
return withinHandleScope([&] {
return jsg::Value(v8Isolate, jsg::check(v8::JSON::Parse(v8Context(), text)));
return v8Ref(jsg::check(v8::JSON::Parse(v8Context(), text)));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/workerd/jsg/jsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,7 @@ class Lock {
return *reinterpret_cast<Lock*>(v8Isolate->GetData(2));
}

Value parseJson(kj::StringPtr text);
Value parseJson(kj::ArrayPtr<const char> data);
Value parseJson(v8::Local<v8::String> text);
template <typename T>
kj::String serializeJson(V8Ref<T>& value) { return serializeJson(value.getHandle(*this)); }
Expand Down
Loading