如何使用 Emscripten 和 Embind 发出的 .js 和 .wasm 工件?

How to use the .js and .wasm artifacts emitted by Emscripten and Embind?

我有两个相当简单的 C++ class 定义及其接口,uuid.{hpp,cpp}uuid_util.{hpp,cpp},我还有一个 uuid_bind.cpp#include <emscripten/bind.h> 的文件将 C++ classes、函数和静态函数定义绑定到 JavaScript.

这两个class首先构建为静态库uuid_lib.a,然后链接到后者的C++源文件并使用em++ --bind -o uuid_module.js uuid_bind.cpp uuid_lib.a(CMake生成)构建到生成 uuid_module.jsuuid_module.wasm。现在,我该如何处理这些?

documentation on Embind有点稀疏,只说

The resulting quick_example.js file can be loaded as a node module or via a <script> tag: ...

我发现 this Google tutorial 结合 Emscripten/Embind 和 node.js,我已经尽可能地复制了它(不包括 Docker 上的位,因为我的 Linux 发行版直接服务于 Emscripten)。我有一个 index.html 和一个 package.json 文件,npm test 启动 http-server,我从 Chrome.

运行

我的印象是 Emscripten/Embind 将简单地充当任何绑定 classes、函数(静态或其他)、变量的转换层,并且可以直接调用来自 JavaScript,但事实并非如此。我在这里错过了什么吗?我对 JS 不是很熟悉。我想在 index.js 中做的就是:

index.js

import uuid_module from './uuid_lib.js';

console.log(uuid_module.uuid_util.generate_type1_uuid("BLA"));
// ... other calls

和 运行 这与 node index.js 一起将 UUID 字符串打印到控制台。


作为背景,我在下面提供了我的 CMakeLists.txtuuid_bind.cpp

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

# Set project file
project(
    uuid_generator
    VERSION 1.0
    DESCRIPTION "A UUID generator"
    LANGUAGES CXX)

# Export compile commands for clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Add Emscripten CMake toolchain file
if(EMSCRIPTEN)
    message("Using Emscripten.")
    set(CMAKE_TOOLCHAIN_FILE
        ${PROJECT_SOURCE_DIR}/deps/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake)
else()
    message("Using system compilers.")
endif(EMSCRIPTEN)

# C++20 guaranteed, no extensions eg. GNU/MSVC
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Strict compilation for MSVC or GCC/Clang
if(MSVC)
    add_compile_options(/W4 /WX)
else()
    add_compile_options(-Wall -Wextra -pedantic)
endif()

# Add uuid_generator as a shared library
add_library(uuid_lib STATIC src/uuid.cpp src/uuid_util.cpp)

# Set C++ library properties: output name, location, etc
set_target_properties(
    uuid_lib
    PROPERTIES OUTPUT_NAME uuid_lib
               PREFIX ""
               ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/dist)

# Set JS binding application properties: set bind, output type, etc. Runs only if Emscripten is enabled.
if(DEFINED ENV{EMSDK})
    add_executable(uuid_module src/uuid_bind.cpp)
    set_target_properties(
        uuid_module
        PROPERTIES OUTPUT_NAME uuid_module
                   SUFFIX ".js"
                   RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/dist)
    target_include_directories(uuid_module
                               PRIVATE ${PROJECT_SOURCE_DIR}/deps/emsdk/upstream/emscripten/system/include)
    target_link_libraries(uuid_module uuid_lib)
    target_link_options(
        uuid_module
        PUBLIC
        $<$<CONFIG:DEBUG>:
        -v
        --bind
        -sEXCEPTION_DEBUG=1
        -sDEMANGLE_SUPPORT=1
        -sDISABLE_EXCEPTION_CATCHING=1
        -sWASM_BIGINT=1
        -sMODULARIZE=1
        -sEXPORT_ES6=1
        -sEXPORT_NAME=uuid_module
        >)
endif(DEFINED ENV{EMSDK})

uuid_bind.cpp

#include "uuid.hpp"
#include "uuid_util.hpp"

