Skip to content

Commit

Permalink
revert tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinEady committed Jun 15, 2024
1 parent e7e8a81 commit 61cd1ec
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 82 deletions.
4 changes: 2 additions & 2 deletions test/array_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Value CreateExternalBufferWithFinalize(const CallbackInfo& info) {
uint8_t* data = new uint8_t[testLength];

ArrayBuffer buffer = ArrayBuffer::New(
info.Env(), data, testLength, [](NogcEnv /*env*/, void* finalizeData) {
info.Env(), data, testLength, [](Env /*env*/, void* finalizeData) {
delete[] static_cast<uint8_t*>(finalizeData);
finalizeCount++;
});
Expand Down Expand Up @@ -94,7 +94,7 @@ Value CreateExternalBufferWithFinalizeHint(const CallbackInfo& info) {
info.Env(),
data,
testLength,
[](NogcEnv /*env*/, void* finalizeData, char* /*finalizeHint*/) {
[](Env /*env*/, void* finalizeData, char* /*finalizeHint*/) {
delete[] static_cast<uint8_t*>(finalizeData);
finalizeCount++;
},
Expand Down
15 changes: 6 additions & 9 deletions test/buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,11 @@ Value CreateExternalBufferWithFinalize(const CallbackInfo& info) {

uint16_t* data = new uint16_t[testLength];

Buffer<uint16_t> buffer =
Buffer<uint16_t>::New(info.Env(),
data,
testLength,
[](NogcEnv /*env*/, uint16_t* finalizeData) {
delete[] finalizeData;
finalizeCount++;
});
Buffer<uint16_t> buffer = Buffer<uint16_t>::New(
info.Env(), data, testLength, [](Env /*env*/, uint16_t* finalizeData) {
delete[] finalizeData;
finalizeCount++;
});

if (buffer.Length() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.")
Expand Down Expand Up @@ -86,7 +83,7 @@ Value CreateExternalBufferWithFinalizeHint(const CallbackInfo& info) {
info.Env(),
data,
testLength,
[](NogcEnv /*env*/, uint16_t* finalizeData, char* /*finalizeHint*/) {
[](Env /*env*/, uint16_t* finalizeData, char* /*finalizeHint*/) {
delete[] finalizeData;
finalizeCount++;
},
Expand Down
15 changes: 6 additions & 9 deletions test/buffer_new_or_copy-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ Value CreateOrCopyExternalBufferWithFinalize(const CallbackInfo& info) {
uint16_t* data = new uint16_t[testLength];
InitData(data, testLength);

Buffer<uint16_t> buffer =
Buffer<uint16_t>::NewOrCopy(info.Env(),
data,
testLength,
[](NogcEnv /*env*/, uint16_t* finalizeData) {
delete[] finalizeData;
finalizeCount++;
});
Buffer<uint16_t> buffer = Buffer<uint16_t>::NewOrCopy(
info.Env(), data, testLength, [](Env /*env*/, uint16_t* finalizeData) {
delete[] finalizeData;
finalizeCount++;
});

if (buffer.Length() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.")
Expand All @@ -54,7 +51,7 @@ Value CreateOrCopyExternalBufferWithFinalizeHint(const CallbackInfo& info) {
info.Env(),
data,
testLength,
[](NogcEnv /*env*/, uint16_t* finalizeData, char* /*finalizeHint*/) {
[](Env /*env*/, uint16_t* finalizeData, char* /*finalizeHint*/) {
delete[] finalizeData;
finalizeCount++;
},
Expand Down
28 changes: 6 additions & 22 deletions test/external.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ Value CreateExternal(const CallbackInfo& info) {

Value CreateExternalWithFinalize(const CallbackInfo& info) {
finalizeCount = 0;
return External<int>::New(
info.Env(), new int(1), [](NogcEnv /*env*/, int* data) {
delete data;
finalizeCount++;
});
return External<int>::New(info.Env(), new int(1), [](Env /*env*/, int* data) {
delete data;
finalizeCount++;
});
}

Value CreateExternalWithFinalizeHint(const CallbackInfo& info) {
Expand All @@ -27,7 +26,7 @@ Value CreateExternalWithFinalizeHint(const CallbackInfo& info) {
return External<int>::New(
info.Env(),
new int(1),
[](NogcEnv /*env*/, int* data, char* /*hint*/) {
[](Env /*env*/, int* data, char* /*hint*/) {
delete data;
finalizeCount++;
},
Expand Down Expand Up @@ -56,21 +55,6 @@ Value GetFinalizeCount(const CallbackInfo& info) {
}

Value CreateExternalWithFinalizeException(const CallbackInfo& info) {
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
auto o = External<int>::New(
info.Env(), new int(1), [](NogcEnv /*env*/, int* data) { delete data; });

info.Env().AddPostFinalizer([](Env env) {
Error error = Error::New(env, "Finalizer exception");
#ifdef NAPI_CPP_EXCEPTIONS
throw error;
#else
error.ThrowAsJavaScriptException();
#endif
});

return o;
#else
return External<int>::New(info.Env(), new int(1), [](Env env, int* data) {
Error error = Error::New(env, "Finalizer exception");
delete data;
Expand All @@ -80,8 +64,8 @@ Value CreateExternalWithFinalizeException(const CallbackInfo& info) {
error.ThrowAsJavaScriptException();
#endif
});
#endif
}

} // end anonymous namespace

Object InitExternal(Env env) {
Expand Down
10 changes: 1 addition & 9 deletions test/movable_callbacks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@ using namespace Napi;

Value createExternal(const CallbackInfo& info) {
FunctionReference ref = Reference<Function>::New(info[0].As<Function>(), 1);

#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
auto env = info.Env();
auto ret = External<char>::New(env, nullptr);

env.AddPostFinalizer(
[ref = std::move(ref)](Napi::Env /*env*/) { ref.Call({}); });
#else
auto ret = External<char>::New(
info.Env(),
nullptr,
[ref = std::move(ref)](Napi::Env /*env*/, char* /*data*/) {
ref.Call({});
});
#endif

return ret;
}

Expand Down
8 changes: 0 additions & 8 deletions test/object/finalizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ static int dummy;
Value AddFinalizer(const CallbackInfo& info) {
ObjectReference* ref = new ObjectReference;
*ref = Persistent(Object::New(info.Env()));
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
info.Env().AddPostFinalizer(
#else
info[0].As<Object>().AddFinalizer(
#endif
[](Napi::Env /*env*/, ObjectReference* ref) {
ref->Set("finalizerCalled", true);
delete ref;
Expand All @@ -23,11 +19,7 @@ Value AddFinalizer(const CallbackInfo& info) {
Value AddFinalizerWithHint(const CallbackInfo& info) {
ObjectReference* ref = new ObjectReference;
*ref = Persistent(Object::New(info.Env()));
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
info.Env().AddPostFinalizer(
#else
info[0].As<Object>().AddFinalizer(
#endif
[](Napi::Env /*env*/, ObjectReference* ref, int* dummy_p) {
ref->Set("finalizerCalledWithCorrectHint", dummy_p == &dummy);
delete ref;
Expand Down
25 changes: 2 additions & 23 deletions test/objectwrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,7 @@ class Test : public Napi::ObjectWrap<Test> {
public:
Test(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Test>(info) {
if (info.Length() > 0) {
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
info.Env().AddPostFinalizer(
[](Napi::Env env, Napi::FunctionReference* finalizeCb) {
if (finalizeCb->IsEmpty()) {
return;
}

finalizeCb->Call(env.Global(), {Napi::Boolean::New(env, true)});
finalizeCb->Unref();
delete finalizeCb;
},
new Napi::FunctionReference(
Napi::Persistent(info[0].As<Napi::Function>())));
#else
finalizeCb_ = Napi::Persistent(info[0].As<Napi::Function>());
#endif
}
// Create an own instance property.
info.This().As<Napi::Object>().DefineProperty(
Expand All @@ -65,7 +50,7 @@ class Test : public Napi::ObjectWrap<Test> {
Env(),
static_cast<uint8_t*>(malloc(1)),
1,
[](Napi::NogcEnv, uint8_t* bufaddr) { free(bufaddr); }));
[](Napi::Env, uint8_t* bufaddr) { free(bufaddr); }));
}

static Napi::Value OwnPropertyGetter(const Napi::CallbackInfo& info) {
Expand Down Expand Up @@ -280,20 +265,14 @@ class Test : public Napi::ObjectWrap<Test> {
}));
}

#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
void Finalize(NODE_ADDON_API_NOGC_ENV_CLASS /*env*/) override {
// Intentionally blank to ensure `override` works correctly.
}
#else
void Finalize(NODE_ADDON_API_NOGC_ENV_CLASS env) override {
void Finalize(Napi::Env env) {
if (finalizeCb_.IsEmpty()) {
return;
}

finalizeCb_.Call(env.Global(), {Napi::Boolean::New(env, true)});
finalizeCb_.Unref();
}
#endif

private:
std::string value_;
Expand Down

0 comments on commit 61cd1ec

Please sign in to comment.