Coder Social home page Coder Social logo

lua-rs's Introduction

Lua rust

Build Status

(WIP) Rust wrapper around the Lua C API.

Documentation

Examples

# Run an interactive lua shell ([FILE] is optional)
$ cargo run --example lua [FILE]

Hello World

extern crate lua;

let mut state = lua::State::new();
state.open_libs();

state.eval("print ('hello world')").unwrap();

Functions

Types that implement the Function trait can be used as lua functions:

extern crate lua;

use lua::{Function, Index};

// A Type for a function that returns the length of a string
enum StringLength {}

impl Function for StringLength {
    type Error = lua::Error;

    fn call(state: &mut lua::State) -> Result<usize, Self::Error> {
        let length = state.get(Index::Bottom(1)).map(|s: &str| s.len())?;
        state.push(length)?;
        Ok(1)
    }
}

let mut state = lua::State::new();

state.push_function::<StringLength>().unwrap();
state.set_global("length");

state.eval("len = length('hello world')").unwrap(); // len = 11

Custom Userdata

Types that implements the UserData trait can be used as userdata. When a type is moved into the stack, the State becomes its owner and will eventually be dropped by the garbage collector.

For a more complete example, including setting up metamethods, see this example.

extern crate lua;

use lua::{UserData, Index};

#[derive(Debug)]
struct Foo {
    bar: Vec<i32>,
    baz: String,
}

impl UserData for Foo {
    // An identifier, unique to the Type
    const METATABLE: &'static str = "Example.foo";
}

let mut state = lua::State::new();

state.push_udata(Foo {
    bar: vec![0; 16],
    baz: String::from("Hello world!"),
}).unwrap();

// Get a reference to the stack
let foo: &Foo = state.get_udata(Index::TOP).unwrap();

// To get a mutable reference, use this instead:
// let foomut: &mut Foo = state.get_udata_mut(Index::TOP).unwrap();

lua-rs's People

Contributors

germangb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.