#include <cstdint>
#include <emscripten.h>
#include <emscripten/bind.h>
#include <string>
#include <iostream>

EMSCRIPTEN_BINDINGS(uuid_generator)
{
    emscripten::constant("UINT29_MAX", uuid_gen::uuid::UINT29_MAX);
    emscripten::constant("ICAO_ADDRESS_MAX", uuid_gen::uuid_util::ICAO_ADDRESS_MAX);
    emscripten::constant("MAC_ADDRESS_MAX", uuid_gen::uuid_util::MAC_ADDRESS_MAX);
    emscripten::constant("RANDOM_NAMESPACE_MAX", uuid_gen::uuid_util::RANDOM_NAMESPACE_MAX);

    emscripten::class_<uuid_gen::uuid>("uuid")
        .property("namespace_type", &uuid_gen::uuid::get_namespace_type)
        .property("namespace_value", &uuid_gen::uuid::get_namespace_value)
        .property("namespace_string", &uuid_gen::uuid::get_namespace_string)
        .property("to_string", &uuid_gen::uuid::to_string)
        .class_function("uniform_int_distr", &uuid_gen::uuid::uniform_int_dist);

    emscripten::class_<uuid_gen::uuid_util>("uuid_util")
        .class_function("generate_type1_uuid", &uuid_gen::uuid_util::generate_type1_uuid)
        .class_function("generate_type2_uuid", &uuid_gen::uuid_util::generate_type2_uuid)
        .class_function("generate_type3_uuid", &uuid_gen::uuid_util::generate_type3_uuid)
        .class_function("generate_type4_uuid", &uuid_gen::uuid_util::generate_type4_uuid)
        .class_function("generate_type5_uuid_str", emscripten::select_overload<uuid_gen::uuid(const std::string &)>(
                                                       &uuid_gen::uuid_util::generate_type5_uuid))
        .class_function("generate_type5_uuid_int", emscripten::select_overload<uuid_gen::uuid(std::uint32_t)>(
                                                       &uuid_gen::uuid_util::generate_type5_uuid))
        .class_function("generate_type6_uuid_str", emscripten::select_overload<uuid_gen::uuid(const std::string &)>(
                                                       &uuid_gen::uuid_util::generate_type6_uuid))
        .class_function("generate_type6_uuid_int", emscripten::select_overload<uuid_gen::uuid(std::uint64_t)>(
                                                       &uuid_gen::uuid_util::generate_type6_uuid))
        .class_function("generate_type7_uuid", &uuid_gen::uuid_util::generate_type7_uuid)
        .class_function("from_string", &uuid_gen::uuid_util::from_string)
        .class_function("is_valid_oper_agency", &uuid_gen::uuid_util::is_valid_oper_agency)
        .class_function("is_valid_loc_atm_id", &uuid_gen::uuid_util::is_valid_loc_atm_id)
        .class_function("is_valid_mac_address_string", &uuid_gen::uuid_util::is_valid_mac_address_string)
        .class_function("namespace_char_encode", &uuid_gen::uuid_util::namespace_char_encode)
        .class_function("namespace_char_decode", &uuid_gen::uuid_util::namespace_char_decode)
        .class_function("namespace_encode", &uuid_gen::uuid_util::namespace_encode)
        .class_function("namespace_decode", &uuid_gen::uuid_util::namespace_decode)
        .class_function("mac_address_encode", &uuid_gen::uuid_util::mac_address_encode)
        .class_function("mac_address_decode", &uuid_gen::uuid_util::mac_address_decode);
};

Am I missing out on something here?

您缺少的是,在 -s MODULARIZE=1 模式下,默认导出是一个工厂,而不是模块对象本身。

您需要先创建一个模块,然后您应该能够访问公开的属性:

import uuidModuleFactory from './uuid_lib.js';

const uuid_module = uuidModuleFactory(/* optional Emscripten config goes here */);

console.log(uuid_module.uuid_util.generate_type1_uuid("BLA"));
// ... other calls