diff options
author | zsfelfoldi <zsfelfoldi@gmail.com> | 2015-03-20 20:22:01 +0800 |
---|---|---|
committer | zsfelfoldi <zsfelfoldi@gmail.com> | 2015-03-20 20:22:01 +0800 |
commit | 8324b683b4e557e6c5c9d572d01f933b3e074185 (patch) | |
tree | 8c6a8c34d87f808c3979875642d4ca38844ad22d /Godeps/_workspace/src/github.com/robertkrimen/otto/documentation_test.go | |
parent | 91f9f355b2e334214e38e1827c624cdcc23c5130 (diff) | |
download | go-tangerine-8324b683b4e557e6c5c9d572d01f933b3e074185.tar.gz go-tangerine-8324b683b4e557e6c5c9d572d01f933b3e074185.tar.zst go-tangerine-8324b683b4e557e6c5c9d572d01f933b3e074185.zip |
using robertkrimen/otto, godeps updated
Diffstat (limited to 'Godeps/_workspace/src/github.com/robertkrimen/otto/documentation_test.go')
-rw-r--r-- | Godeps/_workspace/src/github.com/robertkrimen/otto/documentation_test.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/github.com/robertkrimen/otto/documentation_test.go b/Godeps/_workspace/src/github.com/robertkrimen/otto/documentation_test.go new file mode 100644 index 000000000..04646117f --- /dev/null +++ b/Godeps/_workspace/src/github.com/robertkrimen/otto/documentation_test.go @@ -0,0 +1,95 @@ +package otto + +import ( + "fmt" +) + +func ExampleSynopsis() { + + vm := New() + vm.Run(` + abc = 2 + 2; + console.log("The value of abc is " + abc); // 4 + `) + + value, _ := vm.Get("abc") + { + value, _ := value.ToInteger() + fmt.Println(value) + } + + vm.Set("def", 11) + vm.Run(` + console.log("The value of def is " + def); + `) + + vm.Set("xyzzy", "Nothing happens.") + vm.Run(` + console.log(xyzzy.length); + `) + + value, _ = vm.Run("xyzzy.length") + { + value, _ := value.ToInteger() + fmt.Println(value) + } + + value, err := vm.Run("abcdefghijlmnopqrstuvwxyz.length") + fmt.Println(value) + fmt.Println(err) + + vm.Set("sayHello", func(call FunctionCall) Value { + fmt.Printf("Hello, %s.\n", call.Argument(0).String()) + return UndefinedValue() + }) + + vm.Set("twoPlus", func(call FunctionCall) Value { + right, _ := call.Argument(0).ToInteger() + result, _ := vm.ToValue(2 + right) + return result + }) + + value, _ = vm.Run(` + sayHello("Xyzzy"); + sayHello(); + + result = twoPlus(2.0); + `) + fmt.Println(value) + + // Output: + // The value of abc is 4 + // 4 + // The value of def is 11 + // 16 + // 16 + // undefined + // ReferenceError: 'abcdefghijlmnopqrstuvwxyz' is not defined + // Hello, Xyzzy. + // Hello, undefined. + // 4 +} + +func ExampleConsole() { + + vm := New() + console := map[string]interface{}{ + "log": func(call FunctionCall) Value { + fmt.Println("console.log:", formatForConsole(call.ArgumentList)) + return UndefinedValue() + }, + } + + err := vm.Set("console", console) + + value, err := vm.Run(` + console.log("Hello, World."); + `) + fmt.Println(value) + fmt.Println(err) + + // Output: + // console.log: Hello, World. + // undefined + // <nil> +} |