Skip to content

Commit

Permalink
LibJS+LibWeb: Add JS::Value constructor for JS::Handle<T>
Browse files Browse the repository at this point in the history
Similar to the constructors for ``JS::{Nonnull}GCPtr<T>``, this helper
avoids unnecessary .ptr() clutter when we want to construct Values.
  • Loading branch information
ADKaster committed Dec 19, 2023
1 parent 9624eca commit d361221
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ static void generate_wrap_statement(SourceGenerator& generator, ByteString const
)~~~");
} else if (type.name() == "ArrayBufferView" || type.name() == "BufferSource") {
scoped_generator.append(R"~~~(
@result_expression@ JS::Value(const_cast<JS::Object*>(@value@->raw_object().ptr()));
@result_expression@ JS::Value(@value@->raw_object());
)~~~");
} else if (is<IDL::UnionType>(type)) {
auto& union_type = verify_cast<IDL::UnionType>(type);
Expand Down
6 changes: 6 additions & 0 deletions Userland/Libraries/LibJS/Runtime/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ class Value {
{
}

template<typename T>
Value(Handle<T> const& ptr)
: Value(ptr.ptr())
{
}

double as_double() const
{
VERIFY(is_number());
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/MessageEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ JS::NonnullGCPtr<JS::Object> MessageEvent::ports() const
if (!m_ports_array) {
Vector<JS::Value> port_vector;
for (auto const& port : m_ports) {
port_vector.append(JS::Value(port.ptr()));
port_vector.append(port);
}
m_ports_array = JS::Array::create_from(realm(), port_vector);
MUST(m_ports_array->set_integrity_level(IntegrityLevel::Frozen));
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ WebIDL::ExceptionOr<SerializedTransferRecord> structured_serialize_with_transfer
// FIXME: 2. If transferable has an [[ArrayBufferData]] internal slot and IsSharedArrayBuffer(transferable) is true, then throw a "DataCloneError" DOMException.

// 3. If memory[transferable] exists, then throw a "DataCloneError" DOMException.
auto transferable_value = JS::Value(transferable.ptr());
auto transferable_value = JS::Value(transferable);
if (memory.contains(transferable_value)) {
return WebIDL::DataCloneError::create(*vm.current_realm(), "Cannot transfer value twice"_fly_string);
}
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Streams/ReadableStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_
auto readable_stream = realm.heap().allocate<ReadableStream>(realm, realm);

// 1. If underlyingSource is missing, set it to null.
auto underlying_source = underlying_source_object.has_value() ? JS::Value(underlying_source_object.value().ptr()) : JS::js_null();
auto underlying_source = underlying_source_object.has_value() ? JS::Value(underlying_source_object.value()) : JS::js_null();

// 2. Let underlyingSourceDict be underlyingSource, converted to an IDL value of type UnderlyingSource.
auto underlying_source_dict = TRY(UnderlyingSource::from_value(vm, underlying_source));
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Streams/TransformStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<TransformStream>> TransformStream::construc
auto stream = realm.heap().allocate<TransformStream>(realm, realm);

// 1. If transformer is missing, set it to null.
auto transformer = transformer_object.has_value() ? JS::Value { transformer_object.value().ptr() } : JS::js_null();
auto transformer = transformer_object.has_value() ? JS::Value { transformer_object.value() } : JS::js_null();

// 2. Let transformerDict be transformer, converted to an IDL value of type Transformer.
auto transformer_dict = TRY(Transformer::from_value(vm, transformer));
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Streams/WritableStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStream>> WritableStream::construct_
auto writable_stream = realm.heap().allocate<WritableStream>(realm, realm);

// 1. If underlyingSink is missing, set it to null.
auto underlying_sink = underlying_sink_object.has_value() ? JS::Value(underlying_sink_object.value().ptr()) : JS::js_null();
auto underlying_sink = underlying_sink_object.has_value() ? JS::Value(underlying_sink_object.value()) : JS::js_null();

// 2. Let underlyingSinkDict be underlyingSink, converted to an IDL value of type UnderlyingSink.
auto underlying_sink_dict = TRY(UnderlyingSink::from_value(vm, underlying_sink));
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/WebIDL/DOMException.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace Web {

inline JS::Completion throw_completion(JS::NonnullGCPtr<WebIDL::DOMException> exception)
{
return JS::throw_completion(JS::Value(static_cast<JS::Object*>(exception.ptr())));
return JS::throw_completion(JS::Value(exception));
}

}

0 comments on commit d361221

Please sign in to comment.