From cdb0423401935fe077fe9a35589b8c5671b45b49 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Tue, 13 Aug 2019 18:03:48 -0400 Subject: [PATCH 01/33] Initial commit --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..faa27f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/target +**/*.rs.bk +Cargo.lock + +*.iml +.idea +.vscode \ No newline at end of file From 50f832a25d5f509496c1097761819ba7b62ba16d Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Tue, 13 Aug 2019 18:11:24 -0400 Subject: [PATCH 02/33] Adding luajit submodule --- .gitmodules | 3 +++ luajit | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 luajit diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e7ae3a7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "luajit"] + path = luajit + url = https://github.com/LuaJIT/LuaJIT.git diff --git a/luajit b/luajit new file mode 160000 index 0000000..f0e865d --- /dev/null +++ b/luajit @@ -0,0 +1 @@ +Subproject commit f0e865dd4861520258299d0f2a56491bd9d602e1 From f144a4ff6a2d1a1b723d8d1eb0399f4519532785 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 15 Aug 2019 19:13:42 -0400 Subject: [PATCH 03/33] Add build script and generated ffi --- Cargo.toml | 13 + bindgen.sh | 26 ++ build.rs | 71 +++ examples/hello.lua | 3 + examples/lua.rs | 54 +++ ffi.h | 4 + src/ffi.rs | 1113 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 163 +++++++ tests/test.rs | 40 ++ 9 files changed, 1487 insertions(+) create mode 100644 Cargo.toml create mode 100644 bindgen.sh create mode 100644 build.rs create mode 100644 examples/hello.lua create mode 100644 examples/lua.rs create mode 100644 ffi.h create mode 100644 src/ffi.rs create mode 100644 src/lib.rs create mode 100644 tests/test.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e407f7c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "luajit-sys" +version = "0.0.1" +authors = ["Aaron Loucks "] +edition = "2018" +keywords = ["lua", "luajit", "script"] + +[dependencies] +libc = "0.2" + +[build-dependencies] +cc = "1.0.40" +fs_extra = "1.1.0" \ No newline at end of file diff --git a/bindgen.sh b/bindgen.sh new file mode 100644 index 0000000..5dd058d --- /dev/null +++ b/bindgen.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +BINDGEN_VERSION=$(bindgen --version) + +bindgen -o src/ffi.rs \ + --raw-line "/// Generated with: ${BINDGEN_VERSION}" \ + --whitelist-var "LUA.*" \ + --whitelist-var "LUAJIT.*" \ + --whitelist-type "lua_.*" \ + --whitelist-type "luaL_.*" \ + --whitelist-function "lua_.*" \ + --whitelist-function "luaL_.*" \ + --whitelist-function "luaJIT.*" \ + --ctypes-prefix "libc" \ + --use-core \ + --impl-debug \ + ffi.h -- -I luajit/src + +sed -i -e 's/pub fn \(luaJIT_[^\(]*\)/\/\/\/ \n pub fn \1/' src/ffi.rs +sed -i -e 's/pub fn \(lua_[^\(]*\)/\/\/\/ \n pub fn \1/' src/ffi.rs +sed -i -e 's/pub fn \(luaL_[^\(]*\)/\/\/\/ \n pub fn \1/' src/ffi.rs +sed -i -e 's/pub type \(lua_[^\=]*\)/\/\/\/ \n pub type \1/' src/ffi.rs +sed -i -e 's/pub struct \(lua_[^\{]*\)/\/\/\/ \n pub struct \1/' src/ffi.rs +sed -i -e 's/pub struct \(luaL_[^\{]*\)/\/\/\/ \n pub struct \1/' src/ffi.rs + +cargo +stable fmt \ No newline at end of file diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..d96daef --- /dev/null +++ b/build.rs @@ -0,0 +1,71 @@ +use cc; +use fs_extra::dir; +use fs_extra::dir::CopyOptions; +use std::env; +use std::process::{Command, Stdio}; + +fn main() { + let target = env::var("TARGET").unwrap(); + + let luajit_dir = format!("{}\\luajit", env!("CARGO_MANIFEST_DIR")); + let out_dir = env::var("OUT_DIR").unwrap(); + let src_dir = format!("{}\\luajit\\src", out_dir); + + if cfg!(target_env = "msvc") { + let lib_path = format!("{}\\lua51.lib", &src_dir); + if !std::fs::metadata(&lib_path).is_ok() { + let cl_exe: cc::Tool = cc::windows_registry::find_tool(&target, "cl.exe").unwrap(); + let msvsbuild_bat = format!("{}\\msvcbuild.bat", &src_dir); + + let mut copy_options = CopyOptions::new(); + copy_options.overwrite = true; + dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); + + let mut buildcmd = Command::new(msvsbuild_bat); + for (name, value) in cl_exe.env() { + buildcmd.env(name, value); + } + buildcmd.env("Configuration", "Release"); + buildcmd.args(&["static"]); + buildcmd.current_dir(&src_dir); + buildcmd.stderr(Stdio::inherit()); + + let mut child = buildcmd.spawn().expect("failed to run msvcbuild.bat"); + + if !child + .wait() + .map(|status| status.success()) + .map_err(|_| false) + .unwrap_or(false) + { + panic!("Failed to build luajit"); + } + } + println!("cargo:rustc-link-search=native={}", src_dir); + println!("cargo:rustc-link-lib=static=lua51"); + } else { + let lib_path = format!("{}\\luajit.a", &src_dir); + if !std::fs::metadata(&lib_path).is_ok() { + let mut copy_options = CopyOptions::new(); + copy_options.overwrite = true; + dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); + + let mut buildcmd = Command::new("make"); + buildcmd.current_dir(&src_dir); + buildcmd.stderr(Stdio::inherit()); + + let mut child = buildcmd.spawn().expect("failed to run make"); + + if !child + .wait() + .map(|status| status.success()) + .map_err(|_| false) + .unwrap_or(false) + { + panic!("Failed to build luajit"); + } + } + println!("cargo:rustc-link-search=native={}", src_dir); + println!("cargo:rustc-link-lib=static=luajit"); + } +} diff --git a/examples/hello.lua b/examples/hello.lua new file mode 100644 index 0000000..87a4120 --- /dev/null +++ b/examples/hello.lua @@ -0,0 +1,3 @@ +print("Hello from lua") + +return 1 + 2 diff --git a/examples/lua.rs b/examples/lua.rs new file mode 100644 index 0000000..aa45cae --- /dev/null +++ b/examples/lua.rs @@ -0,0 +1,54 @@ +use std::env; +use std::ffi::{CStr, CString}; +use std::ptr; + +use luajit_sys as sys; + +unsafe fn run_script(script_name: String, script_src: String) { + let lua = sys::luaL_newstate(); + assert_ne!(lua, ptr::null_mut()); + sys::luaL_openlibs(lua); + let script_data = script_src.as_bytes(); + let script_name = CString::new(script_name).unwrap(); + let mut error = sys::luaL_loadbuffer( + lua, + script_data.as_ptr() as _, + script_data.len() as _, + script_name.as_ptr() as _, + ); + if error != 0 { + eprintln!("luaL_loadbuffer failed"); + } else { + error = sys::lua_pcall(lua, 0, 1, 0); + if error != 0 { + eprintln!("lua_pcall failed"); + } + } + let idx = sys::lua_gettop(lua); + if sys::lua_isnoneornil(lua, idx) != 1 { + let s = sys::lua_tostring(lua, idx); + assert_ne!(s, ptr::null(), "lua_tostring returned null"); + let result = CStr::from_ptr(s).to_string_lossy().to_string(); + println!("script result: {}", result); + } + sys::lua_close(lua); +} + +fn main() { + if let Some(script_name) = env::args().skip(1).next() { + let script_src = std::fs::read_to_string(&script_name) + .unwrap_or_else(|e| panic!("failed to read file: '{}' {:?}", &script_name, e)); + unsafe { + run_script(script_name, script_src); + } + } else { + println!( + "{} FILE", + env::current_exe() + .unwrap() + .file_name() + .unwrap() + .to_string_lossy() + ); + } +} diff --git a/ffi.h b/ffi.h new file mode 100644 index 0000000..a9fe40a --- /dev/null +++ b/ffi.h @@ -0,0 +1,4 @@ +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +#include "luajit.h" \ No newline at end of file diff --git a/src/ffi.rs b/src/ffi.rs new file mode 100644 index 0000000..76db086 --- /dev/null +++ b/src/ffi.rs @@ -0,0 +1,1113 @@ +/* automatically generated by rust-bindgen */ + +// Generated with: bindgen 0.49.1 + +pub const LUA_LDIR: &'static [u8; 7usize] = b"!\\lua\\\0"; +pub const LUA_CDIR: &'static [u8; 3usize] = b"!\\\0"; +pub const LUA_PATH_DEFAULT: &'static [u8; 38usize] = + b".\\?.lua;!\\lua\\?.lua;!\\lua\\?\\init.lua;\0"; +pub const LUA_CPATH_DEFAULT: &'static [u8; 30usize] = b".\\?.dll;!\\?.dll;!\\loadall.dll\0"; +pub const LUA_PATH: &'static [u8; 9usize] = b"LUA_PATH\0"; +pub const LUA_CPATH: &'static [u8; 10usize] = b"LUA_CPATH\0"; +pub const LUA_INIT: &'static [u8; 9usize] = b"LUA_INIT\0"; +pub const LUA_DIRSEP: &'static [u8; 2usize] = b"\\\0"; +pub const LUA_PATHSEP: &'static [u8; 2usize] = b";\0"; +pub const LUA_PATH_MARK: &'static [u8; 2usize] = b"?\0"; +pub const LUA_EXECDIR: &'static [u8; 2usize] = b"!\0"; +pub const LUA_IGMARK: &'static [u8; 2usize] = b"-\0"; +pub const LUA_PATH_CONFIG: &'static [u8; 11usize] = b"\\\n;\n?\n!\n-\n\0"; +pub const LUAI_MAXSTACK: u32 = 65500; +pub const LUAI_MAXCSTACK: u32 = 8000; +pub const LUAI_GCPAUSE: u32 = 200; +pub const LUAI_GCMUL: u32 = 200; +pub const LUA_MAXCAPTURES: u32 = 32; +pub const LUA_IDSIZE: u32 = 60; +pub const LUA_NUMBER_SCAN: &'static [u8; 4usize] = b"%lf\0"; +pub const LUA_NUMBER_FMT: &'static [u8; 6usize] = b"%.14g\0"; +pub const LUAI_MAXNUMBER2STR: u32 = 32; +pub const LUA_INTFRMLEN: &'static [u8; 2usize] = b"l\0"; +pub const LUA_VERSION: &'static [u8; 8usize] = b"Lua 5.1\0"; +pub const LUA_RELEASE: &'static [u8; 10usize] = b"Lua 5.1.4\0"; +pub const LUA_VERSION_NUM: u32 = 501; +pub const LUA_COPYRIGHT: &'static [u8; 41usize] = b"Copyright (C) 1994-2008 Lua.org, PUC-Rio\0"; +pub const LUA_AUTHORS: &'static [u8; 49usize] = + b"R. Ierusalimschy, L. H. de Figueiredo & W. Celes\0"; +pub const LUA_SIGNATURE: &'static [u8; 5usize] = b"\x1BLua\0"; +pub const LUA_MULTRET: i32 = -1; +pub const LUA_REGISTRYINDEX: i32 = -10000; +pub const LUA_ENVIRONINDEX: i32 = -10001; +pub const LUA_GLOBALSINDEX: i32 = -10002; +pub const LUA_OK: u32 = 0; +pub const LUA_YIELD: u32 = 1; +pub const LUA_ERRRUN: u32 = 2; +pub const LUA_ERRSYNTAX: u32 = 3; +pub const LUA_ERRMEM: u32 = 4; +pub const LUA_ERRERR: u32 = 5; +pub const LUA_TNONE: i32 = -1; +pub const LUA_TNIL: u32 = 0; +pub const LUA_TBOOLEAN: u32 = 1; +pub const LUA_TLIGHTUSERDATA: u32 = 2; +pub const LUA_TNUMBER: u32 = 3; +pub const LUA_TSTRING: u32 = 4; +pub const LUA_TTABLE: u32 = 5; +pub const LUA_TFUNCTION: u32 = 6; +pub const LUA_TUSERDATA: u32 = 7; +pub const LUA_TTHREAD: u32 = 8; +pub const LUA_MINSTACK: u32 = 20; +pub const LUA_GCSTOP: u32 = 0; +pub const LUA_GCRESTART: u32 = 1; +pub const LUA_GCCOLLECT: u32 = 2; +pub const LUA_GCCOUNT: u32 = 3; +pub const LUA_GCCOUNTB: u32 = 4; +pub const LUA_GCSTEP: u32 = 5; +pub const LUA_GCSETPAUSE: u32 = 6; +pub const LUA_GCSETSTEPMUL: u32 = 7; +pub const LUA_GCISRUNNING: u32 = 9; +pub const LUA_HOOKCALL: u32 = 0; +pub const LUA_HOOKRET: u32 = 1; +pub const LUA_HOOKLINE: u32 = 2; +pub const LUA_HOOKCOUNT: u32 = 3; +pub const LUA_HOOKTAILRET: u32 = 4; +pub const LUA_MASKCALL: u32 = 1; +pub const LUA_MASKRET: u32 = 2; +pub const LUA_MASKLINE: u32 = 4; +pub const LUA_MASKCOUNT: u32 = 8; +pub const LUA_FILEHANDLE: &'static [u8; 6usize] = b"FILE*\0"; +pub const LUA_COLIBNAME: &'static [u8; 10usize] = b"coroutine\0"; +pub const LUA_MATHLIBNAME: &'static [u8; 5usize] = b"math\0"; +pub const LUA_STRLIBNAME: &'static [u8; 7usize] = b"string\0"; +pub const LUA_TABLIBNAME: &'static [u8; 6usize] = b"table\0"; +pub const LUA_IOLIBNAME: &'static [u8; 3usize] = b"io\0"; +pub const LUA_OSLIBNAME: &'static [u8; 3usize] = b"os\0"; +pub const LUA_LOADLIBNAME: &'static [u8; 8usize] = b"package\0"; +pub const LUA_DBLIBNAME: &'static [u8; 6usize] = b"debug\0"; +pub const LUA_BITLIBNAME: &'static [u8; 4usize] = b"bit\0"; +pub const LUA_JITLIBNAME: &'static [u8; 4usize] = b"jit\0"; +pub const LUA_FFILIBNAME: &'static [u8; 4usize] = b"ffi\0"; +pub const LUA_ERRFILE: u32 = 6; +pub const LUA_NOREF: i32 = -2; +pub const LUA_REFNIL: i32 = -1; +pub const LUAJIT_VERSION: &'static [u8; 19usize] = b"LuaJIT 2.1.0-beta3\0"; +pub const LUAJIT_VERSION_NUM: u32 = 20100; +pub const LUAJIT_COPYRIGHT: &'static [u8; 34usize] = b"Copyright (C) 2005-2017 Mike Pall\0"; +pub const LUAJIT_URL: &'static [u8; 19usize] = b"http://luajit.org/\0"; +pub const LUAJIT_MODE_MASK: u32 = 255; +pub const LUAJIT_MODE_OFF: u32 = 0; +pub const LUAJIT_MODE_ON: u32 = 256; +pub const LUAJIT_MODE_FLUSH: u32 = 512; +pub type va_list = __builtin_va_list; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +/// +pub struct lua_State { + _unused: [u8; 0], +} +/// +pub type lua_CFunction = + ::core::option::Option libc::c_int>; +/// +pub type lua_Reader = ::core::option::Option< + unsafe extern "C" fn( + L: *mut lua_State, + ud: *mut libc::c_void, + sz: *mut usize, + ) -> *const libc::c_char, +>; +/// +pub type lua_Writer = ::core::option::Option< + unsafe extern "C" fn( + L: *mut lua_State, + p: *const libc::c_void, + sz: usize, + ud: *mut libc::c_void, + ) -> libc::c_int, +>; +/// +pub type lua_Alloc = ::core::option::Option< + unsafe extern "C" fn( + ud: *mut libc::c_void, + ptr: *mut libc::c_void, + osize: usize, + nsize: usize, + ) -> *mut libc::c_void, +>; +/// +pub type lua_Number = f64; +/// +pub type lua_Integer = isize; +extern "C" { + /// + pub fn lua_newstate(f: lua_Alloc, ud: *mut libc::c_void) -> *mut lua_State; +} +extern "C" { + /// + pub fn lua_close(L: *mut lua_State); +} +extern "C" { + /// + pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State; +} +extern "C" { + /// + pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction; +} +extern "C" { + /// + pub fn lua_gettop(L: *mut lua_State) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_settop(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_pushvalue(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_remove(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_insert(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_replace(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_checkstack(L: *mut lua_State, sz: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: libc::c_int); +} +extern "C" { + /// + pub fn lua_isnumber(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_isstring(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_iscfunction(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_isuserdata(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_type(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_typename(L: *mut lua_State, tp: libc::c_int) -> *const libc::c_char; +} +extern "C" { + /// + pub fn lua_equal(L: *mut lua_State, idx1: libc::c_int, idx2: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_rawequal(L: *mut lua_State, idx1: libc::c_int, idx2: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_lessthan(L: *mut lua_State, idx1: libc::c_int, idx2: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_tonumber(L: *mut lua_State, idx: libc::c_int) -> lua_Number; +} +extern "C" { + /// + pub fn lua_tointeger(L: *mut lua_State, idx: libc::c_int) -> lua_Integer; +} +extern "C" { + /// + pub fn lua_toboolean(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_tolstring( + L: *mut lua_State, + idx: libc::c_int, + len: *mut usize, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn lua_objlen(L: *mut lua_State, idx: libc::c_int) -> usize; +} +extern "C" { + /// + pub fn lua_tocfunction(L: *mut lua_State, idx: libc::c_int) -> lua_CFunction; +} +extern "C" { + /// + pub fn lua_touserdata(L: *mut lua_State, idx: libc::c_int) -> *mut libc::c_void; +} +extern "C" { + /// + pub fn lua_tothread(L: *mut lua_State, idx: libc::c_int) -> *mut lua_State; +} +extern "C" { + /// + pub fn lua_topointer(L: *mut lua_State, idx: libc::c_int) -> *const libc::c_void; +} +extern "C" { + /// + pub fn lua_pushnil(L: *mut lua_State); +} +extern "C" { + /// + pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number); +} +extern "C" { + /// + pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer); +} +extern "C" { + /// + pub fn lua_pushlstring(L: *mut lua_State, s: *const libc::c_char, l: usize); +} +extern "C" { + /// + pub fn lua_pushstring(L: *mut lua_State, s: *const libc::c_char); +} +extern "C" { + /// + pub fn lua_pushvfstring( + L: *mut lua_State, + fmt: *const libc::c_char, + argp: va_list, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn lua_pushfstring(L: *mut lua_State, fmt: *const libc::c_char, ...) + -> *const libc::c_char; +} +extern "C" { + /// + pub fn lua_pushcclosure(L: *mut lua_State, fn_: lua_CFunction, n: libc::c_int); +} +extern "C" { + /// + pub fn lua_pushboolean(L: *mut lua_State, b: libc::c_int); +} +extern "C" { + /// + pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut libc::c_void); +} +extern "C" { + /// + pub fn lua_pushthread(L: *mut lua_State) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_gettable(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_getfield(L: *mut lua_State, idx: libc::c_int, k: *const libc::c_char); +} +extern "C" { + /// + pub fn lua_rawget(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_rawgeti(L: *mut lua_State, idx: libc::c_int, n: libc::c_int); +} +extern "C" { + /// + pub fn lua_createtable(L: *mut lua_State, narr: libc::c_int, nrec: libc::c_int); +} +extern "C" { + /// + pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut libc::c_void; +} +extern "C" { + /// + pub fn lua_getmetatable(L: *mut lua_State, objindex: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_getfenv(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_settable(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_setfield(L: *mut lua_State, idx: libc::c_int, k: *const libc::c_char); +} +extern "C" { + /// + pub fn lua_rawset(L: *mut lua_State, idx: libc::c_int); +} +extern "C" { + /// + pub fn lua_rawseti(L: *mut lua_State, idx: libc::c_int, n: libc::c_int); +} +extern "C" { + /// + pub fn lua_setmetatable(L: *mut lua_State, objindex: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_setfenv(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_call(L: *mut lua_State, nargs: libc::c_int, nresults: libc::c_int); +} +extern "C" { + /// + pub fn lua_pcall( + L: *mut lua_State, + nargs: libc::c_int, + nresults: libc::c_int, + errfunc: libc::c_int, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_cpcall(L: *mut lua_State, func: lua_CFunction, ud: *mut libc::c_void) + -> libc::c_int; +} +extern "C" { + /// + pub fn lua_load( + L: *mut lua_State, + reader: lua_Reader, + dt: *mut libc::c_void, + chunkname: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_dump(L: *mut lua_State, writer: lua_Writer, data: *mut libc::c_void) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_yield(L: *mut lua_State, nresults: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_resume(L: *mut lua_State, narg: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_status(L: *mut lua_State) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_gc(L: *mut lua_State, what: libc::c_int, data: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_error(L: *mut lua_State) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_next(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_concat(L: *mut lua_State, n: libc::c_int); +} +extern "C" { + /// + pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut libc::c_void) -> lua_Alloc; +} +extern "C" { + /// + pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut libc::c_void); +} +extern "C" { + /// + pub fn lua_setlevel(from: *mut lua_State, to: *mut lua_State); +} +/// +pub type lua_Hook = + ::core::option::Option; +extern "C" { + /// + pub fn lua_getstack(L: *mut lua_State, level: libc::c_int, ar: *mut lua_Debug) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_getinfo( + L: *mut lua_State, + what: *const libc::c_char, + ar: *mut lua_Debug, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_getlocal( + L: *mut lua_State, + ar: *const lua_Debug, + n: libc::c_int, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn lua_setlocal( + L: *mut lua_State, + ar: *const lua_Debug, + n: libc::c_int, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn lua_getupvalue( + L: *mut lua_State, + funcindex: libc::c_int, + n: libc::c_int, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn lua_setupvalue( + L: *mut lua_State, + funcindex: libc::c_int, + n: libc::c_int, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn lua_sethook( + L: *mut lua_State, + func: lua_Hook, + mask: libc::c_int, + count: libc::c_int, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_gethook(L: *mut lua_State) -> lua_Hook; +} +extern "C" { + /// + pub fn lua_gethookmask(L: *mut lua_State) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_gethookcount(L: *mut lua_State) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_upvalueid(L: *mut lua_State, idx: libc::c_int, n: libc::c_int) -> *mut libc::c_void; +} +extern "C" { + /// + pub fn lua_upvaluejoin( + L: *mut lua_State, + idx1: libc::c_int, + n1: libc::c_int, + idx2: libc::c_int, + n2: libc::c_int, + ); +} +extern "C" { + /// + pub fn lua_loadx( + L: *mut lua_State, + reader: lua_Reader, + dt: *mut libc::c_void, + chunkname: *const libc::c_char, + mode: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn lua_version(L: *mut lua_State) -> *const lua_Number; +} +extern "C" { + /// + pub fn lua_copy(L: *mut lua_State, fromidx: libc::c_int, toidx: libc::c_int); +} +extern "C" { + /// + pub fn lua_tonumberx( + L: *mut lua_State, + idx: libc::c_int, + isnum: *mut libc::c_int, + ) -> lua_Number; +} +extern "C" { + /// + pub fn lua_tointegerx( + L: *mut lua_State, + idx: libc::c_int, + isnum: *mut libc::c_int, + ) -> lua_Integer; +} +extern "C" { + /// + pub fn lua_isyieldable(L: *mut lua_State) -> libc::c_int; +} +#[repr(C)] +#[derive(Copy, Clone)] +/// +pub struct lua_Debug { + pub event: libc::c_int, + pub name: *const libc::c_char, + pub namewhat: *const libc::c_char, + pub what: *const libc::c_char, + pub source: *const libc::c_char, + pub currentline: libc::c_int, + pub nups: libc::c_int, + pub linedefined: libc::c_int, + pub lastlinedefined: libc::c_int, + pub short_src: [libc::c_char; 60usize], + pub i_ci: libc::c_int, +} +#[test] +fn bindgen_test_layout_lua_Debug() { + assert_eq!( + ::core::mem::size_of::(), + 120usize, + concat!("Size of: ", stringify!(lua_Debug)) + ); + assert_eq!( + ::core::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(lua_Debug)) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).event as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(event) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).name as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(name) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).namewhat as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(namewhat) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).what as *const _ as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(what) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).source as *const _ as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(source) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).currentline as *const _ as usize }, + 40usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(currentline) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).nups as *const _ as usize }, + 44usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(nups) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).linedefined as *const _ as usize }, + 48usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(linedefined) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).lastlinedefined as *const _ as usize }, + 52usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(lastlinedefined) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).short_src as *const _ as usize }, + 56usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(short_src) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).i_ci as *const _ as usize }, + 116usize, + concat!( + "Offset of field: ", + stringify!(lua_Debug), + "::", + stringify!(i_ci) + ) + ); +} +impl ::core::fmt::Debug for lua_Debug { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + write ! ( f , "lua_Debug {{ event: {:?}, name: {:?}, namewhat: {:?}, what: {:?}, source: {:?}, currentline: {:?}, nups: {:?}, linedefined: {:?}, lastlinedefined: {:?}, short_src: [...], i_ci: {:?} }}" , self . event , self . name , self . namewhat , self . what , self . source , self . currentline , self . nups , self . linedefined , self . lastlinedefined , self . i_ci ) + } +} +extern "C" { + /// + pub fn luaL_openlibs(L: *mut lua_State); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +/// +pub struct luaL_Reg { + pub name: *const libc::c_char, + pub func: lua_CFunction, +} +#[test] +fn bindgen_test_layout_luaL_Reg() { + assert_eq!( + ::core::mem::size_of::(), + 16usize, + concat!("Size of: ", stringify!(luaL_Reg)) + ); + assert_eq!( + ::core::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(luaL_Reg)) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).name as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(luaL_Reg), + "::", + stringify!(name) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).func as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(luaL_Reg), + "::", + stringify!(func) + ) + ); +} +extern "C" { + /// + pub fn luaL_openlib( + L: *mut lua_State, + libname: *const libc::c_char, + l: *const luaL_Reg, + nup: libc::c_int, + ); +} +extern "C" { + /// + pub fn luaL_register(L: *mut lua_State, libname: *const libc::c_char, l: *const luaL_Reg); +} +extern "C" { + /// + pub fn luaL_getmetafield( + L: *mut lua_State, + obj: libc::c_int, + e: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_callmeta( + L: *mut lua_State, + obj: libc::c_int, + e: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_typerror( + L: *mut lua_State, + narg: libc::c_int, + tname: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_argerror( + L: *mut lua_State, + numarg: libc::c_int, + extramsg: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_checklstring( + L: *mut lua_State, + numArg: libc::c_int, + l: *mut usize, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn luaL_optlstring( + L: *mut lua_State, + numArg: libc::c_int, + def: *const libc::c_char, + l: *mut usize, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn luaL_checknumber(L: *mut lua_State, numArg: libc::c_int) -> lua_Number; +} +extern "C" { + /// + pub fn luaL_optnumber(L: *mut lua_State, nArg: libc::c_int, def: lua_Number) -> lua_Number; +} +extern "C" { + /// + pub fn luaL_checkinteger(L: *mut lua_State, numArg: libc::c_int) -> lua_Integer; +} +extern "C" { + /// + pub fn luaL_optinteger(L: *mut lua_State, nArg: libc::c_int, def: lua_Integer) -> lua_Integer; +} +extern "C" { + /// + pub fn luaL_checkstack(L: *mut lua_State, sz: libc::c_int, msg: *const libc::c_char); +} +extern "C" { + /// + pub fn luaL_checktype(L: *mut lua_State, narg: libc::c_int, t: libc::c_int); +} +extern "C" { + /// + pub fn luaL_checkany(L: *mut lua_State, narg: libc::c_int); +} +extern "C" { + /// + pub fn luaL_newmetatable(L: *mut lua_State, tname: *const libc::c_char) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_checkudata( + L: *mut lua_State, + ud: libc::c_int, + tname: *const libc::c_char, + ) -> *mut libc::c_void; +} +extern "C" { + /// + pub fn luaL_where(L: *mut lua_State, lvl: libc::c_int); +} +extern "C" { + /// + pub fn luaL_error(L: *mut lua_State, fmt: *const libc::c_char, ...) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_checkoption( + L: *mut lua_State, + narg: libc::c_int, + def: *const libc::c_char, + lst: *const *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_ref(L: *mut lua_State, t: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_unref(L: *mut lua_State, t: libc::c_int, ref_: libc::c_int); +} +extern "C" { + /// + pub fn luaL_loadfile(L: *mut lua_State, filename: *const libc::c_char) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_loadbuffer( + L: *mut lua_State, + buff: *const libc::c_char, + sz: usize, + name: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_loadstring(L: *mut lua_State, s: *const libc::c_char) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_newstate() -> *mut lua_State; +} +extern "C" { + /// + pub fn luaL_gsub( + L: *mut lua_State, + s: *const libc::c_char, + p: *const libc::c_char, + r: *const libc::c_char, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn luaL_findtable( + L: *mut lua_State, + idx: libc::c_int, + fname: *const libc::c_char, + szhint: libc::c_int, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn luaL_fileresult( + L: *mut lua_State, + stat: libc::c_int, + fname: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_execresult(L: *mut lua_State, stat: libc::c_int) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_loadfilex( + L: *mut lua_State, + filename: *const libc::c_char, + mode: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_loadbufferx( + L: *mut lua_State, + buff: *const libc::c_char, + sz: usize, + name: *const libc::c_char, + mode: *const libc::c_char, + ) -> libc::c_int; +} +extern "C" { + /// + pub fn luaL_traceback( + L: *mut lua_State, + L1: *mut lua_State, + msg: *const libc::c_char, + level: libc::c_int, + ); +} +extern "C" { + /// + pub fn luaL_setfuncs(L: *mut lua_State, l: *const luaL_Reg, nup: libc::c_int); +} +extern "C" { + /// + pub fn luaL_pushmodule(L: *mut lua_State, modname: *const libc::c_char, sizehint: libc::c_int); +} +extern "C" { + /// + pub fn luaL_testudata( + L: *mut lua_State, + ud: libc::c_int, + tname: *const libc::c_char, + ) -> *mut libc::c_void; +} +extern "C" { + /// + pub fn luaL_setmetatable(L: *mut lua_State, tname: *const libc::c_char); +} +#[repr(C)] +#[derive(Copy, Clone)] +/// +pub struct luaL_Buffer { + pub p: *mut libc::c_char, + pub lvl: libc::c_int, + pub L: *mut lua_State, + pub buffer: [libc::c_char; 512usize], +} +#[test] +fn bindgen_test_layout_luaL_Buffer() { + assert_eq!( + ::core::mem::size_of::(), + 536usize, + concat!("Size of: ", stringify!(luaL_Buffer)) + ); + assert_eq!( + ::core::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(luaL_Buffer)) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).p as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(luaL_Buffer), + "::", + stringify!(p) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).lvl as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(luaL_Buffer), + "::", + stringify!(lvl) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).L as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(luaL_Buffer), + "::", + stringify!(L) + ) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::())).buffer as *const _ as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(luaL_Buffer), + "::", + stringify!(buffer) + ) + ); +} +impl ::core::fmt::Debug for luaL_Buffer { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + write!( + f, + "luaL_Buffer {{ p: {:?}, lvl: {:?}, L: {:?}, buffer: [...] }}", + self.p, self.lvl, self.L + ) + } +} +extern "C" { + /// + pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Buffer); +} +extern "C" { + /// + pub fn luaL_prepbuffer(B: *mut luaL_Buffer) -> *mut libc::c_char; +} +extern "C" { + /// + pub fn luaL_addlstring(B: *mut luaL_Buffer, s: *const libc::c_char, l: usize); +} +extern "C" { + /// + pub fn luaL_addstring(B: *mut luaL_Buffer, s: *const libc::c_char); +} +extern "C" { + /// + pub fn luaL_addvalue(B: *mut luaL_Buffer); +} +extern "C" { + /// + pub fn luaL_pushresult(B: *mut luaL_Buffer); +} +pub const LUAJIT_MODE_ENGINE: _bindgen_ty_1 = 0; +pub const LUAJIT_MODE_DEBUG: _bindgen_ty_1 = 1; +pub const LUAJIT_MODE_FUNC: _bindgen_ty_1 = 2; +pub const LUAJIT_MODE_ALLFUNC: _bindgen_ty_1 = 3; +pub const LUAJIT_MODE_ALLSUBFUNC: _bindgen_ty_1 = 4; +pub const LUAJIT_MODE_TRACE: _bindgen_ty_1 = 5; +pub const LUAJIT_MODE_WRAPCFUNC: _bindgen_ty_1 = 16; +pub const LUAJIT_MODE_MAX: _bindgen_ty_1 = 17; +pub type _bindgen_ty_1 = i32; +extern "C" { + /// + pub fn luaJIT_setmode(L: *mut lua_State, idx: libc::c_int, mode: libc::c_int) -> libc::c_int; +} +pub type luaJIT_profile_callback = ::core::option::Option< + unsafe extern "C" fn( + data: *mut libc::c_void, + L: *mut lua_State, + samples: libc::c_int, + vmstate: libc::c_int, + ), +>; +extern "C" { + /// + pub fn luaJIT_profile_start( + L: *mut lua_State, + mode: *const libc::c_char, + cb: luaJIT_profile_callback, + data: *mut libc::c_void, + ); +} +extern "C" { + /// + pub fn luaJIT_profile_stop(L: *mut lua_State); +} +extern "C" { + /// + pub fn luaJIT_profile_dumpstack( + L: *mut lua_State, + fmt: *const libc::c_char, + depth: libc::c_int, + len: *mut usize, + ) -> *const libc::c_char; +} +extern "C" { + /// + pub fn luaJIT_version_2_1_0_beta3(); +} +pub type __builtin_va_list = *mut libc::c_char; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e0d799f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,163 @@ +#![no_std] +#![allow(non_snake_case)] +#![allow(non_camel_case_types)] + +//! # LuaJIT 2.1 +//! +//! +//! +//! +//! +//! ## Performance considerations +//! +//! The _Not Yet Implemented_ guide documents which language features will be JIT compiled +//! into native machine code. +//! +//! + +mod ffi; +pub use ffi::*; + +use core::ptr; + +// These are defined as macros + +/// +#[inline] +pub unsafe fn lua_pop(L: *mut lua_State, idx: libc::c_int) { + lua_settop(L, -(idx) - 1) +} + +/// +#[inline] +pub unsafe fn lua_newtable(L: *mut lua_State) { + lua_createtable(L, 0, 0) +} + +/// +#[inline] +pub unsafe fn lua_register(L: *mut lua_State, name: *const libc::c_char, f: lua_CFunction) { + lua_pushcfunction(L, f); + lua_setglobal(L, name); +} + +/// +#[inline] +pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) { + lua_pushcclosure(L, f, 0); +} + +/// +#[inline] +pub unsafe fn lua_strlen(L: *mut lua_State, idx: libc::c_int) -> usize { + lua_objlen(L, idx) +} + +/// +#[inline] +pub unsafe fn lua_isfunction(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { + (lua_type(L, idx) == LUA_TFUNCTION as i32) as i32 +} + +/// +#[inline] +pub unsafe fn lua_istable(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { + (lua_type(L, idx) == LUA_TTABLE as i32) as i32 +} + +/// +#[inline] +pub unsafe fn lua_islightuserdata(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { + (lua_type(L, idx) == LUA_TLIGHTUSERDATA as i32) as i32 +} + +/// +#[inline] +pub unsafe fn lua_isnil(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { + (lua_type(L, idx) == LUA_TNIL as i32) as i32 +} + +/// +#[inline] +pub unsafe fn lua_isboolean(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { + (lua_type(L, idx) == LUA_TBOOLEAN as i32) as i32 +} + +/// +#[inline] +pub unsafe fn lua_isthread(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { + (lua_type(L, idx) == LUA_TTHREAD as i32) as i32 +} + +/// +#[inline] +pub unsafe fn lua_isnone(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { + (lua_type(L, idx) == LUA_TNONE as i32) as i32 +} + +/// +#[inline] +pub unsafe fn lua_isnoneornil(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { + (lua_type(L, idx) <= 0) as i32 +} + +/// +#[inline] +pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &str) { + lua_pushlstring(L, s.as_ptr() as _, s.len() as _); +} + +/// +#[inline] +pub unsafe fn lua_setglobal(L: *mut lua_State, k: *const libc::c_char) { + lua_setfield(L, LUA_GLOBALSINDEX, k); +} + +/// +#[inline] +pub unsafe fn lua_getglobal(L: *mut lua_State, k: *const libc::c_char) { + lua_getfield(L, LUA_GLOBALSINDEX, k) +} + +/// +#[inline] +pub unsafe fn lua_tostring(L: *mut lua_State, idx: libc::c_int) -> *const libc::c_char { + lua_tolstring(L, idx, ptr::null_mut()) +} + +// Additional compatibility items that are defined as macros + +/// `luaL_newstate()` +#[inline] +#[deprecated(since = "Lua 5.1", note = "replace with `luaL_newstate()`")] +pub unsafe fn lua_open() -> *mut lua_State { + luaL_newstate() +} + +/// `lua_pushvalue(L, LUA_REGISTRYINDEX)` +#[inline] +#[deprecated( + since = "Lua 5.1", + note = "replace with `lua_pushvalue(L, LUA_REGISTRYINDEX)`" +)] +pub unsafe fn lua_getregistry(L: *mut lua_State) { + lua_pushvalue(L, LUA_REGISTRYINDEX) +} + +/// `lua_gc(L, LUA_GCCOUNT as _, 0)` +#[inline] +#[deprecated( + since = "Lua 5.1", + note = "replace with `lua_gc(L, LUA_GCCOUNT as _, 0)`" +)] +pub unsafe fn lua_getgccount(L: *mut lua_State) -> libc::c_int { + lua_gc(L, LUA_GCCOUNT as _, 0) +} + +/// `lua_Reader` +#[deprecated(since = "Lua 5.1", note = "replace with `lua_Reader`")] +pub type lua_Chunkreader = lua_Reader; + +/// `lua_Writer` +#[deprecated(since = "Lua 5.1", note = "replace with `lua_Writer`")] +pub type lua_Chunkwriter = lua_Writer; diff --git a/tests/test.rs b/tests/test.rs new file mode 100644 index 0000000..e737156 --- /dev/null +++ b/tests/test.rs @@ -0,0 +1,40 @@ +use luajit_sys as sys; + +#[test] +fn run_script() { + use std::ffi::CStr; + use std::ptr; + + unsafe { + let lua = sys::luaL_newstate(); + assert_ne!(lua, ptr::null_mut()); + sys::luaL_openlibs(lua); + let script_data = b"return 1 + 2"; + let script_name = b"run_script\0"; + let mut error = sys::luaL_loadbuffer( + lua, + script_data.as_ptr() as _, + script_data.len() as _, + script_name.as_ptr() as _, + ); + if error != 0 { + eprintln!("luaL_loadbuffer failed"); + } else { + error = sys::lua_pcall(lua, 0, 1, 0); + if error != 0 { + eprintln!("lua_pcall failed"); + } + } + + let idx = sys::lua_gettop(lua); + println!("lua_gettop = {}", idx); + + let s = sys::lua_tostring(lua, idx); + assert_ne!(s, ptr::null(), "lua_tostring returned null"); + + let result = CStr::from_ptr(s).to_string_lossy().to_string(); + sys::lua_close(lua); + + assert_eq!("3", result); + } +} From dd2810e855ab4baf325a90a37cbef1d6ce1204ff Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 15 Aug 2019 19:28:35 -0400 Subject: [PATCH 04/33] Add azure-pipelines config --- .azure-pipelines.yml | 47 +++++++++++++++++++++++++++++++++++++++ ci/azure-install-rust.yml | 33 +++++++++++++++++++++++++++ ci/azure-test-all.yml | 19 ++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 .azure-pipelines.yml create mode 100644 ci/azure-install-rust.yml create mode 100644 ci/azure-test-all.yml diff --git a/.azure-pipelines.yml b/.azure-pipelines.yml new file mode 100644 index 0000000..62ff799 --- /dev/null +++ b/.azure-pipelines.yml @@ -0,0 +1,47 @@ +trigger: + - master + +pr: + branches: + include: + - master + +jobs: + - job: Windows + pool: + vmImage: vs2017-win2016 + steps: + - template: ci/azure-install-rust.yml + - template: ci/azure-test-all.yml + strategy: + matrix: + stable: + TOOLCHAIN: stable + nightly: + TOOLCHAIN: nightly + + - job: Linux + pool: + vmImage: ubuntu-16.04 + steps: + - template: ci/azure-install-rust.yml + - template: ci/azure-test-all.yml + strategy: + matrix: + stable: + TOOLCHAIN: stable + nightly: + TOOLCHAIN: nightly + + - job: MacOS + pool: + vmImage: macOS-10.14 + steps: + - template: ci/azure-install-rust.yml + - template: ci/azure-test-all.yml + strategy: + matrix: + stable: + TOOLCHAIN: stable + nightly: + TOOLCHAIN: nightly \ No newline at end of file diff --git a/ci/azure-install-rust.yml b/ci/azure-install-rust.yml new file mode 100644 index 0000000..b304a14 --- /dev/null +++ b/ci/azure-install-rust.yml @@ -0,0 +1,33 @@ +steps: + - bash: | + set -e -x + curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain $TOOLCHAIN + source $HOME/.cargo/env + echo "##vso[task.prependpath]$HOME/.cargo/bin" + rustup --version + displayName: Install rustup + condition: eq(variables['Agent.OS'], 'Darwin') +# - script: | +# echo %TOOLCHAIN% +# curl -sSf -o rustup-init.exe https://win.rustup.rs +# rustup-init.exe -v -y --default-toolchain %TOOLCHAIN% +# echo ##vso[task.prependpath]%USERPROFILE%\.cargo\bin +# rustup default %TOOLCHAIN% +# rustup component add rustfmt +# displayName: Install rust (windows) +# condition: eq(variables['Agent.OS'], 'Windows_NT') + - bash: | + set -x + rustup --version + rustup component remove --toolchain $TOOLCHAIN rust-docs || true + rustup default $TOOLCHAIN + rustup update --no-self-update $TOOLCHAIN + rustup toolchain install stable + rustup component add rustfmt --toolchain stable + displayName: Configure rust + - bash: | + set -x + rustc -Vv + cargo -Vv + cargo +stable fmt --version + displayName: Query rustc, cargo, and rustfmt versions \ No newline at end of file diff --git a/ci/azure-test-all.yml b/ci/azure-test-all.yml new file mode 100644 index 0000000..e2e3f4a --- /dev/null +++ b/ci/azure-test-all.yml @@ -0,0 +1,19 @@ +steps: + - bash: | + set -e -x + cargo +stable fmt --all -- --check + displayName: Check formatting + - bash: | + set -e -x + cargo test --no-run + displayName: Build everything + env: + RUST_BACKTRACE: 1 + CARGO_INCREMENTAL: 0 + - bash: | + set -e -x + cargo test + displayName: Run unit tests + env: + RUST_BACKTRACE: 1 + CARGO_INCREMENTAL: 0 From db8d666c8af6d9a0c3d34baadf4b0790a4d5de56 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 15 Aug 2019 19:32:01 -0400 Subject: [PATCH 05/33] Fix path separator --- build.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/build.rs b/build.rs index d96daef..d77dd1a 100644 --- a/build.rs +++ b/build.rs @@ -7,15 +7,15 @@ use std::process::{Command, Stdio}; fn main() { let target = env::var("TARGET").unwrap(); - let luajit_dir = format!("{}\\luajit", env!("CARGO_MANIFEST_DIR")); + let luajit_dir = format!("{}/luajit", env!("CARGO_MANIFEST_DIR")); let out_dir = env::var("OUT_DIR").unwrap(); - let src_dir = format!("{}\\luajit\\src", out_dir); + let src_dir = format!("{}/luajit/src", out_dir); if cfg!(target_env = "msvc") { - let lib_path = format!("{}\\lua51.lib", &src_dir); + let lib_path = format!("{}/lua51.lib", &src_dir); if !std::fs::metadata(&lib_path).is_ok() { let cl_exe: cc::Tool = cc::windows_registry::find_tool(&target, "cl.exe").unwrap(); - let msvsbuild_bat = format!("{}\\msvcbuild.bat", &src_dir); + let msvsbuild_bat = format!("{}/msvcbuild.bat", &src_dir); let mut copy_options = CopyOptions::new(); copy_options.overwrite = true; @@ -44,7 +44,7 @@ fn main() { println!("cargo:rustc-link-search=native={}", src_dir); println!("cargo:rustc-link-lib=static=lua51"); } else { - let lib_path = format!("{}\\luajit.a", &src_dir); + let lib_path = format!("{}/luajit.a", &src_dir); if !std::fs::metadata(&lib_path).is_ok() { let mut copy_options = CopyOptions::new(); copy_options.overwrite = true; From 64b97a3b197e9cd1f107f1a719ac43fb2d440450 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 15 Aug 2019 19:33:49 -0400 Subject: [PATCH 06/33] Debug pipeline --- build.rs | 12 ++++++++++-- ci/azure-test-all.yml | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index d77dd1a..2ff8841 100644 --- a/build.rs +++ b/build.rs @@ -11,17 +11,24 @@ fn main() { let out_dir = env::var("OUT_DIR").unwrap(); let src_dir = format!("{}/luajit/src", out_dir); + dbg!(&luajit_dir); + dbg!(&out_dir); + dbg!(&src_dir); + if cfg!(target_env = "msvc") { let lib_path = format!("{}/lua51.lib", &src_dir); + dbg!(&lib_path); if !std::fs::metadata(&lib_path).is_ok() { let cl_exe: cc::Tool = cc::windows_registry::find_tool(&target, "cl.exe").unwrap(); - let msvsbuild_bat = format!("{}/msvcbuild.bat", &src_dir); + let msvcbuild_bat = format!("{}/msvcbuild.bat", &src_dir); + + dbg!(&msvcbuild_bat); let mut copy_options = CopyOptions::new(); copy_options.overwrite = true; dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); - let mut buildcmd = Command::new(msvsbuild_bat); + let mut buildcmd = Command::new(msvcbuild_bat); for (name, value) in cl_exe.env() { buildcmd.env(name, value); } @@ -45,6 +52,7 @@ fn main() { println!("cargo:rustc-link-lib=static=lua51"); } else { let lib_path = format!("{}/luajit.a", &src_dir); + dbg!(&lib_path); if !std::fs::metadata(&lib_path).is_ok() { let mut copy_options = CopyOptions::new(); copy_options.overwrite = true; diff --git a/ci/azure-test-all.yml b/ci/azure-test-all.yml index e2e3f4a..738bdba 100644 --- a/ci/azure-test-all.yml +++ b/ci/azure-test-all.yml @@ -17,3 +17,8 @@ steps: env: RUST_BACKTRACE: 1 CARGO_INCREMENTAL: 0 + - bash: | + pwd + find ./target + displayName: List files in ./target + condition: always() From 7c7cb2b56ddc72fa625dcc06bcf86bdc8b1d4314 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 15 Aug 2019 20:26:56 -0400 Subject: [PATCH 07/33] Checkout submodules in azure-pipelines --- ci/azure-test-all.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ci/azure-test-all.yml b/ci/azure-test-all.yml index 738bdba..7c08c69 100644 --- a/ci/azure-test-all.yml +++ b/ci/azure-test-all.yml @@ -1,4 +1,6 @@ steps: + - checkout: self + submodules: true - bash: | set -e -x cargo +stable fmt --all -- --check From 4cd8b358d73de0f38301ea3cb5aa68e952b3c37e Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 15 Aug 2019 20:49:42 -0400 Subject: [PATCH 08/33] Set BUILDMODE=static in Makefile --- build.rs | 5 +- etc/Makefile | 721 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 724 insertions(+), 2 deletions(-) create mode 100644 etc/Makefile diff --git a/build.rs b/build.rs index 2ff8841..7c4c6f9 100644 --- a/build.rs +++ b/build.rs @@ -1,8 +1,8 @@ use cc; use fs_extra::dir; use fs_extra::dir::CopyOptions; -use std::env; use std::process::{Command, Stdio}; +use std::{env, fs}; fn main() { let target = env::var("TARGET").unwrap(); @@ -27,6 +27,7 @@ fn main() { let mut copy_options = CopyOptions::new(); copy_options.overwrite = true; dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); + fs::copy(format!("etc/Makefile"), format!("{}/Makefile", &src_dir)).unwrap(); let mut buildcmd = Command::new(msvcbuild_bat); for (name, value) in cl_exe.env() { @@ -51,7 +52,7 @@ fn main() { println!("cargo:rustc-link-search=native={}", src_dir); println!("cargo:rustc-link-lib=static=lua51"); } else { - let lib_path = format!("{}/luajit.a", &src_dir); + let lib_path = format!("{}/libluajit.a", &src_dir); dbg!(&lib_path); if !std::fs::metadata(&lib_path).is_ok() { let mut copy_options = CopyOptions::new(); diff --git a/etc/Makefile b/etc/Makefile new file mode 100644 index 0000000..8f3cf60 --- /dev/null +++ b/etc/Makefile @@ -0,0 +1,721 @@ +############################################################################## +# LuaJIT Makefile. Requires GNU Make. +# +# Please read doc/install.html before changing any variables! +# +# Suitable for POSIX platforms (Linux, *BSD, OSX etc.). +# Also works with MinGW and Cygwin on Windows. +# Please check msvcbuild.bat for building with MSVC on Windows. +# +# Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h +############################################################################## + +MAJVER= 2 +MINVER= 1 +RELVER= 0 +ABIVER= 5.1 +NODOTABIVER= 51 + +############################################################################## +############################# COMPILER OPTIONS ############################# +############################################################################## +# These options mainly affect the speed of the JIT compiler itself, not the +# speed of the JIT-compiled code. Turn any of the optional settings on by +# removing the '#' in front of them. Make sure you force a full recompile +# with "make clean", followed by "make" if you change any options. +# +DEFAULT_CC = gcc +# +# LuaJIT builds as a native 32 or 64 bit binary by default. +CC= $(DEFAULT_CC) +# +# Use this if you want to force a 32 bit build on a 64 bit multilib OS. +#CC= $(DEFAULT_CC) -m32 +# +# Since the assembler part does NOT maintain a frame pointer, it's pointless +# to slow down the C part by not omitting it. Debugging, tracebacks and +# unwinding are not affected -- the assembler part has frame unwind +# information and GCC emits it where needed (x64) or with -g (see CCDEBUG). +CCOPT= -O2 -fomit-frame-pointer +# Use this if you want to generate a smaller binary (but it's slower): +#CCOPT= -Os -fomit-frame-pointer +# Note: it's no longer recommended to use -O3 with GCC 4.x. +# The I-Cache bloat usually outweighs the benefits from aggressive inlining. +# +# Target-specific compiler options: +# +# x86/x64 only: For GCC 4.2 or higher and if you don't intend to distribute +# the binaries to a different machine you could also use: -march=native +# +CCOPT_x86= -march=i686 -msse -msse2 -mfpmath=sse +CCOPT_x64= +CCOPT_arm= +CCOPT_arm64= +CCOPT_ppc= +CCOPT_mips= +# +CCDEBUG= +# Uncomment the next line to generate debug information: +#CCDEBUG= -g +# +CCWARN= -Wall +# Uncomment the next line to enable more warnings: +#CCWARN+= -Wextra -Wdeclaration-after-statement -Wredundant-decls -Wshadow -Wpointer-arith +# +############################################################################## + +############################################################################## +################################ BUILD MODE ################################ +############################################################################## +# The default build mode is mixed mode on POSIX. On Windows this is the same +# as dynamic mode. +# +# Mixed mode creates a static + dynamic library and a statically linked luajit. +#BUILDMODE= mixed +# +# Static mode creates a static library and a statically linked luajit. +BUILDMODE= static +# +# Dynamic mode creates a dynamic library and a dynamically linked luajit. +# Note: this executable will only run when the library is installed! +#BUILDMODE= dynamic +# +############################################################################## + +############################################################################## +################################# FEATURES ################################# +############################################################################## +# Enable/disable these features as needed, but make sure you force a full +# recompile with "make clean", followed by "make". +XCFLAGS= +# +# Permanently disable the FFI extension to reduce the size of the LuaJIT +# executable. But please consider that the FFI library is compiled-in, +# but NOT loaded by default. It only allocates any memory, if you actually +# make use of it. +#XCFLAGS+= -DLUAJIT_DISABLE_FFI +# +# Features from Lua 5.2 that are unlikely to break existing code are +# enabled by default. Some other features that *might* break some existing +# code (e.g. __pairs or os.execute() return values) can be enabled here. +# Note: this does not provide full compatibility with Lua 5.2 at this time. +#XCFLAGS+= -DLUAJIT_ENABLE_LUA52COMPAT +# +# Disable the JIT compiler, i.e. turn LuaJIT into a pure interpreter. +#XCFLAGS+= -DLUAJIT_DISABLE_JIT +# +# Some architectures (e.g. PPC) can use either single-number (1) or +# dual-number (2) mode. Uncomment one of these lines to override the +# default mode. Please see LJ_ARCH_NUMMODE in lj_arch.h for details. +#XCFLAGS+= -DLUAJIT_NUMMODE=1 +#XCFLAGS+= -DLUAJIT_NUMMODE=2 +# +# Enable GC64 mode for x64. +#XCFLAGS+= -DLUAJIT_ENABLE_GC64 +# +############################################################################## + +############################################################################## +############################ DEBUGGING SUPPORT ############################# +############################################################################## +# Enable these options as needed, but make sure you force a full recompile +# with "make clean", followed by "make". +# Note that most of these are NOT suitable for benchmarking or release mode! +# +# Use the system provided memory allocator (realloc) instead of the +# bundled memory allocator. This is slower, but sometimes helpful for +# debugging. This option cannot be enabled on x64 without GC64, since +# realloc usually doesn't return addresses in the right address range. +# OTOH this option is mandatory for Valgrind's memcheck tool on x64 and +# the only way to get useful results from it for all other architectures. +#XCFLAGS+= -DLUAJIT_USE_SYSMALLOC +# +# This define is required to run LuaJIT under Valgrind. The Valgrind +# header files must be installed. You should enable debug information, too. +# Use --suppressions=lj.supp to avoid some false positives. +#XCFLAGS+= -DLUAJIT_USE_VALGRIND +# +# This is the client for the GDB JIT API. GDB 7.0 or higher is required +# to make use of it. See lj_gdbjit.c for details. Enabling this causes +# a non-negligible overhead, even when not running under GDB. +#XCFLAGS+= -DLUAJIT_USE_GDBJIT +# +# Turn on assertions for the Lua/C API to debug problems with lua_* calls. +# This is rather slow -- use only while developing C libraries/embeddings. +#XCFLAGS+= -DLUA_USE_APICHECK +# +# Turn on assertions for the whole LuaJIT VM. This significantly slows down +# everything. Use only if you suspect a problem with LuaJIT itself. +#XCFLAGS+= -DLUA_USE_ASSERT +# +############################################################################## +# You probably don't need to change anything below this line! +############################################################################## + +############################################################################## +# Host system detection. +############################################################################## + +ifeq (Windows,$(findstring Windows,$(OS))$(MSYSTEM)$(TERM)) + HOST_SYS= Windows + HOST_RM= del +else + HOST_SYS:= $(shell uname -s) + ifneq (,$(findstring MINGW,$(HOST_SYS))) + HOST_SYS= Windows + HOST_MSYS= mingw + endif + ifneq (,$(findstring MSYS,$(HOST_SYS))) + HOST_SYS= Windows + HOST_MSYS= mingw + endif + ifneq (,$(findstring CYGWIN,$(HOST_SYS))) + HOST_SYS= Windows + HOST_MSYS= cygwin + endif +endif + +############################################################################## +# Flags and options for host and target. +############################################################################## + +# You can override the following variables at the make command line: +# CC HOST_CC STATIC_CC DYNAMIC_CC +# CFLAGS HOST_CFLAGS TARGET_CFLAGS +# LDFLAGS HOST_LDFLAGS TARGET_LDFLAGS TARGET_SHLDFLAGS +# LIBS HOST_LIBS TARGET_LIBS +# CROSS HOST_SYS TARGET_SYS TARGET_FLAGS +# +# Cross-compilation examples: +# make HOST_CC="gcc -m32" CROSS=i586-mingw32msvc- TARGET_SYS=Windows +# make HOST_CC="gcc -m32" CROSS=powerpc-linux-gnu- + +ASOPTIONS= $(CCOPT) $(CCWARN) $(XCFLAGS) $(CFLAGS) +CCOPTIONS= $(CCDEBUG) $(ASOPTIONS) +LDOPTIONS= $(CCDEBUG) $(LDFLAGS) + +HOST_CC= $(CC) +HOST_RM?= rm -f +# If left blank, minilua is built and used. You can supply an installed +# copy of (plain) Lua 5.1 or 5.2, plus Lua BitOp. E.g. with: HOST_LUA=lua +HOST_LUA= + +HOST_XCFLAGS= -I. +HOST_XLDFLAGS= +HOST_XLIBS= +HOST_ACFLAGS= $(CCOPTIONS) $(HOST_XCFLAGS) $(TARGET_ARCH) $(HOST_CFLAGS) +HOST_ALDFLAGS= $(LDOPTIONS) $(HOST_XLDFLAGS) $(HOST_LDFLAGS) +HOST_ALIBS= $(HOST_XLIBS) $(LIBS) $(HOST_LIBS) + +STATIC_CC = $(CROSS)$(CC) +DYNAMIC_CC = $(CROSS)$(CC) -fPIC +TARGET_CC= $(STATIC_CC) +TARGET_STCC= $(STATIC_CC) +TARGET_DYNCC= $(DYNAMIC_CC) +TARGET_LD= $(CROSS)$(CC) +TARGET_AR= $(CROSS)ar rcus +TARGET_STRIP= $(CROSS)strip + +TARGET_LIBPATH= $(or $(PREFIX),/usr/local)/$(or $(MULTILIB),lib) +TARGET_SONAME= libluajit-$(ABIVER).so.$(MAJVER) +TARGET_DYLIBNAME= libluajit-$(ABIVER).$(MAJVER).dylib +TARGET_DYLIBPATH= $(TARGET_LIBPATH)/$(TARGET_DYLIBNAME) +TARGET_DLLNAME= lua$(NODOTABIVER).dll +TARGET_XSHLDFLAGS= -shared -fPIC -Wl,-soname,$(TARGET_SONAME) +TARGET_DYNXLDOPTS= + +TARGET_LFSFLAGS= -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE +TARGET_XCFLAGS= $(TARGET_LFSFLAGS) -U_FORTIFY_SOURCE +TARGET_XLDFLAGS= +TARGET_XLIBS= -lm +TARGET_TCFLAGS= $(CCOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS) +TARGET_ACFLAGS= $(CCOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS) +TARGET_ASFLAGS= $(ASOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS) +TARGET_ALDFLAGS= $(LDOPTIONS) $(TARGET_XLDFLAGS) $(TARGET_FLAGS) $(TARGET_LDFLAGS) +TARGET_ASHLDFLAGS= $(LDOPTIONS) $(TARGET_XSHLDFLAGS) $(TARGET_FLAGS) $(TARGET_SHLDFLAGS) +TARGET_ALIBS= $(TARGET_XLIBS) $(LIBS) $(TARGET_LIBS) + +TARGET_TESTARCH=$(shell $(TARGET_CC) $(TARGET_TCFLAGS) -E lj_arch.h -dM) +ifneq (,$(findstring LJ_TARGET_X64 ,$(TARGET_TESTARCH))) + TARGET_LJARCH= x64 +else +ifneq (,$(findstring LJ_TARGET_X86 ,$(TARGET_TESTARCH))) + TARGET_LJARCH= x86 +else +ifneq (,$(findstring LJ_TARGET_ARM ,$(TARGET_TESTARCH))) + TARGET_LJARCH= arm +else +ifneq (,$(findstring LJ_TARGET_ARM64 ,$(TARGET_TESTARCH))) + ifneq (,$(findstring __AARCH64EB__ ,$(TARGET_TESTARCH))) + TARGET_ARCH= -D__AARCH64EB__=1 + endif + TARGET_LJARCH= arm64 +else +ifneq (,$(findstring LJ_TARGET_PPC ,$(TARGET_TESTARCH))) + ifneq (,$(findstring LJ_LE 1,$(TARGET_TESTARCH))) + TARGET_ARCH= -DLJ_ARCH_ENDIAN=LUAJIT_LE + else + TARGET_ARCH= -DLJ_ARCH_ENDIAN=LUAJIT_BE + endif + TARGET_LJARCH= ppc +else +ifneq (,$(findstring LJ_TARGET_MIPS ,$(TARGET_TESTARCH))) + ifneq (,$(findstring MIPSEL ,$(TARGET_TESTARCH))) + TARGET_ARCH= -D__MIPSEL__=1 + endif + ifneq (,$(findstring LJ_TARGET_MIPS64 ,$(TARGET_TESTARCH))) + TARGET_LJARCH= mips64 + else + TARGET_LJARCH= mips + endif +else + $(error Unsupported target architecture) +endif +endif +endif +endif +endif +endif + +ifneq (,$(findstring LJ_TARGET_PS3 1,$(TARGET_TESTARCH))) + TARGET_SYS= PS3 + TARGET_ARCH+= -D__CELLOS_LV2__ + TARGET_XCFLAGS+= -DLUAJIT_USE_SYSMALLOC + TARGET_XLIBS+= -lpthread +endif + +TARGET_XCFLAGS+= $(CCOPT_$(TARGET_LJARCH)) +TARGET_ARCH+= $(patsubst %,-DLUAJIT_TARGET=LUAJIT_ARCH_%,$(TARGET_LJARCH)) + +ifneq (,$(PREFIX)) +ifneq (/usr/local,$(PREFIX)) + TARGET_XCFLAGS+= -DLUA_ROOT=\"$(PREFIX)\" + ifneq (/usr,$(PREFIX)) + TARGET_DYNXLDOPTS= -Wl,-rpath,$(TARGET_LIBPATH) + endif +endif +endif +ifneq (,$(MULTILIB)) + TARGET_XCFLAGS+= -DLUA_MULTILIB=\"$(MULTILIB)\" +endif +ifneq (,$(LMULTILIB)) + TARGET_XCFLAGS+= -DLUA_LMULTILIB=\"$(LMULTILIB)\" +endif + +############################################################################## +# Target system detection. +############################################################################## + +TARGET_SYS?= $(HOST_SYS) +ifeq (Windows,$(TARGET_SYS)) + TARGET_STRIP+= --strip-unneeded + TARGET_XSHLDFLAGS= -shared + TARGET_DYNXLDOPTS= +else + TARGET_AR+= 2>/dev/null +ifeq (,$(shell $(TARGET_CC) -o /dev/null -c -x c /dev/null -fno-stack-protector 2>/dev/null || echo 1)) + TARGET_XCFLAGS+= -fno-stack-protector +endif +ifeq (Darwin,$(TARGET_SYS)) + ifeq (,$(MACOSX_DEPLOYMENT_TARGET)) + export MACOSX_DEPLOYMENT_TARGET=10.4 + endif + TARGET_STRIP+= -x + TARGET_XSHLDFLAGS= -dynamiclib -single_module -undefined dynamic_lookup -fPIC + TARGET_DYNXLDOPTS= + TARGET_XSHLDFLAGS+= -install_name $(TARGET_DYLIBPATH) -compatibility_version $(MAJVER).$(MINVER) -current_version $(MAJVER).$(MINVER).$(RELVER) + ifeq (x64,$(TARGET_LJARCH)) + TARGET_XLDFLAGS+= -pagezero_size 10000 -image_base 100000000 + TARGET_XSHLDFLAGS+= -image_base 7fff04c4a000 + endif +else +ifeq (iOS,$(TARGET_SYS)) + TARGET_STRIP+= -x + TARGET_XSHLDFLAGS= -dynamiclib -single_module -undefined dynamic_lookup -fPIC + TARGET_DYNXLDOPTS= + TARGET_XSHLDFLAGS+= -install_name $(TARGET_DYLIBPATH) -compatibility_version $(MAJVER).$(MINVER) -current_version $(MAJVER).$(MINVER).$(RELVER) + ifeq (arm64,$(TARGET_LJARCH)) + TARGET_XCFLAGS+= -fno-omit-frame-pointer + endif +else + ifneq (SunOS,$(TARGET_SYS)) + ifneq (PS3,$(TARGET_SYS)) + TARGET_XLDFLAGS+= -Wl,-E + endif + endif + ifeq (Linux,$(TARGET_SYS)) + TARGET_XLIBS+= -ldl + endif + ifeq (GNU/kFreeBSD,$(TARGET_SYS)) + TARGET_XLIBS+= -ldl + endif +endif +endif +endif + +ifneq ($(HOST_SYS),$(TARGET_SYS)) + ifeq (Windows,$(TARGET_SYS)) + HOST_XCFLAGS+= -malign-double -DLUAJIT_OS=LUAJIT_OS_WINDOWS + else + ifeq (Linux,$(TARGET_SYS)) + HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_LINUX + else + ifeq (Darwin,$(TARGET_SYS)) + HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OSX + else + ifeq (iOS,$(TARGET_SYS)) + HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OSX + else + HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OTHER + endif + endif + endif + endif +endif + +ifneq (,$(CCDEBUG)) + TARGET_STRIP= @: +endif + +############################################################################## +# Files and pathnames. +############################################################################## + +MINILUA_O= host/minilua.o +MINILUA_LIBS= -lm +MINILUA_T= host/minilua +MINILUA_X= $(MINILUA_T) + +ifeq (,$(HOST_LUA)) + HOST_LUA= $(MINILUA_X) + DASM_DEP= $(MINILUA_T) +endif + +DASM_DIR= ../dynasm +DASM= $(HOST_LUA) $(DASM_DIR)/dynasm.lua +DASM_XFLAGS= +DASM_AFLAGS= +DASM_ARCH= $(TARGET_LJARCH) + +ifneq (,$(findstring LJ_LE 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D ENDIAN_LE +else + DASM_AFLAGS+= -D ENDIAN_BE +endif +ifneq (,$(findstring LJ_ARCH_BITS 64,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D P64 +endif +ifneq (,$(findstring LJ_HASJIT 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D JIT +endif +ifneq (,$(findstring LJ_HASFFI 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D FFI +endif +ifneq (,$(findstring LJ_DUALNUM 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D DUALNUM +endif +ifneq (,$(findstring LJ_ARCH_HASFPU 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D FPU + TARGET_ARCH+= -DLJ_ARCH_HASFPU=1 +else + TARGET_ARCH+= -DLJ_ARCH_HASFPU=0 +endif +ifeq (,$(findstring LJ_ABI_SOFTFP 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D HFABI + TARGET_ARCH+= -DLJ_ABI_SOFTFP=0 +else + TARGET_ARCH+= -DLJ_ABI_SOFTFP=1 +endif +ifneq (,$(findstring LJ_NO_UNWIND 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D NO_UNWIND + TARGET_ARCH+= -DLUAJIT_NO_UNWIND +endif +DASM_AFLAGS+= -D VER=$(subst LJ_ARCH_VERSION_,,$(filter LJ_ARCH_VERSION_%,$(subst LJ_ARCH_VERSION ,LJ_ARCH_VERSION_,$(TARGET_TESTARCH)))) +ifeq (Windows,$(TARGET_SYS)) + DASM_AFLAGS+= -D WIN +endif +ifeq (x64,$(TARGET_LJARCH)) + ifeq (,$(findstring LJ_FR2 1,$(TARGET_TESTARCH))) + DASM_ARCH= x86 + endif +else +ifeq (arm,$(TARGET_LJARCH)) + ifeq (iOS,$(TARGET_SYS)) + DASM_AFLAGS+= -D IOS + endif +else +ifeq (ppc,$(TARGET_LJARCH)) + ifneq (,$(findstring LJ_ARCH_SQRT 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D SQRT + endif + ifneq (,$(findstring LJ_ARCH_ROUND 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D ROUND + endif + ifneq (,$(findstring LJ_ARCH_PPC32ON64 1,$(TARGET_TESTARCH))) + DASM_AFLAGS+= -D GPR64 + endif + ifeq (PS3,$(TARGET_SYS)) + DASM_AFLAGS+= -D PPE -D TOC + endif + ifneq (,$(findstring LJ_ARCH_PPC64 ,$(TARGET_TESTARCH))) + DASM_ARCH= ppc64 + endif +endif +endif +endif + +DASM_FLAGS= $(DASM_XFLAGS) $(DASM_AFLAGS) +DASM_DASC= vm_$(DASM_ARCH).dasc + +BUILDVM_O= host/buildvm.o host/buildvm_asm.o host/buildvm_peobj.o \ + host/buildvm_lib.o host/buildvm_fold.o +BUILDVM_T= host/buildvm +BUILDVM_X= $(BUILDVM_T) + +HOST_O= $(MINILUA_O) $(BUILDVM_O) +HOST_T= $(MINILUA_T) $(BUILDVM_T) + +LJVM_S= lj_vm.S +LJVM_O= lj_vm.o +LJVM_BOUT= $(LJVM_S) +LJVM_MODE= elfasm + +LJLIB_O= lib_base.o lib_math.o lib_bit.o lib_string.o lib_table.o \ + lib_io.o lib_os.o lib_package.o lib_debug.o lib_jit.o lib_ffi.o +LJLIB_C= $(LJLIB_O:.o=.c) + +LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o lj_buf.o \ + lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \ + lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \ + lj_strfmt.o lj_strfmt_num.o lj_api.o lj_profile.o \ + lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \ + lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \ + lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \ + lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \ + lj_asm.o lj_trace.o lj_gdbjit.o \ + lj_ctype.o lj_cdata.o lj_cconv.o lj_ccall.o lj_ccallback.o \ + lj_carith.o lj_clib.o lj_cparse.o \ + lj_lib.o lj_alloc.o lib_aux.o \ + $(LJLIB_O) lib_init.o + +LJVMCORE_O= $(LJVM_O) $(LJCORE_O) +LJVMCORE_DYNO= $(LJVMCORE_O:.o=_dyn.o) + +LIB_VMDEF= jit/vmdef.lua +LIB_VMDEFP= $(LIB_VMDEF) + +LUAJIT_O= luajit.o +LUAJIT_A= libluajit.a +LUAJIT_SO= libluajit.so +LUAJIT_T= luajit + +ALL_T= $(LUAJIT_T) $(LUAJIT_A) $(LUAJIT_SO) $(HOST_T) +ALL_HDRGEN= lj_bcdef.h lj_ffdef.h lj_libdef.h lj_recdef.h lj_folddef.h \ + host/buildvm_arch.h +ALL_GEN= $(LJVM_S) $(ALL_HDRGEN) $(LIB_VMDEFP) +WIN_RM= *.obj *.lib *.exp *.dll *.exe *.manifest *.pdb *.ilk +ALL_RM= $(ALL_T) $(ALL_GEN) *.o host/*.o $(WIN_RM) + +############################################################################## +# Build mode handling. +############################################################################## + +# Mixed mode defaults. +TARGET_O= $(LUAJIT_A) +TARGET_T= $(LUAJIT_T) $(LUAJIT_SO) +TARGET_DEP= $(LIB_VMDEF) $(LUAJIT_SO) + +ifeq (Windows,$(TARGET_SYS)) + TARGET_DYNCC= $(STATIC_CC) + LJVM_MODE= peobj + LJVM_BOUT= $(LJVM_O) + LUAJIT_T= luajit.exe + ifeq (cygwin,$(HOST_MSYS)) + LUAJIT_SO= cyg$(TARGET_DLLNAME) + else + LUAJIT_SO= $(TARGET_DLLNAME) + endif + # Mixed mode is not supported on Windows. And static mode doesn't work well. + # C modules cannot be loaded, because they bind to lua51.dll. + ifneq (static,$(BUILDMODE)) + BUILDMODE= dynamic + TARGET_XCFLAGS+= -DLUA_BUILD_AS_DLL + endif +endif +ifeq (Darwin,$(TARGET_SYS)) + LJVM_MODE= machasm +endif +ifeq (iOS,$(TARGET_SYS)) + LJVM_MODE= machasm +endif +ifeq (SunOS,$(TARGET_SYS)) + BUILDMODE= static +endif +ifeq (PS3,$(TARGET_SYS)) + BUILDMODE= static +endif + +ifeq (Windows,$(HOST_SYS)) + MINILUA_T= host/minilua.exe + BUILDVM_T= host/buildvm.exe + ifeq (,$(HOST_MSYS)) + MINILUA_X= host\minilua + BUILDVM_X= host\buildvm + ALL_RM:= $(subst /,\,$(ALL_RM)) + endif +endif + +ifeq (static,$(BUILDMODE)) + TARGET_DYNCC= @: + TARGET_T= $(LUAJIT_T) + TARGET_DEP= $(LIB_VMDEF) +else +ifeq (dynamic,$(BUILDMODE)) + ifneq (Windows,$(TARGET_SYS)) + TARGET_CC= $(DYNAMIC_CC) + endif + TARGET_DYNCC= @: + LJVMCORE_DYNO= $(LJVMCORE_O) + TARGET_O= $(LUAJIT_SO) + TARGET_XLDFLAGS+= $(TARGET_DYNXLDOPTS) +else +ifeq (Darwin,$(TARGET_SYS)) + TARGET_DYNCC= @: + LJVMCORE_DYNO= $(LJVMCORE_O) +endif +ifeq (iOS,$(TARGET_SYS)) + TARGET_DYNCC= @: + LJVMCORE_DYNO= $(LJVMCORE_O) +endif +endif +endif + +Q= @ +E= @echo +#Q= +#E= @: + +############################################################################## +# Make targets. +############################################################################## + +default all: $(TARGET_T) + +amalg: + @grep "^[+|]" ljamalg.c + $(MAKE) all "LJCORE_O=ljamalg.o" + +clean: + $(HOST_RM) $(ALL_RM) + +libbc: + ./$(LUAJIT_T) host/genlibbc.lua -o host/buildvm_libbc.h $(LJLIB_C) + $(MAKE) all + +depend: + @for file in $(ALL_HDRGEN); do \ + test -f $$file || touch $$file; \ + done + @$(HOST_CC) $(HOST_ACFLAGS) -MM *.c host/*.c | \ + sed -e "s| [^ ]*/dasm_\S*\.h||g" \ + -e "s|^\([^l ]\)|host/\1|" \ + -e "s| lj_target_\S*\.h| lj_target_*.h|g" \ + -e "s| lj_emit_\S*\.h| lj_emit_*.h|g" \ + -e "s| lj_asm_\S*\.h| lj_asm_*.h|g" >Makefile.dep + @for file in $(ALL_HDRGEN); do \ + test -s $$file || $(HOST_RM) $$file; \ + done + +.PHONY: default all amalg clean libbc depend + +############################################################################## +# Rules for generated files. +############################################################################## + +$(MINILUA_T): $(MINILUA_O) + $(E) "HOSTLINK $@" + $(Q)$(HOST_CC) $(HOST_ALDFLAGS) -o $@ $(MINILUA_O) $(MINILUA_LIBS) $(HOST_ALIBS) + +host/buildvm_arch.h: $(DASM_DASC) $(DASM_DEP) $(DASM_DIR)/*.lua + $(E) "DYNASM $@" + $(Q)$(DASM) $(DASM_FLAGS) -o $@ $(DASM_DASC) + +host/buildvm.o: $(DASM_DIR)/dasm_*.h + +$(BUILDVM_T): $(BUILDVM_O) + $(E) "HOSTLINK $@" + $(Q)$(HOST_CC) $(HOST_ALDFLAGS) -o $@ $(BUILDVM_O) $(HOST_ALIBS) + +$(LJVM_BOUT): $(BUILDVM_T) + $(E) "BUILDVM $@" + $(Q)$(BUILDVM_X) -m $(LJVM_MODE) -o $@ + +lj_bcdef.h: $(BUILDVM_T) $(LJLIB_C) + $(E) "BUILDVM $@" + $(Q)$(BUILDVM_X) -m bcdef -o $@ $(LJLIB_C) + +lj_ffdef.h: $(BUILDVM_T) $(LJLIB_C) + $(E) "BUILDVM $@" + $(Q)$(BUILDVM_X) -m ffdef -o $@ $(LJLIB_C) + +lj_libdef.h: $(BUILDVM_T) $(LJLIB_C) + $(E) "BUILDVM $@" + $(Q)$(BUILDVM_X) -m libdef -o $@ $(LJLIB_C) + +lj_recdef.h: $(BUILDVM_T) $(LJLIB_C) + $(E) "BUILDVM $@" + $(Q)$(BUILDVM_X) -m recdef -o $@ $(LJLIB_C) + +$(LIB_VMDEF): $(BUILDVM_T) $(LJLIB_C) + $(E) "BUILDVM $@" + $(Q)$(BUILDVM_X) -m vmdef -o $(LIB_VMDEFP) $(LJLIB_C) + +lj_folddef.h: $(BUILDVM_T) lj_opt_fold.c + $(E) "BUILDVM $@" + $(Q)$(BUILDVM_X) -m folddef -o $@ lj_opt_fold.c + +############################################################################## +# Object file rules. +############################################################################## + +%.o: %.c + $(E) "CC $@" + $(Q)$(TARGET_DYNCC) $(TARGET_ACFLAGS) -c -o $(@:.o=_dyn.o) $< + $(Q)$(TARGET_CC) $(TARGET_ACFLAGS) -c -o $@ $< + +%.o: %.S + $(E) "ASM $@" + $(Q)$(TARGET_DYNCC) $(TARGET_ASFLAGS) -c -o $(@:.o=_dyn.o) $< + $(Q)$(TARGET_CC) $(TARGET_ASFLAGS) -c -o $@ $< + +$(LUAJIT_O): + $(E) "CC $@" + $(Q)$(TARGET_STCC) $(TARGET_ACFLAGS) -c -o $@ $< + +$(HOST_O): %.o: %.c + $(E) "HOSTCC $@" + $(Q)$(HOST_CC) $(HOST_ACFLAGS) -c -o $@ $< + +include Makefile.dep + +############################################################################## +# Target file rules. +############################################################################## + +$(LUAJIT_A): $(LJVMCORE_O) + $(E) "AR $@" + $(Q)$(TARGET_AR) $@ $(LJVMCORE_O) + +# The dependency on _O, but linking with _DYNO is intentional. +$(LUAJIT_SO): $(LJVMCORE_O) + $(E) "DYNLINK $@" + $(Q)$(TARGET_LD) $(TARGET_ASHLDFLAGS) -o $@ $(LJVMCORE_DYNO) $(TARGET_ALIBS) + $(Q)$(TARGET_STRIP) $@ + +$(LUAJIT_T): $(TARGET_O) $(LUAJIT_O) $(TARGET_DEP) + $(E) "LINK $@" + $(Q)$(TARGET_LD) $(TARGET_ALDFLAGS) -o $@ $(LUAJIT_O) $(TARGET_O) $(TARGET_ALIBS) + $(Q)$(TARGET_STRIP) $@ + $(E) "OK Successfully built LuaJIT" + +############################################################################## From 2b9a03d961ef35befebfa098a76f3f3c99116cb4 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 15 Aug 2019 21:32:21 -0400 Subject: [PATCH 09/33] Set BUILDMODE=static and -fPIC in Makefile --- build.rs | 2 +- etc/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 7c4c6f9..59b6768 100644 --- a/build.rs +++ b/build.rs @@ -27,7 +27,6 @@ fn main() { let mut copy_options = CopyOptions::new(); copy_options.overwrite = true; dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); - fs::copy(format!("etc/Makefile"), format!("{}/Makefile", &src_dir)).unwrap(); let mut buildcmd = Command::new(msvcbuild_bat); for (name, value) in cl_exe.env() { @@ -58,6 +57,7 @@ fn main() { let mut copy_options = CopyOptions::new(); copy_options.overwrite = true; dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); + fs::copy(format!("etc/Makefile"), format!("{}/Makefile", &src_dir)).unwrap(); let mut buildcmd = Command::new("make"); buildcmd.current_dir(&src_dir); diff --git a/etc/Makefile b/etc/Makefile index 8f3cf60..04a7dd2 100644 --- a/etc/Makefile +++ b/etc/Makefile @@ -207,7 +207,7 @@ HOST_ACFLAGS= $(CCOPTIONS) $(HOST_XCFLAGS) $(TARGET_ARCH) $(HOST_CFLAGS) HOST_ALDFLAGS= $(LDOPTIONS) $(HOST_XLDFLAGS) $(HOST_LDFLAGS) HOST_ALIBS= $(HOST_XLIBS) $(LIBS) $(HOST_LIBS) -STATIC_CC = $(CROSS)$(CC) +STATIC_CC = $(CROSS)$(CC) -fPIC DYNAMIC_CC = $(CROSS)$(CC) -fPIC TARGET_CC= $(STATIC_CC) TARGET_STCC= $(STATIC_CC) From 6e5676b8894273b938fedfef407718ddd35efbfe Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 15 Aug 2019 21:46:56 -0400 Subject: [PATCH 10/33] Disable MACOSX_DEPLOYMENT_TARGET --- etc/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/Makefile b/etc/Makefile index 04a7dd2..2046c6c 100644 --- a/etc/Makefile +++ b/etc/Makefile @@ -318,7 +318,7 @@ ifeq (,$(shell $(TARGET_CC) -o /dev/null -c -x c /dev/null -fno-stack-protector endif ifeq (Darwin,$(TARGET_SYS)) ifeq (,$(MACOSX_DEPLOYMENT_TARGET)) - export MACOSX_DEPLOYMENT_TARGET=10.4 +# export MACOSX_DEPLOYMENT_TARGET=10.4 endif TARGET_STRIP+= -x TARGET_XSHLDFLAGS= -dynamiclib -single_module -undefined dynamic_lookup -fPIC From 6a3c526cfba36503ee6ac607b94022b79a073246 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 15 Aug 2019 22:09:20 -0400 Subject: [PATCH 11/33] Add link args for x86_64-apple-darwin --- .cargo/config | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .cargo/config diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..134419d --- /dev/null +++ b/.cargo/config @@ -0,0 +1,9 @@ +[target.x86_64-apple-darwin] +rustflags = [ + "-C", + "link-arg=-pagezero_size 10000", + +] + +# "-C", +# "link-arg=-image_base 100000000", \ No newline at end of file From e0baa55c2cfbcafa2af6a34c4500cd789b64a705 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sat, 17 Aug 2019 19:59:44 -0400 Subject: [PATCH 12/33] Disable i686 and mingw tests --- .azure-pipelines.yml | 26 +++++++++++++++----------- build.rs | 9 +++++++++ ci/azure-install-rust.yml | 12 +----------- ci/azure-test-all.yml | 12 +++++++++++- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/.azure-pipelines.yml b/.azure-pipelines.yml index 62ff799..c6d683b 100644 --- a/.azure-pipelines.yml +++ b/.azure-pipelines.yml @@ -15,10 +15,14 @@ jobs: - template: ci/azure-test-all.yml strategy: matrix: - stable: - TOOLCHAIN: stable - nightly: - TOOLCHAIN: nightly + stable-x86_64-msvc: + TOOLCHAIN: stable-x86_64-pc-windows-msvc + # stable-x86_64-gnu: + # TOOLCHAIN: stable-x86_64-pc-windows-gnu + # stable-i686-msvc: + # TOOLCHAIN: stable-i686-pc-windows-msvc + # stable-i686-gnu: + # TOOLCHAIN: stable-i686-pc-windows-gnu - job: Linux pool: @@ -28,9 +32,11 @@ jobs: - template: ci/azure-test-all.yml strategy: matrix: - stable: - TOOLCHAIN: stable - nightly: + stable-x86_64: + TOOLCHAIN: stable-x86_64-unknown-linux-gnu + # stable-i686: + # TOOLCHAIN: stable-i686-unknown-linux-gnu + nightly-x86_64: TOOLCHAIN: nightly - job: MacOS @@ -41,7 +47,5 @@ jobs: - template: ci/azure-test-all.yml strategy: matrix: - stable: - TOOLCHAIN: stable - nightly: - TOOLCHAIN: nightly \ No newline at end of file + stable-x86_64: + TOOLCHAIN: stable-x86_64-apple-darwin \ No newline at end of file diff --git a/build.rs b/build.rs index 59b6768..0957824 100644 --- a/build.rs +++ b/build.rs @@ -63,6 +63,11 @@ fn main() { buildcmd.current_dir(&src_dir); buildcmd.stderr(Stdio::inherit()); + if cfg!(target_pointer_width = "32") { + buildcmd.env("HOST_CC", "gcc -m32"); + buildcmd.arg("-e"); + } + let mut child = buildcmd.spawn().expect("failed to run make"); if !child @@ -77,4 +82,8 @@ fn main() { println!("cargo:rustc-link-search=native={}", src_dir); println!("cargo:rustc-link-lib=static=luajit"); } + + // if cfg!(target_os = "macos") && cfg!(target_pointer_width = "64") { + // // RUSTFLAGS='-C link-args=-pagezero_size 10000 -image_base 100000000' + // } } diff --git a/ci/azure-install-rust.yml b/ci/azure-install-rust.yml index b304a14..cd39cc1 100644 --- a/ci/azure-install-rust.yml +++ b/ci/azure-install-rust.yml @@ -7,19 +7,9 @@ steps: rustup --version displayName: Install rustup condition: eq(variables['Agent.OS'], 'Darwin') -# - script: | -# echo %TOOLCHAIN% -# curl -sSf -o rustup-init.exe https://win.rustup.rs -# rustup-init.exe -v -y --default-toolchain %TOOLCHAIN% -# echo ##vso[task.prependpath]%USERPROFILE%\.cargo\bin -# rustup default %TOOLCHAIN% -# rustup component add rustfmt -# displayName: Install rust (windows) -# condition: eq(variables['Agent.OS'], 'Windows_NT') - bash: | - set -x + set -e -x rustup --version - rustup component remove --toolchain $TOOLCHAIN rust-docs || true rustup default $TOOLCHAIN rustup update --no-self-update $TOOLCHAIN rustup toolchain install stable diff --git a/ci/azure-test-all.yml b/ci/azure-test-all.yml index 7c08c69..1b253f7 100644 --- a/ci/azure-test-all.yml +++ b/ci/azure-test-all.yml @@ -1,6 +1,16 @@ steps: - checkout: self submodules: true +# - script: | +# call "C:\Program Files (x86)\Microsoft Visual Studio\2017\VC\Auxiliary\Build\vcvarsall.bat" x86 +# displayName: Call vcvarsalls.bat x86 +# condition: eq(variables['TOOLCHAIN'], 'stable-i686-pc-windows-msvc') +# - bash: pacman -Syu +# condition: eq(variables['TOOLCHAIN'], 'stable-x86_64-pc-windows-gnu') +# displayName: Update msys +# - bash: sudo apt install gcc-multilib +# condition: eq(variables['TOOLCHAIN'], 'stable-i686-unknown-linux-gnu') +# displayName: Install gcc-multilib - bash: | set -e -x cargo +stable fmt --all -- --check @@ -22,5 +32,5 @@ steps: - bash: | pwd find ./target - displayName: List files in ./target + displayName: List files in target condition: always() From a2b17d749c337bd994d518ae688b6356a5f58fad Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 01:02:08 -0500 Subject: [PATCH 13/33] The bash task in azure seems to find powershells find --- ci/azure-test-all.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/azure-test-all.yml b/ci/azure-test-all.yml index 1b253f7..877ab5a 100644 --- a/ci/azure-test-all.yml +++ b/ci/azure-test-all.yml @@ -31,6 +31,6 @@ steps: CARGO_INCREMENTAL: 0 - bash: | pwd - find ./target + /usr/bin/find ./target displayName: List files in target condition: always() From bb75b4a2fd4758cf44c85aaad2d368ea60fadbf0 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 01:13:31 -0500 Subject: [PATCH 14/33] Add README --- README.md | 3 +++ bindgen.sh | 2 +- build.rs | 3 ++- src/ffi.rs | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..879aa22 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Rust LuaJIT 2 Bindings + +[![Build Status](https://dev.azure.com/aloucks/aloucks/_apis/build/status/aloucks.luajit-sys?branchName=master)](https://dev.azure.com/aloucks/aloucks/_build/latest?definitionId=2&branchName=master) diff --git a/bindgen.sh b/bindgen.sh index 5dd058d..27e7b9e 100644 --- a/bindgen.sh +++ b/bindgen.sh @@ -1,6 +1,6 @@ #!/bin/bash -BINDGEN_VERSION=$(bindgen --version) +BINDGEN_VERSION=$(bindgen --version | grep -v -e '^cargo') bindgen -o src/ffi.rs \ --raw-line "/// Generated with: ${BINDGEN_VERSION}" \ diff --git a/build.rs b/build.rs index 0957824..7f749d7 100644 --- a/build.rs +++ b/build.rs @@ -19,7 +19,8 @@ fn main() { let lib_path = format!("{}/lua51.lib", &src_dir); dbg!(&lib_path); if !std::fs::metadata(&lib_path).is_ok() { - let cl_exe: cc::Tool = cc::windows_registry::find_tool(&target, "cl.exe").unwrap(); + let cl_exe: cc::Tool = + cc::windows_registry::find_tool(&target, "cl.exe").expect("cl.exe not found"); let msvcbuild_bat = format!("{}/msvcbuild.bat", &src_dir); dbg!(&msvcbuild_bat); diff --git a/src/ffi.rs b/src/ffi.rs index 76db086..a33ac54 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -1,6 +1,6 @@ /* automatically generated by rust-bindgen */ -// Generated with: bindgen 0.49.1 +// Generated with: bindgen 0.52.0 pub const LUA_LDIR: &'static [u8; 7usize] = b"!\\lua\\\0"; pub const LUA_CDIR: &'static [u8; 3usize] = b"!\\\0"; From a705ff7d25275b90636e3e2092a242bd25ca54e5 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 01:19:51 -0500 Subject: [PATCH 15/33] Rename to luajit2-sys --- Cargo.toml | 4 ++-- README.md | 2 +- examples/lua.rs | 2 +- tests/test.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e407f7c..9970652 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "luajit-sys" +name = "luajit2-sys" version = "0.0.1" authors = ["Aaron Loucks "] edition = "2018" @@ -10,4 +10,4 @@ libc = "0.2" [build-dependencies] cc = "1.0.40" -fs_extra = "1.1.0" \ No newline at end of file +fs_extra = "1.1.0" diff --git a/README.md b/README.md index 879aa22..6d5e744 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Rust LuaJIT 2 Bindings -[![Build Status](https://dev.azure.com/aloucks/aloucks/_apis/build/status/aloucks.luajit-sys?branchName=master)](https://dev.azure.com/aloucks/aloucks/_build/latest?definitionId=2&branchName=master) +[![Build Status](https://dev.azure.com/aloucks/aloucks/_apis/build/status/aloucks.luajit2-sys?branchName=master)](https://dev.azure.com/aloucks/aloucks/_build/latest?definitionId=3&branchName=master) diff --git a/examples/lua.rs b/examples/lua.rs index aa45cae..0784409 100644 --- a/examples/lua.rs +++ b/examples/lua.rs @@ -2,7 +2,7 @@ use std::env; use std::ffi::{CStr, CString}; use std::ptr; -use luajit_sys as sys; +use luajit2_sys as sys; unsafe fn run_script(script_name: String, script_src: String) { let lua = sys::luaL_newstate(); diff --git a/tests/test.rs b/tests/test.rs index e737156..8a018ea 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,4 +1,4 @@ -use luajit_sys as sys; +use luajit2_sys as sys; #[test] fn run_script() { From b00970789a4e8d51f6661d23dd89600048498d5a Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 01:38:51 -0500 Subject: [PATCH 16/33] Add license --- Cargo.toml | 2 + LICENSE-APACHE | 201 +++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE-MIT | 23 ++++++ 3 files changed, 226 insertions(+) create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT diff --git a/Cargo.toml b/Cargo.toml index 9970652..6625e10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,8 @@ version = "0.0.1" authors = ["Aaron Loucks "] edition = "2018" keywords = ["lua", "luajit", "script"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/aloucks/luajit2-sys" [dependencies] libc = "0.2" diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..f007b81 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/LICENSE-2.0 + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..468cd79 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,23 @@ +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. \ No newline at end of file From 727831d4135d7b3dd7396610ac73cf4e6144bc27 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 01:48:17 -0500 Subject: [PATCH 17/33] Add description --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 6625e10..a293d21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [package] name = "luajit2-sys" +description = "LuaJIT-2.1 FFI Bindings" version = "0.0.1" authors = ["Aaron Loucks "] edition = "2018" From 18c5d88e65318c3d7506c510fa328a82decd5b36 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 14:37:42 -0500 Subject: [PATCH 18/33] Cleanup build.rs Set the following environment variables. The luajit lib name varies by platform (lua51 on msvc and luajit everywhere else). - DEP_LUAJIT_INCLUDE - DEP_LUAJIT_LIB_NAME --- Cargo.toml | 1 + build.rs | 143 +++++++++++++++++++++++++++++------------------------ 2 files changed, 80 insertions(+), 64 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a293d21..9dd5807 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" keywords = ["lua", "luajit", "script"] license = "MIT OR Apache-2.0" repository = "https://github.com/aloucks/luajit2-sys" +links = "luajit" [dependencies] libc = "0.2" diff --git a/build.rs b/build.rs index 7f749d7..08e2bbe 100644 --- a/build.rs +++ b/build.rs @@ -1,12 +1,9 @@ -use cc; use fs_extra::dir; use fs_extra::dir::CopyOptions; +use std::env; use std::process::{Command, Stdio}; -use std::{env, fs}; fn main() { - let target = env::var("TARGET").unwrap(); - let luajit_dir = format!("{}/luajit", env!("CARGO_MANIFEST_DIR")); let out_dir = env::var("OUT_DIR").unwrap(); let src_dir = format!("{}/luajit/src", out_dir); @@ -15,76 +12,94 @@ fn main() { dbg!(&out_dir); dbg!(&src_dir); - if cfg!(target_env = "msvc") { - let lib_path = format!("{}/lua51.lib", &src_dir); - dbg!(&lib_path); - if !std::fs::metadata(&lib_path).is_ok() { - let cl_exe: cc::Tool = - cc::windows_registry::find_tool(&target, "cl.exe").expect("cl.exe not found"); - let msvcbuild_bat = format!("{}/msvcbuild.bat", &src_dir); + // DEP_LUAJIT_INCLUDE + // DEB_LUAJIT_LIB_NAME - dbg!(&msvcbuild_bat); + let lib_name = build_luajit(&luajit_dir, &out_dir, &src_dir); - let mut copy_options = CopyOptions::new(); - copy_options.overwrite = true; - dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); + println!("cargo:lib-name={}", lib_name); + println!("cargo:include={}", src_dir); + println!("cargo:rustc-link-search=native={}", src_dir); + println!("cargo:rustc-link-lib=static={}", lib_name); - let mut buildcmd = Command::new(msvcbuild_bat); - for (name, value) in cl_exe.env() { - buildcmd.env(name, value); - } - buildcmd.env("Configuration", "Release"); - buildcmd.args(&["static"]); - buildcmd.current_dir(&src_dir); - buildcmd.stderr(Stdio::inherit()); + // if cfg!(target_os = "macos") && cfg!(target_pointer_width = "64") { + // // RUSTFLAGS='-C link-args=-pagezero_size 10000 -image_base 100000000' + // } +} - let mut child = buildcmd.spawn().expect("failed to run msvcbuild.bat"); +#[cfg(target_env = "msvc")] +fn build_luajit(luajit_dir: &str, out_dir: &str, src_dir: &str) -> &'static str { + const LIB_NAME: &'static str = "lua51"; + let lib_path = format!("{}/{}.lib", &src_dir, LIB_NAME); + dbg!(&lib_path); + if !std::fs::metadata(&lib_path).is_ok() { + let target = env::var("TARGET").unwrap(); + let cl_exe: cc::Tool = + cc::windows_registry::find_tool(&target, "cl.exe").expect("cl.exe not found"); + let msvcbuild_bat = format!("{}/msvcbuild.bat", &src_dir); + dbg!(&msvcbuild_bat); - if !child - .wait() - .map(|status| status.success()) - .map_err(|_| false) - .unwrap_or(false) - { - panic!("Failed to build luajit"); - } + let mut copy_options = CopyOptions::new(); + copy_options.overwrite = true; + dir::copy(&luajit_dir, &out_dir, ©_options) + .expect("failed to copy luajit source to out dir"); + + let mut buildcmd = Command::new(msvcbuild_bat); + for (name, value) in cl_exe.env() { + eprintln!("{:?} = {:?}", name, value); + buildcmd.env(name, value); } - println!("cargo:rustc-link-search=native={}", src_dir); - println!("cargo:rustc-link-lib=static=lua51"); - } else { - let lib_path = format!("{}/libluajit.a", &src_dir); - dbg!(&lib_path); - if !std::fs::metadata(&lib_path).is_ok() { - let mut copy_options = CopyOptions::new(); - copy_options.overwrite = true; - dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); - fs::copy(format!("etc/Makefile"), format!("{}/Makefile", &src_dir)).unwrap(); - let mut buildcmd = Command::new("make"); - buildcmd.current_dir(&src_dir); - buildcmd.stderr(Stdio::inherit()); + buildcmd.env("Configuration", "Release"); + buildcmd.args(&["static"]); + buildcmd.current_dir(&src_dir); + buildcmd.stderr(Stdio::inherit()); - if cfg!(target_pointer_width = "32") { - buildcmd.env("HOST_CC", "gcc -m32"); - buildcmd.arg("-e"); - } + let mut child = buildcmd.spawn().expect("failed to run msvcbuild.bat"); - let mut child = buildcmd.spawn().expect("failed to run make"); - - if !child - .wait() - .map(|status| status.success()) - .map_err(|_| false) - .unwrap_or(false) - { - panic!("Failed to build luajit"); - } + if !child + .wait() + .map(|status| status.success()) + .map_err(|_| false) + .unwrap_or(false) + { + panic!("Failed to build luajit"); } - println!("cargo:rustc-link-search=native={}", src_dir); - println!("cargo:rustc-link-lib=static=luajit"); } - // if cfg!(target_os = "macos") && cfg!(target_pointer_width = "64") { - // // RUSTFLAGS='-C link-args=-pagezero_size 10000 -image_base 100000000' - // } + LIB_NAME +} + +#[cfg(not(target_env = "msvc"))] +fn build_luajit(luajit_dir: &str, out_dir: &str, src_dir: &str) -> &'static str { + const LIB_NAME: &'static str = "luajit"; + let lib_path = format!("{}/lib{}.a", &src_dir, LIB_NAME); + dbg!(&lib_path); + if !std::fs::metadata(&lib_path).is_ok() { + let mut copy_options = CopyOptions::new(); + copy_options.overwrite = true; + dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); + std::fs::copy(format!("etc/Makefile"), format!("{}/Makefile", &src_dir)).unwrap(); + + let mut buildcmd = Command::new("make"); + buildcmd.current_dir(&src_dir); + buildcmd.stderr(Stdio::inherit()); + + if cfg!(target_pointer_width = "32") { + buildcmd.env("HOST_CC", "gcc -m32"); + buildcmd.arg("-e"); + } + + let mut child = buildcmd.spawn().expect("failed to run make"); + + if !child + .wait() + .map(|status| status.success()) + .map_err(|_| false) + .unwrap_or(false) + { + panic!("Failed to build luajit"); + } + } + LIB_NAME } From 56c586ecbf4a79c0d2a8cb0f88512864919d2e3e Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 15:20:31 -0500 Subject: [PATCH 19/33] Update README --- Cargo.toml | 4 +++- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9dd5807..9920e43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,14 @@ [package] name = "luajit2-sys" -description = "LuaJIT-2.1 FFI Bindings" version = "0.0.1" +description = "LuaJIT-2.1 FFI Bindings" authors = ["Aaron Loucks "] edition = "2018" keywords = ["lua", "luajit", "script"] license = "MIT OR Apache-2.0" +readme = "README.md" repository = "https://github.com/aloucks/luajit2-sys" +documentation = "https://docs.rs/luajit2-sys" links = "luajit" [dependencies] diff --git a/README.md b/README.md index 6d5e744..7636366 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,47 @@ # Rust LuaJIT 2 Bindings -[![Build Status](https://dev.azure.com/aloucks/aloucks/_apis/build/status/aloucks.luajit2-sys?branchName=master)](https://dev.azure.com/aloucks/aloucks/_build/latest?definitionId=3&branchName=master) +[![crates.io](https://img.shields.io/crates/v/luajit2-sys.svg)](https://crates.io/crates/luajit2-sys) +[![docs.rs](https://docs.rs/luajit2-sys/badge.svg)](https://docs.rs/luajit2-sys) +[![build](https://dev.azure.com/aloucks/aloucks/_apis/build/status/aloucks.luajit2-sys?branchName=master)](https://dev.azure.com/aloucks/aloucks/_build/latest?definitionId=3&branchName=master) + +```toml +[dependencies] +luajit2-sys = "0.0.1" +``` + +## Exported Cargo Environment Variables + +||| +| -- | -- | +| `DEP_LUAJIT_INCLUDE` | Path to the LuaJIT source and headers | +| `DEP_LUAJIT_LIB_NAME` | Platform specfic lib name (`lua51` on Windows and `luajit` everywhere else) | + +## Example + +```rust +use luajit2_sys as sys; +use std::ffi::CStr; + +fn main() { + unsafe { + let lua = sys::luaL_newstate(); + sys::luaL_openlibs(lua); + let script_data = b"return 1 + 2"; + let script_name = b"run_script\0"; + sys::luaL_loadbuffer( + lua, + script_data.as_ptr() as _, + script_data.len() as _, + script_name.as_ptr() as _, + ); + sys::lua_pcall(lua, 0, 1, 0); + let idx = sys::lua_gettop(lua); + let s = sys::lua_tostring(lua, idx); + let result = CStr::from_ptr(s).to_string_lossy().to_string(); + sys::lua_close(lua); + + println!("result: {}", result); + } +} +``` + From 32f67222d7006a8cf2086299f1cd431916768e32 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 15:29:09 -0500 Subject: [PATCH 20/33] Add weekly build to CI --- .azure-pipelines.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.azure-pipelines.yml b/.azure-pipelines.yml index c6d683b..2445ee1 100644 --- a/.azure-pipelines.yml +++ b/.azure-pipelines.yml @@ -6,6 +6,14 @@ pr: include: - master +schedules: + - cron: "0 12 * * 0" + displayName: Weekly Sunday build + branches: + include: + - master + always: true + jobs: - job: Windows pool: From 972f1022ee3add5e5f6128994a34c62bc6ef948f Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 15:31:07 -0500 Subject: [PATCH 21/33] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7636366..b6b8fc6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Rust LuaJIT 2 Bindings +# Rust LuaJIT Bindings [![crates.io](https://img.shields.io/crates/v/luajit2-sys.svg)](https://crates.io/crates/luajit2-sys) [![docs.rs](https://docs.rs/luajit2-sys/badge.svg)](https://docs.rs/luajit2-sys) From f308e7ba665f3dbf9762c11a248cf556cb663865 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 16:24:01 -0500 Subject: [PATCH 22/33] Version 0.0.2 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9920e43..248c937 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "luajit2-sys" -version = "0.0.1" +version = "0.0.2" description = "LuaJIT-2.1 FFI Bindings" authors = ["Aaron Loucks "] edition = "2018" From 2aa8038854e046bd332578a017b2205a8ee48623 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sun, 19 Jan 2020 16:31:41 -0500 Subject: [PATCH 23/33] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b6b8fc6..873b88e 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ```toml [dependencies] -luajit2-sys = "0.0.1" +luajit2-sys = "0.0.2" ``` ## Exported Cargo Environment Variables From 86757089b42e2d268f35d564e54f04ba50fd9089 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Thu, 2 Mar 2023 15:11:50 +0100 Subject: [PATCH 24/33] chore: Update LuaJIT --- luajit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/luajit b/luajit index f0e865d..505e2c0 160000 --- a/luajit +++ b/luajit @@ -1 +1 @@ -Subproject commit f0e865dd4861520258299d0f2a56491bd9d602e1 +Subproject commit 505e2c03de35e2718eef0d2d3660712e06dadf1f From 089acdbc28c778db64df40d6c4e2655a6f5ddbf9 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Tue, 21 Mar 2023 11:04:21 +0100 Subject: [PATCH 25/33] feat: Implement MSVC cross compilation --- build.rs | 123 +++++++++++++++++-------------------------------------- 1 file changed, 37 insertions(+), 86 deletions(-) diff --git a/build.rs b/build.rs index 08e2bbe..28fe052 100644 --- a/build.rs +++ b/build.rs @@ -3,103 +3,54 @@ use fs_extra::dir::CopyOptions; use std::env; use std::process::{Command, Stdio}; +const LIB_NAME: &str = "luajit"; + fn main() { let luajit_dir = format!("{}/luajit", env!("CARGO_MANIFEST_DIR")); let out_dir = env::var("OUT_DIR").unwrap(); let src_dir = format!("{}/luajit/src", out_dir); + let lib_path = format!("{}/lib{}.a", &src_dir, LIB_NAME); dbg!(&luajit_dir); dbg!(&out_dir); dbg!(&src_dir); + dbg!(&lib_path); - // DEP_LUAJIT_INCLUDE - // DEB_LUAJIT_LIB_NAME + let mut copy_options = CopyOptions::new(); + copy_options.overwrite = true; - let lib_name = build_luajit(&luajit_dir, &out_dir, &src_dir); + dir::copy(&luajit_dir, &out_dir, ©_options).expect("Failed to copy LuaJIT source"); - println!("cargo:lib-name={}", lib_name); + let mut buildcmd = Command::new("make"); + buildcmd.current_dir(&src_dir); + buildcmd.stderr(Stdio::inherit()); + buildcmd.arg("BUILDMODE=static"); + + if env::var("CARGO_CFG_WINDOWS").is_ok() { + buildcmd.arg("TARGET_SYS=Windows"); + buildcmd.arg("CROSS=x86_64-w64-mingw32-"); + } + + if cfg!(target_pointer_width = "32") { + buildcmd.env("HOST_CC", "gcc -m32"); + buildcmd.arg("-e"); + } else { + buildcmd.env("HOST_CC", "gcc"); + } + + let mut child = buildcmd.spawn().expect("failed to run make"); + + if !child + .wait() + .map(|status| status.success()) + .map_err(|_| false) + .unwrap_or(false) + { + panic!("Failed to build luajit"); + } + + println!("cargo:lib-name={}", LIB_NAME); println!("cargo:include={}", src_dir); println!("cargo:rustc-link-search=native={}", src_dir); - println!("cargo:rustc-link-lib=static={}", lib_name); - - // if cfg!(target_os = "macos") && cfg!(target_pointer_width = "64") { - // // RUSTFLAGS='-C link-args=-pagezero_size 10000 -image_base 100000000' - // } -} - -#[cfg(target_env = "msvc")] -fn build_luajit(luajit_dir: &str, out_dir: &str, src_dir: &str) -> &'static str { - const LIB_NAME: &'static str = "lua51"; - let lib_path = format!("{}/{}.lib", &src_dir, LIB_NAME); - dbg!(&lib_path); - if !std::fs::metadata(&lib_path).is_ok() { - let target = env::var("TARGET").unwrap(); - let cl_exe: cc::Tool = - cc::windows_registry::find_tool(&target, "cl.exe").expect("cl.exe not found"); - let msvcbuild_bat = format!("{}/msvcbuild.bat", &src_dir); - dbg!(&msvcbuild_bat); - - let mut copy_options = CopyOptions::new(); - copy_options.overwrite = true; - dir::copy(&luajit_dir, &out_dir, ©_options) - .expect("failed to copy luajit source to out dir"); - - let mut buildcmd = Command::new(msvcbuild_bat); - for (name, value) in cl_exe.env() { - eprintln!("{:?} = {:?}", name, value); - buildcmd.env(name, value); - } - - buildcmd.env("Configuration", "Release"); - buildcmd.args(&["static"]); - buildcmd.current_dir(&src_dir); - buildcmd.stderr(Stdio::inherit()); - - let mut child = buildcmd.spawn().expect("failed to run msvcbuild.bat"); - - if !child - .wait() - .map(|status| status.success()) - .map_err(|_| false) - .unwrap_or(false) - { - panic!("Failed to build luajit"); - } - } - - LIB_NAME -} - -#[cfg(not(target_env = "msvc"))] -fn build_luajit(luajit_dir: &str, out_dir: &str, src_dir: &str) -> &'static str { - const LIB_NAME: &'static str = "luajit"; - let lib_path = format!("{}/lib{}.a", &src_dir, LIB_NAME); - dbg!(&lib_path); - if !std::fs::metadata(&lib_path).is_ok() { - let mut copy_options = CopyOptions::new(); - copy_options.overwrite = true; - dir::copy(&luajit_dir, &out_dir, ©_options).unwrap(); - std::fs::copy(format!("etc/Makefile"), format!("{}/Makefile", &src_dir)).unwrap(); - - let mut buildcmd = Command::new("make"); - buildcmd.current_dir(&src_dir); - buildcmd.stderr(Stdio::inherit()); - - if cfg!(target_pointer_width = "32") { - buildcmd.env("HOST_CC", "gcc -m32"); - buildcmd.arg("-e"); - } - - let mut child = buildcmd.spawn().expect("failed to run make"); - - if !child - .wait() - .map(|status| status.success()) - .map_err(|_| false) - .unwrap_or(false) - { - panic!("Failed to build luajit"); - } - } - LIB_NAME + println!("cargo:rustc-link-lib=static={}", LIB_NAME); } From 206f7884e32837532bf02b9712fd145a92032f35 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Tue, 21 Mar 2023 11:55:46 +0100 Subject: [PATCH 26/33] Move bindgen to build script --- Cargo.toml | 1 + bindgen.sh | 26 -- build.rs | 34 ++ ffi.h | 4 - src/ffi.rs | 1113 ---------------------------------------------------- src/lib.rs | 8 +- 6 files changed, 41 insertions(+), 1145 deletions(-) delete mode 100644 bindgen.sh delete mode 100644 ffi.h delete mode 100644 src/ffi.rs diff --git a/Cargo.toml b/Cargo.toml index 248c937..8758afc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,6 @@ links = "luajit" libc = "0.2" [build-dependencies] +bindgen = "0.64.0" cc = "1.0.40" fs_extra = "1.1.0" diff --git a/bindgen.sh b/bindgen.sh deleted file mode 100644 index 27e7b9e..0000000 --- a/bindgen.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -BINDGEN_VERSION=$(bindgen --version | grep -v -e '^cargo') - -bindgen -o src/ffi.rs \ - --raw-line "/// Generated with: ${BINDGEN_VERSION}" \ - --whitelist-var "LUA.*" \ - --whitelist-var "LUAJIT.*" \ - --whitelist-type "lua_.*" \ - --whitelist-type "luaL_.*" \ - --whitelist-function "lua_.*" \ - --whitelist-function "luaL_.*" \ - --whitelist-function "luaJIT.*" \ - --ctypes-prefix "libc" \ - --use-core \ - --impl-debug \ - ffi.h -- -I luajit/src - -sed -i -e 's/pub fn \(luaJIT_[^\(]*\)/\/\/\/ \n pub fn \1/' src/ffi.rs -sed -i -e 's/pub fn \(lua_[^\(]*\)/\/\/\/ \n pub fn \1/' src/ffi.rs -sed -i -e 's/pub fn \(luaL_[^\(]*\)/\/\/\/ \n pub fn \1/' src/ffi.rs -sed -i -e 's/pub type \(lua_[^\=]*\)/\/\/\/ \n pub type \1/' src/ffi.rs -sed -i -e 's/pub struct \(lua_[^\{]*\)/\/\/\/ \n pub struct \1/' src/ffi.rs -sed -i -e 's/pub struct \(luaL_[^\{]*\)/\/\/\/ \n pub struct \1/' src/ffi.rs - -cargo +stable fmt \ No newline at end of file diff --git a/build.rs b/build.rs index 28fe052..bbafb9a 100644 --- a/build.rs +++ b/build.rs @@ -1,9 +1,11 @@ use fs_extra::dir; use fs_extra::dir::CopyOptions; use std::env; +use std::path::PathBuf; use std::process::{Command, Stdio}; const LIB_NAME: &str = "luajit"; +const LUAJIT_HEADERS: [&str; 4] = ["lua.h", "lualib.h", "lauxlib.h", "luajit.h"]; fn main() { let luajit_dir = format!("{}/luajit", env!("CARGO_MANIFEST_DIR")); @@ -53,4 +55,36 @@ fn main() { println!("cargo:include={}", src_dir); println!("cargo:rustc-link-search=native={}", src_dir); println!("cargo:rustc-link-lib=static={}", LIB_NAME); + + let mut bindings = bindgen::Builder::default(); + + for header in LUAJIT_HEADERS { + println!("cargo:rerun-if-changed={}/src/{}", luajit_dir, header); + bindings = bindings.header(format!("{}/src/{}", luajit_dir, header)); + } + + let bindings = bindings + .allowlist_var("LUA.*") + .allowlist_var("LUAJIT.*") + .allowlist_type("lua_.*") + .allowlist_type("luaL_.*") + .allowlist_function("lua_.*") + .allowlist_function("luaL_.*") + .allowlist_function("luaJIT.*") + .ctypes_prefix("libc") + .impl_debug(true) + .use_core() + .clang_arg("-Iluajit/src") + // Make it pretty + .rustfmt_bindings(true) + .sort_semantically(true) + .merge_extern_blocks(true) + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) + .generate() + .expect("Failed to generate bindings"); + + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + bindings + .write_to_file(out_path.join("bindings.rs")) + .expect("Failed to write bindings"); } diff --git a/ffi.h b/ffi.h deleted file mode 100644 index a9fe40a..0000000 --- a/ffi.h +++ /dev/null @@ -1,4 +0,0 @@ -#include "lua.h" -#include "lualib.h" -#include "lauxlib.h" -#include "luajit.h" \ No newline at end of file diff --git a/src/ffi.rs b/src/ffi.rs deleted file mode 100644 index a33ac54..0000000 --- a/src/ffi.rs +++ /dev/null @@ -1,1113 +0,0 @@ -/* automatically generated by rust-bindgen */ - -// Generated with: bindgen 0.52.0 - -pub const LUA_LDIR: &'static [u8; 7usize] = b"!\\lua\\\0"; -pub const LUA_CDIR: &'static [u8; 3usize] = b"!\\\0"; -pub const LUA_PATH_DEFAULT: &'static [u8; 38usize] = - b".\\?.lua;!\\lua\\?.lua;!\\lua\\?\\init.lua;\0"; -pub const LUA_CPATH_DEFAULT: &'static [u8; 30usize] = b".\\?.dll;!\\?.dll;!\\loadall.dll\0"; -pub const LUA_PATH: &'static [u8; 9usize] = b"LUA_PATH\0"; -pub const LUA_CPATH: &'static [u8; 10usize] = b"LUA_CPATH\0"; -pub const LUA_INIT: &'static [u8; 9usize] = b"LUA_INIT\0"; -pub const LUA_DIRSEP: &'static [u8; 2usize] = b"\\\0"; -pub const LUA_PATHSEP: &'static [u8; 2usize] = b";\0"; -pub const LUA_PATH_MARK: &'static [u8; 2usize] = b"?\0"; -pub const LUA_EXECDIR: &'static [u8; 2usize] = b"!\0"; -pub const LUA_IGMARK: &'static [u8; 2usize] = b"-\0"; -pub const LUA_PATH_CONFIG: &'static [u8; 11usize] = b"\\\n;\n?\n!\n-\n\0"; -pub const LUAI_MAXSTACK: u32 = 65500; -pub const LUAI_MAXCSTACK: u32 = 8000; -pub const LUAI_GCPAUSE: u32 = 200; -pub const LUAI_GCMUL: u32 = 200; -pub const LUA_MAXCAPTURES: u32 = 32; -pub const LUA_IDSIZE: u32 = 60; -pub const LUA_NUMBER_SCAN: &'static [u8; 4usize] = b"%lf\0"; -pub const LUA_NUMBER_FMT: &'static [u8; 6usize] = b"%.14g\0"; -pub const LUAI_MAXNUMBER2STR: u32 = 32; -pub const LUA_INTFRMLEN: &'static [u8; 2usize] = b"l\0"; -pub const LUA_VERSION: &'static [u8; 8usize] = b"Lua 5.1\0"; -pub const LUA_RELEASE: &'static [u8; 10usize] = b"Lua 5.1.4\0"; -pub const LUA_VERSION_NUM: u32 = 501; -pub const LUA_COPYRIGHT: &'static [u8; 41usize] = b"Copyright (C) 1994-2008 Lua.org, PUC-Rio\0"; -pub const LUA_AUTHORS: &'static [u8; 49usize] = - b"R. Ierusalimschy, L. H. de Figueiredo & W. Celes\0"; -pub const LUA_SIGNATURE: &'static [u8; 5usize] = b"\x1BLua\0"; -pub const LUA_MULTRET: i32 = -1; -pub const LUA_REGISTRYINDEX: i32 = -10000; -pub const LUA_ENVIRONINDEX: i32 = -10001; -pub const LUA_GLOBALSINDEX: i32 = -10002; -pub const LUA_OK: u32 = 0; -pub const LUA_YIELD: u32 = 1; -pub const LUA_ERRRUN: u32 = 2; -pub const LUA_ERRSYNTAX: u32 = 3; -pub const LUA_ERRMEM: u32 = 4; -pub const LUA_ERRERR: u32 = 5; -pub const LUA_TNONE: i32 = -1; -pub const LUA_TNIL: u32 = 0; -pub const LUA_TBOOLEAN: u32 = 1; -pub const LUA_TLIGHTUSERDATA: u32 = 2; -pub const LUA_TNUMBER: u32 = 3; -pub const LUA_TSTRING: u32 = 4; -pub const LUA_TTABLE: u32 = 5; -pub const LUA_TFUNCTION: u32 = 6; -pub const LUA_TUSERDATA: u32 = 7; -pub const LUA_TTHREAD: u32 = 8; -pub const LUA_MINSTACK: u32 = 20; -pub const LUA_GCSTOP: u32 = 0; -pub const LUA_GCRESTART: u32 = 1; -pub const LUA_GCCOLLECT: u32 = 2; -pub const LUA_GCCOUNT: u32 = 3; -pub const LUA_GCCOUNTB: u32 = 4; -pub const LUA_GCSTEP: u32 = 5; -pub const LUA_GCSETPAUSE: u32 = 6; -pub const LUA_GCSETSTEPMUL: u32 = 7; -pub const LUA_GCISRUNNING: u32 = 9; -pub const LUA_HOOKCALL: u32 = 0; -pub const LUA_HOOKRET: u32 = 1; -pub const LUA_HOOKLINE: u32 = 2; -pub const LUA_HOOKCOUNT: u32 = 3; -pub const LUA_HOOKTAILRET: u32 = 4; -pub const LUA_MASKCALL: u32 = 1; -pub const LUA_MASKRET: u32 = 2; -pub const LUA_MASKLINE: u32 = 4; -pub const LUA_MASKCOUNT: u32 = 8; -pub const LUA_FILEHANDLE: &'static [u8; 6usize] = b"FILE*\0"; -pub const LUA_COLIBNAME: &'static [u8; 10usize] = b"coroutine\0"; -pub const LUA_MATHLIBNAME: &'static [u8; 5usize] = b"math\0"; -pub const LUA_STRLIBNAME: &'static [u8; 7usize] = b"string\0"; -pub const LUA_TABLIBNAME: &'static [u8; 6usize] = b"table\0"; -pub const LUA_IOLIBNAME: &'static [u8; 3usize] = b"io\0"; -pub const LUA_OSLIBNAME: &'static [u8; 3usize] = b"os\0"; -pub const LUA_LOADLIBNAME: &'static [u8; 8usize] = b"package\0"; -pub const LUA_DBLIBNAME: &'static [u8; 6usize] = b"debug\0"; -pub const LUA_BITLIBNAME: &'static [u8; 4usize] = b"bit\0"; -pub const LUA_JITLIBNAME: &'static [u8; 4usize] = b"jit\0"; -pub const LUA_FFILIBNAME: &'static [u8; 4usize] = b"ffi\0"; -pub const LUA_ERRFILE: u32 = 6; -pub const LUA_NOREF: i32 = -2; -pub const LUA_REFNIL: i32 = -1; -pub const LUAJIT_VERSION: &'static [u8; 19usize] = b"LuaJIT 2.1.0-beta3\0"; -pub const LUAJIT_VERSION_NUM: u32 = 20100; -pub const LUAJIT_COPYRIGHT: &'static [u8; 34usize] = b"Copyright (C) 2005-2017 Mike Pall\0"; -pub const LUAJIT_URL: &'static [u8; 19usize] = b"http://luajit.org/\0"; -pub const LUAJIT_MODE_MASK: u32 = 255; -pub const LUAJIT_MODE_OFF: u32 = 0; -pub const LUAJIT_MODE_ON: u32 = 256; -pub const LUAJIT_MODE_FLUSH: u32 = 512; -pub type va_list = __builtin_va_list; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -/// -pub struct lua_State { - _unused: [u8; 0], -} -/// -pub type lua_CFunction = - ::core::option::Option libc::c_int>; -/// -pub type lua_Reader = ::core::option::Option< - unsafe extern "C" fn( - L: *mut lua_State, - ud: *mut libc::c_void, - sz: *mut usize, - ) -> *const libc::c_char, ->; -/// -pub type lua_Writer = ::core::option::Option< - unsafe extern "C" fn( - L: *mut lua_State, - p: *const libc::c_void, - sz: usize, - ud: *mut libc::c_void, - ) -> libc::c_int, ->; -/// -pub type lua_Alloc = ::core::option::Option< - unsafe extern "C" fn( - ud: *mut libc::c_void, - ptr: *mut libc::c_void, - osize: usize, - nsize: usize, - ) -> *mut libc::c_void, ->; -/// -pub type lua_Number = f64; -/// -pub type lua_Integer = isize; -extern "C" { - /// - pub fn lua_newstate(f: lua_Alloc, ud: *mut libc::c_void) -> *mut lua_State; -} -extern "C" { - /// - pub fn lua_close(L: *mut lua_State); -} -extern "C" { - /// - pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State; -} -extern "C" { - /// - pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction; -} -extern "C" { - /// - pub fn lua_gettop(L: *mut lua_State) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_settop(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_pushvalue(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_remove(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_insert(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_replace(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_checkstack(L: *mut lua_State, sz: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: libc::c_int); -} -extern "C" { - /// - pub fn lua_isnumber(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_isstring(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_iscfunction(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_isuserdata(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_type(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_typename(L: *mut lua_State, tp: libc::c_int) -> *const libc::c_char; -} -extern "C" { - /// - pub fn lua_equal(L: *mut lua_State, idx1: libc::c_int, idx2: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_rawequal(L: *mut lua_State, idx1: libc::c_int, idx2: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_lessthan(L: *mut lua_State, idx1: libc::c_int, idx2: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_tonumber(L: *mut lua_State, idx: libc::c_int) -> lua_Number; -} -extern "C" { - /// - pub fn lua_tointeger(L: *mut lua_State, idx: libc::c_int) -> lua_Integer; -} -extern "C" { - /// - pub fn lua_toboolean(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_tolstring( - L: *mut lua_State, - idx: libc::c_int, - len: *mut usize, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn lua_objlen(L: *mut lua_State, idx: libc::c_int) -> usize; -} -extern "C" { - /// - pub fn lua_tocfunction(L: *mut lua_State, idx: libc::c_int) -> lua_CFunction; -} -extern "C" { - /// - pub fn lua_touserdata(L: *mut lua_State, idx: libc::c_int) -> *mut libc::c_void; -} -extern "C" { - /// - pub fn lua_tothread(L: *mut lua_State, idx: libc::c_int) -> *mut lua_State; -} -extern "C" { - /// - pub fn lua_topointer(L: *mut lua_State, idx: libc::c_int) -> *const libc::c_void; -} -extern "C" { - /// - pub fn lua_pushnil(L: *mut lua_State); -} -extern "C" { - /// - pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number); -} -extern "C" { - /// - pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer); -} -extern "C" { - /// - pub fn lua_pushlstring(L: *mut lua_State, s: *const libc::c_char, l: usize); -} -extern "C" { - /// - pub fn lua_pushstring(L: *mut lua_State, s: *const libc::c_char); -} -extern "C" { - /// - pub fn lua_pushvfstring( - L: *mut lua_State, - fmt: *const libc::c_char, - argp: va_list, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn lua_pushfstring(L: *mut lua_State, fmt: *const libc::c_char, ...) - -> *const libc::c_char; -} -extern "C" { - /// - pub fn lua_pushcclosure(L: *mut lua_State, fn_: lua_CFunction, n: libc::c_int); -} -extern "C" { - /// - pub fn lua_pushboolean(L: *mut lua_State, b: libc::c_int); -} -extern "C" { - /// - pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut libc::c_void); -} -extern "C" { - /// - pub fn lua_pushthread(L: *mut lua_State) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_gettable(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_getfield(L: *mut lua_State, idx: libc::c_int, k: *const libc::c_char); -} -extern "C" { - /// - pub fn lua_rawget(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_rawgeti(L: *mut lua_State, idx: libc::c_int, n: libc::c_int); -} -extern "C" { - /// - pub fn lua_createtable(L: *mut lua_State, narr: libc::c_int, nrec: libc::c_int); -} -extern "C" { - /// - pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut libc::c_void; -} -extern "C" { - /// - pub fn lua_getmetatable(L: *mut lua_State, objindex: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_getfenv(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_settable(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_setfield(L: *mut lua_State, idx: libc::c_int, k: *const libc::c_char); -} -extern "C" { - /// - pub fn lua_rawset(L: *mut lua_State, idx: libc::c_int); -} -extern "C" { - /// - pub fn lua_rawseti(L: *mut lua_State, idx: libc::c_int, n: libc::c_int); -} -extern "C" { - /// - pub fn lua_setmetatable(L: *mut lua_State, objindex: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_setfenv(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_call(L: *mut lua_State, nargs: libc::c_int, nresults: libc::c_int); -} -extern "C" { - /// - pub fn lua_pcall( - L: *mut lua_State, - nargs: libc::c_int, - nresults: libc::c_int, - errfunc: libc::c_int, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_cpcall(L: *mut lua_State, func: lua_CFunction, ud: *mut libc::c_void) - -> libc::c_int; -} -extern "C" { - /// - pub fn lua_load( - L: *mut lua_State, - reader: lua_Reader, - dt: *mut libc::c_void, - chunkname: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_dump(L: *mut lua_State, writer: lua_Writer, data: *mut libc::c_void) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_yield(L: *mut lua_State, nresults: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_resume(L: *mut lua_State, narg: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_status(L: *mut lua_State) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_gc(L: *mut lua_State, what: libc::c_int, data: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_error(L: *mut lua_State) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_next(L: *mut lua_State, idx: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_concat(L: *mut lua_State, n: libc::c_int); -} -extern "C" { - /// - pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut libc::c_void) -> lua_Alloc; -} -extern "C" { - /// - pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut libc::c_void); -} -extern "C" { - /// - pub fn lua_setlevel(from: *mut lua_State, to: *mut lua_State); -} -/// -pub type lua_Hook = - ::core::option::Option; -extern "C" { - /// - pub fn lua_getstack(L: *mut lua_State, level: libc::c_int, ar: *mut lua_Debug) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_getinfo( - L: *mut lua_State, - what: *const libc::c_char, - ar: *mut lua_Debug, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_getlocal( - L: *mut lua_State, - ar: *const lua_Debug, - n: libc::c_int, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn lua_setlocal( - L: *mut lua_State, - ar: *const lua_Debug, - n: libc::c_int, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn lua_getupvalue( - L: *mut lua_State, - funcindex: libc::c_int, - n: libc::c_int, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn lua_setupvalue( - L: *mut lua_State, - funcindex: libc::c_int, - n: libc::c_int, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn lua_sethook( - L: *mut lua_State, - func: lua_Hook, - mask: libc::c_int, - count: libc::c_int, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_gethook(L: *mut lua_State) -> lua_Hook; -} -extern "C" { - /// - pub fn lua_gethookmask(L: *mut lua_State) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_gethookcount(L: *mut lua_State) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_upvalueid(L: *mut lua_State, idx: libc::c_int, n: libc::c_int) -> *mut libc::c_void; -} -extern "C" { - /// - pub fn lua_upvaluejoin( - L: *mut lua_State, - idx1: libc::c_int, - n1: libc::c_int, - idx2: libc::c_int, - n2: libc::c_int, - ); -} -extern "C" { - /// - pub fn lua_loadx( - L: *mut lua_State, - reader: lua_Reader, - dt: *mut libc::c_void, - chunkname: *const libc::c_char, - mode: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn lua_version(L: *mut lua_State) -> *const lua_Number; -} -extern "C" { - /// - pub fn lua_copy(L: *mut lua_State, fromidx: libc::c_int, toidx: libc::c_int); -} -extern "C" { - /// - pub fn lua_tonumberx( - L: *mut lua_State, - idx: libc::c_int, - isnum: *mut libc::c_int, - ) -> lua_Number; -} -extern "C" { - /// - pub fn lua_tointegerx( - L: *mut lua_State, - idx: libc::c_int, - isnum: *mut libc::c_int, - ) -> lua_Integer; -} -extern "C" { - /// - pub fn lua_isyieldable(L: *mut lua_State) -> libc::c_int; -} -#[repr(C)] -#[derive(Copy, Clone)] -/// -pub struct lua_Debug { - pub event: libc::c_int, - pub name: *const libc::c_char, - pub namewhat: *const libc::c_char, - pub what: *const libc::c_char, - pub source: *const libc::c_char, - pub currentline: libc::c_int, - pub nups: libc::c_int, - pub linedefined: libc::c_int, - pub lastlinedefined: libc::c_int, - pub short_src: [libc::c_char; 60usize], - pub i_ci: libc::c_int, -} -#[test] -fn bindgen_test_layout_lua_Debug() { - assert_eq!( - ::core::mem::size_of::(), - 120usize, - concat!("Size of: ", stringify!(lua_Debug)) - ); - assert_eq!( - ::core::mem::align_of::(), - 8usize, - concat!("Alignment of ", stringify!(lua_Debug)) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).event as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(event) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).name as *const _ as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(name) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).namewhat as *const _ as usize }, - 16usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(namewhat) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).what as *const _ as usize }, - 24usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(what) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).source as *const _ as usize }, - 32usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(source) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).currentline as *const _ as usize }, - 40usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(currentline) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).nups as *const _ as usize }, - 44usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(nups) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).linedefined as *const _ as usize }, - 48usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(linedefined) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).lastlinedefined as *const _ as usize }, - 52usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(lastlinedefined) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).short_src as *const _ as usize }, - 56usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(short_src) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).i_ci as *const _ as usize }, - 116usize, - concat!( - "Offset of field: ", - stringify!(lua_Debug), - "::", - stringify!(i_ci) - ) - ); -} -impl ::core::fmt::Debug for lua_Debug { - fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - write ! ( f , "lua_Debug {{ event: {:?}, name: {:?}, namewhat: {:?}, what: {:?}, source: {:?}, currentline: {:?}, nups: {:?}, linedefined: {:?}, lastlinedefined: {:?}, short_src: [...], i_ci: {:?} }}" , self . event , self . name , self . namewhat , self . what , self . source , self . currentline , self . nups , self . linedefined , self . lastlinedefined , self . i_ci ) - } -} -extern "C" { - /// - pub fn luaL_openlibs(L: *mut lua_State); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -/// -pub struct luaL_Reg { - pub name: *const libc::c_char, - pub func: lua_CFunction, -} -#[test] -fn bindgen_test_layout_luaL_Reg() { - assert_eq!( - ::core::mem::size_of::(), - 16usize, - concat!("Size of: ", stringify!(luaL_Reg)) - ); - assert_eq!( - ::core::mem::align_of::(), - 8usize, - concat!("Alignment of ", stringify!(luaL_Reg)) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).name as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(luaL_Reg), - "::", - stringify!(name) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).func as *const _ as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(luaL_Reg), - "::", - stringify!(func) - ) - ); -} -extern "C" { - /// - pub fn luaL_openlib( - L: *mut lua_State, - libname: *const libc::c_char, - l: *const luaL_Reg, - nup: libc::c_int, - ); -} -extern "C" { - /// - pub fn luaL_register(L: *mut lua_State, libname: *const libc::c_char, l: *const luaL_Reg); -} -extern "C" { - /// - pub fn luaL_getmetafield( - L: *mut lua_State, - obj: libc::c_int, - e: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_callmeta( - L: *mut lua_State, - obj: libc::c_int, - e: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_typerror( - L: *mut lua_State, - narg: libc::c_int, - tname: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_argerror( - L: *mut lua_State, - numarg: libc::c_int, - extramsg: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_checklstring( - L: *mut lua_State, - numArg: libc::c_int, - l: *mut usize, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn luaL_optlstring( - L: *mut lua_State, - numArg: libc::c_int, - def: *const libc::c_char, - l: *mut usize, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn luaL_checknumber(L: *mut lua_State, numArg: libc::c_int) -> lua_Number; -} -extern "C" { - /// - pub fn luaL_optnumber(L: *mut lua_State, nArg: libc::c_int, def: lua_Number) -> lua_Number; -} -extern "C" { - /// - pub fn luaL_checkinteger(L: *mut lua_State, numArg: libc::c_int) -> lua_Integer; -} -extern "C" { - /// - pub fn luaL_optinteger(L: *mut lua_State, nArg: libc::c_int, def: lua_Integer) -> lua_Integer; -} -extern "C" { - /// - pub fn luaL_checkstack(L: *mut lua_State, sz: libc::c_int, msg: *const libc::c_char); -} -extern "C" { - /// - pub fn luaL_checktype(L: *mut lua_State, narg: libc::c_int, t: libc::c_int); -} -extern "C" { - /// - pub fn luaL_checkany(L: *mut lua_State, narg: libc::c_int); -} -extern "C" { - /// - pub fn luaL_newmetatable(L: *mut lua_State, tname: *const libc::c_char) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_checkudata( - L: *mut lua_State, - ud: libc::c_int, - tname: *const libc::c_char, - ) -> *mut libc::c_void; -} -extern "C" { - /// - pub fn luaL_where(L: *mut lua_State, lvl: libc::c_int); -} -extern "C" { - /// - pub fn luaL_error(L: *mut lua_State, fmt: *const libc::c_char, ...) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_checkoption( - L: *mut lua_State, - narg: libc::c_int, - def: *const libc::c_char, - lst: *const *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_ref(L: *mut lua_State, t: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_unref(L: *mut lua_State, t: libc::c_int, ref_: libc::c_int); -} -extern "C" { - /// - pub fn luaL_loadfile(L: *mut lua_State, filename: *const libc::c_char) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_loadbuffer( - L: *mut lua_State, - buff: *const libc::c_char, - sz: usize, - name: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_loadstring(L: *mut lua_State, s: *const libc::c_char) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_newstate() -> *mut lua_State; -} -extern "C" { - /// - pub fn luaL_gsub( - L: *mut lua_State, - s: *const libc::c_char, - p: *const libc::c_char, - r: *const libc::c_char, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn luaL_findtable( - L: *mut lua_State, - idx: libc::c_int, - fname: *const libc::c_char, - szhint: libc::c_int, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn luaL_fileresult( - L: *mut lua_State, - stat: libc::c_int, - fname: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_execresult(L: *mut lua_State, stat: libc::c_int) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_loadfilex( - L: *mut lua_State, - filename: *const libc::c_char, - mode: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_loadbufferx( - L: *mut lua_State, - buff: *const libc::c_char, - sz: usize, - name: *const libc::c_char, - mode: *const libc::c_char, - ) -> libc::c_int; -} -extern "C" { - /// - pub fn luaL_traceback( - L: *mut lua_State, - L1: *mut lua_State, - msg: *const libc::c_char, - level: libc::c_int, - ); -} -extern "C" { - /// - pub fn luaL_setfuncs(L: *mut lua_State, l: *const luaL_Reg, nup: libc::c_int); -} -extern "C" { - /// - pub fn luaL_pushmodule(L: *mut lua_State, modname: *const libc::c_char, sizehint: libc::c_int); -} -extern "C" { - /// - pub fn luaL_testudata( - L: *mut lua_State, - ud: libc::c_int, - tname: *const libc::c_char, - ) -> *mut libc::c_void; -} -extern "C" { - /// - pub fn luaL_setmetatable(L: *mut lua_State, tname: *const libc::c_char); -} -#[repr(C)] -#[derive(Copy, Clone)] -/// -pub struct luaL_Buffer { - pub p: *mut libc::c_char, - pub lvl: libc::c_int, - pub L: *mut lua_State, - pub buffer: [libc::c_char; 512usize], -} -#[test] -fn bindgen_test_layout_luaL_Buffer() { - assert_eq!( - ::core::mem::size_of::(), - 536usize, - concat!("Size of: ", stringify!(luaL_Buffer)) - ); - assert_eq!( - ::core::mem::align_of::(), - 8usize, - concat!("Alignment of ", stringify!(luaL_Buffer)) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).p as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(luaL_Buffer), - "::", - stringify!(p) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).lvl as *const _ as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(luaL_Buffer), - "::", - stringify!(lvl) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).L as *const _ as usize }, - 16usize, - concat!( - "Offset of field: ", - stringify!(luaL_Buffer), - "::", - stringify!(L) - ) - ); - assert_eq!( - unsafe { &(*(::core::ptr::null::())).buffer as *const _ as usize }, - 24usize, - concat!( - "Offset of field: ", - stringify!(luaL_Buffer), - "::", - stringify!(buffer) - ) - ); -} -impl ::core::fmt::Debug for luaL_Buffer { - fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - write!( - f, - "luaL_Buffer {{ p: {:?}, lvl: {:?}, L: {:?}, buffer: [...] }}", - self.p, self.lvl, self.L - ) - } -} -extern "C" { - /// - pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Buffer); -} -extern "C" { - /// - pub fn luaL_prepbuffer(B: *mut luaL_Buffer) -> *mut libc::c_char; -} -extern "C" { - /// - pub fn luaL_addlstring(B: *mut luaL_Buffer, s: *const libc::c_char, l: usize); -} -extern "C" { - /// - pub fn luaL_addstring(B: *mut luaL_Buffer, s: *const libc::c_char); -} -extern "C" { - /// - pub fn luaL_addvalue(B: *mut luaL_Buffer); -} -extern "C" { - /// - pub fn luaL_pushresult(B: *mut luaL_Buffer); -} -pub const LUAJIT_MODE_ENGINE: _bindgen_ty_1 = 0; -pub const LUAJIT_MODE_DEBUG: _bindgen_ty_1 = 1; -pub const LUAJIT_MODE_FUNC: _bindgen_ty_1 = 2; -pub const LUAJIT_MODE_ALLFUNC: _bindgen_ty_1 = 3; -pub const LUAJIT_MODE_ALLSUBFUNC: _bindgen_ty_1 = 4; -pub const LUAJIT_MODE_TRACE: _bindgen_ty_1 = 5; -pub const LUAJIT_MODE_WRAPCFUNC: _bindgen_ty_1 = 16; -pub const LUAJIT_MODE_MAX: _bindgen_ty_1 = 17; -pub type _bindgen_ty_1 = i32; -extern "C" { - /// - pub fn luaJIT_setmode(L: *mut lua_State, idx: libc::c_int, mode: libc::c_int) -> libc::c_int; -} -pub type luaJIT_profile_callback = ::core::option::Option< - unsafe extern "C" fn( - data: *mut libc::c_void, - L: *mut lua_State, - samples: libc::c_int, - vmstate: libc::c_int, - ), ->; -extern "C" { - /// - pub fn luaJIT_profile_start( - L: *mut lua_State, - mode: *const libc::c_char, - cb: luaJIT_profile_callback, - data: *mut libc::c_void, - ); -} -extern "C" { - /// - pub fn luaJIT_profile_stop(L: *mut lua_State); -} -extern "C" { - /// - pub fn luaJIT_profile_dumpstack( - L: *mut lua_State, - fmt: *const libc::c_char, - depth: libc::c_int, - len: *mut usize, - ) -> *const libc::c_char; -} -extern "C" { - /// - pub fn luaJIT_version_2_1_0_beta3(); -} -pub type __builtin_va_list = *mut libc::c_char; diff --git a/src/lib.rs b/src/lib.rs index e0d799f..c48ca06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ #![no_std] #![allow(non_snake_case)] #![allow(non_camel_case_types)] +#![allow(clippy::deprecated_semver)] +#![allow(clippy::missing_safety_doc)] //! # LuaJIT 2.1 //! @@ -15,7 +17,9 @@ //! //! -mod ffi; +mod ffi { + include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +} pub use ffi::*; use core::ptr; @@ -92,7 +96,7 @@ pub unsafe fn lua_isthread(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { /// #[inline] pub unsafe fn lua_isnone(L: *mut lua_State, idx: libc::c_int) -> libc::c_int { - (lua_type(L, idx) == LUA_TNONE as i32) as i32 + (lua_type(L, idx) == LUA_TNONE) as i32 } /// From 9ab05ce18cc35b3235b0cc7665d6e860193c9b11 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Thu, 23 Mar 2023 13:35:56 +0100 Subject: [PATCH 27/33] feat: Implement cross compilation for MSVC toolchain --- Cargo.toml | 4 +- build.rs | 179 +++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 156 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8758afc..23f0cd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "luajit2-sys" version = "0.0.2" description = "LuaJIT-2.1 FFI Bindings" authors = ["Aaron Loucks "] -edition = "2018" +edition = "2021" keywords = ["lua", "luajit", "script"] license = "MIT OR Apache-2.0" readme = "README.md" @@ -16,5 +16,5 @@ libc = "0.2" [build-dependencies] bindgen = "0.64.0" -cc = "1.0.40" +cc = "1" fs_extra = "1.1.0" diff --git a/build.rs b/build.rs index bbafb9a..cfcebdd 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,4 @@ +use cc::Build; use fs_extra::dir; use fs_extra::dir::CopyOptions; use std::env; @@ -6,38 +7,101 @@ use std::process::{Command, Stdio}; const LIB_NAME: &str = "luajit"; const LUAJIT_HEADERS: [&str; 4] = ["lua.h", "lualib.h", "lauxlib.h", "luajit.h"]; +const LUAJIT_SRC: [&str; 69] = [ + // LJCORE_O + // The MSVC toolchain cannot compile this assembler file, + // as it contains GNU-specific directives + // "lj_vm.S", + "lj_assert.c", + "lj_gc.c", + "lj_err.c", + "lj_char.c", + "lj_bc.c", + "lj_obj.c", + "lj_buf.c", + "lj_str.c", + "lj_tab.c", + "lj_func.c", + "lj_udata.c", + "lj_meta.c", + "lj_debug.c", + "lj_prng.c", + "lj_state.c", + "lj_dispatch.c", + "lj_vmevent.c", + "lj_vmmath.c", + "lj_strscan.c", + "lj_strfmt.c", + "lj_strfmt_num.c", + "lj_serialize.c", + "lj_api.c", + "lj_profile.c", + "lj_lex.c", + "lj_parse.c", + "lj_bcread.c", + "lj_bcwrite.c", + "lj_load.c", + "lj_ir.c", + "lj_opt_mem.c", + "lj_opt_fold.c", + "lj_opt_narrow.c", + "lj_opt_dce.c", + "lj_opt_loop.c", + "lj_opt_split.c", + "lj_opt_sink.c", + "lj_mcode.c", + "lj_snap.c", + "lj_record.c", + "lj_crecord.c", + "lj_ffrecord.c", + "lj_asm.c", + "lj_trace.c", + "lj_gdbjit.c", + "lj_ctype.c", + "lj_cdata.c", + "lj_cconv.c", + "lj_ccall.c", + "lj_ccallback.c", + "lj_carith.c", + "lj_clib.c", + "lj_cparse.c", + "lj_lib.c", + "lj_alloc.c", + // LJLIB_O + "lib_aux.c", + "lib_base.c", + "lib_math.c", + "lib_bit.c", + "lib_string.c", + "lib_table.c", + "lib_io.c", + "lib_os.c", + "lib_package.c", + "lib_debug.c", + "lib_jit.c", + "lib_ffi.c", + "lib_buffer.c", + "lib_init.c", +]; -fn main() { - let luajit_dir = format!("{}/luajit", env!("CARGO_MANIFEST_DIR")); - let out_dir = env::var("OUT_DIR").unwrap(); - let src_dir = format!("{}/luajit/src", out_dir); - let lib_path = format!("{}/lib{}.a", &src_dir, LIB_NAME); - - dbg!(&luajit_dir); - dbg!(&out_dir); - dbg!(&src_dir); - dbg!(&lib_path); - - let mut copy_options = CopyOptions::new(); - copy_options.overwrite = true; - - dir::copy(&luajit_dir, &out_dir, ©_options).expect("Failed to copy LuaJIT source"); - +fn build_gcc(src_dir: &str) { let mut buildcmd = Command::new("make"); buildcmd.current_dir(&src_dir); buildcmd.stderr(Stdio::inherit()); - buildcmd.arg("BUILDMODE=static"); + buildcmd.arg("--no-silent"); + // We do need to cross-compile even here, so that `lj_vm.o` is created + // for the correct architecture. if env::var("CARGO_CFG_WINDOWS").is_ok() { buildcmd.arg("TARGET_SYS=Windows"); buildcmd.arg("CROSS=x86_64-w64-mingw32-"); } if cfg!(target_pointer_width = "32") { - buildcmd.env("HOST_CC", "gcc -m32"); + buildcmd.arg("HOST_CC='gcc -m32'"); buildcmd.arg("-e"); } else { - buildcmd.env("HOST_CC", "gcc"); + buildcmd.arg("HOST_CC='gcc'"); } let mut child = buildcmd.spawn().expect("failed to run make"); @@ -50,11 +114,66 @@ fn main() { { panic!("Failed to build luajit"); } +} + +fn build_msvc(src_dir: &str, out_dir: &str) { + let mut cc = Build::new(); + // cc can't handle many of the `cland-dl`-specific flags, so + // we need to port them manually from a `make -n` run. + cc.out_dir(out_dir) + // `llvm-as` (which the clang-based toolchain for MSVC would use to compile `lj_vm.S` + // assembler) doesn't support some of the GNU-specific directives. + // However, the previous host-targeted compilation already created the + // object, so we simply link that. + .object(format!("{src_dir}/lj_vm.o")) + .define("_FILE_OFFSET_BITS", "64") + .define("_LARGEFILE_SOURCE", None) + .define("LUA_MULTILIB", "\"lib\"") + .define("LUAJIT_UNWIND_EXTERNAL", None) + .flag("-fcolor-diagnostics") + // Disable warnings + .flag("/W0") + .flag("/U _FORTIFY_SOURCE") + // Link statically + .flag("/MT") + // Omit frame pointers + .flag("/Oy"); + + for f in LUAJIT_SRC { + cc.file(format!("{src_dir}/{f}")); + } + + cc.compile(LIB_NAME); +} + +fn main() { + let luajit_dir = format!("{}/luajit", env!("CARGO_MANIFEST_DIR")); + let out_dir = env::var("OUT_DIR").unwrap(); + let src_dir = format!("{}/luajit/src", out_dir); + + dbg!(&luajit_dir); + dbg!(&out_dir); + dbg!(&src_dir); + + let mut copy_options = CopyOptions::new(); + copy_options.overwrite = true; + + dir::copy(&luajit_dir, &out_dir, ©_options).expect("Failed to copy LuaJIT source"); + + // The first run builds with and for the host architecture. + // This also creates all the tools and generated sources that a compilation needs. + build_gcc(&src_dir); + + // Then, for cross-compilation, we can utilize those generated + // sources to re-compile just the library. + if env::var("CARGO_CFG_WINDOWS").is_ok() { + build_msvc(&src_dir, &out_dir); + } println!("cargo:lib-name={}", LIB_NAME); println!("cargo:include={}", src_dir); - println!("cargo:rustc-link-search=native={}", src_dir); - println!("cargo:rustc-link-lib=static={}", LIB_NAME); + println!("cargo:rustc-link-search={}", out_dir); + println!("cargo:rustc-link-lib={}", LIB_NAME); let mut bindings = bindgen::Builder::default(); @@ -74,14 +193,24 @@ fn main() { .ctypes_prefix("libc") .impl_debug(true) .use_core() - .clang_arg("-Iluajit/src") + .detect_include_paths(true) // Make it pretty .rustfmt_bindings(true) .sort_semantically(true) .merge_extern_blocks(true) - .parse_callbacks(Box::new(bindgen::CargoCallbacks)) - .generate() - .expect("Failed to generate bindings"); + .parse_callbacks(Box::new(bindgen::CargoCallbacks)); + + let bindings = if env::var("CARGO_CFG_WINDOWS").is_ok() { + bindings + .clang_arg("-I/xwin/sdk/include/ucrt") + .clang_arg("-I/xwin/sdk/include/um") + .clang_arg("-I/xwin/sdk/include/shared") + .clang_arg("-I/xwin/crt/include") + .generate() + .expect("Failed to generate bindings") + } else { + bindings.generate().expect("Failed to generate bindings") + }; let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); bindings From 18797c4d2a53834210fd096dd39195ce7f2bce21 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Thu, 23 Mar 2023 15:18:31 +0100 Subject: [PATCH 28/33] fix: Fix linking on Linux --- build.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index cfcebdd..d53448e 100644 --- a/build.rs +++ b/build.rs @@ -168,12 +168,14 @@ fn main() { // sources to re-compile just the library. if env::var("CARGO_CFG_WINDOWS").is_ok() { build_msvc(&src_dir, &out_dir); + println!("cargo:rustc-link-search={}", out_dir); + } else { + println!("cargo:rustc-link-search=native={}", src_dir); } println!("cargo:lib-name={}", LIB_NAME); println!("cargo:include={}", src_dir); - println!("cargo:rustc-link-search={}", out_dir); - println!("cargo:rustc-link-lib={}", LIB_NAME); + println!("cargo:rustc-link-lib=static={}", LIB_NAME); let mut bindings = bindgen::Builder::default(); From 11c4eddaa4667ea7fffad40b034cf3fcb19fbdd3 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Tue, 28 Mar 2023 20:49:15 +0200 Subject: [PATCH 29/33] chore: Fix lcippy warning --- build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.rs b/build.rs index d53448e..11b3984 100644 --- a/build.rs +++ b/build.rs @@ -86,7 +86,7 @@ const LUAJIT_SRC: [&str; 69] = [ fn build_gcc(src_dir: &str) { let mut buildcmd = Command::new("make"); - buildcmd.current_dir(&src_dir); + buildcmd.current_dir(src_dir); buildcmd.stderr(Stdio::inherit()); buildcmd.arg("--no-silent"); From 7395cf0d5360e78e511825b4b5a82b3cc50b4905 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Thu, 6 Apr 2023 11:16:14 +0200 Subject: [PATCH 30/33] fix: Disable 64bit GC --- build.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/build.rs b/build.rs index 11b3984..2f5fc48 100644 --- a/build.rs +++ b/build.rs @@ -90,6 +90,12 @@ fn build_gcc(src_dir: &str) { buildcmd.stderr(Stdio::inherit()); buildcmd.arg("--no-silent"); + // This became enabled by default in https://github.com/LuaJIT/LuaJIT/commit/bd00094c3b50e193fb32aad79b7ea8ea6b78ed25 + // but changes the generated bytecode. Bitsquid does not have it currently. + // The documentation changes in the commit mention that the bytecode change might be "rectified" + // in the future, though. + buildcmd.arg("XCFLAGS=-DLUAJIT_DISABLE_GC64"); + // We do need to cross-compile even here, so that `lj_vm.o` is created // for the correct architecture. if env::var("CARGO_CFG_WINDOWS").is_ok() { From 24da35e631099e914d6fc1bcc863228c48e540ec Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Sun, 9 Apr 2023 12:21:29 +0200 Subject: [PATCH 31/33] fix: Fix LuaJIT on Windows It turned out that with disabling GC64, LuaJIT would immediately overflow the stack when opening a Lua state. For now, this reverts to a version from 2019 that works, but I highly doubt that LuaJIT woould have been broken on Windows for years. I'll have to investigate, or rather trial-and-error further. --- build.rs | 12 +----------- luajit | 2 +- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/build.rs b/build.rs index 2f5fc48..6e7e5a4 100644 --- a/build.rs +++ b/build.rs @@ -7,12 +7,11 @@ use std::process::{Command, Stdio}; const LIB_NAME: &str = "luajit"; const LUAJIT_HEADERS: [&str; 4] = ["lua.h", "lualib.h", "lauxlib.h", "luajit.h"]; -const LUAJIT_SRC: [&str; 69] = [ +const LUAJIT_SRC: [&str; 65] = [ // LJCORE_O // The MSVC toolchain cannot compile this assembler file, // as it contains GNU-specific directives // "lj_vm.S", - "lj_assert.c", "lj_gc.c", "lj_err.c", "lj_char.c", @@ -25,7 +24,6 @@ const LUAJIT_SRC: [&str; 69] = [ "lj_udata.c", "lj_meta.c", "lj_debug.c", - "lj_prng.c", "lj_state.c", "lj_dispatch.c", "lj_vmevent.c", @@ -33,7 +31,6 @@ const LUAJIT_SRC: [&str; 69] = [ "lj_strscan.c", "lj_strfmt.c", "lj_strfmt_num.c", - "lj_serialize.c", "lj_api.c", "lj_profile.c", "lj_lex.c", @@ -80,7 +77,6 @@ const LUAJIT_SRC: [&str; 69] = [ "lib_debug.c", "lib_jit.c", "lib_ffi.c", - "lib_buffer.c", "lib_init.c", ]; @@ -90,12 +86,6 @@ fn build_gcc(src_dir: &str) { buildcmd.stderr(Stdio::inherit()); buildcmd.arg("--no-silent"); - // This became enabled by default in https://github.com/LuaJIT/LuaJIT/commit/bd00094c3b50e193fb32aad79b7ea8ea6b78ed25 - // but changes the generated bytecode. Bitsquid does not have it currently. - // The documentation changes in the commit mention that the bytecode change might be "rectified" - // in the future, though. - buildcmd.arg("XCFLAGS=-DLUAJIT_DISABLE_GC64"); - // We do need to cross-compile even here, so that `lj_vm.o` is created // for the correct architecture. if env::var("CARGO_CFG_WINDOWS").is_ok() { diff --git a/luajit b/luajit index 505e2c0..70f4b15 160000 --- a/luajit +++ b/luajit @@ -1 +1 @@ -Subproject commit 505e2c03de35e2718eef0d2d3660712e06dadf1f +Subproject commit 70f4b15ee45a6137fe6b48b941faea79d72f7159 From c7ff0b7a908806f2ef3045eb5f5b1bb7b78b762c Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Wed, 15 May 2024 21:57:04 +0200 Subject: [PATCH 32/33] Update bindgen --- Cargo.toml | 2 +- build.rs | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 23f0cd3..1f48fa3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,6 @@ links = "luajit" libc = "0.2" [build-dependencies] -bindgen = "0.64.0" +bindgen = "0.69.4" cc = "1" fs_extra = "1.1.0" diff --git a/build.rs b/build.rs index 6e7e5a4..d291dd7 100644 --- a/build.rs +++ b/build.rs @@ -192,11 +192,10 @@ fn main() { .impl_debug(true) .use_core() .detect_include_paths(true) - // Make it pretty - .rustfmt_bindings(true) + .formatter(bindgen::Formatter::Rustfmt) .sort_semantically(true) .merge_extern_blocks(true) - .parse_callbacks(Box::new(bindgen::CargoCallbacks)); + .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())); let bindings = if env::var("CARGO_CFG_WINDOWS").is_ok() { bindings From 6d94a4dd2c296bf1f044ee4c70fb10dca4c1c241 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Wed, 21 Aug 2024 14:33:15 +0200 Subject: [PATCH 33/33] Update bindgen --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1f48fa3..69e935b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,6 @@ links = "luajit" libc = "0.2" [build-dependencies] -bindgen = "0.69.4" +bindgen = "0.70.1" cc = "1" fs_extra = "1.1.0"