@@ -19192,15 +19192,40 @@ pub const Interpreter = struct {
1919219192
1919319193 pub fn applyBinary(self: *Interpreter, op_in: ast.BinaryOp, l_in: Value, r_in: Value) EvalError!Value {
1919419194 const op = op_in;
19195+ const l_object = l_in.isObject() and !l_in.asObj().is_bigint and !l_in.asObj().is_symbol;
19196+ const r_object = r_in.isObject() and !r_in.asObj().is_bigint and !r_in.asObj().is_symbol;
19197+ const has_observable_coercion = switch (op) {
19198+ .add, .sub, .mul, .div, .mod, .pow, .lt, .le, .gt, .ge, .bit_and, .bit_or, .bit_xor, .shl, .shr, .ushr => l_object or r_object,
19199+ .eq, .neq => (l_object and !r_object and !r_in.isUndefined() and !r_in.isNull()) or
19200+ (r_object and !l_object and !l_in.isUndefined() and !l_in.isNull()),
19201+ else => false,
19202+ };
19203+ // Primitive-only arithmetic stays on the existing root-free fast path.
19204+ // Once either side can run user code, publish both originals before the
19205+ // first ToPrimitive so a moving checkpoint cannot stale the other side.
19206+ const roots_mark: ?usize = if (self.gc != null and has_observable_coercion) roots: {
19207+ const mark = try self.pushTempRoot(l_in);
19208+ errdefer self.restoreTempRoots(mark);
19209+ const right_root = try self.pushTempRoot(r_in);
19210+ std.debug.assert(right_root == mark + 1);
19211+ break :roots mark;
19212+ } else null;
19213+ defer if (roots_mark) |mark| self.restoreTempRoots(mark);
19214+
1919519215 var l = l_in;
1919619216 var r = r_in;
1919719217 // Coerce object operands to primitives first (ToPrimitive). `+` uses the
1919819218 // "default" hint; the other arithmetic / relational / bitwise ops use
1919919219 // "number". Equality and instanceof/in handle objects themselves.
1920019220 switch (op) {
1920119221 .add => {
19202- l = try self.toPrimitive(l, .default);
19203- r = try self.toPrimitive(r, .default);
19222+ l = try self.toPrimitive(if (roots_mark) |mark| self.tempRoot(mark, l) else l, .default);
19223+ if (roots_mark) |mark| self.setTempRoot(mark, l);
19224+ r = try self.toPrimitive(if (roots_mark) |mark| self.tempRoot(mark + 1, r) else r, .default);
19225+ if (roots_mark) |mark| {
19226+ self.setTempRoot(mark + 1, r);
19227+ l = self.tempRoot(mark, l);
19228+ }
1920419229 },
1920519230 .sub, .mul, .div, .mod, .pow, .lt, .le, .gt, .ge, .bit_and, .bit_or, .bit_xor, .shl, .shr, .ushr => {
1920619231 // ApplyStringOrNumericBinaryOperator evaluates ToNumeric(lhs)
@@ -19209,10 +19234,15 @@ pub const Interpreter = struct {
1920919234 // each operand in order: a throwing lhs conversion must abort
1921019235 // before the rhs operand is coerced at all (its `valueOf` must not
1921119236 // run). (BigInt mixing is diagnosed later, after both ToNumerics.)
19212- l = try self.toPrimitive(l, .number);
19237+ l = try self.toPrimitive(if (roots_mark) |mark| self.tempRoot(mark, l) else l, .number);
19238+ if (roots_mark) |mark| self.setTempRoot(mark, l);
1921319239 if (l.isObject() and l.asObj().is_symbol)
1921419240 return self.throwError("TypeError", "Cannot convert a Symbol value to a number");
19215- r = try self.toPrimitive(r, .number);
19241+ r = try self.toPrimitive(if (roots_mark) |mark| self.tempRoot(mark + 1, r) else r, .number);
19242+ if (roots_mark) |mark| {
19243+ self.setTempRoot(mark + 1, r);
19244+ l = self.tempRoot(mark, l);
19245+ }
1921619246 if (r.isObject() and r.asObj().is_symbol)
1921719247 return self.throwError("TypeError", "Cannot convert a Symbol value to a number");
1921819248 },
@@ -19224,13 +19254,21 @@ pub const Interpreter = struct {
1922419254 // objects compare by identity, and an object vs null/undefined is
1922519255 // always unequal, so neither is coerced. A BigInt/Symbol value is
1922619256 // boxed as an object here but counts as a primitive operand.
19227- const l_obj = l.isObject() and !l.asObj().is_bigint and !l.asObj().is_symbol;
19228- const r_obj = r.isObject() and !r.asObj().is_bigint and !r.asObj().is_symbol;
19229- if (l_obj and !r_obj and !r.isUndefined() and !r.isNull()) l = try self.toPrimitive(l, .default);
19230- if (r_obj and !l_obj and !l.isUndefined() and !l.isNull()) r = try self.toPrimitive(r, .default);
19257+ if (l_object and !r_object and !r.isUndefined() and !r.isNull()) {
19258+ l = try self.toPrimitive(if (roots_mark) |mark| self.tempRoot(mark, l) else l, .default);
19259+ if (roots_mark) |mark| self.setTempRoot(mark, l);
19260+ }
19261+ if (r_object and !l_object and !l.isUndefined() and !l.isNull()) {
19262+ r = try self.toPrimitive(if (roots_mark) |mark| self.tempRoot(mark + 1, r) else r, .default);
19263+ if (roots_mark) |mark| self.setTempRoot(mark + 1, r);
19264+ }
1923119265 },
1923219266 else => {},
1923319267 }
19268+ if (roots_mark) |mark| {
19269+ l = self.tempRoot(mark, l);
19270+ r = self.tempRoot(mark + 1, r);
19271+ }
1923419272 // (Symbol operands for the number-hint ops above were already rejected
1923519273 // in-order during their ToNumeric coercion.)
1923619274 // BigInt operands. Arithmetic/bitwise require both to be BigInt (mixing
@@ -53487,6 +53525,29 @@ test "variadic string concat uses one exact canonical construction" {
5348753525 try std.testing.expectEqualStrings("RAB|rab", try receiver_order.asWtf8(a));
5348853526}
5348953527
53528+ test "binary observable coercions remain once-only and left-to-right" {
53529+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
53530+ defer arena.deinit();
53531+ const result = try evalSource(arena.allocator(),
53532+ \\let log = '';
53533+ \\function operand(mark, value, fail) {
53534+ \\ return { valueOf() { log += mark; if (fail) throw 1; return value; } };
53535+ \\}
53536+ \\const results = [];
53537+ \\results.push(operand('a', 'A', false) + operand('b', 'B', false));
53538+ \\results.push(operand('c', 7, false) - operand('d', 2, false));
53539+ \\results.push(operand('e', 2, false) < operand('f', 3, false));
53540+ \\results.push(operand('g', 6, false) & operand('h', 3, false));
53541+ \\results.push(operand('i', 5, false) == 5);
53542+ \\results.push(5 == operand('j', 5, false));
53543+ \\try { operand('k', 0, true) + operand('x', 1, false); } catch {}
53544+ \\function* vmBinary(left, right) { return left + right; }
53545+ \\results.push(vmBinary(operand('l', 'L', false), operand('m', 'M', false)).next().value);
53546+ \\results.join(',') + '|' + log
53547+ );
53548+ try std.testing.expectEqualStrings("AB,5,true,2,true,true,LM|abcdefghijklm", try result.asWtf8(arena.allocator()));
53549+ }
53550+
5349053551test "ToString canonicalizes object-produced StringData" {
5349153552 var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
5349253553 defer arena.deinit();
0 commit comments