Hello WebAssembly

听闻 Firefox 52 已经支持了 WebAssembly,兴致大发,手撸一小段,也算是有写过 WebAssembly 的经历了(笑)。

(module
  (import "console" "log" (func $log (param i32)))
  (func $fact (param $n i32) (result i32)
    get_local $n   ;; push parameter $n on stack
    i32.const 0    ;; push constant int32 "0" on stack
    i32.eq         ;; execute "eq" which pops two operands from stack
    if i32         ;; pops one int32 from stack; if it is not "0"
      i32.const 1
    else
      get_local $n
      get_local $n
      i32.const 1
      i32.sub      ;; execute "sub" which pops two operands and pushes the result int32 on stack
      call $fact   ;; call function $fact which pops one int32 from stack and pushes the result int32 on stack
      i32.mul
    end)
  (func (export "calcFact") (param $n i32)
    get_local $n
    call $fact
    call $log)
)
fetch('/upload/2017/03/09/201703092358.wasm').then(response =>
  response.arrayBuffer()
).then(bytes =>
  WebAssembly.instantiate(bytes, {
    console: {
      log: (arg) => console.log(arg)
    }
  })
).then(results => {
  results.instance.exports.calcFact(10)
});

哎呀,代码高亮还不支持 WebAssembly 呀。

不过目前文本格式的 WebAssembly 还没有完全定稿,上面代码的语法是按照 wabt 编写。

*****
Written by 空鱼 on 09 March 2017
        ト 、.  /|
         \\|::|/|,. -‐‐- 、.,_
        ,>''"´:::::::::::::::::::::::`ヽ.
        /:::/::::::::::/|::::::::::::::::::::::`ヽ.
       ./:::/::::::::::/‐ヘ:::::::ト 、ー-::\`フ
      ,'<:;_/:::::::/ __, \| 、_\::::::::>
     ,.|::::∠;:::::::xーt--,   -tー‐ァ7´| 
     !:::::::rレ∨.  `‐'    `ー' ::八 
  -ト 、__人::::ゝ'ヘ              |:::_;:>
  人::::::::::::;>:::;ハ、     ,   .人:|  
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