0

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

这两个类首先构建为静态库uuid_lib.a,然后将其链接到后一个 C++ 源文件并使用em++ --bind -o uuid_module.js uuid_bind.cpp uuid_lib.a(由 CMake 生成)构建以生成uuid_module.jsuuid_module.wasm. 现在,我该怎么处理这些?

Embind 上的文档有些稀疏,只说

生成的quick_example.js文件可以作为节点模块或通过<script>标签加载:...

我找到了这个关于结合 Emscripten/Embind 和 node.js 的 Google 教程,并且我已经尽可能多地复制了它(不包括 Docker 上的位,因为我的 Linux 发行版直接为 Emscripten 服务)。我有一个文件index.html和一个package.json文件,以及从 Chrome 运行的 launch npm testhttp-server

我的印象是 Emscripten/Embind 只是充当任何绑定类、函数(静态或其他)、变量的翻译层,并且可以直接从 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);
};
4

1 回答 1

0

我在这里错过了什么吗?

您缺少的是在-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
于 2021-06-04T20:36:21.717 回答