Wardite
ã¨ããWASM Runtimeãä½ã£ã¦ãã¾ããã
ä»ã®æããRustã§Wasm Runtimeãå®è£ ãããã®å 容ãä¸éãå®è£ ãã段éã§ãã
fibã¾ã§åãããã«ãªã£ãã®ã§ãã´ãªã©ããã®ã³ã¼ã¹ã®ç¯å²ã¯ä¸æ¦å®æ
— Uchio Kondoð¥ (@udzura) 2024å¹´10æ28æ¥
ãã¨ã¯wasmåãç»ãç¶ããã ãã ... pic.twitter.com/65m8qbSvap
以ä¸ã®ãããªwasmããã°ã©ã ãåããã¾ãã
(module (func $fib (export "fib") (param $n i32) (result i32) (if (i32.lt_s (local.get $n) (i32.const 2)) (then (return (i32.const 1))) ) (return (i32.add (call $fib (i32.sub (local.get $n) (i32.const 2))) (call $fib (i32.sub (local.get $n) (i32.const 1))) ) ) ) )
$ gem install wardite $ wat2wasm fib.wat $ wardite ./fib.wasm fib 20 return value: 10946
Warditeã¯ä»¥ä¸ã®ãããªã³ã³ã»ããã§éçºããã¦ãã¾ãã
- Pure Rubyå®è£ ï¼åä½æãæ¨æºã©ã¤ãã©ãªä»¥å¤ã¸ã®ä¾åã¯ä¸åãªããCæ¡å¼µãã«ãããªãï¼
- Fully Type-annotated ï¼rbs-inlineã使ããä»ã®ã¨ããå®è£ ããé¨åã¯ã»ã¼å ¨é¨åä»ãããï¼
- WASI preview 1ã«å¯¾å¿äºå®
WASM GCãComponent Modelãã®ä»å¯¾å¿ããä¸è¨ãä¸éãã§ãã¦ãããæ¤è¨ã«æ¤è¨ãéããããã¨ã
å è¡å®è£ ã¨ãã¦technohippyããã WebAssembly Core Specification v1.0 ã®åºæ¬çãªã»ã¯ã·ã§ã³ã¨å½ä»¤ãRubyã§å®è£ ããããããã®ãããã®ã§ãã...ã
ãã¡ãã¯ããã¢ã¯ãã£ãã«ã¯ã¡ã³ããã³ã¹ãããªãããªã®ã¨ãWASIã«ã¯å¯¾å¿ãã¦ããªãã®ã§ããã®è¾ºå«ãWarditeã¯ã¡ã¾ã¡ã¾ãããã¨æã£ã¦ãã¾ãã
å®è£ ãã¦æã£ããã¨
- rbs-inline ãããã¯ææ¾ããªã...
ä»åãããªãè¤éãªã½ããã¦ã§ã¢ãæ¸ãã¦ãã¾ããã rbs-inline --output && steep check
ãã¦ééããã¨ããã®ã¯å®å¿æãããã¾ãï¼ã¾ã èªåãã¹ããå
¨ç¶è¶³ããªãã®ã§ãããããã§ãsteep checkã®çµæãå
ã«ä¸å®ä»¥ä¸ã«ã¯å®å¿ãã¦å¹çããå¤æ´ãå ãããã¾ãï¼ã
ã¾ããã¨ãã£ã¿ã§ã®ã¡ã½ããè£å®ãå¤æ°ã®åã®è£å®ãå®ç¾©ã¸ã£ã³ããæ¸ãæãã®ææãã®ä»ãæã£ã以ä¸ã«ã¡ããã¨åããã¡ãã£ã¯ãã£ã«çç£æ§ãä¸ãã£ã¦ããã®ãæãã¾ãï¼ãªãã¨ãã£ã¿ã¯ä»åããVSCodeã«ãã¦ãã¾ã...ï¼ã
åã¯é常ã«åã®è¡¨ç¾åã¨æ©è½ã«å¯ãè¨èªã§ããã¨ããã®Rustãæ¸ãã¦ããã®ã§ãããRustã¯ã¨ãã£ã¿ã®æ¯æ´ãæ¬å½ã«ç´ æ´ãããã§ããã§ãããRubyã§ãåäºåã«ä¾¿å©ã«ã¨ãã£ã¿æ¯æ´ãåãããããã«ãªã£ã¦ããã®ã§ã¯ï¼ ã¨æãã¦ãã¾ãã
ã¾ããå人çã«ã¯ã¡ã½ããã®å®ç¾©ã¨åã®å®£è¨ãé¢ãã¦ããã¨èªãéã«ãæ¸ãéã«ãè¾ãã®ã§ã¯ãã¨æã£ã¦ããã®ã§ãrbs-inlineã®ããä¸çã¯ç´ æ´ããããæé«ï¼ã¨ããæ°æã¡ã«æº¢ãã¦ãã¾ããåãæ¸ãããã®è¨æ³ã1æéãããã§å ¨é¨æ £ãã¾ãããä»å¾ãã¯ã©ã¹ãã¡ã½ããã®åã綺éºã«ãªãããæèãã¦å®è£ ãã¦ããããã¨ããã§ãã
- YJIT ä»è©¦ããã
ã¡ãã㨠IPS(Instruction per second)ãè¨æ¸¬ã§ããããã«ãã¦ãYJITå«ãè²ã ãªæ¡ä»¶ã§ããã©ã¼ãã³ã¹åä¸ã試ãããã¨æã£ã¦ãã¾ãããã ãåéé²ã«æ¸¬ã£ã¦å¾®å¦ãªæ°æã¡ã«ãªã£ã ã®ã§ãå½ä»¤é¡ãä¸éããµãã¼ããã¦ãããããããªã¨ã
- VMã®æ°æã¡ã1ãã¯ãã³ããã£ã
å¥ä»¶ã§mrubyã®VMãèªä½ãã¦ããã®ã§ãããæ£ç´ã³ã¼ã«ã¹ã¿ãã¯ã¨ããã¬ã¼ã ã¨ãã®æ¦å¿µãä½ãããããªãã£ãã¨ãããè¥å¹²åéã«ãªããæ°ããã¾ããVMã£ã¦ããä½ããã ããã¨ã«ããä½ã§ãããã§ãã¹ã¿ãã¯ãªãã ãªã¿ãããª...ã
ã¾ããRustã¯ã ãã¶æ £ããã¤ããã§ãããã§ãããä»åRBSæå¹ã§ãã£ã¦ãRubyã®æ¹ãé«éã«æ¸ãããªã¨æ°ä»ãã¦ãã¾ã£ããããmruby/c ãªã©ãåèã«ããªããmruby VMãä¸åº¦Rubyã§ä½ãã¨ããã®ã§ã¯ãªããã¨ããæ°æã¡ã«ãªã£ã¦ããã¨ããã§ã*1ãä½è«ã§ããã
- mruby ã§ãåãããã¨ãããããããªã
Warditeã®ä¸ã§ãä»ã®æCRubyã§æ®éã«ä½¿ãã¦mrubyã§æ¨æºã§ã¤ãã¦ãªãæ©è½ã§ä½¿ã£ã¦ããã®ã¯ StringIO
㨠pack/unpack
ãããã§ãããã®è¾ºã¯polyfillãæ¸ãããããããæ¸ãæãããï¼int32ã®LE表ç¾ããã¼ã¹ãã¦ãã ãã ã£ãããªã®ã§ï¼ããã°ãããªããããããªæããããã¾ããããã¯æ°ãåããã...ã
mrubyã§åãã°æ®éã«ï¼dynamic linkedã§ããï¼ã¯ã³ãã¤ããªã«ãã§ãã¾ããããã
Goã«ããã¼ãä¾åWASM Runtime wazero ã®ä½¿ããæ¹ãã¿ã¦ããã¨ãè¨èªã©ã³ã¿ã¤ã 以å¤ã«ä¾åããªãçµã¿è¾¼ã¿WASM Runtimeã¯éè¦ãããã®ã§ã¯ãªããã¨æãããã¾ããã¨ãããã¨ã§ãã¡ããã¡ã¾ã¡ã¾éçºãã¦ããããã¨æã£ã¦ãã¾ãã
// ruby.wasm ãWASM on Rubyã®ä¸ã§åä½ã§ããããã«ããããããä¸ã¤ã®ãã¤ã«ã¹ãã¼ã³ããªã¨...ã
ã¾ããã´ãªã©ããã®ãRustã§Wasm Runtimeãå®è£ ãããã¯é常ã«ããã§ãã¦ãã¦ã LEB128 å¨ããªã©ã®ç½ ãä¸éã解説ããã¦ããã®ã§ãããªãã¨èªã¿é²ãããã¨ãã§ãã¾ããRustã§ã®åçµãã¨ã¦ãè¯ãããã§ãããã¿ãªããã®ã好ããªè¨èªã«ç§»ãæ¿ãã¦å®è£ ããã®ãé¢ç½ãã¨æãã¾ããç´ æ´ãããé»åæ¸ç±ããããã¨ããããã¾ããï¼
*1:èªã¿è¿ãã¨ä½è¨ã£ã¦ãã ã£ã¦æãã®ãã¨ããããã¨ãã¦ãã...