|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | + |
| 5 | +module.exports = require('../common').runTest(test); |
| 6 | + |
| 7 | +// eslint-disable-next-line camelcase |
| 8 | +function test ({ object_type_tag }) { |
| 9 | + const obj1 = object_type_tag.typedTaggedInstance(0); |
| 10 | + const obj2 = object_type_tag.typedTaggedInstance(1); |
| 11 | + |
| 12 | + // Verify that type tags are correctly accepted. |
| 13 | + assert.strictEqual(object_type_tag.checkTypeTag(0, obj1), true); |
| 14 | + assert.strictEqual(object_type_tag.checkTypeTag(1, obj2), true); |
| 15 | + |
| 16 | + // Verify that wrongly tagged objects are rejected. |
| 17 | + assert.strictEqual(object_type_tag.checkTypeTag(0, obj2), false); |
| 18 | + assert.strictEqual(object_type_tag.checkTypeTag(1, obj1), false); |
| 19 | + |
| 20 | + // Verify that untagged objects are rejected. |
| 21 | + assert.strictEqual(object_type_tag.checkTypeTag(0, {}), false); |
| 22 | + assert.strictEqual(object_type_tag.checkTypeTag(1, {}), false); |
| 23 | + |
| 24 | + // Node v14 and v16 have an issue checking type tags if the `upper` in |
| 25 | + // `napi_type_tag` is 0, so these tests can only be performed on Node version |
| 26 | + // >=18. See: |
| 27 | + // - https://github.com/nodejs/node/issues/43786 |
| 28 | + // - https://github.com/nodejs/node/pull/43788 |
| 29 | + const nodeVersion = parseInt(process.versions.node.split('.')[0]); |
| 30 | + if (nodeVersion < 18) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const obj3 = object_type_tag.typedTaggedInstance(2); |
| 35 | + const obj4 = object_type_tag.typedTaggedInstance(3); |
| 36 | + |
| 37 | + // Verify that untagged objects are rejected. |
| 38 | + assert.strictEqual(object_type_tag.checkTypeTag(0, {}), false); |
| 39 | + assert.strictEqual(object_type_tag.checkTypeTag(1, {}), false); |
| 40 | + |
| 41 | + // Verify that type tags are correctly accepted. |
| 42 | + assert.strictEqual(object_type_tag.checkTypeTag(0, obj1), true); |
| 43 | + assert.strictEqual(object_type_tag.checkTypeTag(1, obj2), true); |
| 44 | + assert.strictEqual(object_type_tag.checkTypeTag(2, obj3), true); |
| 45 | + assert.strictEqual(object_type_tag.checkTypeTag(3, obj4), true); |
| 46 | + |
| 47 | + // Verify that wrongly tagged objects are rejected. |
| 48 | + assert.strictEqual(object_type_tag.checkTypeTag(0, obj2), false); |
| 49 | + assert.strictEqual(object_type_tag.checkTypeTag(1, obj1), false); |
| 50 | + assert.strictEqual(object_type_tag.checkTypeTag(0, obj3), false); |
| 51 | + assert.strictEqual(object_type_tag.checkTypeTag(1, obj4), false); |
| 52 | + assert.strictEqual(object_type_tag.checkTypeTag(2, obj4), false); |
| 53 | + assert.strictEqual(object_type_tag.checkTypeTag(3, obj3), false); |
| 54 | + assert.strictEqual(object_type_tag.checkTypeTag(4, obj3), false); |
| 55 | +} |
0 commit comments