Skip to content

Commit d7daaad

Browse files
improve readme + remove todo
1 parent 4ae89c8 commit d7daaad

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

examples/wasm-demo-cairo1/README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# Demo of `cairo-vm` on WebAssembly
1+
# Demo of `cairo-vm` on WebAssembly (Cairo 1)
22

33
While cairo-vm is compatible with WebAssembly, it doesn't implement any bindings to it.
4-
Instead, create a new WebAssembly crate with cairo-vm as a dependency and implement the required functionality there.
4+
Instead, create a new WebAssembly crate with cairo-vm and cairo1-run as dependencies and implement the required functionality there.
55

6-
Since mimalloc is not automatically compilable to WebAssembly, the cairo-vm dependency should disable the default features, which will in turn disable mimalloc.
6+
Since mimalloc is not automatically compilable to WebAssembly, the cairo-vm dependency should disable the default features, which will in turn disable mimalloc. Simliar to this, WebAssembly is
7+
not compatible with rust standard library. For this reason, cairo1-run should also disable the default features.
78

89
A working example is provided in this repository.
910

@@ -22,10 +23,18 @@ To compile and run the example you need:
2223

2324
To build the example, first you need to compile your Cairo Program, using either cairo 1 or cairo 2 compiler:
2425

26+
If cairo 1 is used:
27+
2528
```sh
2629
cairo-compile -rs ../../cairo_programs/cairo-1-programs/bitwise.cairo ../../cairo_programs/cairo-1-programs/bitwise.sierra
2730
```
2831

32+
If cairo 2 is used:
33+
34+
```sh
35+
cairo-compile -r ../../cairo_programs/cairo-1-programs/bitwise.cairo ../../cairo_programs/cairo-1-programs/bitwise.sierra
36+
```
37+
2938
> WARNING: This example uses `cairo1-run::cairo_run_program` which expectes the program to be compiled with debug names. By default, `cairo-compile` does not include debug names in sierra. Due to this, the flag `-r` or `--replace-ids` is needed.
3039
3140
compile the WebAssembly package:

examples/wasm-demo-cairo1/src/lib.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ pub fn start() {
1616
crate::utils::set_panic_hook();
1717
}
1818

19-
// TODO: check why this is needed. Seems wasm-bindgen expects us to use
20-
// `std::error::Error` even if it's not yet in `core`
2119
macro_rules! wrap_error {
2220
($xp: expr) => {
23-
$xp.map_err(|e| JsError::new(&format!("Error from CairoRunner: {}", e.to_string())))
21+
$xp.map_err(|e| JsValue::from_str(&format!("Error from CairoRunner: {}", e.to_string())))
2422
};
2523
}
2624

2725
#[wasm_bindgen(js_name = runCairoProgram)]
28-
pub fn run_cairo_program() -> Result<String, JsError> {
26+
pub fn run_cairo_program() -> Result<String, JsValue> {
2927
let cairo_run_config = Cairo1RunConfig {
3028
layout: LayoutName::all_cairo,
3129
relocate_mem: true,
@@ -36,17 +34,15 @@ pub fn run_cairo_program() -> Result<String, JsError> {
3634

3735
let sierra_program = {
3836
let program_str = include_str!("../../../cairo_programs/cairo-1-programs/bitwise.sierra");
39-
ProgramParser::new().parse(program_str)?
37+
wrap_error!(ProgramParser::new().parse(program_str))?
4038
};
4139

42-
let (_, _, serialized_output) = wrap_error!(cairo1_run::cairo_run_program(
43-
&sierra_program,
44-
cairo_run_config
45-
))?;
40+
let (_, _, serialized_output) =
41+
wrap_error!(cairo1_run::cairo_run_program(&sierra_program, cairo_run_config))?;
4642

4743
let output = serialized_output.unwrap();
4844

4945
log(&output);
5046

51-
Ok("output".to_string())
47+
Ok(output)
5248
}

0 commit comments

Comments
 (0)