Is there a way to execute a cheerp-wasm program without using the 'path' module for loading?

One interesting feature of Cheerp is its cheerp-wasm target, which compiles C++ into both a .js file and its corresponding .wasm file. Essentially, the .js file acts as a loader for the WebAssembly code.

This particular loaderđź”— uses require("path") to bring in essential filesystem functionalities needed to load the .wasm file as a module. Unfortunately, the environment where my code operates (Screeps) does not support this "path" module.


[8:39:54 AM][shard3]Error: Unknown module 'path'
                        at Object.requireFn (<runtime>:31712:23)
                        at fetchBuffer (main:10:5)
                        at main:30:1
                        at main:42:3
                        at Object.exports.evalCode (<runtime>:15584:76)
                        at Object.requireFn (<runtime>:31730:28)
                        at Object.exports.run (<runtime>:31673:60)

Given that the Cheerp loader relies on functionality that I can't access, how can I properly load my wasm code?


Although Cheerp presents various flags that can be configured, none seem relevant to my current predicament.

Is there a way to direct Cheerp to simply call

bytecode = require("mycode.wasm")
and utilize that? Could Cheerp potentially incorporate the wasm directly as bytecode within the .js itself? Or should I consider developing my own custom loader instead?

Answer â„–1

Cheerp utilizes various methods to retrieve the wasm file depending on the environment.

The supported environments include: browser, node.js, d8, and js.

For the node environment, it assumes the presence of a path module for accessing the wasm from the filesystem.

If this module is not available in your current environment, there may be an issue.

We are working towards providing more flexibility for passing the wasm file in the future, but it poses a challenge to do so in a universal manner.

In the meantime, I can offer a temporary solution.

Here is the code segment responsible for fetching the wasm:

function fetchBuffer(p){
    var b=null,f='function';
    if(typeof fetch===f) {
        b=fetch(p).then(r=>r.arrayBuffer());
    } else if(typeof require===f) {
        p=require('path').join(__dirname, p);
        b=new Promise((y,n)=>{
            require('fs').readFile(p,(e,d)=>{
                if(e) n(e);
                else y(d);
            });
        });
    } else {
        b=new Promise((y,n)=>{
            y(read(p,'binary'));
        });
    }
    return b;
}

This function first attempts to use the fetch method if it's available.

If you're encountering issues due to its undefined status in your environment, you can define it manually before the cheerp code and implement it by using your own require("mycode.wasm").

Below is an example implementation (not tested):

function fetch(path) {
  return new Promise((y,n)=> {
    let ret = {
      arrayBuffer: () => {
        return require("mycode.wasm");
      }
    };
    y(ret);

  });
}

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

Create a Javascript list by using the append method

I am working on creating a list using Javascript and the Append function. In my project, I have an index.php file, script.js file, and a search.php file that executes the mysql query. Although everything is functioning properly, I am not satisfied with t ...

Customizing Bootstrap styles when toggling

I have been working on incorporating a dark/light theme into my project. By creating a separate SCSS file that is imported after the main Bootstrap file, I was able to override certain core Bootstrap styles to achieve a dark/light mode. Now, I am facing th ...

I desire to alter the description of an item within a separate div, specifically in a bootstrap

I used the map method to render all the images. However, I need a way to track the current image index so that I can change the item description located in another div. Alternatively, if you have any other solutions, please let me know. App.js : import Ca ...

Get rid of the unnecessary vertical line that is located at the end of the string

I have come across a situation similar to the one demonstrated in this code snippet: http://plnkr.co/edit/eBjenGqgo3nbnmQ7XxF1?p=preview Currently, I am working with AngularJS 1.5.7. The directives used in my input - ( first ) textarea are the same as tho ...

Data of an object disappears when it is passed to Meteor.call

In my React/Meteor project, I encountered an issue while trying to pass an object with data from the state to a method on the server for database insertion. Surprisingly, when the object is passed from the React component to the Meteor method, one of the c ...

