11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-gc
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../../common');
51cb0ef41Sopenharmony_ciconst test_general = require(`./build/${common.buildType}/test_general`);
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst val1 = '1';
91cb0ef41Sopenharmony_ciconst val2 = 1;
101cb0ef41Sopenharmony_ciconst val3 = 1;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciclass BaseClass {
131cb0ef41Sopenharmony_ci}
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciclass ExtendedClass extends BaseClass {
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst baseObject = new BaseClass();
191cb0ef41Sopenharmony_ciconst extendedObject = new ExtendedClass();
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// Test napi_strict_equals
221cb0ef41Sopenharmony_ciassert.ok(test_general.testStrictEquals(val1, val1));
231cb0ef41Sopenharmony_ciassert.strictEqual(test_general.testStrictEquals(val1, val2), false);
241cb0ef41Sopenharmony_ciassert.ok(test_general.testStrictEquals(val2, val3));
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// Test napi_get_prototype
271cb0ef41Sopenharmony_ciassert.strictEqual(test_general.testGetPrototype(baseObject),
281cb0ef41Sopenharmony_ci                   Object.getPrototypeOf(baseObject));
291cb0ef41Sopenharmony_ciassert.strictEqual(test_general.testGetPrototype(extendedObject),
301cb0ef41Sopenharmony_ci                   Object.getPrototypeOf(extendedObject));
311cb0ef41Sopenharmony_ci// Prototypes for base and extended should be different.
321cb0ef41Sopenharmony_ciassert.notStrictEqual(test_general.testGetPrototype(baseObject),
331cb0ef41Sopenharmony_ci                      test_general.testGetPrototype(extendedObject));
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci// Test version management functions
361cb0ef41Sopenharmony_ciassert.strictEqual(test_general.testGetVersion(), 9);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci[
391cb0ef41Sopenharmony_ci  123,
401cb0ef41Sopenharmony_ci  'test string',
411cb0ef41Sopenharmony_ci  function() {},
421cb0ef41Sopenharmony_ci  new Object(),
431cb0ef41Sopenharmony_ci  true,
441cb0ef41Sopenharmony_ci  undefined,
451cb0ef41Sopenharmony_ci  Symbol(),
461cb0ef41Sopenharmony_ci].forEach((val) => {
471cb0ef41Sopenharmony_ci  assert.strictEqual(test_general.testNapiTypeof(val), typeof val);
481cb0ef41Sopenharmony_ci});
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci// Since typeof in js return object need to validate specific case
511cb0ef41Sopenharmony_ci// for null
521cb0ef41Sopenharmony_ciassert.strictEqual(test_general.testNapiTypeof(null), 'null');
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci// Assert that wrapping twice fails.
551cb0ef41Sopenharmony_ciconst x = {};
561cb0ef41Sopenharmony_citest_general.wrap(x);
571cb0ef41Sopenharmony_ciassert.throws(() => test_general.wrap(x),
581cb0ef41Sopenharmony_ci              { name: 'Error', message: 'Invalid argument' });
591cb0ef41Sopenharmony_ci// Clean up here, otherwise derefItemWasCalled() will be polluted.
601cb0ef41Sopenharmony_citest_general.removeWrap(x);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci// Ensure that wrapping, removing the wrap, and then wrapping again works.
631cb0ef41Sopenharmony_ciconst y = {};
641cb0ef41Sopenharmony_citest_general.wrap(y);
651cb0ef41Sopenharmony_citest_general.removeWrap(y);
661cb0ef41Sopenharmony_ci// Wrapping twice succeeds if a remove_wrap() separates the instances
671cb0ef41Sopenharmony_citest_general.wrap(y);
681cb0ef41Sopenharmony_ci// Clean up here, otherwise derefItemWasCalled() will be polluted.
691cb0ef41Sopenharmony_citest_general.removeWrap(y);
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci// Test napi_adjust_external_memory
721cb0ef41Sopenharmony_ciconst adjustedValue = test_general.testAdjustExternalMemory();
731cb0ef41Sopenharmony_ciassert.strictEqual(typeof adjustedValue, 'number');
741cb0ef41Sopenharmony_ciassert(adjustedValue > 0);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ciasync function runGCTests() {
771cb0ef41Sopenharmony_ci  // Ensure that garbage collecting an object with a wrapped native item results
781cb0ef41Sopenharmony_ci  // in the finalize callback being called.
791cb0ef41Sopenharmony_ci  assert.strictEqual(test_general.derefItemWasCalled(), false);
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  (() => test_general.wrap({}))();
821cb0ef41Sopenharmony_ci  await common.gcUntil('deref_item() was called upon garbage collecting a ' +
831cb0ef41Sopenharmony_ci                       'wrapped object.',
841cb0ef41Sopenharmony_ci                       () => test_general.derefItemWasCalled());
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  // Ensure that removing a wrap and garbage collecting does not fire the
871cb0ef41Sopenharmony_ci  // finalize callback.
881cb0ef41Sopenharmony_ci  let z = {};
891cb0ef41Sopenharmony_ci  test_general.testFinalizeWrap(z);
901cb0ef41Sopenharmony_ci  test_general.removeWrap(z);
911cb0ef41Sopenharmony_ci  z = null;
921cb0ef41Sopenharmony_ci  await common.gcUntil(
931cb0ef41Sopenharmony_ci    'finalize callback was not called upon garbage collection.',
941cb0ef41Sopenharmony_ci    () => (!test_general.finalizeWasCalled()));
951cb0ef41Sopenharmony_ci}
961cb0ef41Sopenharmony_cirunGCTests();
97