MCPcopy Create free account
hub / github.com/dop251/goja / ExampleAssertFunction

Function ExampleAssertFunction

runtime_test.go:1157–1178  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1155}
1156
1157func ExampleAssertFunction() {
1158 vm := New()
1159 _, err := vm.RunString(`
1160 function sum(a, b) {
1161 return a+b;
1162 }
1163 `)
1164 if err != nil {
1165 panic(err)
1166 }
1167 sum, ok := AssertFunction(vm.Get("sum"))
1168 if !ok {
1169 panic("Not a function")
1170 }
1171
1172 res, err := sum(Undefined(), vm.ToValue(40), vm.ToValue(2))
1173 if err != nil {
1174 panic(err)
1175 }
1176 fmt.Println(res)
1177 // Output: 42
1178}
1179
1180func TestGoFuncError(t *testing.T) {
1181 const SCRIPT = `

Callers

nothing calls this directly

Calls 6

NewFunction · 0.85
AssertFunctionFunction · 0.85
UndefinedFunction · 0.85
RunStringMethod · 0.80
ToValueMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…