-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: add Object::TypeTag, Object::CheckTypeTag
PR-URL: #1261 Reviewed-By: Michael Dawson <midawson@redhat.com
- Loading branch information
Showing
9 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "napi.h" | ||
|
||
#if (NAPI_VERSION > 7) | ||
|
||
using namespace Napi; | ||
|
||
static const napi_type_tag type_tags[5] = { | ||
{0xdaf987b3cc62481a, 0xb745b0497f299531}, | ||
{0xbb7936c374084d9b, 0xa9548d0762eeedb9}, | ||
{0xa5ed9ce2e4c00c38, 0}, | ||
{0, 0}, | ||
{0xa5ed9ce2e4c00c38, 0xdaf987b3cc62481a}, | ||
}; | ||
|
||
Value TypeTaggedInstance(const CallbackInfo& info) { | ||
Object instance = Object::New(info.Env()); | ||
uint32_t type_index = info[0].As<Number>().Int32Value(); | ||
|
||
instance.TypeTag(&type_tags[type_index]); | ||
|
||
return instance; | ||
} | ||
|
||
Value CheckTypeTag(const CallbackInfo& info) { | ||
uint32_t type_index = info[0].As<Number>().Int32Value(); | ||
Object instance = info[1].As<Object>(); | ||
|
||
return Boolean::New(info.Env(), | ||
instance.CheckTypeTag(&type_tags[type_index])); | ||
} | ||
|
||
Object InitObjectTypeTag(Env env) { | ||
Object exports = Object::New(env); | ||
exports["checkTypeTag"] = Function::New(env, CheckTypeTag); | ||
exports["typedTaggedInstance"] = Function::New(env, TypeTaggedInstance); | ||
return exports; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = require('../common').runTest(test); | ||
|
||
// eslint-disable-next-line camelcase | ||
function test ({ object_type_tag }) { | ||
const obj1 = object_type_tag.typedTaggedInstance(0); | ||
const obj2 = object_type_tag.typedTaggedInstance(1); | ||
|
||
// Verify that type tags are correctly accepted. | ||
assert.strictEqual(object_type_tag.checkTypeTag(0, obj1), true); | ||
assert.strictEqual(object_type_tag.checkTypeTag(1, obj2), true); | ||
|
||
// Verify that wrongly tagged objects are rejected. | ||
assert.strictEqual(object_type_tag.checkTypeTag(0, obj2), false); | ||
assert.strictEqual(object_type_tag.checkTypeTag(1, obj1), false); | ||
|
||
// Verify that untagged objects are rejected. | ||
assert.strictEqual(object_type_tag.checkTypeTag(0, {}), false); | ||
assert.strictEqual(object_type_tag.checkTypeTag(1, {}), false); | ||
|
||
// Node v14 and v16 have an issue checking type tags if the `upper` in | ||
// `napi_type_tag` is 0, so these tests can only be performed on Node version | ||
// >=18. See: | ||
// - https://github.com/nodejs/node/issues/43786 | ||
// - https://github.com/nodejs/node/pull/43788 | ||
const nodeVersion = parseInt(process.versions.node.split('.')[0]); | ||
if (nodeVersion < 18) { | ||
return; | ||
} | ||
|
||
const obj3 = object_type_tag.typedTaggedInstance(2); | ||
const obj4 = object_type_tag.typedTaggedInstance(3); | ||
|
||
// Verify that untagged objects are rejected. | ||
assert.strictEqual(object_type_tag.checkTypeTag(0, {}), false); | ||
assert.strictEqual(object_type_tag.checkTypeTag(1, {}), false); | ||
|
||
// Verify that type tags are correctly accepted. | ||
assert.strictEqual(object_type_tag.checkTypeTag(0, obj1), true); | ||
assert.strictEqual(object_type_tag.checkTypeTag(1, obj2), true); | ||
assert.strictEqual(object_type_tag.checkTypeTag(2, obj3), true); | ||
assert.strictEqual(object_type_tag.checkTypeTag(3, obj4), true); | ||
|
||
// Verify that wrongly tagged objects are rejected. | ||
assert.strictEqual(object_type_tag.checkTypeTag(0, obj2), false); | ||
assert.strictEqual(object_type_tag.checkTypeTag(1, obj1), false); | ||
assert.strictEqual(object_type_tag.checkTypeTag(0, obj3), false); | ||
assert.strictEqual(object_type_tag.checkTypeTag(1, obj4), false); | ||
assert.strictEqual(object_type_tag.checkTypeTag(2, obj4), false); | ||
assert.strictEqual(object_type_tag.checkTypeTag(3, obj3), false); | ||
assert.strictEqual(object_type_tag.checkTypeTag(4, obj3), false); | ||
} |