-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #972 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
- Loading branch information
Showing
7 changed files
with
198 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <napi.h> | ||
using namespace Napi; | ||
|
||
Symbol CreateNewSymbolWithNoArgs(const Napi::CallbackInfo&) { | ||
return Napi::Symbol(); | ||
} | ||
|
||
Symbol CreateNewSymbolWithCppStrDesc(const Napi::CallbackInfo& info) { | ||
String cppStrKey = info[0].As<String>(); | ||
return Napi::Symbol::New(info.Env(), cppStrKey.Utf8Value()); | ||
} | ||
|
||
Symbol CreateNewSymbolWithCStrDesc(const Napi::CallbackInfo& info) { | ||
String cStrKey = info[0].As<String>(); | ||
return Napi::Symbol::New(info.Env(), cStrKey.Utf8Value().c_str()); | ||
} | ||
|
||
Symbol CreateNewSymbolWithNapiString(const Napi::CallbackInfo& info) { | ||
String strKey = info[0].As<String>(); | ||
return Napi::Symbol::New(info.Env(), strKey); | ||
} | ||
|
||
Symbol GetWellknownSymbol(const Napi::CallbackInfo& info) { | ||
String registrySymbol = info[0].As<String>(); | ||
return Napi::Symbol::WellKnown(info.Env(), | ||
registrySymbol.Utf8Value().c_str()); | ||
} | ||
|
||
Symbol FetchSymbolFromGlobalRegistry(const Napi::CallbackInfo& info) { | ||
String registrySymbol = info[0].As<String>(); | ||
return Napi::Symbol::For(info.Env(), registrySymbol); | ||
} | ||
|
||
Symbol FetchSymbolFromGlobalRegistryWithCppKey(const Napi::CallbackInfo& info) { | ||
String cppStringKey = info[0].As<String>(); | ||
return Napi::Symbol::For(info.Env(), cppStringKey.Utf8Value()); | ||
} | ||
|
||
Symbol FetchSymbolFromGlobalRegistryWithCKey(const Napi::CallbackInfo& info) { | ||
String cppStringKey = info[0].As<String>(); | ||
return Napi::Symbol::For(info.Env(), cppStringKey.Utf8Value().c_str()); | ||
} | ||
|
||
Symbol TestUndefinedSymbolsCanBeCreated(const Napi::CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
return Napi::Symbol::For(env, env.Undefined()); | ||
} | ||
|
||
Symbol TestNullSymbolsCanBeCreated(const Napi::CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
return Napi::Symbol::For(env, env.Null()); | ||
} | ||
|
||
Object InitSymbol(Env env) { | ||
Object exports = Object::New(env); | ||
|
||
exports["createNewSymbolWithNoArgs"] = | ||
Function::New(env, CreateNewSymbolWithNoArgs); | ||
exports["createNewSymbolWithCppStr"] = | ||
Function::New(env, CreateNewSymbolWithCppStrDesc); | ||
exports["createNewSymbolWithCStr"] = | ||
Function::New(env, CreateNewSymbolWithCStrDesc); | ||
exports["createNewSymbolWithNapi"] = | ||
Function::New(env, CreateNewSymbolWithNapiString); | ||
exports["getWellKnownSymbol"] = Function::New(env, GetWellknownSymbol); | ||
exports["getSymbolFromGlobalRegistry"] = | ||
Function::New(env, FetchSymbolFromGlobalRegistry); | ||
exports["getSymbolFromGlobalRegistryWithCKey"] = | ||
Function::New(env, FetchSymbolFromGlobalRegistryWithCKey); | ||
exports["getSymbolFromGlobalRegistryWithCppKey"] = | ||
Function::New(env, FetchSymbolFromGlobalRegistryWithCppKey); | ||
exports["testUndefinedSymbolCanBeCreated"] = | ||
Function::New(env, TestUndefinedSymbolsCanBeCreated); | ||
exports["testNullSymbolCanBeCreated"] = | ||
Function::New(env, TestNullSymbolsCanBeCreated); | ||
return exports; | ||
} |
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,70 @@ | ||
'use strict'; | ||
|
||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
|
||
async function test(binding) | ||
{ | ||
|
||
const wellKnownSymbolFunctions = ['asyncIterator','hasInstance','isConcatSpreadable', 'iterator','match','matchAll','replace','search','split','species','toPrimitive','toStringTag','unscopables']; | ||
|
||
function assertCanCreateSymbol(symbol) | ||
{ | ||
assert(binding.symbol.createNewSymbolWithCppStr(symbol) !== null); | ||
assert(binding.symbol.createNewSymbolWithCStr(symbol) !== null); | ||
assert(binding.symbol.createNewSymbolWithNapi(symbol) !== null); | ||
} | ||
|
||
function assertSymbolAreUnique(symbol) | ||
{ | ||
const symbolOne = binding.symbol.createNewSymbolWithCppStr(symbol); | ||
const symbolTwo = binding.symbol.createNewSymbolWithCppStr(symbol); | ||
|
||
assert(symbolOne !== symbolTwo); | ||
} | ||
|
||
function assertSymbolIsWellknown(symbol) | ||
{ | ||
const symbOne = binding.symbol.getWellKnownSymbol(symbol); | ||
const symbTwo = binding.symbol.getWellKnownSymbol(symbol); | ||
assert(symbOne && symbTwo); | ||
assert(symbOne === symbTwo); | ||
} | ||
|
||
function assertSymbolIsNotWellknown(symbol) | ||
{ | ||
const symbolTest = binding.symbol.getWellKnownSymbol(symbol); | ||
assert(symbolTest === undefined); | ||
} | ||
|
||
function assertCanCreateOrFetchGlobalSymbols(symbol, fetchFunction) | ||
{ | ||
const symbOne = fetchFunction(symbol); | ||
const symbTwo = fetchFunction(symbol); | ||
assert(symbOne && symbTwo); | ||
assert(symbOne === symbTwo); | ||
} | ||
|
||
assertCanCreateSymbol("testing"); | ||
assertSymbolAreUnique("symbol"); | ||
assertSymbolIsNotWellknown("testing"); | ||
|
||
for(const wellknownProperty of wellKnownSymbolFunctions) | ||
{ | ||
assertSymbolIsWellknown(wellknownProperty); | ||
} | ||
|
||
assertCanCreateOrFetchGlobalSymbols("data", binding.symbol.getSymbolFromGlobalRegistry); | ||
assertCanCreateOrFetchGlobalSymbols("CppKey", binding.symbol.getSymbolFromGlobalRegistryWithCppKey); | ||
assertCanCreateOrFetchGlobalSymbols("CKey", binding.symbol.getSymbolFromGlobalRegistryWithCKey); | ||
|
||
assert(binding.symbol.createNewSymbolWithNoArgs() === undefined); | ||
|
||
assert(binding.symbol.testNullSymbolCanBeCreated() === binding.symbol.testNullSymbolCanBeCreated()); | ||
assert(binding.symbol.testUndefinedSymbolCanBeCreated() === binding.symbol.testUndefinedSymbolCanBeCreated()); | ||
assert(binding.symbol.testUndefinedSymbolCanBeCreated() !== binding.symbol.testNullSymbolCanBeCreated()); | ||
} |