Skip to content

Instantly share code, notes, and snippets.

@Thinkofname
Created September 29, 2016 13:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thinkofname/17e8369b5fcd1d307f5d98dfda9266a6 to your computer and use it in GitHub Desktop.
Save Thinkofname/17e8369b5fcd1d307f5d98dfda9266a6 to your computer and use it in GitHub Desktop.
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