Skip to content

Commit b688a86

Browse files
committed
Improve docs
1 parent 69f5a28 commit b688a86

File tree

7 files changed

+63
-46
lines changed

7 files changed

+63
-46
lines changed

_vm.go

-11
Original file line numberDiff line numberDiff line change
@@ -955,17 +955,6 @@ func equals(L *LState, lhs, rhs LValue, raw bool) bool {
955955
return ret
956956
}
957957

958-
func tostring(L *LState, lv LValue) LValue {
959-
if fn, ok := L.metaOp1(lv, "__tostring").assertFunction(); ok {
960-
L.Push(fn)
961-
L.Push(lv)
962-
L.Call(1, 1)
963-
return L.reg.Pop()
964-
} else {
965-
return LString(lv.String())
966-
}
967-
}
968-
969958
func objectRationalWithError(L *LState, lhs, rhs LValue, event string) bool {
970959
switch objectRational(L, lhs, rhs, event) {
971960
case 1:

auxlib.go

+13
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,19 @@ func (ls *LState) OpenLibs() {
398398

399399
/* GopherLua original APIs {{{ */
400400

401+
// ToStringMeta returns string representation of given LValue.
402+
// This method calls the `__tostring` meta method if defined.
403+
func (ls *LState) ToStringMeta(lv LValue) LValue {
404+
if fn, ok := ls.metaOp1(lv, "__tostring").assertFunction(); ok {
405+
ls.Push(fn)
406+
ls.Push(lv)
407+
ls.Call(1, 1)
408+
return ls.reg.Pop()
409+
} else {
410+
return LString(lv.String())
411+
}
412+
}
413+
401414
// Set a module loader to the package.preload table.
402415
func (ls *LState) PreloadModule(name string, loader LGFunction) {
403416
preload := ls.GetField(ls.GetField(ls.Get(EnvironIndex), "package"), "preload")

baselib.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func basePCall(L *LState) int {
268268
func basePrint(L *LState) int {
269269
top := L.GetTop()
270270
for i := 1; i <= top; i++ {
271-
fmt.Print(tostring(L, L.Get(i)).String())
271+
fmt.Print(L.ToStringMeta(L.Get(i)).String())
272272
if i != top {
273273
fmt.Print("\t")
274274
}
@@ -409,7 +409,7 @@ func baseToNumber(L *LState) int {
409409

410410
func baseToString(L *LState) int {
411411
v1 := L.CheckAny(1)
412-
L.Push(tostring(L, v1))
412+
L.Push(L.ToStringMeta(v1))
413413
return 1
414414
}
415415

state.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was generated by go-inline.DO NOT EDIT.
1+
// This file was generated by go-inline. DO NOT EDIT.
22
package lua
33

44
import (
@@ -758,7 +758,7 @@ func (ls *LState) pushCallFrame(cf callFrame, fn LValue, meta bool) { // +inline
758758
ls.RaiseError("stack overflow")
759759
}
760760
// this section is inlined by go-inline
761-
// source function 'is' func (cs *callFrameStack) Push(v callFrame) in _state.go
761+
// source function is 'func (cs *callFrameStack) Push(v callFrame) ' in '_state.go'
762762
{
763763
cs := ls.stack
764764
v := cf
@@ -768,7 +768,7 @@ func (ls *LState) pushCallFrame(cf callFrame, fn LValue, meta bool) { // +inline
768768
}
769769
newcf := ls.stack.Last()
770770
// this section is inlined by go-inline
771-
// source function 'is' func (ls *LState) initCallFrame(cf *callFrame) in _state.go
771+
// source function is 'func (ls *LState) initCallFrame(cf *callFrame) ' in '_state.go'
772772
{
773773
cf := newcf
774774
if cf.Fn.IsG {

table.go

+17
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func newLTable(acap int, hcap int) *LTable {
4848
return tb
4949
}
5050

51+
// Len returns length of this LTable.
5152
func (tb *LTable) Len() int {
5253
if tb.array == nil {
5354
return 0
@@ -63,13 +64,15 @@ func (tb *LTable) Len() int {
6364
return 0
6465
}
6566

67+
// Append appends a given LValue to this LTable.
6668
func (tb *LTable) Append(value LValue) {
6769
if tb.array == nil {
6870
tb.array = make([]LValue, 0, defaultArrayCap)
6971
}
7072
tb.array = append(tb.array, value)
7173
}
7274

75+
// Insert inserts a given LValue at position `i` in this table.
7376
func (tb *LTable) Insert(i int, value LValue) {
7477
if tb.array == nil {
7578
tb.array = make([]LValue, 0, defaultArrayCap)
@@ -88,6 +91,7 @@ func (tb *LTable) Insert(i int, value LValue) {
8891
tb.array[i] = value
8992
}
9093

94+
// MaxN returns a maximum number key that nil value does not exist before it.
9195
func (tb *LTable) MaxN() int {
9296
if tb.array == nil {
9397
return 0
@@ -100,6 +104,7 @@ func (tb *LTable) MaxN() int {
100104
return 0
101105
}
102106

107+
// Remove removes from this table the element at a given position.
103108
func (tb *LTable) Remove(pos int) LValue {
104109
if tb.array == nil {
105110
return LNil
@@ -122,6 +127,9 @@ func (tb *LTable) Remove(pos int) LValue {
122127
return oldval
123128
}
124129

130+
// RawSet sets a given LValue to a given index without the __newindex metamethod.
131+
// It is recommended to use `RawSetString` or `RawSetInt` for performance
132+
// if you already know the given LValue is a string or number.
125133
func (tb *LTable) RawSet(key LValue, value LValue) {
126134
switch v := key.(type) {
127135
case LNumber:
@@ -152,6 +160,7 @@ func (tb *LTable) RawSet(key LValue, value LValue) {
152160
tb.RawSetH(key, value)
153161
}
154162

163+
// RawSetInt sets a given LValue at a position `key` without the __newindex metamethod.
155164
func (tb *LTable) RawSetInt(key int, value LValue) {
156165
if key < 1 || key >= MaxArrayIndex {
157166
tb.RawSetH(LNumber(key), value)
@@ -175,6 +184,7 @@ func (tb *LTable) RawSetInt(key int, value LValue) {
175184
}
176185
}
177186

187+
// RawSetString sets a given LValue to a given string index without the __newindex metamethod.
178188
func (tb *LTable) RawSetString(key string, value LValue) {
179189
if tb.strdict == nil {
180190
tb.strdict = make(map[string]LValue, defaultHashCap)
@@ -186,6 +196,7 @@ func (tb *LTable) RawSetString(key string, value LValue) {
186196
}
187197
}
188198

199+
// RawSetH sets a given LValue to a given index without the __newindex metamethod.
189200
func (tb *LTable) RawSetH(key LValue, value LValue) {
190201
if s, ok := key.(LString); ok {
191202
tb.RawSetString(string(s), value)
@@ -202,6 +213,7 @@ func (tb *LTable) RawSetH(key LValue, value LValue) {
202213
}
203214
}
204215

216+
// RawGet returns an LValue associated with a given key without __index metamethod.
205217
func (tb *LTable) RawGet(key LValue) LValue {
206218
switch v := key.(type) {
207219
case LNumber:
@@ -233,6 +245,7 @@ func (tb *LTable) RawGet(key LValue) LValue {
233245
return LNil
234246
}
235247

248+
// RawGetInt returns an LValue at position `key` without __index metamethod.
236249
func (tb *LTable) RawGetInt(key int) LValue {
237250
if tb.array == nil {
238251
return LNil
@@ -244,6 +257,7 @@ func (tb *LTable) RawGetInt(key int) LValue {
244257
return tb.array[index]
245258
}
246259

260+
// RawGet returns an LValue associated with a given key without __index metamethod.
247261
func (tb *LTable) RawGetH(key LValue) LValue {
248262
if s, sok := key.(LString); sok {
249263
if tb.strdict == nil {
@@ -263,6 +277,7 @@ func (tb *LTable) RawGetH(key LValue) LValue {
263277
return LNil
264278
}
265279

280+
// RawGetString returns an LValue associated with a given key without __index metamethod.
266281
func (tb *LTable) RawGetString(key string) LValue {
267282
if tb.strdict == nil {
268283
return LNil
@@ -273,6 +288,7 @@ func (tb *LTable) RawGetString(key string) LValue {
273288
return LNil
274289
}
275290

291+
// ForEach iterates over this table of elements, yielding each in turn to a given function.
276292
func (tb *LTable) ForEach(cb func(LValue, LValue)) {
277293
if tb.array != nil {
278294
for i, v := range tb.array {
@@ -297,6 +313,7 @@ func (tb *LTable) ForEach(cb func(LValue, LValue)) {
297313
}
298314
}
299315

316+
// This function is equivalent to lua_next ( http://www.lua.org/manual/5.1/manual.html#lua_next ).
300317
func (tb *LTable) Next(key LValue) (LValue, LValue) {
301318
// TODO: inefficient way
302319
if key == LNil {

value.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ type LValue interface {
3636
assertFunction() (*LFunction, bool)
3737
}
3838

39+
// LVIsFalse returns true if a given LValue is a nil or false otherwise false.
3940
func LVIsFalse(v LValue) bool { return v == LNil || v == LFalse }
40-
func LVAsBool(v LValue) bool { return v != LNil && v != LFalse }
41+
42+
// LVIsFalse returns false if a given LValue is a nil or false otherwise true.
43+
func LVAsBool(v LValue) bool { return v != LNil && v != LFalse }
44+
45+
// LVAsString returns string representation of a given LValue
46+
// if the LValue is a string or number, otherwise an empty string.
4147
func LVAsString(v LValue) string {
4248
switch sn := v.(type) {
4349
case LString, LNumber:
@@ -47,6 +53,8 @@ func LVAsString(v LValue) string {
4753
}
4854
}
4955

56+
// LVCanConvToString returns true if a given LValue is a string or number
57+
// otherwise false.
5058
func LVCanConvToString(v LValue) bool {
5159
switch v.(type) {
5260
case LString, LNumber:
@@ -56,6 +64,7 @@ func LVCanConvToString(v LValue) bool {
5664
}
5765
}
5866

67+
// LVAsNumber tries to convert a given LValue to a number.
5968
func LVAsNumber(v LValue) LNumber {
6069
switch lv := v.(type) {
6170
case LNumber:

0 commit comments

Comments
 (0)