-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add Janet native module example
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}, | ||
); |