coerce-symbol-to-prim-invocation.js (1875B)
1 // Copyright (C) 2016 the V8 project authors. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 /*--- 4 esid: sec-addition-operator-plus-runtime-semantics-evaluation 5 es6id: 12.7.3.1 6 description: Invocation of `Symbol.toPrimitive` method during coercion 7 info: | 8 [...] 9 5. Let lprim be ? ToPrimitive(lval). 10 6. Let rprim be ? ToPrimitive(rval). 11 [...] 12 13 ES6 Section 7.2.12 Abstract Equality Comparison 14 15 [...] 16 10. If Type(x) is either String, Number, or Symbol and Type(y) is Object, 17 then return the result of the comparison x == ToPrimitive(y). 18 19 ES6 Section 7.1.1 ToPrimitive ( input [, PreferredType] ) 20 21 1. If PreferredType was not passed, let hint be "default". 22 [...] 23 4. Let exoticToPrim be GetMethod(input, @@toPrimitive). 24 5. ReturnIfAbrupt(exoticToPrim). 25 6. If exoticToPrim is not undefined, then 26 a. Let result be Call(exoticToPrim, input, «hint»). 27 [...] 28 features: [Symbol.toPrimitive] 29 ---*/ 30 31 var left = {}; 32 var right = {}; 33 var log = ''; 34 var leftThisVal, rightThisVal, leftArgs, rightArgs; 35 36 left[Symbol.toPrimitive] = function() { 37 log += 'left'; 38 leftThisVal = this; 39 leftArgs = arguments; 40 }; 41 42 right[Symbol.toPrimitive] = function() { 43 log += 'right'; 44 rightThisVal = this; 45 rightArgs = arguments; 46 }; 47 48 49 left + right; 50 51 assert.sameValue(log, 'leftright', 'methods invoked in correct sequence'); 52 53 assert.sameValue(leftThisVal, left, 'left-hand side `this` value'); 54 assert.sameValue(leftArgs.length, 1, 'left-hand side argument length'); 55 assert.sameValue(leftArgs[0], 'default', 'left-hand side argument value'); 56 57 assert.sameValue(rightThisVal, right, 'right-hand side `this` value'); 58 assert.sameValue(rightArgs.length, 1, 'right-hand side argument length'); 59 assert.sameValue(rightArgs[0], 'default', 'right-hand side argument value'); 60 61 reportCompare(0, 0);