[][src]Struct luajit::state::State

pub struct State { /* fields omitted */ }

Methods

impl State[src]

pub fn new() -> State[src]

Calls LUA C API to instantiate a new Lua state.

pub fn from_ptr(state: *mut lua_State) -> State[src]

Wraps an existing Lua state. Suitable for use in function handlers passed to Lua through the C API.

pub fn open_libs(&mut self)[src]

Opens the Lua standard library on this state.

You can use the other open_* methods to fine tune what library functions should be available to Lua scripts.

pub fn open_base(&mut self)[src]

Opens the Lua basic library on this state.

pub fn open_math(&mut self)[src]

Opens the Lua math library on this state.

pub fn open_string(&mut self)[src]

Opens the Lua string library on this state.

pub fn open_table(&mut self)[src]

Opens the Lua table library on this state.

pub fn open_io(&mut self)[src]

Opens the Lua io library on this state.

pub fn open_os(&mut self)[src]

Opens the Lua os library on this state.

pub fn open_package(&mut self)[src]

Opens the Lua package library on this state.

pub fn open_debug(&mut self)[src]

Opens the Lua debug library on this state.

pub fn open_bit(&mut self)[src]

Opens the Lua bit library on this state.

pub fn open_jit(&mut self)[src]

Opens the LuaJIT JIT library on this state.

pub fn open_ffi(&mut self)[src]

Opens the Lua FFI library on this state.

pub fn settop(&mut self, idx: i32)[src]

Sets the top of the stack to the valid index idx

pub fn pop(&mut self, n: i32)[src]

Pops a value from the top of the stack

pub fn do_string(&mut self, s: &str) -> ThreadStatus[src]

Executes an arbitrary string as Lua code.

Examples

use luajit::{State, ThreadStatus};
 
let mut state = State::new(); // Create new Lua state
state.open_base(); // Need to open base libraries for `print` to be available
 
