/jsc-require

CommonJS modules in JavaScriptCore

Primary LanguageObjective-C

CommonJS modules in JavaScriptCore

This project is an attempt to add add support for CommonJS modules in JavaScriptCore engine.

Usage

make compile
./jsc-require main.js

How it works

require is defined as userspace function with callback to the native code which tries to resolve path to the module, evaluates it as a function in the same context and the passes result back.

Issues

Since modules are evaluated in the same context, any global variable/function defined in module will leak into global namespace.

Alternatives

Pre-process require dependencies before passing to the JS engine

/// foo.js

exports.foo = function() { return 'foo'; };

/// bar.js

var foo = require('foo').foo;

exports.foobar = function() { return foo()+'bar'; };

/// =>

var foo = (function() {
    var module = {}; module.exports = {}; var exports = module.exports;
    // begin foo.js
    exports.foo = function() { return 'foo'; };
    // end foo.js
    return exports;
})();

exports.foobar = function() { return foo()+'bar'; };

/// main.js

var bar = require('bar');

bar.foobar();

/// =>

var bar = (function(){
    var module = {}; module.exports = {}; var exports = module.exports;
    // begin bar.js 
    var foo = (function() {
        var module = {}; module.exports = {}; var exports = module.exports;
        // begin foo.js
        exports.foo = function() { return 'foo'; };
        // end foo.js
        return exports;
    })();
    exports.foobar = function() { return foo()+'bar'; };
    // end bar.js
    return exports;
}({}));

bar.foobar();

/// =>