OLD | NEW |
(Empty) | |
| 1 |
| 2 |
| 3 function ASSERT(b) { |
| 4 print(b ? "OK." : "Error!") |
| 5 } |
| 6 |
| 7 |
| 8 // Getters. |
| 9 print("Getters:") |
| 10 |
| 11 function TestGet(handler) { |
| 12 var o = Proxy.create(handler) |
| 13 ASSERT(o.a === 42) |
| 14 ASSERT(o["b"] === 42) |
| 15 // ASSERT(Object.getOwnPropertyDescriptor(o, "b").value === 42) |
| 16 } |
| 17 |
| 18 TestGet({get: function(r, k) { return 42 }}) |
| 19 TestGet({getPropertyDescriptor: function(k) { return {value: 42} }}) |
| 20 TestGet({getPropertyDescriptor: function(k) { return {get value() { return 42 }}
}}) |
| 21 TestGet({get: undefined, getPropertyDescriptor: function(k) { return {value: 42}
}}) |
| 22 |
| 23 TestGet(Proxy.create({get: function(pr, pk) { return function(r, k) { return 42
} }})) |
| 24 |
| 25 |
| 26 // Setters. |
| 27 print("Setters:") |
| 28 |
| 29 var key |
| 30 var val |
| 31 function TestSet(handler) { |
| 32 var o = Proxy.create(handler) |
| 33 ASSERT(o.a = 42) |
| 34 ASSERT(key === "a") |
| 35 ASSERT(val === 42) |
| 36 ASSERT(o["b"] = 43) |
| 37 ASSERT(key === "b") |
| 38 ASSERT(val === 43) |
| 39 // ASSERT(Object.defineProperty(o, "c", {value: 44})) |
| 40 // ASSERT(key === "c") |
| 41 // ASSERT(val === 44) |
| 42 } |
| 43 |
| 44 TestSet({set: function(r, k, v) { key = k; val = v; return true }}) |
| 45 TestSet({getOwnPropertyDescriptor: function(k) { return {writable: true} }, |
| 46 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
| 47 TestSet({getOwnPropertyDescriptor: function(k) { return {get writable() { return
true }} }, |
| 48 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
| 49 TestSet({getOwnPropertyDescriptor: function(k) { return {set: function(v) { key
= k, val = v }} }}) |
| 50 TestSet({getOwnPropertyDescriptor: function(k) { return null }, |
| 51 getPropertyDescriptor: function(k) { return {writable: true} }, |
| 52 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
| 53 TestSet({getOwnPropertyDescriptor: function(k) { return null }, |
| 54 getPropertyDescriptor: function(k) { return {get writable() { return tr
ue }} }, |
| 55 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
| 56 TestSet({getOwnPropertyDescriptor: function(k) { return null }, |
| 57 getPropertyDescriptor: function(k) { return {set: function(v) { key = k
, val = v }} }}) |
| 58 TestSet({getOwnPropertyDescriptor: function(k) { return null }, |
| 59 getPropertyDescriptor: function(k) { return null }, |
| 60 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
| 61 |
| 62 TestSet(Proxy.create({get: function(pr, pk) { return function(r, k, v) { key = k
; val = v; return true } }})) |
| 63 |
| 64 |
| 65 |
| 66 // Comparison. |
| 67 print("Comparison:") |
| 68 |
| 69 var o1 = Proxy.create({}) |
| 70 var o2 = Proxy.create({}) |
| 71 |
| 72 ASSERT(o1 == o1) |
| 73 ASSERT(o2 == o2) |
| 74 ASSERT(!(o1 == o2)) |
| 75 ASSERT(!(o1 == {})) |
| 76 ASSERT(!({} == o2)) |
| 77 ASSERT(!({} == {})) |
| 78 |
| 79 ASSERT(o1 === o1) |
| 80 ASSERT(o2 === o2) |
| 81 ASSERT(!(o1 === o2)) |
| 82 ASSERT(!(o1 === {})) |
| 83 ASSERT(!({} === o2)) |
| 84 ASSERT(!({} === {})) |
| 85 |
| 86 |
| 87 |
| 88 // Type. |
| 89 print("Type:") |
| 90 |
| 91 ASSERT(typeof Proxy.create({}) == "object") |
| 92 ASSERT("object" == typeof Proxy.create({})) |
| 93 ASSERT((function(o) { return typeof o })(Proxy.create({})) == "object") |
| 94 |
| 95 // No function proxies yet. |
OLD | NEW |