| extern crate libc; | |
| extern "C" { | |
| fn emscripten_asm_const_int(script: *const libc::c_char, ...) -> libc::c_int; | |
| } | |
| macro_rules! c_str { | |
| ($s:expr) => { | |
| concat!($s, "\0").as_ptr() as *const _ | |
| } | |
| } | |
| macro_rules! js { | |
| (($($name:ident = $expr:expr),*) {$($tt:tt)*}) => { | |
| unsafe { emscripten_asm_const_int(c_str!(concat!( | |
| $( | |
| "var ", stringify!($name), " = $0; ", | |
| )* | |
| stringify!($($tt)*))), $($expr),*) } | |
| }; | |
| } | |
| macro_rules! js_raw { | |
| (($($name:ident = $expr:expr),*) {$code:expr}) => { | |
| unsafe { emscripten_asm_const_int(c_str!(concat!( | |
| $( | |
| "var ", stringify!($name), " = $0; ", | |
| )* | |
| $code)), $($expr),*) } | |
| }; | |
| } | |
| fn main() { | |
| println!("Hello world 2"); | |
| println!("test: {}", js!{(){ | |
| var testing = 55; | |
| function test(i) { return i; } | |
| return test(testing) | |
| }}); | |
| println!("test2: {}", js!{(arg0 = 78){ | |
| var testing = 55; | |
| return testing * arg0; | |
| }}); | |
| let test = "hello".as_ptr(); | |
| js!{(v = test){ | |
| console.log(Pointer_stringify(v, 5)); | |
| }}; | |
| js_raw!{(){r#"console.log('Hello world 2');"#}}; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment