Skip to content

Latest commit

 

History

History
135 lines (98 loc) · 2.43 KB

README.md

File metadata and controls

135 lines (98 loc) · 2.43 KB

lj4lj

NOTE: this is a work in progress as of 2024-05-25

lj4lj is an FFI wrapper for luajit's API, written in Yue.

This allows code to create and interact with lua_States.

Example

Using the Light implementation:

In Yue:

import 'lj4lj' as lj4lj

s = lj4lj.Light!

print_num = =>
  num = @s\checkinteger 1
  print num

  0

s\bind_func print_num, 'print_num', 0
s\dostring 'print_num(1)'
s\close!

In Lua:

local lj4lj = require('lj4lj')

local s = lj4lj.Light()

local print_num
print_num = function(self)
	local num = self.s:checkinteger(1)
	print(num)

	return 0
end

s:bind_func(print_num, 'print_num', 0)
s:dostring('print_num(1)')
s:close()

API

lj4lj offers two ways to interact with the Lua API. The first mechanism is through the bare Lua type, which is mostly a direct transformation of luajit/src/lj_api.c. The second is the Light type. Light is an opinionated wrapper around the standard APIs and doesn't require much knowledge of the Lua API itself.

Lua

The Lua type is mostly a direct transformation of luajit/src/lj_api.c. Construction of a new instance calls luaL_newstate, so even if you do not use it, you should close it.

How the Lua API actually works is not documented here. Please read Lua's documentation if you plan on using this wrapper.

Example use:

s = Lua()
s:openlibs()

s:pushstring('TEST_GLOBAL_VALUE')
s:setglobal('_TEST_GLOBAL')

s:loadstring('print(_TEST_GLOBAL)')
s:pcall()

s:close()

Methods

State

  • Lua:close()
  • Lua:pcall()
  • Lua:openlibs()
  • Lua:loadstring()
  • Lua:setfield(idx, name)
  • Lua:getfield(idx, name)
  • Lua:setglobal(name)
  • Lua:getglobal(name)

Stack setters

  • Lua:pushnil()
  • Lua:pushnumber(num)
  • Lua:pushinteger(num)
  • Lua:pushlstring(str, len)
  • Lua:pushstring(str)
  • Lua:pushcclosure(fn, nargs)
  • Lua:pushcfunction(fn)
  • Lua:pushboolean(b)
  • Lua:pushlightuserdata(ud)
  • Lua:pushthread()

Unimplemented stack setters:

  • pushvfstring
  • pushfstring

Stack getters

  • Lua:checktype(idx, typ)
  • Lua:checkany(idx)
  • Lua:typename(t)
  • Lua:iscfunction(idx)
  • Lua:isnumber(idx)
  • Lua:isstring(idx)
  • Lua:isuserdata(idx)
  • Lua:checkinteger(idx)
  • Lua:checklstring(idx, len)
  • Lua:checkudata(idx, tname)
  • Lua:checkoption(idx, def, lst)

Contributing

  • don't be an ass
  • use yue
  • write decent commit messages