summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Reflect/get.js
blob: b38b188b03cdf96dea240a85fd881a50f1946009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/ */

// Reflect.get gets the value of a property.

var x = {p: 1};
assertEq(Reflect.get(x, "p"), 1);
assertEq(Reflect.get(x, "toString"), Object.prototype.toString);
assertEq(Reflect.get(x, "missing"), undefined);


// === Various targets

// Array
assertEq(Reflect.get([], 700), undefined);
assertEq(Reflect.get(["zero", "one"], 1), "one");

// TypedArray
assertEq(Reflect.get(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]), 7), 7);

// Treatment of NaN
var f = new Float64Array([NaN]);
var u = new Uint32Array(f.buffer);
u[0]++;
u[1]++;
assertEq(f[0], NaN);
assertEq(Reflect.get(f, 0), NaN);

// Proxy with no get handler
assertEq(Reflect.get(new Proxy(x, {}), "p"), 1);

// Proxy with a get handler
var obj = new Proxy(x, {
    get(t, k, r) { return k + "ful"; }
});
assertEq(Reflect.get(obj, "mood"), "moodful");

// Exceptions thrown by a proxy's get handler are propagated.
assertThrowsInstanceOf(() => Reflect.get(obj, Symbol()), TypeError);

// Ordinary object, property has a setter and no getter
obj = {set name(x) {}};
assertEq(Reflect.get(obj, "name"), undefined);


// === Receiver

// Receiver argument is passed to getters as the this-value.
obj = { get x() { return this; }};
assertEq(Reflect.get(obj, "x", Math), Math);
assertEq(Reflect.get(Object.create(obj), "x", JSON), JSON);

// If missing, target is passed instead.
assertEq(Reflect.get(obj, "x"), obj);

// Receiver argument is passed to the proxy get handler.
obj = new Proxy({}, {
    get(t, k, r) {
        assertEq(k, "itself");
        return r;
    }
});
assertEq(Reflect.get(obj, "itself"), obj);
assertEq(Reflect.get(obj, "itself", Math), Math);
assertEq(Reflect.get(Object.create(obj), "itself", Math), Math);

// The receiver shouldn't have to be an object
assertEq(Reflect.get(obj, "itself", 37.2), 37.2);

// For more Reflect.get tests, see target.js and propertyKeys.js.

reportCompare(0, 0);