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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從 Go 中調(diào)用 Rust 函數(shù),并使用字符串作為參數(shù)?

如何從 Go 中調(diào)用 Rust 函數(shù),并使用字符串作為參數(shù)?

Go
夢(mèng)里花落0921 2022-08-01 17:00:49
我一直在嘗試將字符串傳遞給Rust函數(shù)(編譯為Wasm),但是對(duì)于我的理解,現(xiàn)在沒(méi)有辦法直接傳遞字符串,因?yàn)椤皊tr”不是“FFI世界”中的類型(至少這是rust編譯器所說(shuō)的):= help: consider using `*const u8` and a length instead所以我所做的是將函數(shù)更改為此形式(而不是使用簡(jiǎn)單的 &str 類型):#[no_mangle]pub extern "C" fn greet(s: *mut u8, len: usize) {    let s = std::str::from_utf8(unsafe { std::slice::from_raw_parts(s, len) }).unwrap();    println!("Hello, {}!", s)}這意味著我需要一個(gè)指針和u8中的字符串長(zhǎng)度。但是,有人讓我注意到WASM模塊是沙盒的,因此它們不能像普通應(yīng)用程序那樣使用普通指針。因此,我必須使用這樣的函數(shù)將內(nèi)存分配到模塊的線性內(nèi)存中:use std::alloc::{alloc, dealloc, Layout};#[no_mangle]pub unsafe fn my_alloc(len: usize) -> *mut u8 {    let align = std::mem::align_of::<usize>();    let layout = Layout::from_size_align_unchecked(size, align);    alloc(layout)}這是一個(gè) JS 函數(shù)的示例,它使用如下的分配函數(shù):function copyMemory(data, instance) {  var ptr = instance.exports.alloc(data.length);  var mem = new Uint8Array(instance.exports.memory.buffer, ptr, data.length);  mem.set(new Uint8Array(data));  return ptr;}我的問(wèn)題是我不知道如何將這個(gè)函數(shù)轉(zhuǎn)換為Go,那是因?yàn)槲冶焕г凇皏ar mem”行,原因如下:我在Go中找不到“instance.exports.memory.buffer”的等效項(xiàng)(實(shí)例是“*wasmtime.實(shí)例“類型)。我不知道如何做Unit8Buffer在Go中所做的。關(guān)于Wasm中記憶的好讀:(https://radu-matei.com/blog/practical-guide-to-wasm-memory/#exchanging-strings-between-modules-and-runtimes)
查看完整描述

1 回答

?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

我花了一點(diǎn)時(shí)間來(lái)確切地理解wasmtime Go-package是如何工作的,但最終我解決了我這樣做的問(wèn)題:


func main() {

    dir, err := ioutil.TempDir("", "out")

    check(err)

    defer os.RemoveAll(dir)

    stdoutPath := filepath.Join(dir, "stdout")


    engine := wasmtime.NewEngine()

    store := wasmtime.NewStore(engine)


    linker := wasmtime.NewLinker(store)


    // Configure WASI imports to write stdout into a file.

    wasiConfig := wasmtime.NewWasiConfig()

    wasiConfig.SetStdoutFile(stdoutPath)



    wasi, err := wasmtime.NewWasiInstance(store, wasiConfig, "wasi_snapshot_preview1")

    check(err)


    // Link WASI

    err = linker.DefineWasi(wasi)

    check(err)


    // Create our module

    module, err := wasmtime.NewModuleFromFile(store.Engine, "wasm_file.wasm")

    check(err)

    instance, err := linker.Instantiate(module)

    check(err)



    fn := instance.GetExport("greet").Func()

    memory := instance.GetExport("memory").Memory()

    alloc := instance.GetExport("my_alloc").Func()


    // // string for alloc

    size2 := int32(len([]byte("Elvis")))


    // //Allocate memomory

    ptr2, err := alloc.Call(size2)

    pointer, _ := ptr2.(int32)


    buf := memory.UnsafeData()

    for i, v := range []byte("Elvis") {

        buf[pointer+int32(i)] = v

    }


    // Use string func

    _, err = fn.Call(pointer, size2 )

    check(err)

    

    // Print WASM stdout

    out, err := ioutil.ReadFile(stdoutPath)

    check(err)

    fmt.Print(string(out))

}


查看完整回答
反對(duì) 回復(fù) 2022-08-01
  • 1 回答
  • 0 關(guān)注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)