Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Core/Node-API/Source/env_hermes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ namespace Napi
// exceptions thrown FROM user callbacks are already handled in
// AppRuntime::Dispatch's try/catch.
(void)runtime->drainJobs();
// WeakRef targets are kept alive only until the current job finishes.
runtime->clearKeptObjects();
}

Napi::Value Eval(Napi::Env env, const char* source, const char* sourceUrl)
Expand Down
2 changes: 2 additions & 0 deletions Tests/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ endif()

if(NAPI_JAVASCRIPT_ENGINE STREQUAL "Chakra")
set(SOURCES ${SOURCES} "Source/Tests.NodeApi.Chakra.cpp")
elseif(NAPI_JAVASCRIPT_ENGINE STREQUAL "Hermes")
set(SOURCES ${SOURCES} "Source/Tests.NodeApi.Hermes.cpp")
elseif(NAPI_JAVASCRIPT_ENGINE STREQUAL "JavaScriptCore")
set(SOURCES ${SOURCES} "Source/Tests.NodeApi.JavaScriptCore.cpp")
endif()
Expand Down
16 changes: 8 additions & 8 deletions Tests/UnitTests/Source/Scripts/tests.scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ describe("setTimeout", function () {
});

it("should call the given function after the given delay", function (done) {
const startTime = new Date().getTime();
const startTime = performance.now();
setTimeout(() => {
try {
expect(new Date().getTime() - startTime).to.be.at.least(10);
expect(performance.now() - startTime).to.be.at.least(10);
done();
}
catch (e) {
Expand All @@ -27,11 +27,11 @@ describe("setTimeout", function () {
});

it("should call the given nested function after the given delay", function (done) {
const startTime = new Date().getTime();
const startTime = performance.now();
setTimeout(() => {
setTimeout(() => {
try {
expect(new Date().getTime() - startTime).to.be.at.least(20);
expect(performance.now() - startTime).to.be.at.least(20);
done();
}
catch (e) {
Expand All @@ -42,10 +42,10 @@ describe("setTimeout", function () {
});

it("should call the given function after the given delay when the delay is a string representing a valid number", function (done) {
const startTime = new Date().getTime();
const startTime = performance.now();
setTimeout(() => {
try {
expect(new Date().getTime() - startTime).to.be.at.least(10);
expect(performance.now() - startTime).to.be.at.least(10);
done();
}
catch (e) {
Expand Down Expand Up @@ -143,12 +143,12 @@ describe("setInterval", function () {
});

it("should call the given function at the given interval", function (done) {
let startTime = new Date().getTime();
const startTime = performance.now();
let tickCount = 0;
const id = setInterval(() => {
try {
tickCount++;
expect(new Date().getTime() - startTime).to.be.at.least(tickCount * 10);
expect(performance.now() - startTime).to.be.at.least(tickCount * 10);
if (tickCount > 2) {
clearInterval(id);
done();
Expand Down
36 changes: 36 additions & 0 deletions Tests/UnitTests/Source/Tests.NodeApi.Hermes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <Babylon/AppRuntime.h>
#include <gtest/gtest.h>
#include <napi/env.h>

#include <chrono>
#include <future>
#include <stdexcept>

TEST(NodeApi, HermesDrainJobsReleasesWeakRefTargets)
{
std::promise<double> externalBytes;
auto completed = externalBytes.get_future();
Babylon::AppRuntime runtime{};
runtime.Dispatch([&](Napi::Env env) {
try
{
for (size_t i = 0; i < 256; ++i)
{
{
Napi::HandleScope scope{env};
Napi::Eval(env, "new WeakRef(new Uint8Array(1024 * 1024));", "weakref-job.js");
}
Napi::DrainJobs(env);
}
externalBytes.set_value(Napi::Eval(env,
"HermesInternal.getInstrumentedStats().js_externalBytes",
"weakref-heap.js").As<Napi::Number>().DoubleValue());
}
catch (const Napi::Error& error)
{
externalBytes.set_exception(std::make_exception_ptr(std::runtime_error{error.Message()}));
}
});
ASSERT_EQ(completed.wait_for(std::chrono::seconds{30}), std::future_status::ready);
EXPECT_LT(completed.get(), 64 * 1024 * 1024);
}
Loading