本文介绍了如何在WebAssembly中使用(C ++ STL的)向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <iostream>
#include<vector>
using namespace std;
vector<int> ver;
int pushData(int n)
{
for(int i=0;i<n;i++)
{
ver.push_back(i);
}
}
我想从JS调用pushData函数并推送一些数据矢量 ver,以后再使用。
请解释如何使用WebAssembly。
I want to call pushData function from JS and push some data to vector "ver" and use it later.please explain how to do that using WebAssembly.
推荐答案
我尝试通过使用以下答案回答您的问题:
I tried to answer your question, by using this answer:https://stackoverflow.com/a/46748966/544721
进行求解:
#include<vector>
const int SIZE = 10;
std::vector<int> data(10);
void add(int value) {
for (int i=0; i<SIZE; i++) {
data[i] = data[i] + value;
}
}
int* getData() {
return &(data[0]);
}
和js:
var wasmModule = new WebAssembly.Module(wasmCode);
var wasmInstance = new WebAssembly.Instance(wasmModule, wasmImports);
var offset = wasmInstance.exports.getData();
var linearMemory = new Uint32Array(wasmInstance.exports.memory.buffer, offset, 10);
for (var i = 0; i < linearMemory.length; i++) {
linearMemory[i] = i;
}
wasmInstance.exports.add(10);
for (var i = 0; i < linearMemory.length; i++) {
log(linearMemory[i]);
}
WasmFiddle:
WasmFiddle: https://wasdk.github.io/WasmFiddle//?wuycy
但是似乎有一些链接器错误:
But it looks like there is some linker error:
line 2: Uncaught LinkError: WebAssembly Instantiation: Import #9 module="env" function="__dso_handle" error: global import must be a number
任何人都可以帮助制作此C ++代码以在WasmFiddle中运行?
Can anyone help to make this C++ code to run in WasmFiddle?
这篇关于如何在WebAssembly中使用(C ++ STL的)向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!