emscripten-core / emsdk

Emscripten SDK
http://emscripten.org
Other
3.03k stars 691 forks source link

Emscripten How to import External JS module in C file and compile with WASM compiler author #311

Open Srinirajini opened 5 years ago

Srinirajini commented 5 years ago

Hi All, Wanted to check if WASM allows importing external JS utilities(like doing npm install ) and compile along with the C code using WASM compiler. I will explain my use case below to relate the requirement. I am porting an existing USB library which is implemented in C to WebUSB APIs, to support Chrome OS WebApps architecture, this library gives access of the USB device to many different application, currently it is all working using the libUSB context, this library creates libusb context and dispatch to application for later control. I understand, in WebUSB the object device(promise) returned by requestDevice() API holds the complete context for later control of USB device including open and select configuration and many other further USB APIs. So, to handover this device object back to my C library call from JS code(I am writing inline JS functions using EM_JS macro), I need to manually allocate the memory of this object and copy the content of the object and pass back to C routines, so that, when C routines makes any consequent call to WebUSB, I can translate this memory object passed from C to the JS Object reference, and work with WebUSB APIs. For calculating the memory, I come across the npm package "object-sizeof" but I am struck how to import these external modules as like the module import in JS coding in WASM please let me know if WASM supports such importing of JS modules in C/C++ file, if so please provide me some reference.

Thanks.

kripken commented 5 years ago

Emscripten doesn't have any special npm support, but EM_JS and EM_ASM let you use arbitrary JS code, so you can use modules from npm. Just require() them and use them normally.

Srinirajini commented 5 years ago

Thanks, for clarification, I am trying to using require inside EM_JS function as below but failed with error. below is my code sample. EM_JS(int, chromeUSBInit, (em_arg_callback_func ptr),{ const markup = '

    ';
    const tlt = document.querySelector('title');
    tlt.textContent = 'USB-SPARK';
    const btnPerm = document.querySelector('body');
    btnPerm.insertAdjacentHTML('beforeend', markup);
    **_const sizeOf = require('object-sizeof');_**
    let device;
    //Request for device
    return Asyncify.handleSleep(function(wakeUp){
            btnPerm.addEventListener('click', async (e) => {
                    e.preventDefault();
                    try{
                            device = await navigator.usb.requestDevice({ filters: [{ vendorId: 0x0508 }] }); 
                            if(!device.opened){
                                    chromeUSBOpen(device); //TODO this should be removed from here and need to return a memory of Object
                                    **_console.log(`Size of the device Object is : ${sizeOf(device)}`);_**
                                    wakeUp(20);
                            }
                    }
                    catch(e){
                            console.log(`Error after Opening the device : ${e}`);
                    }   
            }); 
    })  

});

But I am getting below error while running in the chrome.

_exception thrown: ReferenceError: require is not defined,ReferenceError: require is not defined at chromeUSBInit (http://localhost:8080/usbprobe.js:1745:508) at wasm-function[12]:486 at ret.(anonymous function) (http://localhost:8080/usbprobe.js:3068:35) at Object.Module.main (http://localhost:8080/usbprobe.js:3364:32) at Object.callMain (http://localhost:8080/usbprobe.js:3601:30) at doRun (http://localhost:8080/usbprobe.js:3663:60) at http://localhost:8080/usbprobe.js:3674:7

Please let me know If I am missing anything.

Thanks.

kripken commented 5 years ago

require only exists in Node.js. It looks like you're running it in a browser.

There are various ways to get Node.js code to run in browsers - I'm not familiar with them enough to say though.