summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Orlenko <zxteam@protonmail.com>2024-11-09 12:44:00 +0000
committerAlex Orlenko <zxteam@protonmail.com>2024-11-09 12:44:00 +0000
commita3cd25db7ad41fc6862848171c8a774cb728a373 (patch)
tree2c6355b871de2ee44b9c8543b1d93c93ba6a171f
parenta4bfeb7752430b3ae3ccaedec2507257e6a15d76 (diff)
downloadmlua-a3cd25db7ad41fc6862848171c8a774cb728a373.zip
Support Luau 0.650 native vector library
-rw-r--r--src/luau/mod.rs18
-rw-r--r--src/state/raw.rs23
-rw-r--r--src/stdlib.rs7
-rw-r--r--tests/luau.rs24
-rw-r--r--tests/serde.rs18
5 files changed, 39 insertions, 51 deletions
diff --git a/src/luau/mod.rs b/src/luau/mod.rs
index 75d1a76..b3935d3 100644
--- a/src/luau/mod.rs
+++ b/src/luau/mod.rs
@@ -1,5 +1,5 @@
use std::ffi::CStr;
-use std::os::raw::{c_float, c_int};
+use std::os::raw::c_int;
use crate::error::Result;
use crate::state::Lua;
@@ -11,7 +11,6 @@ impl Lua {
let globals = self.globals();
globals.raw_set("collectgarbage", self.create_c_function(lua_collectgarbage)?)?;
- globals.raw_set("vector", self.create_c_function(lua_vector)?)?;
// Set `_VERSION` global to include version number
// The environment variable `LUAU_VERSION` set by the build script
@@ -65,21 +64,6 @@ unsafe extern "C-unwind" fn lua_collectgarbage(state: *mut ffi::lua_State) -> c_
}
}
-// Luau vector datatype constructor
-unsafe extern "C-unwind" fn lua_vector(state: *mut ffi::lua_State) -> c_int {
- let x = ffi::luaL_checknumber(state, 1) as c_float;
- let y = ffi::luaL_checknumber(state, 2) as c_float;
- let z = ffi::luaL_checknumber(state, 3) as c_float;
- #[cfg(feature = "luau-vector4")]
- let w = ffi::luaL_checknumber(state, 4) as c_float;
-
- #[cfg(not(feature = "luau-vector4"))]
- ffi::lua_pushvector(state, x, y, z);
- #[cfg(feature = "luau-vector4")]
- ffi::lua_pushvector(state, x, y, z, w);
- 1
-}
-
pub(crate) use package::register_package_module;
mod package;
diff --git a/src/state/raw.rs b/src/state/raw.rs
index f838990..53aca04 100644
--- a/src/state/raw.rs
+++ b/src/state/raw.rs
@@ -1347,6 +1347,12 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()>
ffi::lua_pop(state, 1);
}
+ #[cfg(feature = "luau")]
+ if libs.contains(StdLib::VECTOR) {
+ requiref(state, ffi::LUA_VECLIBNAME, ffi::luaopen_vector, 1)?;
+ ffi::lua_pop(state, 1);
+ }
+
if libs.contains(StdLib::MATH) {
requiref(state, ffi::LUA_MATHLIBNAME, ffi::luaopen_math, 1)?;
ffi::lua_pop(state, 1);
@@ -1369,16 +1375,15 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()>
}
#[cfg(feature = "luajit")]
- {
- if libs.contains(StdLib::JIT) {
- requiref(state, ffi::LUA_JITLIBNAME, ffi::luaopen_jit, 1)?;
- ffi::lua_pop(state, 1);
- }
+ if libs.contains(StdLib::JIT) {
+ requiref(state, ffi::LUA_JITLIBNAME, ffi::luaopen_jit, 1)?;
+ ffi::lua_pop(state, 1);
+ }
- if libs.contains(StdLib::FFI) {
- requiref(state, ffi::LUA_FFILIBNAME, ffi::luaopen_ffi, 1)?;
- ffi::lua_pop(state, 1);
- }
+ #[cfg(feature = "luajit")]
+ if libs.contains(StdLib::FFI) {
+ requiref(state, ffi::LUA_FFILIBNAME, ffi::luaopen_ffi, 1)?;
+ ffi::lua_pop(state, 1);
}
Ok(())
diff --git a/src/stdlib.rs b/src/stdlib.rs
index c71d149..b05e056 100644
--- a/src/stdlib.rs
+++ b/src/stdlib.rs
@@ -48,12 +48,17 @@ impl StdLib {
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
pub const BUFFER: StdLib = StdLib(1 << 9);
+ /// [`vector`](https://luau-lang.org/library#vector-library) library
+ #[cfg(any(feature = "luau", doc))]
+ #[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
+ pub const VECTOR: StdLib = StdLib(1 << 10);
+
/// [`jit`](http://luajit.org/ext_jit.html) library
///
/// Requires `feature = "luajit"`
#[cfg(any(feature = "luajit", doc))]
#[cfg_attr(docsrs, doc(cfg(feature = "luajit")))]
- pub const JIT: StdLib = StdLib(1 << 9);
+ pub const JIT: StdLib = StdLib(1 << 11);
/// (**unsafe**) [`ffi`](http://luajit.org/ext_ffi.html) library
///
diff --git a/tests/luau.rs b/tests/luau.rs
index 0b37f7d..b45404c 100644
--- a/tests/luau.rs
+++ b/tests/luau.rs
@@ -97,17 +97,19 @@ fn test_require() -> Result<()> {
fn test_vectors() -> Result<()> {
let lua = Lua::new();
- let v: Vector = lua.load("vector(1, 2, 3) + vector(3, 2, 1)").eval()?;
+ let v: Vector = lua
+ .load("vector.create(1, 2, 3) + vector.create(3, 2, 1)")
+ .eval()?;
assert_eq!(v, [4.0, 4.0, 4.0]);
// Test conversion into Rust array
- let v: [f64; 3] = lua.load("vector(1, 2, 3)").eval()?;
+ let v: [f64; 3] = lua.load("vector.create(1, 2, 3)").eval()?;
assert!(v == [1.0, 2.0, 3.0]);
// Test vector methods
lua.load(
r#"
- local v = vector(1, 2, 3)
+ local v = vector.create(1, 2, 3)
assert(v.x == 1)
assert(v.y == 2)
assert(v.z == 3)
@@ -118,7 +120,7 @@ fn test_vectors() -> Result<()> {
// Test vector methods (fastcall)
lua.load(
r#"
- local v = vector(1, 2, 3)
+ local v = vector.create(1, 2, 3)
assert(v.x == 1)
assert(v.y == 2)
assert(v.z == 3)
@@ -135,17 +137,19 @@ fn test_vectors() -> Result<()> {
fn test_vectors() -> Result<()> {
let lua = Lua::new();
- let v: Vector = lua.load("vector(1, 2, 3, 4) + vector(4, 3, 2, 1)").eval()?;
+ let v: Vector = lua
+ .load("vector.create(1, 2, 3, 4) + vector.create(4, 3, 2, 1)")
+ .eval()?;
assert_eq!(v, [5.0, 5.0, 5.0, 5.0]);
// Test conversion into Rust array
- let v: [f64; 4] = lua.load("vector(1, 2, 3, 4)").eval()?;
+ let v: [f64; 4] = lua.load("vector.create(1, 2, 3, 4)").eval()?;
assert!(v == [1.0, 2.0, 3.0, 4.0]);
// Test vector methods
lua.load(
r#"
- local v = vector(1, 2, 3, 4)
+ local v = vector.create(1, 2, 3, 4)
assert(v.x == 1)
assert(v.y == 2)
assert(v.z == 3)
@@ -157,7 +161,7 @@ fn test_vectors() -> Result<()> {
// Test vector methods (fastcall)
lua.load(
r#"
- local v = vector(1, 2, 3, 4)
+ local v = vector.create(1, 2, 3, 4)
assert(v.x == 1)
assert(v.y == 2)
assert(v.z == 3)
@@ -180,10 +184,10 @@ fn test_vector_metatable() -> Result<()> {
r#"
{
__index = {
- new = vector,
+ new = vector.create,
product = function(a, b)
- return vector(a.x * b.x, a.y * b.y, a.z * b.z)
+ return vector.create(a.x * b.x, a.y * b.y, a.z * b.z)
end
}
}
diff --git a/tests/serde.rs b/tests/serde.rs
index cafbe70..4efb653 100644
--- a/tests/serde.rs
+++ b/tests/serde.rs
@@ -133,13 +133,7 @@ fn test_serialize_failure() -> Result<(), Box<dyn StdError>> {
fn test_serialize_vector() -> Result<(), Box<dyn StdError>> {
let lua = Lua::new();
- let globals = lua.globals();
- globals.set(
- "vector",
- lua.create_function(|_, (x, y, z)| Ok(mlua::Vector::new(x, y, z)))?,
- )?;
-
- let val = lua.load("{_vector = vector(1, 2, 3)}").eval::<Value>()?;
+ let val = lua.load("{_vector = vector.create(1, 2, 3)}").eval::<Value>()?;
let json = serde_json::json!({
"_vector": [1.0, 2.0, 3.0],
});
@@ -156,13 +150,9 @@ fn test_serialize_vector() -> Result<(), Box<dyn StdError>> {
fn test_serialize_vector() -> Result<(), Box<dyn StdError>> {
let lua = Lua::new();
- let globals = lua.globals();
- globals.set(
- "vector",
- lua.create_function(|_, (x, y, z, w)| Ok(mlua::Vector::new(x, y, z, w)))?,
- )?;
-
- let val = lua.load("{_vector = vector(1, 2, 3, 4)}").eval::<Value>()?;
+ let val = lua
+ .load("{_vector = vector.create(1, 2, 3, 4)}")
+ .eval::<Value>()?;
let json = serde_json::json!({
"_vector": [1.0, 2.0, 3.0, 4.0],
});