1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
#include "napi.h"
#include "test_helper.h"
using namespace Napi;
// Native wrappers for testing Object::Get()
Value GetPropertyWithUint32(const CallbackInfo& info);
Value GetPropertyWithNapiValue(const CallbackInfo& info);
Value GetPropertyWithNapiWrapperValue(const CallbackInfo& info);
Value GetPropertyWithCStyleString(const CallbackInfo& info);
Value GetPropertyWithCppStyleString(const CallbackInfo& info);
// Native wrappers for testing Object::Set()
Value SetPropertyWithUint32(const CallbackInfo& info);
Value SetPropertyWithNapiValue(const CallbackInfo& info);
Value SetPropertyWithNapiWrapperValue(const CallbackInfo& info);
Value SetPropertyWithCStyleString(const CallbackInfo& info);
Value SetPropertyWithCppStyleString(const CallbackInfo& info);
// Native wrappers for testing Object::Delete()
Value DeletePropertyWithUint32(const CallbackInfo& info);
Value DeletePropertyWithNapiValue(const CallbackInfo& info);
Value DeletePropertyWithNapiWrapperValue(const CallbackInfo& info);
Value DeletePropertyWithCStyleString(const CallbackInfo& info);
Value DeletePropertyWithCppStyleString(const CallbackInfo& info);
// Native wrappers for testing Object::HasOwnProperty()
Value HasOwnPropertyWithNapiValue(const CallbackInfo& info);
Value HasOwnPropertyWithNapiWrapperValue(const CallbackInfo& info);
Value HasOwnPropertyWithCStyleString(const CallbackInfo& info);
Value HasOwnPropertyWithCppStyleString(const CallbackInfo& info);
// Native wrappers for testing Object::Has()
Value HasPropertyWithUint32(const CallbackInfo& info);
Value HasPropertyWithNapiValue(const CallbackInfo& info);
Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info);
Value HasPropertyWithCStyleString(const CallbackInfo& info);
Value HasPropertyWithCppStyleString(const CallbackInfo& info);
// Native wrappers for testing Object::AddFinalizer()
Value AddFinalizer(const CallbackInfo& info);
Value AddFinalizerWithHint(const CallbackInfo& info);
// Native wrappers for testing Object::operator []
Value SubscriptGetWithCStyleString(const CallbackInfo& info);
Value SubscriptGetWithCppStyleString(const CallbackInfo& info);
Value SubscriptGetAtIndex(const CallbackInfo& info);
void SubscriptSetWithCStyleString(const CallbackInfo& info);
void SubscriptSetWithCppStyleString(const CallbackInfo& info);
void SubscriptSetAtIndex(const CallbackInfo& info);
static bool testValue = true;
// Used to test void* Data() integrity
struct UserDataHolder {
int32_t value;
};
Value TestGetter(const CallbackInfo& info) {
return Boolean::New(info.Env(), testValue);
}
void TestSetter(const CallbackInfo& info) {
testValue = info[0].As<Boolean>();
}
Value TestGetterWithUserData(const CallbackInfo& info) {
const UserDataHolder* holder = reinterpret_cast<UserDataHolder*>(info.Data());
return Number::New(info.Env(), holder->value);
}
void TestSetterWithUserData(const CallbackInfo& info) {
UserDataHolder* holder = reinterpret_cast<UserDataHolder*>(info.Data());
holder->value = info[0].As<Number>().Int32Value();
}
Value TestFunction(const CallbackInfo& info) {
return Boolean::New(info.Env(), true);
}
Value TestFunctionWithUserData(const CallbackInfo& info) {
UserDataHolder* holder = reinterpret_cast<UserDataHolder*>(info.Data());
return Number::New(info.Env(), holder->value);
}
Value EmptyConstructor(const CallbackInfo& info) {
auto env = info.Env();
bool isEmpty = info[0].As<Boolean>();
Object object = isEmpty ? Object() : Object(env, Object::New(env));
return Boolean::New(env, object.IsEmpty());
}
Value ConstructorFromObject(const CallbackInfo& info) {
auto env = info.Env();
Object object = info[0].As<Object>();
return Object(env, object);
}
Array GetPropertyNames(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Array arr = MaybeUnwrap(obj.GetPropertyNames());
return arr;
}
void DefineProperties(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String nameType = info[1].As<String>();
Env env = info.Env();
Boolean trueValue = Boolean::New(env, true);
UserDataHolder* holder = new UserDataHolder();
holder->value = 1234;
if (nameType.Utf8Value() == "literal") {
obj.DefineProperties({
PropertyDescriptor::Accessor(env, obj, "readonlyAccessor", TestGetter),
PropertyDescriptor::Accessor(
env, obj, "readwriteAccessor", TestGetter, TestSetter),
PropertyDescriptor::Accessor(env,
obj,
"readonlyAccessorWithUserData",
TestGetterWithUserData,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Accessor(env,
obj,
"readwriteAccessorWithUserData",
TestGetterWithUserData,
TestSetterWithUserData,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Accessor<TestGetter>("readonlyAccessorT"),
PropertyDescriptor::Accessor<TestGetter, TestSetter>(
"readwriteAccessorT"),
PropertyDescriptor::Accessor<TestGetterWithUserData>(
"readonlyAccessorWithUserDataT",
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Accessor<TestGetterWithUserData,
TestSetterWithUserData>(
"readwriteAccessorWithUserDataT",
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Value("readonlyValue", trueValue),
PropertyDescriptor::Value("readwriteValue", trueValue, napi_writable),
PropertyDescriptor::Value(
"enumerableValue", trueValue, napi_enumerable),
PropertyDescriptor::Value(
"configurableValue", trueValue, napi_configurable),
PropertyDescriptor::Function(env, obj, "function", TestFunction),
PropertyDescriptor::Function(env,
obj,
"functionWithUserData",
TestFunctionWithUserData,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
});
} else if (nameType.Utf8Value() == "string") {
// VS2013 has lifetime issues when passing temporary objects into the
// constructor of another object. It generates code to destruct the object
// as soon as the constructor call returns. Since this isn't a common case
// for using std::string objects, I'm refactoring the test to work around
// the issue.
std::string str1("readonlyAccessor");
std::string str2("readwriteAccessor");
std::string str1a("readonlyAccessorWithUserData");
std::string str2a("readwriteAccessorWithUserData");
std::string str1t("readonlyAccessorT");
std::string str2t("readwriteAccessorT");
std::string str1at("readonlyAccessorWithUserDataT");
std::string str2at("readwriteAccessorWithUserDataT");
std::string str3("readonlyValue");
std::string str4("readwriteValue");
std::string str5("enumerableValue");
std::string str6("configurableValue");
std::string str7("function");
std::string str8("functionWithUserData");
obj.DefineProperties({
PropertyDescriptor::Accessor(env, obj, str1, TestGetter),
PropertyDescriptor::Accessor(env, obj, str2, TestGetter, TestSetter),
PropertyDescriptor::Accessor(env,
obj,
str1a,
TestGetterWithUserData,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Accessor(env,
obj,
str2a,
TestGetterWithUserData,
TestSetterWithUserData,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Accessor<TestGetter>(str1t),
PropertyDescriptor::Accessor<TestGetter, TestSetter>(str2t),
PropertyDescriptor::Accessor<TestGetterWithUserData>(
str1at,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Accessor<TestGetterWithUserData,
TestSetterWithUserData>(
str2at,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Value(str3, trueValue),
PropertyDescriptor::Value(str4, trueValue, napi_writable),
PropertyDescriptor::Value(str5, trueValue, napi_enumerable),
PropertyDescriptor::Value(str6, trueValue, napi_configurable),
PropertyDescriptor::Function(env, obj, str7, TestFunction),
PropertyDescriptor::Function(env,
obj,
str8,
TestFunctionWithUserData,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
});
} else if (nameType.Utf8Value() == "value") {
obj.DefineProperties({
PropertyDescriptor::Accessor(
env, obj, Napi::String::New(env, "readonlyAccessor"), TestGetter),
PropertyDescriptor::Accessor(
env,
obj,
Napi::String::New(env, "readwriteAccessor"),
TestGetter,
TestSetter),
PropertyDescriptor::Accessor(
env,
obj,
Napi::String::New(env, "readonlyAccessorWithUserData"),
TestGetterWithUserData,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Accessor(
env,
obj,
Napi::String::New(env, "readwriteAccessorWithUserData"),
TestGetterWithUserData,
TestSetterWithUserData,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Accessor<TestGetter>(
Napi::String::New(env, "readonlyAccessorT")),
PropertyDescriptor::Accessor<TestGetter, TestSetter>(
Napi::String::New(env, "readwriteAccessorT")),
PropertyDescriptor::Accessor<TestGetterWithUserData>(
Napi::String::New(env, "readonlyAccessorWithUserDataT"),
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Accessor<TestGetterWithUserData,
TestSetterWithUserData>(
Napi::String::New(env, "readwriteAccessorWithUserDataT"),
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
PropertyDescriptor::Value(Napi::String::New(env, "readonlyValue"),
trueValue),
PropertyDescriptor::Value(
Napi::String::New(env, "readwriteValue"), trueValue, napi_writable),
PropertyDescriptor::Value(Napi::String::New(env, "enumerableValue"),
trueValue,
napi_enumerable),
PropertyDescriptor::Value(Napi::String::New(env, "configurableValue"),
trueValue,
napi_configurable),
PropertyDescriptor::Function(
env, obj, Napi::String::New(env, "function"), TestFunction),
PropertyDescriptor::Function(
env,
obj,
Napi::String::New(env, "functionWithUserData"),
TestFunctionWithUserData,
napi_property_attributes::napi_default,
reinterpret_cast<void*>(holder)),
});
}
}
void DefineValueProperty(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Name name = info[1].As<Name>();
Value value = info[2];
obj.DefineProperty(PropertyDescriptor::Value(name, value));
}
Value CreateObjectUsingMagic(const CallbackInfo& info) {
Env env = info.Env();
Object obj = Object::New(env);
obj["cp_false"] = false;
obj["cp_true"] = true;
obj[std::string("s_true")] = true;
obj[std::string("s_false")] = false;
obj["0"] = 0;
obj[(uint32_t)42] = 120;
obj["0.0f"] = 0.0f;
obj["0.0"] = 0.0;
obj["-1"] = -1;
obj["foo2"] = u"foo";
obj[std::string("foo4")] = NAPI_WIDE_TEXT("foo");
obj[std::string("foo5")] = "foo";
obj[std::string("foo6")] = std::u16string(NAPI_WIDE_TEXT("foo"));
obj[std::string("foo7")] = std::string("foo");
obj[std::string("circular")] = obj;
obj["circular2"] = obj;
return obj;
}
#ifdef NAPI_CPP_EXCEPTIONS
Value Sum(const CallbackInfo& info) {
Object object = info[0].As<Object>();
int64_t sum = 0;
for (const auto& e : object) {
sum += static_cast<Value>(e.second).As<Number>().Int64Value();
}
return Number::New(info.Env(), sum);
}
void Increment(const CallbackInfo& info) {
Env env = info.Env();
Object object = info[0].As<Object>();
for (auto e : object) {
int64_t value = static_cast<Value>(e.second).As<Number>().Int64Value();
++value;
e.second = Napi::Number::New(env, value);
}
}
#endif // NAPI_CPP_EXCEPTIONS
Value InstanceOf(const CallbackInfo& info) {
Object obj = info[0].UnsafeAs<Object>();
Function constructor = info[1].As<Function>();
return Boolean::New(info.Env(), MaybeUnwrap(obj.InstanceOf(constructor)));
}
Object InitObject(Env env) {
Object exports = Object::New(env);
exports["emptyConstructor"] = Function::New(env, EmptyConstructor);
exports["constructorFromObject"] = Function::New(env, ConstructorFromObject);
exports["GetPropertyNames"] = Function::New(env, GetPropertyNames);
exports["defineProperties"] = Function::New(env, DefineProperties);
exports["defineValueProperty"] = Function::New(env, DefineValueProperty);
exports["getPropertyWithUint32"] = Function::New(env, GetPropertyWithUint32);
exports["getPropertyWithNapiValue"] =
Function::New(env, GetPropertyWithNapiValue);
exports["getPropertyWithNapiWrapperValue"] =
Function::New(env, GetPropertyWithNapiWrapperValue);
exports["getPropertyWithCStyleString"] =
Function::New(env, GetPropertyWithCStyleString);
exports["getPropertyWithCppStyleString"] =
Function::New(env, GetPropertyWithCppStyleString);
exports["setPropertyWithUint32"] = Function::New(env, SetPropertyWithUint32);
exports["setPropertyWithNapiValue"] =
Function::New(env, SetPropertyWithNapiValue);
exports["setPropertyWithNapiWrapperValue"] =
Function::New(env, SetPropertyWithNapiWrapperValue);
exports["setPropertyWithCStyleString"] =
Function::New(env, SetPropertyWithCStyleString);
exports["setPropertyWithCppStyleString"] =
Function::New(env, SetPropertyWithCppStyleString);
exports["deletePropertyWithUint32"] =
Function::New(env, DeletePropertyWithUint32);
exports["deletePropertyWithNapiValue"] =
Function::New(env, DeletePropertyWithNapiValue);
exports["deletePropertyWithNapiWrapperValue"] =
Function::New(env, DeletePropertyWithNapiWrapperValue);
exports["deletePropertyWithCStyleString"] =
Function::New(env, DeletePropertyWithCStyleString);
exports["deletePropertyWithCppStyleString"] =
Function::New(env, DeletePropertyWithCppStyleString);
exports["hasOwnPropertyWithNapiValue"] =
Function::New(env, HasOwnPropertyWithNapiValue);
exports["hasOwnPropertyWithNapiWrapperValue"] =
Function::New(env, HasOwnPropertyWithNapiWrapperValue);
exports["hasOwnPropertyWithCStyleString"] =
Function::New(env, HasOwnPropertyWithCStyleString);
exports["hasOwnPropertyWithCppStyleString"] =
Function::New(env, HasOwnPropertyWithCppStyleString);
exports["hasPropertyWithUint32"] = Function::New(env, HasPropertyWithUint32);
exports["hasPropertyWithNapiValue"] =
Function::New(env, HasPropertyWithNapiValue);
exports["hasPropertyWithNapiWrapperValue"] =
Function::New(env, HasPropertyWithNapiWrapperValue);
exports["hasPropertyWithCStyleString"] =
Function::New(env, HasPropertyWithCStyleString);
exports["hasPropertyWithCppStyleString"] =
Function::New(env, HasPropertyWithCppStyleString);
exports["createObjectUsingMagic"] =
Function::New(env, CreateObjectUsingMagic);
#ifdef NAPI_CPP_EXCEPTIONS
exports["sum"] = Function::New(env, Sum);
exports["increment"] = Function::New(env, Increment);
#endif // NAPI_CPP_EXCEPTIONS
exports["addFinalizer"] = Function::New(env, AddFinalizer);
exports["addFinalizerWithHint"] = Function::New(env, AddFinalizerWithHint);
exports["instanceOf"] = Function::New(env, InstanceOf);
exports["subscriptGetWithCStyleString"] =
Function::New(env, SubscriptGetWithCStyleString);
exports["subscriptGetWithCppStyleString"] =
Function::New(env, SubscriptGetWithCppStyleString);
exports["subscriptGetAtIndex"] = Function::New(env, SubscriptGetAtIndex);
exports["subscriptSetWithCStyleString"] =
Function::New(env, SubscriptSetWithCStyleString);
exports["subscriptSetWithCppStyleString"] =
Function::New(env, SubscriptSetWithCppStyleString);
exports["subscriptSetAtIndex"] = Function::New(env, SubscriptSetAtIndex);
return exports;
}
|