From db84fcc0d1ee46ad649b494609e2accbc9aa2de9 Mon Sep 17 00:00:00 2001 From: tommie Date: Thu, 14 Oct 2021 16:18:01 +0200 Subject: [PATCH] Fix wrong return value for Promise.{Then,Catch} (#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. --- promise_test.go | 8 ++++---- v8go.cc | 40 +++++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/promise_test.go b/promise_test.go index 396b11588..0ecf467d2 100644 --- a/promise_test.go +++ b/promise_test.go @@ -102,11 +102,11 @@ func TestPromiseRejected(t *testing.T) { t.Fatalf("expected [%v], was: %+v", val2, thenInfo.Args()) } - if then2Fulfilled { - t.Fatalf("unexpectedly called onFulfilled") + if !then2Fulfilled { + t.Fatalf("expected call to onFulfilled, got none") } - if !then2Rejected { - t.Fatalf("expected call to onRejected, got none") + if then2Rejected { + t.Fatalf("unexpectedly called onRejected") } } diff --git a/v8go.cc b/v8go.cc index 7f5f36ade..144470fc2 100644 --- a/v8go.cc +++ b/v8go.cc @@ -20,6 +20,7 @@ struct _EXCEPTION_POINTERS; #include "libplatform/libplatform.h" #include "v8.h" +#include "_cgo_export.h" using namespace v8; @@ -287,7 +288,6 @@ static void FunctionTemplateCallback(const FunctionCallbackInfo& info) { // we can use the context registry to match the Context on the Go side Local local_ctx = iso->GetCurrentContext(); int ctx_ref = local_ctx->GetEmbedderData(1).As()->Value(); - ContextPtr goContext(int ctxref); m_ctx* ctx = goContext(ctx_ref); int callback_ref = info.Data().As()->Value(); @@ -311,8 +311,6 @@ static void FunctionTemplateCallback(const FunctionCallbackInfo& info) { args[i] = tracked_value(ctx, val); } - ValuePtr goFunctionCallback(int ctxref, int cbref, - const ValuePtr* thisAndArgs, int args_count); ValuePtr val = goFunctionCallback(ctx_ref, callback_ref, thisAndArgs, args_count); if (val != nullptr) { @@ -1182,12 +1180,12 @@ RtnValue PromiseThen(ValuePtr ptr, int callback_ref) { rtn.error = ExceptionError(try_catch, iso, local_ctx); return rtn; } - m_value* promise_val = new m_value; - promise_val->iso = iso; - promise_val->ctx = ctx; - promise_val->ptr = - Persistent>(iso, promise); - rtn.value = tracked_value(ctx, promise_val); + m_value* result_val = new m_value; + result_val->iso = iso; + result_val->ctx = ctx; + result_val->ptr = + Persistent>(iso, result); + rtn.value = tracked_value(ctx, result_val); return rtn; } @@ -1215,12 +1213,12 @@ RtnValue PromiseThen2(ValuePtr ptr, int on_fulfilled_ref, int on_rejected_ref) { rtn.error = ExceptionError(try_catch, iso, local_ctx); return rtn; } - m_value* promise_val = new m_value; - promise_val->iso = iso; - promise_val->ctx = ctx; - promise_val->ptr = - Persistent>(iso, promise); - rtn.value = tracked_value(ctx, promise_val); + m_value* result_val = new m_value; + result_val->iso = iso; + result_val->ctx = ctx; + result_val->ptr = + Persistent>(iso, result); + rtn.value = tracked_value(ctx, result_val); return rtn; } @@ -1240,12 +1238,12 @@ RtnValue PromiseCatch(ValuePtr ptr, int callback_ref) { rtn.error = ExceptionError(try_catch, iso, local_ctx); return rtn; } - m_value* promise_val = new m_value; - promise_val->iso = iso; - promise_val->ctx = ctx; - promise_val->ptr = - Persistent>(iso, promise); - rtn.value = tracked_value(ctx, promise_val); + m_value* result_val = new m_value; + result_val->iso = iso; + result_val->ctx = ctx; + result_val->ptr = + Persistent>(iso, result); + rtn.value = tracked_value(ctx, result_val); return rtn; }