forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectwrap.js
209 lines (170 loc) · 5.21 KB
/
objectwrap.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const test = (binding) => {
const Test = binding.objectwrap.Test;
const testValue = (obj, clazz) => {
assert.strictEqual(obj.testValue, true);
assert.strictEqual(obj[clazz.kTestValueInternal], false);
};
const testAccessor = (obj, clazz) => {
// read-only, write-only
{
obj.testSetter = 'instance getter';
assert.strictEqual(obj.testGetter, 'instance getter');
obj.testSetter = 'instance getter 2';
assert.strictEqual(obj.testGetter, 'instance getter 2');
}
// read write-only
{
let error;
try { const read = obj.testSetter; } catch (e) { error = e; }
// no error
assert.strictEqual(error, undefined);
// read is undefined
assert.strictEqual(obj.testSetter, undefined);
}
// write read-only
{
let error;
try { obj.testGetter = 'write'; } catch (e) { error = e; }
assert.strictEqual(error.name, 'TypeError');
}
// rw
{
obj.testGetSet = 'instance getset';
assert.strictEqual(obj.testGetSet, 'instance getset');
obj.testGetSet = 'instance getset 2';
assert.strictEqual(obj.testGetSet, 'instance getset 2');
}
// rw symbol
{
obj[clazz.kTestAccessorInternal] = 'instance internal getset';
assert.strictEqual(obj[clazz.kTestAccessorInternal], 'instance internal getset');
obj[clazz.kTestAccessorInternal] = 'instance internal getset 2';
assert.strictEqual(obj[clazz.kTestAccessorInternal], 'instance internal getset 2');
}
};
const testMethod = (obj, clazz) => {
assert.strictEqual(obj.testMethod('method'), 'method instance');
assert.strictEqual(obj[clazz.kTestMethodInternal]('method'), 'method instance internal');
};
const testEnumerables = (obj, clazz) => {
// Object.keys: only object
assert.deepEqual(Object.keys(obj), []);
// for..in: object + prototype
{
const keys = [];
for (let key in obj) {
keys.push(key);
}
assert(keys.length = 4);
assert(obj.testGetSet);
assert(obj.testGetter);
assert(obj.testValue);
assert(obj.testMethod);
}
};
const testConventions = (obj, clazz) => {
// test @@toStringTag
{
assert.strictEqual(obj[Symbol.toStringTag], 'TestTag');
assert.strictEqual('' + obj, '[object TestTag]');
}
// test @@iterator
{
obj.testSetter = 'iterator';
const values = [];
for (let item of obj) {
values.push(item);
}
assert.deepEqual(values, ['iterator']);
}
};
const testStaticValue = (clazz) => {
assert.strictEqual(clazz.testStaticValue, 'value');
assert.strictEqual(clazz[clazz.kTestStaticValueInternal], 5);
}
const testStaticAccessor = (clazz) => {
// read-only, write-only
{
const tempObj = {};
clazz.testStaticSetter = tempObj;
assert.strictEqual(clazz.testStaticGetter, tempObj);
const tempArray = [];
clazz.testStaticSetter = tempArray;
assert.strictEqual(clazz.testStaticGetter, tempArray);
}
// read write-only
{
let error;
try { const read = clazz.testStaticSetter; } catch (e) { error = e; }
// no error
assert.strictEqual(error, undefined);
// read is undefined
assert.strictEqual(clazz.testStaticSetter, undefined);
}
// write-read-only
{
let error;
try { clazz.testStaticGetter = 'write'; } catch (e) { error = e; }
assert.strictEqual(error.name, 'TypeError');
}
// rw
{
clazz.testStaticGetSet = 9;
assert.strictEqual(clazz.testStaticGetSet, 9);
clazz.testStaticGetSet = 4;
assert.strictEqual(clazz.testStaticGetSet, 4);
}
// rw symbol
{
clazz[clazz.kTestStaticAccessorInternal] = 'static internal getset';
assert.strictEqual(clazz[clazz.kTestStaticAccessorInternal], 'static internal getset');
}
};
const testStaticMethod = (clazz) => {
assert.strictEqual(clazz.testStaticMethod('method'), 'method static');
assert.strictEqual(clazz[clazz.kTestStaticMethodInternal]('method'), 'method static internal');
};
const testStaticEnumerables = (clazz) => {
// Object.keys
assert.deepEqual(Object.keys(clazz), [
'testStaticValue',
'testStaticGetter',
'testStaticGetSet',
'testStaticMethod'
]);
// for..in
{
const keys = [];
for (let key in clazz) {
keys.push(key);
}
assert.deepEqual(keys, [
'testStaticValue',
'testStaticGetter',
'testStaticGetSet',
'testStaticMethod'
]);
}
};
const testObj = (obj, clazz) => {
testValue(obj, clazz);
testAccessor(obj, clazz);
testMethod(obj, clazz);
testEnumerables(obj, clazz);
testConventions(obj, clazz);
}
const testClass = (clazz) => {
testStaticValue(clazz);
testStaticAccessor(clazz);
testStaticMethod(clazz);
testStaticEnumerables(clazz);
};
// `Test` is needed for accessing exposed symbols
testObj(new Test(), Test);
testClass(Test);
}
test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));