thisarg-primitive-sloppy.js (3688B)
1 // |reftest| async 2 // Copyright (C) 2022 Igalia, S.L. All rights reserved. 3 // This code is governed by the BSD license found in the LICENSE file. 4 5 /*--- 6 esid: sec-array.fromasync 7 description: > 8 If thisArg is a primitive, mapfn is called with a wrapper this-value or the 9 global, according to the usual rules of sloppy mode 10 info: | 11 6. If _mapping_ is *true*, then 12 a. Let _mappedValue_ be Call(_mapfn_, _thisArg_, « _nextValue_, 𝔽(_k_) »). 13 14 OrdinaryCallBindThis, when _F_.[[ThisMode]] is ~global~, where _F_ is the 15 function object: 16 6. Else, 17 a. If _thisArgument_ is *undefined* or *null*, then 18 i. Let _globalEnv_ be _calleeRealm_.[[GlobalEnv]]. 19 ii. Assert: _globalEnv_ is a Global Environment Record. 20 iii. Let _thisValue_ be _globalEnv_.[[GlobalThisValue]]. 21 b. Else, 22 i. Let _thisValue_ be ! ToObject(_thisArgument_). 23 ii. NOTE: ToObject produces wrapper objects using _calleeRealm_. 24 flags: [async, noStrict] 25 includes: [asyncHelpers.js] 26 features: [Array.fromAsync] 27 ---*/ 28 29 asyncTest(async () => { 30 await Array.fromAsync([1, 2, 3], async function () { 31 assert.sameValue( 32 this, 33 globalThis, 34 "the global should be bound as the this-value of mapfn when thisArg is undefined" 35 ); 36 }, undefined); 37 38 await Array.fromAsync([1, 2, 3], async function () { 39 assert.sameValue( 40 this, 41 globalThis, 42 "the global should be bound as the this-value of mapfn when thisArg is null" 43 ); 44 }, null); 45 46 await Array.fromAsync([1, 2, 3], async function () { 47 assert.notSameValue(this, "string", "string thisArg should not be bound as the this-value of mapfn"); 48 assert.sameValue(typeof this, "object", "a String wrapper object should be bound as the this-value of mapfn when thisArg is a string") 49 assert.sameValue(this.valueOf(), "string", "String wrapper object should have the same primitive value as thisArg"); 50 }, "string"); 51 52 await Array.fromAsync([1, 2, 3], async function () { 53 assert.notSameValue(this, 3.1416, "number thisArg should be not bound as the this-value of mapfn"); 54 assert.sameValue(typeof this, "object", "a Number wrapper object should be bound as the this-value of mapfn when thisArg is a number") 55 assert.sameValue(this.valueOf(), 3.1416, "Number wrapper object should have the same primitive value as thisArg"); 56 }, 3.1416); 57 58 await Array.fromAsync([1, 2, 3], async function () { 59 assert.notSameValue(this, 42n, "bigint thisArg should not be bound as the this-value of mapfn"); 60 assert.sameValue(typeof this, "object", "a BigInt wrapper object should be bound as the this-value of mapfn when thisArg is a bigint") 61 assert.sameValue(this.valueOf(), 42n, "BigInt wrapper object should have the same primitive value as thisArg"); 62 }, 42n); 63 64 await Array.fromAsync([1, 2, 3], async function () { 65 assert.notSameValue(this, true, "boolean thisArg should not be bound as the this-value of mapfn"); 66 assert.sameValue(typeof this, "object", "a Boolean wrapper object should be bound as the this-value of mapfn when thisArg is a boolean") 67 assert.sameValue(this.valueOf(), true, "Boolean wrapper object should have the same primitive value as thisArg"); 68 }, true); 69 70 const symbolThis = Symbol("symbol"); 71 await Array.fromAsync([1, 2, 3], async function () { 72 assert.notSameValue(this, symbolThis, "symbol thisArg should not be bound as the this-value of mapfn"); 73 assert.sameValue(typeof this, "object", "a Symbol wrapper object should be bound as the this-value of mapfn when thisArg is a symbol") 74 assert.sameValue(this.valueOf(), symbolThis, "Symbol wrapper object should have the same primitive value as thisArg"); 75 }, symbolThis); 76 });