Skip to content

Commit 590521b

Browse files
committed
Solve bug in trampoline callback heap, and add test for rejection.
1 parent b150db2 commit 590521b

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

source/loaders/node_loader/source/node_loader_impl.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,22 +1134,13 @@ napi_value node_loader_impl_async_future_resolve(loader_impl_node node_impl, fut
11341134
{
11351135
napi_value result;
11361136

1137-
napi_status status;
1138-
11391137
value arg, ret;
11401138

1141-
napi_handle_scope handle_scope;
1142-
11431139
if (node_impl == NULL || resolve == NULL)
11441140
{
11451141
return nullptr;
11461142
}
11471143

1148-
/* Create scope */
1149-
status = napi_open_handle_scope(node_impl->env, &handle_scope);
1150-
1151-
node_loader_impl_exception(node_impl->env, status);
1152-
11531144
/* Convert the argument to a value */
11541145
arg = node_loader_impl_napi_to_value(node_impl, node_impl->env, v);
11551146

@@ -1167,11 +1158,6 @@ napi_value node_loader_impl_async_future_resolve(loader_impl_node node_impl, fut
11671158
/* Return the result */
11681159
result = node_loader_impl_value_to_napi(node_impl, node_impl->env, ret);
11691160

1170-
/* Close scope */
1171-
status = napi_close_handle_scope(node_impl->env, handle_scope);
1172-
1173-
node_loader_impl_exception(node_impl->env, status);
1174-
11751161
/* Destroy return value */
11761162
value_type_destroy(ret);
11771163

@@ -1182,22 +1168,13 @@ napi_value node_loader_impl_async_future_reject(loader_impl_node node_impl, futu
11821168
{
11831169
napi_value result;
11841170

1185-
napi_status status;
1186-
11871171
value arg, ret;
11881172

1189-
napi_handle_scope handle_scope;
1190-
11911173
if (node_impl == NULL || reject == NULL)
11921174
{
11931175
return nullptr;
11941176
}
11951177

1196-
/* Create scope */
1197-
status = napi_open_handle_scope(node_impl->env, &handle_scope);
1198-
1199-
node_loader_impl_exception(node_impl->env, status);
1200-
12011178
/* Convert the argument to a value */
12021179
arg = node_loader_impl_napi_to_value(node_impl, node_impl->env, v);
12031180

@@ -1215,11 +1192,6 @@ napi_value node_loader_impl_async_future_reject(loader_impl_node node_impl, futu
12151192
/* Return the result */
12161193
result = node_loader_impl_value_to_napi(node_impl, node_impl->env, ret);
12171194

1218-
/* Close scope */
1219-
status = napi_close_handle_scope(node_impl->env, handle_scope);
1220-
1221-
node_loader_impl_exception(node_impl->env, status);
1222-
12231195
/* Destroy return value */
12241196
value_type_destroy(ret);
12251197

source/tests/metacall_node_async_test/source/metacall_node_async_test.cpp

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ TEST_F(metacall_node_async_test, DefaultConstructor)
4343
"function f(x) {\n"
4444
"\treturn new Promise(r => console.log(`Promise executed: ${util.inspect(r)} -> ${x}`) || r(x));\n"
4545
"}\n"
46-
"module.exports = { f };\n";
46+
"function g(x) {\n"
47+
"\treturn new Promise((_, r) => console.log(`Promise executed: ${util.inspect(r)} -> ${x}`) || r(x));\n"
48+
"}\n"
49+
"module.exports = { f, g };\n";
4750

4851
EXPECT_EQ((int) 0, (int) metacall_load_from_memory("node", buffer, sizeof(buffer), NULL));
4952

@@ -59,14 +62,13 @@ TEST_F(metacall_node_async_test, DefaultConstructor)
5962
234
6063
};
6164

65+
/* Test resolve */
6266
void * future = metacall_async("f", args);
6367

64-
metacall_value_destroy(args[0]);
65-
6668
EXPECT_NE((void *) NULL, (void *) future);
6769

6870
EXPECT_EQ((enum metacall_value_id) metacall_value_id(future), (enum metacall_value_id) METACALL_FUTURE);
69-
71+
7072
void * ret = metacall_await(future, [](void * result, void * data) -> void * {
7173
EXPECT_NE((void *) NULL, (void *) result);
7274

@@ -99,7 +101,50 @@ TEST_F(metacall_node_async_test, DefaultConstructor)
99101

100102
EXPECT_EQ((enum metacall_value_id) metacall_value_id(ret), (enum metacall_value_id) METACALL_FUTURE);
101103

104+
metacall_value_destroy(ret);
105+
106+
/* Test reject */
107+
future = metacall_async("g", args);
108+
109+
EXPECT_NE((void *) NULL, (void *) future);
110+
111+
EXPECT_EQ((enum metacall_value_id) metacall_value_id(future), (enum metacall_value_id) METACALL_FUTURE);
112+
113+
ret = metacall_await(future, [](void *, void *) -> void * {
114+
int this_should_never_be_executed = 0;
115+
116+
EXPECT_EQ((int) 1, (int) this_should_never_be_executed);
117+
118+
printf("Resolve C Callback\n");
119+
120+
return NULL;
121+
}, [](void * result, void * data) -> void * {
122+
EXPECT_NE((void *) NULL, (void *) result);
123+
124+
EXPECT_EQ((enum metacall_value_id) metacall_value_id(result), (enum metacall_value_id) METACALL_DOUBLE);
125+
126+
EXPECT_EQ((double) 10.0, (double) metacall_value_to_double(result));
127+
128+
EXPECT_NE((void *) NULL, (void *) data);
129+
130+
struct async_context * ctx = static_cast<struct async_context *>(data);
131+
132+
EXPECT_EQ((int) 234, (int) ctx->value);
133+
134+
printf("Reject C Callback\n");
135+
136+
return metacall_value_create_double(15.0);
137+
}, static_cast<void *>(&ctx));
138+
139+
metacall_value_destroy(future);
140+
141+
EXPECT_NE((void *) NULL, (void *) ret);
142+
143+
EXPECT_EQ((enum metacall_value_id) metacall_value_id(ret), (enum metacall_value_id) METACALL_FUTURE);
144+
102145
metacall_value_destroy(ret);
146+
147+
metacall_value_destroy(args[0]);
103148
}
104149
#endif /* OPTION_BUILD_LOADERS_NODE */
105150

0 commit comments

Comments
 (0)