When the pagecontainer is displayed

I am in the process of developing a JQM app. It is designed as a multiple page application that utilizes jquery mobile page divs to manage the visibility of pages during navigation. The layout of the pages looks something like this: <div data-role="p ...

At the beginning of the launch, the program ended unexpectedly with error code 0xc000013

As a student embarking on a solo journey to learn C++, I encountered an issue while working with string Here is the code I tested (which is not functioning as expected) #include <iostream> #include <string> using namespace std; int main() { ...

Assigning a private class member using a function after the object has been initialized through a constructor in C++

I am having trouble setting a private class member in an object after it has been created using a constructor. The constructor sets several public members but does not modify the private member. class Tower { std::vector<Tower> jtowers; public: ...

Ajax requests that are delayed to the same URL with identical data

While waiting for the back end developers to add a "cancel all" function, which would cancel all tasks tracked by the system, I am trying to create a workaround by cancelling each task individually. The cancellation REST service requires an ID in the form ...

Issue with Bootstrap flash alert CSS on mobile devices is not functioning properly

I am in the process of developing a Rails application. Within my app, I utilize Bootstrap's CSS for displaying flash messages on alerts. Instead of fully incorporating Bootstrap's CSS, I only integrate styles related to alerts in order to prevent ...

Connecting within a webpage without the use of the pound sign

I have a simple question as I am relatively new to programming, having only started about two weeks ago. I want to create a menu with three options: menu1, menu2, and menu3. My question is how can I create a link without using the hashtag (#)? For exampl ...

The test may detect a variable that was not initialized

I'm trying to understand why I get the error message "variable may not have been initialized" when testing (variable === "some text"), but I don't receive the same error when using (typeof passwordHashOrg !== 'undefined') The code that ...

Creating a visually stunning image grid akin to the meticulously designed layouts found

Forgive my lack of knowledge, but I'm curious about how to create an image or text grid similar to Tumblr using HTML and CSS. I'm looking to achieve a layout like this: ...

Stripe.js is frustrated by the duplicate inclusion in the code

Having an issue with Stripe.js - a warning keeps popping up in the console: Seems like Stripe.js has been loaded multiple times. Make sure to only load it once per page. Although the javascript is only present once on the page and the network tab in Dev ...

Steps for transforming an array of arrays into a JSX element within a React component, where each nested array contains its own clipPath

The array_groups variable consists of an array containing multiple arrays. Each inner array holds the coordinates of regular circles that are closely spaced together. The intention is to clipPath these inner circle arrays as a whole. When the mouse hovers ...

Collecting information from websites by changing JavaScript parameters

I am currently attempting to extract intraday prices for a specific company from the website Enel Intraday. The issue I am facing is that when the data is fetched, it is spread across hundreds of pages, making it extremely time-consuming to gather all the ...

Optimizing Wordpress by Efficiently Enqueueing Javascript

As a beginner with a WordPress website, I am aware that in order to execute scripts on a WordPress page, they need to be enqueued in the functions.php file. However, I'm unsure about the correct process for this. The specific JavaScript file I want t ...

Instructions for removing and recreating an input field along with its parent elements when the value of a Select tag is changed

I currently have a table with four fields, as illustrated below: Semester | Exam Status | GPA | Fee Status My query is regarding the behavior when changing the value in Exam_Status: I noticed that the Select tag-> does not clear automatically. Specifi ...

C++ TCP/IP parsing and responding program

Having to work with raw IP packets in C++ due to utilizing Open VPN as a library, which handles IP packets delivery, I am searching for a library that can handle responses while delivering only the TCP/UDP payload. Although I came across libtrace, it seem ...

Close any open alerts using Protractor

While using protractor and cucumber, I have encountered an issue where some tests may result in displaying an alert box. In order to handle this, I want to check for the presence of an alert box at the start of each test and close/dismiss it if it exists. ...