第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Go中調用WebAssembly代碼時如何使用Go切片?

在Go中調用WebAssembly代碼時如何使用Go切片?

Go
夢里花落0921 2023-08-21 14:26:51
我想在 Go 中使用 WebAssembly 計算數(shù)組的總和:package mainimport (    "encoding/binary"    "encoding/hex"    "fmt"    wasm "github.com/wasmerio/go-ext-wasm/wasmer")const length int32 = 4func main() {    // Instantiate the module.    wasmbyte, _ := wasm.ReadBytes("test.wasm")    instance, _ := wasm.NewInstance(wasmbyte)    defer instance.Close()    hasmemory := instance.HasMemory()    fmt.Println("it has memory:", (hasmemory))    a := []int32{1, 2, 3, 4}    var i int32    var ptr [length]*int32    for i = 0; i < length; i++ {        ptr[i] = &a[i]        fmt.Printf("Value of a[%d] = %d\n", i, *ptr[i])        // pass int value        // lengths := binary.LittleEndian.Uint32(a)        // fmt.Printf("customLen=%d\n", int32(lengths))        // result := int32(lengths)        allocateResult, err := instance.Exports["bar"](*ptr[i], length)        if err != nil {            fmt.Println("error is here", err)        }        binary.LittleEndian.PutUint32(instance.Memory.Data()[0:4], uint32(length))        inputPointer := allocateResult.ToI32()        //  Write the subject into the memory.        memory := instance.Memory.Data()[inputPointer:]        binary.LittleEndian.Uint32(memory)        resp := hex.EncodeToString(memory)        fmt.Println("resp:", resp)    }}但這段代碼沒有給我預期的結果。我的代碼的邏輯是計算數(shù)組的總和。數(shù)組的值是在運行時給出的。我需要做哪些改變?我的 Rust 代碼看起來像這樣use std::os::raw::c_int;#[no_mangle]pub extern fn bar(my_array: *const c_int, length: c_int) -> *mut c_int{    let slice = unsafe { std::slice::from_raw_parts(my_array, length as usize) };    let resp: i32 = slice.iter().sum();    resp as *mut c_int}
查看完整描述

1 回答

?
慕雪6442864

TA貢獻1812條經(jīng)驗 獲得超5個贊

將數(shù)據(jù)復制到 WebAssembly 內存(例如 WebAssembly 內存地址 0):


    a := []int32{10, 20, 30, 40}

    // Copy data to wasm memory:

    bytes := instance.Memory.Data()

    for i, v := range a {

        binary.LittleEndian.PutUint32(bytes[4*i:], uint32(v))

    }

bar從 WebAssembly 實例獲取導出的函數(shù):


    bar := instance.Exports["bar"]

使用 WebAssembly 內存地址和長度調用導出的函數(shù):


    result, err := bar(0, 4)

    if err != nil {

        panic(err)

    }


    fmt.Println(result)

該main.go文件:


package main


import (

    "encoding/binary"

    "fmt"


    wasm "github.com/wasmerio/go-ext-wasm/wasmer"

)


func main() {

    // Instantiate the module.

    wasmbyte, err := wasm.ReadBytes("test.wasm")

    if err != nil {

        panic(err)

    }


    instance, err := wasm.NewInstance(wasmbyte)

    if err != nil {

        panic(err)

    }


    defer instance.Close()

    hasmemory := instance.HasMemory()

    fmt.Println("it has memory:", hasmemory)


    a := []int32{10, 20, 30, 40}

    // Copy data to wasm memory:

    bytes := instance.Memory.Data()

    for i, v := range a {

        binary.LittleEndian.PutUint32(bytes[4*i:], uint32(v))

    }


    bar := instance.Exports["bar"]


    result, err := bar(0, 4)

    if err != nil {

        panic(err)

    }


    fmt.Println(result)

}


輸出(go run main.go):


it has memory: true

100

以下src/lib.rs文件具有相同的 wasm 二進制文件(因為 Rust 切片只有指針和長度):


#[no_mangle]

pub extern "C" fn bar(slice: &[i32]) -> i32 {

    slice.iter().sum()

}

轉換為test.wasm:


rustc --target wasm32-unknown-unknown -O --crate-type=cdylib src/lib.rs  -o test.wasm


輸出轉換為test.wat( wasm2wat test.wasm -o test.wat) 以查看函數(shù)$bar參數(shù) ( param i32 i32):


(module

  (type (;0;) (func (param i32 i32) (result i32)))

  (func $bar (type 0) (param i32 i32) (result i32)

    (local i32)

    block  ;; label = @1

      local.get 1

      br_if 0 (;@1;)

      i32.const 0

      return

    end

    local.get 1

    i32.const 2

    i32.shl

    local.set 2

    i32.const 0

    local.set 1

    loop  ;; label = @1

      local.get 0

      i32.load

      local.get 1

      i32.add

      local.set 1

      local.get 0

      i32.const 4

      i32.add

      local.set 0

      local.get 2

      i32.const -4

      i32.add

      local.tee 2

      br_if 0 (;@1;)

    end

    local.get 1)

  (table (;0;) 1 1 funcref)

  (memory (;0;) 16)

  (global (;0;) (mut i32) (i32.const 1048576))

  (global (;1;) i32 (i32.const 1048576))

  (global (;2;) i32 (i32.const 1048576))

  (export "memory" (memory 0))

  (export "bar" (func $bar))

  (export "__data_end" (global 1))

  (export "__heap_base" (global 2)))


查看完整回答
反對 回復 2023-08-21
  • 1 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號