Truly independent web browser
LibJS+LibWeb: Parse and print WebDriver BiDi BigInts with LibJS
Shannon Boothe6bdafefdc32
diff --git a/Libraries/LibJS/Runtime/BigInt.cpp b/Libraries/LibJS/Runtime/BigInt.cppindex a5db8c5478d..1b144c91a25 100644--- a/Libraries/LibJS/Runtime/BigInt.cpp+++ b/Libraries/LibJS/Runtime/BigInt.cpp@@ -32,6 +32,16 @@ Crypto::SignedBigInteger BigInt::big_integer() constreturn Crypto::SignedBigInteger { Crypto::UnsignedBigInteger { magnitude_words }, js_bigint_is_negative(bigint) };}+GC::Ptr<BigInt> BigInt::create_from_string(VM& vm, Utf16View const& string)+{+ return cell_from_abi<BigInt>(js_bigint_create_from_string(vm_to_abi(vm), utf16_view_to_abi(string)));+}++String BigInt::to_string() const+{+ return owned_utf16_string_from_abi(js_bigint_to_string(bigint_to_abi(*this), 10)).to_utf8();+}+// The decimal digits with the "n" of a BigInt literal.Utf16String BigInt::to_utf16_string() const{diff --git a/Libraries/LibJS/Runtime/BigInt.h b/Libraries/LibJS/Runtime/BigInt.hindex 908f795bc8e..8b2751b326e 100644--- a/Libraries/LibJS/Runtime/BigInt.h+++ b/Libraries/LibJS/Runtime/BigInt.h@@ -24,9 +24,15 @@ class JS_API BigInt final : public EngineCell {public:[[nodiscard]] static GC::Ref<BigInt> create(VM&, Crypto::SignedBigInteger);+ // https://tc39.es/ecma262/#sec-stringtobigint+ [[nodiscard]] static GC::Ptr<BigInt> create_from_string(VM&, Utf16View const&);+// The integer lives in the Rust runtime's cell, so this returns a copy rather than a reference to it.Crypto::SignedBigInteger big_integer() const;+ // The decimal digits, after a "-" if it is negative, which is ToString(bigint).+ String to_string() const;+Utf16String to_utf16_string() const;};diff --git a/Libraries/LibJS/Rust/src/embedding/bigint.rs b/Libraries/LibJS/Rust/src/embedding/bigint.rsindex ac015bc6103..fda0b30090a 100644--- a/Libraries/LibJS/Rust/src/embedding/bigint.rs+++ b/Libraries/LibJS/Rust/src/embedding/bigint.rs@@ -11,11 +11,12 @@use num_bigint::Sign;use crate::embedding::abi_types::{- JSBigInt, JSOwnedUtf16String, cell_from_abi, cell_into_abi, owned_utf16_string_into_abi, vm_from_abi,+ JSBigInt, JSOwnedUtf16String, JSUtf16View, cell_from_abi, cell_into_abi, owned_utf16_string_into_abi, vm_from_abi,};use crate::layout::host_class::JSVM;use crate::runtime::big_int::{BigInt, SignedBigInteger};use crate::runtime::big_int_algorithms;+use crate::runtime::value_conversions;/// A BigInt of the magnitude in the `word_count` words at `words`, negated if `is_negative` is set. A zero magnitude/// makes 0n whatever the sign. Call on the VM's thread.@@ -42,6 +43,22 @@ pub unsafe extern "C" fn js_bigint_create_from_magnitude(cell_into_abi(BigInt::create(vm, SignedBigInteger::from_slice(sign, words)))}+/// StringToBigInt(string): the BigInt of the StringIntegerLiteral that `string` is, or null if it is not one. Call on+/// the VM's thread.+///+/// # Safety+///+/// `vm` must be the embedder's VM, and `string` a valid view.+#[unsafe(no_mangle)]+pub unsafe extern "C" fn js_bigint_create_from_string(vm: *mut JSVM, string: JSUtf16View) -> *mut JSBigInt {+ // SAFETY: The caller passes its VM and a valid view.+ let (vm, string) = unsafe { (vm_from_abi(vm), string.as_view()) };+ match string.with_utf16_code_units(value_conversions::string_to_bigint) {+ Some(big_integer) => cell_into_abi(BigInt::create(vm, big_integer)),+ None => core::ptr::null_mut(),+ }+}+/// Whether `bigint` is less than 0n. Call on the VM's thread.////// # Safetydiff --git a/Libraries/LibWeb/WebDriver/BiDi/RemoteValue.cpp b/Libraries/LibWeb/WebDriver/BiDi/RemoteValue.cppindex dff1353407c..ba3e1f5c9d7 100644--- a/Libraries/LibWeb/WebDriver/BiDi/RemoteValue.cpp+++ b/Libraries/LibWeb/WebDriver/BiDi/RemoteValue.cpp@@ -9,7 +9,6 @@#include <AK/JsonArray.h>#include <AK/Random.h>#include <AK/TemporaryChange.h>-#include <LibCrypto/BigInt/SignedBigInteger.h>#include <LibGC/RootVector.h>#include <LibJS/Runtime/AbstractOperations.h>#include <LibJS/Runtime/Array.h>@@ -202,7 +201,7 @@ static Optional<JsonObject> serialize_primitive_protocol_value(JS::Value value)// Let remote value be a map matching the script.BigIntValue production in the local end definition, with the// value property set to the result of running the ToString operation on value.remote_value.set("type"sv, "bigint"sv);- remote_value.set("value"sv, MUST(value.as_bigint().big_integer().to_base(10)));+ remote_value.set("value"sv, value.as_bigint().to_string());} else {return {};}@@ -934,50 +933,6 @@ JsonValue serialize_as_a_remote_value(JS::Value value, SerializationOptions consreturn remote_value;}-// https://tc39.es/ecma262/#sec-stringtobigint-static Optional<::Crypto::SignedBigInteger> string_to_bigint(StringView string)-{- // NB: StringIntegerLiteral allows the whitespace and line terminators that StrWhiteSpaceChar does around the- // literal, a sign only for a decimal literal, and an empty literal for 0n.- auto literal = string.trim_whitespace();- if (literal.is_empty())- return ::Crypto::SignedBigInteger { 0 };-- u16 base = 10;- if (literal.length() > 2 && literal[0] == '0') {- auto prefix = to_ascii_lowercase(literal[1]);- if (prefix == 'x')- base = 16;- else if (prefix == 'o')- base = 8;- else if (prefix == 'b')- base = 2;- if (base != 10)- literal = literal.substring_view(2);- }-- bool is_negative = false;- if (base == 10 && (literal.starts_with('-') || literal.starts_with('+'))) {- is_negative = literal[0] == '-';- literal = literal.substring_view(1);- }-- if (literal.is_empty())- return {};- for (auto character : literal) {- if (!is_ascii_hex_digit(character) || parse_ascii_hex_digit(character) >= base)- return {};- }-- auto result = ::Crypto::SignedBigInteger::from_base(base, literal);- if (result.is_error())- return {};- auto bigint = result.release_value();- if (is_negative)- bigint.negate();- return bigint;-}-// https://tc39.es/ecma262/#sec-timeclipstatic double time_clip(double time){@@ -1075,10 +1030,10 @@ static Optional<ErrorOr<JS::Value, Error>> deserialize_primitive_protocol_value(// 3. Return success with data bigint_result.if (!value.has_value() || !value->is_string())return ErrorOr<JS::Value, Error> { invalid_argument("A bigint value must have a string value"sv) };- auto bigint = string_to_bigint(value->as_string());- if (!bigint.has_value())+ auto bigint = JS::BigInt::create_from_string(vm, Utf16String::from_utf8(value->as_string()));+ if (!bigint)return ErrorOr<JS::Value, Error> { invalid_argument("A bigint value must be a valid BigInt"sv) };- return ErrorOr<JS::Value, Error> { JS::Value(JS::BigInt::create(vm, bigint.release_value())) };+ return ErrorOr<JS::Value, Error> { JS::Value(bigint.ptr()) };}return {};diff --git a/Tests/LibWeb/TestStructuredSerializeDurability.cpp b/Tests/LibWeb/TestStructuredSerializeDurability.cppindex 4cd00dc93ec..86d9a3ec87d 100644--- a/Tests/LibWeb/TestStructuredSerializeDurability.cpp+++ b/Tests/LibWeb/TestStructuredSerializeDurability.cpp@@ -161,7 +161,7 @@ TEST_CASE(value_tag_primitives_round_trip)value.append(big_integer.data(), big_integer.size());auto decoded = decode(value);EXPECT(decoded.is_bigint());- EXPECT_EQ(MUST(decoded.as_bigint().big_integer().to_base(10)), "123456789012345678901234567890"_string);+ EXPECT_EQ(decoded.as_bigint().to_string(), "123456789012345678901234567890"_string);}}@@ -188,7 +188,7 @@ TEST_CASE(value_tag_wrapper_objects_round_trip)Array<u8, 3> big_integer { 0x00, 0x03, 0xE7 }; // +999append_storage_leb128(value, big_integer.size());value.append(big_integer.data(), big_integer.size());- EXPECT_EQ(MUST(as<JS::BigIntObject>(decode(value).as_object()).bigint().big_integer().to_base(10)), "999"_string);+ EXPECT_EQ(as<JS::BigIntObject>(decode(value).as_object()).bigint().to_string(), "999"_string);}}