1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-26 20:28:49 +08:00

Add embedding example & isolate synopsis testing

This commit is contained in:
Robert Krimen
2012-10-09 18:47:39 -07:00
parent a31375b07b
commit 89e6086ff6
6 changed files with 128 additions and 53 deletions

View File

@@ -41,6 +41,27 @@ Package otto is a JavaScript parser and interpreter written natively in Go.
...
}
Embedding a Go function in JavaScript:
Otto.Set("sayHello", func(call otto.FunctionCall) otto.Value {
fmt.Printf("Hello, %s.\n", call.Argument(0).String())
return otto.UndefinedValue()
})
Otto.Set("twoPlus", func(call otto.FunctionCall) otto.Value {
right, _ := call.Argument(0).ToInteger()
result, _ := otto.ToValue(2 + right)
return result
})
result, _ = Otto.Run(`
// First, say a greeting
sayHello("Xyzzy") // Hello, Xyzzy.
sayHello() // Hello, undefined
result = twoPlus(2.0) // 4
`)
## Usage
#### type FunctionCall