Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rust & Emscriptenの話

Yosuke Onoue
November 21, 2015

Rust & Emscriptenの話

Yosuke Onoue

November 21, 2015
Tweet

More Decks by Yosuke Onoue

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ͓ͷ͏͑ ( @_likr ) • ՄࢹԽͷݚڀͱ͔΍ͬͯΔ • ng-kyotoͱGDG

    KobeͰελοϑ΍ͬͯΔ • D3.jsɺAngularJSɺReactͱ͔΍ͬͯΔ
  2. Emscripten • http://emscripten.org/ • LLVM to JavaScript Compiler • C/C++ϓϩάϥϜΛJavaScriptʹม׵

    • ࠷దԽΛ༗ޮԽ͢Δͱasm.jsΛग़ྗ • কདྷతʹ͸WebAssemblyʹ΋ରԠ
  3. rust-webplatform • https://github.com/tcr/rust-webplatform • DOM APIͷബ͍ϥούʔ • ηοτΞοϓεΫϦϓτ෇͖ • cargo-build

    (https://github.com/AerialX/cargo-build) • rust-rt-minimal (https://github.com/AerialX/rust)
  4. Hello World 1 extern crate webplatform; 2 3 fn main()

    { 4 let document = webplatform::init(); 5 6 let p = document.element_create("p").unwrap(); 7 p.html_append("hello world"); 8 9 let body = document.element_query("body").unwrap(); 10 body.append(&p); 11 12 webplatform::spin(); 13 }
  5. ࡞ͬͨ 29 pub fn v(tag: &str, attributes: Vec<Attribute>, children: Vec<Node>)

    -> Node { 30 Node::Element(Element { 31 tag: String::from_str(tag), 32 attributes: attributes, 33 children: children 34 }) 35 } ཁૉ໊ɺଐੑɺࢠComponentΛड͚औͬͯComponentΛੜ੒͢Δؔ਺ ComponentΛड͚औͬͯHTMLͷॻ͖ग़͠Λߦ͏ؔ਺ 9 pub fn mount<'a, T: 'a + Component>( 10 document: Rc<RefCell<Document<'a>>>, 11 root: Rc<RefCell<HtmlNode<'a>>>, 12 component: Rc<RefCell<T>>) { 13 let target = root.borrow(); 14 target.html_set(""); 15 let node = component.borrow().render(); 16 render(document, target.deref(), node, root.clone(), component); 17 }
  6. ࠶Hello World 8 struct Main; 9 10 impl Component for

    Main { 11 fn render(&self) -> Node { 12 v("div", 13 vec![attribute("class", text_value("hoge"))], 14 vec![ 15 v("p", vec![], vec![ 16 text_node("hello"), 17 ]), 18 v("p", vec![], vec![ 19 text_node("world"), 20 ]) 21 ] 22 ) 23 } 24 } 25 26 fn main() { 27 let main_component = Rc::new(RefCell::new(Main)); 28 let document = Rc::new(RefCell::new(webplatform::init())); 29 let body = Rc::new(RefCell::new(document.borrow().element_query("body").unwrap())); 30 vdom::mount(document.clone(), body.clone(), main_component.clone()); 31 webplatform::spin(); 32 } <div class="hoge"> <p>hello</p> <p>world</p> </div>
  7. Data Binding 10 struct Main { 11 state: Rc<RefCell<HashMap<String, String>>>

    12 } 13 14 impl Component for Main { 15 fn render(&self) -> Node { 16 let key = String::from_str("message"); 17 let message = self.state.borrow().get(&key).unwrap().clone(); 18 v("div", 19 vec![attribute("class", text_value("hoge"))], 20 vec![ 21 v("p", vec![], vec![ 22 Node::Text(message.clone()), 23 ]), 24 v("input", vec![ 25 attribute("value", AttributeValue::Text(message.clone())), 26 attribute("change", AttributeValue::EventHandler(Box::new({ 27 let state = self.state.clone(); 28 move |e: &mut Event| { 29 let key = String::from_str("message"); 30 let mut st = state.borrow_mut(); 31 let node = e.target.as_mut().unwrap(); 32 st.insert(key, node.prop_get_str("value")); 33 } 34 }))), 35 ], vec![]) 36 ] 37 ) 38 } 39 } onchangeͷΠϕϯτϋϯυϥͰstateΛߋ৽ stateͷ಺༰Λදࣔ