基于WebAssembly 的H.265 Web播放器——wasm导出的addFunction设置异步回调的方法

Calling JavaScript functions as function pointers from C

You can use addFunction to return an integer value that represents a function pointer. Passing that integer to C code then lets it call that value as a function pointer, and the JavaScript function you sent to addFunction will be called.

See test_add_function in tests/test_core.py for an example.

You should build with -s ALLOW_TABLE_GROWTH to allow new functions to be added to the table. Otherwise by default the table has a fixed size.

Note

When using addFunction on LLVM wasm backend, you need to provide an additional second argument, a Wasm function signature string. Each character within a signature string represents a type. The first character represents the return type of a function, and remaining characters are for parameter types.

  • 'v': void type

  • 'i': 32-bit integer type

  • 'j': 64-bit integer type (currently does not exist in JavaScript)

  • 'f': 32-bit float type

  • 'd': 64-bit float type

For example, if you add a function that takes an integer and does not return anything, you can do addFunction(your_function, 'vi');. See tests/interop/test_add_function_post.js for an example.

 

可以使用addFunction返回一个表示函数指针的整数值。将该整数传递给C代码,然后让它将该值作为函数指针调用,然后调用发送到addFunction的JavaScript函数。

 

在LLVM wasm后端使用addFunction时, 需要提供另一个参数wasm函数签名字符串。签名字符串中的 每个字符代表一种类型第一个字符表示函数的返回类型其余字符表示参数类型。

 

Module.addFunction(()=>{

}, "sig");

 

回调函数参数类型:


typedef void (*VideoCallbackN)(unsigned char *data_y, unsigned char *data_u, unsigned char *data_v, int line1, int line2, int line3, int width, int height, long pts);

 

sig:'viiiiiiiid'  —— 错误的!

 

 

sig:'viiiiiiiii'  —— 正确的!

 

——记一次耗时的debug过程

 

https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,确保你的项目已经正确地链接了 Emscripten SDK。接下来,在 CMakeLists.txt 中添加以下内容: ```cmake cmake_minimum_required(VERSION 3.0) project(your_project) # 设置编译选项 set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) # 添加 Emscripten 编译器 SET(CMAKE_C_COMPILER "emcc") SET(CMAKE_CXX_COMPILER "em++") # 添加 Emscripten 编译选项 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM=1 -s USE_SDL=2 -s USE_WEBGL2=1 -s FULL_ES3=1 -s USE_GLFW=3") # 添加源代码 add_executable(your_project main.cpp) # 导出 C 函数为 JavaScript 模块 set_target_properties(your_project PROPERTIES LINK_FLAGS "-s EXPORTED_FUNCTIONS='[\"_your_c_function\"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\", \"cwrap\"]'") # 指定输出文件名 set_target_properties(your_project PROPERTIES OUTPUT_NAME "your_project") # 指定输出文件类型为 JavaScript 模块 set_target_properties(your_project PROPERTIES SUFFIX ".js") # 拷贝输出文件到指定目录 add_custom_command(TARGET your_project POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:your_project> ${CMAKE_CURRENT_SOURCE_DIR}/output/) ``` 以上代码做了以下几件事: 1. 设置编译选项,包括编译标准、编译器等等。 2. 添加 Emscripten 编译器和编译选项。 3. 添加源代码。 4. 导出 C 函数为 JavaScript 模块,这里的 `_your_c_function` 是你需要导出的函数名。 5. 指定输出文件名为 `your_project.js`。 6. 拷贝输出文件到指定目录。 最后,你可以通过以下命令来生成 WebAssembly 项目: ```bash mkdir build && cd build cmake .. make ``` 如果一切顺利,你将得到 `your_project.js` 和 `your_project.wasm` 两个文件,其中前者是 JavaScript 模块,后者是 WebAssembly 模块。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xw-何妨吟啸且徐行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值