-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
exec.js
80 lines (68 loc) 路 2.02 KB
/
exec.js
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
73
74
75
76
77
78
79
80
function dec({ get, set }, context) {
context.addInitializer(function() {
this[context.name + 'Context'] = context;
});
return {
get() {
return get.call(this) + 1;
},
set(v) {
set.call(this, v + 1);
},
init(v) {
return v ? v : 1;
}
}
}
class Foo {
@dec
accessor a;
@dec
accessor b = 123;
@dec
accessor ['c'] = 456;
}
let foo = new Foo();
const aContext = foo['aContext'];
const bContext = foo['bContext'];
const cContext = foo['cContext'];
expect(aContext.access.has(foo)).toBe(true);
expect(aContext.access.has({})).toBe(false);
expect(aContext.access.has(Object.create(foo))).toBe(true);
expect(aContext.access.has({ a: 1 })).toBe(true);
expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true);
expect(foo.a).toBe(2);
expect(aContext.access.get(foo)).toBe(2);
foo.a = 123;
expect(foo.a).toBe(125);
expect(aContext.access.get(foo)).toBe(125);
aContext.access.set(foo, 456);
expect(foo.a).toBe(458);
expect(aContext.access.get(foo)).toBe(458);
expect(aContext.name).toBe('a');
expect(aContext.kind).toBe('accessor');
expect(aContext.static).toBe(false);
expect(aContext.private).toBe(false);
expect(typeof aContext.addInitializer).toBe('function');
expect(foo.hasOwnProperty('a')).toBe(false);
expect(Foo.prototype.hasOwnProperty('a')).toBe(true);
expect(foo.b).toBe(124);
foo.b = 123;
expect(foo.b).toBe(125);
expect(bContext.name).toBe('b');
expect(bContext.kind).toBe('accessor');
expect(bContext.static).toBe(false);
expect(bContext.private).toBe(false);
expect(typeof bContext.addInitializer).toBe('function');
expect(foo.hasOwnProperty('b')).toBe(false);
expect(Foo.prototype.hasOwnProperty('b')).toBe(true);
expect(foo.c).toBe(457);
foo.c = 456;
expect(foo.c).toBe(458);
expect(cContext.name).toBe('c');
expect(cContext.kind).toBe('accessor');
expect(cContext.static).toBe(false);
expect(cContext.private).toBe(false);
expect(typeof cContext.addInitializer).toBe('function');
expect(foo.hasOwnProperty('c')).toBe(false);
expect(Foo.prototype.hasOwnProperty('c')).toBe(true);