Truly independent web browser
LibJS: Don't convert a constant property key to a primitive in place
Shannon Booth4242e237bf6c
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen.rs b/Libraries/LibJS/Rust/src/bytecode/codegen.rsindex dff785472e5..0f8666bde90 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen.rs@@ -4362,6 +4362,20 @@ fn emit_update_op(generator: &mut Generator, op: UpdateOp, prefixed: bool, value}}+/// Convert a property name to the primitive that ToPropertyKey turns into a property key without observable effects,+/// in a register of its own unless it is a constant, which is a primitive already.+fn emit_to_primitive_property_key(generator: &mut Generator, property_name: &ScopedOperand) -> ScopedOperand {+ if property_name.operand().is_constant() {+ return property_name.clone();+ }+ let property_name = generator.copy_if_needed_to_preserve_evaluation_order(property_name);+ generator.emit(Instruction::ToPrimitiveWithStringHint {+ dst: property_name.operand(),+ value: property_name.operand(),+ });+ property_name+}+/// Preserve the primitive key produced by GetValue so PutValue does not convert it again.fn emit_read_modify_write_property_key(generator: &mut Generator,@@ -6287,12 +6301,7 @@ fn generate_object_expression(let effectively_computed = property.is_computed && !is_string_literal_key;let computed_key = if effectively_computed || !is_string_key {let key = generate_expression_or_undefined(&property.key, generator, None);- let key = generator.copy_if_needed_to_preserve_evaluation_order(&key);- generator.emit(Instruction::ToPrimitiveWithStringHint {- dst: key.operand(),- value: key.operand(),- });- Some(key)+ Some(emit_to_primitive_property_key(generator, &key))} else {None};@@ -8962,11 +8971,7 @@ fn generate_object_binding_pattern(}Some(BindingEntryName::Expression(expression)) => {let property_name = generate_expression_or_undefined(expression, generator, None);- let property_name = generator.copy_if_needed_to_preserve_evaluation_order(&property_name);- generator.emit(Instruction::ToPrimitiveWithStringHint {- dst: property_name.operand(),- value: property_name.operand(),- });+ let property_name = emit_to_primitive_property_key(generator, &property_name);if has_rest {excluded_names.push(property_name.clone());}diff --git a/Tests/LibJS/Bytecode/expected/for-in-object-property-iterator-next.txt b/Tests/LibJS/Bytecode/expected/for-in-object-property-iterator-next.txtindex fb82a7b9735..78234480088 100644--- a/Tests/LibJS/Bytecode/expected/for-in-object-property-iterator-next.txt+++ b/Tests/LibJS/Bytecode/expected/for-in-object-property-iterator-next.txt@@ -1,4 +1,4 @@-$a8285fc4 for-in-object-property-iterator-next.js:8:1+$b8c00624 for-in-object-property-iterator-next.js:8:1Registers: 8Blocks: 1Constants:@@ -13,13 +13,11 @@ block0:[ 0] Enter[ 8] GetGlobal value_feedback:0, dst:reg6, `collect`[ 18] NewObject dst:reg7- [ 28] ToPrimitiveWithStringHint dst:Int32(2), value:Int32(2)- [ 38] PutByValue keyed_feedback:0, base:reg7, property:Int32(2), src:String("two"), kind:Own- [ 58] PutById base:reg7, `foo`, src:String("foo"), kind:Own- [ 78] ToPrimitiveWithStringHint dst:Int32(7), value:Int32(7)- [ 88] PutByValue keyed_feedback:1, base:reg7, property:Int32(7), src:String("seven"), kind:Own- [ a8] Call value_feedback:1, call_feedback:0, dst:reg5, callee:reg6, this_value:Undefined, collect, arguments:[reg7]- [ d0] End value:reg5+ [ 28] PutByValue keyed_feedback:0, base:reg7, property:Int32(2), src:String("two"), kind:Own+ [ 48] PutById base:reg7, `foo`, src:String("foo"), kind:Own+ [ 68] PutByValue keyed_feedback:1, base:reg7, property:Int32(7), src:String("seven"), kind:Own+ [ 88] Call value_feedback:1, call_feedback:0, dst:reg5, callee:reg6, this_value:Undefined, collect, arguments:[reg7]+ [ b0] End value:reg5collect$d9304438 for-in-object-property-iterator-next.js:2:18diff --git a/Tests/LibJS/Bytecode/expected/indexed-append-guards.txt b/Tests/LibJS/Bytecode/expected/indexed-append-guards.txtindex b6f309a7de0..ae80bb3847c 100644--- a/Tests/LibJS/Bytecode/expected/indexed-append-guards.txt+++ b/Tests/LibJS/Bytecode/expected/indexed-append-guards.txt@@ -1,4 +1,4 @@-$7fde02a5 indexed-append-guards.js:16:1+$8829d5d5 indexed-append-guards.js:16:1Registers: 11Blocks: 7Constants:@@ -18,62 +18,61 @@ block0:[ 8] GetGlobal value_feedback:0, dst:reg6, `Object`[ 18] GetById value_feedback:1, dst:reg7, base:reg6, `preventExtensions` (Object.preventExtensions)[ 30] NewObject dst:reg8- [ 40] ToPrimitiveWithStringHint dst:Int32(0), value:Int32(0)- [ 50] PutByValue keyed_feedback:0, base:reg8, property:Int32(0), src:Int32(1), kind:Own- [ 70] Call value_feedback:2, call_feedback:0, dst:reg5, callee:reg7, this_value:reg6, Object.preventExtensions, arguments:[reg8]- [ 98] DynamicInitializeLexicalBinding `object`, src:reg5- [ a8] GetGlobal value_feedback:3, dst:reg7, `expect_type_error`- [ b8] NewFunction dst:reg6, shared_function_data_index:0- [ d0] Call value_feedback:4, call_feedback:1, dst:reg5, callee:reg7, this_value:Undefined, expect_type_error, arguments:[reg6]- [ f8] GetGlobal value_feedback:5, dst:reg7, `object`- [ 108] GetByValue value_feedback:6, keyed_feedback:1, dst:reg6, base:reg7, property:Int32(1) (object[Int32(1)])- [ 128] StrictlyInequals arith_feedback:0, dst:reg7, lhs:reg6, rhs:Undefined- [ 138] MovSrcUndefined dst:reg6- [ 140] JumpFalse condition:reg7, target:block2+ [ 40] PutByValue keyed_feedback:0, base:reg8, property:Int32(0), src:Int32(1), kind:Own+ [ 60] Call value_feedback:2, call_feedback:0, dst:reg5, callee:reg7, this_value:reg6, Object.preventExtensions, arguments:[reg8]+ [ 88] DynamicInitializeLexicalBinding `object`, src:reg5+ [ 98] GetGlobal value_feedback:3, dst:reg7, `expect_type_error`+ [ a8] NewFunction dst:reg6, shared_function_data_index:0+ [ c0] Call value_feedback:4, call_feedback:1, dst:reg5, callee:reg7, this_value:Undefined, expect_type_error, arguments:[reg6]+ [ e8] GetGlobal value_feedback:5, dst:reg7, `object`+ [ f8] GetByValue value_feedback:6, keyed_feedback:1, dst:reg6, base:reg7, property:Int32(1) (object[Int32(1)])+ [ 118] StrictlyInequals arith_feedback:0, dst:reg7, lhs:reg6, rhs:Undefined+ [ 128] MovSrcUndefined dst:reg6+ [ 130] JumpFalse condition:reg7, target:block2block1:- [ 150] GetGlobal value_feedback:7, dst:reg9, `Error`- [ 160] CallConstruct value_feedback:8, call_feedback:2, dst:reg8, callee:reg9, Error, arguments:[String("Indexed object append mutated")]- [ 188] Throw src:reg8+ [ 140] GetGlobal value_feedback:7, dst:reg9, `Error`+ [ 150] CallConstruct value_feedback:8, call_feedback:2, dst:reg8, callee:reg9, Error, arguments:[String("Indexed object append mutated")]+ [ 178] Throw src:reg8block2:- [ 190] NewPrimitiveArray dst:reg5, elements:[1, 2]- [ 1b0] DynamicInitializeLexicalBinding `array`, src:reg5- [ 1c0] GetGlobal value_feedback:9, dst:reg7, `Object`- [ 1d0] GetById value_feedback:10, dst:reg8, base:reg7, `defineProperty` (Object.defineProperty)- [ 1e8] GetGlobal value_feedback:11, dst:reg9, `array`- [ 1f8] NewObject dst:reg10- [ 208] InitObjectLiteralProperty object:reg10, `writable`, src:Bool(false), shape_cache_index:0, property_slot:0- [ 220] CacheObjectShape object:reg10- [ 230] Call value_feedback:12, call_feedback:3, dst:reg5, callee:reg8, this_value:reg7, Object.defineProperty, arguments:[reg9, String("length"), reg10]- [ 260] GetGlobal value_feedback:13, dst:reg8, `expect_type_error`- [ 270] NewFunction dst:reg7, shared_function_data_index:1- [ 288] Call value_feedback:14, call_feedback:4, dst:reg6, callee:reg8, this_value:Undefined, expect_type_error, arguments:[reg7]- [ 2b0] GetGlobal value_feedback:15, dst:reg5, `array`- [ 2c0] GetLength value_feedback:16, keyed_feedback:2, dst:reg8, base:reg5 (array.length)- [ 2d8] StrictlyInequalsRhsInt32 arith_feedback:1, dst:reg5, lhs:reg8, rhs:2- [ 2e8] MovSrcUndefined dst:reg8- [ 2f0] JumpFalse condition:reg5, target:block4+ [ 180] NewPrimitiveArray dst:reg5, elements:[1, 2]+ [ 1a0] DynamicInitializeLexicalBinding `array`, src:reg5+ [ 1b0] GetGlobal value_feedback:9, dst:reg7, `Object`+ [ 1c0] GetById value_feedback:10, dst:reg8, base:reg7, `defineProperty` (Object.defineProperty)+ [ 1d8] GetGlobal value_feedback:11, dst:reg9, `array`+ [ 1e8] NewObject dst:reg10+ [ 1f8] InitObjectLiteralProperty object:reg10, `writable`, src:Bool(false), shape_cache_index:0, property_slot:0+ [ 210] CacheObjectShape object:reg10+ [ 220] Call value_feedback:12, call_feedback:3, dst:reg5, callee:reg8, this_value:reg7, Object.defineProperty, arguments:[reg9, String("length"), reg10]+ [ 250] GetGlobal value_feedback:13, dst:reg8, `expect_type_error`+ [ 260] NewFunction dst:reg7, shared_function_data_index:1+ [ 278] Call value_feedback:14, call_feedback:4, dst:reg6, callee:reg8, this_value:Undefined, expect_type_error, arguments:[reg7]+ [ 2a0] GetGlobal value_feedback:15, dst:reg5, `array`+ [ 2b0] GetLength value_feedback:16, keyed_feedback:2, dst:reg8, base:reg5 (array.length)+ [ 2c8] StrictlyInequalsRhsInt32 arith_feedback:1, dst:reg5, lhs:reg8, rhs:2+ [ 2d8] MovSrcUndefined dst:reg8+ [ 2e0] JumpFalse condition:reg5, target:block4block3:- [ 300] GetGlobal value_feedback:17, dst:reg10, `Error`- [ 310] CallConstruct value_feedback:18, call_feedback:5, dst:reg7, callee:reg10, Error, arguments:[String("Array length changed")]- [ 338] Throw src:reg7+ [ 2f0] GetGlobal value_feedback:17, dst:reg10, `Error`+ [ 300] CallConstruct value_feedback:18, call_feedback:5, dst:reg7, callee:reg10, Error, arguments:[String("Array length changed")]+ [ 328] Throw src:reg7block4:- [ 340] GetGlobal value_feedback:19, dst:reg6, `array`- [ 350] GetByValue value_feedback:20, keyed_feedback:3, dst:reg5, base:reg6, property:Int32(2) (array[Int32(2)])- [ 370] StrictlyInequals arith_feedback:2, dst:reg6, lhs:reg5, rhs:Undefined- [ 380] MovSrcUndefined dst:reg5- [ 388] JumpFalse condition:reg6, target:block6+ [ 330] GetGlobal value_feedback:19, dst:reg6, `array`+ [ 340] GetByValue value_feedback:20, keyed_feedback:3, dst:reg5, base:reg6, property:Int32(2) (array[Int32(2)])+ [ 360] StrictlyInequals arith_feedback:2, dst:reg6, lhs:reg5, rhs:Undefined+ [ 370] MovSrcUndefined dst:reg5+ [ 378] JumpFalse condition:reg6, target:block6block5:- [ 398] GetGlobal value_feedback:21, dst:reg10, `Error`- [ 3a8] CallConstruct value_feedback:22, call_feedback:6, dst:reg7, callee:reg10, Error, arguments:[String("Array append mutated")]- [ 3d0] Throw src:reg7+ [ 388] GetGlobal value_feedback:21, dst:reg10, `Error`+ [ 398] CallConstruct value_feedback:22, call_feedback:6, dst:reg7, callee:reg10, Error, arguments:[String("Array append mutated")]+ [ 3c0] Throw src:reg7block6:- [ 3d8] End value:reg5+ [ 3c8] End value:reg5expect_type_error$fc915998 indexed-append-guards.js:4:5diff --git a/Tests/LibJS/Bytecode/expected/numeric-getter-no-lhs-name.txt b/Tests/LibJS/Bytecode/expected/numeric-getter-no-lhs-name.txtindex d6dfd7657b5..e64b5e18bab 100644--- a/Tests/LibJS/Bytecode/expected/numeric-getter-no-lhs-name.txt+++ b/Tests/LibJS/Bytecode/expected/numeric-getter-no-lhs-name.txt@@ -1,4 +1,4 @@-$0656c3bd numeric-getter-no-lhs-name.js:1:1+$091ab4cd numeric-getter-no-lhs-name.js:1:1Registers: 7Blocks: 1Constants:@@ -7,14 +7,13 @@ $0656c3bd numeric-getter-no-lhs-name.js:1:1block0:[ 0] Enter[ 8] NewObject dst:reg5- [ 18] ToPrimitiveWithStringHint dst:Int32(0), value:Int32(0)- [ 28] NewFunction dst:reg6, shared_function_data_index:0, home_object:reg5- [ 40] SetFunctionName function:reg6, name:Int32(0)- [ 50] PutByValue keyed_feedback:0, base:reg5, property:Int32(0), src:reg6, kind:Getter- [ 70] SetGlobal `obj`, src:reg5- [ 80] GetGlobal value_feedback:0, dst:reg5, `obj`- [ 90] GetByValue value_feedback:1, keyed_feedback:1, dst:reg6, base:reg5, property:Int32(0) (obj[Int32(0)])- [ b0] End value:reg6+ [ 18] NewFunction dst:reg6, shared_function_data_index:0, home_object:reg5+ [ 30] SetFunctionName function:reg6, name:Int32(0)+ [ 40] PutByValue keyed_feedback:0, base:reg5, property:Int32(0), src:reg6, kind:Getter+ [ 60] SetGlobal `obj`, src:reg5+ [ 70] GetGlobal value_feedback:0, dst:reg5, `obj`+ [ 80] GetByValue value_feedback:1, keyed_feedback:1, dst:reg6, base:reg5, property:Int32(0) (obj[Int32(0)])+ [ a0] End value:reg6get 0$d054d509 numeric-getter-no-lhs-name.js:3:9diff --git a/Tests/LibJS/Bytecode/expected/numeric-index-object-literal.txt b/Tests/LibJS/Bytecode/expected/numeric-index-object-literal.txtindex 061d0857d12..28f6e28b4b9 100644--- a/Tests/LibJS/Bytecode/expected/numeric-index-object-literal.txt+++ b/Tests/LibJS/Bytecode/expected/numeric-index-object-literal.txt@@ -11,7 +11,7 @@ block0:[ 38] End value:reg5-test$7912604c numeric-index-object-literal.js:2:5+test$815e337c numeric-index-object-literal.js:2:5Registers: 6Blocks: 1Constants:@@ -25,10 +25,7 @@ test$7912604c numeric-index-object-literal.js:2:5block0:[ 0] Enter[ 8] NewObject dst:reg5- [ 18] ToPrimitiveWithStringHint dst:Int32(100), value:Int32(100)- [ 28] PutByValue keyed_feedback:0, base:reg5, property:Int32(100), src:Int32(1), kind:Own- [ 48] ToPrimitiveWithStringHint dst:Int32(200), value:Int32(200)- [ 58] PutByValue keyed_feedback:1, base:reg5, property:Int32(200), src:Int32(2), kind:Own- [ 78] ToPrimitiveWithStringHint dst:Int32(300), value:Int32(300)- [ 88] PutByValue keyed_feedback:2, base:reg5, property:Int32(300), src:Int32(3), kind:Own- [ a8] Return value:reg5+ [ 18] PutByValue keyed_feedback:0, base:reg5, property:Int32(100), src:Int32(1), kind:Own+ [ 38] PutByValue keyed_feedback:1, base:reg5, property:Int32(200), src:Int32(2), kind:Own+ [ 58] PutByValue keyed_feedback:2, base:reg5, property:Int32(300), src:Int32(3), kind:Own+ [ 78] Return value:reg5diff --git a/Tests/LibJS/Bytecode/expected/numeric-property-key.txt b/Tests/LibJS/Bytecode/expected/numeric-property-key.txtindex 80d8de95eac..6c9f34aced4 100644--- a/Tests/LibJS/Bytecode/expected/numeric-property-key.txt+++ b/Tests/LibJS/Bytecode/expected/numeric-property-key.txt@@ -1,4 +1,4 @@-$312160b0 numeric-property-key.js:4:1+$28d58d80 numeric-property-key.js:4:1Registers: 6Blocks: 1Constants:@@ -9,7 +9,6 @@ $312160b0 numeric-property-key.js:4:1block0:[ 0] Enter[ 8] NewObject dst:reg5- [ 18] ToPrimitiveWithStringHint dst:Int32(20), value:Int32(20)- [ 28] PutByValue keyed_feedback:0, base:reg5, property:Int32(20), src:Int32(3), kind:Own- [ 48] SetGlobal `obj`, src:reg5- [ 58] End value:Undefined+ [ 18] PutByValue keyed_feedback:0, base:reg5, property:Int32(20), src:Int32(3), kind:Own+ [ 38] SetGlobal `obj`, src:reg5+ [ 48] End value:Undefined