Skip to content

Commit db84fcc

Browse files
authored
Fix wrong return value for Promise.{Then,Catch} (rogchap#193)
* Fix wrong return value for Promise.{Then,Catch}. They should return the new promise, not the input one. A test approved of the bad behavior. * Rely on _cgo_export.h instead of inlined extern references.
1 parent 10e762d commit db84fcc

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

promise_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ func TestPromiseRejected(t *testing.T) {
102102
t.Fatalf("expected [%v], was: %+v", val2, thenInfo.Args())
103103
}
104104

105-
if then2Fulfilled {
106-
t.Fatalf("unexpectedly called onFulfilled")
105+
if !then2Fulfilled {
106+
t.Fatalf("expected call to onFulfilled, got none")
107107
}
108-
if !then2Rejected {
109-
t.Fatalf("expected call to onRejected, got none")
108+
if then2Rejected {
109+
t.Fatalf("unexpectedly called onRejected")
110110
}
111111
}
112112

v8go.cc

+19-21
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct _EXCEPTION_POINTERS;
2020

2121
#include "libplatform/libplatform.h"
2222
#include "v8.h"
23+
#include "_cgo_export.h"
2324

2425
using namespace v8;
2526

@@ -287,7 +288,6 @@ static void FunctionTemplateCallback(const FunctionCallbackInfo<Value>& info) {
287288
// we can use the context registry to match the Context on the Go side
288289
Local<Context> local_ctx = iso->GetCurrentContext();
289290
int ctx_ref = local_ctx->GetEmbedderData(1).As<Integer>()->Value();
290-
ContextPtr goContext(int ctxref);
291291
m_ctx* ctx = goContext(ctx_ref);
292292

293293
int callback_ref = info.Data().As<Integer>()->Value();
@@ -311,8 +311,6 @@ static void FunctionTemplateCallback(const FunctionCallbackInfo<Value>& info) {
311311
args[i] = tracked_value(ctx, val);
312312
}
313313

314-
ValuePtr goFunctionCallback(int ctxref, int cbref,
315-
const ValuePtr* thisAndArgs, int args_count);
316314
ValuePtr val =
317315
goFunctionCallback(ctx_ref, callback_ref, thisAndArgs, args_count);
318316
if (val != nullptr) {
@@ -1182,12 +1180,12 @@ RtnValue PromiseThen(ValuePtr ptr, int callback_ref) {
11821180
rtn.error = ExceptionError(try_catch, iso, local_ctx);
11831181
return rtn;
11841182
}
1185-
m_value* promise_val = new m_value;
1186-
promise_val->iso = iso;
1187-
promise_val->ctx = ctx;
1188-
promise_val->ptr =
1189-
Persistent<Value, CopyablePersistentTraits<Value>>(iso, promise);
1190-
rtn.value = tracked_value(ctx, promise_val);
1183+
m_value* result_val = new m_value;
1184+
result_val->iso = iso;
1185+
result_val->ctx = ctx;
1186+
result_val->ptr =
1187+
Persistent<Value, CopyablePersistentTraits<Value>>(iso, result);
1188+
rtn.value = tracked_value(ctx, result_val);
11911189
return rtn;
11921190
}
11931191

@@ -1215,12 +1213,12 @@ RtnValue PromiseThen2(ValuePtr ptr, int on_fulfilled_ref, int on_rejected_ref) {
12151213
rtn.error = ExceptionError(try_catch, iso, local_ctx);
12161214
return rtn;
12171215
}
1218-
m_value* promise_val = new m_value;
1219-
promise_val->iso = iso;
1220-
promise_val->ctx = ctx;
1221-
promise_val->ptr =
1222-
Persistent<Value, CopyablePersistentTraits<Value>>(iso, promise);
1223-
rtn.value = tracked_value(ctx, promise_val);
1216+
m_value* result_val = new m_value;
1217+
result_val->iso = iso;
1218+
result_val->ctx = ctx;
1219+
result_val->ptr =
1220+
Persistent<Value, CopyablePersistentTraits<Value>>(iso, result);
1221+
rtn.value = tracked_value(ctx, result_val);
12241222
return rtn;
12251223
}
12261224

@@ -1240,12 +1238,12 @@ RtnValue PromiseCatch(ValuePtr ptr, int callback_ref) {
12401238
rtn.error = ExceptionError(try_catch, iso, local_ctx);
12411239
return rtn;
12421240
}
1243-
m_value* promise_val = new m_value;
1244-
promise_val->iso = iso;
1245-
promise_val->ctx = ctx;
1246-
promise_val->ptr =
1247-
Persistent<Value, CopyablePersistentTraits<Value>>(iso, promise);
1248-
rtn.value = tracked_value(ctx, promise_val);
1241+
m_value* result_val = new m_value;
1242+
result_val->iso = iso;
1243+
result_val->ctx = ctx;
1244+
result_val->ptr =
1245+
Persistent<Value, CopyablePersistentTraits<Value>>(iso, result);
1246+
rtn.value = tracked_value(ctx, result_val);
12491247
return rtn;
12501248
}
12511249

0 commit comments

Comments
 (0)