Ok,

Last night I put together an example of the problem I am encountering. I am 
sure others have run into this issue before so there must be a solution, 
may I hear an answer soon. I will attach a gzipped tarball of a simple 
example project showing how my loader script can use a module required() by 
it.

I have reduced the module pattern used by Emscripten into a simple 
Javascript module, but also provided a simple C 'Hello World' which 
compiles with the included Makefile using emmake.

I also will reproduce in this message the simple Javscript example, both 
loader and module, for your viewing pleasure.

*loader.js*
/**
 * loader
 *
 * This Javascript module demonstrates loading an emscripten module 
dynamically
 * into the nodeJS environment.
 */

var argc = process.argv.length;
if (argc != 3) {
    console.log("ERROR: Must specify the decoder filename!");
    process.exit(-10);
}
var decoderFileName = process.argv[2];
console.log("attempting to load decoder " + decoderFileName);

var fs   = require('fs');
var DecoderModule = require( decoderFileName );

var Module = {
    noInitialRun: true,
    preRun: function() {
        console.log("module prerun executed!");
    },
    postRun: function() {
        console.log("module postrun executed!");
    }
}

// Load the decoder into the Module namespace.

DecoderModule(Module);

// Call the main entry point and then perform the tasks..
Module._main();
Module._performTask1();
var result = Module._performTask2(2, 4);
console.log("result of task2 = " + result);



And now for the simple Javascript module which mirrors the module pattern 
generated by Emscripten

*test.js*
/**
 * This code uses the design pattern of the emscripten module created with
 * -s MODULARIZE=1 -s EXPORT_NAME="'DecoderModule'"
 *
 * I have taken some short-cuts but believe this code approximates the 
module
 * well enough to experiment with.
 */

var DecoderModule = function(DecoderModule) {
    console.log("DecoderModule() called!");;

    if (typeof DecoderModule.preRun === 'function')
        DecoderModule.preRun();

    DecoderModule._main = function() {
        console.log("Hello world from the world of Javascript!");
    }

    DecoderModule._performTask1 = function() {
        console.log("performTask1 called!");
        return "function1 called!";
    }

    DecoderModule._performTask2 = function(a, b) {
        console.log("performTask2 called!");
        return (a + (a * b));
    }

    if (DecoderModule.noInitialRun == false)
        DecoderModule._main();

    if (typeof DecoderModule.postRun === 'function')
        DecoderModule.postRun();

    return DecoderModule;
}

// The next line enables the module to be 'required()' by the loader module.
// HOW TO GET THIS AUTOMATICALLY GENERATED?
module.exports = DecoderModule;




Take notice, please, of the last few lines of the modules code... Here is 
where the DecoderModule function is exported from the module. THIS IS THE 
SECRET to being able to load it into the nodeJS environment using the 
require() statement. Without it all is lost in this department.

I run the code like this:

$> nodejs ./load.js ./test.js

attempting to load decoder ./test.js
DecoderModule() called!
module prerun executed!
module postrun executed!
Hello world from the world of Javascript!
performTask1 called!
performTask2 called!
result of task2 = 10




Now you can download my tarball and attempt to build the C version using 
Emscripten. The Makefile is simple:

# Makefile
#

EMSCRIPTEN_FLAGS=-s MODULARIZE=1 -s EXPORT_NAME="'DecoderModule'"

component.js : component.c
    $(CC) -o component.js component.c $(EMSCRIPTEN_FLAGS)

clean:
    rm component.js

You can make the component.js module using 'emmake make' on this.

In my archive I provide a compiled version of the C module but with the 
needed 'module.exports = DecoderModule' line which seems to me to be the 
deal-breaker in being able to get output code in my module which I can use 
in the NodeJS test bench application I am working on.

Thank you,

Michael A. Uman
Sr. Software Engineer, Sigma Designs, Inc.

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to emscripten-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to