Skip to content

Commit

Permalink
function: register context for duration of FunctionCall (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Figueiredo authored Apr 16, 2021
1 parent 4c18ca5 commit 571ffb0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (fn *Function) Call(args ...Valuer) (*Value, error) {
}
argptr = (*C.ValuePtr)(unsafe.Pointer(&cArgs[0]))
}
fn.ctx.register()
rtn := C.FunctionCall(fn.ptr, C.int(len(args)), argptr)
fn.ctx.deregister()
return getValue(fn.ctx, rtn), getError(rtn)
}
31 changes: 31 additions & 0 deletions function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@ func TestFunctionCall(t *testing.T) {
}
}

func TestFunctionCallToGoFunc(t *testing.T) {
t.Parallel()

iso, _ := v8go.NewIsolate()
global, _ := v8go.NewObjectTemplate(iso)

called := false
printfn, _ := v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
called = true
return nil
})

global.Set("print", printfn, v8go.ReadOnly)

ctx, err := v8go.NewContext(iso, global)
failIf(t, err)
val, err := ctx.RunScript(`(a, b) => { print("foo"); }`, "")
failIf(t, err)
fn, err := val.AsFunction()
failIf(t, err)
resultValue, err := fn.Call()
failIf(t, err)

if !called {
t.Errorf("expected my function to be called, wasn't")
}
if !resultValue.IsUndefined() {
t.Errorf("expected undefined, got: %v", resultValue.DetailString())
}
}

func TestFunctionCallError(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 571ffb0

Please sign in to comment.