-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/arm64
- Loading branch information
Showing
8 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go | ||
|
||
// #include "v8go.h" | ||
import "C" | ||
import ( | ||
"unsafe" | ||
) | ||
|
||
// Function is a JavaScript function. | ||
type Function struct { | ||
*Value | ||
} | ||
|
||
// Call this JavaScript function with the given arguments. | ||
func (fn *Function) Call(args ...Valuer) (*Value, error) { | ||
var argptr *C.ValuePtr | ||
if len(args) > 0 { | ||
var cArgs = make([]C.ValuePtr, len(args)) | ||
for i, arg := range args { | ||
cArgs[i] = arg.value().ptr | ||
} | ||
argptr = (*C.ValuePtr)(unsafe.Pointer(&cArgs[0])) | ||
} | ||
rtn := C.FunctionCall(fn.ptr, C.int(len(args)), argptr) | ||
return getValue(fn.ctx, rtn), getError(rtn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"rogchap.com/v8go" | ||
) | ||
|
||
func TestFunctionCall(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx, err := v8go.NewContext() | ||
failIf(t, err) | ||
_, err = ctx.RunScript("function add(a, b) { return a + b; }", "") | ||
failIf(t, err) | ||
addValue, err := ctx.Global().Get("add") | ||
failIf(t, err) | ||
iso, _ := ctx.Isolate() | ||
|
||
arg1, err := v8go.NewValue(iso, int32(1)) | ||
failIf(t, err) | ||
|
||
fn, _ := addValue.AsFunction() | ||
resultValue, err := fn.Call(arg1, arg1) | ||
failIf(t, err) | ||
|
||
if resultValue.Int32() != 2 { | ||
t.Errorf("expected 1 + 1 = 2, got: %v", resultValue.DetailString()) | ||
} | ||
} | ||
|
||
func TestFunctionCallError(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx, err := v8go.NewContext() | ||
failIf(t, err) | ||
_, err = ctx.RunScript("function throws() { throw 'error'; }", "script.js") | ||
failIf(t, err) | ||
addValue, err := ctx.Global().Get("throws") | ||
failIf(t, err) | ||
|
||
fn, _ := addValue.AsFunction() | ||
_, err = fn.Call() | ||
if err == nil { | ||
t.Errorf("expected an error, got none") | ||
} | ||
got := *(err.(*v8go.JSError)) | ||
want := v8go.JSError{Message: "error", Location: "script.js:1:21"} | ||
if got != want { | ||
t.Errorf("want %+v, got: %+v", want, got) | ||
} | ||
} | ||
|
||
func failIf(t *testing.T, err error) { | ||
t.Helper() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters