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
|
#include <napi.h>
#include "test_helper.h"
Napi::ObjectReference testStaticContextRef;
Napi::Value StaticGetter(const Napi::CallbackInfo& /*info*/) {
return MaybeUnwrap(testStaticContextRef.Value().Get("value"));
}
void StaticSetter(const Napi::CallbackInfo& /*info*/,
const Napi::Value& value) {
testStaticContextRef.Value().Set("value", value);
}
void StaticMethodVoidCb(const Napi::CallbackInfo& info) {
StaticSetter(info, info[0].As<Napi::Number>());
}
Napi::Value TestStaticMethod(const Napi::CallbackInfo& info) {
std::string str = MaybeUnwrap(info[0].ToString());
return Napi::String::New(info.Env(), str + " static");
}
Napi::Value TestStaticMethodInternal(const Napi::CallbackInfo& info) {
std::string str = MaybeUnwrap(info[0].ToString());
return Napi::String::New(info.Env(), str + " static internal");
}
class Test : public Napi::ObjectWrap<Test> {
public:
Test(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Test>(info) {
if (info.Length() > 0) {
finalizeCb_ = Napi::Persistent(info[0].As<Napi::Function>());
}
// Create an own instance property.
info.This().As<Napi::Object>().DefineProperty(
Napi::PropertyDescriptor::Accessor(info.Env(),
info.This().As<Napi::Object>(),
"ownProperty",
OwnPropertyGetter,
napi_enumerable,
this));
// Create an own instance property with a templated function.
info.This().As<Napi::Object>().DefineProperty(
Napi::PropertyDescriptor::Accessor<OwnPropertyGetter>(
"ownPropertyT", napi_enumerable, this));
bufref_ = Napi::Persistent(Napi::Buffer<uint8_t>::New(
Env(),
static_cast<uint8_t*>(malloc(1)),
1,
[](Napi::Env, uint8_t* bufaddr) { free(bufaddr); }));
}
static Napi::Value OwnPropertyGetter(const Napi::CallbackInfo& info) {
return static_cast<Test*>(info.Data())->Getter(info);
}
static Napi::Value CanUnWrap(const Napi::CallbackInfo& info) {
Napi::Object wrappedObject = info[0].As<Napi::Object>();
std::string expectedString = info[1].As<Napi::String>();
Test* nativeObject = Test::Unwrap(wrappedObject);
std::string strVal = MaybeUnwrap(nativeObject->Getter(info).ToString());
return Napi::Boolean::New(info.Env(), strVal == expectedString);
}
void Setter(const Napi::CallbackInfo& /*info*/, const Napi::Value& value) {
value_ = MaybeUnwrap(value.ToString());
}
Napi::Value Getter(const Napi::CallbackInfo& info) {
return Napi::String::New(info.Env(), value_);
}
Napi::Value TestMethod(const Napi::CallbackInfo& info) {
std::string str = MaybeUnwrap(info[0].ToString());
return Napi::String::New(info.Env(), str + " instance");
}
Napi::Value TestMethodInternal(const Napi::CallbackInfo& info) {
std::string str = MaybeUnwrap(info[0].ToString());
return Napi::String::New(info.Env(), str + " instance internal");
}
Napi::Value ToStringTag(const Napi::CallbackInfo& info) {
return Napi::String::From(info.Env(), "TestTag");
}
// creates dummy array, returns `([value])[Symbol.iterator]()`
Napi::Value Iterator(const Napi::CallbackInfo& info) {
Napi::Array array = Napi::Array::New(info.Env());
array.Set(array.Length(), Napi::String::From(info.Env(), value_));
return MaybeUnwrap(
MaybeUnwrap(array.Get(MaybeUnwrap(
Napi::Symbol::WellKnown(info.Env(), "iterator"))))
.As<Napi::Function>()
.Call(array, {}));
}
void TestVoidMethodT(const Napi::CallbackInfo& info) {
value_ = MaybeUnwrap(info[0].ToString());
}
Napi::Value TestMethodT(const Napi::CallbackInfo& info) {
return Napi::String::New(info.Env(), value_);
}
static Napi::Value TestStaticMethodT(const Napi::CallbackInfo& info) {
return Napi::String::New(info.Env(), s_staticMethodText);
}
static void TestStaticVoidMethodT(const Napi::CallbackInfo& info) {
s_staticMethodText = MaybeUnwrap(info[0].ToString());
}
static void Initialize(Napi::Env env, Napi::Object exports) {
Napi::Symbol kTestStaticValueInternal =
Napi::Symbol::New(env, "kTestStaticValueInternal");
Napi::Symbol kTestStaticAccessorInternal =
Napi::Symbol::New(env, "kTestStaticAccessorInternal");
Napi::Symbol kTestStaticAccessorTInternal =
Napi::Symbol::New(env, "kTestStaticAccessorTInternal");
Napi::Symbol kTestStaticMethodInternal =
Napi::Symbol::New(env, "kTestStaticMethodInternal");
Napi::Symbol kTestStaticMethodTInternal =
Napi::Symbol::New(env, "kTestStaticMethodTInternal");
Napi::Symbol kTestStaticVoidMethodTInternal =
Napi::Symbol::New(env, "kTestStaticVoidMethodTInternal");
Napi::Symbol kTestStaticVoidMethodInternal =
Napi::Symbol::New(env, "kTestStaticVoidMethodInternal");
Napi::Symbol kTestValueInternal =
Napi::Symbol::New(env, "kTestValueInternal");
Napi::Symbol kTestAccessorInternal =
Napi::Symbol::New(env, "kTestAccessorInternal");
Napi::Symbol kTestAccessorTInternal =
Napi::Symbol::New(env, "kTestAccessorTInternal");
Napi::Symbol kTestMethodInternal =
Napi::Symbol::New(env, "kTestMethodInternal");
Napi::Symbol kTestMethodTInternal =
Napi::Symbol::New(env, "kTestMethodTInternal");
Napi::Symbol kTestVoidMethodTInternal =
Napi::Symbol::New(env, "kTestVoidMethodTInternal");
exports.Set(
"Test",
DefineClass(
env,
"Test",
{
// expose symbols for testing
StaticValue("kTestStaticValueInternal",
kTestStaticValueInternal),
StaticValue("kTestStaticAccessorInternal",
kTestStaticAccessorInternal),
StaticValue("kTestStaticAccessorTInternal",
kTestStaticAccessorTInternal),
StaticValue("kTestStaticMethodInternal",
kTestStaticMethodInternal),
StaticValue("kTestStaticMethodTInternal",
kTestStaticMethodTInternal),
StaticValue("kTestStaticVoidMethodInternal",
kTestStaticVoidMethodInternal),
StaticValue("kTestStaticVoidMethodTInternal",
kTestStaticVoidMethodTInternal),
StaticValue("kTestValueInternal", kTestValueInternal),
StaticValue("kTestAccessorInternal", kTestAccessorInternal),
StaticValue("kTestAccessorTInternal", kTestAccessorTInternal),
StaticValue("kTestMethodInternal", kTestMethodInternal),
StaticValue("kTestMethodTInternal", kTestMethodTInternal),
StaticValue("kTestVoidMethodTInternal",
kTestVoidMethodTInternal),
// test data
StaticValue("testStaticValue",
Napi::String::New(env, "value"),
napi_enumerable),
StaticValue(kTestStaticValueInternal,
Napi::Number::New(env, 5),
napi_default),
StaticAccessor("testStaticGetter",
&StaticGetter,
nullptr,
napi_enumerable),
StaticAccessor(
"testStaticSetter", nullptr, &StaticSetter, napi_default),
StaticAccessor("testStaticGetSet",
&StaticGetter,
&StaticSetter,
napi_enumerable),
StaticAccessor(kTestStaticAccessorInternal,
&StaticGetter,
&StaticSetter,
napi_enumerable),
StaticAccessor<&StaticGetter>("testStaticGetterT"),
StaticAccessor<&StaticGetter, &StaticSetter>(
"testStaticGetSetT"),
StaticAccessor<&StaticGetter, &StaticSetter>(
kTestStaticAccessorTInternal),
StaticMethod(
"testStaticVoidMethod", &StaticMethodVoidCb, napi_default),
StaticMethod(kTestStaticVoidMethodInternal,
&StaticMethodVoidCb,
napi_default),
StaticMethod(
"testStaticMethod", &TestStaticMethod, napi_enumerable),
StaticMethod(kTestStaticMethodInternal,
&TestStaticMethodInternal,
napi_default),
StaticMethod<&TestStaticVoidMethodT>("testStaticVoidMethodT"),
StaticMethod<&TestStaticMethodT>("testStaticMethodT"),
StaticMethod<&TestStaticVoidMethodT>(
kTestStaticVoidMethodTInternal),
StaticMethod<&TestStaticMethodT>(kTestStaticMethodTInternal),
StaticMethod("canUnWrap", &CanUnWrap, napi_enumerable),
InstanceValue("testValue",
Napi::Boolean::New(env, true),
napi_enumerable),
InstanceValue(kTestValueInternal,
Napi::Boolean::New(env, false),
napi_enumerable),
InstanceAccessor(
"testGetter", &Test::Getter, nullptr, napi_enumerable),
InstanceAccessor(
"testSetter", nullptr, &Test::Setter, napi_default),
InstanceAccessor("testGetSet",
&Test::Getter,
&Test::Setter,
napi_enumerable),
InstanceAccessor(kTestAccessorInternal,
&Test::Getter,
&Test::Setter,
napi_enumerable),
InstanceAccessor<&Test::Getter>("testGetterT"),
InstanceAccessor<&Test::Getter, &Test::Setter>("testGetSetT"),
InstanceAccessor<&Test::Getter, &Test::Setter>(
kTestAccessorInternal),
InstanceMethod(
"testMethod", &Test::TestMethod, napi_enumerable),
InstanceMethod(kTestMethodInternal,
&Test::TestMethodInternal,
napi_default),
InstanceMethod<&Test::TestMethodT>("testMethodT"),
InstanceMethod<&Test::TestVoidMethodT>("testVoidMethodT"),
InstanceMethod<&Test::TestMethodT>(kTestMethodTInternal),
InstanceMethod<&Test::TestVoidMethodT>(
kTestVoidMethodTInternal),
// conventions
InstanceAccessor(
MaybeUnwrap(Napi::Symbol::WellKnown(env, "toStringTag")),
&Test::ToStringTag,
nullptr,
napi_enumerable),
InstanceMethod(
MaybeUnwrap(Napi::Symbol::WellKnown(env, "iterator")),
&Test::Iterator,
napi_default),
}));
}
void Finalize(Napi::Env env) {
if (finalizeCb_.IsEmpty()) {
return;
}
finalizeCb_.Call(env.Global(), {Napi::Boolean::New(env, true)});
finalizeCb_.Unref();
}
private:
std::string value_;
Napi::FunctionReference finalizeCb_;
static std::string s_staticMethodText;
Napi::Reference<Napi::Buffer<uint8_t>> bufref_;
};
std::string Test::s_staticMethodText;
Napi::Object InitObjectWrap(Napi::Env env) {
testStaticContextRef = Napi::Persistent(Napi::Object::New(env));
testStaticContextRef.SuppressDestruct();
Napi::Object exports = Napi::Object::New(env);
Test::Initialize(env, exports);
return exports;
}
|