Invoking a JavaScript function from a WebAssembly module

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>");*/
}

Answer №1

The documentation for emscripten outlines various methods for communication between JavaScript and WebAssembly:

Check it out here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Delaying loops with JQuery Ajax

I am attempting to implement a delay in my AJAX data processing so that the loop runs a bit slower. Here's the code I'm working with: $(document).ready(function (){ $('#button').click(function(){ $('#hide').show ...

Regex: Enabling commas in the name of an Excel file

My JavaScript code is set up to extract data from an excel file. As a first step, I define a regular expression and assign it to a variable named regex var regex = /^([a-zA-Z0-9\s_!()\\.\-:])+(.xls|.xlsx)$/; Following this, there is s ...

Navigating a path and executing unique functions based on varying URLs: A guide

I am trying to send a post request to the path /users and then right away send another post request to /users/:id. However, I need the actions to be different for each of these URLs, so I cannot use the array method to apply the same middleware. The goal ...

I am struggling to set up angular-localstorage4

I have been following the instructions in the provided link: angular-localstorage4 When attempting to import WebStorageModule and LocalStorageService from angular-localstorage, I encounter an error in the console even though the compilation is successful. ...

Using classic ASP to populate a JavaScript array from a VBScript drop-down menu

To create a JavaScript array based on the selected values from multiple drop down lists, each with the same name due to being generated in a for loop, the following code snippet is currently being used: <script language="JavaScript"> var array ...

Instructions on how to open the <details> element from a link on a different page

Imagine a scenario where you have a Home Page with a link to a second page that features some content enclosed by a <details> element. However, the <details> element on the second page is initially closed, and you wish for the link on the home ...

Effortless integration of file-watching functionality

Currently, I have a basic application that sends XML data to an SQS Queue. In order to enhance the functionality, I am searching for a straightforward fileWatcher solution in node.js that will provide more detailed information than what is obtained through ...

When clicking, the drop-down feature is malfunctioning as all the menus are dropping down simultaneously

I have been trying to implement a dropdown on click feature for my website, but it is not functioning as intended. () When I click on button 1, button 2 also drops down unexpectedly. This behavior is not desired. Below is the code snippet from the page: ...

Incorporating map reduce and other methods, is it possible to locate the initial item that meets specific criteria within a nested array and terminate the search upon discovery?

How can you locate the first item that meets specific criteria within a nested array and halt the search once it is found? In a one-dimensional array, this task can be accomplished with the Array.find function. But how would you tackle this in a two-dimen ...

Ways to align and fix a button within a specific div element

How can I make the button with position: fixed property only visible inside the second div and not remain fixed when scrolling to the first or last div? .one{ height:600px; width: 100%; background-color: re ...

Challenges with Hangman in JavaScript

As a beginner in JavaScript, I recently developed a simple hangman-like game. However, I encountered some challenges that I need help with. One issue is related to my lettersGuessed array. Currently, the array displays every time a key is pressed and repea ...

What can be done to resolve the error message "Matchong query does not exist"?

Recently, I've been working on developing a like system similar to Instagram's using Django and Ajax. However, after completing the code, I encountered a frustrating error message that read "Post matching query does not exist." Despite thoroughly ...

Trouble in the pipeline: C pipes are failing to

For a coding assignment, I am tasked with setting up a ring of processes using fork() and passing a message through the ring. The current issue is that I cannot successfully pass a message from the initial process to its immediate child. This could be due ...

Creating a variable by using a conditional operation in JavaScript

When the statement <code>name = name || {} is used, it throws a reference error. However, using var name = name || {} works perfectly fine. Can you explain how variable initialization in JavaScript functions? ...

Is it possible to compress a 6-byte integer into a 4-byte integer?

In my C code application, I am currently receiving a 4 byte integer from an external module and using it throughout the application. However, the module has now started sending a 6 byte integer instead. Unfortunately, I am unable to make changes to my ap ...

The implicit wait feature in WebDriver JavaScript seems to be ineffective

Waiting for the error message to appear seems to be a bit tricky. I tried using browser.driver.manage().timeouts().implicitlyWait() but had to resort to using browser.driver.sleep() instead. this.getErrorMessage = function () { var defer = protracto ...

Is it possible to execute this animation with a single click for repetitive playback?

CODEPEN const btt = document.querySelector('.btt'); btt.addEventListener('click', function(){ this.classList.toggle('anime'); }); Is there a way to achieve the desired effect with just one click? ...

Unable to utilize angular-local-storage

Here is the code snippet I'm working with: angular.module('MyModule').controller('MyController', ['$scope', '$stateParams','$location', '$http','LocalStorageModule', function($s ...

Mastering the fundamentals of C programming will help me gain a better understanding of arrays and strings

I'm relatively new to programming in C, so please excuse this potentially beginner question :P . Imagine I have an array arr[] containing numbers ranging from 100 to 999. My task is to extract each digit of the array elements and find the difference ...

Discover every user receiving a database object instead of individual records with mLab

While using my express application with the mLab database, I encountered an issue. When trying to find only one record, everything works fine. However, when attempting to retrieve a list of users from the database, I receive the following response. Query ...