I created a program in C to generate prime numbers up to a specified value.
My goal is to convert this program into WebAssembly and integrate it with JavaScript so that every time the function isPrime() returns true, I can invoke a JS function "document.write(i + ">br>")" to display the prime numbers on the browser. Essentially, I am looking to make calls to a JS function from within the wasm module.
I am aware of a useful tool called , which can be used for compiling C code to wasm format.
Thank you in advance for any assistance provided.
#include <stdio.h>
#include <math.h>
int checkPrime(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 displayPrimes(int n){
int i;
for(i = 2; i <= n; i++)
if(checkPrime(i))
/* here I want to trigger: JSfunction->document.write(i + "<br>");*/
}