Skip to content

Commit

Permalink
chore: Add Janet native module example
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed Apr 20, 2024
1 parent 51164f3 commit 59875d5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,8 @@ members = ["janetrs_macros", "janetrs_version"]
[[example]]
name = "hello_world"
required-features = ["amalgation"]

[[example]]
name = "janet_native_module"
crate-type = ["cdylib", "staticlib"]
required-features = ["amalgation"]
34 changes: 34 additions & 0 deletions examples/janet_native_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! For a more complete example to create a Janet package with jpm, check out [this
//! template repository](https://github.com/GrayJack/rust-janet-module-template)

use janetrs::{declare_janet_mod, janet_fn, jpanic, Janet, JanetTuple, TaggedJanet};

/// (template/hello)
///
/// Rust say hello
#[janet_fn(arity(fix(0)))]
pub fn rust_hello(_args: &mut [Janet]) -> Janet {
println!("Hello from Rust!");
Janet::nil()
}

/// (template/chars)
///
/// If the argument is a buffer or string, return a array or tuple of the chars of the
/// argument, else return nil
#[janet_fn(arity(fix(1)))]
pub fn chars(args: &mut [Janet]) -> Janet {
match args[0].unwrap() {
TaggedJanet::Buffer(b) => b.chars().collect::<JanetTuple>().into(),
TaggedJanet::String(s) => s.chars().collect::<JanetTuple>().into(),
_ => jpanic!(
"bad slot #0, expected string|buffer, got {}",
args[0].kind()
),
}
}

declare_janet_mod!("template";
{"hello", rust_hello},
{"chars", chars},
);

0 comments on commit 59875d5

Please sign in to comment.