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

pub struct State { /* fields omitted */ }

Methods

impl State
[src]

Calls LUA C API to instantiate a new Lua state.

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

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.

Opens the Lua basic library on this state.

Opens the Lua math library on this state.

Opens the Lua string library on this state.

Opens the Lua table library on this state.

Opens the Lua io library on this state.

Opens the Lua os library on this state.

Opens the Lua package library on this state.

Opens the Lua debug library on this state.

Opens the Lua bit library on this state.

Opens the LuaJIT JIT library on this state.

Opens the Lua FFI library on this state.

Sets the top of the stack to the valid index idx

Pops a value from the top of the stack

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

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

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

Maps directly to lua_pcall without additional handling.

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

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

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

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

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

Retrieves a string from the Lua stack.

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

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

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

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

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

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

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

See new_userdata for more usage.

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

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

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

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

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.

Copys the value at idx to the top of the stack

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);
}

Push a new nil value onto the Lua stack.

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

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

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

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

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);
}

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.

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

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

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
    T: From<U>, 
[src]

🔬 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> Any for T where
    T: 'static + ?Sized
[src]

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

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

The type returned in the event of a conversion error.

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