簡體   English   中英

從 Wasm 模塊調用 JS function

[英]Calling JS function from Wasm module

我在 C 中編寫了一個程序,用於打印直到給定數字的素數。
我想將它編譯為 WebAssembly,每次當 isPrime() 為真時,我想調用 JS function "document.write(i + ">br>")" 只是為了在瀏覽器中打印素數。 所以實際上我想從 wasm 模塊調用 JS function 。
我知道這個工具: 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM