Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ members = [
resolver = "3"


[turing.profile.release]
strip = true
lto = true
opt-level = 3

[tests.profile.release]
opt-level = "z"
# 0 = no optimization
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,8 @@ pub struct FfiParam {
```


### Compiling for Windows from Linux
Download the `mingw-64` package and compile using:
```
cargo x w
```
13 changes: 12 additions & 1 deletion turing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ edition = "2024"

[lib]
crate-type = ["cdylib", "staticlib", "rlib"]
name = "turing_rs"

[profile.release]
strip = true
lto = true
opt-level = 3

split-debuginfo = "packed"
debug = "line-tables-only"
panic = "abort"
codegen-units = 1

[features]
default = ["wasm", "lua", "global_ffi"]
Expand All @@ -23,7 +34,7 @@ harness = false


[dependencies]
anyhow = "1.0"
anyhow = {version = "1.0", features = ["backtrace"] }
serde_json = "1.0.149"
glam = "0.30.10"

Expand Down
14 changes: 7 additions & 7 deletions turing/benches/lua_api_bench.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use criterion::{Criterion, criterion_group, criterion_main};
use std::ffi::{c_void, CString};
use std::hint::black_box;
use turing::engine::types::ScriptFnMetadata;
use turing::interop::params::{DataType, FreeableDataType, Param, Params};
use turing::{ExternalFunctions, Turing};
use turing_rs::engine::types::ScriptFnMetadata;
use turing_rs::interop::params::{DataType, FreeableDataType, Param, Params};
use turing_rs::{ExternalFunctions, Turing};

struct DirectExt {}
impl ExternalFunctions for DirectExt {
Expand All @@ -28,14 +28,14 @@ impl ExternalFunctions for DirectExt {
}

extern "C" fn log_info_wasm(
_params: turing::interop::params::FfiParamArray,
) -> turing::interop::params::FfiParam {
_params: turing_rs::interop::params::FfiParamArray,
) -> turing_rs::interop::params::FfiParam {
Param::Void.to_ext_param()
}

extern "C" fn fetch_string(
_params: turing::interop::params::FfiParamArray,
) -> turing::interop::params::FfiParam {
_params: turing_rs::interop::params::FfiParamArray,
) -> turing_rs::interop::params::FfiParam {
Param::String("this is a host provided string!".to_string()).to_ext_param()
}

Expand Down
6 changes: 3 additions & 3 deletions turing/benches/wasm_api_bench.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use criterion::{Criterion, criterion_group, criterion_main};
use turing::engine::types::ScriptFnMetadata;
use turing_rs::engine::types::ScriptFnMetadata;
use std::env;
use std::ffi::{c_void, CString};
use std::fs::File;
use std::hint::black_box;
use std::io::Write;
use turing::interop::params::{DataType, FfiParam, FfiParamArray, FreeableDataType, Param, Params};
use turing::{ExternalFunctions, Turing};
use turing_rs::interop::params::{DataType, FfiParam, FfiParamArray, FreeableDataType, Param, Params};
use turing_rs::{ExternalFunctions, Turing};

struct DirectExt {}
impl ExternalFunctions for DirectExt {
Expand Down
4 changes: 1 addition & 3 deletions turing/src/engine/lua_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,7 @@ impl<Ext: ExternalFunctions> LuaInterpreter<Ext> {
};

// we assume the function exists because we cached it earlier
let Some((name, _)) = &self.func_cache.get(&cache_key) else {
return Param::Error(format!("Function with key '{cache_key:?}' not found"))
};
let (name, _) = &self.func_cache.get(&cache_key);
let name = name.as_str();

let func = module.get::<Value>(name);
Expand Down
21 changes: 9 additions & 12 deletions turing/src/engine/wasm_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ impl DataType {

DataType::F32 => Ok(ValType::F32),
DataType::F64 => Ok(ValType::F64),
DataType::Void => Err(anyhow!("Void is only allowed as a singular return type for WASM.")), // voids are represented as i32 0

_ => Err(anyhow!("Invalid wasm value type: {}", self))
_ => Err(anyhow!("Invalid wasm value type: {}", self)),
}
}

Expand Down Expand Up @@ -591,8 +592,13 @@ impl<Ext: ExternalFunctions + Send + Sync + 'static> WasmInterpreter<Ext> {
name.insert(0, '_');

let p_types = metadata.param_types.iter().map(|d| d.to_val_type()).collect::<Result<Vec<ValType>>>()?;
let r_types = metadata.return_type.iter().map(|d| d.to_val_type()).collect::<Result<Vec<ValType>>>()?;

// if the only return type is void, we treat it as no return types
let r_types = if metadata.return_type.len() == 1 && metadata.return_type.first().cloned() == Some(DataType::Void) {
Vec::new()
} else {
metadata.return_type.iter().map(|d| d.to_val_type()).collect::<Result<Vec<ValType>>>()?
};
let ft = FuncType::new(engine, p_types, r_types);
let cap = metadata.capability.clone();
let callback = metadata.callback;
Expand Down Expand Up @@ -689,18 +695,9 @@ impl<Ext: ExternalFunctions + Send + Sync + 'static> WasmInterpreter<Ext> {
}

// Try cache first to avoid repeated name lookup and Val boxing/unboxing.
let Some((_, f)) = self.func_cache.get(&cache_key)
// This shouldn't be necessary as all exported functions are indexed on load
let (_, f) = self.func_cache.get(&cache_key);

// .or_else(|| {
// let name = d.fn_name_cache.get(cache_key)?;
// let found = instance.get_func(&mut self.store, name)?;
// self.func_cache.insert(cache_key, found);
// Some(found)
// })
else {
return Param::Error("Function does not exist".to_string());
};

let args = params.to_wasm_args(data);
if let Err(e) = args {
Expand Down
2 changes: 1 addition & 1 deletion turing/src/global_ffi/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ unsafe extern "C" fn turing_script_load(turing: *mut TuringInstance, source: *co
};

if let Err(e) = turing.load_script(source, &capabilities) {
Param::Error(format!("{}", e))
Param::Error(format!("{}\n{}", e, e.backtrace()))
} else {
Param::Void
}.to_rs_param()
Expand Down
8 changes: 4 additions & 4 deletions turing/src/key_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ where
}

#[inline]
pub fn get(&self, key: &K) -> Option<&V> {
self.values.get(key.clone().into() as usize)
pub fn get(&self, key: &K) -> &V {
unsafe { self.values.get_unchecked(key.clone().into() as usize) }
}

#[inline]
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.values.get_mut(key.clone().into() as usize)
pub fn get_mut(&mut self, key: &K) -> &mut V {
unsafe { self.values.get_unchecked_mut(key.clone().into() as usize) }
}

/// Clears all values from the KeyVec.
Expand Down
12 changes: 9 additions & 3 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use serde::Deserialize;

#[derive(Deserialize)]
struct CargoToml {
package: Package
package: Package,
lib: Lib,
}

#[derive(Deserialize)]
Expand All @@ -14,6 +15,11 @@ struct Package {
name: String,
}

#[derive(Deserialize)]
struct Lib {
name: String,
}

fn main() {
let mut args = env::args().skip(1);
let task = args.next().unwrap_or_else(|| {
Expand Down Expand Up @@ -70,14 +76,14 @@ fn build_windows() {
.expect("Failed to parse Cargo.toml");

let version = cargo.package.version;
let lib_name = cargo.package.name;
let lib_name = cargo.lib.name;

let built = format!("target/{}/release/{}.dll", target, lib_name);
let output = Path::new("dist").join(format!("{}-{}.dll", lib_name, version));

fs::create_dir_all("dist").expect("Failed to create dist directory");
fs::copy(&built, &output)
.unwrap_or_else(|e| panic!("Failed to copy DLL: {}", e));
.unwrap_or_else(|e| panic!("Failed to copy DLL: {}.dll {}", lib_name, e));

println!("Windows dll generated in dist");

Expand Down
Loading