Skip to content

atl::value

atl::Value: a handle to a Lua value, and the conversions between Rust and Lua types.

pub struct Value

A handle to any Lua value: tables, functions, game objects, strings... The handle is freed when the Value is dropped; clones get their own handle.

Methods

pub const fn nil() -> Value

Nil.

pub fn adopt(h: Handle) -> Value

Takes ownership of a raw handle.

pub fn borrow(h: Handle) -> Value

Wraps a raw handle without taking ownership; it isn't freed.

pub fn release(mut self) -> Handle

Gives up ownership and returns a raw handle the caller must free.

pub fn global(path: &str) -> Value

Looks up a dotted global path, e.g. Value::global("sm.vec3.new").

pub fn table() -> Value

A new empty table.

pub fn from<T: IntoLua + ?Sized>(v: &T) -> Value

Converts a Rust value to Lua.

pub fn handle(&self) -> Handle

The raw handle.

pub fn is_nil(&self) -> bool

True for nil.

pub fn kind(&self) -> Type

The Lua type.

pub fn is(&self, t: Type) -> bool

True when the type is t.

pub fn truthy(&self) -> bool

Lua truthiness: false only for nil and false.

pub fn to_number(&self) -> f64

The value as a number (0 if it isn't one).

pub fn to_lua_string(&self) -> String

Lua's tostring() of the value.

pub fn to_bytes(&self) -> Vec<u8>

The bytes of a Lua string (or of tostring() for anything else).

pub fn to<T: FromLua>(&self) -> T

Converts the value to T.

pub fn field<K: Key>(&self, key: K) -> Value

t[key], where the key is a field name, a 1-based index or a Value.

pub fn get<T: FromLua, K: Key>(&self, key: K) -> T

t[key] converted to T.

pub fn number(&self, field: &str) -> f64

t[field] as a number, without creating a handle.

pub fn set<K: Key, T: IntoLua + ?Sized>(&self, key: K, v: &T) -> &Self

t[key] = v. Returns self for chaining.

pub fn length(&self) -> i32

The length operator #t.

pub fn append<T: IntoLua + ?Sized>(&self, v: &T) -> &Self

Appends v at t[#t + 1].

pub fn pairs(&self) -> Pairs<'_>

Iterates the table's key/value pairs with Lua's next().

pub fn call<R: FromLua>(&self, args: impl Args) -> R

Calls the value as a function and returns its first result as R. A Lua error traps the mod.

pub fn method<R: FromLua>(&self, name: &str, args: impl Args) -> R

Calls value:name(args...) and returns its first result as R. A Lua error traps the mod.

pub fn try_call(&self, args: impl Args) -> Option<Vec<Value>>

Calls the value as a function and returns every result, or None if it raised an error, so the mod can handle the failure itself.

pub fn try_method(&self, name: &str, args: impl Args) -> Option<Vec<Value>>

Calls value:name(args...) and returns every result, or None on a Lua error.

pub fn method_all(&self, name: &str, args: impl Args) -> Vec<Value>

Calls value:name(args...) and returns every result.

pub fn call_all(&self, args: impl Args) -> Vec<Value>

Calls the value as a function and returns every result.

Implements Clone, Codec, Debug, Default, Display, Drop, FromLua, IntoLua, Key, PartialEq

pub struct Pairs<'a>

Iterator over a table with Lua's next(); used by Value::pairs().

Implements Iterator

pub struct Nil

Converts to Lua nil; pass Nil as an argument.

Implements Clone, Copy, Debug, Default, Eq, IntoLua, PartialEq

pub struct Global<T>(UnsafeCell<T>)

A static holding mutable state. WASM mods are single-threaded, so this is a plain cell.

Methods

pub const fn new(v: T) -> Self

Implements Sync

pub struct GlobalCell

A Lua global looked up once and cached (global!).

Methods

pub const fn new() -> Self
pub fn get(&'static self, path: &str) -> &'static Value

The global, looked up on first use.

Implements Default, Sync

pub enum Type

The Lua type of a Value. Game objects (Shape, Vec3, ...) are Userdata.

Variants

  • Nil = 0
  • Boolean = 1
  • Number = 2
  • String = 3
  • Table = 4
  • Function = 5
  • Userdata = 6
  • Thread = 7

Implements Clone, Copy, Debug, Eq, PartialEq

pub enum Arg<'a>

One argument of a pending call, converted without creating a handle where possible (numbers, booleans and strings go straight to the host).

Variants

  • Nil
  • Number(f64)
  • Bool(bool)
  • Str(&'a [u8])
  • Handle(Handle): A handle owned by someone else.
  • Owned(Value): A handle created for this call, freed with the Arg.

Methods

pub fn push(&self)

Appends this argument to the host's argument buffer.

pub fn into_value(self) -> Value

Turns the argument into a standalone Lua value.

pub trait Key

A table key: a string field or a 1-based integer index.

pub trait IntoLua

Converts a Rust value to a Lua argument. Implement it to pass your own types to Lua.

Trait items

fn to_arg(&self) -> Arg<'_>

The value as an argument; borrowed strings and handles stay borrowed.

pub trait FromLua: Sized

Converts a Lua value to a Rust value. Implement it to read your own types from Lua.

Trait items

fn from_lua(v: &Value) -> Self

Reads the value.

pub trait Args

The arguments of a call: a tuple of IntoLua values, () for none.

Trait items

type Prepared<'a>: AsRef<[Arg<'a>]> where Self: 'a

The prepared arguments.

fn prepare(&self) -> Self::Prepared<'_>

Converts every argument before the call starts (a conversion may itself call Lua).

pub trait Object: Sized

Base trait for game object wrappers (Shape, Body, Player, ...). Holds the Lua object and calls its methods.

Trait items

const LUA_TYPE: &'static str

The Lua type name type() reports for this object ("Shape", ...).

fn value(&self) -> &Value

The wrapped Lua value.

fn from_value(v: Value) -> Self

Wraps a Lua value.

fn is_nil(&self) -> bool

True for nil.

fn exists(&self) -> bool

True when the object still exists in the game (sm.exists).

fn method<R: FromLua>(&self, name: &str, args: impl Args) -> R

Calls the Lua method name on the object.

pub fn lua_error(context: &str) -> !

Traps the mod with context and the host's last error message.

pub fn invoke<A: Args + ?Sized>(f: Handle, args: &A) -> i32

Calls f(args...) on the host; returns the result count, or -1 on a Lua error.

pub fn invoke_method<A: Args + ?Sized>(obj: Handle, name: &str, args: &A) -> i32

Calls obj:name(args...) on the host; returns the result count, or -1 on a Lua error.