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
|
#include "napi.h"
#include "test_helper.h"
using namespace Napi;
class FuncRefObject : public Napi::ObjectWrap<FuncRefObject> {
public:
FuncRefObject(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<FuncRefObject>(info) {
Napi::Env env = info.Env();
int argLen = info.Length();
if (argLen <= 0 || !info[0].IsNumber()) {
Napi::TypeError::New(env, "First param should be a number")
.ThrowAsJavaScriptException();
return;
}
Napi::Number value = info[0].As<Napi::Number>();
this->_value = value.Int32Value();
}
Napi::Value GetValue(const Napi::CallbackInfo& info) {
int value = this->_value;
return Napi::Number::New(info.Env(), value);
}
private:
int _value;
};
namespace {
Value ConstructRefFromExisitingRef(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
FunctionReference movedRef;
ref.Reset(info[0].As<Function>());
movedRef = std::move(ref);
return MaybeUnwrap(movedRef({}));
}
Value CallWithVectorArgs(const CallbackInfo& info) {
HandleScope scope(info.Env());
std::vector<napi_value> newVec;
FunctionReference ref;
ref.Reset(info[0].As<Function>());
for (int i = 1; i < (int)info.Length(); i++) {
newVec.push_back(info[i]);
}
return MaybeUnwrap(ref.Call(newVec));
}
Value CallWithInitList(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
ref.Reset(info[0].As<Function>());
return MaybeUnwrap(ref.Call({info[1], info[2], info[3]}));
}
Value CallWithRecvInitList(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
ref.Reset(info[0].As<Function>());
return MaybeUnwrap(ref.Call(info[1], {info[2], info[3], info[4]}));
}
Value CallWithRecvVector(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
std::vector<napi_value> newVec;
ref.Reset(info[0].As<Function>());
for (int i = 2; i < (int)info.Length(); i++) {
newVec.push_back(info[i]);
}
return MaybeUnwrap(ref.Call(info[1], newVec));
}
Value CallWithRecvArgc(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
ref.Reset(info[0].As<Function>());
size_t argLength = info.Length() > 2 ? info.Length() - 2 : 0;
std::unique_ptr<napi_value[]> args{argLength > 0 ? new napi_value[argLength]
: nullptr};
for (size_t i = 0; i < argLength; ++i) {
args[i] = info[i + 2];
}
return MaybeUnwrap(ref.Call(info[1], argLength, args.get()));
}
Value MakeAsyncCallbackWithInitList(const Napi::CallbackInfo& info) {
Napi::FunctionReference ref;
ref.Reset(info[0].As<Function>());
Napi::AsyncContext context(info.Env(), "func_ref_resources", {});
return MaybeUnwrap(
ref.MakeCallback(Napi::Object::New(info.Env()), {}, context));
}
Value MakeAsyncCallbackWithVector(const Napi::CallbackInfo& info) {
Napi::FunctionReference ref;
ref.Reset(info[0].As<Function>());
std::vector<napi_value> newVec;
Napi::AsyncContext context(info.Env(), "func_ref_resources", {});
for (int i = 1; i < (int)info.Length(); i++) {
newVec.push_back(info[i]);
}
return MaybeUnwrap(
ref.MakeCallback(Napi::Object::New(info.Env()), newVec, context));
}
Value MakeAsyncCallbackWithArgv(const Napi::CallbackInfo& info) {
Napi::FunctionReference ref;
ref.Reset(info[0].As<Function>());
size_t argLength = info.Length() > 1 ? info.Length() - 1 : 0;
std::unique_ptr<napi_value[]> args{argLength > 0 ? new napi_value[argLength]
: nullptr};
for (size_t i = 0; i < argLength; ++i) {
args[i] = info[i + 1];
}
Napi::AsyncContext context(info.Env(), "func_ref_resources", {});
return MaybeUnwrap(ref.MakeCallback(Napi::Object::New(info.Env()),
argLength,
argLength > 0 ? args.get() : nullptr,
context));
}
Value CreateFunctionReferenceUsingNew(const Napi::CallbackInfo& info) {
Napi::Function func = ObjectWrap<FuncRefObject>::DefineClass(
info.Env(),
"MyObject",
{ObjectWrap<FuncRefObject>::InstanceMethod("getValue",
&FuncRefObject::GetValue)});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
return MaybeUnwrapOr(constructor->New({info[0].As<Number>()}), Object());
}
Value CreateFunctionReferenceUsingNewVec(const Napi::CallbackInfo& info) {
Napi::Function func = ObjectWrap<FuncRefObject>::DefineClass(
info.Env(),
"MyObject",
{ObjectWrap<FuncRefObject>::InstanceMethod("getValue",
&FuncRefObject::GetValue)});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
std::vector<napi_value> newVec;
newVec.push_back(info[0]);
return MaybeUnwrapOr(constructor->New(newVec), Object());
}
Value Call(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
ref.Reset(info[0].As<Function>());
return MaybeUnwrapOr(ref.Call({}), Value());
}
Value Construct(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
ref.Reset(info[0].As<Function>());
return MaybeUnwrapOr(ref.New({}), Object());
}
} // namespace
Object InitFunctionReference(Env env) {
Object exports = Object::New(env);
exports["CreateFuncRefWithNew"] =
Function::New(env, CreateFunctionReferenceUsingNew);
exports["CreateFuncRefWithNewVec"] =
Function::New(env, CreateFunctionReferenceUsingNewVec);
exports["CallWithRecvArgc"] = Function::New(env, CallWithRecvArgc);
exports["CallWithRecvVector"] = Function::New(env, CallWithRecvVector);
exports["CallWithRecvInitList"] = Function::New(env, CallWithRecvInitList);
exports["CallWithInitList"] = Function::New(env, CallWithInitList);
exports["CallWithVec"] = Function::New(env, CallWithVectorArgs);
exports["ConstructWithMove"] =
Function::New(env, ConstructRefFromExisitingRef);
exports["AsyncCallWithInitList"] =
Function::New(env, MakeAsyncCallbackWithInitList);
exports["AsyncCallWithVector"] =
Function::New(env, MakeAsyncCallbackWithVector);
exports["AsyncCallWithArgv"] = Function::New(env, MakeAsyncCallbackWithArgv);
exports["call"] = Function::New(env, Call);
exports["construct"] = Function::New(env, Construct);
return exports;
}
|