You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- By default, GopherLua does not show Go stack traces when panics occur.
206
207
- You can get Go stack traces by setting this to ``true`` .
@@ -222,7 +223,7 @@ Calling Go from Lua
222
223
L.Push(lua.LNumber(lv * 2)) /* push result */
223
224
return 1 /* number of results */
224
225
}
225
-
226
+
226
227
func main() {
227
228
L := lua.NewState()
228
229
defer L.Close()
@@ -252,17 +253,52 @@ Working with coroutines.
252
253
fmt.Println(err.Error())
253
254
break
254
255
}
255
-
256
+
256
257
for i, lv := range values {
257
258
fmt.Printf("%v : %v\n", i, lv)
258
259
}
259
-
260
+
260
261
if st == lua.ResumeOK {
261
262
fmt.Println("yield break(ok)")
262
263
break
263
264
}
264
265
}
265
266
267
+
+++++++++++++++++++++++++++++++++++++++++
268
+
Opening a subset of builtin modules
269
+
+++++++++++++++++++++++++++++++++++++++++
270
+
271
+
The following demonstrates how to open a subset of the built-in modules in Lua, say for example to avoid enabling modules with access to local files or system calls.
272
+
273
+
main.go
274
+
275
+
.. code-block:: go
276
+
277
+
package main
278
+
279
+
import (
280
+
"github.com/yuin/gopher-lua"
281
+
)
282
+
283
+
func main() {
284
+
L := lua.NewState(lua.Options{SkipOpenLibs: true})
285
+
defer L.Close()
286
+
for _, loader := range []lua.LGFunction{
287
+
lua.OpenLoad, // Must be first!
288
+
lua.OpenBase, // Contains most builtins, pretty much required
289
+
lua.OpenString,
290
+
lua.OpenMath,
291
+
lua.OpenTable,
292
+
lua.OpenCoroutine,
293
+
lua.OpenChannel,
294
+
} {
295
+
_ = loader(L)
296
+
}
297
+
if err := L.DoFile("main.lua"); err != nil {
298
+
panic(err)
299
+
}
300
+
}
301
+
266
302
+++++++++++++++++++++++++++++++++++++++++
267
303
Creating a module by Go
268
304
+++++++++++++++++++++++++++++++++++++++++
@@ -272,26 +308,26 @@ mymodule.go
272
308
.. code-block:: go
273
309
274
310
package mymodule
275
-
311
+
276
312
import (
277
313
"github.com/yuin/gopher-lua"
278
314
)
279
-
315
+
280
316
func Loader(L *lua.LState) int {
281
317
// register functions to the table
282
318
mod := L.SetFuncs(L.NewTable(), exports)
283
319
// register other stuff
284
320
L.SetField(mod, "name", lua.LString("value"))
285
-
321
+
286
322
// returns the module
287
323
L.Push(mod)
288
324
return 1
289
325
}
290
-
326
+
291
327
var exports = map[string]lua.LGFunction{
292
328
"myfunc": myfunc,
293
329
}
294
-
330
+
295
331
func myfunc(L *lua.LState) int {
296
332
return 0
297
333
}
@@ -301,12 +337,12 @@ mymain.go
301
337
.. code-block:: go
302
338
303
339
package main
304
-
340
+
305
341
import (
306
342
"./mymodule"
307
343
"github.com/yuin/gopher-lua"
308
344
)
309
-
345
+
310
346
func main() {
311
347
L := lua.NewState()
312
348
defer L.Close()
@@ -359,9 +395,9 @@ You can extend GopherLua with new types written in Go.
359
395
type Person struct {
360
396
Name string
361
397
}
362
-
398
+
363
399
const luaPersonTypeName = "person"
364
-
400
+
365
401
// Registers my person type to given L.
366
402
func registerPersonType(L *lua.LState) {
367
403
mt := L.NewTypeMetatable(luaPersonTypeName)
@@ -371,7 +407,7 @@ You can extend GopherLua with new types written in Go.
0 commit comments