|
1 | 1 | #include <napi.h> |
2 | 2 |
|
3 | 3 | #include <string_view> |
| 4 | +#include <utility> |
4 | 5 |
|
5 | 6 | #include "test_helper.h" |
6 | 7 | using namespace Napi; |
7 | 8 |
|
8 | 9 | namespace { |
9 | 10 |
|
10 | | -class StringLike { |
11 | | - public: |
12 | | - explicit StringLike(const std::string& value) : _value(value) {} |
| 11 | +struct StringLike { |
| 12 | + operator std::string() const { return "unexpected-string-key"; } |
| 13 | + operator std::string_view() const { return value; } |
13 | 14 |
|
14 | | - operator std::string() const { return _value; } |
15 | | - operator std::string_view() const { return _value; } |
| 15 | + std::string value; |
| 16 | +}; |
| 17 | + |
| 18 | +struct RvalueStringLike { |
| 19 | + operator std::string() && { return "unexpected-rvalue-string-key"; } |
| 20 | + operator std::string_view() && { return value; } |
| 21 | + |
| 22 | + std::string value; |
| 23 | +}; |
| 24 | + |
| 25 | +struct StringOnlyLike { |
| 26 | + operator std::string() const { return value; } |
| 27 | + |
| 28 | + std::string value; |
| 29 | +}; |
| 30 | + |
| 31 | +struct BothBases : std::string, std::string_view {}; |
| 32 | + |
| 33 | +struct ViewAndNapiString : std::string_view, Napi::String {}; |
| 34 | + |
| 35 | +struct StringReferenceLike { |
| 36 | + operator std::string&() const { return stringValue; } |
| 37 | + operator std::string&&() const { return std::move(stringValue); } |
| 38 | + operator std::string_view() const { return viewValue; } |
16 | 39 |
|
17 | | - private: |
18 | | - std::string _value; |
| 40 | + mutable std::string stringValue; |
| 41 | + std::string_view viewValue; |
| 42 | +}; |
| 43 | + |
| 44 | +struct ImplicitStringViewLike { |
| 45 | + operator std::string_view() const { return value; } |
| 46 | + |
| 47 | + std::string_view value; |
| 48 | +}; |
| 49 | + |
| 50 | +struct ExplicitStringViewLike { |
| 51 | + explicit operator std::string_view() const { return value; } |
| 52 | + |
| 53 | + std::string_view value; |
| 54 | +}; |
| 55 | + |
| 56 | +struct ImplicitAndExplicitStringViewLike : ImplicitStringViewLike, |
| 57 | + ExplicitStringViewLike { |
| 58 | + operator std::string() const { return "unexpected-string-key"; } |
19 | 59 | }; |
20 | 60 |
|
21 | 61 | } // namespace |
@@ -64,10 +104,59 @@ Symbol FetchSymbolFromGlobalRegistryWithStringViewKey( |
64 | 104 |
|
65 | 105 | Symbol FetchSymbolFromGlobalRegistryWithStringLikeKey( |
66 | 106 | const Napi::CallbackInfo& info) { |
67 | | - StringLike key(info[0].As<String>().Utf8Value()); |
| 107 | + StringLike key{info[0].As<String>().Utf8Value()}; |
68 | 108 | return MaybeUnwrap(Napi::Symbol::For(info.Env(), key)); |
69 | 109 | } |
70 | 110 |
|
| 111 | +Symbol FetchSymbolFromGlobalRegistryWithRvalueStringLikeKey( |
| 112 | + const Napi::CallbackInfo& info) { |
| 113 | + return MaybeUnwrap(Napi::Symbol::For( |
| 114 | + info.Env(), RvalueStringLike{info[0].As<String>().Utf8Value()})); |
| 115 | +} |
| 116 | + |
| 117 | +Symbol FetchSymbolFromGlobalRegistryWithStringOnlyLikeKey( |
| 118 | + const Napi::CallbackInfo& info) { |
| 119 | + StringOnlyLike key{info[0].As<String>().Utf8Value()}; |
| 120 | + return MaybeUnwrap(Napi::Symbol::For(info.Env(), key)); |
| 121 | +} |
| 122 | + |
| 123 | +Symbol FetchSymbolFromGlobalRegistryWithBothBasesKey( |
| 124 | + const Napi::CallbackInfo& info) { |
| 125 | + std::string value = info[0].As<String>().Utf8Value(); |
| 126 | + BothBases key; |
| 127 | + static_cast<std::string&>(key) = "unexpected-string-key"; |
| 128 | + static_cast<std::string_view&>(key) = value; |
| 129 | + return MaybeUnwrap(Symbol::For(info.Env(), key)); |
| 130 | +} |
| 131 | + |
| 132 | +Symbol FetchSymbolFromGlobalRegistryWithViewAndNapiStringKey( |
| 133 | + const Napi::CallbackInfo& info) { |
| 134 | + Env env = info.Env(); |
| 135 | + std::string value = info[0].As<String>().Utf8Value(); |
| 136 | + ViewAndNapiString key; |
| 137 | + static_cast<std::string_view&>(key) = value; |
| 138 | + static_cast<Napi::String&>(key) = |
| 139 | + Napi::String::New(env, "unexpected-napi-string-key"); |
| 140 | + return MaybeUnwrap(Symbol::For(env, key)); |
| 141 | +} |
| 142 | + |
| 143 | +Symbol FetchSymbolFromGlobalRegistryWithStringReferenceKey( |
| 144 | + const Napi::CallbackInfo& info) { |
| 145 | + std::string value = info[0].As<String>().Utf8Value(); |
| 146 | + StringReferenceLike key{"unexpected-string-reference-key", value}; |
| 147 | + return MaybeUnwrap(Symbol::For(info.Env(), key)); |
| 148 | +} |
| 149 | + |
| 150 | +Symbol FetchSymbolFromGlobalRegistryWithImplicitViewKey( |
| 151 | + const Napi::CallbackInfo& info) { |
| 152 | + std::string value = info[0].As<String>().Utf8Value(); |
| 153 | + ImplicitAndExplicitStringViewLike key; |
| 154 | + static_cast<ImplicitStringViewLike&>(key).value = value; |
| 155 | + static_cast<ExplicitStringViewLike&>(key).value = |
| 156 | + "unexpected-explicit-string-view-key"; |
| 157 | + return MaybeUnwrap(Symbol::For(info.Env(), key)); |
| 158 | +} |
| 159 | + |
71 | 160 | Symbol FetchSymbolFromGlobalRegistryWithCKey(const Napi::CallbackInfo& info) { |
72 | 161 | String cppStringKey = info[0].As<String>(); |
73 | 162 | return MaybeUnwrap( |
@@ -106,6 +195,18 @@ Object InitSymbol(Env env) { |
106 | 195 | Function::New(env, FetchSymbolFromGlobalRegistryWithStringViewKey); |
107 | 196 | exports["getSymbolFromGlobalRegistryWithStringLikeKey"] = |
108 | 197 | Function::New(env, FetchSymbolFromGlobalRegistryWithStringLikeKey); |
| 198 | + exports["getSymbolFromGlobalRegistryWithRvalueStringLikeKey"] = |
| 199 | + Function::New(env, FetchSymbolFromGlobalRegistryWithRvalueStringLikeKey); |
| 200 | + exports["getSymbolFromGlobalRegistryWithStringOnlyLikeKey"] = |
| 201 | + Function::New(env, FetchSymbolFromGlobalRegistryWithStringOnlyLikeKey); |
| 202 | + exports["getSymbolFromGlobalRegistryWithBothBasesKey"] = |
| 203 | + Function::New(env, FetchSymbolFromGlobalRegistryWithBothBasesKey); |
| 204 | + exports["getSymbolFromGlobalRegistryWithViewAndNapiStringKey"] = |
| 205 | + Function::New(env, FetchSymbolFromGlobalRegistryWithViewAndNapiStringKey); |
| 206 | + exports["getSymbolFromGlobalRegistryWithStringReferenceKey"] = |
| 207 | + Function::New(env, FetchSymbolFromGlobalRegistryWithStringReferenceKey); |
| 208 | + exports["getSymbolFromGlobalRegistryWithImplicitViewKey"] = |
| 209 | + Function::New(env, FetchSymbolFromGlobalRegistryWithImplicitViewKey); |
109 | 210 | exports["testUndefinedSymbolCanBeCreated"] = |
110 | 211 | Function::New(env, TestUndefinedSymbolsCanBeCreated); |
111 | 212 | exports["testNullSymbolCanBeCreated"] = |
|
0 commit comments