1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 funcs = make(map[uint32]func(Value, []Value) any)
17 // Func is a wrapped Go function to be called by JavaScript.
19 Value // the JavaScript function that invokes the Go function
23 // FuncOf returns a function to be used by JavaScript.
25 // The Go function fn is called with the value of JavaScript's "this" keyword and the
26 // arguments of the invocation. The return value of the invocation is
27 // the result of the Go function mapped back to JavaScript according to ValueOf.
29 // Invoking the wrapped Go function from JavaScript will
30 // pause the event loop and spawn a new goroutine.
31 // Other wrapped functions which are triggered during a call from Go to JavaScript
32 // get executed on the same goroutine.
34 // As a consequence, if one wrapped function blocks, JavaScript's event loop
35 // is blocked until that function returns. Hence, calling any async JavaScript
36 // API, which requires the event loop, like fetch (http.Client), will cause an
37 // immediate deadlock. Therefore a blocking function should explicitly start a
40 // Func.Release must be called to free up resources when the function will not be invoked any more.
41 func FuncOf(fn func(this Value, args []Value) any) Func {
49 Value: jsGo.Call("_makeFuncWrapper", id),
53 // Release frees up resources allocated for the function.
54 // The function must not be invoked after calling Release.
55 // It is allowed to call Release while the function is still running.
56 func (c Func) Release() {
62 // setEventHandler is defined in the runtime package.
63 func setEventHandler(fn func())
66 setEventHandler(handleEvent)
70 cb := jsGo.Get("_pendingEvent")
74 jsGo.Set("_pendingEvent", Null())
76 id := uint32(cb.Get("id").Int())
77 if id == 0 { // zero indicates deadlock
84 Global().Get("console").Call("error", "call to released function")
88 this := cb.Get("this")
89 argsObj := cb.Get("args")
90 args := make([]Value, argsObj.Length())
92 args[i] = argsObj.Index(i)
94 result := f(this, args)
95 cb.Set("result", result)