luajit.cr v0.4.0
luajit.cr
LuaJIT bindings for Crystal
Installation
- 
Install LuaJIT
 - 
Add the dependency to your
shard.yml: 
dependencies:
  luajit:
    github: mdwagner/luajit.cr
    version: ~> 0.4.0
- Run 
shards install 
Windows
Simple
- Run 
.\scripts\build.ps1to install toext\luajitdirectory 
Example (powershell):
.\scripts\build.ps1
# ...build output...
crystal run src\example.cr
Advanced
If you would rather Bring-Your-Own-LuaJIT, the following changes must be made:
- Add LuaJIT directory to environment variable 
CRYSTAL_LIBRARY_PATH- NOTE: make sure to still include Crystal's compiler directory as well (can be found by running 
crystal env CRYSTAL_LIBRARY_PATH) 
 - NOTE: make sure to still include Crystal's compiler directory as well (can be found by running 
 - Add LuaJIT directory to PATH
- This isn't completely necessary, but if you plan to leverage 
-Dpreview_dllit's required 
 - This isn't completely necessary, but if you plan to leverage 
 
Usage
require "luajit"
# Basic Hello World
Luajit.run do |state|
  state.execute! <<-'LUA'
  print("Hello World!")
  LUA
end
# Crystal type to Lua object
class Account < Luajit::LuaObject
  def_class_method "new" do |state|
    _self = new
    Luajit.setup_userdata(state, _self, self)
    1
  end
  def_instance_method "deposit" do |state|
    _self = Luajit.userdata_value(state, self, 1)
    value = state.to_i(2)
    _self.deposit(value)
    0
  end
  def_instance_method "withdraw" do |state|
    _self = Luajit.userdata_value(state, self, 1)
    value = state.to_i(2)
    _self.withdraw(value)
    0
  end
  def_instance_method "get_balance" do |state|
    _self = Luajit.userdata_value(state, self, 1)
    state.push(_self.balance)
    1
  end
  property balance : Int32 = 0
  def deposit(value : Int32)
    self.balance += value
  end
  def withdraw(value : Int32)
    self.balance -= value
  end
end
Luajit.run do |state|
  Luajit.create_lua_object(state, Account)
  state.execute! <<-'LUA'
  local account = Account.new()
  account:deposit(2000)
  account:withdraw(100)
  assert(account:get_balance() == 1900)
  LUA
end
Development
- LuaJIT bindings
 - Works on Windows (+build script)
 - Safe wrappers
 - Better tests
 - Better docs + readme
 - Crystal to Lua object wrapper
 
If you encounter any bugs, feel free to open an issue!
Contributing
- Fork it (https://github.com/mdwagner/luajit.cr/fork)
 - Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
 
Contributors
- Michael Wagner - creator and maintainer
 
Repository
  luajit.cr
Owner
  
  Statistic
  - 4
 - 0
 - 0
 - 2
 - 0
 - 2 months ago
 - July 18, 2023
 
License
  MIT License
Links
  
  Synced at
  Mon, 03 Nov 2025 08:58:39 GMT
Languages