Skip to content

Commit

Permalink
Merge pull request #2403 from billdawson/timob-9315
Browse files Browse the repository at this point in the history
TIMOB-9315 When loading external modules, also load any CommonJS module ...
  • Loading branch information
Opie Cyrus committed Jun 15, 2012
2 parents 6d65914 + 2f044e5 commit 70a9f79
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
43 changes: 27 additions & 16 deletions android/runtime/common/src/js/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,35 @@ Module.prototype.createModuleWrapper = function(externalModule, sourceUrl) {
return wrapper;
}

function extendModuleWithCommonJs(externalModule, id, thiss, context) {
if (kroll.isExternalCommonJsModule(id)) {
var jsModule = new Module(id + ".commonjs", thiss, context);
jsModule.load(id, kroll.getExternalCommonJsModule(id));
if (jsModule.exports) {
if (kroll.DBG) {
kroll.log(TAG, "Extending native module '" + id + "' with the CommonJS module that was packaged with it.");
}
kroll.extend(externalModule, jsModule.exports);
}
}
}

// Loads a native / external (3rd party) module
Module.prototype.loadExternalModule = function(id, externalBinding, context) {

var sourceUrl = context === undefined ? "app://app.js" : context.sourceUrl;
var externalModule;
var returnObj;

if (kroll.runtime === "rhino") {
// TODO -- add support for context specific invokers in Rhino
var bindingKey = Object.keys(externalBinding)[0];
if (bindingKey) {
externalModule = externalBinding[bindingKey];
}

extendModuleWithCommonJs(externalModule, id, this, context);

return externalModule;

} else {
Expand Down Expand Up @@ -173,6 +190,9 @@ Module.prototype.loadExternalModule = function(id, externalBinding, context) {
}

wrapper = this.createModuleWrapper(externalModule, sourceUrl);

extendModuleWithCommonJs(wrapper, id, this, context);

this.wrapperCache[id] = wrapper;

return wrapper;
Expand All @@ -192,22 +212,17 @@ Module.prototype.require = function (request, context, useCache) {

// get external binding (for external / 3rd party modules)
var externalBinding = kroll.externalBinding(request);
var isExternalCommonJs = false;

if (externalBinding) {
return this.loadExternalModule(request, externalBinding, context);
}

var isExternalCommonJs = kroll.isExternalCommonJsModule(request)

var filename = request;

if (!isExternalCommonJs) {
var resolved = resolveFilename(request, this);
var id = resolved[0];
filename = resolved[1];
if (kroll.DBG) {
kroll.log(TAG, 'Loading module: ' + request + ' -> ' + filename);
}
var resolved = resolveFilename(request, this);
var id = resolved[0];
filename = resolved[1];
if (kroll.DBG) {
kroll.log(TAG, 'Loading module: ' + request + ' -> ' + filename);
}

if (useCache) {
Expand All @@ -220,11 +235,7 @@ Module.prototype.require = function (request, context, useCache) {
// Create and attempt to load the module.
var module = new Module(id, this, context);

if (isExternalCommonJs) {
module.load(filename, kroll.getExternalCommonJsModule(filename));
} else {
module.load(filename);
}
module.load(filename);

if (useCache) {
// Cache the module for future requests.
Expand Down
6 changes: 2 additions & 4 deletions support/android/templates/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ public void onCreate()
className = module['module_apiName']
isJSMod = (module.has_key('is_native_js_module') and module['is_native_js_module'])
%>
## Don't do binding/bootstrap stuff if it's just a module carrying CommonJS
% if not isJSMod:
runtime.addExternalModule("${manifest.moduleid}", ${manifest.moduleid}.${className}Bootstrap.class);
% else:
% if isJSMod:
runtime.addExternalCommonJsModule("${manifest.moduleid}", ${manifest.moduleid}.CommonJsSourceProvider.class);
% endif
% endfor
Expand Down Expand Up @@ -113,7 +111,7 @@ public void onCreate()
isJSMod = (module.has_key('is_native_js_module') and module['is_native_js_module'])
%>

% if runtime == "rhino" and not isJSMod:
% if runtime == "rhino":
KrollBindings.addExternalBinding("${manifest.moduleid}", ${module['class_name']}Prototype.class);
${manifest.moduleid}.${manifest.name}GeneratedBindings.init();
% endif
Expand Down

0 comments on commit 70a9f79

Please sign in to comment.