Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TinyGoでWasmを生成する / Compile Wasm by TinyGo

TinyGoでWasmを生成する / Compile Wasm by TinyGo

TinyGoでWebAssemblyをコンパイルした話

hachi (Hayao Kimura)

May 20, 2019
Tweet

More Decks by hachi (Hayao Kimura)

Other Decks in Programming

Transcript

  1. ©2019 Wantedly, Inc. "CPVUNF • ໦ଜ ॣੜ @hachiblogʢ͸ͪ΅ʔʣ • Wantedly

    Inc. ͷ৽ଔʁ ̍೥໨ • goྺ൒೥ఔ౓ • ΧϝϥɺμΠϏϯά • ΩʔϘʔυࣗ࡞͍ͨ͠ͻͱ
  2. ©2019 Wantedly, Inc. 1. TinyGoͱ͸ • TinyGoͷಛ௃ • TinyGoͰͰ͖Δ͜ͱ 2.

    TinyGoͰWasmΛ࡞Δ • ͳͥTinyGoͳͷ͔ • WasmΛ࡞ͬͯΈΔ Agenda
  3. ©2019 Wantedly, Inc. 5JOZ(PͰ8BTNΛ࡞Δ ΍ͬͨ͜ͱ 1. jsͰbase64Τϯίʔυ 2. wasmʹ౉ͯ͠σίʔυ •

    ͜͜Ͱॏ͍ॲཧΛhogehoge͢Δ 3. base64ʹ࠶Τϯίʔυͯ͠jsʹฦ͢
  4. ©2019 Wantedly, Inc. 5JOZ(PͰ8BTNΛ࡞Δ ΍ͬͨ͜ͱ 1. jsͰbase64Τϯίʔυ 2. wasmʹ౉ͯ͠σίʔυ •

    ͜͜Ͱॏ͍ॲཧΛhogehoge͢Δ 3. base64ʹ࠶Τϯίʔυͯ͠jsʹฦ͢
  5. ©2019 Wantedly, Inc. 5JOZ(PͰ8BTNΛ࡞Δ ΍ͬͨ͜ͱ 1. jsͰbase64Τϯίʔυ 2. wasmʹ౉ͯ͠σίʔυ •

    ͜͜Ͱॏ͍ॲཧΛhogehoge͢Δ 3. base64ʹ࠶Τϯίʔυͯ͠jsʹฦ͢ function includeFile() { var obj = document.getElementById('getImage') var cvs = document.createElement('canvas'); cvs.width = obj.width; cvs.height = obj.height; var ctx = cvs.getContext('2d'); ctx.drawImage(obj, 0, 0); var data = cvs.toDataURL('image/jpeg'); document.getElementById('hidden').textContent = data.replace(/^data.*base64,/, '');; wasm.exports.convertFile(); }
  6. ©2019 Wantedly, Inc. 5JOZ(PͰ8BTNΛ࡞Δ ΍ͬͨ͜ͱ 1. jsͰbase64Τϯίʔυ 2. wasmʹ౉ͯ͠σίʔυ •

    ͜͜Ͱॏ͍ॲཧΛhogehoge͢Δ 3. base64ʹ࠶Τϯίʔυͯ͠jsʹฦ͢
  7. ©2019 Wantedly, Inc. 5JOZ(PͰ8BTNΛ࡞Δ wasmʹ౉ͨ͠ͱ͖ͷॲཧ import ( "syscall/js" "encoding/base64" "image/jpeg"

    "bytes" ) func convertFile() { global := js.Global() document := global.Get("document") image_string := document.Call("getElementById", "hidden").Get("textContent").String() data, _ := base64.StdEncoding.DecodeString(image_string) jpgI, _ := jpeg.Decode(bytes.NewReader(data)) //ॏ͍ॲཧ var buffer bytes.Buffer jpeg.Encode(&buffer,jpgI,nil) encode_string := base64.StdEncoding.EncodeToString(buffer.Bytes()) image_viewer := document.Call("getElementById", "hidden-reader") global.Call("eval","console.log('test')") image_viewer.Set("textContent",encode_string) }
  8. ©2019 Wantedly, Inc. 5JOZ(PͰ8BTNΛ࡞Δ wasmʹ౉ͨ͠ͱ͖ͷॲཧ import ( "syscall/js" "encoding/base64" "image/jpeg"

    "bytes" ) func convertFile() { global := js.Global() document := global.Get("document") image_string := document.Call("getElementById", "hidden").Get("textContent").String() data, _ := base64.StdEncoding.DecodeString(image_string) jpgI, _ := jpeg.Decode(bytes.NewReader(data)) //ॏ͍ॲཧ var buffer bytes.Buffer jpeg.Encode(&buffer,jpgI,nil) encode_string := base64.StdEncoding.EncodeToString(buffer.Bytes()) image_viewer := document.Call("getElementById", "hidden-reader") global.Call("eval","console.log('test')") image_viewer.Set("textContent",encode_string) }
  9. ©2019 Wantedly, Inc. 5JOZ(PͰ8BTNΛ࡞Δ wasmʹ౉ͨ͠ͱ͖ͷॲཧ import ( "syscall/js" "encoding/base64" "image/jpeg"

    "bytes" ) func convertFile() { global := js.Global() document := global.Get("document") image_string := document.Call("getElementById", "hidden").Get("textContent").String() data, _ := base64.StdEncoding.DecodeString(image_string) jpgI, _ := jpeg.Decode(bytes.NewReader(data)) //ॏ͍ॲཧ var buffer bytes.Buffer jpeg.Encode(&buffer,jpgI,nil) encode_string := base64.StdEncoding.EncodeToString(buffer.Bytes()) image_viewer := document.Call("getElementById", "hidden-reader") global.Call("eval","console.log('test')") image_viewer.Set("textContent",encode_string) }
  10. ©2019 Wantedly, Inc. ίϯύΠϧΤϥʔ 5JOZ(PͰ8BTNΛ࡞Δ ΍ͬͨ͜ͱ > tinygo build -o

    src/wasm.wasm -target=wasm src/export/wasm.go usr/local/go/src/image/ycbcr.go:176:20: todo: full slice expressions (with max): []byte