diff --git a/externalhandler.go b/externalhandler.go new file mode 100644 index 0000000..548346d --- /dev/null +++ b/externalhandler.go @@ -0,0 +1,134 @@ +package main + +import ( + "log" + "os" + "path/filepath" + + "github.com/aarzilli/golua/lua" +) + +type ExternalHandler struct { + l *lua.State +} + +func newExternalHandler() *ExternalHandler { + h := &ExternalHandler{} + h.l = lua.NewState() + h.l.OpenLibs() + + // get current executable path + exePath, err := os.Executable() + if err != nil { + log.Fatal(err) + } + + exePath = filepath.Dir(exePath) + + h.l.DoFile(exePath + `/lua/init.lua`) + + // traverse ./handler directory, find all .lua files and load them + directory := exePath + "/handlers" + files, err := os.ReadDir(directory) + if err != nil { + log.Fatal(err) + } + + for _, file := range files { + if filepath.Ext(file.Name()) == ".lua" { + h.l.DoFile(filepath.Join(directory, file.Name())) + } + } + + return h +} + +func (h *ExternalHandler) preprocessExternalChapterListURL(u string) string { + h.l.GetGlobal("PreprocessChapterListURL") + h.l.PushString(u) + h.l.Call(1, 1) + if !h.l.IsString(-1) { + return u + } + return h.l.ToString(-1) +} + +func (h *ExternalHandler) extractExternalChapterList(u string, rawPageContent []byte) (title string, chapters []*NovelChapterInfo) { + h.l.GetGlobal("ExtractChapterList") + h.l.PushString(u) + h.l.PushBytes(rawPageContent) + h.l.Call(2, 2) + if !h.l.IsString(-2) || !h.l.IsTable(-1) { + return + } + + title = h.l.ToString(-2) + h.l.PushNil() + for h.l.Next(-2) != 0 { + // 现在栈的情况:-1 => 值(表),-2 => 键,-3 => 章节表 + if h.l.IsTable(-1) { + chapter := &NovelChapterInfo{} + h.l.PushString("Index") + h.l.GetTable(-2) + if !h.l.IsNumber(-1) { + continue + } + chapter.Index = int(h.l.ToInteger(-1)) + h.l.Pop(1) + + h.l.PushString("Title") + h.l.GetTable(-2) + if !h.l.IsString(-1) { + continue + } + chapter.Title = h.l.ToString(-1) + h.l.Pop(1) + + h.l.PushString("URL") + h.l.GetTable(-2) + if !h.l.IsString(-1) { + continue + } + chapter.URL = h.l.ToString(-1) + h.l.Pop(1) + + chapters = append(chapters, chapter) + } + h.l.Pop(1) + } + + return +} + +func (h *ExternalHandler) extractExternalChapterContent(rawPageContent []byte) (c []byte) { + h.l.GetGlobal("ExtractChapterContent") + h.l.PushBytes(rawPageContent) + h.l.Call(1, 1) + if !h.l.IsString(-1) { + return + } + return []byte(h.l.ToString(-1)) +} + +func (h *ExternalHandler) canHandleExternalSite(u string) bool { + h.l.GetGlobal("FindHandler") + h.l.PushString(u) + h.l.Call(1, 1) + if !h.l.IsNumber(-1) { + return false + } + return h.l.ToBoolean(-1) +} + +func init() { + handler := newExternalHandler() + + registerNovelSiteHandler(&NovelSiteHandler{ + Title: `飘天文学`, + Urls: []string{`https://www.piaotia.com/`}, + CanHandle: handler.canHandleExternalSite, + PreprocessChapterListURL: handler.preprocessExternalChapterListURL, + ExtractChapterList: handler.extractExternalChapterList, + ExtractChapterContent: handler.extractExternalChapterContent, + }) +} diff --git a/go.mod b/go.mod index 7027085..813c5da 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/PuerkitoBio/goquery v1.8.1 + github.com/aarzilli/golua v0.0.0-20210507130708-11106aa57765 github.com/bmaupin/go-epub v1.1.0 github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-gonic/gin v1.9.1 @@ -28,3 +29,7 @@ require ( golang.org/x/text v0.14.0 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) + +replace ( + github.com/aarzilli/golua => ./golua +) diff --git a/go.sum b/go.sum index 20d9ef3..a1ed0ce 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM= github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ= +github.com/aarzilli/golua v0.0.0-20210507130708-11106aa57765 h1:N6gB4UCRBZz8twlJbMFiCKj0zX5Et2nFU/LRafT4x80= +github.com/aarzilli/golua v0.0.0-20210507130708-11106aa57765/go.mod h1:hMjfaJVSqVnxenMlsxrq3Ni+vrm9Hs64tU4M7dhUoO4= github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/bmaupin/go-epub v1.1.0 h1:XJyvvjchtUlbZ2P7eaEeB8EFw2NgVY5ycREFpmd6MKM= diff --git a/golua/.travis.yml b/golua/.travis.yml new file mode 100644 index 0000000..a80e062 --- /dev/null +++ b/golua/.travis.yml @@ -0,0 +1,14 @@ +language: go +dist: xenial +before_install: + - sudo add-apt-repository ppa:vbernat/haproxy-1.6 -y + - sudo apt-get -qq update + - sudo apt-get install -y liblua5.1-dev + - sudo apt-get install -y liblua5.2-dev + - sudo apt-get install -y liblua5.3-dev +install: true +script: + - go test -v github.com/aarzilli/golua/lua + - go test -v -tags lua52 github.com/aarzilli/golua/lua + - go test -v -tags lua53 github.com/aarzilli/golua/lua + diff --git a/golua/LICENSE b/golua/LICENSE new file mode 100644 index 0000000..12a8196 --- /dev/null +++ b/golua/LICENSE @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2010 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/golua/README.md b/golua/README.md new file mode 100644 index 0000000..f948f43 --- /dev/null +++ b/golua/README.md @@ -0,0 +1,161 @@ +Go Bindings for the lua C API +========================= + +[![Build Status](https://travis-ci.org/aarzilli/golua.svg?branch=master)](https://travis-ci.org/aarzilli/golua) + +Simplest way to install: + + # go get github.com/aarzilli/golua/lua + +You can then try to run the examples: + + $ cd golua/_example/ + $ go run basic.go + $ go run alloc.go + $ go run panic.go + $ go run userdata.go + +This library is configured using build tags. By default it will look for a library (or "shared object") called: + +* lua5.1 on Linux and macOS +* lua on Windows +* lua-5.1 on FreeBSD + +If this doesn't work `-tags luadash5.1` can be used to force `lua-5.1`, and `-tags llua` can be used to force `lua`. + +If you want to statically link to liblua.a you can do that with `-tags luaa`. Luajit can also be used by +specifying `-tags luajit`. + +The library uses lua5.1 by default but also supports lua5.2 by specifying `-tags lua52`, lua5.3 by +specifying `-tags lua53`, and lua5.4 by specifying `-tags lua54`. + +QUICK START +--------------------- + +Create a new Virtual Machine with: + +```go +L := lua.NewState() +L.OpenLibs() +defer L.Close() +``` + +Lua's Virtual Machine is stack based, you can call lua functions like this: + +```go +// push "print" function on the stack +L.GetGlobal("print") +// push the string "Hello World!" on the stack +L.PushString("Hello World!") +// call print with one argument, expecting no results +L.Call(1, 0) +``` + +Of course this isn't very useful, more useful is executing lua code from a file or from a string: + +```go +// executes a string of lua code +err := L.DoString("...") +// executes a file +err = L.DoFile(filename) +``` + +You will also probably want to publish go functions to the virtual machine, you can do it by: + +```go +func adder(L *lua.State) int { + a := L.ToInteger(1) + b := L.ToInteger(2) + L.PushInteger(a + b) + return 1 // number of return values +} + +func main() { + L := lua.NewState() + defer L.Close() + L.OpenLibs() + + L.Register("adder", adder) + L.DoString("print(adder(2, 2))") +} +``` + +ON ERROR HANDLING +--------------------- + +Lua's exceptions are incompatible with Go, golua works around this incompatibility by setting up protected execution environments in `lua.State.DoString`, `lua.State.DoFile` and lua.State.Call and turning every exception into a Go panic. + +This means that: + +1. In general you can't do any exception handling from Lua, `pcall` and `xpcall` are renamed to `unsafe_pcall` and `unsafe_xpcall`. They are only safe to be called from Lua code that never calls back to Go. Use at your own risk. + +2. The call to lua.State.Error, present in previous versions of this library, has been removed as it is nonsensical + +3. Method calls on a newly created `lua.State` happen in an unprotected environment, if Lua throws an exception as a result your program will be terminated. If this is undesirable perform your initialization like this: + +```go +func LuaStateInit(L *lua.State) int { + … initialization goes here… + return 0 +} + +… +L.PushGoFunction(LuaStateInit) +err := L.Call(0, 0) +… +``` + +ON THREADS AND COROUTINES +--------------------- + +'lua.State' is not thread safe, but the library itself is. Lua's coroutines exist but (to my knowledge) have never been tested and are likely to encounter the same problems that errors have, use at your own peril. + +ODDS AND ENDS +--------------------- + +* If you want to build against lua5.2, lua5.3, or lua5.4 use the build tags lua52, lua53, or lua54 respectively. +* Compiling from source yields only a static link library (liblua.a), you can either produce the dynamic link library on your own or use the `luaa` build tag. + +LUAJIT +--------------------- + +To link with [luajit-2.0.x](http://luajit.org/luajit.html), you can use CGO_CFLAGS and CGO_LDFLAGS environment variables + +``` +$ CGO_CFLAGS=`pkg-config luajit --cflags` +$ CGO_LDFLAGS=`pkg-config luajit --libs-only-L` +$ go get -f -u -tags luajit github.com/aarzilli/golua/lua +``` + +CONTRIBUTORS +--------------------- + +* Adam Fitzgerald (original author) +* Alessandro Arzilli +* Steve Donovan +* Harley Laue +* James Nurmi +* Ruitao +* Xushiwei +* Isaint +* hsinhoyeh +* Viktor Palmkvist +* HongZhen Peng +* Admin36 +* Pierre Neidhardt (@Ambrevar) +* HuangWei (@huangwei1024) +* Adam Saponara + +SEE ALSO +--------------------- + +- [Luar](https://github.com/stevedonovan/luar/) is a reflection layer on top of golua API providing a simplified way to publish go functions to a Lua VM. +- [lunatico](https://github.com/fiatjaf/lunatico) is a reflection layer that allows you to push and read Go values to a Lua VM without understanding the Lua stack. +- [Golua unicode](https://github.com/Ambrevar/golua) is an extension library that adds unicode support to golua and replaces lua regular expressions with re2. + +Licensing +------------- +GoLua is released under the MIT license. +Please see the LICENSE file for more information. + +Lua is Copyright (c) Lua.org, PUC-Rio. All rights reserved. diff --git a/golua/TODO b/golua/TODO new file mode 100644 index 0000000..2849395 --- /dev/null +++ b/golua/TODO @@ -0,0 +1,6 @@ +- find all calls to lua api that can result in lua_error calls and rework them (for example checkarg stuff) +- lua.go: Dump implementing lua_dump +- lua.go: Load implementing lua_load +- AtPanic slightly broken when nil is passed, if we think passing nil has value to extract the current atpanic function we should also make sure it doesn't break everything +- threads implementation is probably fucked completely should look into it +- lauxlib.go:CheckOption is not implemented \ No newline at end of file diff --git a/golua/_example/alloc.go b/golua/_example/alloc.go new file mode 100644 index 0000000..cd3b437 --- /dev/null +++ b/golua/_example/alloc.go @@ -0,0 +1,55 @@ +package main + +import "github.com/aarzilli/golua/lua" +import "unsafe" +import "fmt" + +var refHolder = map[unsafe.Pointer][]byte{} + +//a terrible allocator! +//meant to be illustrative of the mechanics, +//not usable as an actual implementation +func AllocatorF(ptr unsafe.Pointer, osize uint, nsize uint) unsafe.Pointer { + if nsize == 0 { + if _, ok := refHolder[ptr]; ok { + delete(refHolder, ptr) + } + ptr = unsafe.Pointer(nil) + } else if osize != nsize { + slice := make([]byte, nsize) + + if oldslice, ok := refHolder[ptr]; ok { + copy(slice, oldslice) + _ = oldslice + delete(refHolder, ptr) + } + + ptr = unsafe.Pointer(&(slice[0])) + refHolder[ptr] = slice + } + //fmt.Println("in allocf"); + return ptr +} + +func A2(ptr unsafe.Pointer, osize uint, nsize uint) unsafe.Pointer { + return AllocatorF(ptr, osize, nsize) +} + +func main() { + + //refHolder = make([][]byte,0,500); + + L := lua.NewStateAlloc(AllocatorF) + defer L.Close() + L.OpenLibs() + + L.SetAllocf(A2) + + for i := 0; i < 10; i++ { + L.GetGlobal("print") + L.PushString("Hello World!") + L.Call(1, 0) + } + + fmt.Println(len(refHolder)) +} diff --git a/golua/_example/basic.go b/golua/_example/basic.go new file mode 100644 index 0000000..28f766d --- /dev/null +++ b/golua/_example/basic.go @@ -0,0 +1,47 @@ +package main + +import "github.com/aarzilli/golua/lua" +import "fmt" + +func test(L *lua.State) int { + fmt.Println("hello world! from go!") + return 0 +} + +func test2(L *lua.State) int { + arg := L.CheckInteger(-1) + argfrombottom := L.CheckInteger(1) + fmt.Print("test2 arg: ") + fmt.Println(arg) + fmt.Print("from bottom: ") + fmt.Println(argfrombottom) + return 0 +} + +func main() { + L := lua.NewState() + defer L.Close() + L.OpenLibs() + + L.GetGlobal("print") + L.PushString("Hello World!") + L.Call(1, 0) + + L.PushGoFunction(test) + L.PushGoFunction(test) + L.PushGoFunction(test) + L.PushGoFunction(test) + + L.PushGoFunction(test2) + L.PushInteger(42) + L.Call(1, 0) + + L.Call(0, 0) + L.Call(0, 0) + L.Call(0, 0) + + // this will fail as we didn't register test2 function + err := L.DoString("test2(42)") + + fmt.Printf("Ciao %v\n", err) +} diff --git a/golua/_example/calls.lua b/golua/_example/calls.lua new file mode 100644 index 0000000..454109a --- /dev/null +++ b/golua/_example/calls.lua @@ -0,0 +1,19 @@ +-- The only point of this file is to generate an interesting stack trace + +function call3(n) + if n == 3 then + error("Some error") + end +end + +function call2() + for i = 1, 4, 1 do + call3(i) + end +end + +function call1() + call2() +end + +call1() diff --git a/golua/_example/dump_load.go b/golua/_example/dump_load.go new file mode 100644 index 0000000..9d8c95b --- /dev/null +++ b/golua/_example/dump_load.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + + "github.com/aarzilli/golua/lua" +) + +// dumpAndLoadTest: dump a function chunk to bytecodes, then load bytecodes and call function +func dumpAndLoadTest(L *lua.State) { + loadret := L.LoadString(`print("msg from dump_and_load_test")`) + if loadret != 0 { + panic(fmt.Sprintf("LoadString error: %v", loadret)) + } + dumpret := L.Dump() + if dumpret != 0 { + panic(fmt.Sprintf("Dump error: %v", dumpret)) + } + + isstring := L.IsString(-1) + if !isstring { + panic("stack top not a string") + } + bytecodes := L.ToBytes(-1) + loadret = L.Load(bytecodes, "chunk_from_dump_and_load_test") + if loadret != 0 { + panic(fmt.Sprintf("Load error: %v", loadret)) + } + err := L.Call(0, 0) + if err != nil { + panic(fmt.Sprintf("Call error: %v", err)) + } +} + +func main() { + L := lua.NewState() + defer L.Close() + L.OpenLibs() + + dumpAndLoadTest(L) +} diff --git a/golua/_example/error.go b/golua/_example/error.go new file mode 100644 index 0000000..5ae65d9 --- /dev/null +++ b/golua/_example/error.go @@ -0,0 +1,48 @@ +package main + +import "github.com/aarzilli/golua/lua" +import "fmt" +import "errors" +import "os" + +func testDefault(L *lua.State) { + err := L.DoString("print(\"Unknown variable\" .. x)") + fmt.Printf("Error is: %v\n", err) + if err == nil { + fmt.Printf("Error shouldn't have been nil\n") + os.Exit(1) + } +} + +func faultyfunc(L *lua.State) int { + panic(errors.New("An error")) +} + +func testRegistered(L *lua.State) { + L.Register("faultyfunc", faultyfunc) + err := L.DoString("faultyfunc()") + fmt.Printf("Error is %v\n", err) + if err == nil { + fmt.Printf("Error shouldn't have been nil\n") + os.Exit(1) + } +} + +func test2(L *lua.State) { + err := L.DoString("error(\"Some error\")") + fmt.Printf("Error is %v\n", err) + if err == nil { + fmt.Printf("Error shouldn't have been nil\n") + os.Exit(1) + } +} + +func main() { + L := lua.NewState() + defer L.Close() + L.OpenLibs() + + testDefault(L) + testRegistered(L) + test2(L) +} diff --git a/golua/_example/panic.go b/golua/_example/panic.go new file mode 100644 index 0000000..ef8b9d7 --- /dev/null +++ b/golua/_example/panic.go @@ -0,0 +1,37 @@ +package main + +import "github.com/aarzilli/golua/lua" +import "fmt" + +func test(L *lua.State) int { + fmt.Println("hello world! from go!") + return 0 +} + +func main() { + + var L *lua.State + + L = lua.NewState() + defer L.Close() + L.OpenLibs() + + currentPanicf := L.AtPanic(nil) + currentPanicf = L.AtPanic(currentPanicf) + newPanic := func(L1 *lua.State) int { + fmt.Println("I AM PANICKING!!!", currentPanicf) + if currentPanicf != nil { + return currentPanicf(L1) + } + + return 1 + } + + L.AtPanic(newPanic) + + //force a panic + L.PushNil() + L.Call(0, 0) + + fmt.Println("End") +} diff --git a/golua/_example/quickstart.go b/golua/_example/quickstart.go new file mode 100644 index 0000000..bffca02 --- /dev/null +++ b/golua/_example/quickstart.go @@ -0,0 +1,23 @@ +package main + +import "github.com/aarzilli/golua/lua" + +func adder(L *lua.State) int { + a := L.ToInteger(1) + b := L.ToInteger(2) + L.PushInteger(int64(a + b)) + return 1 +} + +func main() { + L := lua.NewState() + defer L.Close() + L.OpenLibs() + + L.GetGlobal("print") + L.PushString("Hello World!") + L.Call(1, 0) + + L.Register("adder", adder) + L.DoString("print(adder(2, 2))") +} diff --git a/golua/_example/userdata.go b/golua/_example/userdata.go new file mode 100644 index 0000000..5cd56b3 --- /dev/null +++ b/golua/_example/userdata.go @@ -0,0 +1,87 @@ +package main + +import "github.com/aarzilli/golua/lua" +import "unsafe" +import "fmt" + +type Userdata struct { + a, b int +} + +func userDataProper(L *lua.State) { + rawptr := L.NewUserdata(uintptr(unsafe.Sizeof(Userdata{}))) + var ptr *Userdata + ptr = (*Userdata)(rawptr) + ptr.a = 2 + ptr.b = 3 + + fmt.Println(ptr) + + rawptr2 := L.ToUserdata(-1) + ptr2 := (*Userdata)(rawptr2) + + fmt.Println(ptr2) +} + +func example_function(L *lua.State) int { + fmt.Println("Heeeeelllllooooooooooo nuuurse!!!!") + return 0 +} + +func goDefinedFunctions(L *lua.State) { + /* example_function is registered inside Lua VM */ + L.Register("example_function", example_function) + + /* This code demonstrates checking that a value on the stack is a go function */ + L.CheckStack(1) + L.GetGlobal("example_function") + if !L.IsGoFunction(-1) { + panic("Not a go function") + } + L.Pop(1) + + /* We call example_function from inside Lua VM */ + L.MustDoString("example_function()") +} + +type TestObject struct { + AField int +} + +func goDefinedObjects(L *lua.State) { + t := &TestObject{42} + + L.PushGoStruct(t) + L.SetGlobal("t") + + /* This code demonstrates checking that a value on the stack is a go object */ + L.CheckStack(1) + L.GetGlobal("t") + if !L.IsGoStruct(-1) { + panic("Not a go struct") + } + L.Pop(1) + + /* This code demonstrates access and assignment to a field of a go object */ + L.MustDoString("print('AField of t is: ' .. t.AField .. ' before assignment');") + L.MustDoString("t.AField = 10;") + L.MustDoString("print('AField of t is: ' .. t.AField .. ' after assignment');") +} + +func main() { + L := lua.NewState() + defer L.Close() + L.OpenLibs() + + /* + This function stores a go object inside Lua VM + */ + userDataProper(L) + + /* + This function demonstrates exposing a function implemented in go to interpreted Lua code + */ + goDefinedFunctions(L) + + goDefinedObjects(L) +} diff --git a/golua/go.mod b/golua/go.mod new file mode 100644 index 0000000..9a21e99 --- /dev/null +++ b/golua/go.mod @@ -0,0 +1,3 @@ +module github.com/aarzilli/golua + +go 1.15 diff --git a/golua/lua/c-golua.c b/golua/lua/c-golua.c new file mode 100644 index 0000000..79342af --- /dev/null +++ b/golua/lua/c-golua.c @@ -0,0 +1,354 @@ +#include +#include +#include +#include +#include +#include "_cgo_export.h" + +#define MT_GOFUNCTION "GoLua.GoFunction" +#define MT_GOINTERFACE "GoLua.GoInterface" + +#define GOLUA_DEFAULT_MSGHANDLER "golua_default_msghandler" + +static const char GoStateRegistryKey = 'k'; //golua registry key +static const char PanicFIDRegistryKey = 'k'; + + +/* taken from lua5.2 source */ +void *testudata(lua_State *L, int ud, const char *tname) +{ + void *p = lua_touserdata(L, ud); + if (p != NULL) + { /* value is a userdata? */ + if (lua_getmetatable(L, ud)) + { /* does it have a metatable? */ + luaL_getmetatable(L, tname); /* get correct metatable */ + if (!lua_rawequal(L, -1, -2)) /* not the same? */ + p = NULL; /* value is a userdata with wrong metatable */ + lua_pop(L, 2); /* remove both metatables */ + return p; + } + } + return NULL; /* value is not a userdata with a metatable */ +} + +int clua_isgofunction(lua_State *L, int n) +{ + return testudata(L, n, MT_GOFUNCTION) != NULL; +} + +int clua_isgostruct(lua_State *L, int n) +{ + return testudata(L, n, MT_GOINTERFACE) != NULL; +} + +unsigned int* clua_checkgosomething(lua_State* L, int index, const char *desired_metatable) +{ + if (desired_metatable != NULL) + { + return testudata(L, index, desired_metatable); + } + else + { + unsigned int *sid = testudata(L, index, MT_GOFUNCTION); + if (sid != NULL) return sid; + return testudata(L, index, MT_GOINTERFACE); + } +} + +size_t clua_getgostate(lua_State* L) +{ + size_t gostateindex; + //get gostate from registry entry + lua_pushlightuserdata(L,(void*)&GoStateRegistryKey); + lua_gettable(L, LUA_REGISTRYINDEX); + gostateindex = (size_t)lua_touserdata(L,-1); + lua_pop(L,1); + return gostateindex; +} + + +//wrapper for callgofunction +int callback_function(lua_State* L) +{ + int r; + unsigned int *fid = clua_checkgosomething(L, 1, MT_GOFUNCTION); + size_t gostateindex = clua_getgostate(L); + //remove the go function from the stack (to present same behavior as lua_CFunctions) + lua_remove(L,1); + return golua_callgofunction(gostateindex, fid!=NULL ? *fid : -1); +} + +//wrapper for gchook +int gchook_wrapper(lua_State* L) +{ + //printf("Garbage collection wrapper\n"); + unsigned int* fid = clua_checkgosomething(L, -1, NULL); + size_t gostateindex = clua_getgostate(L); + if (fid != NULL) + return golua_gchook(gostateindex,*fid); + return 0; +} + +unsigned int clua_togofunction(lua_State* L, int index) +{ + unsigned int *r = clua_checkgosomething(L, index, MT_GOFUNCTION); + return (r != NULL) ? *r : -1; +} + +unsigned int clua_togostruct(lua_State *L, int index) +{ + unsigned int *r = clua_checkgosomething(L, index, MT_GOINTERFACE); + return (r != NULL) ? *r : -1; +} + +void clua_pushgofunction(lua_State* L, unsigned int fid) +{ + unsigned int* fidptr = (unsigned int *)lua_newuserdata(L, sizeof(unsigned int)); + *fidptr = fid; + luaL_getmetatable(L, MT_GOFUNCTION); + lua_setmetatable(L, -2); +} + +static int callback_c (lua_State* L) +{ + int fid = clua_togofunction(L,lua_upvalueindex(1)); + size_t gostateindex = clua_getgostate(L); + return golua_callgofunction(gostateindex,fid); +} + +void clua_pushcallback(lua_State* L) +{ + lua_pushcclosure(L,callback_c,1); +} + +void clua_pushgostruct(lua_State* L, unsigned int iid) +{ + unsigned int* iidptr = (unsigned int *)lua_newuserdata(L, sizeof(unsigned int)); + *iidptr = iid; + luaL_getmetatable(L, MT_GOINTERFACE); + lua_setmetatable(L,-2); +} + +int default_panicf(lua_State *L) +{ + const char *s = lua_tostring(L, -1); + printf("Lua unprotected panic: %s\n", s); + abort(); +} + +void clua_setgostate(lua_State* L, size_t gostateindex) +{ + lua_atpanic(L, default_panicf); + lua_pushlightuserdata(L,(void*)&GoStateRegistryKey); + lua_pushlightuserdata(L, (void*)gostateindex); + //set into registry table + lua_settable(L, LUA_REGISTRYINDEX); +} + +/* called when lua code attempts to access a field of a published go object */ +int interface_index_callback(lua_State *L) +{ + unsigned int *iid = clua_checkgosomething(L, 1, MT_GOINTERFACE); + if (iid == NULL) + { + lua_pushnil(L); + return 1; + } + + char *field_name = (char *)lua_tostring(L, 2); + if (field_name == NULL) + { + lua_pushnil(L); + return 1; + } + + size_t gostateindex = clua_getgostate(L); + + int r = golua_interface_index_callback(gostateindex, *iid, field_name); + + if (r < 0) + { + lua_error(L); + return 0; + } + else + { + return r; + } +} + +/* called when lua code attempts to set a field of a published go object */ +int interface_newindex_callback(lua_State *L) +{ + unsigned int *iid = clua_checkgosomething(L, 1, MT_GOINTERFACE); + if (iid == NULL) + { + lua_pushnil(L); + return 1; + } + + char *field_name = (char *)lua_tostring(L, 2); + if (field_name == NULL) + { + lua_pushnil(L); + return 1; + } + + size_t gostateindex = clua_getgostate(L); + + int r = golua_interface_newindex_callback(gostateindex, *iid, field_name); + + if (r < 0) + { + lua_error(L); + return 0; + } + else + { + return r; + } +} + +int panic_msghandler(lua_State *L) +{ + size_t gostateindex = clua_getgostate(L); + go_panic_msghandler(gostateindex, (char *)lua_tolstring(L, -1, NULL)); + return 0; +} + +void clua_hide_pcall(lua_State *L) +{ + lua_getglobal(L, "pcall"); + lua_setglobal(L, "unsafe_pcall"); + lua_pushnil(L); + lua_setglobal(L, "pcall"); + + lua_getglobal(L, "xpcall"); + lua_setglobal(L, "unsafe_xpcall"); + lua_pushnil(L); + lua_setglobal(L, "xpcall"); +} + +void clua_initstate(lua_State* L) +{ + /* create the GoLua.GoFunction metatable */ + luaL_newmetatable(L, MT_GOFUNCTION); + + // gofunction_metatable[__call] = &callback_function + lua_pushliteral(L,"__call"); + lua_pushcfunction(L,&callback_function); + lua_settable(L,-3); + + // gofunction_metatable[__gc] = &gchook_wrapper + lua_pushliteral(L,"__gc"); + lua_pushcfunction(L,&gchook_wrapper); + lua_settable(L,-3); + lua_pop(L,1); + + luaL_newmetatable(L, MT_GOINTERFACE); + + // gointerface_metatable[__gc] = &gchook_wrapper + lua_pushliteral(L, "__gc"); + lua_pushcfunction(L, &gchook_wrapper); + lua_settable(L, -3); + + // gointerface_metatable[__index] = &interface_index_callback + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, &interface_index_callback); + lua_settable(L, -3); + + // gointerface_metatable[__newindex] = &interface_newindex_callback + lua_pushliteral(L, "__newindex"); + lua_pushcfunction(L, &interface_newindex_callback); + lua_settable(L, -3); + + lua_register(L, GOLUA_DEFAULT_MSGHANDLER, &panic_msghandler); + lua_pop(L, 1); +} + + +int callback_panicf(lua_State* L) +{ + lua_pushlightuserdata(L,(void*)&PanicFIDRegistryKey); + lua_gettable(L,LUA_REGISTRYINDEX); + unsigned int fid = lua_tointeger(L,-1); + lua_pop(L,1); + size_t gostateindex = clua_getgostate(L); + return golua_callpanicfunction(gostateindex,fid); + +} + +//TODO: currently setting garbage when panicf set to null +GoInterface clua_atpanic(lua_State* L, unsigned int panicf_id) +{ + //get old panicfid + unsigned int old_id; + lua_pushlightuserdata(L, (void*)&PanicFIDRegistryKey); + lua_gettable(L,LUA_REGISTRYINDEX); + if(lua_isnil(L, -1) == 0) + old_id = lua_tointeger(L,-1); + lua_pop(L, 1); + + //set registry key for function id of go panic function + lua_pushlightuserdata(L, (void*)&PanicFIDRegistryKey); + //push id value + lua_pushinteger(L, panicf_id); + //set into registry table + lua_settable(L, LUA_REGISTRYINDEX); + + //now set the panic function + lua_CFunction pf = lua_atpanic(L,&callback_panicf); + //make a GoInterface with a wrapped C panicf or the original go panicf + if(pf == &callback_panicf) + { + return golua_idtointerface(old_id); + } + else + { + //TODO: technically UB, function ptr -> non function ptr + return golua_cfunctiontointerface((GoUintptr *)pf); + } +} + +int clua_callluacfunc(lua_State* L, lua_CFunction f) +{ + return f(L); +} + +void* allocwrapper(void* ud, void *ptr, size_t osize, size_t nsize) +{ + return (void*)golua_callallocf((GoUintptr)ud,(GoUintptr)ptr,osize,nsize); +} + +lua_State* clua_newstate(void* goallocf) +{ + return lua_newstate(&allocwrapper,goallocf); +} + +void clua_setallocf(lua_State* L, void* goallocf) +{ + lua_setallocf(L,&allocwrapper,goallocf); +} + +void clua_openbase(lua_State* L) +{ + lua_pushcfunction(L,&luaopen_base); + lua_pushstring(L,""); + lua_call(L, 1, 0); + clua_hide_pcall(L); +} + +void clua_hook_function(lua_State *L, lua_Debug *ar) +{ + lua_checkstack(L, 2); + size_t gostateindex = clua_getgostate(L); + golua_callgohook(gostateindex); +} + +void clua_sethook(lua_State* L, int n) +{ + lua_sethook(L, &clua_hook_function, LUA_MASKCOUNT, n); +} + + diff --git a/golua/lua/dummy.go b/golua/lua/dummy.go new file mode 100644 index 0000000..491fbb8 --- /dev/null +++ b/golua/lua/dummy.go @@ -0,0 +1,21 @@ +// +build dummy + +// This file is part of a workaround for `go mod vendor` which won't +// vendor C files if there are no Go files in the same directory. +// This prevents the C header files in lua/ from being vendored. +// +// This Go file imports the lua package where there is another +// dummy.go file which is the second part of this workaround. +// +// These two files combined make it so `go mod vendor` behaves correctly. +// +// See this issue for reference: https://github.com/golang/go/issues/26366 + +package lua + +import ( + _ "github.com/aarzilli/golua/lua/lua51" + _ "github.com/aarzilli/golua/lua/lua52" + _ "github.com/aarzilli/golua/lua/lua53" + _ "github.com/aarzilli/golua/lua/lua54" +) diff --git a/golua/lua/golua.go b/golua/lua/golua.go new file mode 100644 index 0000000..ac59452 --- /dev/null +++ b/golua/lua/golua.go @@ -0,0 +1,292 @@ +package lua + +/* +#cgo !lua52,!lua53,!lua54 CFLAGS: -I ${SRCDIR}/lua51 +#cgo lua52 CFLAGS: -I ${SRCDIR}/lua52 +#cgo lua53 CFLAGS: -I ${SRCDIR}/lua53 +#cgo lua54 CFLAGS: -I ${SRCDIR}/lua54 + +#include +#include +#include +*/ +import "C" + +import ( + "reflect" + "sync" + "unsafe" +) + +// Type of allocation functions to use with NewStateAlloc +type Alloc func(ptr unsafe.Pointer, osize uint, nsize uint) unsafe.Pointer + +// This is the type of go function that can be registered as lua functions +type LuaGoFunction func(L *State) int + +// This is the type of a go function that can be used as a lua_Hook +type HookFunction func(L *State) + +// The errorstring used by State.SetExecutionLimit +const ExecutionQuantumExceeded = "Lua execution quantum exceeded" + +// Wrapper to keep cgo from complaining about incomplete ptr type +//export State +type State struct { + // Wrapped lua_State object + s *C.lua_State + + // index of this object inside the goStates array + Index uintptr + + // Registry of go object that have been pushed to Lua VM + registry []interface{} + + // Freelist for funcs indices, to allow for freeing + freeIndices []uint + + // User self defined memory alloc func for the lua State + allocfn *Alloc + + // User defined hook function + hookFn HookFunction +} + +var goStates map[uintptr]*State +var goStatesMutex sync.Mutex + +func init() { + goStates = make(map[uintptr]*State, 16) +} + +func registerGoState(L *State) { + goStatesMutex.Lock() + defer goStatesMutex.Unlock() + L.Index = uintptr(unsafe.Pointer(L)) + goStates[L.Index] = L +} + +func unregisterGoState(L *State) { + goStatesMutex.Lock() + defer goStatesMutex.Unlock() + delete(goStates, L.Index) +} + +func getGoState(gostateindex uintptr) *State { + goStatesMutex.Lock() + defer goStatesMutex.Unlock() + return goStates[gostateindex] +} + +//export golua_callgofunction +func golua_callgofunction(gostateindex uintptr, fid uint) int { + L1 := getGoState(gostateindex) + if fid < 0 { + panic(&LuaError{0, "Requested execution of an unknown function", L1.StackTrace()}) + } + f := L1.registry[fid].(LuaGoFunction) + return f(L1) +} + +//export golua_callgohook +func golua_callgohook(gostateindex uintptr) { + L1 := getGoState(gostateindex) + if L1.hookFn != nil { + L1.hookFn(L1) + } +} + +var typeOfBytes = reflect.TypeOf([]byte(nil)) + +//export golua_interface_newindex_callback +func golua_interface_newindex_callback(gostateindex uintptr, iid uint, field_name_cstr *C.char) int { + L := getGoState(gostateindex) + iface := L.registry[iid] + ifacevalue := reflect.ValueOf(iface).Elem() + + field_name := C.GoString(field_name_cstr) + + fval := ifacevalue.FieldByName(field_name) + + if fval.Kind() == reflect.Ptr { + fval = fval.Elem() + } + + luatype := LuaValType(C.lua_type(L.s, 3)) + + switch fval.Kind() { + case reflect.Bool: + if luatype == LUA_TBOOLEAN { + fval.SetBool(int(C.lua_toboolean(L.s, 3)) != 0) + return 1 + } else { + L.PushString("Wrong assignment to field " + field_name) + return -1 + } + + case reflect.Int: + fallthrough + case reflect.Int8: + fallthrough + case reflect.Int16: + fallthrough + case reflect.Int32: + fallthrough + case reflect.Int64: + if luatype == LUA_TNUMBER { + fval.SetInt(int64(luaToInteger(L.s, 3))) + return 1 + } else { + L.PushString("Wrong assignment to field " + field_name) + return -1 + } + + case reflect.Uint: + fallthrough + case reflect.Uint8: + fallthrough + case reflect.Uint16: + fallthrough + case reflect.Uint32: + fallthrough + case reflect.Uint64: + if luatype == LUA_TNUMBER { + fval.SetUint(uint64(luaToInteger(L.s, 3))) + return 1 + } else { + L.PushString("Wrong assignment to field " + field_name) + return -1 + } + + case reflect.String: + if luatype == LUA_TSTRING { + fval.SetString(C.GoString(C.lua_tolstring(L.s, 3, nil))) + return 1 + } else { + L.PushString("Wrong assignment to field " + field_name) + return -1 + } + + case reflect.Float32: + fallthrough + case reflect.Float64: + if luatype == LUA_TNUMBER { + fval.SetFloat(float64(luaToNumber(L.s, 3))) + return 1 + } else { + L.PushString("Wrong assignment to field " + field_name) + return -1 + } + case reflect.Slice: + if fval.Type() == typeOfBytes { + if luatype == LUA_TSTRING { + fval.SetBytes(L.ToBytes(3)) + return 1 + } else { + L.PushString("Wrong assignment to field " + field_name) + return -1 + } + } + } + + L.PushString("Unsupported type of field " + field_name + ": " + fval.Type().String()) + return -1 +} + +//export golua_interface_index_callback +func golua_interface_index_callback(gostateindex uintptr, iid uint, field_name *C.char) int { + L := getGoState(gostateindex) + iface := L.registry[iid] + ifacevalue := reflect.ValueOf(iface).Elem() + + fval := ifacevalue.FieldByName(C.GoString(field_name)) + + if fval.Kind() == reflect.Ptr { + fval = fval.Elem() + } + + switch fval.Kind() { + case reflect.Bool: + L.PushBoolean(fval.Bool()) + return 1 + + case reflect.Int: + fallthrough + case reflect.Int8: + fallthrough + case reflect.Int16: + fallthrough + case reflect.Int32: + fallthrough + case reflect.Int64: + L.PushInteger(fval.Int()) + return 1 + + case reflect.Uint: + fallthrough + case reflect.Uint8: + fallthrough + case reflect.Uint16: + fallthrough + case reflect.Uint32: + fallthrough + case reflect.Uint64: + L.PushInteger(int64(fval.Uint())) + return 1 + + case reflect.String: + L.PushString(fval.String()) + return 1 + + case reflect.Float32: + fallthrough + case reflect.Float64: + L.PushNumber(fval.Float()) + return 1 + case reflect.Slice: + if fval.Type() == typeOfBytes { + L.PushBytes(fval.Bytes()) + return 1 + } + } + + L.PushString("Unsupported type of field: " + fval.Type().String()) + return -1 +} + +//export golua_gchook +func golua_gchook(gostateindex uintptr, id uint) int { + L1 := getGoState(gostateindex) + L1.unregister(id) + return 0 +} + +//export golua_callpanicfunction +func golua_callpanicfunction(gostateindex uintptr, id uint) int { + L1 := getGoState(gostateindex) + f := L1.registry[id].(LuaGoFunction) + return f(L1) +} + +//export golua_idtointerface +func golua_idtointerface(id uint) interface{} { + return id +} + +//export golua_cfunctiontointerface +func golua_cfunctiontointerface(f *uintptr) interface{} { + return f +} + +//export golua_callallocf +func golua_callallocf(fp uintptr, ptr uintptr, osize uint, nsize uint) uintptr { + return uintptr((*((*Alloc)(unsafe.Pointer(fp))))(unsafe.Pointer(ptr), osize, nsize)) +} + +//export go_panic_msghandler +func go_panic_msghandler(gostateindex uintptr, z *C.char) { + L := getGoState(gostateindex) + s := C.GoString(z) + + panic(&LuaError{LUA_ERRERR, s, L.StackTrace()}) +} diff --git a/golua/lua/golua.h b/golua/lua/golua.h new file mode 100644 index 0000000..667e1ab --- /dev/null +++ b/golua/lua/golua.h @@ -0,0 +1,36 @@ +#include + +typedef struct { void *t; void *v; } GoInterface; + +#define GOLUA_DEFAULT_MSGHANDLER "golua_default_msghandler" + +/* function to setup metatables, etc */ +void clua_initstate(lua_State* L); +void clua_hide_pcall(lua_State *L); + +unsigned int clua_togofunction(lua_State* L, int index); +unsigned int clua_togostruct(lua_State *L, int index); +void clua_pushcallback(lua_State* L); +void clua_pushgofunction(lua_State* L, unsigned int fid); +void clua_pushgostruct(lua_State *L, unsigned int fid); +void clua_setgostate(lua_State* L, size_t gostateindex); +int dump_chunk (lua_State *L); +int load_chunk(lua_State *L, const char *b, int size, const char* chunk_name); +size_t clua_getgostate(lua_State* L); +GoInterface clua_atpanic(lua_State* L, unsigned int panicf_id); +int clua_callluacfunc(lua_State* L, lua_CFunction f); +lua_State* clua_newstate(void* goallocf); +void clua_setallocf(lua_State* L, void* goallocf); + +void clua_openbase(lua_State* L); +void clua_openio(lua_State* L); +void clua_openmath(lua_State* L); +void clua_openpackage(lua_State* L); +void clua_openstring(lua_State* L); +void clua_opentable(lua_State* L); +void clua_openos(lua_State* L); +void clua_sethook(lua_State* L, int n); + +int clua_isgofunction(lua_State *L, int n); +int clua_isgostruct(lua_State *L, int n); + diff --git a/golua/lua/golua_c_lua51.go b/golua/lua/golua_c_lua51.go new file mode 100644 index 0000000..5348dc6 --- /dev/null +++ b/golua/lua/golua_c_lua51.go @@ -0,0 +1,209 @@ +//+build !lua52,!lua53,!lua54 + +package lua + +/* +#include +#include +#include +#include + +typedef struct _chunk { + int size; // chunk size + char *buffer; // chunk data + char* toread; // chunk to read +} chunk; + +static const char * reader (lua_State *L, void *ud, size_t *sz) { + chunk *ck = (chunk *)ud; + if (ck->size > LUAL_BUFFERSIZE) { + ck->size -= LUAL_BUFFERSIZE; + *sz = LUAL_BUFFERSIZE; + ck->toread = ck->buffer; + ck->buffer += LUAL_BUFFERSIZE; + }else{ + *sz = ck->size; + ck->toread = ck->buffer; + ck->size = 0; + } + return ck->toread; +} + +static int writer (lua_State *L, const void* b, size_t size, void* B) { + static int count=0; + (void)L; + luaL_addlstring((luaL_Buffer*) B, (const char *)b, size); + return 0; +} + +// load function chunk dumped from dump_chunk +int load_chunk(lua_State *L, char *b, int size, const char* chunk_name) { + chunk ck; + ck.buffer = b; + ck.size = size; + int err; + err = lua_load(L, reader, &ck, chunk_name); + if (err != 0) { + return luaL_error(L, "unable to load chunk, err: %d", err); + } + return 0; +} + +void clua_openio(lua_State* L) +{ + lua_pushcfunction(L,&luaopen_io); + lua_pushstring(L,"io"); + lua_call(L, 1, 0); +} + +void clua_openmath(lua_State* L) +{ + lua_pushcfunction(L,&luaopen_math); + lua_pushstring(L,"math"); + lua_call(L, 1, 0); +} + +void clua_openpackage(lua_State* L) +{ + lua_pushcfunction(L,&luaopen_package); + lua_pushstring(L,"package"); + lua_call(L, 1, 0); +} + +void clua_openstring(lua_State* L) +{ + lua_pushcfunction(L,&luaopen_string); + lua_pushstring(L,"string"); + lua_call(L, 1, 0); +} + +void clua_opentable(lua_State* L) +{ + lua_pushcfunction(L,&luaopen_table); + lua_pushstring(L,"table"); + lua_call(L, 1, 0); +} + +void clua_openos(lua_State* L) +{ + lua_pushcfunction(L,&luaopen_os); + lua_pushstring(L,"os"); + lua_call(L, 1, 0); +} + +// dump function chunk from luaL_loadstring +int dump_chunk (lua_State *L) { + luaL_Buffer b; + luaL_checktype(L, -1, LUA_TFUNCTION); + lua_settop(L, -1); + luaL_buffinit(L,&b); + int err; + err = lua_dump(L, writer, &b); + if (err != 0){ + return luaL_error(L, "unable to dump given function, err:%d", err); + } + luaL_pushresult(&b); + return 0; +} +*/ +import "C" + +import "unsafe" + +func luaToInteger(s *C.lua_State, n C.int) C.longlong { + return C.lua_tointeger(s, n) +} + +func luaToNumber(s *C.lua_State, n C.int) C.double { + return C.lua_tonumber(s, n) +} + +func lualLoadFile(s *C.lua_State, filename *C.char) C.int { + return C.luaL_loadfile(s, filename) +} + +// lua_equal +func (L *State) Equal(index1, index2 int) bool { + return C.lua_equal(L.s, C.int(index1), C.int(index2)) == 1 +} + +// lua_getfenv +func (L *State) GetfEnv(index int) { + C.lua_getfenv(L.s, C.int(index)) +} + +// lua_lessthan +func (L *State) LessThan(index1, index2 int) bool { + return C.lua_lessthan(L.s, C.int(index1), C.int(index2)) == 1 +} + +// lua_setfenv +func (L *State) SetfEnv(index int) { + C.lua_setfenv(L.s, C.int(index)) +} + +func (L *State) ObjLen(index int) uint { + return uint(C.lua_objlen(L.s, C.int(index))) +} + +// lua_tointeger +func (L *State) ToInteger(index int) int { + return int(C.lua_tointeger(L.s, C.int(index))) +} + +// lua_tonumber +func (L *State) ToNumber(index int) float64 { + return float64(C.lua_tonumber(L.s, C.int(index))) +} + +// lua_yield +func (L *State) Yield(nresults int) int { + return int(C.lua_yield(L.s, C.int(nresults))) +} + +func (L *State) pcall(nargs, nresults, errfunc int) int { + return int(C.lua_pcall(L.s, C.int(nargs), C.int(nresults), C.int(errfunc))) +} + +// Pushes on the stack the value of a global variable (lua_getglobal) +func (L *State) GetGlobal(name string) { L.GetField(LUA_GLOBALSINDEX, name) } + +// lua_resume +func (L *State) Resume(narg int) int { + return int(C.lua_resume(L.s, C.int(narg))) +} + +// lua_setglobal +func (L *State) SetGlobal(name string) { + Cname := C.CString(name) + defer C.free(unsafe.Pointer(Cname)) + C.lua_setfield(L.s, C.int(LUA_GLOBALSINDEX), Cname) +} + +// lua_insert +func (L *State) Insert(index int) { C.lua_insert(L.s, C.int(index)) } + +// lua_remove +func (L *State) Remove(index int) { + C.lua_remove(L.s, C.int(index)) +} + +// lua_replace +func (L *State) Replace(index int) { + C.lua_replace(L.s, C.int(index)) +} + +// lua_rawgeti +func (L *State) RawGeti(index int, n int) { + C.lua_rawgeti(L.s, C.int(index), C.int(n)) +} + +// lua_rawseti +func (L *State) RawSeti(index int, n int) { + C.lua_rawseti(L.s, C.int(index), C.int(n)) +} + +// lua_gc +func (L *State) GC(what, data int) int { + return int(C.lua_gc(L.s, C.int(what), C.int(data))) +} diff --git a/golua/lua/golua_c_lua52.go b/golua/lua/golua_c_lua52.go new file mode 100644 index 0000000..6195393 --- /dev/null +++ b/golua/lua/golua_c_lua52.go @@ -0,0 +1,230 @@ +//+build lua52 + +package lua + +/* +#include +#include +#include +#include + +typedef struct _chunk { + int size; // chunk size + char *buffer; // chunk data + char* toread; // chunk to read +} chunk; + +static const char * reader (lua_State *L, void *ud, size_t *sz) { + chunk *ck = (chunk *)ud; + if (ck->size > LUAL_BUFFERSIZE) { + ck->size -= LUAL_BUFFERSIZE; + *sz = LUAL_BUFFERSIZE; + ck->toread = ck->buffer; + ck->buffer += LUAL_BUFFERSIZE; + }else{ + *sz = ck->size; + ck->toread = ck->buffer; + ck->size = 0; + } + return ck->toread; +} + +static int writer (lua_State *L, const void* b, size_t size, void* B) { + static int count=0; + (void)L; + luaL_addlstring((luaL_Buffer*) B, (const char *)b, size); + return 0; +} + +// load function chunk dumped from dump_chunk +int load_chunk(lua_State *L, char *b, int size, const char* chunk_name) { + chunk ck; + ck.buffer = b; + ck.size = size; + int err; + err = lua_load(L, reader, &ck, chunk_name, NULL); + if (err != 0) { + return luaL_error(L, "unable to load chunk, err: %d", err); + } + return 0; +} + +void clua_openio(lua_State* L) +{ + luaL_requiref(L, "io", &luaopen_io, 1); + lua_pop(L, 1); +} + +void clua_openmath(lua_State* L) +{ + luaL_requiref(L, "math", &luaopen_math, 1); + lua_pop(L, 1); +} + +void clua_openpackage(lua_State* L) +{ + luaL_requiref(L, "package", &luaopen_package, 1); + lua_pop(L, 1); +} + +void clua_openstring(lua_State* L) +{ + luaL_requiref(L, "string", &luaopen_string, 1); + lua_pop(L, 1); +} + +void clua_opentable(lua_State* L) +{ + luaL_requiref(L, "table", &luaopen_table, 1); + lua_pop(L, 1); +} + +void clua_openos(lua_State* L) +{ + luaL_requiref(L, "os", &luaopen_os, 1); + lua_pop(L, 1); +} + +void clua_opencoroutine(lua_State *L) +{ + luaL_requiref(L, "coroutine", &luaopen_coroutine, 1); + lua_pop(L, 1); +} + +void clua_opendebug(lua_State *L) +{ + luaL_requiref(L, "debug", &luaopen_debug, 1); + lua_pop(L, 1); +} + +void clua_openbit32(lua_State *L) +{ + luaL_requiref(L, "bit32", &luaopen_bit32, 1); + lua_pop(L, 1); +} + +// dump function chunk from luaL_loadstring +int dump_chunk (lua_State *L) { + luaL_Buffer b; + luaL_checktype(L, -1, LUA_TFUNCTION); + lua_settop(L, -1); + luaL_buffinit(L,&b); + int err; + err = lua_dump(L, writer, &b); + if (err != 0){ + return luaL_error(L, "unable to dump given function, err:%d", err); + } + luaL_pushresult(&b); + return 0; +} +*/ +import "C" + +import "unsafe" + +func luaToInteger(s *C.lua_State, n C.int) C.longlong { + return C.lua_tointegerx(s, n, nil) +} + +func luaToNumber(s *C.lua_State, n C.int) C.double { + return C.lua_tonumberx(s, n, nil) +} + +func lualLoadFile(s *C.lua_State, filename *C.char) C.int { + return C.luaL_loadfilex(s, filename, nil) +} + +// lua_equal +func (L *State) Equal(index1, index2 int) bool { + return C.lua_compare(L.s, C.int(index1), C.int(index2), C.LUA_OPEQ) == 1 +} + +// lua_lessthan +func (L *State) LessThan(index1, index2 int) bool { + return C.lua_compare(L.s, C.int(index1), C.int(index2), C.LUA_OPLT) == 1 +} + +func (L *State) ObjLen(index int) uint { + return uint(C.lua_rawlen(L.s, C.int(index))) +} + +// lua_tointeger +func (L *State) ToInteger(index int) int { + return int(C.lua_tointegerx(L.s, C.int(index), nil)) +} + +// lua_tonumber +func (L *State) ToNumber(index int) float64 { + return float64(C.lua_tonumberx(L.s, C.int(index), nil)) +} + +// lua_yield +func (L *State) Yield(nresults int) int { + return int(C.lua_yieldk(L.s, C.int(nresults), 0, nil)) +} + +func (L *State) pcall(nargs, nresults, errfunc int) int { + return int(C.lua_pcallk(L.s, C.int(nargs), C.int(nresults), C.int(errfunc), 0, nil)) +} + +// Pushes on the stack the value of a global variable (lua_getglobal) +func (L *State) GetGlobal(name string) { + Ck := C.CString(name) + defer C.free(unsafe.Pointer(Ck)) + C.lua_getglobal(L.s, Ck) +} + +// lua_resume +func (L *State) Resume(narg int) int { + return int(C.lua_resume(L.s, nil, C.int(narg))) +} + +// lua_setglobal +func (L *State) SetGlobal(name string) { + Cname := C.CString(name) + defer C.free(unsafe.Pointer(Cname)) + C.lua_setglobal(L.s, Cname) +} + +// Calls luaopen_debug +func (L *State) OpenDebug() { + C.clua_opendebug(L.s) +} + +// Calls luaopen_bit32 +func (L *State) OpenBit32() { + C.clua_openbit32(L.s) +} + +// Calls luaopen_coroutine +func (L *State) OpenCoroutine() { + C.clua_opencoroutine(L.s) +} + +// lua_insert +func (L *State) Insert(index int) { C.lua_insert(L.s, C.int(index)) } + +// lua_remove +func (L *State) Remove(index int) { + C.lua_remove(L.s, C.int(index)) +} + +// lua_replace +func (L *State) Replace(index int) { + C.lua_replace(L.s, C.int(index)) +} + +// lua_rawgeti +func (L *State) RawGeti(index int, n int) { + C.lua_rawgeti(L.s, C.int(index), C.int(n)) +} + +// lua_rawseti +func (L *State) RawSeti(index int, n int) { + C.lua_rawseti(L.s, C.int(index), C.int(n)) +} + +// lua_gc +func (L *State) GC(what, data int) int { + return int(C.lua_gc(L.s, C.int(what), C.int(data))) +} diff --git a/golua/lua/golua_c_lua53.go b/golua/lua/golua_c_lua53.go new file mode 100644 index 0000000..d490b1b --- /dev/null +++ b/golua/lua/golua_c_lua53.go @@ -0,0 +1,232 @@ +//+build lua53 + +package lua + +/* +#include +#include +#include +#include + +typedef struct _chunk { + int size; // chunk size + char *buffer; // chunk data + char* toread; // chunk to read +} chunk; + +static const char * reader (lua_State *L, void *ud, size_t *sz) { + chunk *ck = (chunk *)ud; + if (ck->size > LUAL_BUFFERSIZE) { + ck->size -= LUAL_BUFFERSIZE; + *sz = LUAL_BUFFERSIZE; + ck->toread = ck->buffer; + ck->buffer += LUAL_BUFFERSIZE; + }else{ + *sz = ck->size; + ck->toread = ck->buffer; + ck->size = 0; + } + return ck->toread; +} + +static int writer (lua_State *L, const void* b, size_t size, void* B) { + static int count=0; + (void)L; + luaL_addlstring((luaL_Buffer*) B, (const char *)b, size); + return 0; +} + +// load function chunk dumped from dump_chunk +int load_chunk(lua_State *L, char *b, int size, const char* chunk_name) { + chunk ck; + ck.buffer = b; + ck.size = size; + int err; + err = lua_load(L, reader, &ck, chunk_name, NULL); + if (err != 0) { + return luaL_error(L, "unable to load chunk, err: %d", err); + } + return 0; +} + +void clua_openio(lua_State* L) +{ + luaL_requiref(L, "io", &luaopen_io, 1); + lua_pop(L, 1); +} + +void clua_openmath(lua_State* L) +{ + luaL_requiref(L, "math", &luaopen_math, 1); + lua_pop(L, 1); +} + +void clua_openpackage(lua_State* L) +{ + luaL_requiref(L, "package", &luaopen_package, 1); + lua_pop(L, 1); +} + +void clua_openstring(lua_State* L) +{ + luaL_requiref(L, "string", &luaopen_string, 1); + lua_pop(L, 1); +} + +void clua_opentable(lua_State* L) +{ + luaL_requiref(L, "table", &luaopen_table, 1); + lua_pop(L, 1); +} + +void clua_openos(lua_State* L) +{ + luaL_requiref(L, "os", &luaopen_os, 1); + lua_pop(L, 1); +} + +void clua_opencoroutine(lua_State *L) +{ + luaL_requiref(L, "coroutine", &luaopen_coroutine, 1); + lua_pop(L, 1); +} + +void clua_opendebug(lua_State *L) +{ + luaL_requiref(L, "debug", &luaopen_debug, 1); + lua_pop(L, 1); +} + +void clua_openbit32(lua_State *L) +{ + luaL_requiref(L, "bit32", &luaopen_bit32, 1); + lua_pop(L, 1); +} + +// dump function chunk from luaL_loadstring +int dump_chunk (lua_State *L) { + luaL_Buffer b; + luaL_checktype(L, -1, LUA_TFUNCTION); + lua_settop(L, -1); + luaL_buffinit(L,&b); + int err; + err = lua_dump(L, writer, &b, 0); + if (err != 0){ + return luaL_error(L, "unable to dump given function, err:%d", err); + } + luaL_pushresult(&b); + return 0; +} +*/ +import "C" + +import "unsafe" + +func luaToInteger(s *C.lua_State, n C.int) C.longlong { + return C.lua_tointegerx(s, n, nil) +} + +func luaToNumber(s *C.lua_State, n C.int) C.double { + return C.lua_tonumberx(s, n, nil) +} + +func lualLoadFile(s *C.lua_State, filename *C.char) C.int { + return C.luaL_loadfilex(s, filename, nil) +} + +// lua_equal +func (L *State) Equal(index1, index2 int) bool { + return C.lua_compare(L.s, C.int(index1), C.int(index2), C.LUA_OPEQ) == 1 +} + +// lua_lessthan +func (L *State) LessThan(index1, index2 int) bool { + return C.lua_compare(L.s, C.int(index1), C.int(index2), C.LUA_OPLT) == 1 +} + +func (L *State) ObjLen(index int) uint { + return uint(C.lua_rawlen(L.s, C.int(index))) +} + +// lua_tointeger +func (L *State) ToInteger(index int) int { + return int(C.lua_tointegerx(L.s, C.int(index), nil)) +} + +// lua_tonumber +func (L *State) ToNumber(index int) float64 { + return float64(C.lua_tonumberx(L.s, C.int(index), nil)) +} + +// lua_yield +func (L *State) Yield(nresults int) int { + return int(C.lua_yieldk(L.s, C.int(nresults), 0, nil)) +} + +func (L *State) pcall(nargs, nresults, errfunc int) int { + return int(C.lua_pcallk(L.s, C.int(nargs), C.int(nresults), C.int(errfunc), 0, nil)) +} + +// Pushes on the stack the value of a global variable (lua_getglobal) +func (L *State) GetGlobal(name string) { + Ck := C.CString(name) + defer C.free(unsafe.Pointer(Ck)) + C.lua_getglobal(L.s, Ck) +} + +// lua_resume +func (L *State) Resume(narg int) int { + return int(C.lua_resume(L.s, nil, C.int(narg))) +} + +// lua_setglobal +func (L *State) SetGlobal(name string) { + Cname := C.CString(name) + defer C.free(unsafe.Pointer(Cname)) + C.lua_setglobal(L.s, Cname) +} + +// Calls luaopen_debug +func (L *State) OpenDebug() { + C.clua_opendebug(L.s) +} + +// Calls luaopen_bit32 +func (L *State) OpenBit32() { + C.clua_openbit32(L.s) +} + +// Calls luaopen_coroutine +func (L *State) OpenCoroutine() { + C.clua_opencoroutine(L.s) +} + +// lua_insert +func (L *State) Insert(index int) { C.lua_rotate(L.s, C.int(index), 1) } + +// lua_remove +func (L *State) Remove(index int) { + C.lua_rotate(L.s, C.int(index), -1) + C.lua_settop(L.s, C.int(-2)) +} + +// lua_replace +func (L *State) Replace(index int) { + C.lua_copy(L.s, -1, C.int(index)) + C.lua_settop(L.s, -2) +} + +// lua_rawgeti +func (L *State) RawGeti(index int, n int) { + C.lua_rawgeti(L.s, C.int(index), C.longlong(n)) +} + +// lua_rawseti +func (L *State) RawSeti(index int, n int) { + C.lua_rawseti(L.s, C.int(index), C.longlong(n)) +} + +// lua_gc +func (L *State) GC(what, data int) int { + return int(C.lua_gc(L.s, C.int(what), C.int(data))) +} diff --git a/golua/lua/golua_c_lua54.go b/golua/lua/golua_c_lua54.go new file mode 100644 index 0000000..9acafe5 --- /dev/null +++ b/golua/lua/golua_c_lua54.go @@ -0,0 +1,229 @@ +//+build lua54 + +package lua + +/* +#include +#include +#include +#include + +typedef struct _chunk { + int size; // chunk size + char *buffer; // chunk data + char* toread; // chunk to read +} chunk; + +LUA_API void *lua_newuserdata (lua_State *L, size_t size) { + return lua_newuserdatauv(L, size, 1); +} + +LUA_API int (lua_gc_compat) (lua_State *L, int what, int data) { + return lua_gc(L, what, data); +} + +static const char * reader (lua_State *L, void *ud, size_t *sz) { + chunk *ck = (chunk *)ud; + if (ck->size > LUAL_BUFFERSIZE) { + ck->size -= LUAL_BUFFERSIZE; + *sz = LUAL_BUFFERSIZE; + ck->toread = ck->buffer; + ck->buffer += LUAL_BUFFERSIZE; + }else{ + *sz = ck->size; + ck->toread = ck->buffer; + ck->size = 0; + } + return ck->toread; +} + +static int writer (lua_State *L, const void* b, size_t size, void* B) { + static int count=0; + (void)L; + luaL_addlstring((luaL_Buffer*) B, (const char *)b, size); + return 0; +} + +// load function chunk dumped from dump_chunk +int load_chunk(lua_State *L, char *b, int size, const char* chunk_name) { + chunk ck; + ck.buffer = b; + ck.size = size; + int err; + err = lua_load(L, reader, &ck, chunk_name, NULL); + if (err != 0) { + return luaL_error(L, "unable to load chunk, err: %d", err); + } + return 0; +} + +void clua_openio(lua_State* L) +{ + luaL_requiref(L, "io", &luaopen_io, 1); + lua_pop(L, 1); +} + +void clua_openmath(lua_State* L) +{ + luaL_requiref(L, "math", &luaopen_math, 1); + lua_pop(L, 1); +} + +void clua_openpackage(lua_State* L) +{ + luaL_requiref(L, "package", &luaopen_package, 1); + lua_pop(L, 1); +} + +void clua_openstring(lua_State* L) +{ + luaL_requiref(L, "string", &luaopen_string, 1); + lua_pop(L, 1); +} + +void clua_opentable(lua_State* L) +{ + luaL_requiref(L, "table", &luaopen_table, 1); + lua_pop(L, 1); +} + +void clua_openos(lua_State* L) +{ + luaL_requiref(L, "os", &luaopen_os, 1); + lua_pop(L, 1); +} + +void clua_opencoroutine(lua_State *L) +{ + luaL_requiref(L, "coroutine", &luaopen_coroutine, 1); + lua_pop(L, 1); +} + +void clua_opendebug(lua_State *L) +{ + luaL_requiref(L, "debug", &luaopen_debug, 1); + lua_pop(L, 1); +} + +// dump function chunk from luaL_loadstring +int dump_chunk (lua_State *L) { + luaL_Buffer b; + luaL_checktype(L, -1, LUA_TFUNCTION); + lua_settop(L, -1); + luaL_buffinit(L,&b); + int err; + err = lua_dump(L, writer, &b, 0); + if (err != 0){ + return luaL_error(L, "unable to dump given function, err:%d", err); + } + luaL_pushresult(&b); + return 0; +} +*/ +import "C" + +import "unsafe" + +func luaToInteger(s *C.lua_State, n C.int) C.longlong { + return C.lua_tointegerx(s, n, nil) +} + +func luaToNumber(s *C.lua_State, n C.int) C.double { + return C.lua_tonumberx(s, n, nil) +} + +func lualLoadFile(s *C.lua_State, filename *C.char) C.int { + return C.luaL_loadfilex(s, filename, nil) +} + +// lua_equal +func (L *State) Equal(index1, index2 int) bool { + return C.lua_compare(L.s, C.int(index1), C.int(index2), C.LUA_OPEQ) == 1 +} + +// lua_lessthan +func (L *State) LessThan(index1, index2 int) bool { + return C.lua_compare(L.s, C.int(index1), C.int(index2), C.LUA_OPLT) == 1 +} + +func (L *State) ObjLen(index int) uint { + return uint(C.lua_rawlen(L.s, C.int(index))) +} + +// lua_tointeger +func (L *State) ToInteger(index int) int { + return int(C.lua_tointegerx(L.s, C.int(index), nil)) +} + +// lua_tonumber +func (L *State) ToNumber(index int) float64 { + return float64(C.lua_tonumberx(L.s, C.int(index), nil)) +} + +// lua_yield +func (L *State) Yield(nresults int) int { + return int(C.lua_yieldk(L.s, C.int(nresults), 0, nil)) +} + +func (L *State) pcall(nargs, nresults, errfunc int) int { + return int(C.lua_pcallk(L.s, C.int(nargs), C.int(nresults), C.int(errfunc), 0, nil)) +} + +// Pushes on the stack the value of a global variable (lua_getglobal) +func (L *State) GetGlobal(name string) { + Ck := C.CString(name) + defer C.free(unsafe.Pointer(Ck)) + C.lua_getglobal(L.s, Ck) +} + +// lua_resume +func (L *State) Resume(narg int) int { + return int(C.lua_resume(L.s, nil, C.int(narg), nil)) +} + +// lua_setglobal +func (L *State) SetGlobal(name string) { + Cname := C.CString(name) + defer C.free(unsafe.Pointer(Cname)) + C.lua_setglobal(L.s, Cname) +} + +// Calls luaopen_debug +func (L *State) OpenDebug() { + C.clua_opendebug(L.s) +} + +// Calls luaopen_coroutine +func (L *State) OpenCoroutine() { + C.clua_opencoroutine(L.s) +} + +// lua_insert +func (L *State) Insert(index int) { C.lua_rotate(L.s, C.int(index), 1) } + +// lua_remove +func (L *State) Remove(index int) { + C.lua_rotate(L.s, C.int(index), -1) + C.lua_settop(L.s, C.int(-2)) +} + +// lua_replace +func (L *State) Replace(index int) { + C.lua_copy(L.s, -1, C.int(index)) + C.lua_settop(L.s, -2) +} + +// lua_rawgeti +func (L *State) RawGeti(index int, n int) { + C.lua_rawgeti(L.s, C.int(index), C.longlong(n)) +} + +// lua_rawseti +func (L *State) RawSeti(index int, n int) { + C.lua_rawseti(L.s, C.int(index), C.longlong(n)) +} + +// lua_gc +func (L *State) GC(what, data int) int { + return int(C.lua_gc_compat(L.s, C.int(what), C.int(data))) +} diff --git a/golua/lua/lauxlib.go b/golua/lua/lauxlib.go new file mode 100644 index 0000000..343da92 --- /dev/null +++ b/golua/lua/lauxlib.go @@ -0,0 +1,237 @@ +package lua + +//#include +//#include +//#include +//#include +//#include "golua.h" +import "C" +import "unsafe" + +type LuaError struct { + code int + message string + stackTrace []LuaStackEntry +} + +func (err *LuaError) Error() string { + return err.message +} + +func (err *LuaError) Code() int { + return err.code +} + +func (err *LuaError) StackTrace() []LuaStackEntry { + return err.stackTrace +} + +// luaL_argcheck +// WARNING: before b30b2c62c6712c6683a9d22ff0abfa54c8267863 the function ArgCheck had the opposite behaviour +func (L *State) Argcheck(cond bool, narg int, extramsg string) { + if !cond { + Cextramsg := C.CString(extramsg) + defer C.free(unsafe.Pointer(Cextramsg)) + C.luaL_argerror(L.s, C.int(narg), Cextramsg) + } +} + +// luaL_argerror +func (L *State) ArgError(narg int, extramsg string) int { + Cextramsg := C.CString(extramsg) + defer C.free(unsafe.Pointer(Cextramsg)) + return int(C.luaL_argerror(L.s, C.int(narg), Cextramsg)) +} + +// luaL_callmeta +func (L *State) CallMeta(obj int, e string) int { + Ce := C.CString(e) + defer C.free(unsafe.Pointer(Ce)) + return int(C.luaL_callmeta(L.s, C.int(obj), Ce)) +} + +// luaL_checkany +func (L *State) CheckAny(narg int) { + C.luaL_checkany(L.s, C.int(narg)) +} + +// luaL_checkinteger +func (L *State) CheckInteger(narg int) int { + return int(C.luaL_checkinteger(L.s, C.int(narg))) +} + +// luaL_checknumber +func (L *State) CheckNumber(narg int) float64 { + return float64(C.luaL_checknumber(L.s, C.int(narg))) +} + +// luaL_checkstring +func (L *State) CheckString(narg int) string { + var length C.size_t + return C.GoString(C.luaL_checklstring(L.s, C.int(narg), &length)) +} + +// luaL_checkoption +// +// BUG(everyone_involved): not implemented +func (L *State) CheckOption(narg int, def string, lst []string) int { + //TODO: complication: lst conversion to const char* lst[] from string slice + return 0 +} + +// luaL_checktype +func (L *State) CheckType(narg int, t LuaValType) { + C.luaL_checktype(L.s, C.int(narg), C.int(t)) +} + +// luaL_checkudata +func (L *State) CheckUdata(narg int, tname string) unsafe.Pointer { + Ctname := C.CString(tname) + defer C.free(unsafe.Pointer(Ctname)) + return unsafe.Pointer(C.luaL_checkudata(L.s, C.int(narg), Ctname)) +} + +// Executes file, returns nil for no errors or the lua error string on failure +func (L *State) DoFile(filename string) error { + if r := L.LoadFile(filename); r != 0 { + return &LuaError{r, L.ToString(-1), L.StackTrace()} + } + return L.Call(0, LUA_MULTRET) +} + +// Executes the string, returns nil for no errors or the lua error string on failure +func (L *State) DoString(str string) error { + if r := L.LoadString(str); r != 0 { + return &LuaError{r, L.ToString(-1), L.StackTrace()} + } + return L.Call(0, LUA_MULTRET) +} + +// Like DoString but panics on error +func (L *State) MustDoString(str string) { + if err := L.DoString(str); err != nil { + panic(err) + } +} + +// luaL_getmetafield +func (L *State) GetMetaField(obj int, e string) bool { + Ce := C.CString(e) + defer C.free(unsafe.Pointer(Ce)) + return C.luaL_getmetafield(L.s, C.int(obj), Ce) != 0 +} + +// luaL_getmetatable +func (L *State) LGetMetaTable(tname string) { + Ctname := C.CString(tname) + defer C.free(unsafe.Pointer(Ctname)) + C.lua_getfield(L.s, LUA_REGISTRYINDEX, Ctname) +} + +// luaL_gsub +func (L *State) GSub(s string, p string, r string) string { + Cs := C.CString(s) + Cp := C.CString(p) + Cr := C.CString(r) + defer func() { + C.free(unsafe.Pointer(Cs)) + C.free(unsafe.Pointer(Cp)) + C.free(unsafe.Pointer(Cr)) + }() + + return C.GoString(C.luaL_gsub(L.s, Cs, Cp, Cr)) +} + +// luaL_loadfile +func (L *State) LoadFile(filename string) int { + Cfilename := C.CString(filename) + defer C.free(unsafe.Pointer(Cfilename)) + return int(lualLoadFile(L.s, Cfilename)) +} + +// luaL_loadstring +func (L *State) LoadString(s string) int { + Cs := C.CString(s) + defer C.free(unsafe.Pointer(Cs)) + return int(C.luaL_loadstring(L.s, Cs)) +} + +// lua_dump +func (L *State) Dump() int { + ret := int(C.dump_chunk(L.s)) + return ret +} + +// lua_load +func (L *State) Load(bs []byte, name string) int { + chunk := C.CString(string(bs)) + ckname := C.CString(name) + defer C.free(unsafe.Pointer(chunk)) + defer C.free(unsafe.Pointer(ckname)) + ret := int(C.load_chunk(L.s, chunk, C.int(len(bs)), ckname)) + if ret != 0 { + return ret + } + return 0 +} + +// luaL_newmetatable +func (L *State) NewMetaTable(tname string) bool { + Ctname := C.CString(tname) + defer C.free(unsafe.Pointer(Ctname)) + return C.luaL_newmetatable(L.s, Ctname) != 0 +} + +// luaL_newstate +func NewState() *State { + ls := (C.luaL_newstate()) + if ls == nil { + return nil + } + L := newState(ls) + return L +} + +// luaL_openlibs +func (L *State) OpenLibs() { + C.luaL_openlibs(L.s) + C.clua_hide_pcall(L.s) +} + +// luaL_optinteger +func (L *State) OptInteger(narg int, d int) int { + return int(C.luaL_optinteger(L.s, C.int(narg), C.lua_Integer(d))) +} + +// luaL_optnumber +func (L *State) OptNumber(narg int, d float64) float64 { + return float64(C.luaL_optnumber(L.s, C.int(narg), C.lua_Number(d))) +} + +// luaL_optstring +func (L *State) OptString(narg int, d string) string { + var length C.size_t + Cd := C.CString(d) + defer C.free(unsafe.Pointer(Cd)) + return C.GoString(C.luaL_optlstring(L.s, C.int(narg), Cd, &length)) +} + +// luaL_ref +func (L *State) Ref(t int) int { + return int(C.luaL_ref(L.s, C.int(t))) +} + +// luaL_typename +func (L *State) LTypename(index int) string { + return C.GoString(C.lua_typename(L.s, C.lua_type(L.s, C.int(index)))) +} + +// luaL_unref +func (L *State) Unref(t int, ref int) { + C.luaL_unref(L.s, C.int(t), C.int(ref)) +} + +// luaL_where +func (L *State) Where(lvl int) { + C.luaL_where(L.s, C.int(lvl)) +} diff --git a/golua/lua/lua.go b/golua/lua/lua.go new file mode 100644 index 0000000..df6467a --- /dev/null +++ b/golua/lua/lua.go @@ -0,0 +1,636 @@ +// This package provides access to the excellent lua language interpreter from go code. +// +// Access to most of the functions in lua.h and lauxlib.h is provided as well as additional convenience functions to publish Go objects and functions to lua code. +// +// The documentation of this package is no substitute for the official lua documentation and in many instances methods are described only with the name of their C equivalent +package lua + +/* +#cgo !lua52,!lua53,!lua54 CFLAGS: -I ${SRCDIR}/lua51 +#cgo lua52 CFLAGS: -I ${SRCDIR}/lua52 +#cgo lua53 CFLAGS: -I ${SRCDIR}/lua53 +#cgo lua54 CFLAGS: -I ${SRCDIR}/lua54 +#cgo llua LDFLAGS: -llua + +#cgo luaa LDFLAGS: -llua -lm -ldl +#cgo luajit LDFLAGS: -lluajit-5.1 +#cgo lluadash5.1 LDFLAGS: -llua-5.1 + +#cgo linux,!lua52,!lua53,!lua54,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua5.1 +#cgo linux,lua52,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua5.2 +#cgo linux,lua53,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua5.3 +#cgo linux,lua54,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua5.4 -lm + +#cgo darwin,!lua52,!lua53,!lua54,!llua,!luaa,!luajit,!lluadash5.1 pkg-config: lua5.1 +#cgo darwin,lua52,!llua,!luaa,!luajit,!lluadash5.1 pkg-config: lua5.2 +#cgo darwin,lua53,!llua,!luaa,!luajit,!lluadash5.1 pkg-config: lua5.3 +#cgo darwin,lua54,!llua,!luaa,!luajit,!lluadash5.1 pkg-config: lua5.4 m + +#cgo freebsd,!lua52,!lua53,!lua54,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua-5.1 +#cgo freebsd,lua52,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua-5.2 +#cgo freebsd,lua53,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua-5.3 +#cgo freebsd,lua54,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua-5.4 -lm + +#cgo windows,!lua52,!lua53,!lua54,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -L${SRCDIR} -llua -lmingwex -lmingw32 +#cgo windows,lua52,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua52 +#cgo windows,lua53,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua53 +#cgo windows,lua54,!llua,!luaa,!luajit,!lluadash5.1 LDFLAGS: -llua54 + +#include +#include + +#include "golua.h" + +*/ +import "C" +import ( + "fmt" + "unsafe" +) + +type LuaStackEntry struct { + Name string + Source string + ShortSource string + CurrentLine int +} + +func newState(L *C.lua_State) *State { + newstate := &State{L, 0, make([]interface{}, 0, 8), make([]uint, 0, 8), nil, nil} + registerGoState(newstate) + C.clua_setgostate(L, C.size_t(newstate.Index)) + C.clua_initstate(L) + return newstate +} + +func (L *State) addFreeIndex(i uint) { + freelen := len(L.freeIndices) + //reallocate if necessary + if freelen+1 > cap(L.freeIndices) { + newSlice := make([]uint, freelen, cap(L.freeIndices)*2) + copy(newSlice, L.freeIndices) + L.freeIndices = newSlice + } + //reslice + L.freeIndices = L.freeIndices[0 : freelen+1] + L.freeIndices[freelen] = i +} + +func (L *State) getFreeIndex() (index uint, ok bool) { + freelen := len(L.freeIndices) + //if there exist entries in the freelist + if freelen > 0 { + i := L.freeIndices[freelen-1] //get index + //fmt.Printf("Free indices before: %v\n", L.freeIndices) + L.freeIndices = L.freeIndices[0 : freelen-1] //'pop' index from list + //fmt.Printf("Free indices after: %v\n", L.freeIndices) + return i, true + } + return 0, false +} + +//returns the registered function id +func (L *State) register(f interface{}) uint { + //fmt.Printf("Registering %v\n") + index, ok := L.getFreeIndex() + //fmt.Printf("\tfreeindex: index = %v, ok = %v\n", index, ok) + //if not ok, then we need to add new index by extending the slice + if !ok { + index = uint(len(L.registry)) + //reallocate backing array if necessary + if index+1 > uint(cap(L.registry)) { + newcap := cap(L.registry) * 2 + if index+1 > uint(newcap) { + newcap = int(index + 1) + } + newSlice := make([]interface{}, index, newcap) + copy(newSlice, L.registry) + L.registry = newSlice + } + //reslice + L.registry = L.registry[0 : index+1] + } + //fmt.Printf("\tregistering %d %v\n", index, f) + L.registry[index] = f + return index +} + +func (L *State) unregister(fid uint) { + //fmt.Printf("Unregistering %d (len: %d, value: %v)\n", fid, len(L.registry), L.registry[fid]) + if (fid < uint(len(L.registry))) && (L.registry[fid] != nil) { + L.registry[fid] = nil + L.addFreeIndex(fid) + } +} + +// Like lua_pushcfunction pushes onto the stack a go function as user data +func (L *State) PushGoFunction(f LuaGoFunction) { + fid := L.register(f) + C.clua_pushgofunction(L.s, C.uint(fid)) +} + +// PushGoClosure pushes a lua.LuaGoFunction to the stack wrapped in a Closure. +// this permits the go function to reflect lua type 'function' when checking with type() +// this implements behaviour akin to lua_pushcfunction() in lua C API. +func (L *State) PushGoClosure(f LuaGoFunction) { + L.PushGoFunction(f) // leaves Go function userdata on stack + C.clua_pushcallback(L.s) // wraps the userdata object with a closure making it into a function +} + +// Sets a metamethod to execute a go function +// +// The code: +// +// L.LGetMetaTable(tableName) +// L.SetMetaMethod(methodName, function) +// +// is the logical equivalent of: +// +// L.LGetMetaTable(tableName) +// L.PushGoFunction(function) +// L.SetField(-2, methodName) +// +// except this wouldn't work because pushing a go function results in user data not a cfunction +func (L *State) SetMetaMethod(methodName string, f LuaGoFunction) { + L.PushGoFunction(f) // leaves Go function userdata on stack + C.clua_pushcallback(L.s) // wraps the userdata object with a closure making it into a function + L.SetField(-2, methodName) +} + +// Pushes a Go struct onto the stack as user data. +// +// The user data will be rigged so that lua code can access and change to public members of simple types directly +func (L *State) PushGoStruct(iface interface{}) { + iid := L.register(iface) + C.clua_pushgostruct(L.s, C.uint(iid)) +} + +// Push a pointer onto the stack as user data. +// +// This function doesn't save a reference to the interface, it is the responsibility of the caller of this function to insure that the interface outlasts the lifetime of the lua object that this function creates. +func (L *State) PushLightUserdata(ud *interface{}) { + //push + C.lua_pushlightuserdata(L.s, unsafe.Pointer(ud)) +} + +// Creates a new user data object of specified size and returns it +func (L *State) NewUserdata(size uintptr) unsafe.Pointer { + return unsafe.Pointer(C.lua_newuserdata(L.s, C.size_t(size))) +} + +// Sets the AtPanic function, returns the old one +// +// BUG(everyone_involved): passing nil causes serious problems +func (L *State) AtPanic(panicf LuaGoFunction) (oldpanicf LuaGoFunction) { + fid := uint(0) + if panicf != nil { + fid = L.register(panicf) + } + oldres := interface{}(C.clua_atpanic(L.s, C.uint(fid))) + switch i := oldres.(type) { + case C.uint: + f := L.registry[uint(i)].(LuaGoFunction) + //free registry entry + L.unregister(uint(i)) + return f + case C.lua_CFunction: + return func(L1 *State) int { + return int(C.clua_callluacfunc(L1.s, i)) + } + } + //generally we only get here if the panicf got set to something like nil + //potentially dangerous because we may silently fail + return nil +} + +func (L *State) callEx(nargs, nresults int, catch bool) (err error) { + if catch { + defer func() { + if err2 := recover(); err2 != nil { + if _, ok := err2.(error); ok { + err = err2.(error) + } + return + } + }() + } + + L.GetGlobal(C.GOLUA_DEFAULT_MSGHANDLER) + // We must record where we put the error handler in the stack otherwise it will be impossible to remove after the pcall when nresults == LUA_MULTRET + erridx := L.GetTop() - nargs - 1 + L.Insert(erridx) + r := L.pcall(nargs, nresults, erridx) + L.Remove(erridx) + if r != 0 { + err = &LuaError{r, L.ToString(-1), L.StackTrace()} + if !catch { + panic(err) + } + } + return +} + +// lua_call +func (L *State) Call(nargs, nresults int) (err error) { + return L.callEx(nargs, nresults, true) +} + +// Like lua_call but panics on errors +func (L *State) MustCall(nargs, nresults int) { + L.callEx(nargs, nresults, false) +} + +// lua_checkstack +func (L *State) CheckStack(extra int) bool { + return C.lua_checkstack(L.s, C.int(extra)) != 0 +} + +// lua_close +func (L *State) Close() { + C.lua_close(L.s) + unregisterGoState(L) +} + +// lua_concat +func (L *State) Concat(n int) { + C.lua_concat(L.s, C.int(n)) +} + +// lua_createtable +func (L *State) CreateTable(narr int, nrec int) { + C.lua_createtable(L.s, C.int(narr), C.int(nrec)) +} + +// lua_getfield +func (L *State) GetField(index int, k string) { + Ck := C.CString(k) + defer C.free(unsafe.Pointer(Ck)) + C.lua_getfield(L.s, C.int(index), Ck) +} + +// lua_getmetatable +func (L *State) GetMetaTable(index int) bool { + return C.lua_getmetatable(L.s, C.int(index)) != 0 +} + +// lua_gettable +func (L *State) GetTable(index int) { C.lua_gettable(L.s, C.int(index)) } + +// lua_gettop +func (L *State) GetTop() int { return int(C.lua_gettop(L.s)) } + +// Returns true if lua_type == LUA_TBOOLEAN +func (L *State) IsBoolean(index int) bool { + return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TBOOLEAN +} + +// Returns true if the value at index is a LuaGoFunction +func (L *State) IsGoFunction(index int) bool { + return C.clua_isgofunction(L.s, C.int(index)) != 0 +} + +// Returns true if the value at index is user data pushed with PushGoStruct +func (L *State) IsGoStruct(index int) bool { + return C.clua_isgostruct(L.s, C.int(index)) != 0 +} + +// Returns true if the value at index is user data pushed with PushGoFunction +func (L *State) IsFunction(index int) bool { + return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TFUNCTION +} + +// Returns true if the value at index is light user data +func (L *State) IsLightUserdata(index int) bool { + return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TLIGHTUSERDATA +} + +// lua_isnil +func (L *State) IsNil(index int) bool { return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TNIL } + +// lua_isnone +func (L *State) IsNone(index int) bool { return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TNONE } + +// lua_isnoneornil +func (L *State) IsNoneOrNil(index int) bool { return int(C.lua_type(L.s, C.int(index))) <= 0 } + +// lua_isnumber +func (L *State) IsNumber(index int) bool { return C.lua_isnumber(L.s, C.int(index)) == 1 } + +// lua_isstring +func (L *State) IsString(index int) bool { return C.lua_isstring(L.s, C.int(index)) == 1 } + +// lua_istable +func (L *State) IsTable(index int) bool { + return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TTABLE +} + +// lua_isthread +func (L *State) IsThread(index int) bool { + return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TTHREAD +} + +// lua_isuserdata +func (L *State) IsUserdata(index int) bool { return C.lua_isuserdata(L.s, C.int(index)) == 1 } + +// Creates a new lua interpreter state with the given allocation function +func NewStateAlloc(f Alloc) *State { + ls := C.clua_newstate(unsafe.Pointer(&f)) + L := newState(ls) + L.allocfn = &f + return L +} + +// lua_newtable +func (L *State) NewTable() { + C.lua_createtable(L.s, 0, 0) +} + +// lua_newthread +func (L *State) NewThread() *State { + //TODO: call newState with result from C.lua_newthread and return it + //TODO: should have same lists as parent + // but may complicate gc + s := C.lua_newthread(L.s) + return &State{s, 0, nil, nil, nil, nil} +} + +// lua_next +func (L *State) Next(index int) int { + return int(C.lua_next(L.s, C.int(index))) +} + +// lua_objlen +// lua_pop +func (L *State) Pop(n int) { + //Why is this implemented this way? I don't get it... + //C.lua_pop(L.s, C.int(n)); + C.lua_settop(L.s, C.int(-n-1)) +} + +// lua_pushboolean +func (L *State) PushBoolean(b bool) { + var bint int + if b { + bint = 1 + } else { + bint = 0 + } + C.lua_pushboolean(L.s, C.int(bint)) +} + +// lua_pushstring +func (L *State) PushString(str string) { + Cstr := C.CString(str) + defer C.free(unsafe.Pointer(Cstr)) + C.lua_pushlstring(L.s, Cstr, C.size_t(len(str))) +} + +func (L *State) PushBytes(b []byte) { + C.lua_pushlstring(L.s, (*C.char)(unsafe.Pointer(&b[0])), C.size_t(len(b))) +} + +// lua_pushinteger +func (L *State) PushInteger(n int64) { + C.lua_pushinteger(L.s, C.lua_Integer(n)) +} + +// lua_pushnil +func (L *State) PushNil() { + C.lua_pushnil(L.s) +} + +// lua_pushnumber +func (L *State) PushNumber(n float64) { + C.lua_pushnumber(L.s, C.lua_Number(n)) +} + +// lua_pushthread +func (L *State) PushThread() (isMain bool) { + return C.lua_pushthread(L.s) != 0 +} + +// lua_pushvalue +func (L *State) PushValue(index int) { + C.lua_pushvalue(L.s, C.int(index)) +} + +// lua_rawequal +func (L *State) RawEqual(index1 int, index2 int) bool { + return C.lua_rawequal(L.s, C.int(index1), C.int(index2)) != 0 +} + +// lua_rawget +func (L *State) RawGet(index int) { + C.lua_rawget(L.s, C.int(index)) +} + +// lua_rawset +func (L *State) RawSet(index int) { + C.lua_rawset(L.s, C.int(index)) +} + +// Registers a Go function as a global variable +func (L *State) Register(name string, f LuaGoFunction) { + L.PushGoFunction(f) + L.SetGlobal(name) +} + +// lua_setallocf +func (L *State) SetAllocf(f Alloc) { + L.allocfn = &f + C.clua_setallocf(L.s, unsafe.Pointer(L.allocfn)) +} + +// lua_setfield +func (L *State) SetField(index int, k string) { + Ck := C.CString(k) + defer C.free(unsafe.Pointer(Ck)) + C.lua_setfield(L.s, C.int(index), Ck) +} + +// lua_setmetatable +func (L *State) SetMetaTable(index int) { + C.lua_setmetatable(L.s, C.int(index)) +} + +// lua_settable +func (L *State) SetTable(index int) { + C.lua_settable(L.s, C.int(index)) +} + +// lua_settop +func (L *State) SetTop(index int) { + C.lua_settop(L.s, C.int(index)) +} + +// lua_status +func (L *State) Status() int { + return int(C.lua_status(L.s)) +} + +// lua_toboolean +func (L *State) ToBoolean(index int) bool { + return C.lua_toboolean(L.s, C.int(index)) != 0 +} + +// Returns the value at index as a Go function (it must be something pushed with PushGoFunction) +func (L *State) ToGoFunction(index int) (f LuaGoFunction) { + if !L.IsGoFunction(index) { + return nil + } + fid := C.clua_togofunction(L.s, C.int(index)) + if fid < 0 { + return nil + } + return L.registry[fid].(LuaGoFunction) +} + +// Returns the value at index as a Go Struct (it must be something pushed with PushGoStruct) +func (L *State) ToGoStruct(index int) (f interface{}) { + if !L.IsGoStruct(index) { + return nil + } + fid := C.clua_togostruct(L.s, C.int(index)) + if fid < 0 { + return nil + } + return L.registry[fid] +} + +// lua_tostring +func (L *State) ToString(index int) string { + var size C.size_t + r := C.lua_tolstring(L.s, C.int(index), &size) + return C.GoStringN(r, C.int(size)) +} + +func (L *State) ToBytes(index int) []byte { + var size C.size_t + b := C.lua_tolstring(L.s, C.int(index), &size) + return C.GoBytes(unsafe.Pointer(b), C.int(size)) +} + +// lua_topointer +func (L *State) ToPointer(index int) uintptr { + return uintptr(C.lua_topointer(L.s, C.int(index))) +} + +// lua_tothread +func (L *State) ToThread(index int) *State { + //TODO: find a way to link lua_State* to existing *State, return that + return &State{} +} + +// lua_touserdata +func (L *State) ToUserdata(index int) unsafe.Pointer { + return unsafe.Pointer(C.lua_touserdata(L.s, C.int(index))) +} + +// lua_type +func (L *State) Type(index int) LuaValType { + return LuaValType(C.lua_type(L.s, C.int(index))) +} + +// lua_typename +func (L *State) Typename(tp int) string { + return C.GoString(C.lua_typename(L.s, C.int(tp))) +} + +// lua_xmove +func XMove(from *State, to *State, n int) { + C.lua_xmove(from.s, to.s, C.int(n)) +} + +// Restricted library opens + +// Calls luaopen_base +func (L *State) OpenBase() { + C.clua_openbase(L.s) +} + +// Calls luaopen_io +func (L *State) OpenIO() { + C.clua_openio(L.s) +} + +// Calls luaopen_math +func (L *State) OpenMath() { + C.clua_openmath(L.s) +} + +// Calls luaopen_package +func (L *State) OpenPackage() { + C.clua_openpackage(L.s) +} + +// Calls luaopen_string +func (L *State) OpenString() { + C.clua_openstring(L.s) +} + +// Calls luaopen_table +func (L *State) OpenTable() { + C.clua_opentable(L.s) +} + +// Calls luaopen_os +func (L *State) OpenOS() { + C.clua_openos(L.s) +} + +// Sets the lua hook (lua_sethook). +// This and SetExecutionLimit are mutual exclusive +func (L *State) SetHook(f HookFunction, instrNumber int) { + L.hookFn = f + C.clua_sethook(L.s, C.int(instrNumber)) +} + +// Sets the maximum number of operations to execute at instrNumber, after this the execution ends +// This and SetHook are mutual exclusive +func (L *State) SetExecutionLimit(instrNumber int) { + L.SetHook(func(l *State) { + l.RaiseError(ExecutionQuantumExceeded) + }, instrNumber) +} + +// Returns the current stack trace +func (L *State) StackTrace() []LuaStackEntry { + r := []LuaStackEntry{} + var d C.lua_Debug + Sln := C.CString("Sln") + defer C.free(unsafe.Pointer(Sln)) + + for depth := 0; C.lua_getstack(L.s, C.int(depth), &d) > 0; depth++ { + C.lua_getinfo(L.s, Sln, &d) + ssb := make([]byte, C.LUA_IDSIZE) + for i := 0; i < C.LUA_IDSIZE; i++ { + ssb[i] = byte(d.short_src[i]) + if ssb[i] == 0 { + ssb = ssb[:i] + break + } + } + ss := string(ssb) + + r = append(r, LuaStackEntry{C.GoString(d.name), C.GoString(d.source), ss, int(d.currentline)}) + } + + return r +} + +func (L *State) RaiseError(msg string) { + st := L.StackTrace() + prefix := "" + if len(st) >= 2 { + prefix = fmt.Sprintf("%s:%d: ", st[1].ShortSource, st[1].CurrentLine) + } + panic(&LuaError{0, prefix + msg, st}) +} + +func (L *State) NewError(msg string) *LuaError { + return &LuaError{0, msg, L.StackTrace()} +} + +func (L *State) GetState() *C.lua_State { + return L.s +} diff --git a/golua/lua/lua51/dummy.go b/golua/lua/lua51/dummy.go new file mode 100644 index 0000000..651a797 --- /dev/null +++ b/golua/lua/lua51/dummy.go @@ -0,0 +1,7 @@ +// +build dummy + +// Package lua contains only a C header files. +// +// This Go file is part of a workaround for `go mod vendor`. +// Please see the file dummy.go in the parent directory for more information. +package lua diff --git a/golua/lua/lua51/lauxlib.h b/golua/lua/lua51/lauxlib.h new file mode 100644 index 0000000..3425823 --- /dev/null +++ b/golua/lua/lua51/lauxlib.h @@ -0,0 +1,174 @@ +/* +** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lauxlib_h +#define lauxlib_h + + +#include +#include + +#include "lua.h" + + +#if defined(LUA_COMPAT_GETN) +LUALIB_API int (luaL_getn) (lua_State *L, int t); +LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); +#else +#define luaL_getn(L,i) ((int)lua_objlen(L, i)) +#define luaL_setn(L,i,j) ((void)0) /* no op! */ +#endif + +#if defined(LUA_COMPAT_OPENLIB) +#define luaI_openlib luaL_openlib +#endif + + +/* extra error code for `luaL_load' */ +#define LUA_ERRFILE (LUA_ERRERR+1) + + +typedef struct luaL_Reg { + const char *name; + lua_CFunction func; +} luaL_Reg; + + + +LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, + const luaL_Reg *l, int nup); +LUALIB_API void (luaL_register) (lua_State *L, const char *libname, + const luaL_Reg *l); +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); +LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, + size_t *l); +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, + const char *def, size_t *l); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); + +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, + lua_Integer def); + +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); +LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int narg); + +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); + +LUALIB_API void (luaL_where) (lua_State *L, int lvl); +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); + +LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, + const char *const lst[]); + +LUALIB_API int (luaL_ref) (lua_State *L, int t); +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); + +LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); +LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, + const char *name); +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); + +LUALIB_API lua_State *(luaL_newstate) (void); + + +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, + const char *r); + +LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, + const char *fname, int szhint); + + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define luaL_argcheck(L, cond,numarg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) + +#define luaL_dofile(L, fn) \ + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_dostring(L, s) \ + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) + +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + + + +typedef struct luaL_Buffer { + char *p; /* current position in buffer */ + int lvl; /* number of strings in the stack (level) */ + lua_State *L; + char buffer[LUAL_BUFFERSIZE]; +} luaL_Buffer; + +#define luaL_addchar(B,c) \ + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ + (*(B)->p++ = (char)(c))) + +/* compatibility only */ +#define luaL_putchar(B,c) luaL_addchar(B,c) + +#define luaL_addsize(B,n) ((B)->p += (n)) + +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); +LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); + + +/* }====================================================== */ + + +/* compatibility with ref system */ + +/* pre-defined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ + (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) + +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) + +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) + + +#define luaL_reg luaL_Reg + +#endif + + diff --git a/golua/lua/lua51/lua.h b/golua/lua/lua51/lua.h new file mode 100644 index 0000000..e4bdfd3 --- /dev/null +++ b/golua/lua/lua51/lua.h @@ -0,0 +1,388 @@ +/* +** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $ +** Lua - An Extensible Extension Language +** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** See Copyright Notice at the end of this file +*/ + + +#ifndef lua_h +#define lua_h + +#include +#include + + +#include "luaconf.h" + + +#define LUA_VERSION "Lua 5.1" +#define LUA_RELEASE "Lua 5.1.4" +#define LUA_VERSION_NUM 501 +#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" + + +/* mark for precompiled code (`Lua') */ +#define LUA_SIGNATURE "\033Lua" + +/* option for multiple returns in `lua_pcall' and `lua_call' */ +#define LUA_MULTRET (-1) + + +/* +** pseudo-indices +*/ +#define LUA_REGISTRYINDEX (-10000) +#define LUA_ENVIRONINDEX (-10001) +#define LUA_GLOBALSINDEX (-10002) +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) + + +/* thread status; 0 is OK */ +#define LUA_YIELD 1 +#define LUA_ERRRUN 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRERR 5 + + +typedef struct lua_State lua_State; + +typedef int (*lua_CFunction) (lua_State *L); + + +/* +** functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); + + +/* +** prototype for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + + +/* +** basic types +*/ +#define LUA_TNONE (-1) + +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_TFUNCTION 6 +#define LUA_TUSERDATA 7 +#define LUA_TTHREAD 8 + + + +/* minimum Lua stack available to a C function */ +#define LUA_MINSTACK 20 + + +/* +** generic extra include file +*/ +#if defined(LUA_USER_H) +#include LUA_USER_H +#endif + + +/* type of numbers in Lua */ +typedef LUA_NUMBER lua_Number; + + +/* type for integer functions */ +typedef LUA_INTEGER lua_Integer; + + + +/* +** state manipulation +*/ +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API void (lua_close) (lua_State *L); +LUA_API lua_State *(lua_newthread) (lua_State *L); + +LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); + + +/* +** basic stack manipulation +*/ +LUA_API int (lua_gettop) (lua_State *L); +LUA_API void (lua_settop) (lua_State *L, int idx); +LUA_API void (lua_pushvalue) (lua_State *L, int idx); +LUA_API void (lua_remove) (lua_State *L, int idx); +LUA_API void (lua_insert) (lua_State *L, int idx); +LUA_API void (lua_replace) (lua_State *L, int idx); +LUA_API int (lua_checkstack) (lua_State *L, int sz); + +LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); + + +/* +** access functions (stack -> C) +*/ + +LUA_API int (lua_isnumber) (lua_State *L, int idx); +LUA_API int (lua_isstring) (lua_State *L, int idx); +LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isuserdata) (lua_State *L, int idx); +LUA_API int (lua_type) (lua_State *L, int idx); +LUA_API const char *(lua_typename) (lua_State *L, int tp); + +LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); + +LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); +LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); +LUA_API int (lua_toboolean) (lua_State *L, int idx); +LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); +LUA_API size_t (lua_objlen) (lua_State *L, int idx); +LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); +LUA_API void *(lua_touserdata) (lua_State *L, int idx); +LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); +LUA_API const void *(lua_topointer) (lua_State *L, int idx); + + +/* +** push functions (C -> stack) +*/ +LUA_API void (lua_pushnil) (lua_State *L); +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); +LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); +LUA_API void (lua_pushstring) (lua_State *L, const char *s); +LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, + va_list argp); +LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); +LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); +LUA_API void (lua_pushboolean) (lua_State *L, int b); +LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); +LUA_API int (lua_pushthread) (lua_State *L); + + +/* +** get functions (Lua -> stack) +*/ +LUA_API void (lua_gettable) (lua_State *L, int idx); +LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawget) (lua_State *L, int idx); +LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); +LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); +LUA_API int (lua_getmetatable) (lua_State *L, int objindex); +LUA_API void (lua_getfenv) (lua_State *L, int idx); + + +/* +** set functions (stack -> Lua) +*/ +LUA_API void (lua_settable) (lua_State *L, int idx); +LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawset) (lua_State *L, int idx); +LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); +LUA_API int (lua_setmetatable) (lua_State *L, int objindex); +LUA_API int (lua_setfenv) (lua_State *L, int idx); + + +/* +** `load' and `call' functions (load and run Lua code) +*/ +LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); +LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); +LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); +LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname); + +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); + + +/* +** coroutine functions +*/ +LUA_API int (lua_yield) (lua_State *L, int nresults); +LUA_API int (lua_resume) (lua_State *L, int narg); +LUA_API int (lua_status) (lua_State *L); + +/* +** garbage-collection function and options +*/ + +#define LUA_GCSTOP 0 +#define LUA_GCRESTART 1 +#define LUA_GCCOLLECT 2 +#define LUA_GCCOUNT 3 +#define LUA_GCCOUNTB 4 +#define LUA_GCSTEP 5 +#define LUA_GCSETPAUSE 6 +#define LUA_GCSETSTEPMUL 7 + +LUA_API int (lua_gc) (lua_State *L, int what, int data); + + +/* +** miscellaneous functions +*/ + +LUA_API int (lua_error) (lua_State *L); + +LUA_API int (lua_next) (lua_State *L, int idx); + +LUA_API void (lua_concat) (lua_State *L, int n); + +LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define lua_pop(L,n) lua_settop(L, -(n)-1) + +#define lua_newtable(L) lua_createtable(L, 0, 0) + +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) + +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) + +#define lua_strlen(L,i) lua_objlen(L, (i)) + +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) + +#define lua_pushliteral(L, s) \ + lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) + +#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) +#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) + +#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) + + + +/* +** compatibility macros and functions +*/ + +#define lua_open() luaL_newstate() + +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) + +#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) + +#define lua_Chunkreader lua_Reader +#define lua_Chunkwriter lua_Writer + + +/* hack */ +LUA_API void lua_setlevel (lua_State *from, lua_State *to); + + +/* +** {====================================================================== +** Debug API +** ======================================================================= +*/ + + +/* +** Event codes +*/ +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILRET 4 + + +/* +** Event masks +*/ +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +typedef struct lua_Debug lua_Debug; /* activation record */ + + +/* Functions to be called by the debuger in specific events */ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); + + +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); + +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); +LUA_API lua_Hook lua_gethook (lua_State *L); +LUA_API int lua_gethookmask (lua_State *L); +LUA_API int lua_gethookcount (lua_State *L); + + +struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) `global', `local', `field', `method' */ + const char *what; /* (S) `Lua', `C', `main', `tail' */ + const char *source; /* (S) */ + int currentline; /* (l) */ + int nups; /* (u) number of upvalues */ + int linedefined; /* (S) */ + int lastlinedefined; /* (S) */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + int i_ci; /* active function */ +}; + +/* }====================================================================== */ + + +/****************************************************************************** +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + + +#endif diff --git a/golua/lua/lua51/luaconf.h b/golua/lua/lua51/luaconf.h new file mode 100644 index 0000000..3a099ce --- /dev/null +++ b/golua/lua/lua51/luaconf.h @@ -0,0 +1,767 @@ +/* +** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $ +** Configuration file for Lua +** See Copyright Notice in lua.h +*/ + + +#ifndef lconfig_h +#define lconfig_h + +#include +#include + + +/* +** ================================================================== +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + + +/* +@@ LUA_ANSI controls the use of non-ansi features. +** CHANGE it (define it) if you want Lua to avoid the use of any +** non-ansi feature or library. +*/ +#if defined(__STRICT_ANSI__) +#define LUA_ANSI +#endif + + +#if !defined(LUA_ANSI) && defined(_WIN32) +#define LUA_WIN +#endif + +#if defined(LUA_USE_LINUX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#define LUA_USE_READLINE /* needs some extra libraries */ +#endif + +#if defined(LUA_USE_MACOSX) +#define LUA_USE_POSIX +#define LUA_DL_DYLD /* does not need extra library */ +#endif + + + +/* +@@ LUA_USE_POSIX includes all functionallity listed as X/Open System +@* Interfaces Extension (XSI). +** CHANGE it (define it) if your system is XSI compatible. +*/ +#if defined(LUA_USE_POSIX) +#define LUA_USE_MKSTEMP +#define LUA_USE_ISATTY +#define LUA_USE_POPEN +#define LUA_USE_ULONGJMP +#endif + + +/* +@@ LUA_PATH and LUA_CPATH are the names of the environment variables that +@* Lua check to set its paths. +@@ LUA_INIT is the name of the environment variable that Lua +@* checks for initialization code. +** CHANGE them if you want different names. +*/ +#define LUA_PATH "LUA_PATH" +#define LUA_CPATH "LUA_CPATH" +#define LUA_INIT "LUA_INIT" + + +/* +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for +@* Lua libraries. +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for +@* C libraries. +** CHANGE them if your machine has a non-conventional directory +** hierarchy or if you want to install your libraries in +** non-conventional directories. +*/ +#if defined(_WIN32) +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_PATH_DEFAULT \ + ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua" +#define LUA_CPATH_DEFAULT \ + ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" + +#else +#define LUA_ROOT "/usr/local/" +#define LUA_ROOT2 "/usr/" +#define LUA_LDIR LUA_ROOT "share/lua/5.1/" +#define LUA_LDIR2 LUA_ROOT2 "share/lua/5.1/" +#define LUA_CDIR LUA_ROOT "lib/lua/5.1/" +#define LUA_CDIR2 LUA_ROOT2 "lib/lua/5.1/" +#define LUA_PATH_DEFAULT \ + "./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ + LUA_LDIR2"?.lua;" LUA_LDIR2"?/init.lua" +#define LUA_CPATH_DEFAULT \ + "./?.so;" LUA_CDIR"?.so;" LUA_CDIR2"?.so;" LUA_CDIR"loadall.so" +#endif + + +/* +@@ LUA_DIRSEP is the directory separator (for submodules). +** CHANGE it if your machine does not use "/" as the directory separator +** and is not Windows. (On Windows Lua automatically uses "\".) +*/ +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + + +/* +@@ LUA_PATHSEP is the character that separates templates in a path. +@@ LUA_PATH_MARK is the string that marks the substitution points in a +@* template. +@@ LUA_EXECDIR in a Windows path is replaced by the executable's +@* directory. +@@ LUA_IGMARK is a mark to ignore all before it when bulding the +@* luaopen_ function name. +** CHANGE them if for some reason your system cannot use those +** characters. (E.g., if one of those characters is a common character +** in file/directory names.) Probably you do not need to change them. +*/ +#define LUA_PATHSEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXECDIR "!" +#define LUA_IGMARK "-" + + +/* +@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. +** CHANGE that if ptrdiff_t is not adequate on your machine. (On most +** machines, ptrdiff_t gives a good choice between int or long.) +*/ +#define LUA_INTEGER ptrdiff_t + + +/* +@@ LUA_API is a mark for all core API functions. +@@ LUALIB_API is a mark for all standard library functions. +** CHANGE them if you need to define those functions in some special way. +** For instance, if you want to create one Windows DLL with the core and +** the libraries, you may want to use the following definition (define +** LUA_BUILD_AS_DLL to get it). +*/ +#if defined(LUA_BUILD_AS_DLL) + +#if defined(LUA_CORE) || defined(LUA_LIB) +#define LUA_API __declspec(dllexport) +#else +#define LUA_API __declspec(dllimport) +#endif + +#else + +#define LUA_API extern + +#endif + +/* more often than not the libs go together with the core */ +#define LUALIB_API LUA_API + + +/* +@@ LUAI_FUNC is a mark for all extern functions that are not to be +@* exported to outside modules. +@@ LUAI_DATA is a mark for all extern (const) variables that are not to +@* be exported to outside modules. +** CHANGE them if you need to mark them in some special way. Elf/gcc +** (versions 3.2 and later) mark them as "hidden" to optimize access +** when Lua is compiled as a shared library. +*/ +#if defined(luaall_c) +#define LUAI_FUNC static +#define LUAI_DATA /* empty */ + +#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + defined(__ELF__) +#define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#define LUAI_DATA LUAI_FUNC + +#else +#define LUAI_FUNC extern +#define LUAI_DATA extern +#endif + + + +/* +@@ LUA_QL describes how error messages quote program elements. +** CHANGE it if you want a different appearance. +*/ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + + +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@* of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 + + +/* +** {================================================================== +** Stand-alone configuration +** =================================================================== +*/ + +#if defined(lua_c) || defined(luaall_c) + +/* +@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that +@* is, whether we're running lua interactively). +** CHANGE it if you have a better definition for non-POSIX/non-Windows +** systems. +*/ +#if defined(LUA_USE_ISATTY) +#include +#define lua_stdin_is_tty() isatty(0) +#elif defined(LUA_WIN) +#include +#include +#define lua_stdin_is_tty() _isatty(_fileno(stdin)) +#else +#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ +#endif + + +/* +@@ LUA_PROMPT is the default prompt used by stand-alone Lua. +@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua. +** CHANGE them if you want different prompts. (You can also change the +** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.) +*/ +#define LUA_PROMPT "> " +#define LUA_PROMPT2 ">> " + + +/* +@@ LUA_PROGNAME is the default name for the stand-alone Lua program. +** CHANGE it if your stand-alone interpreter has a different name and +** your system is not able to detect that name automatically. +*/ +#define LUA_PROGNAME "lua" + + +/* +@@ LUA_MAXINPUT is the maximum length for an input line in the +@* stand-alone interpreter. +** CHANGE it if you need longer lines. +*/ +#define LUA_MAXINPUT 512 + + +/* +@@ lua_readline defines how to show a prompt and then read a line from +@* the standard input. +@@ lua_saveline defines how to "save" a read line in a "history". +@@ lua_freeline defines how to free a line read by lua_readline. +** CHANGE them if you want to improve this functionality (e.g., by using +** GNU readline and history facilities). +*/ +#if defined(LUA_USE_READLINE) +#include +#include +#include +#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) +#define lua_saveline(L,idx) \ + if (lua_strlen(L,idx) > 0) /* non-empty line? */ \ + add_history(lua_tostring(L, idx)); /* add it to history */ +#define lua_freeline(L,b) ((void)L, free(b)) +#else +#define lua_readline(L,b,p) \ + ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ + fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ +#define lua_saveline(L,idx) { (void)L; (void)idx; } +#define lua_freeline(L,b) { (void)L; (void)b; } +#endif + +#endif + +/* }================================================================== */ + + +/* +@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles +@* as a percentage. +** CHANGE it if you want the GC to run faster or slower (higher values +** mean larger pauses which mean slower collection.) You can also change +** this value dynamically. +*/ +#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */ + + +/* +@@ LUAI_GCMUL defines the default speed of garbage collection relative to +@* memory allocation as a percentage. +** CHANGE it if you want to change the granularity of the garbage +** collection. (Higher values mean coarser collections. 0 represents +** infinity, where each step performs a full collection.) You can also +** change this value dynamically. +*/ +#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ + + + +/* +@@ LUA_COMPAT_GETN controls compatibility with old getn behavior. +** CHANGE it (define it) if you want exact compatibility with the +** behavior of setn/getn in Lua 5.0. +*/ +#undef LUA_COMPAT_GETN + +/* +@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib. +** CHANGE it to undefined as soon as you do not need a global 'loadlib' +** function (the function is still available as 'package.loadlib'). +*/ +#undef LUA_COMPAT_LOADLIB + +/* +@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature. +** CHANGE it to undefined as soon as your programs use only '...' to +** access vararg parameters (instead of the old 'arg' table). +*/ +#define LUA_COMPAT_VARARG + +/* +@@ LUA_COMPAT_MOD controls compatibility with old math.mod function. +** CHANGE it to undefined as soon as your programs use 'math.fmod' or +** the new '%' operator instead of 'math.mod'. +*/ +#define LUA_COMPAT_MOD + +/* +@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting +@* facility. +** CHANGE it to 2 if you want the old behaviour, or undefine it to turn +** off the advisory error when nesting [[...]]. +*/ +#define LUA_COMPAT_LSTR 1 + +/* +@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name. +** CHANGE it to undefined as soon as you rename 'string.gfind' to +** 'string.gmatch'. +*/ +#define LUA_COMPAT_GFIND + +/* +@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib' +@* behavior. +** CHANGE it to undefined as soon as you replace to 'luaL_register' +** your uses of 'luaL_openlib' +*/ +#define LUA_COMPAT_OPENLIB + + + +/* +@@ luai_apicheck is the assert macro used by the Lua-C API. +** CHANGE luai_apicheck if you want Lua to perform some checks in the +** parameters it gets from API calls. This may slow down the interpreter +** a bit, but may be quite useful when debugging C code that interfaces +** with Lua. A useful redefinition is to use assert.h. +*/ +#if defined(LUA_USE_APICHECK) +#include +#define luai_apicheck(L,o) { (void)L; assert(o); } +#else +#define luai_apicheck(L,o) { (void)L; } +#endif + + +/* +@@ LUAI_BITSINT defines the number of bits in an int. +** CHANGE here if Lua cannot automatically detect the number of bits of +** your machine. Probably you do not need to change this. +*/ +/* avoid overflows in comparison */ +#if INT_MAX-20 < 32760 +#define LUAI_BITSINT 16 +#elif INT_MAX > 2147483640L +/* int has at least 32 bits */ +#define LUAI_BITSINT 32 +#else +#error "you must define LUA_BITSINT with number of bits in an integer" +#endif + + +/* +@@ LUAI_UINT32 is an unsigned integer with at least 32 bits. +@@ LUAI_INT32 is an signed integer with at least 32 bits. +@@ LUAI_UMEM is an unsigned integer big enough to count the total +@* memory used by Lua. +@@ LUAI_MEM is a signed integer big enough to count the total memory +@* used by Lua. +** CHANGE here if for some weird reason the default definitions are not +** good enough for your machine. (The definitions in the 'else' +** part always works, but may waste space on machines with 64-bit +** longs.) Probably you do not need to change this. +*/ +#if LUAI_BITSINT >= 32 +#define LUAI_UINT32 unsigned int +#define LUAI_INT32 int +#define LUAI_MAXINT32 INT_MAX +#define LUAI_UMEM size_t +#define LUAI_MEM ptrdiff_t +#else +/* 16-bit ints */ +#define LUAI_UINT32 unsigned long +#define LUAI_INT32 long +#define LUAI_MAXINT32 LONG_MAX +#define LUAI_UMEM unsigned long +#define LUAI_MEM long +#endif + + +/* +@@ LUAI_MAXCALLS limits the number of nested calls. +** CHANGE it if you need really deep recursive calls. This limit is +** arbitrary; its only purpose is to stop infinite recursion before +** exhausting memory. +*/ +#define LUAI_MAXCALLS 20000 + + +/* +@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function +@* can use. +** CHANGE it if you need lots of (Lua) stack space for your C +** functions. This limit is arbitrary; its only purpose is to stop C +** functions to consume unlimited stack space. (must be smaller than +** -LUA_REGISTRYINDEX) +*/ +#define LUAI_MAXCSTACK 8000 + + + +/* +** {================================================================== +** CHANGE (to smaller values) the following definitions if your system +** has a small C stack. (Or you may want to change them to larger +** values if your system has a large C stack and these limits are +** too rigid for you.) Some of these constants control the size of +** stack-allocated arrays used by the compiler or the interpreter, while +** others limit the maximum number of recursive calls that the compiler +** or the interpreter can perform. Values too large may cause a C stack +** overflow for some forms of deep constructs. +** =================================================================== +*/ + + +/* +@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and +@* syntactical nested non-terminals in a program. +*/ +#define LUAI_MAXCCALLS 200 + + +/* +@@ LUAI_MAXVARS is the maximum number of local variables per function +@* (must be smaller than 250). +*/ +#define LUAI_MAXVARS 200 + + +/* +@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function +@* (must be smaller than 250). +*/ +#define LUAI_MAXUPVALUES 60 + + +/* +@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +*/ +#define LUAL_BUFFERSIZE BUFSIZ + +/* }================================================================== */ + + + + +/* +** {================================================================== +@@ LUA_NUMBER is the type of numbers in Lua. +** CHANGE the following definitions only if you want to build Lua +** with a number type different from double. You may also need to +** change lua_number2int & lua_number2integer. +** =================================================================== +*/ + +#define LUA_NUMBER_DOUBLE +#define LUA_NUMBER double + +/* +@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' +@* over a number. +*/ +#define LUAI_UACNUMBER double + + +/* +@@ LUA_NUMBER_SCAN is the format for reading numbers. +@@ LUA_NUMBER_FMT is the format for writing numbers. +@@ lua_number2str converts a number to a string. +@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. +@@ lua_str2number converts a string to a number. +*/ +#define LUA_NUMBER_SCAN "%lf" +#define LUA_NUMBER_FMT "%.14g" +#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) +#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ +#define lua_str2number(s,p) strtod((s), (p)) + + +/* +@@ The luai_num* macros define the primitive operations over numbers. +*/ +#if defined(LUA_CORE) +#include +#define luai_numadd(a,b) ((a)+(b)) +#define luai_numsub(a,b) ((a)-(b)) +#define luai_nummul(a,b) ((a)*(b)) +#define luai_numdiv(a,b) ((a)/(b)) +#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b)) +#define luai_numpow(a,b) (pow(a,b)) +#define luai_numunm(a) (-(a)) +#define luai_numeq(a,b) ((a)==(b)) +#define luai_numlt(a,b) ((a)<(b)) +#define luai_numle(a,b) ((a)<=(b)) +#define luai_numisnan(a) (!luai_numeq((a), (a))) +#endif + + +/* +@@ lua_number2int is a macro to convert lua_Number to int. +@@ lua_number2integer is a macro to convert lua_Number to lua_Integer. +** CHANGE them if you know a faster way to convert a lua_Number to +** int (with any rounding method and without throwing errors) in your +** system. In Pentium machines, a naive typecast from double to int +** in C is extremely slow, so any alternative is worth trying. +*/ + +/* On a Pentium, resort to a trick */ +#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \ + (defined(__i386) || defined (_M_IX86) || defined(__i386__)) + +/* On a Microsoft compiler, use assembler */ +#if defined(_MSC_VER) + +#define lua_number2int(i,d) __asm fld d __asm fistp i +#define lua_number2integer(i,n) lua_number2int(i, n) + +/* the next trick should work on any Pentium, but sometimes clashes + with a DirectX idiosyncrasy */ +#else + +union luai_Cast { double l_d; long l_l; }; +#define lua_number2int(i,d) \ + { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; } +#define lua_number2integer(i,n) lua_number2int(i, n) + +#endif + + +/* this option always works, but may be slow */ +#else +#define lua_number2int(i,d) ((i)=(int)(d)) +#define lua_number2integer(i,d) ((i)=(lua_Integer)(d)) + +#endif + +/* }================================================================== */ + + +/* +@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment. +** CHANGE it if your system requires alignments larger than double. (For +** instance, if your system supports long doubles and they must be +** aligned in 16-byte boundaries, then you should add long double in the +** union.) Probably you do not need to change this. +*/ +#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } + + +/* +@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling. +** CHANGE them if you prefer to use longjmp/setjmp even with C++ +** or if want/don't to use _longjmp/_setjmp instead of regular +** longjmp/setjmp. By default, Lua handles errors with exceptions when +** compiling as C++ code, with _longjmp/_setjmp when asked to use them, +** and with longjmp/setjmp otherwise. +*/ +#if defined(__cplusplus) +/* C++ exceptions */ +#define LUAI_THROW(L,c) throw(c) +#define LUAI_TRY(L,c,a) try { a } catch(...) \ + { if ((c)->status == 0) (c)->status = -1; } +#define luai_jmpbuf int /* dummy variable */ + +#elif defined(LUA_USE_ULONGJMP) +/* in Unix, try _longjmp/_setjmp (more efficient) */ +#define LUAI_THROW(L,c) _longjmp((c)->b, 1) +#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } +#define luai_jmpbuf jmp_buf + +#else +/* default handling with long jumps */ +#define LUAI_THROW(L,c) longjmp((c)->b, 1) +#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } +#define luai_jmpbuf jmp_buf + +#endif + + +/* +@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern +@* can do during pattern-matching. +** CHANGE it if you need more captures. This limit is arbitrary. +*/ +#define LUA_MAXCAPTURES 32 + + +/* +@@ lua_tmpnam is the function that the OS library uses to create a +@* temporary name. +@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam. +** CHANGE them if you have an alternative to tmpnam (which is considered +** insecure) or if you want the original tmpnam anyway. By default, Lua +** uses tmpnam except when POSIX is available, where it uses mkstemp. +*/ +#if defined(loslib_c) || defined(luaall_c) + +#if defined(LUA_USE_MKSTEMP) +#include +#define LUA_TMPNAMBUFSIZE 32 +#define lua_tmpnam(b,e) { \ + strcpy(b, "/tmp/lua_XXXXXX"); \ + e = mkstemp(b); \ + if (e != -1) close(e); \ + e = (e == -1); } + +#else +#define LUA_TMPNAMBUFSIZE L_tmpnam +#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } +#endif + +#endif + + +/* +@@ lua_popen spawns a new process connected to the current one through +@* the file streams. +** CHANGE it if you have a way to implement it in your system. +*/ +#if defined(LUA_USE_POPEN) + +#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) +#define lua_pclose(L,file) ((void)L, (pclose(file) != -1)) + +#elif defined(LUA_WIN) + +#define lua_popen(L,c,m) ((void)L, _popen(c,m)) +#define lua_pclose(L,file) ((void)L, (_pclose(file) != -1)) + +#else + +#define lua_popen(L,c,m) ((void)((void)c, m), \ + luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0) +#define lua_pclose(L,file) ((void)((void)L, file), 0) + +#endif + +/* +@@ LUA_DL_* define which dynamic-library system Lua should use. +** CHANGE here if Lua has problems choosing the appropriate +** dynamic-library system for your platform (either Windows' DLL, Mac's +** dyld, or Unix's dlopen). If your system is some kind of Unix, there +** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for +** it. To use dlopen you also need to adapt the src/Makefile (probably +** adding -ldl to the linker options), so Lua does not select it +** automatically. (When you change the makefile to add -ldl, you must +** also add -DLUA_USE_DLOPEN.) +** If you do not want any kind of dynamic library, undefine all these +** options. +** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD. +*/ +#if defined(LUA_USE_DLOPEN) +#define LUA_DL_DLOPEN +#endif + +#if defined(LUA_WIN) +#define LUA_DL_DLL +#endif + + +/* +@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State +@* (the data goes just *before* the lua_State pointer). +** CHANGE (define) this if you really need that. This value must be +** a multiple of the maximum alignment required for your machine. +*/ +#define LUAI_EXTRASPACE 0 + + +/* +@@ luai_userstate* allow user-specific actions on threads. +** CHANGE them if you defined LUAI_EXTRASPACE and need to do something +** extra when a thread is created/deleted/resumed/yielded. +*/ +#define luai_userstateopen(L) ((void)L) +#define luai_userstateclose(L) ((void)L) +#define luai_userstatethread(L,L1) ((void)L) +#define luai_userstatefree(L) ((void)L) +#define luai_userstateresume(L,n) ((void)L) +#define luai_userstateyield(L,n) ((void)L) + + +/* +@@ LUA_INTFRMLEN is the length modifier for integer conversions +@* in 'string.format'. +@@ LUA_INTFRM_T is the integer type correspoding to the previous length +@* modifier. +** CHANGE them if your system supports long long or does not support long. +*/ + +#if defined(LUA_USELONGLONG) + +#define LUA_INTFRMLEN "ll" +#define LUA_INTFRM_T long long + +#else + +#define LUA_INTFRMLEN "l" +#define LUA_INTFRM_T long + +#endif + + + +/* =================================================================== */ + +/* +** Local configuration. You can use this space to add your redefinitions +** without modifying the main part of the file. +*/ + + + +#endif + diff --git a/golua/lua/lua51/lualib.h b/golua/lua/lua51/lualib.h new file mode 100644 index 0000000..469417f --- /dev/null +++ b/golua/lua/lua51/lualib.h @@ -0,0 +1,53 @@ +/* +** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lua standard libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lualib_h +#define lualib_h + +#include "lua.h" + + +/* Key to file-handle type */ +#define LUA_FILEHANDLE "FILE*" + + +#define LUA_COLIBNAME "coroutine" +LUALIB_API int (luaopen_base) (lua_State *L); + +#define LUA_TABLIBNAME "table" +LUALIB_API int (luaopen_table) (lua_State *L); + +#define LUA_IOLIBNAME "io" +LUALIB_API int (luaopen_io) (lua_State *L); + +#define LUA_OSLIBNAME "os" +LUALIB_API int (luaopen_os) (lua_State *L); + +#define LUA_STRLIBNAME "string" +LUALIB_API int (luaopen_string) (lua_State *L); + +#define LUA_MATHLIBNAME "math" +LUALIB_API int (luaopen_math) (lua_State *L); + +#define LUA_DBLIBNAME "debug" +LUALIB_API int (luaopen_debug) (lua_State *L); + +#define LUA_LOADLIBNAME "package" +LUALIB_API int (luaopen_package) (lua_State *L); + + +/* open all previous libraries */ +LUALIB_API void (luaL_openlibs) (lua_State *L); + + + +#ifndef lua_assert +#define lua_assert(x) ((void)0) +#endif + + +#endif diff --git a/golua/lua/lua52/dummy.go b/golua/lua/lua52/dummy.go new file mode 100644 index 0000000..651a797 --- /dev/null +++ b/golua/lua/lua52/dummy.go @@ -0,0 +1,7 @@ +// +build dummy + +// Package lua contains only a C header files. +// +// This Go file is part of a workaround for `go mod vendor`. +// Please see the file dummy.go in the parent directory for more information. +package lua diff --git a/golua/lua/lua52/lauxlib.h b/golua/lua/lua52/lauxlib.h new file mode 100644 index 0000000..ac4d15f --- /dev/null +++ b/golua/lua/lua52/lauxlib.h @@ -0,0 +1,212 @@ +/* +** $Id: lauxlib.h,v 1.120 2011/11/29 15:55:08 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lauxlib_h +#define lauxlib_h + + +#include +#include + +#include "lua.h" + + + +/* extra error code for `luaL_load' */ +#define LUA_ERRFILE (LUA_ERRERR+1) + + +typedef struct luaL_Reg { + const char *name; + lua_CFunction func; +} luaL_Reg; + + +LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); +#define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) + +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); +LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); +LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, + size_t *l); +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, + const char *def, size_t *l); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); + +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, + lua_Integer def); +LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); +LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, + lua_Unsigned def); + +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); +LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int narg); + +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); +LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); +LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); + +LUALIB_API void (luaL_where) (lua_State *L, int lvl); +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); + +LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, + const char *const lst[]); + +LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); +LUALIB_API int (luaL_execresult) (lua_State *L, int stat); + +/* pre-defined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +LUALIB_API int (luaL_ref) (lua_State *L, int t); +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); + +LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, + const char *mode); + +#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) + +LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, + const char *name, const char *mode); +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); + +LUALIB_API lua_State *(luaL_newstate) (void); + +LUALIB_API int (luaL_len) (lua_State *L, int idx); + +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, + const char *r); + +LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); + +LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); + +LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, + const char *msg, int level); + +LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, + lua_CFunction openf, int glb); + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + + +#define luaL_newlibtable(L,l) \ + lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) + +#define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) + +#define luaL_argcheck(L, cond,numarg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) + +#define luaL_dofile(L, fn) \ + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_dostring(L, s) \ + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) + +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) + +#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) + + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + +typedef struct luaL_Buffer { + char *b; /* buffer address */ + size_t size; /* buffer size */ + size_t n; /* number of characters in buffer */ + lua_State *L; + char initb[LUAL_BUFFERSIZE]; /* initial buffer */ +} luaL_Buffer; + + +#define luaL_addchar(B,c) \ + ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ + ((B)->b[(B)->n++] = (c))) + +#define luaL_addsize(B,s) ((B)->n += (s)) + +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); +LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); +LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); + +#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) + +/* }====================================================== */ + + + +/* +** {====================================================== +** File handles for IO library +** ======================================================= +*/ + +/* +** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and +** initial structure 'luaL_Stream' (it may contain other fields +** after that initial structure). +*/ + +#define LUA_FILEHANDLE "FILE*" + + +typedef struct luaL_Stream { + FILE *f; /* stream (NULL for incompletely created streams) */ + lua_CFunction closef; /* to close stream (NULL for closed streams) */ +} luaL_Stream; + +/* }====================================================== */ + + + +/* compatibility with old module system */ +#if defined(LUA_COMPAT_MODULE) + +LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, + int sizehint); +LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, + const luaL_Reg *l, int nup); + +#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) + +#endif + + +#endif + + diff --git a/golua/lua/lua52/lua.h b/golua/lua/lua52/lua.h new file mode 100644 index 0000000..f9800fc --- /dev/null +++ b/golua/lua/lua52/lua.h @@ -0,0 +1,444 @@ +/* +** $Id: lua.h,v 1.285 2013/03/15 13:04:22 roberto Exp $ +** Lua - A Scripting Language +** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** See Copyright Notice at the end of this file +*/ + + +#ifndef lua_h +#define lua_h + +#include +#include + + +#include "luaconf.h" + + +#define LUA_VERSION_MAJOR "5" +#define LUA_VERSION_MINOR "2" +#define LUA_VERSION_NUM 502 +#define LUA_VERSION_RELEASE "2" + +#define LUA_VERSION "Lua 5.2" +#define LUA_RELEASE "Lua 5.2.2" +#define LUA_COPYRIGHT "Lua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" + + +/* mark for precompiled code ('Lua') */ +#define LUA_SIGNATURE "\033Lua" + +/* option for multiple returns in 'lua_pcall' and 'lua_call' */ +#define LUA_MULTRET (-1) + + +/* +** pseudo-indices +*/ +#define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX +#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) + + +/* thread status */ +#define LUA_OK 0 +#define LUA_YIELD 1 +#define LUA_ERRRUN 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRGCMM 5 +#define LUA_ERRERR 6 + + +typedef struct lua_State lua_State; + +typedef int (*lua_CFunction) (lua_State *L); + + +/* +** functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); + + +/* +** prototype for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + + +/* +** basic types +*/ +#define LUA_TNONE (-1) + +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_TFUNCTION 6 +#define LUA_TUSERDATA 7 +#define LUA_TTHREAD 8 + +#define LUA_NUMTAGS 9 + + + +/* minimum Lua stack available to a C function */ +#define LUA_MINSTACK 20 + + +/* predefined values in the registry */ +#define LUA_RIDX_MAINTHREAD 1 +#define LUA_RIDX_GLOBALS 2 +#define LUA_RIDX_LAST LUA_RIDX_GLOBALS + + +/* type of numbers in Lua */ +typedef LUA_NUMBER lua_Number; + + +/* type for integer functions */ +typedef LUA_INTEGER lua_Integer; + +/* unsigned integer type */ +typedef LUA_UNSIGNED lua_Unsigned; + + + +/* +** generic extra include file +*/ +#if defined(LUA_USER_H) +#include LUA_USER_H +#endif + + +/* +** RCS ident string +*/ +extern const char lua_ident[]; + + +/* +** state manipulation +*/ +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API void (lua_close) (lua_State *L); +LUA_API lua_State *(lua_newthread) (lua_State *L); + +LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); + + +LUA_API const lua_Number *(lua_version) (lua_State *L); + + +/* +** basic stack manipulation +*/ +LUA_API int (lua_absindex) (lua_State *L, int idx); +LUA_API int (lua_gettop) (lua_State *L); +LUA_API void (lua_settop) (lua_State *L, int idx); +LUA_API void (lua_pushvalue) (lua_State *L, int idx); +LUA_API void (lua_remove) (lua_State *L, int idx); +LUA_API void (lua_insert) (lua_State *L, int idx); +LUA_API void (lua_replace) (lua_State *L, int idx); +LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); +LUA_API int (lua_checkstack) (lua_State *L, int sz); + +LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); + + +/* +** access functions (stack -> C) +*/ + +LUA_API int (lua_isnumber) (lua_State *L, int idx); +LUA_API int (lua_isstring) (lua_State *L, int idx); +LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isuserdata) (lua_State *L, int idx); +LUA_API int (lua_type) (lua_State *L, int idx); +LUA_API const char *(lua_typename) (lua_State *L, int tp); + +LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); +LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); +LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *isnum); +LUA_API int (lua_toboolean) (lua_State *L, int idx); +LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); +LUA_API size_t (lua_rawlen) (lua_State *L, int idx); +LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); +LUA_API void *(lua_touserdata) (lua_State *L, int idx); +LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); +LUA_API const void *(lua_topointer) (lua_State *L, int idx); + + +/* +** Comparison and arithmetic functions +*/ + +#define LUA_OPADD 0 /* ORDER TM */ +#define LUA_OPSUB 1 +#define LUA_OPMUL 2 +#define LUA_OPDIV 3 +#define LUA_OPMOD 4 +#define LUA_OPPOW 5 +#define LUA_OPUNM 6 + +LUA_API void (lua_arith) (lua_State *L, int op); + +#define LUA_OPEQ 0 +#define LUA_OPLT 1 +#define LUA_OPLE 2 + +LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); + + +/* +** push functions (C -> stack) +*/ +LUA_API void (lua_pushnil) (lua_State *L); +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); +LUA_API void (lua_pushunsigned) (lua_State *L, lua_Unsigned n); +LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l); +LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); +LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, + va_list argp); +LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); +LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); +LUA_API void (lua_pushboolean) (lua_State *L, int b); +LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); +LUA_API int (lua_pushthread) (lua_State *L); + + +/* +** get functions (Lua -> stack) +*/ +LUA_API void (lua_getglobal) (lua_State *L, const char *var); +LUA_API void (lua_gettable) (lua_State *L, int idx); +LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawget) (lua_State *L, int idx); +LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); +LUA_API void (lua_rawgetp) (lua_State *L, int idx, const void *p); +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); +LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); +LUA_API int (lua_getmetatable) (lua_State *L, int objindex); +LUA_API void (lua_getuservalue) (lua_State *L, int idx); + + +/* +** set functions (stack -> Lua) +*/ +LUA_API void (lua_setglobal) (lua_State *L, const char *var); +LUA_API void (lua_settable) (lua_State *L, int idx); +LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawset) (lua_State *L, int idx); +LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); +LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); +LUA_API int (lua_setmetatable) (lua_State *L, int objindex); +LUA_API void (lua_setuservalue) (lua_State *L, int idx); + + +/* +** 'load' and 'call' functions (load and run Lua code) +*/ +LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx, + lua_CFunction k); +#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) + +LUA_API int (lua_getctx) (lua_State *L, int *ctx); + +LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, + int ctx, lua_CFunction k); +#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) + +LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname, + const char *mode); + +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); + + +/* +** coroutine functions +*/ +LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx, + lua_CFunction k); +#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) +LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); +LUA_API int (lua_status) (lua_State *L); + +/* +** garbage-collection function and options +*/ + +#define LUA_GCSTOP 0 +#define LUA_GCRESTART 1 +#define LUA_GCCOLLECT 2 +#define LUA_GCCOUNT 3 +#define LUA_GCCOUNTB 4 +#define LUA_GCSTEP 5 +#define LUA_GCSETPAUSE 6 +#define LUA_GCSETSTEPMUL 7 +#define LUA_GCSETMAJORINC 8 +#define LUA_GCISRUNNING 9 +#define LUA_GCGEN 10 +#define LUA_GCINC 11 + +LUA_API int (lua_gc) (lua_State *L, int what, int data); + + +/* +** miscellaneous functions +*/ + +LUA_API int (lua_error) (lua_State *L); + +LUA_API int (lua_next) (lua_State *L, int idx); + +LUA_API void (lua_concat) (lua_State *L, int n); +LUA_API void (lua_len) (lua_State *L, int idx); + +LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); +LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL) +#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL) +#define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL) + +#define lua_pop(L,n) lua_settop(L, -(n)-1) + +#define lua_newtable(L) lua_createtable(L, 0, 0) + +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) + +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) + +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) + +#define lua_pushliteral(L, s) \ + lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) + +#define lua_pushglobaltable(L) \ + lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS) + +#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) + + + +/* +** {====================================================================== +** Debug API +** ======================================================================= +*/ + + +/* +** Event codes +*/ +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILCALL 4 + + +/* +** Event masks +*/ +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +typedef struct lua_Debug lua_Debug; /* activation record */ + + +/* Functions to be called by the debugger in specific events */ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); + + +LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); +LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); +LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); +LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); + +LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); +LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, + int fidx2, int n2); + +LUA_API int (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); +LUA_API lua_Hook (lua_gethook) (lua_State *L); +LUA_API int (lua_gethookmask) (lua_State *L); +LUA_API int (lua_gethookcount) (lua_State *L); + + +struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ + const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ + const char *source; /* (S) */ + int currentline; /* (l) */ + int linedefined; /* (S) */ + int lastlinedefined; /* (S) */ + unsigned char nups; /* (u) number of upvalues */ + unsigned char nparams;/* (u) number of parameters */ + char isvararg; /* (u) */ + char istailcall; /* (t) */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + struct CallInfo *i_ci; /* active function */ +}; + +/* }====================================================================== */ + + +/****************************************************************************** +* Copyright (C) 1994-2013 Lua.org, PUC-Rio. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + + +#endif diff --git a/golua/lua/lua52/luaconf.h b/golua/lua/lua52/luaconf.h new file mode 100644 index 0000000..df802c9 --- /dev/null +++ b/golua/lua/lua52/luaconf.h @@ -0,0 +1,551 @@ +/* +** $Id: luaconf.h,v 1.176 2013/03/16 21:10:18 roberto Exp $ +** Configuration file for Lua +** See Copyright Notice in lua.h +*/ + + +#ifndef lconfig_h +#define lconfig_h + +#include +#include + + +/* +** ================================================================== +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + + +/* +@@ LUA_ANSI controls the use of non-ansi features. +** CHANGE it (define it) if you want Lua to avoid the use of any +** non-ansi feature or library. +*/ +#if !defined(LUA_ANSI) && defined(__STRICT_ANSI__) +#define LUA_ANSI +#endif + + +#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE) +#define LUA_WIN /* enable goodies for regular Windows platforms */ +#endif + +#if defined(LUA_WIN) +#define LUA_DL_DLL +#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ +#endif + + + +#if defined(LUA_USE_LINUX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#define LUA_USE_READLINE /* needs some extra libraries */ +#define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */ +#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ +#define LUA_USE_LONGLONG /* assume support for long long */ +#endif + +#if defined(LUA_USE_MACOSX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* does not need -ldl */ +#define LUA_USE_READLINE /* needs an extra library: -lreadline */ +#define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */ +#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ +#define LUA_USE_LONGLONG /* assume support for long long */ +#endif + + + +/* +@@ LUA_USE_POSIX includes all functionality listed as X/Open System +@* Interfaces Extension (XSI). +** CHANGE it (define it) if your system is XSI compatible. +*/ +#if defined(LUA_USE_POSIX) +#define LUA_USE_MKSTEMP +#define LUA_USE_ISATTY +#define LUA_USE_POPEN +#define LUA_USE_ULONGJMP +#define LUA_USE_GMTIME_R +#endif + + + +/* +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for +@* Lua libraries. +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for +@* C libraries. +** CHANGE them if your machine has a non-conventional directory +** hierarchy or if you want to install your libraries in +** non-conventional directories. +*/ +#if defined(_WIN32) /* { */ +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" ".\\?.lua" +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll;" ".\\?.dll" + +#else /* }{ */ + +#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/" +#define LUA_ROOT "/usr/local/" +#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR +#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" "./?.lua" +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif /* } */ + + +/* +@@ LUA_DIRSEP is the directory separator (for submodules). +** CHANGE it if your machine does not use "/" as the directory separator +** and is not Windows. (On Windows Lua automatically uses "\".) +*/ +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + + +/* +@@ LUA_ENV is the name of the variable that holds the current +@@ environment, used to access global names. +** CHANGE it if you do not like this name. +*/ +#define LUA_ENV "_ENV" + + +/* +@@ LUA_API is a mark for all core API functions. +@@ LUALIB_API is a mark for all auxiliary library functions. +@@ LUAMOD_API is a mark for all standard library opening functions. +** CHANGE them if you need to define those functions in some special way. +** For instance, if you want to create one Windows DLL with the core and +** the libraries, you may want to use the following definition (define +** LUA_BUILD_AS_DLL to get it). +*/ +#if defined(LUA_BUILD_AS_DLL) /* { */ + +#if defined(LUA_CORE) || defined(LUA_LIB) /* { */ +#define LUA_API __declspec(dllexport) +#else /* }{ */ +#define LUA_API __declspec(dllimport) +#endif /* } */ + +#else /* }{ */ + +#define LUA_API extern + +#endif /* } */ + + +/* more often than not the libs go together with the core */ +#define LUALIB_API LUA_API +#define LUAMOD_API LUALIB_API + + +/* +@@ LUAI_FUNC is a mark for all extern functions that are not to be +@* exported to outside modules. +@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables +@* that are not to be exported to outside modules (LUAI_DDEF for +@* definitions and LUAI_DDEC for declarations). +** CHANGE them if you need to mark them in some special way. Elf/gcc +** (versions 3.2 and later) mark them as "hidden" to optimize access +** when Lua is compiled as a shared library. Not all elf targets support +** this attribute. Unfortunately, gcc does not offer a way to check +** whether the target offers that support, and those without support +** give a warning about it. To avoid these warnings, change to the +** default definition. +*/ +#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + defined(__ELF__) /* { */ +#define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#define LUAI_DDEC LUAI_FUNC +#define LUAI_DDEF /* empty */ + +#else /* }{ */ +#define LUAI_FUNC extern +#define LUAI_DDEC extern +#define LUAI_DDEF /* empty */ +#endif /* } */ + + + +/* +@@ LUA_QL describes how error messages quote program elements. +** CHANGE it if you want a different appearance. +*/ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + + +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@* of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 + + +/* +@@ luai_writestring/luai_writeline define how 'print' prints its results. +** They are only used in libraries and the stand-alone program. (The #if +** avoids including 'stdio.h' everywhere.) +*/ +#if defined(LUA_LIB) || defined(lua_c) +#include +#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) +#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout)) +#endif + +/* +@@ luai_writestringerror defines how to print error messages. +** (A format string with one argument is enough for Lua...) +*/ +#define luai_writestringerror(s,p) \ + (fprintf(stderr, (s), (p)), fflush(stderr)) + + +/* +@@ LUAI_MAXSHORTLEN is the maximum length for short strings, that is, +** strings that are internalized. (Cannot be smaller than reserved words +** or tags for metamethods, as these strings must be internalized; +** #("function") = 8, #("__newindex") = 10.) +*/ +#define LUAI_MAXSHORTLEN 40 + + + +/* +** {================================================================== +** Compatibility with previous versions +** =================================================================== +*/ + +/* +@@ LUA_COMPAT_ALL controls all compatibility options. +** You can define it to get all options, or change specific options +** to fit your specific needs. +*/ +#if defined(LUA_COMPAT_ALL) /* { */ + +/* +@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. +** You can replace it with 'table.unpack'. +*/ +#define LUA_COMPAT_UNPACK + +/* +@@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'. +** You can replace it with 'package.searchers'. +*/ +#define LUA_COMPAT_LOADERS + +/* +@@ macro 'lua_cpcall' emulates deprecated function lua_cpcall. +** You can call your C function directly (with light C functions). +*/ +#define lua_cpcall(L,f,u) \ + (lua_pushcfunction(L, (f)), \ + lua_pushlightuserdata(L,(u)), \ + lua_pcall(L,1,0,0)) + + +/* +@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. +** You can rewrite 'log10(x)' as 'log(x, 10)'. +*/ +#define LUA_COMPAT_LOG10 + +/* +@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base +** library. You can rewrite 'loadstring(s)' as 'load(s)'. +*/ +#define LUA_COMPAT_LOADSTRING + +/* +@@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library. +*/ +#define LUA_COMPAT_MAXN + +/* +@@ The following macros supply trivial compatibility for some +** changes in the API. The macros themselves document how to +** change your code to avoid using them. +*/ +#define lua_strlen(L,i) lua_rawlen(L, (i)) + +#define lua_objlen(L,i) lua_rawlen(L, (i)) + +#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) +#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) + +/* +@@ LUA_COMPAT_MODULE controls compatibility with previous +** module functions 'module' (Lua) and 'luaL_register' (C). +*/ +#define LUA_COMPAT_MODULE + +#endif /* } */ + +/* }================================================================== */ + + + +/* +@@ LUAI_BITSINT defines the number of bits in an int. +** CHANGE here if Lua cannot automatically detect the number of bits of +** your machine. Probably you do not need to change this. +*/ +/* avoid overflows in comparison */ +#if INT_MAX-20 < 32760 /* { */ +#define LUAI_BITSINT 16 +#elif INT_MAX > 2147483640L /* }{ */ +/* int has at least 32 bits */ +#define LUAI_BITSINT 32 +#else /* }{ */ +#error "you must define LUA_BITSINT with number of bits in an integer" +#endif /* } */ + + +/* +@@ LUA_INT32 is an signed integer with exactly 32 bits. +@@ LUAI_UMEM is an unsigned integer big enough to count the total +@* memory used by Lua. +@@ LUAI_MEM is a signed integer big enough to count the total memory +@* used by Lua. +** CHANGE here if for some weird reason the default definitions are not +** good enough for your machine. Probably you do not need to change +** this. +*/ +#if LUAI_BITSINT >= 32 /* { */ +#define LUA_INT32 int +#define LUAI_UMEM size_t +#define LUAI_MEM ptrdiff_t +#else /* }{ */ +/* 16-bit ints */ +#define LUA_INT32 long +#define LUAI_UMEM unsigned long +#define LUAI_MEM long +#endif /* } */ + + +/* +@@ LUAI_MAXSTACK limits the size of the Lua stack. +** CHANGE it if you need a different limit. This limit is arbitrary; +** its only purpose is to stop Lua to consume unlimited stack +** space (and to reserve some numbers for pseudo-indices). +*/ +#if LUAI_BITSINT >= 32 +#define LUAI_MAXSTACK 1000000 +#else +#define LUAI_MAXSTACK 15000 +#endif + +/* reserve some space for error handling */ +#define LUAI_FIRSTPSEUDOIDX (-LUAI_MAXSTACK - 1000) + + + + +/* +@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +** CHANGE it if it uses too much C-stack space. +*/ +#define LUAL_BUFFERSIZE BUFSIZ + + + + +/* +** {================================================================== +@@ LUA_NUMBER is the type of numbers in Lua. +** CHANGE the following definitions only if you want to build Lua +** with a number type different from double. You may also need to +** change lua_number2int & lua_number2integer. +** =================================================================== +*/ + +#define LUA_NUMBER_DOUBLE +#define LUA_NUMBER double + +/* +@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' +@* over a number. +*/ +#define LUAI_UACNUMBER double + + +/* +@@ LUA_NUMBER_SCAN is the format for reading numbers. +@@ LUA_NUMBER_FMT is the format for writing numbers. +@@ lua_number2str converts a number to a string. +@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. +*/ +#define LUA_NUMBER_SCAN "%lf" +#define LUA_NUMBER_FMT "%.14g" +#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) +#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ + + +/* +@@ l_mathop allows the addition of an 'l' or 'f' to all math operations +*/ +#define l_mathop(x) (x) + + +/* +@@ lua_str2number converts a decimal numeric string to a number. +@@ lua_strx2number converts an hexadecimal numeric string to a number. +** In C99, 'strtod' does both conversions. C89, however, has no function +** to convert floating hexadecimal strings to numbers. For these +** systems, you can leave 'lua_strx2number' undefined and Lua will +** provide its own implementation. +*/ +#define lua_str2number(s,p) strtod((s), (p)) + +#if defined(LUA_USE_STRTODHEX) +#define lua_strx2number(s,p) strtod((s), (p)) +#endif + + +/* +@@ The luai_num* macros define the primitive operations over numbers. +*/ + +/* the following operations need the math library */ +#if defined(lobject_c) || defined(lvm_c) +#include +#define luai_nummod(L,a,b) ((a) - l_mathop(floor)((a)/(b))*(b)) +#define luai_numpow(L,a,b) (l_mathop(pow)(a,b)) +#endif + +/* these are quite standard operations */ +#if defined(LUA_CORE) +#define luai_numadd(L,a,b) ((a)+(b)) +#define luai_numsub(L,a,b) ((a)-(b)) +#define luai_nummul(L,a,b) ((a)*(b)) +#define luai_numdiv(L,a,b) ((a)/(b)) +#define luai_numunm(L,a) (-(a)) +#define luai_numeq(a,b) ((a)==(b)) +#define luai_numlt(L,a,b) ((a)<(b)) +#define luai_numle(L,a,b) ((a)<=(b)) +#define luai_numisnan(L,a) (!luai_numeq((a), (a))) +#endif + + + +/* +@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. +** CHANGE that if ptrdiff_t is not adequate on your machine. (On most +** machines, ptrdiff_t gives a good choice between int or long.) +*/ +#define LUA_INTEGER ptrdiff_t + +/* +@@ LUA_UNSIGNED is the integral type used by lua_pushunsigned/lua_tounsigned. +** It must have at least 32 bits. +*/ +#define LUA_UNSIGNED unsigned LUA_INT32 + + + +/* +** Some tricks with doubles +*/ + +#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */ +/* +** The next definitions activate some tricks to speed up the +** conversion from doubles to integer types, mainly to LUA_UNSIGNED. +** +@@ LUA_MSASMTRICK uses Microsoft assembler to avoid clashes with a +** DirectX idiosyncrasy. +** +@@ LUA_IEEE754TRICK uses a trick that should work on any machine +** using IEEE754 with a 32-bit integer type. +** +@@ LUA_IEEELL extends the trick to LUA_INTEGER; should only be +** defined when LUA_INTEGER is a 32-bit integer. +** +@@ LUA_IEEEENDIAN is the endianness of doubles in your machine +** (0 for little endian, 1 for big endian); if not defined, Lua will +** check it dynamically for LUA_IEEE754TRICK (but not for LUA_NANTRICK). +** +@@ LUA_NANTRICK controls the use of a trick to pack all types into +** a single double value, using NaN values to represent non-number +** values. The trick only works on 32-bit machines (ints and pointers +** are 32-bit values) with numbers represented as IEEE 754-2008 doubles +** with conventional endianess (12345678 or 87654321), in CPUs that do +** not produce signaling NaN values (all NaNs are quiet). +*/ + +/* Microsoft compiler on a Pentium (32 bit) ? */ +#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */ + +#define LUA_MSASMTRICK +#define LUA_IEEEENDIAN 0 +#define LUA_NANTRICK + + +/* pentium 32 bits? */ +#elif defined(__i386__) || defined(__i386) || defined(__X86__) /* }{ */ + +#define LUA_IEEE754TRICK +#define LUA_IEEELL +#define LUA_IEEEENDIAN 0 +#define LUA_NANTRICK + +/* pentium 64 bits? */ +#elif defined(__x86_64) /* }{ */ + +#define LUA_IEEE754TRICK +#define LUA_IEEEENDIAN 0 + +#elif defined(__POWERPC__) || defined(__ppc__) /* }{ */ + +#define LUA_IEEE754TRICK +#define LUA_IEEEENDIAN 1 + +#else /* }{ */ + +/* assume IEEE754 and a 32-bit integer type */ +#define LUA_IEEE754TRICK + +#endif /* } */ + +#endif /* } */ + +/* }================================================================== */ + + + + +/* =================================================================== */ + +/* +** Local configuration. You can use this space to add your redefinitions +** without modifying the main part of the file. +*/ + + + +#endif + diff --git a/golua/lua/lua52/lualib.h b/golua/lua/lua52/lualib.h new file mode 100644 index 0000000..9fd126b --- /dev/null +++ b/golua/lua/lua52/lualib.h @@ -0,0 +1,55 @@ +/* +** $Id: lualib.h,v 1.43 2011/12/08 12:11:37 roberto Exp $ +** Lua standard libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lualib_h +#define lualib_h + +#include "lua.h" + + + +LUAMOD_API int (luaopen_base) (lua_State *L); + +#define LUA_COLIBNAME "coroutine" +LUAMOD_API int (luaopen_coroutine) (lua_State *L); + +#define LUA_TABLIBNAME "table" +LUAMOD_API int (luaopen_table) (lua_State *L); + +#define LUA_IOLIBNAME "io" +LUAMOD_API int (luaopen_io) (lua_State *L); + +#define LUA_OSLIBNAME "os" +LUAMOD_API int (luaopen_os) (lua_State *L); + +#define LUA_STRLIBNAME "string" +LUAMOD_API int (luaopen_string) (lua_State *L); + +#define LUA_BITLIBNAME "bit32" +LUAMOD_API int (luaopen_bit32) (lua_State *L); + +#define LUA_MATHLIBNAME "math" +LUAMOD_API int (luaopen_math) (lua_State *L); + +#define LUA_DBLIBNAME "debug" +LUAMOD_API int (luaopen_debug) (lua_State *L); + +#define LUA_LOADLIBNAME "package" +LUAMOD_API int (luaopen_package) (lua_State *L); + + +/* open all previous libraries */ +LUALIB_API void (luaL_openlibs) (lua_State *L); + + + +#if !defined(lua_assert) +#define lua_assert(x) ((void)0) +#endif + + +#endif diff --git a/golua/lua/lua53/dummy.go b/golua/lua/lua53/dummy.go new file mode 100644 index 0000000..651a797 --- /dev/null +++ b/golua/lua/lua53/dummy.go @@ -0,0 +1,7 @@ +// +build dummy + +// Package lua contains only a C header files. +// +// This Go file is part of a workaround for `go mod vendor`. +// Please see the file dummy.go in the parent directory for more information. +package lua diff --git a/golua/lua/lua53/lauxlib.h b/golua/lua/lua53/lauxlib.h new file mode 100644 index 0000000..0bac246 --- /dev/null +++ b/golua/lua/lua53/lauxlib.h @@ -0,0 +1,256 @@ +/* +** $Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lauxlib_h +#define lauxlib_h + + +#include +#include + +#include "lua.h" + + + +/* extra error code for 'luaL_load' */ +#define LUA_ERRFILE (LUA_ERRERR+1) + + +typedef struct luaL_Reg { + const char *name; + lua_CFunction func; +} luaL_Reg; + + +#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) + +LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); +#define luaL_checkversion(L) \ + luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) + +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); +LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); +LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, + size_t *l); +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, + const char *def, size_t *l); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); + +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, + lua_Integer def); + +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); +LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int arg); + +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); +LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); +LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); + +LUALIB_API void (luaL_where) (lua_State *L, int lvl); +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); + +LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, + const char *const lst[]); + +LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); +LUALIB_API int (luaL_execresult) (lua_State *L, int stat); + +/* pre-defined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +LUALIB_API int (luaL_ref) (lua_State *L, int t); +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); + +LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, + const char *mode); + +#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) + +LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, + const char *name, const char *mode); +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); + +LUALIB_API lua_State *(luaL_newstate) (void); + +LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); + +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, + const char *r); + +LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); + +LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); + +LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, + const char *msg, int level); + +LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, + lua_CFunction openf, int glb); + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + + +#define luaL_newlibtable(L,l) \ + lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) + +#define luaL_newlib(L,l) \ + (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) + +#define luaL_argcheck(L, cond,arg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) + +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) + +#define luaL_dofile(L, fn) \ + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_dostring(L, s) \ + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) + +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) + +#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) + + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + +typedef struct luaL_Buffer { + char *b; /* buffer address */ + size_t size; /* buffer size */ + size_t n; /* number of characters in buffer */ + lua_State *L; + char initb[LUAL_BUFFERSIZE]; /* initial buffer */ +} luaL_Buffer; + + +#define luaL_addchar(B,c) \ + ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ + ((B)->b[(B)->n++] = (c))) + +#define luaL_addsize(B,s) ((B)->n += (s)) + +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); +LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); +LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); + +#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) + +/* }====================================================== */ + + + +/* +** {====================================================== +** File handles for IO library +** ======================================================= +*/ + +/* +** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and +** initial structure 'luaL_Stream' (it may contain other fields +** after that initial structure). +*/ + +#define LUA_FILEHANDLE "FILE*" + + +typedef struct luaL_Stream { + FILE *f; /* stream (NULL for incompletely created streams) */ + lua_CFunction closef; /* to close stream (NULL for closed streams) */ +} luaL_Stream; + +/* }====================================================== */ + + + +/* compatibility with old module system */ +#if defined(LUA_COMPAT_MODULE) + +LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, + int sizehint); +LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, + const luaL_Reg *l, int nup); + +#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) + +#endif + + +/* +** {================================================================== +** "Abstraction Layer" for basic report of messages and errors +** =================================================================== +*/ + +/* print a string */ +#if !defined(lua_writestring) +#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) +#endif + +/* print a newline and flush the output */ +#if !defined(lua_writeline) +#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) +#endif + +/* print an error message */ +#if !defined(lua_writestringerror) +#define lua_writestringerror(s,p) \ + (fprintf(stderr, (s), (p)), fflush(stderr)) +#endif + +/* }================================================================== */ + + +/* +** {============================================================ +** Compatibility with deprecated conversions +** ============================================================= +*/ +#if defined(LUA_COMPAT_APIINTCASTS) + +#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) +#define luaL_optunsigned(L,a,d) \ + ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) + +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) + +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#endif +/* }============================================================ */ + + + +#endif + + diff --git a/golua/lua/lua53/lua.h b/golua/lua/lua53/lua.h new file mode 100644 index 0000000..6ae3112 --- /dev/null +++ b/golua/lua/lua53/lua.h @@ -0,0 +1,486 @@ +/* +** $Id: lua.h,v 1.328 2015/06/03 13:03:38 roberto Exp $ +** Lua - A Scripting Language +** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** See Copyright Notice at the end of this file +*/ + + +#ifndef lua_h +#define lua_h + +#include +#include + + +#include "luaconf.h" + + +#define LUA_VERSION_MAJOR "5" +#define LUA_VERSION_MINOR "3" +#define LUA_VERSION_NUM 503 +#define LUA_VERSION_RELEASE "1" + +#define LUA_VERSION "Lua 5.3" +#define LUA_RELEASE "Lua 5.3.1" +#define LUA_COPYRIGHT "Lua 5.3.1 Copyright (C) 1994-2015 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" + + +/* mark for precompiled code ('Lua') */ +#define LUA_SIGNATURE "\x1bLua" + +/* option for multiple returns in 'lua_pcall' and 'lua_call' */ +#define LUA_MULTRET (-1) + + +/* +** Pseudo-indices +** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty +** space after that to help overflow detection) +*/ +#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) +#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) + + +/* thread status */ +#define LUA_OK 0 +#define LUA_YIELD 1 +#define LUA_ERRRUN 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRGCMM 5 +#define LUA_ERRERR 6 + + +typedef struct lua_State lua_State; + + +/* +** basic types +*/ +#define LUA_TNONE (-1) + +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_TFUNCTION 6 +#define LUA_TUSERDATA 7 +#define LUA_TTHREAD 8 + +#define LUA_NUMTAGS 9 + + + +/* minimum Lua stack available to a C function */ +#define LUA_MINSTACK 20 + + +/* predefined values in the registry */ +#define LUA_RIDX_MAINTHREAD 1 +#define LUA_RIDX_GLOBALS 2 +#define LUA_RIDX_LAST LUA_RIDX_GLOBALS + + +/* type of numbers in Lua */ +typedef LUA_NUMBER lua_Number; + + +/* type for integer functions */ +typedef LUA_INTEGER lua_Integer; + +/* unsigned integer type */ +typedef LUA_UNSIGNED lua_Unsigned; + +/* type for continuation-function contexts */ +typedef LUA_KCONTEXT lua_KContext; + + +/* +** Type for C functions registered with Lua +*/ +typedef int (*lua_CFunction) (lua_State *L); + +/* +** Type for continuation functions +*/ +typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); + + +/* +** Type for functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); + + +/* +** Type for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + + + +/* +** generic extra include file +*/ +#if defined(LUA_USER_H) +#include LUA_USER_H +#endif + + +/* +** RCS ident string +*/ +extern const char lua_ident[]; + + +/* +** state manipulation +*/ +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API void (lua_close) (lua_State *L); +LUA_API lua_State *(lua_newthread) (lua_State *L); + +LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); + + +LUA_API const lua_Number *(lua_version) (lua_State *L); + + +/* +** basic stack manipulation +*/ +LUA_API int (lua_absindex) (lua_State *L, int idx); +LUA_API int (lua_gettop) (lua_State *L); +LUA_API void (lua_settop) (lua_State *L, int idx); +LUA_API void (lua_pushvalue) (lua_State *L, int idx); +LUA_API void (lua_rotate) (lua_State *L, int idx, int n); +LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); +LUA_API int (lua_checkstack) (lua_State *L, int n); + +LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); + + +/* +** access functions (stack -> C) +*/ + +LUA_API int (lua_isnumber) (lua_State *L, int idx); +LUA_API int (lua_isstring) (lua_State *L, int idx); +LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isinteger) (lua_State *L, int idx); +LUA_API int (lua_isuserdata) (lua_State *L, int idx); +LUA_API int (lua_type) (lua_State *L, int idx); +LUA_API const char *(lua_typename) (lua_State *L, int tp); + +LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); +LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); +LUA_API int (lua_toboolean) (lua_State *L, int idx); +LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); +LUA_API size_t (lua_rawlen) (lua_State *L, int idx); +LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); +LUA_API void *(lua_touserdata) (lua_State *L, int idx); +LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); +LUA_API const void *(lua_topointer) (lua_State *L, int idx); + + +/* +** Comparison and arithmetic functions +*/ + +#define LUA_OPADD 0 /* ORDER TM, ORDER OP */ +#define LUA_OPSUB 1 +#define LUA_OPMUL 2 +#define LUA_OPMOD 3 +#define LUA_OPPOW 4 +#define LUA_OPDIV 5 +#define LUA_OPIDIV 6 +#define LUA_OPBAND 7 +#define LUA_OPBOR 8 +#define LUA_OPBXOR 9 +#define LUA_OPSHL 10 +#define LUA_OPSHR 11 +#define LUA_OPUNM 12 +#define LUA_OPBNOT 13 + +LUA_API void (lua_arith) (lua_State *L, int op); + +#define LUA_OPEQ 0 +#define LUA_OPLT 1 +#define LUA_OPLE 2 + +LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); + + +/* +** push functions (C -> stack) +*/ +LUA_API void (lua_pushnil) (lua_State *L); +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); +LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); +LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); +LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, + va_list argp); +LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); +LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); +LUA_API void (lua_pushboolean) (lua_State *L, int b); +LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); +LUA_API int (lua_pushthread) (lua_State *L); + + +/* +** get functions (Lua -> stack) +*/ +LUA_API int (lua_getglobal) (lua_State *L, const char *name); +LUA_API int (lua_gettable) (lua_State *L, int idx); +LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); +LUA_API int (lua_rawget) (lua_State *L, int idx); +LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); +LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); + +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); +LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); +LUA_API int (lua_getmetatable) (lua_State *L, int objindex); +LUA_API int (lua_getuservalue) (lua_State *L, int idx); + + +/* +** set functions (stack -> Lua) +*/ +LUA_API void (lua_setglobal) (lua_State *L, const char *name); +LUA_API void (lua_settable) (lua_State *L, int idx); +LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); +LUA_API void (lua_rawset) (lua_State *L, int idx); +LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); +LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); +LUA_API int (lua_setmetatable) (lua_State *L, int objindex); +LUA_API void (lua_setuservalue) (lua_State *L, int idx); + + +/* +** 'load' and 'call' functions (load and run Lua code) +*/ +LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, + lua_KContext ctx, lua_KFunction k); +#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) + +LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, + lua_KContext ctx, lua_KFunction k); +#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) + +LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname, const char *mode); + +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); + + +/* +** coroutine functions +*/ +LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, + lua_KFunction k); +LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); +LUA_API int (lua_status) (lua_State *L); +LUA_API int (lua_isyieldable) (lua_State *L); + +#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) + + +/* +** garbage-collection function and options +*/ + +#define LUA_GCSTOP 0 +#define LUA_GCRESTART 1 +#define LUA_GCCOLLECT 2 +#define LUA_GCCOUNT 3 +#define LUA_GCCOUNTB 4 +#define LUA_GCSTEP 5 +#define LUA_GCSETPAUSE 6 +#define LUA_GCSETSTEPMUL 7 +#define LUA_GCISRUNNING 9 + +LUA_API int (lua_gc) (lua_State *L, int what, int data); + + +/* +** miscellaneous functions +*/ + +LUA_API int (lua_error) (lua_State *L); + +LUA_API int (lua_next) (lua_State *L, int idx); + +LUA_API void (lua_concat) (lua_State *L, int n); +LUA_API void (lua_len) (lua_State *L, int idx); + +LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); + +LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); +LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); + + + +/* +** {============================================================== +** some useful macros +** =============================================================== +*/ + +#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) + +#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) +#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) + +#define lua_pop(L,n) lua_settop(L, -(n)-1) + +#define lua_newtable(L) lua_createtable(L, 0, 0) + +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) + +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) + +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) + +#define lua_pushliteral(L, s) lua_pushstring(L, "" s) + +#define lua_pushglobaltable(L) \ + lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS) + +#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) + + +#define lua_insert(L,idx) lua_rotate(L, (idx), 1) + +#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) + +#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) + +/* }============================================================== */ + + +/* +** {============================================================== +** compatibility macros for unsigned conversions +** =============================================================== +*/ +#if defined(LUA_COMPAT_APIINTCASTS) + +#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) +#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) +#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) + +#endif +/* }============================================================== */ + +/* +** {====================================================================== +** Debug API +** ======================================================================= +*/ + + +/* +** Event codes +*/ +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILCALL 4 + + +/* +** Event masks +*/ +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +typedef struct lua_Debug lua_Debug; /* activation record */ + + +/* Functions to be called by the debugger in specific events */ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); + + +LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); +LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); +LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); +LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); + +LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); +LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, + int fidx2, int n2); + +LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); +LUA_API lua_Hook (lua_gethook) (lua_State *L); +LUA_API int (lua_gethookmask) (lua_State *L); +LUA_API int (lua_gethookcount) (lua_State *L); + + +struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ + const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ + const char *source; /* (S) */ + int currentline; /* (l) */ + int linedefined; /* (S) */ + int lastlinedefined; /* (S) */ + unsigned char nups; /* (u) number of upvalues */ + unsigned char nparams;/* (u) number of parameters */ + char isvararg; /* (u) */ + char istailcall; /* (t) */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + struct CallInfo *i_ci; /* active function */ +}; + +/* }====================================================================== */ + + +/****************************************************************************** +* Copyright (C) 1994-2015 Lua.org, PUC-Rio. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + + +#endif diff --git a/golua/lua/lua53/luaconf.h b/golua/lua/lua53/luaconf.h new file mode 100644 index 0000000..d1cdc48 --- /dev/null +++ b/golua/lua/lua53/luaconf.h @@ -0,0 +1,763 @@ +/* +** $Id: luaconf.h,v 1.251 2015/05/20 17:39:23 roberto Exp $ +** Configuration file for Lua +** See Copyright Notice in lua.h +*/ + + +#ifndef luaconf_h +#define luaconf_h + +#include +#include + + +/* +** =================================================================== +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + + +/* +** {==================================================================== +** System Configuration: macros to adapt (if needed) Lua to some +** particular platform, for instance compiling it with 32-bit numbers or +** restricting it to C89. +** ===================================================================== +*/ + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You +** can also define LUA_32BITS in the make file, but changing here you +** ensure that all software connected to Lua will be compiled with the +** same configuration. +*/ +/* #define LUA_32BITS */ + + +/* +@@ LUA_USE_C89 controls the use of non-ISO-C89 features. +** Define it if you want Lua to avoid the use of a few C99 features +** or Windows-specific features on Windows. +*/ +/* #define LUA_USE_C89 */ + + +/* +** By default, Lua on Windows use (some) specific Windows features +*/ +#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) +#define LUA_USE_WINDOWS /* enable goodies for regular Windows */ +#endif + + +#if defined(LUA_USE_WINDOWS) +#define LUA_DL_DLL /* enable support for DLL */ +#define LUA_USE_C89 /* broadly, Windows is C89 */ +#endif + + +#if defined(LUA_USE_LINUX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#define LUA_USE_READLINE /* needs some extra libraries */ +#endif + + +#if defined(LUA_USE_MACOSX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ +#define LUA_USE_READLINE /* needs an extra library: -lreadline */ +#endif + + +/* +@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for +** C89 ('long' and 'double'); Windows always has '__int64', so it does +** not need to use this case. +*/ +#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) +#define LUA_C89_NUMBERS +#endif + + + +/* +@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. +*/ +/* avoid undefined shifts */ +#if ((INT_MAX >> 15) >> 15) >= 1 +#define LUAI_BITSINT 32 +#else +/* 'int' always must have at least 16 bits */ +#define LUAI_BITSINT 16 +#endif + + +/* +@@ LUA_INT_TYPE defines the type for Lua integers. +@@ LUA_FLOAT_TYPE defines the type for Lua floats. +** Lua should work fine with any mix of these options (if supported +** by your C compiler). The usual configurations are 64-bit integers +** and 'double' (the default), 32-bit integers and 'float' (for +** restricted platforms), and 'long'/'double' (for C compilers not +** compliant with C99, which may not have support for 'long long'). +*/ + +/* predefined options for LUA_INT_TYPE */ +#define LUA_INT_INT 1 +#define LUA_INT_LONG 2 +#define LUA_INT_LONGLONG 3 + +/* predefined options for LUA_FLOAT_TYPE */ +#define LUA_FLOAT_FLOAT 1 +#define LUA_FLOAT_DOUBLE 2 +#define LUA_FLOAT_LONGDOUBLE 3 + +#if defined(LUA_32BITS) /* { */ +/* +** 32-bit integers and 'float' +*/ +#if LUAI_BITSINT >= 32 /* use 'int' if big enough */ +#define LUA_INT_TYPE LUA_INT_INT +#else /* otherwise use 'long' */ +#define LUA_INT_TYPE LUA_INT_LONG +#endif +#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT + +#elif defined(LUA_C89_NUMBERS) /* }{ */ +/* +** largest types available for C89 ('long' and 'double') +*/ +#define LUA_INT_TYPE LUA_INT_LONG +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE + +#endif /* } */ + + +/* +** default configuration for 64-bit Lua ('long long' and 'double') +*/ +#if !defined(LUA_INT_TYPE) +#define LUA_INT_TYPE LUA_INT_LONGLONG +#endif + +#if !defined(LUA_FLOAT_TYPE) +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE +#endif /* } */ + +/* }================================================================== */ + + + + +/* +** {================================================================== +** Configuration for Paths. +** =================================================================== +*/ + +/* +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for +** Lua libraries. +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for +** C libraries. +** CHANGE them if your machine has a non-conventional directory +** hierarchy or if you want to install your libraries in +** non-conventional directories. +*/ +#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#if defined(_WIN32) /* { */ +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ + LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ + ".\\?.lua;" ".\\?\\init.lua" +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.dll;" \ + LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ + LUA_CDIR"loadall.dll;" ".\\?.dll" + +#else /* }{ */ + +/* This defines DEB_HOST_MULTIARCH */ +//#include "lua5.3-deb-multiarch.h" + +#define LUA_ROOT "/usr/local/" +#define LUA_ROOT2 "/usr/" +#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" +#define LUA_LDIR2 LUA_ROOT2 "share/lua/" LUA_VDIR "/" +#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" +#define LUA_CDIR2 LUA_ROOT2 "lib/" DEB_HOST_MULTIARCH "/lua/" LUA_VDIR "/" +#define LUA_CDIR3 LUA_ROOT2 "lib/lua/" LUA_VDIR "/" + +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ + LUA_LDIR2"?.lua;" LUA_LDIR2"?/init.lua;" \ + "./?.lua;" "./?/init.lua" +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.so;" LUA_CDIR2"?.so;" LUA_CDIR3"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif /* } */ + + +/* +@@ LUA_DIRSEP is the directory separator (for submodules). +** CHANGE it if your machine does not use "/" as the directory separator +** and is not Windows. (On Windows Lua automatically uses "\".) +*/ +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Marks for exported symbols in the C code +** =================================================================== +*/ + +/* +@@ LUA_API is a mark for all core API functions. +@@ LUALIB_API is a mark for all auxiliary library functions. +@@ LUAMOD_API is a mark for all standard library opening functions. +** CHANGE them if you need to define those functions in some special way. +** For instance, if you want to create one Windows DLL with the core and +** the libraries, you may want to use the following definition (define +** LUA_BUILD_AS_DLL to get it). +*/ +#if defined(LUA_BUILD_AS_DLL) /* { */ + +#if defined(LUA_CORE) || defined(LUA_LIB) /* { */ +#define LUA_API __declspec(dllexport) +#else /* }{ */ +#define LUA_API __declspec(dllimport) +#endif /* } */ + +#else /* }{ */ + +#ifdef __cplusplus +#define LUA_API extern "C" +#else +#define LUA_API extern +#endif + +#endif /* } */ + + +/* more often than not the libs go together with the core */ +#define LUALIB_API LUA_API +#define LUAMOD_API LUALIB_API + + +/* +@@ LUAI_FUNC is a mark for all extern functions that are not to be +** exported to outside modules. +@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables +** that are not to be exported to outside modules (LUAI_DDEF for +** definitions and LUAI_DDEC for declarations). +** CHANGE them if you need to mark them in some special way. Elf/gcc +** (versions 3.2 and later) mark them as "hidden" to optimize access +** when Lua is compiled as a shared library. Not all elf targets support +** this attribute. Unfortunately, gcc does not offer a way to check +** whether the target offers that support, and those without support +** give a warning about it. To avoid these warnings, change to the +** default definition. +*/ +#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + defined(__ELF__) /* { */ +#define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#else /* }{ */ +#define LUAI_FUNC extern +#endif /* } */ + +#define LUAI_DDEC LUAI_FUNC +#define LUAI_DDEF /* empty */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Compatibility with previous versions +** =================================================================== +*/ + +/* +@@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2. +@@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1. +** You can define it to get all options, or change specific options +** to fit your specific needs. +*/ +#if defined(LUA_COMPAT_5_2) /* { */ + +/* +@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated +** functions in the mathematical library. +*/ +#define LUA_COMPAT_MATHLIB + +/* +@@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'. +*/ +#define LUA_COMPAT_BITLIB + +/* +@@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod. +*/ +#define LUA_COMPAT_IPAIRS + +/* +@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for +** manipulating other integer types (lua_pushunsigned, lua_tounsigned, +** luaL_checkint, luaL_checklong, etc.) +*/ +#define LUA_COMPAT_APIINTCASTS + +#endif /* } */ + + +#if defined(LUA_COMPAT_5_1) /* { */ + +/* Incompatibilities from 5.2 -> 5.3 */ +#define LUA_COMPAT_MATHLIB +#define LUA_COMPAT_APIINTCASTS + +/* +@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. +** You can replace it with 'table.unpack'. +*/ +#define LUA_COMPAT_UNPACK + +/* +@@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'. +** You can replace it with 'package.searchers'. +*/ +#define LUA_COMPAT_LOADERS + +/* +@@ macro 'lua_cpcall' emulates deprecated function lua_cpcall. +** You can call your C function directly (with light C functions). +*/ +#define lua_cpcall(L,f,u) \ + (lua_pushcfunction(L, (f)), \ + lua_pushlightuserdata(L,(u)), \ + lua_pcall(L,1,0,0)) + + +/* +@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. +** You can rewrite 'log10(x)' as 'log(x, 10)'. +*/ +#define LUA_COMPAT_LOG10 + +/* +@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base +** library. You can rewrite 'loadstring(s)' as 'load(s)'. +*/ +#define LUA_COMPAT_LOADSTRING + +/* +@@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library. +*/ +#define LUA_COMPAT_MAXN + +/* +@@ The following macros supply trivial compatibility for some +** changes in the API. The macros themselves document how to +** change your code to avoid using them. +*/ +#define lua_strlen(L,i) lua_rawlen(L, (i)) + +#define lua_objlen(L,i) lua_rawlen(L, (i)) + +#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) +#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) + +/* +@@ LUA_COMPAT_MODULE controls compatibility with previous +** module functions 'module' (Lua) and 'luaL_register' (C). +*/ +#define LUA_COMPAT_MODULE + +#endif /* } */ + + +/* +@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a +@@ a float mark ('.0'). +** This macro is not on by default even in compatibility mode, +** because this is not really an incompatibility. +*/ +/* #define LUA_COMPAT_FLOATSTRING */ + +/* }================================================================== */ + + + +/* +** {================================================================== +** Configuration for Numbers. +** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* +** satisfy your needs. +** =================================================================== +*/ + +/* +@@ LUA_NUMBER is the floating-point type used by Lua. +@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' +@@ over a floating number. +@@ l_mathlim(x) corrects limit name 'x' to the proper float type +** by prefixing it with one of FLT/DBL/LDBL. +@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. +@@ LUA_NUMBER_FMT is the format for writing floats. +@@ lua_number2str converts a float to a string. +@@ l_mathop allows the addition of an 'l' or 'f' to all math operations. +@@ lua_str2number converts a decimal numeric string to a number. +*/ + +#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ + +#define LUA_NUMBER float + +#define l_mathlim(n) (FLT_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.7g" + +#define l_mathop(op) op##f + +#define lua_str2number(s,p) strtof((s), (p)) + + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ + +#define LUA_NUMBER long double + +#define l_mathlim(n) (LDBL_##n) + +#define LUAI_UACNUMBER long double + +#define LUA_NUMBER_FRMLEN "L" +#define LUA_NUMBER_FMT "%.19Lg" + +#define l_mathop(op) op##l + +#define lua_str2number(s,p) strtold((s), (p)) + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ + +#define LUA_NUMBER double + +#define l_mathlim(n) (DBL_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.14g" + +#define l_mathop(op) op + +#define lua_str2number(s,p) strtod((s), (p)) + +#else /* }{ */ + +#error "numeric float type not defined" + +#endif /* } */ + + +#define l_floor(x) (l_mathop(floor)(x)) + +#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) + + +/* +@@ lua_numbertointeger converts a float number to an integer, or +** returns 0 if float is not within the range of a lua_Integer. +** (The range comparisons are tricky because of rounding. The tests +** here assume a two-complement representation, where MININTEGER always +** has an exact representation as a float; MAXINTEGER may not have one, +** and therefore its conversion to float may have an ill-defined value.) +*/ +#define lua_numbertointeger(n,p) \ + ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ + (*(p) = (LUA_INTEGER)(n), 1)) + + + +/* +@@ LUA_INTEGER is the integer type used by Lua. +** +@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. +** +@@ LUAI_UACINT is the result of an 'usual argument conversion' +@@ over a lUA_INTEGER. +@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. +@@ LUA_INTEGER_FMT is the format for writing integers. +@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. +@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. +@@ lua_integer2str converts an integer to a string. +*/ + + +/* The following definitions are good for most cases here */ + +#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" +#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n)) + +#define LUAI_UACINT LUA_INTEGER + +/* +** use LUAI_UACINT here to avoid problems with promotions (which +** can turn a comparison between unsigneds into a signed comparison) +*/ +#define LUA_UNSIGNED unsigned LUAI_UACINT + + +/* now the variable definitions */ + +#if LUA_INT_TYPE == LUA_INT_INT /* { int */ + +#define LUA_INTEGER int +#define LUA_INTEGER_FRMLEN "" + +#define LUA_MAXINTEGER INT_MAX +#define LUA_MININTEGER INT_MIN + +#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ + +#define LUA_INTEGER long +#define LUA_INTEGER_FRMLEN "l" + +#define LUA_MAXINTEGER LONG_MAX +#define LUA_MININTEGER LONG_MIN + +#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ + +#if defined(LLONG_MAX) /* { */ +/* use ISO C99 stuff */ + +#define LUA_INTEGER long long +#define LUA_INTEGER_FRMLEN "ll" + +#define LUA_MAXINTEGER LLONG_MAX +#define LUA_MININTEGER LLONG_MIN + +#elif defined(LUA_USE_WINDOWS) /* }{ */ +/* in Windows, can use specific Windows types */ + +#define LUA_INTEGER __int64 +#define LUA_INTEGER_FRMLEN "I64" + +#define LUA_MAXINTEGER _I64_MAX +#define LUA_MININTEGER _I64_MIN + +#else /* }{ */ + +#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ + or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" + +#endif /* } */ + +#else /* }{ */ + +#error "numeric integer type not defined" + +#endif /* } */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Dependencies with C99 and other C details +** =================================================================== +*/ + +/* +@@ lua_strx2number converts an hexadecimal numeric string to a number. +** In C99, 'strtod' does that conversion. Otherwise, you can +** leave 'lua_strx2number' undefined and Lua will provide its own +** implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_strx2number(s,p) lua_str2number(s,p) +#endif + + +/* +@@ lua_number2strx converts a float to an hexadecimal numeric string. +** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. +** Otherwise, you can leave 'lua_number2strx' undefined and Lua will +** provide its own implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_number2strx(L,b,f,n) sprintf(b,f,n) +#endif + + +/* +** 'strtof' and 'opf' variants for math functions are not valid in +** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the +** availability of these variants. ('math.h' is already included in +** all files that use these macros.) +*/ +#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) +#undef l_mathop /* variants not available */ +#undef lua_str2number +#define l_mathop(op) (lua_Number)op /* no variant */ +#define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) +#endif + + +/* +@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation +** functions. It must be a numerical type; Lua will use 'intptr_t' if +** available, otherwise it will use 'ptrdiff_t' (the nearest thing to +** 'intptr_t' in C89) +*/ +#define LUA_KCONTEXT ptrdiff_t + +#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ + __STDC_VERSION__ >= 199901L +#include +#if defined(INTPTR_MAX) /* even in C99 this type is optional */ +#undef LUA_KCONTEXT +#define LUA_KCONTEXT intptr_t +#endif +#endif + + +/* +@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). +** Change that if you do not want to use C locales. (Code using this +** macro must include header 'locale.h'.) +*/ +#if !defined(lua_getlocaledecpoint) +#define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Language Variations +** ===================================================================== +*/ + +/* +@@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some +** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from +** numbers to strings. Define LUA_NOCVTS2N to turn off automatic +** coercion from strings to numbers. +*/ +/* #define LUA_NOCVTN2S */ +/* #define LUA_NOCVTS2N */ + + +/* +@@ LUA_USE_APICHECK turns on several consistency checks on the C API. +** Define it as a help when debugging C code. +*/ +#if defined(LUA_USE_APICHECK) +#include +#define luai_apicheck(l,e) assert(e) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Macros that affect the API and must be stable (that is, must be the +** same when you compile Lua and when you compile code that links to +** Lua). You probably do not want/need to change them. +** ===================================================================== +*/ + +/* +@@ LUAI_MAXSTACK limits the size of the Lua stack. +** CHANGE it if you need a different limit. This limit is arbitrary; +** its only purpose is to stop Lua from consuming unlimited stack +** space (and to reserve some numbers for pseudo-indices). +*/ +#if LUAI_BITSINT >= 32 +#define LUAI_MAXSTACK 1000000 +#else +#define LUAI_MAXSTACK 15000 +#endif + + +/* +@@ LUA_EXTRASPACE defines the size of a raw memory area associated with +** a Lua state with very fast access. +** CHANGE it if you need a different size. +*/ +#define LUA_EXTRASPACE (sizeof(void *)) + + +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@@ of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 + + +/* +@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +** CHANGE it if it uses too much C-stack space. (For long double, +** 'string.format("%.99f", 1e4932)' needs ~5030 bytes, so a +** smaller buffer would force a memory allocation for each call to +** 'string.format'.) +*/ +#if defined(LUA_FLOAT_LONGDOUBLE) +#define LUAL_BUFFERSIZE 8192 +#else +#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) +#endif + +/* }================================================================== */ + + +/* +@@ LUA_QL describes how error messages quote program elements. +** Lua does not use these macros anymore; they are here for +** compatibility only. +*/ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + + + + +/* =================================================================== */ + +/* +** Local configuration. You can use this space to add your redefinitions +** without modifying the main part of the file. +*/ + + + + + +#endif + diff --git a/golua/lua/lua53/lualib.h b/golua/lua/lua53/lualib.h new file mode 100644 index 0000000..5165c0f --- /dev/null +++ b/golua/lua/lua53/lualib.h @@ -0,0 +1,58 @@ +/* +** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 roberto Exp $ +** Lua standard libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lualib_h +#define lualib_h + +#include "lua.h" + + + +LUAMOD_API int (luaopen_base) (lua_State *L); + +#define LUA_COLIBNAME "coroutine" +LUAMOD_API int (luaopen_coroutine) (lua_State *L); + +#define LUA_TABLIBNAME "table" +LUAMOD_API int (luaopen_table) (lua_State *L); + +#define LUA_IOLIBNAME "io" +LUAMOD_API int (luaopen_io) (lua_State *L); + +#define LUA_OSLIBNAME "os" +LUAMOD_API int (luaopen_os) (lua_State *L); + +#define LUA_STRLIBNAME "string" +LUAMOD_API int (luaopen_string) (lua_State *L); + +#define LUA_UTF8LIBNAME "utf8" +LUAMOD_API int (luaopen_utf8) (lua_State *L); + +#define LUA_BITLIBNAME "bit32" +LUAMOD_API int (luaopen_bit32) (lua_State *L); + +#define LUA_MATHLIBNAME "math" +LUAMOD_API int (luaopen_math) (lua_State *L); + +#define LUA_DBLIBNAME "debug" +LUAMOD_API int (luaopen_debug) (lua_State *L); + +#define LUA_LOADLIBNAME "package" +LUAMOD_API int (luaopen_package) (lua_State *L); + + +/* open all previous libraries */ +LUALIB_API void (luaL_openlibs) (lua_State *L); + + + +#if !defined(lua_assert) +#define lua_assert(x) ((void)0) +#endif + + +#endif diff --git a/golua/lua/lua54/dummy.go b/golua/lua/lua54/dummy.go new file mode 100644 index 0000000..651a797 --- /dev/null +++ b/golua/lua/lua54/dummy.go @@ -0,0 +1,7 @@ +// +build dummy + +// Package lua contains only a C header files. +// +// This Go file is part of a workaround for `go mod vendor`. +// Please see the file dummy.go in the parent directory for more information. +package lua diff --git a/golua/lua/lua54/lauxlib.h b/golua/lua/lua54/lauxlib.h new file mode 100644 index 0000000..59fef6a --- /dev/null +++ b/golua/lua/lua54/lauxlib.h @@ -0,0 +1,276 @@ +/* +** $Id: lauxlib.h $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lauxlib_h +#define lauxlib_h + + +#include +#include + +#include "lua.h" + + +/* global table */ +#define LUA_GNAME "_G" + + +typedef struct luaL_Buffer luaL_Buffer; + + +/* extra error code for 'luaL_loadfilex' */ +#define LUA_ERRFILE (LUA_ERRERR+1) + + +/* key, in the registry, for table of loaded modules */ +#define LUA_LOADED_TABLE "_LOADED" + + +/* key, in the registry, for table of preloaded loaders */ +#define LUA_PRELOAD_TABLE "_PRELOAD" + + +typedef struct luaL_Reg { + const char *name; + lua_CFunction func; +} luaL_Reg; + + +#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) + +LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); +#define luaL_checkversion(L) \ + luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) + +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); +LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); +LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); +LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, + size_t *l); +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, + const char *def, size_t *l); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); + +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, + lua_Integer def); + +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); +LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int arg); + +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); +LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); +LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); + +LUALIB_API void (luaL_where) (lua_State *L, int lvl); +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); + +LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, + const char *const lst[]); + +LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); +LUALIB_API int (luaL_execresult) (lua_State *L, int stat); + + +/* predefined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +LUALIB_API int (luaL_ref) (lua_State *L, int t); +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); + +LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, + const char *mode); + +#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) + +LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, + const char *name, const char *mode); +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); + +LUALIB_API lua_State *(luaL_newstate) (void); + +LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); + +LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, + const char *p, const char *r); +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, + const char *p, const char *r); + +LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); + +LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); + +LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, + const char *msg, int level); + +LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, + lua_CFunction openf, int glb); + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + + +#define luaL_newlibtable(L,l) \ + lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) + +#define luaL_newlib(L,l) \ + (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) + +#define luaL_argcheck(L, cond,arg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) + +#define luaL_argexpected(L,cond,arg,tname) \ + ((void)((cond) || luaL_typeerror(L, (arg), (tname)))) + +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) + +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) + +#define luaL_dofile(L, fn) \ + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_dostring(L, s) \ + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) + +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) + +#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) + + +/* push the value used to represent failure/error */ +#define luaL_pushfail(L) lua_pushnil(L) + + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + +struct luaL_Buffer { + char *b; /* buffer address */ + size_t size; /* buffer size */ + size_t n; /* number of characters in buffer */ + lua_State *L; + union { + LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ + char b[LUAL_BUFFERSIZE]; /* initial buffer */ + } init; +}; + + +#define luaL_bufflen(bf) ((bf)->n) +#define luaL_buffaddr(bf) ((bf)->b) + + +#define luaL_addchar(B,c) \ + ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ + ((B)->b[(B)->n++] = (c))) + +#define luaL_addsize(B,s) ((B)->n += (s)) + +#define luaL_buffsub(B,s) ((B)->n -= (s)) + +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); +LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); +LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); + +#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) + +/* }====================================================== */ + + + +/* +** {====================================================== +** File handles for IO library +** ======================================================= +*/ + +/* +** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and +** initial structure 'luaL_Stream' (it may contain other fields +** after that initial structure). +*/ + +#define LUA_FILEHANDLE "FILE*" + + +typedef struct luaL_Stream { + FILE *f; /* stream (NULL for incompletely created streams) */ + lua_CFunction closef; /* to close stream (NULL for closed streams) */ +} luaL_Stream; + +/* }====================================================== */ + +/* +** {================================================================== +** "Abstraction Layer" for basic report of messages and errors +** =================================================================== +*/ + +/* print a string */ +#if !defined(lua_writestring) +#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) +#endif + +/* print a newline and flush the output */ +#if !defined(lua_writeline) +#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) +#endif + +/* print an error message */ +#if !defined(lua_writestringerror) +#define lua_writestringerror(s,p) \ + (fprintf(stderr, (s), (p)), fflush(stderr)) +#endif + +/* }================================================================== */ + + +/* +** {============================================================ +** Compatibility with deprecated conversions +** ============================================================= +*/ +#if defined(LUA_COMPAT_APIINTCASTS) + +#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) +#define luaL_optunsigned(L,a,d) \ + ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) + +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) + +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#endif +/* }============================================================ */ + + + +#endif + + diff --git a/golua/lua/lua54/lua.h b/golua/lua/lua54/lua.h new file mode 100644 index 0000000..6e00bc1 --- /dev/null +++ b/golua/lua/lua54/lua.h @@ -0,0 +1,518 @@ +/* +** $Id: lua.h $ +** Lua - A Scripting Language +** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** See Copyright Notice at the end of this file +*/ + + +#ifndef lua_h +#define lua_h + +#include +#include + + +#include "luaconf.h" + + +#define LUA_VERSION_MAJOR "5" +#define LUA_VERSION_MINOR "4" +#define LUA_VERSION_RELEASE "2" + +#define LUA_VERSION_NUM 504 +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) + +#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2020 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" + + +/* mark for precompiled code ('Lua') */ +#define LUA_SIGNATURE "\x1bLua" + +/* option for multiple returns in 'lua_pcall' and 'lua_call' */ +#define LUA_MULTRET (-1) + + +/* +** Pseudo-indices +** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty +** space after that to help overflow detection) +*/ +#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) +#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) + + +/* thread status */ +#define LUA_OK 0 +#define LUA_YIELD 1 +#define LUA_ERRRUN 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRERR 5 + + +typedef struct lua_State lua_State; + + +/* +** basic types +*/ +#define LUA_TNONE (-1) + +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_TFUNCTION 6 +#define LUA_TUSERDATA 7 +#define LUA_TTHREAD 8 + +#define LUA_NUMTYPES 9 + + + +/* minimum Lua stack available to a C function */ +#define LUA_MINSTACK 20 + + +/* predefined values in the registry */ +#define LUA_RIDX_MAINTHREAD 1 +#define LUA_RIDX_GLOBALS 2 +#define LUA_RIDX_LAST LUA_RIDX_GLOBALS + + +/* type of numbers in Lua */ +typedef LUA_NUMBER lua_Number; + + +/* type for integer functions */ +typedef LUA_INTEGER lua_Integer; + +/* unsigned integer type */ +typedef LUA_UNSIGNED lua_Unsigned; + +/* type for continuation-function contexts */ +typedef LUA_KCONTEXT lua_KContext; + + +/* +** Type for C functions registered with Lua +*/ +typedef int (*lua_CFunction) (lua_State *L); + +/* +** Type for continuation functions +*/ +typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); + + +/* +** Type for functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); + + +/* +** Type for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + + +/* +** Type for warning functions +*/ +typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); + + + + +/* +** generic extra include file +*/ +#if defined(LUA_USER_H) +#include LUA_USER_H +#endif + + +/* +** RCS ident string +*/ +extern const char lua_ident[]; + + +/* +** state manipulation +*/ +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API void (lua_close) (lua_State *L); +LUA_API lua_State *(lua_newthread) (lua_State *L); +LUA_API int (lua_resetthread) (lua_State *L); + +LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); + + +LUA_API lua_Number (lua_version) (lua_State *L); + + +/* +** basic stack manipulation +*/ +LUA_API int (lua_absindex) (lua_State *L, int idx); +LUA_API int (lua_gettop) (lua_State *L); +LUA_API void (lua_settop) (lua_State *L, int idx); +LUA_API void (lua_pushvalue) (lua_State *L, int idx); +LUA_API void (lua_rotate) (lua_State *L, int idx, int n); +LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); +LUA_API int (lua_checkstack) (lua_State *L, int n); + +LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); + + +/* +** access functions (stack -> C) +*/ + +LUA_API int (lua_isnumber) (lua_State *L, int idx); +LUA_API int (lua_isstring) (lua_State *L, int idx); +LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isinteger) (lua_State *L, int idx); +LUA_API int (lua_isuserdata) (lua_State *L, int idx); +LUA_API int (lua_type) (lua_State *L, int idx); +LUA_API const char *(lua_typename) (lua_State *L, int tp); + +LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); +LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); +LUA_API int (lua_toboolean) (lua_State *L, int idx); +LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); +LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx); +LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); +LUA_API void *(lua_touserdata) (lua_State *L, int idx); +LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); +LUA_API const void *(lua_topointer) (lua_State *L, int idx); + + +/* +** Comparison and arithmetic functions +*/ + +#define LUA_OPADD 0 /* ORDER TM, ORDER OP */ +#define LUA_OPSUB 1 +#define LUA_OPMUL 2 +#define LUA_OPMOD 3 +#define LUA_OPPOW 4 +#define LUA_OPDIV 5 +#define LUA_OPIDIV 6 +#define LUA_OPBAND 7 +#define LUA_OPBOR 8 +#define LUA_OPBXOR 9 +#define LUA_OPSHL 10 +#define LUA_OPSHR 11 +#define LUA_OPUNM 12 +#define LUA_OPBNOT 13 + +LUA_API void (lua_arith) (lua_State *L, int op); + +#define LUA_OPEQ 0 +#define LUA_OPLT 1 +#define LUA_OPLE 2 + +LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); + + +/* +** push functions (C -> stack) +*/ +LUA_API void (lua_pushnil) (lua_State *L); +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); +LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); +LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); +LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, + va_list argp); +LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); +LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); +LUA_API void (lua_pushboolean) (lua_State *L, int b); +LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); +LUA_API int (lua_pushthread) (lua_State *L); + + +/* +** get functions (Lua -> stack) +*/ +LUA_API int (lua_getglobal) (lua_State *L, const char *name); +LUA_API int (lua_gettable) (lua_State *L, int idx); +LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); +LUA_API int (lua_rawget) (lua_State *L, int idx); +LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); +LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); + +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); +LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); +LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); +LUA_API int (lua_getmetatable) (lua_State *L, int objindex); +LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n); + + +/* +** set functions (stack -> Lua) +*/ +LUA_API void (lua_setglobal) (lua_State *L, const char *name); +LUA_API void (lua_settable) (lua_State *L, int idx); +LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); +LUA_API void (lua_rawset) (lua_State *L, int idx); +LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); +LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); +LUA_API int (lua_setmetatable) (lua_State *L, int objindex); +LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n); + + +/* +** 'load' and 'call' functions (load and run Lua code) +*/ +LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, + lua_KContext ctx, lua_KFunction k); +#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) + +LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, + lua_KContext ctx, lua_KFunction k); +#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) + +LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname, const char *mode); + +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); + + +/* +** coroutine functions +*/ +LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, + lua_KFunction k); +LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg, + int *nres); +LUA_API int (lua_status) (lua_State *L); +LUA_API int (lua_isyieldable) (lua_State *L); + +#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) + + +/* +** Warning-related functions +*/ +LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud); +LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); + + +/* +** garbage-collection function and options +*/ + +#define LUA_GCSTOP 0 +#define LUA_GCRESTART 1 +#define LUA_GCCOLLECT 2 +#define LUA_GCCOUNT 3 +#define LUA_GCCOUNTB 4 +#define LUA_GCSTEP 5 +#define LUA_GCSETPAUSE 6 +#define LUA_GCSETSTEPMUL 7 +#define LUA_GCISRUNNING 9 +#define LUA_GCGEN 10 +#define LUA_GCINC 11 + +LUA_API int (lua_gc) (lua_State *L, int what, ...); +LUA_API int (lua_gc_compat) (lua_State *L, int what, int data); + + +/* +** miscellaneous functions +*/ + +LUA_API int (lua_error) (lua_State *L); + +LUA_API int (lua_next) (lua_State *L, int idx); + +LUA_API void (lua_concat) (lua_State *L, int n); +LUA_API void (lua_len) (lua_State *L, int idx); + +LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); + +LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); +LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); + +LUA_API void (lua_toclose) (lua_State *L, int idx); + + +/* +** {============================================================== +** some useful macros +** =============================================================== +*/ + +#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) + +#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) +#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) + +#define lua_pop(L,n) lua_settop(L, -(n)-1) + +#define lua_newtable(L) lua_createtable(L, 0, 0) + +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) + +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) + +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) + +#define lua_pushliteral(L, s) lua_pushstring(L, "" s) + +#define lua_pushglobaltable(L) \ + ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) + +#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) + + +#define lua_insert(L,idx) lua_rotate(L, (idx), 1) + +#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) + +#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) + +/* }============================================================== */ + + +/* +** {============================================================== +** compatibility macros +** =============================================================== +*/ +#if defined(LUA_COMPAT_APIINTCASTS) + +#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) +#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) +#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) + +#endif + +#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) +#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) + +#define LUA_NUMTAGS LUA_NUMTYPES + +/* }============================================================== */ + +/* +** {====================================================================== +** Debug API +** ======================================================================= +*/ + + +/* +** Event codes +*/ +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILCALL 4 + + +/* +** Event masks +*/ +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +typedef struct lua_Debug lua_Debug; /* activation record */ + + +/* Functions to be called by the debugger in specific events */ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); + + +LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); +LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); +LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); +LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); + +LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); +LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, + int fidx2, int n2); + +LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); +LUA_API lua_Hook (lua_gethook) (lua_State *L); +LUA_API int (lua_gethookmask) (lua_State *L); +LUA_API int (lua_gethookcount) (lua_State *L); + +LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit); + +struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ + const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ + const char *source; /* (S) */ + size_t srclen; /* (S) */ + int currentline; /* (l) */ + int linedefined; /* (S) */ + int lastlinedefined; /* (S) */ + unsigned char nups; /* (u) number of upvalues */ + unsigned char nparams;/* (u) number of parameters */ + char isvararg; /* (u) */ + char istailcall; /* (t) */ + unsigned short ftransfer; /* (r) index of first value transferred */ + unsigned short ntransfer; /* (r) number of transferred values */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + struct CallInfo *i_ci; /* active function */ +}; + +/* }====================================================================== */ + + +/****************************************************************************** +* Copyright (C) 1994-2020 Lua.org, PUC-Rio. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + + +#endif diff --git a/golua/lua/lua54/luaconf.h b/golua/lua/lua54/luaconf.h new file mode 100644 index 0000000..d9cf18c --- /dev/null +++ b/golua/lua/lua54/luaconf.h @@ -0,0 +1,761 @@ +/* +** $Id: luaconf.h $ +** Configuration file for Lua +** See Copyright Notice in lua.h +*/ + + +#ifndef luaconf_h +#define luaconf_h + +#include +#include + + +/* +** =================================================================== +** General Configuration File for Lua +** +** Some definitions here can be changed externally, through the +** compiler (e.g., with '-D' options). Those are protected by +** '#if !defined' guards. However, several other definitions should +** be changed directly here, either because they affect the Lua +** ABI (by making the changes here, you ensure that all software +** connected to Lua, such as C libraries, will be compiled with the +** same configuration); or because they are seldom changed. +** +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + + +/* +** {==================================================================== +** System Configuration: macros to adapt (if needed) Lua to some +** particular platform, for instance restricting it to C89. +** ===================================================================== +*/ + +/* +@@ LUA_USE_C89 controls the use of non-ISO-C89 features. +** Define it if you want Lua to avoid the use of a few C99 features +** or Windows-specific features on Windows. +*/ +/* #define LUA_USE_C89 */ + + +/* +** By default, Lua on Windows use (some) specific Windows features +*/ +#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) +#define LUA_USE_WINDOWS /* enable goodies for regular Windows */ +#endif + + +#if defined(LUA_USE_WINDOWS) +#define LUA_DL_DLL /* enable support for DLL */ +#define LUA_USE_C89 /* broadly, Windows is C89 */ +#endif + + +#if defined(LUA_USE_LINUX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#endif + + +#if defined(LUA_USE_MACOSX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ +#endif + + +/* +@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. +*/ +#define LUAI_IS32INT ((UINT_MAX >> 30) >= 3) + +/* }================================================================== */ + + + +/* +** {================================================================== +** Configuration for Number types. +** =================================================================== +*/ + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. +*/ +/* #define LUA_32BITS */ + + +/* +@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for +** C89 ('long' and 'double'); Windows always has '__int64', so it does +** not need to use this case. +*/ +#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) +#define LUA_C89_NUMBERS +#endif + + +/* +@@ LUA_INT_TYPE defines the type for Lua integers. +@@ LUA_FLOAT_TYPE defines the type for Lua floats. +** Lua should work fine with any mix of these options supported +** by your C compiler. The usual configurations are 64-bit integers +** and 'double' (the default), 32-bit integers and 'float' (for +** restricted platforms), and 'long'/'double' (for C compilers not +** compliant with C99, which may not have support for 'long long'). +*/ + +/* predefined options for LUA_INT_TYPE */ +#define LUA_INT_INT 1 +#define LUA_INT_LONG 2 +#define LUA_INT_LONGLONG 3 + +/* predefined options for LUA_FLOAT_TYPE */ +#define LUA_FLOAT_FLOAT 1 +#define LUA_FLOAT_DOUBLE 2 +#define LUA_FLOAT_LONGDOUBLE 3 + +#if defined(LUA_32BITS) /* { */ +/* +** 32-bit integers and 'float' +*/ +#if LUAI_IS32INT /* use 'int' if big enough */ +#define LUA_INT_TYPE LUA_INT_INT +#else /* otherwise use 'long' */ +#define LUA_INT_TYPE LUA_INT_LONG +#endif +#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT + +#elif defined(LUA_C89_NUMBERS) /* }{ */ +/* +** largest types available for C89 ('long' and 'double') +*/ +#define LUA_INT_TYPE LUA_INT_LONG +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE + +#endif /* } */ + + +/* +** default configuration for 64-bit Lua ('long long' and 'double') +*/ +#if !defined(LUA_INT_TYPE) +#define LUA_INT_TYPE LUA_INT_LONGLONG +#endif + +#if !defined(LUA_FLOAT_TYPE) +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE +#endif + +/* }================================================================== */ + + + +/* +** {================================================================== +** Configuration for Paths. +** =================================================================== +*/ + +/* +** LUA_PATH_SEP is the character that separates templates in a path. +** LUA_PATH_MARK is the string that marks the substitution points in a +** template. +** LUA_EXEC_DIR in a Windows path is replaced by the executable's +** directory. +*/ +#define LUA_PATH_SEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXEC_DIR "!" + + +/* +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for +** Lua libraries. +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for +** C libraries. +** CHANGE them if your machine has a non-conventional directory +** hierarchy or if you want to install your libraries in +** non-conventional directories. +*/ + +#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#if defined(_WIN32) /* { */ +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" + +#if !defined(LUA_PATH_DEFAULT) +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ + LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ + ".\\?.lua;" ".\\?\\init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.dll;" \ + LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ + LUA_CDIR"loadall.dll;" ".\\?.dll" +#endif + +#else /* }{ */ + +#define LUA_ROOT "/usr/local/" +#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" +#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" + +#if !defined(LUA_PATH_DEFAULT) +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ + "./?.lua;" "./?/init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif + +#endif /* } */ + + +/* +@@ LUA_DIRSEP is the directory separator (for submodules). +** CHANGE it if your machine does not use "/" as the directory separator +** and is not Windows. (On Windows Lua automatically uses "\".) +*/ +#if !defined(LUA_DIRSEP) + +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Marks for exported symbols in the C code +** =================================================================== +*/ + +/* +@@ LUA_API is a mark for all core API functions. +@@ LUALIB_API is a mark for all auxiliary library functions. +@@ LUAMOD_API is a mark for all standard library opening functions. +** CHANGE them if you need to define those functions in some special way. +** For instance, if you want to create one Windows DLL with the core and +** the libraries, you may want to use the following definition (define +** LUA_BUILD_AS_DLL to get it). +*/ +#if defined(LUA_BUILD_AS_DLL) /* { */ + +#if defined(LUA_CORE) || defined(LUA_LIB) /* { */ +#define LUA_API __declspec(dllexport) +#else /* }{ */ +#define LUA_API __declspec(dllimport) +#endif /* } */ + +#else /* }{ */ + +#define LUA_API extern + +#endif /* } */ + + +/* +** More often than not the libs go together with the core. +*/ +#define LUALIB_API LUA_API +#define LUAMOD_API LUA_API + + +/* +@@ LUAI_FUNC is a mark for all extern functions that are not to be +** exported to outside modules. +@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, +** none of which to be exported to outside modules (LUAI_DDEF for +** definitions and LUAI_DDEC for declarations). +** CHANGE them if you need to mark them in some special way. Elf/gcc +** (versions 3.2 and later) mark them as "hidden" to optimize access +** when Lua is compiled as a shared library. Not all elf targets support +** this attribute. Unfortunately, gcc does not offer a way to check +** whether the target offers that support, and those without support +** give a warning about it. To avoid these warnings, change to the +** default definition. +*/ +#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + defined(__ELF__) /* { */ +#define LUAI_FUNC __attribute__((visibility("internal"))) extern +#else /* }{ */ +#define LUAI_FUNC extern +#endif /* } */ + +#define LUAI_DDEC(dec) LUAI_FUNC dec +#define LUAI_DDEF /* empty */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Compatibility with previous versions +** =================================================================== +*/ + +/* +@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. +** You can define it to get all options, or change specific options +** to fit your specific needs. +*/ +#if defined(LUA_COMPAT_5_3) /* { */ + +/* +@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated +** functions in the mathematical library. +** (These functions were already officially removed in 5.3; +** nevertheless they are still available here.) +*/ +#define LUA_COMPAT_MATHLIB + +/* +@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for +** manipulating other integer types (lua_pushunsigned, lua_tounsigned, +** luaL_checkint, luaL_checklong, etc.) +** (These macros were also officially removed in 5.3, but they are still +** available here.) +*/ +#define LUA_COMPAT_APIINTCASTS + + +/* +@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod +** using '__lt'. +*/ +#define LUA_COMPAT_LT_LE + + +/* +@@ The following macros supply trivial compatibility for some +** changes in the API. The macros themselves document how to +** change your code to avoid using them. +** (Once more, these macros were officially removed in 5.3, but they are +** still available here.) +*/ +#define lua_strlen(L,i) lua_rawlen(L, (i)) + +#define lua_objlen(L,i) lua_rawlen(L, (i)) + +#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) +#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) + +#endif /* } */ + +/* }================================================================== */ + + + +/* +** {================================================================== +** Configuration for Numbers. +** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* +** satisfy your needs. +** =================================================================== +*/ + +/* +@@ LUA_NUMBER is the floating-point type used by Lua. +@@ LUAI_UACNUMBER is the result of a 'default argument promotion' +@@ over a floating number. +@@ l_floatatt(x) corrects float attribute 'x' to the proper float type +** by prefixing it with one of FLT/DBL/LDBL. +@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. +@@ LUA_NUMBER_FMT is the format for writing floats. +@@ lua_number2str converts a float to a string. +@@ l_mathop allows the addition of an 'l' or 'f' to all math operations. +@@ l_floor takes the floor of a float. +@@ lua_str2number converts a decimal numeral to a number. +*/ + + +/* The following definitions are good for most cases here */ + +#define l_floor(x) (l_mathop(floor)(x)) + +#define lua_number2str(s,sz,n) \ + l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) + +/* +@@ lua_numbertointeger converts a float number with an integral value +** to an integer, or returns 0 if float is not within the range of +** a lua_Integer. (The range comparisons are tricky because of +** rounding. The tests here assume a two-complement representation, +** where MININTEGER always has an exact representation as a float; +** MAXINTEGER may not have one, and therefore its conversion to float +** may have an ill-defined value.) +*/ +#define lua_numbertointeger(n,p) \ + ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ + (*(p) = (LUA_INTEGER)(n), 1)) + + +/* now the variable definitions */ + +#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ + +#define LUA_NUMBER float + +#define l_floatatt(n) (FLT_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.7g" + +#define l_mathop(op) op##f + +#define lua_str2number(s,p) strtof((s), (p)) + + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ + +#define LUA_NUMBER long double + +#define l_floatatt(n) (LDBL_##n) + +#define LUAI_UACNUMBER long double + +#define LUA_NUMBER_FRMLEN "L" +#define LUA_NUMBER_FMT "%.19Lg" + +#define l_mathop(op) op##l + +#define lua_str2number(s,p) strtold((s), (p)) + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ + +#define LUA_NUMBER double + +#define l_floatatt(n) (DBL_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.14g" + +#define l_mathop(op) op + +#define lua_str2number(s,p) strtod((s), (p)) + +#else /* }{ */ + +#error "numeric float type not defined" + +#endif /* } */ + + + +/* +@@ LUA_INTEGER is the integer type used by Lua. +** +@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. +** +@@ LUAI_UACINT is the result of a 'default argument promotion' +@@ over a LUA_INTEGER. +@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. +@@ LUA_INTEGER_FMT is the format for writing integers. +@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. +@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. +@@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. +@@ LUA_UNSIGNEDBITS is the number of bits in a LUA_UNSIGNED. +@@ lua_integer2str converts an integer to a string. +*/ + + +/* The following definitions are good for most cases here */ + +#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" + +#define LUAI_UACINT LUA_INTEGER + +#define lua_integer2str(s,sz,n) \ + l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) + +/* +** use LUAI_UACINT here to avoid problems with promotions (which +** can turn a comparison between unsigneds into a signed comparison) +*/ +#define LUA_UNSIGNED unsigned LUAI_UACINT + + +#define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT) + + +/* now the variable definitions */ + +#if LUA_INT_TYPE == LUA_INT_INT /* { int */ + +#define LUA_INTEGER int +#define LUA_INTEGER_FRMLEN "" + +#define LUA_MAXINTEGER INT_MAX +#define LUA_MININTEGER INT_MIN + +#define LUA_MAXUNSIGNED UINT_MAX + +#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ + +#define LUA_INTEGER long +#define LUA_INTEGER_FRMLEN "l" + +#define LUA_MAXINTEGER LONG_MAX +#define LUA_MININTEGER LONG_MIN + +#define LUA_MAXUNSIGNED ULONG_MAX + +#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ + +/* use presence of macro LLONG_MAX as proxy for C99 compliance */ +#if defined(LLONG_MAX) /* { */ +/* use ISO C99 stuff */ + +#define LUA_INTEGER long long +#define LUA_INTEGER_FRMLEN "ll" + +#define LUA_MAXINTEGER LLONG_MAX +#define LUA_MININTEGER LLONG_MIN + +#define LUA_MAXUNSIGNED ULLONG_MAX + +#elif defined(LUA_USE_WINDOWS) /* }{ */ +/* in Windows, can use specific Windows types */ + +#define LUA_INTEGER __int64 +#define LUA_INTEGER_FRMLEN "I64" + +#define LUA_MAXINTEGER _I64_MAX +#define LUA_MININTEGER _I64_MIN + +#define LUA_MAXUNSIGNED _UI64_MAX + +#else /* }{ */ + +#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ + or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" + +#endif /* } */ + +#else /* }{ */ + +#error "numeric integer type not defined" + +#endif /* } */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Dependencies with C99 and other C details +** =================================================================== +*/ + +/* +@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. +** (All uses in Lua have only one format item.) +*/ +#if !defined(LUA_USE_C89) +#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) +#else +#define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) +#endif + + +/* +@@ lua_strx2number converts a hexadecimal numeral to a number. +** In C99, 'strtod' does that conversion. Otherwise, you can +** leave 'lua_strx2number' undefined and Lua will provide its own +** implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_strx2number(s,p) lua_str2number(s,p) +#endif + + +/* +@@ lua_pointer2str converts a pointer to a readable string in a +** non-specified way. +*/ +#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) + + +/* +@@ lua_number2strx converts a float to a hexadecimal numeral. +** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. +** Otherwise, you can leave 'lua_number2strx' undefined and Lua will +** provide its own implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_number2strx(L,b,sz,f,n) \ + ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) +#endif + + +/* +** 'strtof' and 'opf' variants for math functions are not valid in +** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the +** availability of these variants. ('math.h' is already included in +** all files that use these macros.) +*/ +#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) +#undef l_mathop /* variants not available */ +#undef lua_str2number +#define l_mathop(op) (lua_Number)op /* no variant */ +#define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) +#endif + + +/* +@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation +** functions. It must be a numerical type; Lua will use 'intptr_t' if +** available, otherwise it will use 'ptrdiff_t' (the nearest thing to +** 'intptr_t' in C89) +*/ +#define LUA_KCONTEXT ptrdiff_t + +#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ + __STDC_VERSION__ >= 199901L +#include +#if defined(INTPTR_MAX) /* even in C99 this type is optional */ +#undef LUA_KCONTEXT +#define LUA_KCONTEXT intptr_t +#endif +#endif + + +/* +@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). +** Change that if you do not want to use C locales. (Code using this +** macro must include the header 'locale.h'.) +*/ +#if !defined(lua_getlocaledecpoint) +#define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Language Variations +** ===================================================================== +*/ + +/* +@@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some +** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from +** numbers to strings. Define LUA_NOCVTS2N to turn off automatic +** coercion from strings to numbers. +*/ +/* #define LUA_NOCVTN2S */ +/* #define LUA_NOCVTS2N */ + + +/* +@@ LUA_USE_APICHECK turns on several consistency checks on the C API. +** Define it as a help when debugging C code. +*/ +#if defined(LUA_USE_APICHECK) +#include +#define luai_apicheck(l,e) assert(e) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Macros that affect the API and must be stable (that is, must be the +** same when you compile Lua and when you compile code that links to +** Lua). +** ===================================================================== +*/ + +/* +@@ LUAI_MAXSTACK limits the size of the Lua stack. +** CHANGE it if you need a different limit. This limit is arbitrary; +** its only purpose is to stop Lua from consuming unlimited stack +** space (and to reserve some numbers for pseudo-indices). +** (It must fit into max(size_t)/32.) +*/ +#if LUAI_IS32INT +#define LUAI_MAXSTACK 1000000 +#else +#define LUAI_MAXSTACK 15000 +#endif + + +/* +@@ LUA_EXTRASPACE defines the size of a raw memory area associated with +** a Lua state with very fast access. +** CHANGE it if you need a different size. +*/ +#define LUA_EXTRASPACE (sizeof(void *)) + + +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@@ of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 + + +/* +@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +*/ +#define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) + + +/* +@@ LUAI_MAXALIGN defines fields that, when used in a union, ensure +** maximum alignment for the other items in that union. +*/ +#define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l + +/* }================================================================== */ + + + + + +/* =================================================================== */ + +/* +** Local configuration. You can use this space to add your redefinitions +** without modifying the main part of the file. +*/ + + + + + +#endif + diff --git a/golua/lua/lua54/lualib.h b/golua/lua/lua54/lualib.h new file mode 100644 index 0000000..eb08b53 --- /dev/null +++ b/golua/lua/lua54/lualib.h @@ -0,0 +1,58 @@ +/* +** $Id: lualib.h $ +** Lua standard libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lualib_h +#define lualib_h + +#include "lua.h" + + +/* version suffix for environment variable names */ +#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR + + +LUAMOD_API int (luaopen_base) (lua_State *L); + +#define LUA_COLIBNAME "coroutine" +LUAMOD_API int (luaopen_coroutine) (lua_State *L); + +#define LUA_TABLIBNAME "table" +LUAMOD_API int (luaopen_table) (lua_State *L); + +#define LUA_IOLIBNAME "io" +LUAMOD_API int (luaopen_io) (lua_State *L); + +#define LUA_OSLIBNAME "os" +LUAMOD_API int (luaopen_os) (lua_State *L); + +#define LUA_STRLIBNAME "string" +LUAMOD_API int (luaopen_string) (lua_State *L); + +#define LUA_UTF8LIBNAME "utf8" +LUAMOD_API int (luaopen_utf8) (lua_State *L); + +#define LUA_MATHLIBNAME "math" +LUAMOD_API int (luaopen_math) (lua_State *L); + +#define LUA_DBLIBNAME "debug" +LUAMOD_API int (luaopen_debug) (lua_State *L); + +#define LUA_LOADLIBNAME "package" +LUAMOD_API int (luaopen_package) (lua_State *L); + + +/* open all previous libraries */ +LUALIB_API void (luaL_openlibs) (lua_State *L); + + + +#if !defined(lua_assert) +#define lua_assert(x) ((void)0) +#endif + + +#endif diff --git a/golua/lua/lua_defs_lua51.go b/golua/lua/lua_defs_lua51.go new file mode 100644 index 0000000..3696c39 --- /dev/null +++ b/golua/lua/lua_defs_lua51.go @@ -0,0 +1,73 @@ +//+build !lua52,!lua53,!lua54 + +package lua + +/* +#include + #include + #include + +*/ +import "C" + +type LuaValType int + +const ( + LUA_TNIL = LuaValType(C.LUA_TNIL) + LUA_TNUMBER = LuaValType(C.LUA_TNUMBER) + LUA_TBOOLEAN = LuaValType(C.LUA_TBOOLEAN) + LUA_TSTRING = LuaValType(C.LUA_TSTRING) + LUA_TTABLE = LuaValType(C.LUA_TTABLE) + LUA_TFUNCTION = LuaValType(C.LUA_TFUNCTION) + LUA_TUSERDATA = LuaValType(C.LUA_TUSERDATA) + LUA_TTHREAD = LuaValType(C.LUA_TTHREAD) + LUA_TLIGHTUSERDATA = LuaValType(C.LUA_TLIGHTUSERDATA) +) + +const ( + LUA_VERSION = C.LUA_VERSION + LUA_RELEASE = C.LUA_RELEASE + LUA_VERSION_NUM = C.LUA_VERSION_NUM + LUA_COPYRIGHT = C.LUA_COPYRIGHT + LUA_AUTHORS = C.LUA_AUTHORS + LUA_MULTRET = C.LUA_MULTRET + LUA_REGISTRYINDEX = C.LUA_REGISTRYINDEX + LUA_ENVIRONINDEX = C.LUA_ENVIRONINDEX + LUA_GLOBALSINDEX = C.LUA_GLOBALSINDEX + LUA_YIELD = C.LUA_YIELD + LUA_ERRRUN = C.LUA_ERRRUN + LUA_ERRSYNTAX = C.LUA_ERRSYNTAX + LUA_ERRMEM = C.LUA_ERRMEM + LUA_ERRERR = C.LUA_ERRERR + LUA_TNONE = C.LUA_TNONE + LUA_MINSTACK = C.LUA_MINSTACK + LUA_GCSTOP = C.LUA_GCSTOP + LUA_GCRESTART = C.LUA_GCRESTART + LUA_GCCOLLECT = C.LUA_GCCOLLECT + LUA_GCCOUNT = C.LUA_GCCOUNT + LUA_GCCOUNTB = C.LUA_GCCOUNTB + LUA_GCSTEP = C.LUA_GCSTEP + LUA_GCSETPAUSE = C.LUA_GCSETPAUSE + LUA_GCSETSTEPMUL = C.LUA_GCSETSTEPMUL + LUA_HOOKCALL = C.LUA_HOOKCALL + LUA_HOOKRET = C.LUA_HOOKRET + LUA_HOOKLINE = C.LUA_HOOKLINE + LUA_HOOKCOUNT = C.LUA_HOOKCOUNT + LUA_HOOKTAILRET = C.LUA_HOOKTAILRET + LUA_MASKCALL = C.LUA_MASKCALL + LUA_MASKRET = C.LUA_MASKRET + LUA_MASKLINE = C.LUA_MASKLINE + LUA_MASKCOUNT = C.LUA_MASKCOUNT + LUA_ERRFILE = C.LUA_ERRFILE + LUA_NOREF = C.LUA_NOREF + LUA_REFNIL = C.LUA_REFNIL + LUA_FILEHANDLE = C.LUA_FILEHANDLE + LUA_COLIBNAME = C.LUA_COLIBNAME + LUA_TABLIBNAME = C.LUA_TABLIBNAME + LUA_IOLIBNAME = C.LUA_IOLIBNAME + LUA_OSLIBNAME = C.LUA_OSLIBNAME + LUA_STRLIBNAME = C.LUA_STRLIBNAME + LUA_MATHLIBNAME = C.LUA_MATHLIBNAME + LUA_DBLIBNAME = C.LUA_DBLIBNAME + LUA_LOADLIBNAME = C.LUA_LOADLIBNAME +) diff --git a/golua/lua/lua_defs_lua52.go b/golua/lua/lua_defs_lua52.go new file mode 100644 index 0000000..c6623f0 --- /dev/null +++ b/golua/lua/lua_defs_lua52.go @@ -0,0 +1,71 @@ +//+build lua52 + +package lua + +/* +#include + #include + #include + +*/ +import "C" + +type LuaValType int + +const ( + LUA_TNIL = LuaValType(C.LUA_TNIL) + LUA_TNUMBER = LuaValType(C.LUA_TNUMBER) + LUA_TBOOLEAN = LuaValType(C.LUA_TBOOLEAN) + LUA_TSTRING = LuaValType(C.LUA_TSTRING) + LUA_TTABLE = LuaValType(C.LUA_TTABLE) + LUA_TFUNCTION = LuaValType(C.LUA_TFUNCTION) + LUA_TUSERDATA = LuaValType(C.LUA_TUSERDATA) + LUA_TTHREAD = LuaValType(C.LUA_TTHREAD) + LUA_TLIGHTUSERDATA = LuaValType(C.LUA_TLIGHTUSERDATA) +) + +const ( + LUA_OK = C.LUA_OK + LUA_VERSION = C.LUA_VERSION + LUA_RELEASE = C.LUA_RELEASE + LUA_VERSION_NUM = C.LUA_VERSION_NUM + LUA_COPYRIGHT = C.LUA_COPYRIGHT + LUA_AUTHORS = C.LUA_AUTHORS + LUA_MULTRET = C.LUA_MULTRET + LUA_REGISTRYINDEX = C.LUA_REGISTRYINDEX + LUA_YIELD = C.LUA_YIELD + LUA_ERRRUN = C.LUA_ERRRUN + LUA_ERRSYNTAX = C.LUA_ERRSYNTAX + LUA_ERRMEM = C.LUA_ERRMEM + LUA_ERRERR = C.LUA_ERRERR + LUA_TNONE = C.LUA_TNONE + LUA_MINSTACK = C.LUA_MINSTACK + LUA_GCSTOP = C.LUA_GCSTOP + LUA_GCRESTART = C.LUA_GCRESTART + LUA_GCCOLLECT = C.LUA_GCCOLLECT + LUA_GCCOUNT = C.LUA_GCCOUNT + LUA_GCCOUNTB = C.LUA_GCCOUNTB + LUA_GCSTEP = C.LUA_GCSTEP + LUA_GCSETPAUSE = C.LUA_GCSETPAUSE + LUA_GCSETSTEPMUL = C.LUA_GCSETSTEPMUL + LUA_HOOKCALL = C.LUA_HOOKCALL + LUA_HOOKRET = C.LUA_HOOKRET + LUA_HOOKLINE = C.LUA_HOOKLINE + LUA_HOOKCOUNT = C.LUA_HOOKCOUNT + LUA_MASKCALL = C.LUA_MASKCALL + LUA_MASKRET = C.LUA_MASKRET + LUA_MASKLINE = C.LUA_MASKLINE + LUA_MASKCOUNT = C.LUA_MASKCOUNT + LUA_ERRFILE = C.LUA_ERRFILE + LUA_NOREF = C.LUA_NOREF + LUA_REFNIL = C.LUA_REFNIL + LUA_FILEHANDLE = C.LUA_FILEHANDLE + LUA_COLIBNAME = C.LUA_COLIBNAME + LUA_TABLIBNAME = C.LUA_TABLIBNAME + LUA_IOLIBNAME = C.LUA_IOLIBNAME + LUA_OSLIBNAME = C.LUA_OSLIBNAME + LUA_STRLIBNAME = C.LUA_STRLIBNAME + LUA_MATHLIBNAME = C.LUA_MATHLIBNAME + LUA_DBLIBNAME = C.LUA_DBLIBNAME + LUA_LOADLIBNAME = C.LUA_LOADLIBNAME +) diff --git a/golua/lua/lua_defs_lua53.go b/golua/lua/lua_defs_lua53.go new file mode 100644 index 0000000..0a67433 --- /dev/null +++ b/golua/lua/lua_defs_lua53.go @@ -0,0 +1,71 @@ +//+build lua53 + +package lua + +/* +#include + #include + #include + +*/ +import "C" + +type LuaValType int + +const ( + LUA_TNIL = LuaValType(C.LUA_TNIL) + LUA_TNUMBER = LuaValType(C.LUA_TNUMBER) + LUA_TBOOLEAN = LuaValType(C.LUA_TBOOLEAN) + LUA_TSTRING = LuaValType(C.LUA_TSTRING) + LUA_TTABLE = LuaValType(C.LUA_TTABLE) + LUA_TFUNCTION = LuaValType(C.LUA_TFUNCTION) + LUA_TUSERDATA = LuaValType(C.LUA_TUSERDATA) + LUA_TTHREAD = LuaValType(C.LUA_TTHREAD) + LUA_TLIGHTUSERDATA = LuaValType(C.LUA_TLIGHTUSERDATA) +) + +const ( + LUA_OK = C.LUA_OK + LUA_VERSION = C.LUA_VERSION + LUA_RELEASE = C.LUA_RELEASE + LUA_VERSION_NUM = C.LUA_VERSION_NUM + LUA_COPYRIGHT = C.LUA_COPYRIGHT + LUA_AUTHORS = C.LUA_AUTHORS + LUA_MULTRET = C.LUA_MULTRET + LUA_REGISTRYINDEX = C.LUA_REGISTRYINDEX + LUA_YIELD = C.LUA_YIELD + LUA_ERRRUN = C.LUA_ERRRUN + LUA_ERRSYNTAX = C.LUA_ERRSYNTAX + LUA_ERRMEM = C.LUA_ERRMEM + LUA_ERRERR = C.LUA_ERRERR + LUA_TNONE = C.LUA_TNONE + LUA_MINSTACK = C.LUA_MINSTACK + LUA_GCSTOP = C.LUA_GCSTOP + LUA_GCRESTART = C.LUA_GCRESTART + LUA_GCCOLLECT = C.LUA_GCCOLLECT + LUA_GCCOUNT = C.LUA_GCCOUNT + LUA_GCCOUNTB = C.LUA_GCCOUNTB + LUA_GCSTEP = C.LUA_GCSTEP + LUA_GCSETPAUSE = C.LUA_GCSETPAUSE + LUA_GCSETSTEPMUL = C.LUA_GCSETSTEPMUL + LUA_HOOKCALL = C.LUA_HOOKCALL + LUA_HOOKRET = C.LUA_HOOKRET + LUA_HOOKLINE = C.LUA_HOOKLINE + LUA_HOOKCOUNT = C.LUA_HOOKCOUNT + LUA_MASKCALL = C.LUA_MASKCALL + LUA_MASKRET = C.LUA_MASKRET + LUA_MASKLINE = C.LUA_MASKLINE + LUA_MASKCOUNT = C.LUA_MASKCOUNT + LUA_ERRFILE = C.LUA_ERRFILE + LUA_NOREF = C.LUA_NOREF + LUA_REFNIL = C.LUA_REFNIL + LUA_FILEHANDLE = C.LUA_FILEHANDLE + LUA_COLIBNAME = C.LUA_COLIBNAME + LUA_TABLIBNAME = C.LUA_TABLIBNAME + LUA_IOLIBNAME = C.LUA_IOLIBNAME + LUA_OSLIBNAME = C.LUA_OSLIBNAME + LUA_STRLIBNAME = C.LUA_STRLIBNAME + LUA_MATHLIBNAME = C.LUA_MATHLIBNAME + LUA_DBLIBNAME = C.LUA_DBLIBNAME + LUA_LOADLIBNAME = C.LUA_LOADLIBNAME +) diff --git a/golua/lua/lua_defs_lua54.go b/golua/lua/lua_defs_lua54.go new file mode 100644 index 0000000..f376835 --- /dev/null +++ b/golua/lua/lua_defs_lua54.go @@ -0,0 +1,71 @@ +//+build lua54 + +package lua + +/* +#include + #include + #include + +*/ +import "C" + +type LuaValType int + +const ( + LUA_TNIL = LuaValType(C.LUA_TNIL) + LUA_TNUMBER = LuaValType(C.LUA_TNUMBER) + LUA_TBOOLEAN = LuaValType(C.LUA_TBOOLEAN) + LUA_TSTRING = LuaValType(C.LUA_TSTRING) + LUA_TTABLE = LuaValType(C.LUA_TTABLE) + LUA_TFUNCTION = LuaValType(C.LUA_TFUNCTION) + LUA_TUSERDATA = LuaValType(C.LUA_TUSERDATA) + LUA_TTHREAD = LuaValType(C.LUA_TTHREAD) + LUA_TLIGHTUSERDATA = LuaValType(C.LUA_TLIGHTUSERDATA) +) + +const ( + LUA_OK = C.LUA_OK + LUA_VERSION = C.LUA_VERSION + LUA_RELEASE = C.LUA_RELEASE + LUA_VERSION_NUM = C.LUA_VERSION_NUM + LUA_COPYRIGHT = C.LUA_COPYRIGHT + LUA_AUTHORS = C.LUA_AUTHORS + LUA_MULTRET = C.LUA_MULTRET + LUA_REGISTRYINDEX = C.LUA_REGISTRYINDEX + LUA_YIELD = C.LUA_YIELD + LUA_ERRRUN = C.LUA_ERRRUN + LUA_ERRSYNTAX = C.LUA_ERRSYNTAX + LUA_ERRMEM = C.LUA_ERRMEM + LUA_ERRERR = C.LUA_ERRERR + LUA_TNONE = C.LUA_TNONE + LUA_MINSTACK = C.LUA_MINSTACK + LUA_GCSTOP = C.LUA_GCSTOP + LUA_GCRESTART = C.LUA_GCRESTART + LUA_GCCOLLECT = C.LUA_GCCOLLECT + LUA_GCCOUNT = C.LUA_GCCOUNT + LUA_GCCOUNTB = C.LUA_GCCOUNTB + LUA_GCSTEP = C.LUA_GCSTEP + LUA_GCSETPAUSE = C.LUA_GCSETPAUSE + LUA_GCSETSTEPMUL = C.LUA_GCSETSTEPMUL + LUA_HOOKCALL = C.LUA_HOOKCALL + LUA_HOOKRET = C.LUA_HOOKRET + LUA_HOOKLINE = C.LUA_HOOKLINE + LUA_HOOKCOUNT = C.LUA_HOOKCOUNT + LUA_MASKCALL = C.LUA_MASKCALL + LUA_MASKRET = C.LUA_MASKRET + LUA_MASKLINE = C.LUA_MASKLINE + LUA_MASKCOUNT = C.LUA_MASKCOUNT + LUA_ERRFILE = C.LUA_ERRFILE + LUA_NOREF = C.LUA_NOREF + LUA_REFNIL = C.LUA_REFNIL + LUA_FILEHANDLE = C.LUA_FILEHANDLE + LUA_COLIBNAME = C.LUA_COLIBNAME + LUA_TABLIBNAME = C.LUA_TABLIBNAME + LUA_IOLIBNAME = C.LUA_IOLIBNAME + LUA_OSLIBNAME = C.LUA_OSLIBNAME + LUA_STRLIBNAME = C.LUA_STRLIBNAME + LUA_MATHLIBNAME = C.LUA_MATHLIBNAME + LUA_DBLIBNAME = C.LUA_DBLIBNAME + LUA_LOADLIBNAME = C.LUA_LOADLIBNAME +) diff --git a/golua/lua/lua_test.go b/golua/lua/lua_test.go new file mode 100644 index 0000000..7d86c7e --- /dev/null +++ b/golua/lua/lua_test.go @@ -0,0 +1,437 @@ +package lua + +import ( + "testing" + "unsafe" +) + +type TestStruct struct { + IntField int + StringField string + FloatField float64 +} + +func TestGoStruct(t *testing.T) { + L := NewState() + L.OpenLibs() + defer L.Close() + + ts := &TestStruct{10, "test", 2.3} + + L.CheckStack(1) + + L.PushGoStruct(ts) + L.SetGlobal("t") + + L.GetGlobal("t") + if !L.IsGoStruct(-1) { + t.Fatal("Not go struct") + } + + tsr := L.ToGoStruct(-1).(*TestStruct) + if tsr != ts { + t.Fatal("Retrieved something different from what we inserted") + } + + L.Pop(1) + + L.PushString("This is not a struct") + if L.ToGoStruct(-1) != nil { + t.Fatal("Non-GoStruct value attempted to convert into GoStruct should result in nil") + } + + L.Pop(1) +} + +func TestCheckStringSuccess(t *testing.T) { + L := NewState() + L.OpenLibs() + defer L.Close() + + Test := func(L *State) int { + L.PushString("this is a test") + L.CheckString(-1) + return 0 + } + + L.Register("test", Test) + err := L.DoString("test()") + if err != nil { + t.Fatalf("DoString did return an error: %v\n", err.Error()) + } +} + +func TestCheckStringFail(t *testing.T) { + L := NewState() + L.OpenLibs() + defer L.Close() + + Test := func(L *State) int { + L.CheckString(-1) + return 0 + } + + L.Register("test", Test) + err := L.DoString("test();") + if err == nil { + t.Fatal("DoString did not return an error\n") + } +} + +func TestPCallHidden(t *testing.T) { + L := NewState() + L.OpenLibs() + defer L.Close() + + err := L.DoString("pcall(print, \"ciao\")") + if err == nil { + t.Fatal("Can use pcall\n") + } + + err = L.DoString("unsafe_pcall(print, \"ciao\")") + if err != nil { + t.Fatal("Can not use unsafe_pcall\n") + } +} + +func TestCall(t *testing.T) { + L := NewState() + L.OpenLibs() + defer L.Close() + + test := func(L *State) int { + arg1 := L.ToString(1) + arg2 := L.ToString(2) + arg3 := L.ToString(3) + + if arg1 != "Argument1" { + t.Fatal("Got wrong argument (1)") + } + + if arg2 != "Argument2" { + t.Fatal("Got wrong argument (2)") + } + + if arg3 != "Argument3" { + t.Fatal("Got wrong argument (3)") + } + + L.PushString("Return1") + L.PushString("Return2") + + return 2 + } + + L.Register("test", test) + + L.PushString("Dummy") + L.GetGlobal("test") + L.PushString("Argument1") + L.PushString("Argument2") + L.PushString("Argument3") + err := L.Call(3, 2) + + if err != nil { + t.Fatalf("Error executing call: %v\n", err) + } + + dummy := L.ToString(1) + ret1 := L.ToString(2) + ret2 := L.ToString(3) + + if dummy != "Dummy" { + t.Fatal("The stack was disturbed") + } + + if ret1 != "Return1" { + t.Fatalf("Wrong return value (1) got: <%s>", ret1) + } + + if ret2 != "Return2" { + t.Fatalf("Wrong return value (2) got: <%s>", ret2) + } +} + +// equivalent to basic.go +func TestLikeBasic(t *testing.T) { + L := NewState() + defer L.Close() + L.OpenLibs() + + testCalled := 0 + + test := func(L *State) int { + testCalled++ + return 0 + } + + test2Arg := -1 + test2Argfrombottom := -1 + test2 := func(L *State) int { + test2Arg = L.CheckInteger(-1) + test2Argfrombottom = L.CheckInteger(1) + return 0 + } + + L.GetGlobal("print") + L.PushString("Hello World!") + if err := L.Call(1, 0); err != nil { + t.Fatalf("Call to print returned error") + } + + L.PushGoFunction(test) + L.PushGoFunction(test) + L.PushGoFunction(test) + L.PushGoFunction(test2) + L.PushInteger(42) + if err := L.Call(1, 0); err != nil { + t.Fatalf("Call to print returned error") + } + if (test2Arg != 42) || (test2Argfrombottom != 42) { + t.Fatalf("Call to test2 didn't work") + } + + if err := L.Call(0, 0); err != nil { + t.Fatalf("Call to print returned error") + } + if err := L.Call(0, 0); err != nil { + t.Fatalf("Call to print returned error") + } + if err := L.Call(0, 0); err != nil { + t.Fatalf("Call to print returned error") + } + if testCalled != 3 { + t.Fatalf("Test function not called the correct number of times: %d\n", testCalled) + } + + // this will fail as we didn't register test2 function + if err := L.DoString("test2(42)"); err == nil { + t.Fatal("No error when calling unregistered function") + } +} + +// equivalent to quickstart.go +func TestLikeQuickstart(t *testing.T) { + adder := func(L *State) int { + a := L.ToInteger(1) + b := L.ToInteger(2) + L.PushInteger(int64(a + b)) + return 1 + } + + L := NewState() + defer L.Close() + L.OpenLibs() + + L.Register("adder", adder) + + if err := L.DoString("return adder(2, 2)"); err != nil { + t.Fatalf("Error during call to adder: %v\n", err) + } + if r := L.ToInteger(1); r != 4 { + t.Fatalf("Wrong return value from adder (was: %d)\n", r) + } +} + +// equivalent to userdata.go +func TestLikeUserdata(t *testing.T) { + type Userdata struct { + a, b int + } + + userDataProper := func(L *State) { + rawptr := L.NewUserdata(uintptr(unsafe.Sizeof(Userdata{}))) + var ptr *Userdata + ptr = (*Userdata)(rawptr) + ptr.a = 2 + ptr.b = 3 + + rawptr2 := L.ToUserdata(-1) + ptr2 := (*Userdata)(rawptr2) + + if ptr != ptr2 { + t.Fatalf("Failed to create userdata\n") + } + } + + testCalled := 0 + test := func(L *State) int { + testCalled++ + return 0 + } + + goDefinedFunctions := func(L *State) { + // example_function is registered inside Lua VM + L.Register("test", test) + + // This code demonstrates checking that a value on the stack is a go function + L.CheckStack(1) + L.GetGlobal("test") + if !L.IsGoFunction(-1) { + t.Fatalf("IsGoFunction failed to recognize a Go function object") + } + L.Pop(1) + + // We call example_function from inside Lua VM + testCalled = 0 + if err := L.DoString("test()"); err != nil { + t.Fatalf("Error executing test function: %v\n", err) + } + if testCalled != 1 { + t.Fatalf("It appears the test function wasn't actually called\n") + } + } + + type TestObject struct { + AField int + } + + goDefinedObjects := func(L *State) { + z := &TestObject{42} + + L.PushGoStruct(z) + L.SetGlobal("z") + + // This code demonstrates checking that a value on the stack is a go object + L.CheckStack(1) + L.GetGlobal("z") + if !L.IsGoStruct(-1) { + t.Fatal("IsGoStruct failed to recognize a Go struct\n") + } + L.Pop(1) + + // This code demonstrates access and assignment to a field of a go object + if err := L.DoString("return z.AField"); err != nil { + t.Fatal("Couldn't execute code") + } + before := L.ToInteger(-1) + L.Pop(1) + if before != 42 { + t.Fatalf("Wrong value of z.AField before change (%d)\n", before) + } + if err := L.DoString("z.AField = 10;"); err != nil { + t.Fatal("Couldn't execute code") + } + if err := L.DoString("return z.AField"); err != nil { + t.Fatal("Couldn't execute code") + } + after := L.ToInteger(-1) + L.Pop(1) + if after != 10 { + t.Fatalf("Wrong value of z.AField after change (%d)\n", after) + } + } + + L := NewState() + defer L.Close() + L.OpenLibs() + + userDataProper(L) + goDefinedFunctions(L) + goDefinedObjects(L) +} + +func TestStackTrace(t *testing.T) { + L := NewState() + defer L.Close() + L.OpenLibs() + + err := L.DoFile("../_example/calls.lua") + if err == nil { + t.Fatal("No error returned from the execution of calls.lua") + } + + le := err.(*LuaError) + + if le.Code() != LUA_ERRERR { + t.Fatalf("Wrong kind of error encountered running calls.lua: %v (%d %d)\n", le, le.Code(), LUA_ERRERR) + } + + if len(le.StackTrace()) != 6 { + t.Fatalf("Wrong size of stack trace (%v)\n", le.StackTrace()) + } +} + +func TestConv(t *testing.T) { + L := NewState() + defer L.Close() + L.OpenLibs() + + L.PushString("10") + n := L.ToNumber(-1) + if n != 10 { + t.Fatalf("Wrong conversion (str -> int)") + } + if L.Type(-1) != LUA_TSTRING { + t.Fatalf("Wrong type (str)") + } + + L.Pop(1) + + L.PushInteger(10) + s := L.ToString(-1) + if s != "10" { + t.Fatalf("Wrong conversion (int -> str)") + } + + L.Pop(1) + + L.PushString("a\000test") + s = L.ToString(-1) + if s != "a\000test" { + t.Fatalf("Wrong conversion (str -> str): <%s>", s) + } +} + +func TestDumpAndLoad(t *testing.T) { + L := NewState() + defer L.Close() + L.OpenLibs() + + loadret := L.LoadString(`print("msg from dump_and_load_test")`) + if loadret != 0 { + t.Fatalf("LoadString error: %v", loadret) + } + dumpret := L.Dump() + if dumpret != 0 { + t.Fatalf("Dump error: %v", dumpret) + } + + isstring := L.IsString(-1) + if !isstring { + t.Fatalf("stack top not a string") + } + bytecodes := L.ToBytes(-1) + loadret = L.Load(bytecodes, "chunk_from_dump_and_load_test") + if loadret != 0 { + t.Fatalf("Load error: %v", loadret) + } + err := L.Call(0, 0) + if err != nil { + t.Fatalf("Call error: %v", err) + } +} + +func TestCustomDebugHook(t *testing.T) { + L := NewState() + defer L.Close() + + L.SetHook(func(l *State) { + l.RaiseError("stop") + }, 1) + + err := L.DoString(` + local x = 0 + while(1 ~= 0) do + x = 2 + end + `) + + if err == nil { + t.Fatalf("Script should have raised an error") + } else { + if err.Error() != "stop" { + t.Fatal("Error should be coming from the hook") + } + } +}