-
Notifications
You must be signed in to change notification settings - Fork 471
/
Copy pathfunction.js
137 lines (110 loc) · 4.66 KB
/
function.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
'use strict';
const assert = require('assert');
module.exports = require('./common').runTest(binding => {
test(binding.function.plain);
test(binding.function.templated);
testLambda(binding.function.lambda);
});
function test (binding) {
assert.strictEqual(binding.emptyConstructor(true), true);
assert.strictEqual(binding.emptyConstructor(false), false);
let obj = {};
assert.deepStrictEqual(binding.voidCallback(obj), undefined);
assert.deepStrictEqual(obj, { foo: 'bar' });
assert.deepStrictEqual(binding.valueCallback(), { foo: 'bar' });
/* eslint-disable-next-line new-cap */
assert.strictEqual(new binding.newTargetCallback(), binding.newTargetCallback);
assert.strictEqual(binding.newTargetCallback(), undefined);
let args = null;
let ret = null;
let receiver = null;
function testFunction () {
receiver = this;
args = [].slice.call(arguments);
return ret;
}
function testConstructor () {
args = [].slice.call(arguments);
}
function makeCallbackTestFunction (receiver, expectedOne, expectedTwo, expectedThree) {
return function callback (one, two, three) {
assert.strictEqual(this, receiver);
assert.strictEqual(one, expectedOne);
assert.strictEqual(two, expectedTwo);
assert.strictEqual(three, expectedThree);
};
}
ret = 4;
assert.strictEqual(binding.callWithArgs(testFunction, 1, 2, 3), 4);
assert.strictEqual(receiver, undefined);
assert.deepStrictEqual(args, [1, 2, 3]);
ret = 5;
assert.strictEqual(binding.callWithVector(testFunction, 2, 3, 4), 5);
assert.strictEqual(receiver, undefined);
assert.deepStrictEqual(args, [2, 3, 4]);
ret = 5;
assert.strictEqual(binding.callWithVectorUsingCppWrapper(testFunction, 2, 3, 4), 5);
assert.strictEqual(receiver, undefined);
assert.deepStrictEqual(args, [2, 3, 4]);
ret = 6;
assert.strictEqual(binding.callWithReceiverAndArgs(testFunction, obj, 3, 4, 5), 6);
assert.deepStrictEqual(receiver, obj);
assert.deepStrictEqual(args, [3, 4, 5]);
ret = 7;
assert.strictEqual(binding.callWithReceiverAndVector(testFunction, obj, 4, 5, 6), 7);
assert.deepStrictEqual(receiver, obj);
assert.deepStrictEqual(args, [4, 5, 6]);
ret = 7;
assert.strictEqual(binding.callWithReceiverAndVectorUsingCppWrapper(testFunction, obj, 4, 5, 6), 7);
assert.deepStrictEqual(receiver, obj);
assert.deepStrictEqual(args, [4, 5, 6]);
ret = 8;
assert.strictEqual(binding.callWithCStyleArray(testFunction, 5, 6, 7), ret);
assert.deepStrictEqual(receiver, undefined);
assert.deepStrictEqual(args, [5, 6, 7]);
ret = 9;
assert.strictEqual(binding.callWithReceiverAndCStyleArray(testFunction, obj, 6, 7, 8), ret);
assert.deepStrictEqual(receiver, obj);
assert.deepStrictEqual(args, [6, 7, 8]);
ret = 10;
assert.strictEqual(binding.callWithFunctionOperator(testFunction, 7, 8, 9), ret);
assert.strictEqual(receiver, undefined);
assert.deepStrictEqual(args, [7, 8, 9]);
assert.throws(() => {
binding.callWithInvalidReceiver(() => {});
}, /Invalid (pointer passed as )?argument/);
obj = binding.callConstructorWithArgs(testConstructor, 5, 6, 7);
assert(obj instanceof testConstructor);
assert.deepStrictEqual(args, [5, 6, 7]);
obj = binding.callConstructorWithVector(testConstructor, 6, 7, 8);
assert(obj instanceof testConstructor);
assert.deepStrictEqual(args, [6, 7, 8]);
obj = binding.callConstructorWithCStyleArray(testConstructor, 7, 8, 9);
assert(obj instanceof testConstructor);
assert.deepStrictEqual(args, [7, 8, 9]);
obj = {};
assert.deepStrictEqual(binding.voidCallbackWithData(obj), undefined);
assert.deepStrictEqual(obj, { foo: 'bar', data: 1 });
assert.deepStrictEqual(binding.valueCallbackWithData(), { foo: 'bar', data: 1 });
assert.strictEqual(binding.voidCallback.name, 'voidCallback');
assert.strictEqual(binding.valueCallback.name, 'valueCallback');
let testConstructCall;
binding.isConstructCall((result) => { testConstructCall = result; });
assert.ok(!testConstructCall);
/* eslint-disable no-new, new-cap */
new binding.isConstructCall((result) => { testConstructCall = result; });
/* eslint-enable no-new, new-cap */
assert.ok(testConstructCall);
obj = {};
binding.makeCallbackWithArgs(makeCallbackTestFunction(obj, '1', '2', '3'), obj, '1', '2', '3');
binding.makeCallbackWithVector(makeCallbackTestFunction(obj, 4, 5, 6), obj, 4, 5, 6);
binding.makeCallbackWithCStyleArray(makeCallbackTestFunction(obj, 7, 8, 9), obj, 7, 8, 9);
assert.throws(() => {
binding.makeCallbackWithInvalidReceiver(() => {});
});
}
function testLambda (binding) {
assert.ok(binding.lambdaWithNoCapture());
assert.ok(binding.lambdaWithCapture());
assert.ok(binding.lambdaWithMoveOnlyCapture());
}