├── LICENSE ├── calling-go.go ├── calling-lua.go ├── hello.go ├── hello.lua └── hello2.go /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Nils Lagerkvist 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | -------------------------------------------------------------------------------- /calling-go.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/yuin/gopher-lua" 4 | 5 | func square(L *lua.LState) int { 6 | i := L.ToInt(1) // retrieve first function argument and convert to integer 7 | ln := lua.LNumber(i * i) // make calculation and cast to LNumber 8 | L.Push(ln) // Push it to the stack 9 | return 1 // Notify that we pushed one value to the stack 10 | } 11 | 12 | func main() { 13 | L := lua.NewState() 14 | defer L.Close() 15 | 16 | L.SetGlobal("square", L.NewFunction(square)) 17 | if err := L.DoString(`print("4 * 4 = " .. square(4))`); err != nil { 18 | panic(err) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /calling-lua.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/yuin/gopher-lua" 7 | ) 8 | 9 | // luaCode is the Lua code we want to call from Go 10 | var luaCode = ` 11 | function concat(a, b) 12 | return a .. " + " .. b 13 | end 14 | ` 15 | 16 | func main() { 17 | L := lua.NewState() 18 | defer L.Close() 19 | 20 | if err := L.DoString(luaCode); err != nil { 21 | panic(err) 22 | } 23 | 24 | // Call the Lua function concat 25 | if err := L.CallByParam(lua.P{ 26 | Fn: L.GetGlobal("concat"), 27 | NRet: 1, 28 | Protect: true, 29 | }, lua.LString("Go"), lua.LString("Lua")); err != nil { 30 | panic(err) 31 | } 32 | 33 | // Get the returned value from the stack and cast it to a lua.LString 34 | if str, ok := L.Get(-1).(lua.LString); ok { 35 | fmt.Println(str) 36 | } 37 | 38 | // Pop the value from the stack 39 | L.Pop(1) 40 | } 41 | -------------------------------------------------------------------------------- /hello.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/yuin/gopher-lua" 4 | 5 | func main() { 6 | L := lua.NewState() 7 | defer L.Close() 8 | if err := L.DoString(`print("Hello World")`); err != nil { 9 | panic(err) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hello.lua: -------------------------------------------------------------------------------- 1 | print("Hello World") 2 | -------------------------------------------------------------------------------- /hello2.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/yuin/gopher-lua" 4 | 5 | func main() { 6 | L := lua.NewState() 7 | defer L.Close() 8 | if err := L.DoString(`function sayHello() print("Hello Again") end`); err != nil { 9 | panic(err) 10 | } 11 | 12 | if err := L.DoString(`sayHello()`); err != nil { 13 | panic(err) 14 | } 15 | } 16 | --------------------------------------------------------------------------------