wasm使用心得

#define WASM_API extern "C" EMSCRIPTEN_KEEPALIVE

WASM_API int getUnsignedChar(unsigned char *unsignedStr);

int str2int(char *str)

{

    if (str == NULL)

        return 0;



    int num = atoi(str);

    return num;

}

使用EMSCRIPTEN_KEEPALIVE可以简单导出函数,提供调用。

EMSCRIPTEN_BINDINGS(TestClass_bindings)

{

    emscripten::class_<TestClass>("TestClass")

        .constructor<>()

        .constructor<long, int>()

        .function("setChar", &TestClass::setChar)

        .function("print", &TestClass::print);

}

class TestClass

{

public:

    const char *str;

    int size;



    TestClass()

    {

        str = NULL;

        size = 0;

    }



    TestClass(long str, int size)

    {

        this->str = (char *)str;

        this->size = size;

    }



    void setChar(long str,int size)

    {

        this->str = (char*)str;

        this->size = size;

    }



    void print()

    {

        cout << "Received file data (" << size << " bytes): " << std::endl;

        cout.write(str, size);

    }

};

使用EMSCRIPTEN_BINDINGS可以导出类,提供调用或者构造对象。

此时编译使用的emcc命令需要加上 --bind,否则编译不成功。

并且,由于webassembly不支持指针(具体哪些没研究,反正不支持,有懂得大佬教教)

类成员函数绑定时,参数不可为指针,否则会报错。

如果非要用指针怎么办?

目前我的解决方法是 js把地址用long类型传给c++,再强制转换成指针类型。

EmbData(long ptrCtrl1, long ptrCtrl2)
    {
        m_line1 = (PointI *)ptrCtrl1;
        m_line2 = (PointI *)ptrCtrl2;
    }
EMSCRIPTEN_BINDINGS(bindings)
{
    class_<EmbData>("EmbData")
        .constructor<>()
        .constructor<long, long>();
}

并且EMSCRIPTEN_BINDINGS也无法导出类的指针类型的成员变量,这时候可以为指针变量设置

get函数与set函数,以便操作。

class Data
{
public:
    Data()
    {
    }
PointI *m_ptr;

PointI *PointIGetter() const//get函数必须加const
	{
		return m_ptr;
	}

	void PointISetter(CFPointI *m_ptr)
	{
		this->m_ptr = m_ptr;
	}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值