{"id":2299,"date":"2020-11-18T22:06:38","date_gmt":"2020-11-18T13:06:38","guid":{"rendered":"https:\/\/mattyan.net\/blog\/?p=2299"},"modified":"2020-11-18T22:06:38","modified_gmt":"2020-11-18T13:06:38","slug":"rust%e3%81%a7wasm%e3%82%92%e5%ae%9f%e8%a1%8c%e3%81%97%e3%81%a6%e3%81%bf%e3%81%9f","status":"publish","type":"post","link":"https:\/\/mattyan.net\/blog\/?p=2299","title":{"rendered":"rust\u3067wasm\u3092\u5b9f\u884c\u3057\u3066\u307f\u305f"},"content":{"rendered":"<p>rust\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u30d7\u30e9\u30b0\u30a4\u30f3\u6a5f\u69cb\u3092\u7d44\u307f\u8fbc\u3080\u306b\u306f\u3069\u3046\u3057\u305f\u3089\u826f\u3044\u304b\u8abf\u3079\u3066\u3044\u305f\u3068\u3053\u308d\u3001rust\u3067wasm\u3092\u5b9f\u884c\u3059\u308b\u65b9\u6cd5\u304c\u51fa\u3066\u304d\u305f\u306e\u3067\u8a66\u3057\u306b\u5b9f\u88c5\u3057\u3066\u307f\u305f\u3002<br \/>\n<!--more--><\/p>\n<h2>\u958b\u767a\u74b0\u5883\u69cb\u7bc9<\/h2>\n<p>wasm\u3092\u30d3\u30eb\u30c9\u3059\u308b\u305f\u3081\u306b\u3001Scoop\u3067llvm\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n<pre>scoop install llvm<\/pre>\n<p>clang\u3068\u4e00\u7dd2\u306bwasm\u7528\u30ea\u30f3\u30ab\u3068\u304b\u8272\u3005\u4e00\u7dd2\u306b\u5165\u3063\u3066\u304f\u308b<\/p>\n<h2>wasm\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u958b\u767a<\/h2>\n<p>llvm\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u7d42\u308f\u3063\u305f\u3089c\u3067\u30d7\u30ed\u30b0\u30e9\u30e0\u4f5c\u6210<\/p>\n<pre><code class=\"language-c\" data-line=\"\">#include &lt;stdint.h&gt;\n__attribute__((visibility(&quot;default&quot;))) \/\/ clang\u306b\u95a2\u6570\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u3088\u3046\u306b\u6307\u5b9a\nint add(int32_t a, int32_t b) {\n    return a + b;\n}\n<\/code><\/pre>\n<p>int\u578b2\u3064\u53d7\u3051\u53d6\u3063\u3066\u8db3\u3057\u7b97\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3059\u3060\u3051\u306e\u95a2\u6570\u3002<br \/>\n\u30d3\u30eb\u30c9\u3059\u308b\u305f\u3081\u306eMakefile\u3082<\/p>\n<pre><code class=\"language-makefile\" data-line=\"\">all: wasm.wasm\nwasm.wasm: wasm.o\n\tclang -Wl,--no-entry -Wl,--export-dynamic -nostdlib --target=wasm32 -o wasm.wasm wasm.o\nwasm.o: wasm.c\n\tclang --target=wasm32 -c wasm.c -o wasm.o\n<\/code><\/pre>\n<p>\u3042\u3068\u306fmake\u3067wasm.wasm\u3092\u4f5c\u6210\u3057\u3066wasm\u30d5\u30a1\u30a4\u30eb\u306e\u6e96\u5099\u306f\u5b8c\u4e86\u3002<\/p>\n<h2>rust\u5074\u306e\u958b\u767a<\/h2>\n<p>\u5b9f\u969b\u306bwasm\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u3093\u3067\u5b9f\u884c\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u958b\u767a\u3002\u307e\u305a\u306fcargo\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u6e96\u5099<\/p>\n<pre>cargo new --bin wasm_rt<\/pre>\n<p>\u6b21\u306bCargo.toml\u306e\u6e96\u5099\u3002<\/p>\n<pre><code class=\"\" data-line=\"\">[package]\nname = &quot;wasm_rt&quot;\nversion = &quot;0.1.0&quot;\nauthors = [&quot;mattyan &lt;mattyan.mail@gmail.com&gt;&quot;]\nedition = &quot;2018&quot;\n# See more keys and their definitions at https:\/\/doc.rust-lang.org\/cargo\/reference\/manifest.html\n[dependencies]\nwasmer = &quot;1.0.0-alpha&quot;\n<\/code><\/pre>\n<p>\u6700\u521d\u306f<a href=\"https:\/\/crates.io\/crates\/wasmer\/\">wasmer 0.16<\/a>\u3092\u4f7f\u3063\u3066\u305f\u3093\u3060\u3051\u3069\u3001\u3069\u3046\u3057\u3066\u3082\u30d3\u30eb\u30c9\u304c\u3067\u304d\u306a\u304b\u3063\u305f\u306e\u3067\u3001<a href=\"https:\/\/crates.io\/crates\/wasmer\/1.0.0-alpha5\">1.0.0-alpha5<\/a>\u306b\u5909\u66f4\u3002<\/p>\n<pre><code class=\"language-rust\" data-line=\"\">extern crate wasmer;\nuse wasmer::{Store, Module, Instance, imports, Value};\nuse std::fs::File;\nuse std::io::Read;\nfn main() {\n    \/\/ wasm\u30d5\u30a1\u30a4\u30eb\u8aad\u307f\u8fbc\u307f\n    let mut wasm_file = File::open(&quot;.\/wasm.wasm&quot;).unwrap();\n    let mut wasm_file_buf = Vec::new();\n    let _ = wasm_file.read_to_end(&amp;mut wasm_file_buf).unwrap();\n    \/\/ wasm\u30d5\u30a1\u30a4\u30eb\u5b9f\u884c\u6e96\u5099\n    let store = Store::default();\n    let module = Module::new(&amp;store, &amp;wasm_file_buf).unwrap();\n    let import_object = imports! {};\n    let instance = Instance::new(&amp;module, &amp;import_object).unwrap();\n    let add = instance.exports.get_function(&quot;add&quot;).unwrap();\n    \/\/ wasm\u306e\u95a2\u6570\u5b9f\u884c\n    let response = add.call(&amp;[Value::I32(1), Value::I32(2)]).unwrap();\n    println!(&quot;1 + 2 = {}&quot;, response[0].to_string().parse::&lt;i32&gt;().unwrap());\n}\n<\/code><\/pre>\n<p>wasm.wasm\u3092\u8aad\u307f\u8fbc\u3093\u3067add\u95a2\u6570\u3092\u53d6\u5f97\u3002<br \/>\n\u305d\u308c\u3092\u547c\u3073\u51fa\u3057\u3066\u7d50\u679c\u3092\u53d6\u5f97\u3057\u3066\u3044\u308b\u3002<\/p>\n<pre>\nPS wasm_rt> cargo run\n   Compiling wasm_rt v0.1.0 (wasm_rt)\n    Finished dev [unoptimized + debuginfo] target(s) in 5.60s\n     Running `target\\debug\\wasm_rt.exe`\n1 + 2 = 3\n<\/pre>\n<p>cargo run\u3067\u30d3\u30eb\u30c9\u3057\u3066cargo run\u3057\u305f\u3089\u3001\u7121\u4e8b\u306b\u5b9f\u884c\u3067\u304d\u305f\u3002<br \/>\n\u304a\u307e\u3051: \u4eca\u56de\u304b\u3089\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30b7\u30f3\u30bf\u30c3\u30af\u30b9\u30cf\u30a4\u30e9\u30a4\u30c8\u306b APH Prism Syntax Highlighter\u3092\u5c0e\u5165\u3057\u3066\u307f\u305f\u3002<br \/>\n\u5c11\u3057\u66f8\u304f\u306e\u304c\u9762\u5012\u3060\u3051\u3069\u3001\u5bfe\u5fdc\u8a00\u8a9e\u304c\u591a\u3044\u304b\u3089\u3053\u3063\u3061\u512a\u5148\u306b\u3057\u3088\u3046\u3068\u601d\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>rust\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u30d7\u30e9\u30b0\u30a4\u30f3\u6a5f\u69cb\u3092\u7d44\u307f\u8fbc\u3080\u306b\u306f\u3069\u3046\u3057\u305f\u3089\u826f\u3044\u304b\u8abf\u3079\u3066\u3044\u305f\u3068\u3053\u308d\u3001rust\u3067wasm\u3092\u5b9f\u884c\u3059\u308b\u65b9\u6cd5\u304c\u51fa\u3066\u304d\u305f\u306e\u3067\u8a66\u3057\u306b\u5b9f\u88c5\u3057\u3066\u307f\u305f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2299","post","type-post","status-publish","format-standard","hentry","category-3"],"_links":{"self":[{"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2299"}],"version-history":[{"count":0,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2299\/revisions"}],"wp:attachment":[{"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}