Merge pull request #8 from livioribeiro/master

Add lua (luajit) example
This commit is contained in:
Alex Crichton
2015-09-25 17:42:04 -07:00
4 changed files with 48 additions and 0 deletions

View 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
View 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

View File

@@ -0,0 +1,4 @@
#[no_mangle]
pub extern fn double_input(input: i32) -> i32 {
input * 2
}

View 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)