| 14 | const addon_new = require(`./build/${buildType}/test_reference_all_types`); |
| 15 | |
| 16 | async function runTests(addon, isVersion8, isLocalSymbol) { |
| 17 | let allEntries = []; |
| 18 | |
| 19 | (() => { |
| 20 | // Create values of all napi_valuetype types. |
| 21 | const undefinedValue = undefined; |
| 22 | const nullValue = null; |
| 23 | const booleanValue = false; |
| 24 | const numberValue = 42; |
| 25 | const stringValue = 'test_string'; |
| 26 | const globalSymbolValue = Symbol.for('test_symbol_global'); |
| 27 | const localSymbolValue = Symbol('test_symbol_local'); |
| 28 | const symbolValue = isLocalSymbol ? localSymbolValue : globalSymbolValue; |
| 29 | const objectValue = { x: 1, y: 2 }; |
| 30 | const functionValue = (x, y) => x + y; |
| 31 | const externalValue = addon.createExternal(); |
| 32 | const bigintValue = 9007199254740991n; |
| 33 | |
| 34 | // The position of entries in the allEntries array corresponds to the |
| 35 | // napi_valuetype enum value. See the CreateRef function for the |
| 36 | // implementation details. |
| 37 | allEntries = [ |
| 38 | { value: undefinedValue, canBeWeak: false, canBeRefV8: false }, |
| 39 | { value: nullValue, canBeWeak: false, canBeRefV8: false }, |
| 40 | { value: booleanValue, canBeWeak: false, canBeRefV8: false }, |
| 41 | { value: numberValue, canBeWeak: false, canBeRefV8: false }, |
| 42 | { value: stringValue, canBeWeak: false, canBeRefV8: false }, |
| 43 | { value: symbolValue, canBeWeak: isLocalSymbol, canBeRefV8: true, |
| 44 | isAlwaysStrong: !isLocalSymbol }, |
| 45 | { value: objectValue, canBeWeak: true, canBeRefV8: true }, |
| 46 | { value: functionValue, canBeWeak: true, canBeRefV8: true }, |
| 47 | { value: externalValue, canBeWeak: true, canBeRefV8: true }, |
| 48 | { value: bigintValue, canBeWeak: false, canBeRefV8: false }, |
| 49 | ]; |
| 50 | |
| 51 | // Go over all values of different types, create strong ref values for |
| 52 | // them, read the stored values, and check how the ref count works. |
| 53 | for (const entry of allEntries) { |
| 54 | if (!isVersion8 || entry.canBeRefV8) { |
| 55 | const index = addon.createRef(entry.value); |
| 56 | const refValue = addon.getRefValue(index); |
| 57 | assert.strictEqual(entry.value, refValue); |
| 58 | assert.strictEqual(addon.ref(index), 2); |
| 59 | assert.strictEqual(addon.unref(index), 1); |
| 60 | assert.strictEqual(addon.unref(index), 0); |
| 61 | } else { |
| 62 | assert.throws(() => { addon.createRef(entry.value); }, |
| 63 | { |
| 64 | name: 'Error', |
| 65 | message: 'Invalid argument', |
| 66 | }); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // When the reference count is zero, then object types become weak pointers |
| 71 | // and other types are released. |
| 72 | // Here we know that the GC is not run yet because the values are |
| 73 | // still in the allEntries array. |