Socket
Socket
Sign inDemoInstall

github.com/zxh/luar

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/zxh/luar

Package luar provides a convenient interface between Lua and Go. It uses Alessandro Arzilli's golua (https://github.com/aarzilli/golua). Most Go values can be passed to Lua: basic types, strings, complex numbers, user-defined types, pointers, composite types, functions, channels, etc. Conversely, most Lua values can be converted to Go values. Composite types are processed recursively. Methods can be called on user-defined types. These methods will be callable using _dot-notation_ rather than colon notation. Arrays, slices, maps and structs can be copied as tables, or alternatively passed over as Lua proxy objects which can be naturally indexed. In the case of structs and string maps, fields have priority over methods. Use 'luar.method(<value>, <method>)(<params>...)' to call shadowed methods. Unexported struct fields are ignored. The "lua" tag is used to match fields in struct conversion. You may pass a Lua table to an imported Go function; if the table is 'array-like' then it is converted to a Go slice; if it is 'map-like' then it is converted to a Go map. Pointer values encode as the value pointed to when unproxified. Usual operators (arithmetic, string concatenation, pairs/ipairs, etc.) work on proxies too. The type of the result depends on the type of the operands. The rules are as follows: - If the operands are of the same type, use this type. - If one type is a Lua number, use the other, user-defined type. - If the types are different and not Lua numbers, convert to a complex proxy, a Lua number, or a Lua string according to the result kind. Channel proxies can be manipulated with the following methods: - close(): Close the channel. - recv() value: Fetch and return a value from the channel. - send(x value): Send a value in the channel. Complex proxies can be manipulated with the following attributes: - real: The real part. - imag: The imaginary part. Slice proxies can be manipulated with the following methods/attributes: - append(x ...value) sliceProxy: Append the elements and return the new slice. The elements must be convertible to the slice element type. - cap: The capacity of the slice. - slice(i, j integer) sliceProxy: Return the sub-slice that ranges from 'i' to 'j' excluded, starting from 1. String proxies can be browsed rune by rune with the pairs/ipairs functions. These runes are encoded as strings in Lua. Indexing a string proxy (starting from 1) will return the corresponding byte as a Lua string. String proxies can be manipulated with the following method: - slice(i, j integer) sliceProxy: Return the sub-string that ranges from 'i' to 'j' excluded, starting from 1. Pointers to structs and structs within pointers are automatically dereferenced. Slices must be looped over with 'ipairs'.


Version published

Readme

Source

Luar: Lua reflection bindings for Go

Luar is designed to make using Lua from Go more convenient. Go structs, slices and maps can be automatically converted to Lua tables and vice-versa. The resulting conversion can either be a copy or a proxy. In the latter case, any change made to the result will reflect on the source.

Any Go function can be made available to Lua scripts, without having to write C-style wrappers.

Luar support cyclic structures (map[string]interface{}, lists, etc.).

User-defined types can be made available to Lua as well: their exported methods can be called and usual operations such as indexing or arithmetic can be performed.

See the documentation for usage instructions and examples.

Installation

Install with

go get <repo>/luar

Luar uses Alessandro Arzilli's golua. See golua's homepage for further installation details.

REPL

Version 1.x features an example REPL that is available in the cmd folder.

Changelog

Luar 2

This is a rewrite of 1.x with extended features and a cleaner API. The main differences with the previous version:

  • The function prototypes of GoToLua and LuaToGo are simpler and do not require the use of reflection from the callers. The dontproxify argument is gone, use GoToLuaProxy to control proxification.

  • The Copy* functions and GoLuaFunc are gone, use GoToLua and LuaToGo instead.

  • Use Register instead of RawRegister.

  • InitProxies is gone since it was not needed.

  • The LuaObject and LuaTableIter structure fields are unexported.

  • LuaObject methods not only work on Lua functions but also on anything with a __call metamethods. Idem for tables and the __index/__newindex metamethods.

  • Use NewLuaObjectFromName(L, "_G") instead of Global.

  • Lookup and Geti gone. Instead the Get and GetObject functions are variadic: each subfield argument can be any valid Lua key (string, integer...).

  • Use (*LuaObject) Call instead of (*LuaObject) Callf. The protoype of (*LuaObject) Call has changed in a fashion similar to GoToLua and LuaToGo. Types is gone as it is no longer needed.

  • Register ProxyIpairs and ProxyPairs instead of calling LuarSetup.

  • Register and use Unproxify instead of ArrayToTable, MapToTable, ProxyRaw, SliceToTable and StructToTable.

  • ComplexReal and ComplexImag have been replaced by the proxy attributes real and imag, respectively.

  • SliceSub and SliceAppend have been replaced by the proxy methods slice and append, respectively. Slice proxies have the cap metamethod alongside append and slice.

  • String proxies have a slice method just like slice proxies. They can be looped rune-by-rune over with ipairs.

The range of supported conversion has been extended:

  • LuaToGo can convert to interfaces and pointers with several levels of indirection.

  • LuaToGo can convert to non-empty maps and structs.

Luar v2

The v1 API suffers from several design issues: The GoToLua and LuaToGo functions take a reflect.Type parameter, which is bad design. Sadly changing this would break backward compatibility.

The v2 rewrite fixes the design issues, usability and other bugs. Overall it greatly simplifies the API.

Check it out in the v2 branch of this repository.

FAQs

Last updated on 17 Aug 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc