从 Wasm 模块调用 JS 函数
Calling JS function from Wasm module
我用 C 编写了一个程序,用于打印不超过给定数字的质数。
我想将其编译为 WebAssembly,每次当 isPrime() 为真时,我想调用 JS 函数 "document.write(i + ">br>")" 只是为了在浏览器中打印素数。所以实际上我想从 wasm 模块调用 JS 函数。
我知道这个工具:https://wasdk.github.io/WasmFiddle/ 用于从 C 编译到 wasm。
提前感谢您的帮助。
#include <stdio.h>
#include <math.h>
int isPrime(num) {
int i;
if(num == 2) return 1;
if(num % 2 == 0) return 0;
int sq = (int) sqrt(num) + 1;
for(i = 3; i < sq; i = i + 2) if(num % i == 0) return 0;
return 1;
}
void printPrimes(int n){
int i;
for(i = 2; i <= n; i++)
if(isPrime(i))
/* here I want to call: JSfunction->document.write(i + "<br>");*/
}
emscripten 文档描述了您可以在 JavaScript 和 Webassembly 之间进行交互的几种不同方式:
https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html.
我用 C 编写了一个程序,用于打印不超过给定数字的质数。
我想将其编译为 WebAssembly,每次当 isPrime() 为真时,我想调用 JS 函数 "document.write(i + ">br>")" 只是为了在浏览器中打印素数。所以实际上我想从 wasm 模块调用 JS 函数。
我知道这个工具:https://wasdk.github.io/WasmFiddle/ 用于从 C 编译到 wasm。
提前感谢您的帮助。
#include <stdio.h>
#include <math.h>
int isPrime(num) {
int i;
if(num == 2) return 1;
if(num % 2 == 0) return 0;
int sq = (int) sqrt(num) + 1;
for(i = 3; i < sq; i = i + 2) if(num % i == 0) return 0;
return 1;
}
void printPrimes(int n){
int i;
for(i = 2; i <= n; i++)
if(isPrime(i))
/* here I want to call: JSfunction->document.write(i + "<br>");*/
}
emscripten 文档描述了您可以在 JavaScript 和 Webassembly 之间进行交互的几种不同方式:
https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html.