Merge pull request #8 from livioribeiro/master
Add lua (luajit) example
This commit is contained in:
9
luajit-to-rust/Cargo.toml
Normal file
9
luajit-to-rust/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "luajit-to-rust"
|
||||
version = "0.1.0"
|
||||
# Modified based on python version
|
||||
authors = ["Livio Ribeiro <livioribeiro@outlook.com>"]
|
||||
|
||||
[lib]
|
||||
name = "double_input"
|
||||
crate-type = ["dylib"]
|
||||
14
luajit-to-rust/Makefile
Normal file
14
luajit-to-rust/Makefile
Normal file
@@ -0,0 +1,14 @@
|
||||
ifeq ($(shell uname),Darwin)
|
||||
EXT := dylib
|
||||
else
|
||||
EXT := so
|
||||
endif
|
||||
|
||||
all: target/debug/libdouble_input.$(EXT)
|
||||
luajit src/main.lua
|
||||
|
||||
target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml
|
||||
cargo build
|
||||
|
||||
clean:
|
||||
rm -rf target
|
||||
4
luajit-to-rust/src/lib.rs
Normal file
4
luajit-to-rust/src/lib.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
#[no_mangle]
|
||||
pub extern fn double_input(input: i32) -> i32 {
|
||||
input * 2
|
||||
}
|
||||
21
luajit-to-rust/src/main.lua
Normal file
21
luajit-to-rust/src/main.lua
Normal file
@@ -0,0 +1,21 @@
|
||||
local ffi = require('ffi')
|
||||
|
||||
local ext
|
||||
|
||||
if ffi.os == 'Linux' then
|
||||
ext = 'so'
|
||||
else
|
||||
ext = 'dylib'
|
||||
end
|
||||
|
||||
ffi.cdef[[
|
||||
int32_t double_input(int32_t input);
|
||||
]]
|
||||
|
||||
local lib = ffi.load('target/debug/libdouble_input.' .. ext)
|
||||
local double_input = lib.double_input
|
||||
|
||||
local input = 4
|
||||
local output = double_input(input)
|
||||
|
||||
print(input .. " * 2 = " .. output)
|
||||
Reference in New Issue
Block a user