{"id":91375,"date":"2020-05-01T18:59:15","date_gmt":"2020-05-01T10:59:15","guid":{"rendered":"http:\/\/4563.org\/?p=91375"},"modified":"2020-05-01T18:59:15","modified_gmt":"2020-05-01T10:59:15","slug":"%e5%a5%bd%e5%ba%b7%e9%80%9f%e9%80%92","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=91375","title":{"rendered":"\u597d\u5eb7\u901f\u9012"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u597d\u5eb7\u901f\u9012               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Mohanson <\/span>  <span><i><\/i> 16<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u524d\u51e0\u5e74\u8fd8\u4e00\u76f4\u4ee5\u4e3a AssemblyScript \u662f\u4e2a\u73a9\u5177, \u6700\u8fd1\u62ff\u8d77\u6765\u770b\u4e86\u4e0b\u5c45\u7136\u53d1\u73b0\u5df2\u7ecf\u86ee\u6210\u719f\u4e86, \u9042\u82b1\u4e86\u70b9\u65f6\u95f4\u8ba9 AssemblyScript \u4ee3\u7801\u53ef\u4ee5\u539f\u751f\u8dd1\u5728 Python \u73af\u5883\u4e0b&#8230;<\/p>\n<pre><code>$ pip3 install pywasm <\/code><\/pre>\n<blockquote>\n<p>pywasm \u662f\u4f7f\u7528\u7eaf python \u5b9e\u73b0\u7684 wasm \u89e3\u91ca\u5668, \u4f5c\u8005\u5c31\u662f\u672c\u4eba, \u672c\u6765\u5df2\u7ecf\u51e0\u4e2a\u6708\u6ca1\u66f4\u65b0\u4e86, \u8fd1\u671f\u7a81\u7136\u6536\u5230 pypa(python \u7684\u5b98\u65b9\u5305\u7ba1\u7406\u5668\u56e2\u961f) \u4e00\u5f00\u53d1\u8005\u53d1\u6765\u7684\u4e00\u5c01\u611f\u8c22\u4fe1, \u5c31\u51b3\u5b9a\u5f00\u59cb\u8bc8\u5c38\u7ee7\u7eed\u5bf9\u5176\u8fdb\u884c\u5b8c\u5584.<\/p>\n<\/blockquote>\n<h1>AssemblyScript: \u4e3a wasm \u53d1\u660e\u7684\u7f16\u7a0b\u8bed\u8a00<\/h1>\n<p> <\/p>\n<p>\u76ee\u524d\u4e3a\u6b62, \u8bb8\u591a\u9ad8\u7ea7\u8bed\u8a00\u90fd\u80fd\u7f16\u8bd1\u5230 wasm. \u5176\u4e2d\u6700\u6210\u719f\u7684\u662f C \u8bed\u8a00: \u6bd5\u7adf\u5728\u8bbe\u8ba1\u5f00\u53d1\u8fc7\u7a0b\u4e2d C \u5c31\u662f wasm \u7684\u7b2c\u4e00\u76ee\u6807. \u4f46\u662f C \u6bd5\u7adf\u5bf9\u5927\u4f17\u4e0d\u592a\u53cb\u597d, \u56e0\u6b64\u5728 C \u4e4b\u4e0a\u4e00\u95e8\u65b0\u7684\u8bed\u8a00\u51fa\u73b0\u4e86, \u5b83\u5c31\u662f AssemblyScript, \u4e00\u95e8\u5bf9 TypeScript \u505a\u4e86\u51cf\u6cd5\u7684\u7f16\u7a0b\u8bed\u8a00.<\/p>\n<p>\u6211\u4eec\u65b0\u5efa\u4e00\u4e2a\u76ee\u5f55, \u8fdb\u5165\u8be5\u76ee\u5f55<\/p>\n<pre><code>$ npm install --save-dev assemblyscript $ npx asinit . <\/code><\/pre>\n<p>asinit \u547d\u4ee4\u81ea\u52a8\u521b\u5efa\u5efa\u8bae\u7684\u76ee\u5f55\u7ed3\u6784\u548c\u914d\u7f6e\u6587\u4ef6. \u7136\u540e, \u6253\u5f00 .\/assembly\/index.ts, \u5176\u6e90\u4ee3\u7801\u662f\u4e00\u4e2a\u793a\u4f8b\u7684\u52a0\u6cd5\u51fd\u6570, \u5982\u4e0b<\/p>\n<pre><code>export function add(a: i32, b: i32): i32 {   return a + b; } <\/code><\/pre>\n<pre><code>$ npm run asbuild <\/code><\/pre>\n<p>\u8fd0\u884c\u5982\u4e0a\u547d\u4ee4\u5c06\u7a0b\u5e8f\u7f16\u8bd1\u4e3a WebAssembly.<\/p>\n<p>\u4f7f\u7528\u6839\u76ee\u5f55\u4e2d\u7684 index.js \u5b9e\u4f8b\u5316\u548c\u5bfc\u51fa WebAssembly \u6a21\u5757, \u60a8\u5c06\u53ef\u4ee5\u50cf\u4efb\u4f55\u5176\u5b83\u6a21\u5757\u4e00\u6837\u4f7f\u7528\u5b83, \u5176\u663e\u7740\u533a\u522b\u5728\u4e8e, \u6a21\u5757\u5bfc\u51fa\u7684\u552f\u4e00\u503c\u662f\u6574\u6570\u548c\u6d6e\u70b9\u6570. \u5230\u76ee\u524d\u4e3a\u6b62, \u4e00\u5207\u90fd\u5f88\u597d&#8230;<\/p>\n<h1>Run on pywasm<\/h1>\n<p> <\/p>\n<pre><code>$ vim index.py <\/code><\/pre>\n<p>\u5c06\u4ee5\u4e0b\u4ee3\u7801\u62f7\u8d1d\u5230 index.py \u4e2d, \u4ee3\u7801\u4e2d\u505a\u4e86\u4e24\u4ef6\u4e8b: \u8f7d\u5165 wasm \u4e8c\u8fdb\u5236\u6587\u4ef6, \u5e76\u8c03\u7528\u51fd\u6570 add(10, 20). import pywasm<\/p>\n<pre><code>def env_abort(_: pywasm.Ctx):     return   vm = pywasm.load('.\/build\/optimized.wasm', {     'env': {         'abort': env_abort,     } }) r = vm.exec('add', [10, 20]) print(r) <\/code><\/pre>\n<pre><code>$ python3 index.py # 30 <\/code><\/pre>\n<p>Bingo!<\/p>\n<blockquote>\n<p>\u672c\u6587\u4ee3\u7801\u5df2\u53d1\u5e03\u81f3 github, \u60a8\u53ef\u4ee5\u76f4\u63a5\u514b\u9686\u4ee3\u7801\u5230\u672c\u5730, \u5982\u6b64, \u5c31\u4e0d\u7528\u81ea\u5df1\u6572\u4ee3\u7801\u5566!<\/p>\n<\/blockquote>\n<pre><code>$ git clone https:\/\/github.com\/mohanson\/pywasm_assemblyscript <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u5eb7\u901f\u9012 \u8cc7\u6df1\u5927\u4f6c : Mohan&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91375"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91375"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91375\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91375"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}