| 3 | #include "test_helper.h" |
| 4 | |
| 5 | class FunctionTest : public Napi::ObjectWrap<FunctionTest> { |
| 6 | public: |
| 7 | FunctionTest(const Napi::CallbackInfo& info) |
| 8 | : Napi::ObjectWrap<FunctionTest>(info) {} |
| 9 | |
| 10 | static Napi::Value OnCalledAsFunction(const Napi::CallbackInfo& info) { |
| 11 | // If called with a "true" argument, throw an exeption to test the handling. |
| 12 | if (!info[0].IsUndefined() && MaybeUnwrap(info[0].ToBoolean())) { |
| 13 | NAPI_THROW(Napi::Error::New(info.Env(), "an exception"), Napi::Value()); |
| 14 | } |
| 15 | // Otherwise, act as a factory. |
| 16 | std::vector<napi_value> args; |
| 17 | for (size_t i = 0; i < info.Length(); i++) args.push_back(info[i]); |
| 18 | return MaybeUnwrap(GetConstructor(info.Env()).New(args)); |
| 19 | } |
| 20 | |
| 21 | static void Initialize(Napi::Env env, Napi::Object exports) { |
| 22 | const char* name = "FunctionTest"; |
| 23 | Napi::Function func = DefineClass(env, name, {}); |
| 24 | Napi::FunctionReference* ctor = new Napi::FunctionReference(); |
| 25 | *ctor = Napi::Persistent(func); |
| 26 | env.SetInstanceData(ctor); |
| 27 | exports.Set(name, func); |
| 28 | } |
| 29 | |
| 30 | static Napi::Function GetConstructor(Napi::Env env) { |
| 31 | return env.GetInstanceData<Napi::FunctionReference>()->Value(); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | Napi::Value ObjectWrapFunctionFactory(const Napi::CallbackInfo& info) { |
| 36 | Napi::Object exports = Napi::Object::New(info.Env()); |
no outgoing calls
no test coverage detected