Truly independent web browser
LibWeb: Make the js-string concat builtin build a rope
Andreas Kling518a97b0dd39
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/builtins.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/builtins.rsindex 81715fb0bb9..2797394629f 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/builtins.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/builtins.rs@@ -174,7 +174,7 @@ fn char_code_at(string: Gc<PrimitiveString>, index: u32) -> i32 {reason = "the steps of every builtin have the one type, and some can trap")]mod js_string_builtins {- use libjs_rust::api::{PrimitiveString, Vm};+ use libjs_rust::api::{Must, PrimitiveString, Vm};use libwasm_engine::runtime::{Trap, Value as WasmValue};use super::{@@ -391,9 +391,13 @@ mod js_string_builtins {let second = unwrap_string(vm, arguments[1])?;// 3. Return ! Call(String.prototype.concat, first, « second »).- let mut result = code_units_of(first);- result.extend(code_units_of(second));- Ok(string_reference_of_code_units(vm, &result))+ // NB: String.prototype.concat makes a rope, so code that appends piece by piece does not copy the string so far+ // on every append. It throws a RangeError if the result would be too long, which traps here instead.+ if second.length_in_utf16_code_units() >= u32::MAX as usize - first.length_in_utf16_code_units() {+ return Err(trap("Invalid string length"));+ }+ let result = PrimitiveString::create_from_concatenation(vm, first, second).must();+ Ok(string_reference(vm, result))}// https://webassembly.github.io/spec/js-api/#js-string-substringdiff --git a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-js-string-concat-many-appends.txt b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-js-string-concat-many-appends.txtnew file mode 100644index 00000000000..0c67b15f6c4--- /dev/null+++ b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-js-string-concat-many-appends.txt@@ -0,0 +1,4 @@+length: 600003+same as repeat: true+last code unit: 233+trap: true, longest is over 2^30: truediff --git a/Tests/LibWeb/Text/input/Wasm/WebAssembly-js-string-concat-many-appends.html b/Tests/LibWeb/Text/input/Wasm/WebAssembly-js-string-concat-many-appends.htmlnew file mode 100644index 00000000000..1226b705203--- /dev/null+++ b/Tests/LibWeb/Text/input/Wasm/WebAssembly-js-string-concat-many-appends.html@@ -0,0 +1,46 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ // (module+ // (import "wasm:js-string" "concat" (func $concat (param externref externref) (result (ref extern))))+ // (func (export "append") (param $piece externref) (param $count i32) (result externref)+ // (local $result externref)+ // (local.set $result (local.get $piece))+ // (block $done+ // (loop $again+ // (br_if $done (i32.eqz (local.get $count)))+ // (local.set $result (call $concat (local.get $result) (local.get $piece)))+ // (local.set $count (i32.sub (local.get $count) (i32.const 1)))+ // (br $again)))+ // (local.get $result)))+ const bytes = new Uint8Array([+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x02, 0x60, 0x02, 0x6f, 0x6f, 0x01,+ 0x64, 0x6f, 0x60, 0x02, 0x6f, 0x7f, 0x01, 0x6f, 0x02, 0x19, 0x01, 0x0e, 0x77, 0x61, 0x73, 0x6d,+ 0x3a, 0x6a, 0x73, 0x2d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x06, 0x63, 0x6f, 0x6e, 0x63, 0x61,+ 0x74, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x07, 0x0a, 0x01, 0x06, 0x61, 0x70, 0x70, 0x65, 0x6e,+ 0x64, 0x00, 0x01, 0x0a, 0x28, 0x01, 0x26, 0x01, 0x01, 0x6f, 0x20, 0x00, 0x21, 0x02, 0x02, 0x40,+ 0x03, 0x40, 0x20, 0x01, 0x45, 0x0d, 0x01, 0x20, 0x02, 0x20, 0x00, 0x10, 0x00, 0x21, 0x02, 0x20,+ 0x01, 0x41, 0x01, 0x6b, 0x21, 0x01, 0x0c, 0x00, 0x0b, 0x0b, 0x20, 0x02, 0x0b,+ ]);++ test(() => {+ const module = new WebAssembly.Module(bytes, { builtins: ["js-string"] });+ const { append } = new WebAssembly.Instance(module, {}).exports;++ // Appending a piece at a time copies nothing, so many appends take as long as the pieces do to write.+ const appended = append("abé", 200000);+ println(`length: ${appended.length}`);+ println(`same as repeat: ${appended === "abé".repeat(200001)}`);+ println(`last code unit: ${appended.charCodeAt(appended.length - 1)}`);++ // Doubling gets past the longest string there can be in a few appends, which traps.+ let doubled = "x";+ try {+ for (let i = 0; i < 40; ++i)+ doubled = append(doubled, 1);+ println("no trap");+ } catch (error) {+ println(`trap: ${error instanceof WebAssembly.RuntimeError}, longest is over 2^30: ${doubled.length > 2 ** 30}`);+ }+ });+</script>