examples/

directory
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2026 License: MIT

README

Examples

Runnable programs for the luapure embedding API. Each is a standalone main.go importing github.com/htcom-code/lua-pure/lua (no other dependencies), so they compile under go build ./... and stay current with the API.

Run one with go run ./examples/<name>:

example shows
basic the README quickstart — expose a Go function, run a script, read results
embed hand a Go table to a script; ToValue/FromValue data round-trip
userdata bind a Go type as userdata with a method table and CheckUserData
sandbox NewSandbox + per-call _ENV (RunWith) + a SetContext deadline
channel a Go channel as userdata: two goroutines/LStates message-pass
channel-timeout cancellable channel recv via L.Context() + a SetContext deadline
openlib write a library that installs like a built-in (OpenString-style): a free OpenX(L) opener that exposes Go helpers as a global
customlib register host modules with Requiref (eager) and Preload (lazy)
debugger in-process Debugger: set a breakpoint, inspect the frame, Eval in scope, resume

For small, doc-integrated snippets see the runnable Example functions in lua/example_test.go (they render in make doc-web). The full API surface is in make doc; differences from PUC are in docs/COMPATIBILITY.md.

Directories

Path Synopsis
basic is the README quickstart as a runnable program: embed the VM, expose a Go function, run a script, and read the results back.
basic is the README quickstart as a runnable program: embed the VM, expose a Go function, run a script, and read the results back.
channel exposes a Go channel to Lua as userdata so two goroutines, each with its own LState, communicate by message passing.
channel exposes a Go channel to Lua as userdata so two goroutines, each with its own LState, communicate by message passing.
channel-timeout shows the cancellable receive pattern: a Go callback that blocks on a channel reads L.Context() and selects on its Done() channel, so a SetContext deadline frees the goroutine instead of blocking forever.
channel-timeout shows the cancellable receive pattern: a Go callback that blocks on a channel reads L.Context() and selects on its Done() channel, so a SetContext deadline frees the goroutine instead of blocking forever.
customlib registers host-provided modules so scripts can require() them: Requiref opens a module eagerly (and optionally as a global), Preload registers a lazy loader that runs on the first require.
customlib registers host-provided modules so scripts can require() them: Requiref opens a module eagerly (and optionally as a global), Preload registers a lazy loader that runs on the first require.
debugger drives the in-process Debugger: set a breakpoint, run a script, and each time it stops inspect the frame and evaluate expressions in its scope, then resume.
debugger drives the in-process Debugger: set a breakpoint, run a script, and each time it stops inspect the frame and evaluate expressions in its scope, then resume.
embed shows the data round-trip: build a table in Go and hand it to a script as a global, then convert Go values to Lua and back with ToValue/FromValue.
embed shows the data round-trip: build a table in Go and hand it to a script as a global, then convert Go values to Lua and back with ToValue/FromValue.
openlib shows how to write your own library that installs like a built-in one.
openlib shows how to write your own library that installs like a built-in one.
sandbox runs untrusted-ish code safely: a state with only safe libraries, a per-call _ENV that confines globals, and a deadline that stops a runaway loop.
sandbox runs untrusted-ish code safely: a state with only safe libraries, a per-call _ENV that confines globals, and a deadline that stops a runaway loop.
userdata binds a Go type into Lua: register a named metatable, give it a method table, and hand scripts a userdatum they call methods on.
userdata binds a Go type into Lua: register a named metatable, give it a method table, and hand scripts a userdatum they call methods on.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL