Skip to content

Commit

Permalink
tuples, graph opt, optional resource name, version bump, all python t…
Browse files Browse the repository at this point in the history
…ests passing
  • Loading branch information
Pedro Arruda committed Jul 30, 2024
1 parent 78c5642 commit f339cc0
Show file tree
Hide file tree
Showing 64 changed files with 1,181 additions and 473 deletions.
44 changes: 3 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ install-dylib: cjyafn

install-so: cjyafn
cp target/release/libcjyafn.so /usr/local/lib/

bump-minor:
cargo set-version --bump minor --package cjyafn jyafn jyafn-python

bump:
cargo set-version --bump patch --package cjyafn --package jyafn --package jyafn-python
5 changes: 1 addition & 4 deletions cjyafn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
[package]
name = "cjyafn"
version = "0.1.2"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
doc = false
name = "cjyafn"
crate-type = ["cdylib"]

[dependencies]
get-size = "0.1.4"
jyafn = { path = "../jyafn", default-features = false }
libloading = "0.8.4"
serde_json = "1.0.115"
smallvec = { version = "1.13.2", features = ["serde"] }

[build-dependencies]
cbindgen = "0.26.0"
58 changes: 50 additions & 8 deletions cjyafn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ pub unsafe extern "C" fn graph_to_json(graph: *const ()) -> *const c_char {
/// Expects `graph` to be a valid pointer to a graph.
#[no_mangle]
pub unsafe extern "C" fn graph_render(graph: *const ()) -> Outcome {
with(graph, |graph: &Graph| new_c_str(graph.render().to_string()))
try_with(graph, |graph: &Graph| {
Ok(new_c_str(graph.render()?.to_string()))
})
}

/// # Safety
Expand Down Expand Up @@ -361,7 +363,7 @@ pub unsafe extern "C" fn layout_from_json(json: *const c_char) -> Outcome {
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_size(layout: *const ()) -> usize {
with_unchecked(layout, |layout: &Layout| layout.size())
with_unchecked(layout, |layout: &Layout| layout.size()).in_bytes()
}

/// # Safety
Expand Down Expand Up @@ -416,6 +418,14 @@ pub unsafe extern "C" fn layout_is_struct(layout: *const ()) -> bool {
})
}

/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_is_tuple(layout: *const ()) -> bool {
with_unchecked(layout, |layout: &Layout| matches!(layout, Layout::Tuple(_)))
}

/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
Expand Down Expand Up @@ -454,6 +464,36 @@ pub unsafe extern "C" fn layout_as_struct(layout: *const ()) -> *const () {
})
}

/// # Safety
///
/// Expects the `strct` parameter to be a valid pointer to a jyafn struct.
#[no_mangle]
pub unsafe extern "C" fn layout_tuple_size(layout: *const ()) -> usize {
with_unchecked(layout, |layout: &Layout| {
if let Layout::Tuple(t) = layout {
return t.len();
}

0
})
}

/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_get_tuple_item(layout: *const (), index: usize) -> *const () {
with_unchecked(layout, |layout: &Layout| {
if let Layout::Tuple(t) = layout {
if index < t.len() {
return &t[index] as *const Layout as *const ();
}
}

std::ptr::null()
})
}

/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
Expand Down Expand Up @@ -565,15 +605,15 @@ pub unsafe extern "C" fn function_name(func: *const ()) -> *const c_char {
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_input_size(func: *const ()) -> usize {
with_unchecked(func, |func: &Function| func.input_size())
with_unchecked(func, |func: &Function| func.input_size()).in_bytes()
}

/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_output_size(func: *const ()) -> usize {
with_unchecked(func, |func: &Function| func.output_size())
with_unchecked(func, |func: &Function| func.output_size()).in_bytes()
}

/// # Safety
Expand Down Expand Up @@ -692,13 +732,15 @@ pub unsafe extern "C" fn function_call_raw(
) -> Outcome {
with_unchecked(func, |func: &Function| {
match std::panic::catch_unwind(|| {
let input = std::slice::from_raw_parts(input, func.input_size());
let output = std::slice::from_raw_parts_mut(output, func.output_size());
let input = std::slice::from_raw_parts(input, func.input_size().in_bytes());
let output = std::slice::from_raw_parts_mut(output, func.output_size().in_bytes());

let fn_err = func.call_raw(input, output);
if !fn_err.is_null() {
let fn_err = Box::from_raw(fn_err).take();
return Outcome::from_result(Result::<(), Error>::Err(fn_err.into()));
return Outcome::from_result(Result::<(), Error>::Err(rust::Error::StatusRaised(
fn_err,
)));
}

Outcome::from_result(Result::<(), Error>::Ok(()))
Expand All @@ -720,7 +762,7 @@ pub unsafe extern "C" fn function_call_raw(
#[no_mangle]
pub unsafe extern "C" fn function_eval_raw(func: *const (), input: *const u8) -> Outcome {
with(func, |func: &Function| {
let input = std::slice::from_raw_parts(input, func.input_size());
let input = std::slice::from_raw_parts(input, func.input_size().in_bytes());
Outcome::from_result(
func.eval_raw(input)
.map(|output| Box::leak(output) as *const [u8] as *const ()),
Expand Down
5 changes: 4 additions & 1 deletion jyafn-ext/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
build:
./build-all.sh
./run-all.sh build

install:
./run-all.sh install
16 changes: 16 additions & 0 deletions jyafn-ext/extensions/dummy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,19 @@ impl Resource for Dummy {
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_load() {
unsafe {
let ptr = extension_init() as *mut i8;
if ptr.is_null() {
panic!("prt was null");
}
println!("{:?}", CString::from_raw(ptr));
}
}
}
Loading

0 comments on commit f339cc0

Please sign in to comment.