1'use strict';
2// Flags: --expose-gc
3
4const common = require('../../common');
5const test_general = require(`./build/${common.buildType}/test_general`);
6const assert = require('assert');
7
8const val1 = '1';
9const val2 = 1;
10const val3 = 1;
11
12class BaseClass {
13}
14
15class ExtendedClass extends BaseClass {
16}
17
18const baseObject = new BaseClass();
19const extendedObject = new ExtendedClass();
20
21// Test napi_strict_equals
22assert.ok(test_general.testStrictEquals(val1, val1));
23assert.strictEqual(test_general.testStrictEquals(val1, val2), false);
24assert.ok(test_general.testStrictEquals(val2, val3));
25
26// Test napi_get_prototype
27assert.strictEqual(test_general.testGetPrototype(baseObject),
28                   Object.getPrototypeOf(baseObject));
29assert.strictEqual(test_general.testGetPrototype(extendedObject),
30                   Object.getPrototypeOf(extendedObject));
31// Prototypes for base and extended should be different.
32assert.notStrictEqual(test_general.testGetPrototype(baseObject),
33                      test_general.testGetPrototype(extendedObject));
34
35// Test version management functions
36assert.strictEqual(test_general.testGetVersion(), 9);
37
38[
39  123,
40  'test string',
41  function() {},
42  new Object(),
43  true,
44  undefined,
45  Symbol(),
46].forEach((val) => {
47  assert.strictEqual(test_general.testNapiTypeof(val), typeof val);
48});
49
50// Since typeof in js return object need to validate specific case
51// for null
52assert.strictEqual(test_general.testNapiTypeof(null), 'null');
53
54// Assert that wrapping twice fails.
55const x = {};
56test_general.wrap(x);
57assert.throws(() => test_general.wrap(x),
58              { name: 'Error', message: 'Invalid argument' });
59// Clean up here, otherwise derefItemWasCalled() will be polluted.
60test_general.removeWrap(x);
61
62// Ensure that wrapping, removing the wrap, and then wrapping again works.
63const y = {};
64test_general.wrap(y);
65test_general.removeWrap(y);
66// Wrapping twice succeeds if a remove_wrap() separates the instances
67test_general.wrap(y);
68// Clean up here, otherwise derefItemWasCalled() will be polluted.
69test_general.removeWrap(y);
70
71// Test napi_adjust_external_memory
72const adjustedValue = test_general.testAdjustExternalMemory();
73assert.strictEqual(typeof adjustedValue, 'number');
74assert(adjustedValue > 0);
75
76async function runGCTests() {
77  // Ensure that garbage collecting an object with a wrapped native item results
78  // in the finalize callback being called.
79  assert.strictEqual(test_general.derefItemWasCalled(), false);
80
81  (() => test_general.wrap({}))();
82  await common.gcUntil('deref_item() was called upon garbage collecting a ' +
83                       'wrapped object.',
84                       () => test_general.derefItemWasCalled());
85
86  // Ensure that removing a wrap and garbage collecting does not fire the
87  // finalize callback.
88  let z = {};
89  test_general.testFinalizeWrap(z);
90  test_general.removeWrap(z);
91  z = null;
92  await common.gcUntil(
93    'finalize callback was not called upon garbage collection.',
94    () => (!test_general.finalizeWasCalled()));
95}
96runGCTests();
97