• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4require('../common');
5
6const assert = require('assert');
7const {
8  ArrayOfApply,
9  ArrayPrototypePushApply,
10  ArrayPrototypeUnshiftApply,
11  MathMaxApply,
12  MathMinApply,
13  StringPrototypeConcatApply,
14  TypedArrayOfApply,
15} = require('internal/test/binding').primordials;
16
17{
18  const arr1 = [1, 2, 3];
19  const arr2 = ArrayOfApply(arr1);
20
21  assert.deepStrictEqual(arr2, arr1);
22  assert.notStrictEqual(arr2, arr1);
23}
24
25{
26  const array = [1, 2, 3];
27  const i32Array = TypedArrayOfApply(Int32Array, array);
28
29  assert(i32Array instanceof Int32Array);
30  assert.strictEqual(i32Array.length, array.length);
31  for (let i = 0, { length } = array; i < length; i++) {
32    assert.strictEqual(i32Array[i], array[i], `i32Array[${i}] === array[${i}]`);
33  }
34}
35
36{
37  const arr1 = [1, 2, 3];
38  const arr2 = [4, 5, 6];
39
40  const expected = [...arr1, ...arr2];
41
42  assert.strictEqual(ArrayPrototypePushApply(arr1, arr2), expected.length);
43  assert.deepStrictEqual(arr1, expected);
44}
45
46{
47  const arr1 = [1, 2, 3];
48  const arr2 = [4, 5, 6];
49
50  const expected = [...arr2, ...arr1];
51
52  assert.strictEqual(ArrayPrototypeUnshiftApply(arr1, arr2), expected.length);
53  assert.deepStrictEqual(arr1, expected);
54}
55
56{
57  const array = [1, 2, 3];
58  assert.strictEqual(MathMaxApply(array), 3);
59  assert.strictEqual(MathMinApply(array), 1);
60}
61
62{
63  let hint;
64  const obj = { [Symbol.toPrimitive](h) {
65    hint = h;
66    return '[object Object]';
67  } };
68
69  const args = ['foo ', obj, ' bar'];
70  const result = StringPrototypeConcatApply('', args);
71
72  assert.strictEqual(hint, 'string');
73  assert.strictEqual(result, 'foo [object Object] bar');
74}
75