()
| 1155 | } |
| 1156 | |
| 1157 | func 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 | |
| 1180 | func TestGoFuncError(t *testing.T) { |
| 1181 | const SCRIPT = ` |