Skip to content

Commit

Permalink
Merge pull request #3818 from pingwang2011/timob-11408
Browse files Browse the repository at this point in the history
timob-11408: Android: Runtime error is thrown for require method when called from window opened via URL
  • Loading branch information
ayeung committed Jan 29, 2013
2 parents 29e5e8a + 9a09065 commit 9a61dac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
31 changes: 16 additions & 15 deletions android/runtime/common/src/js/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,24 +339,25 @@ Module.prototype._runScript = function (source, filename) {
context.sourceUrl = url;
context.module = this;

// Create a "context global" that's specific to each module
var contextGlobal = context.global = {
exports: this.exports,
require: require,
module: this,
__filename: filename,
__dirname: path.dirname(filename),
kroll: kroll
};
contextGlobal.global = contextGlobal;

// Add support for console logging
contextGlobal.console = NativeModule.require('console');

var ti = new Titanium.Wrapper(context);
contextGlobal.Ti = contextGlobal.Titanium = ti;

if (kroll.runtime == "rhino") {
// Create a "context global" that's specific to each module
var contextGlobal = context.global = {
exports: this.exports,
require: require,
module: this,
__filename: filename,
__dirname: path.dirname(filename),
kroll: kroll
};
contextGlobal.global = contextGlobal;

// Add support for console logging
contextGlobal.console = NativeModule.require('console');

contextGlobal.Ti = contextGlobal.Titanium = ti;

// We initialize the context with the standard Javascript APIs and globals first before running the script
var newContext = context.global = ti.global = Script.createContext(contextGlobal);
bootstrap.bootstrapGlobals(newContext, Titanium);
Expand Down
20 changes: 14 additions & 6 deletions android/runtime/common/src/js/titanium.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,21 @@ function TiInclude(filename, baseUrl, scopeVars) {
} else {
var source = getUrlSource(filename, sourceUrl),
wrappedSource = "with(sandbox) { " + source + "\n }",
filePath = sourceUrl.href.replace("app://", "");
filePath = sourceUrl.href.replace("app://", ""),
contextGlobal = ti.global;

// Use the global V8 Context directly, since we don't create a
// new context for modules due to TIMOB-11752.
// Put sandbox on the global scope
sandbox = localSandbox;
return Script.runInThisContext(wrappedSource, filePath, true);
if (contextGlobal) {
// We're running inside another window, so we run against it's context
contextGlobal.sandbox = localSandbox;
return Script.runInContext(wrappedSource, contextGlobal, filePath, true);

} else {
// We're running inside modules. Since we don't create a new context for modules
// due to TIMOB-11752, we use the global V8 Context directly.
// Put sandbox on the global scope
sandbox = localSandbox;
return Script.runInThisContext(wrappedSource, filePath, true);
}

}
}
Expand Down

0 comments on commit 9a61dac

Please sign in to comment.