let status = state.do_string(r#"print("Hello world!")"#);
assert!(status == ThreadStatus::Ok);

pub fn call(&mut self, nargs: i32, nres: i32)[src]

Maps to lua_call, calls the function on the top of the stack.

pub fn pcall(
    &mut self,
    nargs: i32,
    nres: i32,
    err_func: i32
) -> Result<(), (ThreadStatus, String)>
[src]

Maps to lua_pcall and automatically catches an error, returning the string on the top of the stack as an Err result.

pub fn pcallx(&mut self, nargs: i32, nres: i32, err_func: i32) -> ThreadStatus[src]

Maps directly to lua_pcall without additional handling.

pub fn register(&mut self, name: &str, f: LuaFunction)[src]

Registers function f as a Lua global named name

Examples

use luajit::{State, ThreadStatus, c_int};
use luajit::ffi::lua_State;
 
unsafe extern "C" fn hello(L: *mut lua_State) -> c_int {
    println!("Hello world!");
 
    0
}
 
let mut state = State::new();
state.register("hello", hello);
 
let status = state.do_string("hello()");
assert!(status == ThreadStatus::Ok);

Using an argument.

use luajit::{State, ThreadStatus, c_int};
use luajit::ffi::lua_State;
 
unsafe extern "C" fn hello_name(l: *mut lua_State) -> c_int {
    let mut state = State::from_ptr(l);
    match state.to_str(1) {
        Some(s) => println!("Hello {}", s),
        None => println!("You have no name!"),
    }
 
    0
}
 
let mut state = State::new();
state.register("hello", hello_name);
 
let status = state.do_string(r#"hello("world!")"#);
assert!(status == ThreadStatus::Ok);

pub fn is_number(&mut self, idx: c_int) -> bool[src]

Test if the value at idx on the stack is a number.

pub fn is_string(&mut self, idx: c_int) -> bool[src]

Test if the value at idx on the stack is a string.

pub fn is_bool(&mut self, idx: c_int) -> bool[src]

Test if the value at idx on the stack is a boolean.

pub fn is_userdata(&mut self, idx: c_int) -> bool[src]

Test if the value at idx on the stack is a userdata object

pub fn to_str(&mut self, idx: c_int) -> Option<&str>[src]

Retrieves a string from the Lua stack.

pub fn to_int(&mut self, idx: c_int) -> Option<i32>[src]

Return the value on the stack at idx as an integer.

pub fn to_long(&mut self, idx: c_int) -> Option<i64>[src]

Return the value on the stack at idx as an integer.

pub fn to_bool(&mut self, idx: c_int) -> Option<bool>[src]

Return the value on the stack at idx as an bool.

pub fn to_float(&mut self, idx: c_int) -> Option<f32>[src]

Return the value on the stack at idx as an float.

pub fn to_double(&mut self, idx: c_int) -> Option<f64>[src]

Return the value on the stack at idx as an double.

pub fn to_raw_userdata(&mut self, idx: c_int) -> Option<*mut c_void>[src]

Returns the userdata on the top of the Lua stack as a raw pointer

pub fn to_userdata<T>(&mut self, idx: c_int) -> Option<*mut T>[src]

Returns the userdata from the top of the Lua stack, cast as a pointer to type T

See new_userdata for more usage.

pub fn check_userdata_ex<T>(&mut self, idx: c_int, ty: &str) -> Option<*mut T>[src]

Validates that the userdata at idx has metatable ty from the Lua registry and returns a pointer to the userdata object

pub fn check_userdata<T>(&mut self, idx: i32) -> Option<*mut T> where
    T: LuaObject
[src]

Validates that the userdata at idx is an instance of struct T where T implements LuaObject, and returns a pointer to the userdata object

pub fn set_global(&mut self, name: &str)[src]

Pops a value of the Lua stack and sets it as a global value named name

pub fn set_field(&mut self, idx: i32, name: &str)[src]

Sets the value of name on the table t pointed to by idx as the value on the top of the stack.

Equivalent to t[name] = v where t is the value at idx and v is the value at the top of the stack

pub fn register_fns(&mut self, name: Option<&str>, fns: Vec<luaL_Reg>)[src]

Registers all functions in fns on the global table name. If name is None, all functions are instead registered on the value on the top of the stack.

pub fn push_value(&mut self, idx: i32)[src]

Copys the value at idx to the top of the stack

pub fn push<T>(&mut self, val: T) where
    T: LuaValue
[src]

Pushes a LuaValue to the lua stack.

Examples

use luajit::State;
 
let mut state = State::new();
state.push(5);
state.push("Hello world!");

Can also be used with structs that implement LuaObject

#[macro_use] extern crate luajit;
 
use luajit::{State, LuaObject, c_int};
use luajit::ffi::luaL_Reg;
 
struct Point2D {
    x: i32,
    y: i32,
}
 
impl LuaObject for Point2D {
    fn name() -> *const i8 {
        c_str!("Point2D")
    }
 
    fn lua_fns() -> Vec<luaL_Reg> {
        vec!(lua_method!("add", Point2D, Point2D::add))
    }
}
 
impl Point2D {
    fn add(&mut self, state: &mut State) -> c_int {
        state.push(self.x + self.y);
 
        1
    }
 
    fn new() -> Point2D {
        Point2D {
            x: 0,
            y: 0,
        }
    }
}
 
 
fn main() {
    let mut state = State::new();
    state.open_libs();
    state.push(Point2D::new());
    state.set_global("point");
    let res = state.do_string(r#"print(point:add())"#);
    assert_eq!(res, luajit::ThreadStatus::Ok);
}

pub fn push_nil(&mut self)[src]

Push a new nil value onto the Lua stack.

pub fn get_global(&mut self, name: &str)[src]

Gets a value from the globals object and pushes it to the top of the stack.

pub fn get_field(&mut self, idx: i32, name: &str)[src]

Gets a value name from the table on the stack at idx and and pushes the fetched value to the top of the stack.

pub fn new_table(&mut self)[src]

Creates a new table and pushes it to the top of the stack

pub fn new_raw_userdata(&mut self, sz: usize) -> *mut c_void[src]

Allocates a new Lua userdata block, and returns the pointer to it. The returned pointer is owned by the Lua state.

pub fn new_userdata<T>(&mut self) -> *mut T[src]

Allocates a new Lua userdata block of size sizeof(T) for use to store Rust objects on the Lua stack. The returned pointer is owned by the Lua state.

Examples

Useful for pushing an arbitrary struct to the Lua stack

extern crate luajit;
 
use luajit::State;
 
struct Point2D {
    x: i32,
    y: i32,
}
 
impl Point2D {
    fn add(&self) -> i32 {
        self.x + self.y
    }
     
    fn set_x(&mut self, x: i32) {
        self.x = x;
    }
 
    fn set_y(&mut self, y: i32) {
        self.y = y;
    }
 
    fn new() -> Point2D {
        Point2D {
            x: 0,
            y: 0,
        }
    }
}
 
 
fn main() {
    let mut state = State::new();
    state.open_libs();
    unsafe {
        *state.new_userdata() = Point2D::new();
    }
 
    let point: &mut Point2D = unsafe { &mut *state.to_userdata(-1).unwrap() };
     
    point.set_x(2);
    point.set_y(4);
 
    assert_eq!(point.add(), 6);
}

pub fn register_struct<T>(&mut self) where
    T: LuaObject
[src]

Registers all of the methods for LuaObject T as a global metatable with name struct_type and leaves it on the top of the stack.

pub fn load_file(&mut self, path: &Path) -> Result<(), (ThreadStatus, String)>[src]

Maps to luaL_loadfile, this method validates that the file exists before passing it into the Lua C API.

pub fn do_file(&mut self, path: &Path) -> Result<(), (ThreadStatus, String)>[src]

Equivalent of luaL_dofile, loads a file and then immediately executes it with pcall, returning the result.

pub fn checkstack(&mut self, n: usize) -> bool[src]

Ensures that there are at least n free stack slots in the stack. Returns false if it cannot grow the stack to that size.

Trait Implementations

impl Drop for State[src]

Auto Trait Implementations

impl !Send for State

impl !Sync for State

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]