{"id":1952,"date":"2017-09-07T15:19:47","date_gmt":"2017-09-07T07:19:47","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1952"},"modified":"2017-09-07T15:19:47","modified_gmt":"2017-09-07T07:19:47","slug":"emscripten-%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1952","title":{"rendered":"Emscripten \u5165\u95e8\u6559\u7a0b"},"content":{"rendered":"<p>\n\tWeb \u6280\u672f\u7a81\u98de\u731b\u8fdb\uff0c\u4f46\u662f\u6709\u4e00\u4e2a\u9886\u57df\u4e00\u76f4\u65e0\u6cd5\u7a81\u7834 &#8212;- \u6e38\u620f\u3002\n<\/p>\n<p>\n\t\u6e38\u620f\u7684\u6027\u80fd\u8981\u6c42\u975e\u5e38\u9ad8\uff0c\u4e00\u4e9b\u5927\u578b\u6e38\u620f\u8fde PC \u8dd1\u8d77\u6765\u90fd\u5f88\u5403\u529b\uff0c\u66f4\u4e0d\u8981\u63d0\u5728\u6d4f\u89c8\u5668\u7684\u6c99\u76d2\u6a21\u578b\u91cc\u8dd1\u4e86\uff01\u4f46\u662f\uff0c\u5c3d\u7ba1\u5f88\u56f0\u96be\uff0c\u8bb8\u591a\u5f00\u53d1\u8005\u59cb\u7ec8\u6ca1\u653e\u5f03\uff0c\u5e0c\u671b\u8ba9\u6d4f\u89c8\u5668\u8fd0\u884c 3D \u6e38\u620f\u3002\n<\/p>\n<p>\n\t<a href=\"https:\/\/github.com\/kripken\/emscripten\" target=\"_blank\">Emscripten<\/a>\u662f\u4e00\u4e2a\u7f16\u8bd1\u5668\u9879\u76ee\u3002\u8fd9\u4e2a\u7f16\u8bd1\u5668\u53ef\u4ee5\u5c06 C \/ C++ \u4ee3\u7801\u7f16\u8bd1\u6210 JS \u4ee3\u7801\uff0c\u4f46\u4e0d\u662f\u666e\u901a\u7684 JS\uff0c\u800c\u662f\u4e00\u79cd\u53eb\u505a&nbsp;<a href=\"http:\/\/asmjs.org\/\" target=\"_blank\">asm.js<\/a>&nbsp;\u7684 JavaScript \u53d8\u4f53\u3002\n<\/p>\n<p>\n\t\u672c\u6587\u5c31\u5c06\u4ecb\u7ecd Emscripten \u7684\u57fa\u672c\u7528\u6cd5\uff0c\u4ecb\u7ecd\u5982\u4f55\u5c06 C \/ C++ \u8f6c\u6210 JS\u3002\n<\/p>\n<h2>\n\tEmscripten \u7b80\u4ecb<br \/>\n<\/h2>\n<p>\n\t\u867d\u7136 asm.js \u53ef\u4ee5\u624b\u5199\uff0c\u4f46\u662f\u5b83\u4ece\u6765\u5c31\u662f\u7f16\u8bd1\u5668\u7684\u76ee\u6807\u8bed\u8a00\uff0c\u8981\u901a\u8fc7\u7f16\u8bd1\u4ea7\u751f\u3002\u76ee\u524d\uff0c\u751f\u6210 asm.js \u7684\u4e3b\u8981\u5de5\u5177\u662f<a href=\"http:\/\/emscripten.org\/\" target=\"_blank\">Emscripten<\/a>\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Emscripten\" src=\"http:\/\/img.bbs.csdn.net\/upload\/201709\/07\/1504766953_58418.jpg\" style=\"width: 700px;height: 250px\" \/>\n<\/p>\n<p>\n\tEmscripten \u7684\u5e95\u5c42\u662f LLVM \u7f16\u8bd1\u5668\uff0c\u7406\u8bba\u4e0a\u4efb\u4f55\u53ef\u4ee5\u751f\u6210 LLVM IR\uff08Intermediate Representation\uff09\u7684\u8bed\u8a00\uff0c\u90fd\u53ef\u4ee5\u7f16\u8bd1\u751f\u6210 asm.js\u3002 \u4f46\u662f\u5b9e\u9645\u4e0a\uff0cEmscripten \u51e0\u4e4e\u53ea\u7528\u4e8e\u5c06 C \/ C++ \u4ee3\u7801\u7f16\u8bd1\u751f\u6210 asm.js\u3002\n<\/p>\n<pre class=\"brush:plain;\">\r\nC\/C++ &rArr; LLVM ==&gt; LLVM IR &rArr; Emscripten &rArr; asm.js<\/pre>\n<p>\n\t<img decoding=\"async\" alt=\"Emscripten \u7684\u5e95\u5c42\u662f LLVM \u7f16\u8bd1\u5668\" src=\"http:\/\/img.bbs.csdn.net\/upload\/201709\/07\/1504764263_369791.png\" style=\"width: 640px;height: 480px\" \/>\n<\/p>\n<h2>\n\tEmscripten \u7684\u5b89\u88c5<br \/>\n<\/h2>\n<p>\n\tEmscripten \u7684\u5b89\u88c5\u53ef\u4ee5\u6839\u636e<a href=\"http:\/\/kripken.github.io\/emscripten-site\/docs\/getting_started\/downloads.html\" target=\"_blank\">\u5b98\u65b9\u6587\u6863<\/a>\u3002\u7531\u4e8e\u4f9d\u8d56\u8f83\u591a\uff0c\u5b89\u88c5\u8d77\u6765\u6bd4\u8f83\u9ebb\u70e6\uff0c\u6211\u53d1\u73b0\u66f4\u65b9\u4fbf\u7684\u65b9\u6cd5\u662f<a href=\"http:\/\/kripken.github.io\/emscripten-site\/docs\/getting_started\/downloads.html#updating-the-emscripten-sdk\" target=\"_blank\">\u5b89\u88c5 SDK<\/a>\u3002\n<\/p>\n<p>\n\t\u4f60\u53ef\u4ee5\u6309\u7167\u4e0b\u9762\u7684<a href=\"https:\/\/github.com\/kripken\/emscripten\/issues\/5443#issuecomment-320981440\" target=\"_blank\">\u6b65\u9aa4<\/a>\u64cd\u4f5c\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ git clone https:\/\/github.com\/juj\/emsdk.git\r\n$ cd emsdk\r\n$ .\/emsdk install --build=Release sdk-incoming-64bit binaryen-master-64bit\r\n$ .\/emsdk activate --build=Release sdk-incoming-64bit binaryen-master-64bit\r\n$ source .\/emsdk_env.sh<\/pre>\n<p>\n\t\u6ce8\u610f\uff0c\u6700\u540e\u4e00\u884c\u975e\u5e38\u91cd\u8981\u3002\u6bcf\u6b21\u91cd\u65b0\u767b\u9646\u6216\u8005\u65b0\u5efa Shell \u7a97\u53e3\uff0c\u90fd\u8981\u6267\u884c\u4e00\u6b21\u8fd9\u884c\u547d\u4ee4source .\/emsdk_env.sh\u3002\n<\/p>\n<h2>\n\t\u4f7f\u7528<br \/>\n<\/h2>\n<p>\n\t\u9996\u5148\uff0c\u65b0\u5efa\u4e00\u4e2a\u6700\u7b80\u5355\u7684 C++ \u7a0b\u5e8fhello.cc\u3002\n<\/p>\n<pre class=\"brush:cpp;\">\r\n#include &lt;iostream&gt;\r\nint main() {\r\n  std::cout &lt;&lt; &quot;Hello World!&quot; &lt;&lt; std::endl;\r\n}<\/pre>\n<p>\n\t\u7136\u540e\uff0c\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u8f6c\u6210 asm.js\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ emcc hello.cc\r\n$ node a.out.js\r\nHello World!<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0cemcc\u547d\u4ee4\u7528\u4e8e\u7f16\u8bd1\u6e90\u7801\uff0c\u9ed8\u8ba4\u751f\u6210a.out.js\u3002\u4f7f\u7528 Node \u6267\u884ca.out.js\uff0c\u5c31\u4f1a\u5728\u547d\u4ee4\u884c\u8f93\u51fa Hello World\u3002<br \/>\n\t\u6ce8\u610f\uff0casm.js \u9ed8\u8ba4\u81ea\u52a8\u6267\u884cmain\u51fd\u6570\u3002<br \/>\n\temcc\u662f Emscripten \u7684\u7f16\u8bd1\u547d\u4ee4\u3002\u5b83\u7684\u7528\u6cd5\u975e\u5e38\u7b80\u5355\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n# \u751f\u6210 a.out.js\r\n$ emcc hello.c\r\n# \u751f\u6210 hello.js\r\n$ emcc hello.c -o hello.js\r\n# \u751f\u6210 hello.html \u548c hello.js\r\n$ emcc hello.c -o hello.html<\/pre>\n<h2>\n\tC\/C++ \u8c03\u7528 JavaScript<br \/>\n<\/h2>\n<p>\n\tEmscripten \u5141\u8bb8 C \/ C++ \u4ee3\u7801\u76f4\u63a5\u8c03\u7528 JavaScript\u3002\n<\/p>\n<p>\n\t\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6<code>example1.cc<\/code>\uff0c\u5199\u5165\u4e0b\u9762\u7684\u4ee3\u7801\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n#include &lt;emscripten.h&gt;\r\nint main() {\r\n  EM_ASM({ alert(&#039;Hello World!&#039;); });\r\n}<\/pre>\n<p>\n\tEM_ASM\u662f\u4e00\u4e2a\u5b8f\uff0c\u4f1a\u8c03\u7528\u5d4c\u5165\u7684 JavaScript \u4ee3\u7801\u3002\u6ce8\u610f\uff0cJavaScript \u4ee3\u7801\u8981\u5199\u5728\u5927\u62ec\u53f7\u91cc\u9762\u3002\u7136\u540e\uff0c\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u7f16\u8bd1\u6210 asm.js\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ emcc example1.cc -o example1.html<\/pre>\n<p>\n\t\u6d4f\u89c8\u5668\u6253\u5f00example1.html\uff0c\u5c31\u4f1a\u8df3\u51fa\u5bf9\u8bdd\u6846Hello World!\u3002\n<\/p>\n<h2>\n\tC\/C++ \u4e0e JavaScript \u7684\u901a\u4fe1<br \/>\n<\/h2>\n<p>\n\tEmscripten \u5141\u8bb8 C \/ C++ \u4ee3\u7801\u4e0e JavaScript \u901a\u4fe1\u3002<br \/>\n\t\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6example2.cc\uff0c\u5199\u5165\u4e0b\u9762\u7684\u4ee3\u7801\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n#include &lt;emscripten.h&gt;\r\n#include &lt;iostream&gt;\r\nint main() {\r\n  int val1 = 21;\r\n  int val2 = EM_ASM_INT({ return $0 * 2; }, val1);\r\n  std::cout &lt;&lt; &quot;val2 == &quot; &lt;&lt; val2 &lt;&lt; std::endl;\r\n}<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0cEM_ASM_INT\u8868\u793a JavaScript \u4ee3\u7801\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u5b83\u7684\u53c2\u6570\u91cc\u9762\u7684$0\u8868\u793a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c$1\u8868\u793a\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002EM_ASM_INT\u7684\u5176\u4ed6\u53c2\u6570\u4f1a\u6309\u7167\u987a\u5e8f\uff0c\u4f20\u5165 JavaScript \u8868\u8fbe\u5f0f\u3002<br \/>\n\t\u7136\u540e\uff0c\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u7f16\u8bd1\u6210 asm.js\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ emcc example2.cc -o example2.html<\/pre>\n<p>\n\t\u6d4f\u89c8\u5668\u6253\u5f00\u7f51\u9875example2.html\uff0c\u4f1a\u663e\u793aval2 == 42\u3002\n<\/p>\n<h2>\n\tEM_ASM \u5b8f\u7cfb\u5217<br \/>\n<\/h2>\n<p>\n\tEmscripten \u63d0\u4f9b\u4ee5\u4e0b\u5b8f\u3002\n<\/p>\n<ul>\n<li>\n\t\tEM_ASM\uff1a\u8c03\u7528 JS \u4ee3\u7801\uff0c\u6ca1\u6709\u53c2\u6570\uff0c\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c\u3002\n\t<\/li>\n<li>\n\t\tEMASMARGS\uff1a\u8c03\u7528 JS \u4ee3\u7801\uff0c\u53ef\u4ee5\u6709\u4efb\u610f\u4e2a\u53c2\u6570\uff0c\u4f46\u662f\u6ca1\u6709\u8fd4\u56de\u503c\u3002\n\t<\/li>\n<li>\n\t\tEMASMINT\uff1a\u8c03\u7528 JS \u4ee3\u7801\uff0c\u53ef\u4ee5\u6709\u4efb\u610f\u4e2a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002\n\t<\/li>\n<li>\n\t\tEMASMDOUBLE\uff1a\u8c03\u7528 JS \u4ee3\u7801\uff0c\u53ef\u4ee5\u6709\u4efb\u610f\u4e2a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002\n\t<\/li>\n<li>\n\t\tEMASMINT_V\uff1a\u8c03\u7528 JS \u4ee3\u7801\uff0c\u6ca1\u6709\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002\n\t<\/li>\n<li>\n\t\tEMASMDOUBLE_V\uff1a\u8c03\u7528 JS \u4ee3\u7801\uff0c\u6ca1\u6709\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u4e0b\u9762\u662f\u4e00\u4e2aEM_ASM_ARGS\u7684\u4f8b\u5b50\u3002\u65b0\u5efa\u6587\u4ef6example3.cc\uff0c\u5199\u5165\u4e0b\u9762\u7684\u4ee3\u7801\u3002\n<\/p>\n<pre class=\"brush:cpp;\">\r\n#include &lt;emscripten.h&gt;\r\n#include &lt;string&gt;\r\nvoid Alert(const std::string &amp; msg) {\r\n  EM_ASM_ARGS({\r\n    var msg = Pointer_stringify($0);\r\n    alert(msg);\r\n  }, msg.c_str());\r\n}\r\nint main() {\r\n  Alert(&quot;Hello from C++!&quot;);\r\n}<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f20\u5165 JS \u4ee3\u7801\u3002\u7531\u4e8e\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u6240\u4ee5\u4f7f\u7528EM_ASM_ARGS\u3002\u53e6\u5916\uff0c\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u5728 C \/ C++ \u91cc\u9762\uff0c\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u6240\u4ee5\u8981\u8c03\u7528Pointer_stringify()\u65b9\u6cd5\u5c06\u5b57\u7b26\u6570\u7ec4\u8f6c\u6210 JS \u7684\u5b57\u7b26\u4e32\u3002<br \/>\n\t\u63a5\u7740\uff0c\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u8f6c\u6210 asm.js\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ emcc example3.cc -o example3.html<\/pre>\n<p>\n\t\u6d4f\u89c8\u5668\u6253\u5f00example3.html\uff0c\u4f1a\u8df3\u51fa\u5bf9\u8bdd\u6846&quot;Hello from C++!&quot;\u3002\n<\/p>\n<h2>\n\tJavaScript \u8c03\u7528 C \/ C++ \u4ee3\u7801<br \/>\n<\/h2>\n<p>\n\tJS \u4ee3\u7801\u4e5f\u53ef\u4ee5\u8c03\u7528 C \/ C++ \u4ee3\u7801\u3002\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6example4.cc\uff0c\u5199\u5165\u4e0b\u9762\u7684\u4ee3\u7801\u3002\n<\/p>\n<pre class=\"brush:cpp;\">\r\n#include &lt;emscripten.h&gt;\r\nextern &quot;C&quot; {\r\n  double SquareVal(double val) {\r\n    return val * val;\r\n  }\r\n}\r\nint main() {\r\n  EM_ASM({\r\n    SquareVal = Module.cwrap(&#039;SquareVal&#039;, &#039;number&#039;, ['number']);\r\n    var x = 12.5;\r\n    alert(&#039;Computing: &#039; + x + &#039; * &#039; + x + &#039; = &#039; + SquareVal(x));\r\n  });\r\n}<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0cEM_ASM\u6267\u884c JS \u4ee3\u7801\uff0c\u91cc\u9762\u6709\u4e00\u4e2a C \u8bed\u8a00\u51fd\u6570SquareVal\u3002\u8fd9\u4e2a\u51fd\u6570\u5fc5\u987b\u653e\u5728extern &quot;C&quot;\u4ee3\u7801\u5757\u4e4b\u4e2d\u5b9a\u4e49\uff0c\u800c\u4e14 JS \u4ee3\u7801\u8fd8\u8981\u7528Module.cwrap()\u65b9\u6cd5\u5f15\u5165\u8fd9\u4e2a\u51fd\u6570\u3002<br \/>\n\tModule.cwrap()\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff0c\u542b\u4e49\u5982\u4e0b\u3002\n<\/p>\n<ul>\n<li>\n\t\tC \u51fd\u6570\u7684\u540d\u79f0\uff0c\u653e\u5728\u5f15\u53f7\u4e4b\u4e2d\u3002\n\t<\/li>\n<li>\n\t\tC \u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002\u5982\u679c\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u628a\u7c7b\u578b\u5199\u6210null\u3002\n\t<\/li>\n<li>\n\t\t\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u7684\u6570\u7ec4\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u9664\u4e86Module.cwrap()\uff0c\u8fd8\u6709\u4e00\u4e2aModule.ccall()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728 JS \u4ee3\u7801\u4e4b\u4e2d\u8c03\u7528 C \u51fd\u6570\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar result = Module.ccall(&#039;int_sqrt&#039;, \/\/ C \u51fd\u6570\u7684\u540d\u79f0\r\n  &#039;number&#039;, \/\/ \u8fd4\u56de\u503c\u7684\u7c7b\u578b\r\n  ['number'], \/\/ \u53c2\u6570\u7c7b\u578b\u7684\u6570\u7ec4\r\n  [28] \/\/ \u53c2\u6570\u6570\u7ec4\r\n); <\/pre>\n<p>\n\t\u56de\u5230\u524d\u9762\u7684\u793a\u4f8b\uff0c\u73b0\u5728\u5c06example4.cc\u7f16\u8bd1\u6210 asm.js\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$  emcc -s EXPORTED_FUNCTIONS=&quot;['_SquareVal', '_main']&quot; example4.cc -o example4.html<\/pre>\n<p>\n\t\u6ce8\u610f\uff0c\u7f16\u8bd1\u547d\u4ee4\u91cc\u9762\u8981\u7528-s EXPORTED_FUNCTIONS\u53c2\u6570\u7ed9\u51fa\u8f93\u51fa\u7684\u51fd\u6570\u540d\u6570\u7ec4\uff0c\u800c\u4e14\u51fd\u6570\u540d\u524d\u9762\u52a0\u4e0b\u5212\u7ebf\u3002\u672c\u4f8b\u53ea\u8f93\u51fa\u4e24\u4e2a C \u51fd\u6570\uff0c\u6240\u4ee5\u8981\u5199\u6210[&#8216;_SquareVal&#8217;, &#8216;_main&#8217;]\u3002<br \/>\n\t\u6d4f\u89c8\u5668\u6253\u5f00example4.html\uff0c\u5c31\u4f1a\u770b\u5230\u5f39\u51fa\u7684\u5bf9\u8bdd\u6846\u91cc\u9762\u663e\u793a\u4e0b\u9762\u7684\u5185\u5bb9\u3002\n<\/p>\n<pre class=\"brush:plain;\">\r\nComputing: 12.5 * 12.5 = 156.25 <\/pre>\n<h2>\n\tC \u51fd\u6570\u8f93\u51fa\u4e3a JavaScript \u6a21\u5757<br \/>\n<\/h2>\n<p>\n\t\u53e6\u4e00\u79cd\u60c5\u51b5\u662f\u8f93\u51fa C \u51fd\u6570\uff0c\u4f9b\u7f51\u9875\u91cc\u9762\u7684 JavaScript \u811a\u672c\u8c03\u7528\u3002 \u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6example5.cc\uff0c\u5199\u5165\u4e0b\u9762\u7684\u4ee3\u7801\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nextern &quot;C&quot; {\r\n  double SquareVal(double val) {\r\n    return val * val;\r\n  }\r\n}<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0cSquareVal\u662f\u4e00\u4e2a C \u51fd\u6570\uff0c\u653e\u5728extern &quot;C&quot;\u4ee3\u7801\u5757\u91cc\u9762\uff0c\u5c31\u53ef\u4ee5\u5bf9\u5916\u8f93\u51fa\u3002<br \/>\n\t\u7136\u540e\uff0c\u7f16\u8bd1\u8fd9\u4e2a\u51fd\u6570\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ emcc -s EXPORTED_FUNCTIONS=&quot;['_SquareVal']&quot; example5.cc -o example5.js<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c-s EXPORTED_FUNCTIONS\u53c2\u6570\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u4ee3\u7801\u91cc\u9762\u9700\u8981\u8f93\u51fa\u7684\u51fd\u6570\u540d\u3002\u51fd\u6570\u540d\u524d\u9762\u8981\u52a0\u4e0b\u5212\u7ebf\u3002<br \/>\n\t\u63a5\u7740\uff0c\u5199\u4e00\u4e2a\u7f51\u9875\uff0c\u52a0\u8f7d\u521a\u521a\u751f\u6210\u7684example5.js\u3002\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;!DOCTYPE HTML&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;Test File&lt;\/h1&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;example5.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\n  SquareVal = Module.cwrap(&#039;SquareVal&#039;, &#039;number&#039;, ['number']);\r\n  document.write(&quot;result == &quot; + SquareVal(10));\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>\n\t\u6d4f\u89c8\u5668\u6253\u5f00\u8fd9\u4e2a\u7f51\u9875\uff0c\u5c31\u53ef\u4ee5\u770b\u5230result == 100\u4e86\u3002\n<\/p>\n<h2>\n\tNode \u8c03\u7528 C \u51fd\u6570<br \/>\n<\/h2>\n<p>\n\t\u5982\u679c\u6267\u884c\u73af\u5883\u4e0d\u662f\u6d4f\u89c8\u5668\uff0c\u800c\u662f Node\uff0c\u90a3\u4e48\u8c03\u7528 C \u51fd\u6570\u5c31\u66f4\u65b9\u4fbf\u4e86\u3002\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6example6.c\uff0c\u5199\u5165\u4e0b\u9762\u7684\u4ee3\u7801\u3002\n<\/p>\n<pre class=\"brush:cpp;\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;emscripten.h&gt;\r\nvoid sayHi() {\r\n  printf(&quot;Hi!\\n&quot;);\r\n}\r\nint daysInWeek() {\r\n  return 7;\r\n}<\/pre>\n<p>\n\t\u7136\u540e\uff0c\u5c06\u8fd9\u4e2a\u811a\u672c\u7f16\u8bd1\u6210 asm.js\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ emcc -s EXPORTED_FUNCTIONS=&quot;['_sayHi', '_daysInWeek']&quot; example6.c -o example6.js<\/pre>\n<p>\n\t\u63a5\u7740\uff0c\u5199\u4e00\u4e2a Node \u811a\u672ctest.js\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar em_module = require(&#039;.\/api_example.js&#039;);\r\nem_module._sayHi();\r\nem_module.ccall(&quot;sayHi&quot;);\r\nconsole.log(em_module._daysInWeek());<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0cNode \u811a\u672c\u8c03\u7528 C \u51fd\u6570\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u4f7f\u7528\u4e0b\u5212\u7ebf\u51fd\u6570\u540d\u8c03\u7528em_module._sayHi()\uff0c\u53e6\u4e00\u79cd\u4f7f\u7528ccall\u65b9\u6cd5\u8c03\u7528em_module.ccall(&quot;sayHi&quot;)\u3002<br \/>\n\t\u8fd0\u884c\u8fd9\u4e2a\u811a\u672c\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u547d\u4ee4\u884c\u7684\u8f93\u51fa\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ node test.js\r\nHi!\r\nHi!\r\n7<\/pre>\n<h2>\n\t\u7528\u9014<br \/>\n<\/h2>\n<p>\n\tasm.js \u4e0d\u4ec5\u80fd\u8ba9\u6d4f\u89c8\u5668\u8fd0\u884c&nbsp;<a href=\"http:\/\/kripken.github.io\/boon\/boon.html\" target=\"_blank\">3D \u6e38\u620f<\/a>\uff0c\u8fd8\u53ef\u4ee5\u8fd0\u884c\u5404\u79cd<a href=\"https:\/\/github.com\/dherman\/asm.js\/wiki\/Projects-using-asm.js\" target=\"_blank\">\u670d\u52a1\u5668\u8f6f\u4ef6<\/a>\uff0c\u6bd4\u5982&nbsp;<a href=\"https:\/\/github.com\/kripken\/lua.vm.js\" target=\"_blank\">Lua<\/a>\u3001<a href=\"https:\/\/github.com\/xxuejie\/webruby\" target=\"_blank\">Ruby<\/a>&nbsp;\u548c&nbsp;<a href=\"https:\/\/github.com\/kripken\/sql.js\" target=\"_blank\">SQLite<\/a>\u3002 \u8fd9\u610f\u5473\u7740\u5f88\u591a\u5de5\u5177\u548c\u7b97\u6cd5\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u73b0\u6210\u7684\u4ee3\u7801\uff0c\u4e0d\u7528\u91cd\u65b0\u5199\u4e00\u904d\u3002\n<\/p>\n<p>\n\t\u53e6\u5916\uff0c\u7531\u4e8e asm.js \u7684\u8fd0\u884c\u901f\u5ea6\u8f83\u5feb\uff0c\u6240\u4ee5\u4e00\u4e9b\u8ba1\u7b97\u5bc6\u96c6\u578b\u7684\u64cd\u4f5c\uff08\u6bd4\u5982\u8ba1\u7b97 Hash\uff09\u53ef\u4ee5\u4f7f\u7528 C \/ C++ \u5b9e\u73b0\uff0c\u518d\u5728 JS \u4e2d\u8c03\u7528\u5b83\u4eec\u3002\n<\/p>\n<p>\n\t\u771f\u5b9e\u7684\u8f6c\u7801\u5b9e\u4f8b\u53ef\u4ee5\u770b\u4e00\u4e0b&nbsp;<a href=\"https:\/\/github.com\/kripken\/zee.js\" target=\"_blank\">gzlib<\/a>&nbsp;\u7684\u7f16\u8bd1\uff0c\u53c2\u8003\u5b83\u7684&nbsp;<a href=\"https:\/\/github.com\/kripken\/zee.js\/blob\/master\/Makefile\" target=\"_blank\">Makefile<\/a>&nbsp;\u600e\u4e48\u5199\u3002\n<\/p>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/en.wikipedia.org\/wiki\/Asm.js\" target=\"_blank\">asm.js<\/a>, by Wikipedia\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/kripken.github.io\/mloc_emscripten_talk\/cppcon.html#\/\" target=\"_blank\">Emscripten &amp; asm.js: C++&#039;s role in the modern web<\/a>, by Alon Zakai\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/kripken.github.io\/emscripten-site\/docs\/getting_started\/Tutorial.html\" target=\"_blank\">Emscripten Tutorial<\/a>, by Emscripten\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/johnresig.com\/blog\/asmjs-javascript-compile-target\/\" target=\"_blank\">Asm.js: The JavaScript Compile Target<\/a>, by John Resig\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/devosoft.org\/an-introduction-to-web-development-with-emscripten\/\" target=\"_blank\">An Introduction to Web Development with Emscripten<\/a>, by Charles Ofria\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/kripken.github.io\/emscripten-site\/docs\/porting\/connecting_cpp_and_javascript\/Interacting-with-code.html\" target=\"_blank\">Interacting with code<\/a>, by Emscripten\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/pspdfkit.com\/blog\/2017\/webassembly-a-new-hope\/\" target=\"_blank\">WebAssembly: A New Hope<\/a>, by Philipp Spiess and James Swift\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/www.sitepoint.com\/understanding-asm-js\/\" target=\"_blank\">Understanding asm.js<\/a>, by Afshin Mehrabani\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1952\">Emscripten \u5165\u95e8\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Web \u6280\u672f\u7a81\u98de\u731b\u8fdb\uff0c\u4f46\u662f\u6709\u4e00\u4e2a\u9886\u57df\u4e00\u76f4\u65e0\u6cd5\u7a81\u7834 &#8212;- \u6e38\u620f\u3002 \u6e38\u620f\u7684\u6027\u80fd\u8981\u6c42\u975e\u5e38\u9ad8\uff0c\u4e00\u4e9b\u5927\u578b\u6e38\u620f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[1489,1488,1015,1490,1106],"class_list":["post-1952","post","type-post","status-publish","format-standard","hentry","category-html5","tag-3d-","tag-asm-js","tag-c","tag-emscripten","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1952"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1952\/revisions"}],"predecessor-version":[{"id":1953,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1952\/revisions\/1953"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}