This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
forked from mtabini/go-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libs.go
54 lines (53 loc) · 1.99 KB
/
libs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package lua
// OpenLibraries opens all standard libraries. Alternatively, the host program
// can open them individually by using Require to call BaseOpen (for the basic
// library), PackageOpen (for the package library), CoroutineOpen (for the
// coroutine library), StringOpen (for the string library), TableOpen (for the
// table library), MathOpen (for the mathematical library), Bit32Open (for the
// bit library), IOOpen (for the I/O library), OSOpen (for the Operating System
// library), and DebugOpen (for the debug library).
//
// The standard Lua libraries provide useful functions that are implemented
// directly through the Go API. Some of these functions provide essential
// services to the language (e.g. Type and MetaTable); others provide access
// to "outside" services (e.g. I/O); and others could be implemented in Lua
// itself, but are quite useful or have critical performance requirements that
// deserve an implementation in Go (e.g. table.sort).
//
// All libraries are implemented through the official Go API. Currently, Lua
// has the following standard libraries:
// basic library
// package library
// string manipulation
// table manipulation
// mathematical functions (sin, log, etc.);
// bitwise operations
// input and output
// operating system facilities
// debug facilities
// Except for the basic and the package libraries, each library provides all
// its functions as fields of a global table or as methods of its objects.
func OpenLibraries(l *State, preloaded ...RegistryFunction) {
libs := []RegistryFunction{
{"_G", BaseOpen},
{"package", PackageOpen},
// {"coroutine", CoroutineOpen},
{"table", TableOpen},
{"io", IOOpen},
{"os", OSOpen},
{"string", StringOpen},
{"bit32", Bit32Open},
{"math", MathOpen},
{"debug", DebugOpen},
}
for _, lib := range libs {
Require(l, lib.Name, lib.Function, true)
l.Pop(1)
}
SubTable(l, RegistryIndex, "_PRELOAD")
for _, lib := range preloaded {
l.PushGoFunction(lib.Function)
l.SetField(-2, lib.Name)
}
l.Pop(1)
}