|
| 1 | +#include <napi.h> |
| 2 | +using namespace Napi; |
| 3 | + |
| 4 | +Symbol CreateNewSymbolWithNoArgs(const Napi::CallbackInfo&) { |
| 5 | + return Napi::Symbol(); |
| 6 | +} |
| 7 | + |
| 8 | +Symbol CreateNewSymbolWithCppStrDesc(const Napi::CallbackInfo& info) { |
| 9 | + String cppStrKey = info[0].As<String>(); |
| 10 | + return Napi::Symbol::New(info.Env(), cppStrKey.Utf8Value()); |
| 11 | +} |
| 12 | + |
| 13 | +Symbol CreateNewSymbolWithCStrDesc(const Napi::CallbackInfo& info) { |
| 14 | + String cStrKey = info[0].As<String>(); |
| 15 | + return Napi::Symbol::New(info.Env(), cStrKey.Utf8Value().c_str()); |
| 16 | +} |
| 17 | + |
| 18 | +Symbol CreateNewSymbolWithNapiString(const Napi::CallbackInfo& info) { |
| 19 | + String strKey = info[0].As<String>(); |
| 20 | + return Napi::Symbol::New(info.Env(), strKey); |
| 21 | +} |
| 22 | + |
| 23 | +Symbol GetWellknownSymbol(const Napi::CallbackInfo& info) { |
| 24 | + String registrySymbol = info[0].As<String>(); |
| 25 | + return Napi::Symbol::WellKnown(info.Env(), |
| 26 | + registrySymbol.Utf8Value().c_str()); |
| 27 | +} |
| 28 | + |
| 29 | +Symbol FetchSymbolFromGlobalRegistry(const Napi::CallbackInfo& info) { |
| 30 | + String registrySymbol = info[0].As<String>(); |
| 31 | + return Napi::Symbol::For(info.Env(), registrySymbol); |
| 32 | +} |
| 33 | + |
| 34 | +Symbol FetchSymbolFromGlobalRegistryWithCppKey(const Napi::CallbackInfo& info) { |
| 35 | + String cppStringKey = info[0].As<String>(); |
| 36 | + return Napi::Symbol::For(info.Env(), cppStringKey.Utf8Value()); |
| 37 | +} |
| 38 | + |
| 39 | +Symbol FetchSymbolFromGlobalRegistryWithCKey(const Napi::CallbackInfo& info) { |
| 40 | + String cppStringKey = info[0].As<String>(); |
| 41 | + return Napi::Symbol::For(info.Env(), cppStringKey.Utf8Value().c_str()); |
| 42 | +} |
| 43 | + |
| 44 | +Symbol TestUndefinedSymbolsCanBeCreated(const Napi::CallbackInfo& info) { |
| 45 | + Napi::Env env = info.Env(); |
| 46 | + return Napi::Symbol::For(env, env.Undefined()); |
| 47 | +} |
| 48 | + |
| 49 | +Symbol TestNullSymbolsCanBeCreated(const Napi::CallbackInfo& info) { |
| 50 | + Napi::Env env = info.Env(); |
| 51 | + return Napi::Symbol::For(env, env.Null()); |
| 52 | +} |
| 53 | + |
| 54 | +Object InitSymbol(Env env) { |
| 55 | + Object exports = Object::New(env); |
| 56 | + |
| 57 | + exports["createNewSymbolWithNoArgs"] = |
| 58 | + Function::New(env, CreateNewSymbolWithNoArgs); |
| 59 | + exports["createNewSymbolWithCppStr"] = |
| 60 | + Function::New(env, CreateNewSymbolWithCppStrDesc); |
| 61 | + exports["createNewSymbolWithCStr"] = |
| 62 | + Function::New(env, CreateNewSymbolWithCStrDesc); |
| 63 | + exports["createNewSymbolWithNapi"] = |
| 64 | + Function::New(env, CreateNewSymbolWithNapiString); |
| 65 | + exports["getWellKnownSymbol"] = Function::New(env, GetWellknownSymbol); |
| 66 | + exports["getSymbolFromGlobalRegistry"] = |
| 67 | + Function::New(env, FetchSymbolFromGlobalRegistry); |
| 68 | + exports["getSymbolFromGlobalRegistryWithCKey"] = |
| 69 | + Function::New(env, FetchSymbolFromGlobalRegistryWithCKey); |
| 70 | + exports["getSymbolFromGlobalRegistryWithCppKey"] = |
| 71 | + Function::New(env, FetchSymbolFromGlobalRegistryWithCppKey); |
| 72 | + exports["testUndefinedSymbolCanBeCreated"] = |
| 73 | + Function::New(env, TestUndefinedSymbolsCanBeCreated); |
| 74 | + exports["testNullSymbolCanBeCreated"] = |
| 75 | + Function::New(env, TestNullSymbolsCanBeCreated); |
| 76 | + return exports; |
| 77 | +} |
0 commit comments