我正在尝试使用 add 函数从 C++ 类生成一个极其简单的 WebAssembly 模块。这是源 C++ 类和相应的 djinni 文件
// ***src/MathUtils.cpp***
#include "MathUtils.hpp"
class MathUtils_ : public MathUtils {
public:
double add_fff64(double a, double b) override {
return a + b;
}
};
// ***djinni/MathUtils.djinni***
MathUtils = interface +c {
add_fff64(a: f64, b: f64): f64;
}
使用 Snapchat/Djinni 的 wasm 界面,我生成了以下文件:
// ***src/MathUtils.hpp***
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from MathUtils.djinni
#pragma once
class MathUtils {
public:
virtual ~MathUtils() = default;
virtual double add_fff64(double a, double b) = 0;
};
// ***wasm/MathUtils.hpp***
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from MathUtils.djinni
#pragma once
#include "src/MathUtils.hpp"
#include "/Users/sam/snap.djinni/support-lib/wasm/djinni_wasm.hpp"
namespace djinni_generated {
struct MathUtils : ::djinni::JsInterface<::MathUtils, MathUtils> {
using CppType = std::shared_ptr<::MathUtils>;
using CppOptType = std::shared_ptr<::MathUtils>;
using JsType = em::val;
using Boxed = MathUtils;
static CppType toCpp(JsType j) { return _fromJs(j); }
static JsType fromCppOpt(const CppOptType& c) { return {_toJs(c)}; }
static JsType fromCpp(const CppType& c) {
::djinni::checkForNull(c.get(), "MathUtils::fromCpp");
return fromCppOpt(c);
}
static em::val cppProxyMethods();
static double add_fff64(const CppType& self, double w_a,double w_b);
};
} // namespace djinni_generated
// ***wasm/MathUtils.cpp***
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from MathUtils.djinni
#include "wasm/MathUtils.hpp" // my header
namespace djinni_generated {
em::val MathUtils::cppProxyMethods() {
static const em::val methods = em::val::array(std::vector<std::string> {
"addFff64",
});
return methods;
}
double MathUtils::add_fff64(const CppType& self, double w_a,double w_b) {
try {
auto r = self->add_fff64(::djinni::F64::toCpp(w_a),
::djinni::F64::toCpp(w_b));
return ::djinni::F64::fromCpp(r);
}
catch(const std::exception& e) {
return ::djinni::ExceptionHandlingTraits<::djinni::F64>::handleNativeException(e);
}
}
EMSCRIPTEN_BINDINGS(_MathUtils) {
em::class_<::MathUtils>("MathUtils")
.smart_ptr<std::shared_ptr<::MathUtils>>("MathUtils")
.function("nativeDestroy", &MathUtils::nativeDestroy)
.function("addFff64", MathUtils::add_fff64)
;
}
} // namespace djinni_generated
由此,我尝试生成一个简单的本机 JavaScript 模块,该模块允许我调用类中的 add 函数。
我已经有效地尝试使用每个可以想象的相关标志运行以下脚本的多个版本:
emcc wasm/MathUtils.cpp src/MathUtils.cpp /Users/sam/snap.djinni/support-lib/wasm/djinni_wasm.cpp -o out/MathUtils.js --bind
虽然这确实已编译成 JavaScript 输出,但MathUtils
中没有
add_fff64
类或任何MathUtils.js
函数。事实上,当我尝试在通用本机 html 文件中调用 add_fff64
函数时,我收到错误。
这对我来说特别奇怪,因为当使用
wasm-objdump
和wasm2wat
等工具调试生成的WebAssembly文件时,MathUtils
类和addFff64
都出现了。
- 0010490: 7562 6c65 3e00 6164 6446 6666 3634 0048 uble>.addFff64.H
网上教程和示例使它看起来很简单,但我还没有找到有人遇到这个问题。不太确定我做错了什么 - 我是 emscripten 和 djinni 的新手,所以即使是任何调试建议也会很棒!
设法找到一个解决方案,在此存储库中进行了描述和突出显示:github.com/SamKouteili/DjinniWasmDemo