Browse Source

Fixed export of Promise

Dmitry Panov 3 years ago
parent
commit
d6b8625efe
2 changed files with 24 additions and 0 deletions
  1. 11 0
      builtin_promise.go
  2. 13 0
      runtime_test.go

+ 11 - 0
builtin_promise.go

@@ -2,6 +2,7 @@ package goja
 
 
 import (
 import (
 	"github.com/dop251/goja/unistring"
 	"github.com/dop251/goja/unistring"
+	"reflect"
 )
 )
 
 
 type PromiseState int
 type PromiseState int
@@ -42,6 +43,8 @@ type promiseReaction struct {
 	handler    *jobCallback
 	handler    *jobCallback
 }
 }
 
 
+var typePromise = reflect.TypeOf((*Promise)(nil))
+
 // Promise is a Go wrapper around ECMAScript Promise. Calling Runtime.ToValue() on it
 // Promise is a Go wrapper around ECMAScript Promise. Calling Runtime.ToValue() on it
 // returns the underlying Object. Calling Export() on a Promise Object returns a Promise.
 // returns the underlying Object. Calling Export() on a Promise Object returns a Promise.
 //
 //
@@ -136,6 +139,14 @@ func (p *Promise) fulfill(value Value) Value {
 	return _undefined
 	return _undefined
 }
 }
 
 
+func (p *Promise) exportType() reflect.Type {
+	return typePromise
+}
+
+func (p *Promise) export(*objectExportCtx) interface{} {
+	return p
+}
+
 func (r *Runtime) newPromiseResolveThenableJob(p *Promise, thenable Value, then *jobCallback) func() {
 func (r *Runtime) newPromiseResolveThenableJob(p *Promise, thenable Value, then *jobCallback) func() {
 	return func() {
 	return func() {
 		resolve, reject := p.createResolvingFunctions()
 		resolve, reject := p.createResolvingFunctions()

+ 13 - 0
runtime_test.go

@@ -2322,6 +2322,19 @@ assert.sameValue(callCount, 3, '"then"" invoked once for every iterated value');
 	testScript1(TESTLIB+SCRIPT, _undefined, t)
 	testScript1(TESTLIB+SCRIPT, _undefined, t)
 }
 }
 
 
+func TestPromiseExport(t *testing.T) {
+	vm := New()
+	p, _, _ := vm.NewPromise()
+	pv := vm.ToValue(p)
+	if actual := pv.ExportType(); actual != reflect.TypeOf((*Promise)(nil)) {
+		t.Fatalf("Export type: %v", actual)
+	}
+
+	if ev := pv.Export(); ev != p {
+		t.Fatalf("Export value: %v", ev)
+	}
+}
+
 /*
 /*
 func TestArrayConcatSparse(t *testing.T) {
 func TestArrayConcatSparse(t *testing.T) {
 function foo(a,b,c)
 function foo(a,b,c)