MCPcopy Create free account
hub / github.com/devld/go-drive / WrapVmCall

Function WrapVmCall

script/utils.go:321–345  ·  view source on GitHub ↗
(vm *VM, fn func(vm *VM, args Values) any)

Source from the content-addressed store, hash-verified

319}
320
321func WrapVmCall(vm *VM, fn func(vm *VM, args Values) any) any {
322 return func(c otto.FunctionCall) otto.Value {
323 // Resolve into a local variable instead of reassigning the captured
324 // vm: the closure is shared across all forked VMs (otto copies the
325 // runtime but not the Go closure), so writing the captured variable
326 // would race between concurrent VM runs.
327 actualVM := vm.resolveVM(c.Otto)
328 if actualVM == nil {
329 panic("detached vm")
330 }
331
332 defer func() {
333 if e := recover(); e != nil {
334 actualVM.ThrowError(e)
335 }
336 }()
337
338 ret := fn(actualVM, newValues(actualVM, c.ArgumentList))
339 ov, e := actualVM.o.ToValue(ret)
340 if e != nil {
341 actualVM.ThrowTypeError(e.Error())
342 }
343 return ov
344 }
345}
346
347func wrapVmRun(ctx context.Context, vm *VM, fn func() (otto.Value, error)) (value *Value, e error) {
348 defer func() {

Callers 1

initVarsForVmFunction · 0.85

Calls 5

newValuesFunction · 0.85
resolveVMMethod · 0.80
ThrowErrorMethod · 0.80
ThrowTypeErrorMethod · 0.80
ErrorMethod · 0.65

Tested by

no test coverage detected