Skip to content

Commit

Permalink
Add sample
Browse files Browse the repository at this point in the history
  • Loading branch information
udzura committed Jan 22, 2024
1 parent 4b31180 commit 1adbde9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target
*.wasm
*.mrb
27 changes: 17 additions & 10 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ resolver = "2"

members = [
"mruby-objdump",
"mrubyedge",
"mrubyedge", "wasmsample",
]
14 changes: 14 additions & 0 deletions wasmsample/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "wasmsample"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies.mrubyedge]
path = "../mrubyedge"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
31 changes: 31 additions & 0 deletions wasmsample/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
extern crate mrubyedge;

use mrubyedge::{mrb_helper, vm::RObject};

const DATA: &'static [u8] = include_bytes!("./hello.mrb");
const FNNAME: &'static str = "hello_mruby_from_wasm";

#[no_mangle]
pub fn hello_mruby_from_wasm() {
let rite = mrubyedge::rite::load(DATA).unwrap();
let mut vm = mrubyedge::vm::VM::open(rite);
vm.prelude().unwrap();
vm.eval_insn().unwrap();

let objclass_sym = vm.target_class.unwrap() as usize;
let top_self = RObject::RInstance {
class_index: objclass_sym,
};
let args = vec![];

match mrb_helper::mrb_funcall(&mut vm, &top_self, FNNAME.to_string(), &args) {
Ok(retval) => {
eprintln!("{:?}", retval);
}
Err(ex) => {
dbg!(ex);
}
};

()
}

0 comments on commit 1adbde9

Please sign in to comment.