我如何使用Emscripten和WebAssembly返回C ++中构造的向量

问题描述 投票:0回答:1
我想返回JavaScript在C ++中创建的类型,称为PlayerAction。 PlayerAction有两个元素,播放器,类型INT和操作,类型INT_64

我试图以下面的方式进行操作,但它不起作用,它返回一个怪异的对象。 我的C ++中的Code Bellow:

#include <emscripten/bind.h> #include <vector> using Player = int; using Action = int64_t; struct PlayerAction { Player player; Action action; bool operator==(const PlayerAction& other) const { return player == other.player && action == other.action; } }; std::vector<PlayerAction> getAction() { std::vector<PlayerAction> action; PlayerAction action1 = {0, 2}; PlayerAction action2 = {1, 4}; action.push_back(action1); action.push_back(action2); return action; } // Bindings EMSCRIPTEN_BINDINGS(my_module) { emscripten::value_object<PlayerAction>("PlayerAction") .field("player", &PlayerAction::player) .field("action", &PlayerAction::action); emscripten::function("getAction", &getAction); emscripten::register_vector<PlayerAction>("vector<PlayerAction>"); }

HTML和JS:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Exemplo Emscripten</title>
    
</head>
<body>
    <script>
        var Module = {
            onRuntimeInitialized: function() {
                console.log(Module.getAction());
            }
        }
    </script>
    <script src="exemplo.js"></script>
</body>
</html>

现在这是一个非常古老的问题,但是答案是您要获得的Emscripten对象不是JavaScript数组,而是一种特殊的向量对象,您需要使用get()访问元素。
    
c++ webassembly emscripten
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.