【问题标题】:How to use vectors (of C++ STL) with WebAssembly如何在 WebAssembly 中使用(C++ STL 的)向量
【发布时间】:2018-05-07 19:14:13
【问题描述】:
#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 做到这一点。

【问题讨论】:

    标签: c++14 webassembly


    【解决方案1】:

    我正在做和你一样的事情。在我看来,在 WASM 中使用 STL 非常困难。

    我的解决方案是创建一个模拟向量。Wasm 只支持 int32、int64、float32 和 float64,并且 wasm 的地址与其他进程不同。所以直接导入库是不可行的。可以通过代理或转换调用库函数,也可以自己写。

    在这种情况下,vector不能直接导入。可以创建一个名为vector的类,并实现push_back函数。

    class vector{
    public:
        bool push_back(int i){
           // do something
        }
        int& at(uint index){
           // do something
        }
    private:
        int* int_ptr;
    }
    

    更多细节在这里 https://aransentin.github.io/cwasm/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 2011-03-07
      相关资源
      最近更新 更多