diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java index 8aaf1253d44..f2ca9c185ef 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java @@ -1502,26 +1502,53 @@ public final class Global extends ScriptObject implements Scope { } /** - * Global load implementation - Nashorn extension + * Global load implementation - Nashorn extension. * - * @param self scope - * @param source source to load + *
+ * load builtin loads the given script. Script source can be a URL or a File + * or a script object with name and script properties. Evaluated code gets + * global object "this" and uses global object as scope for evaluation. + *
+ *+ * If self is undefined or null or global, then global object is used + * as scope as well as "this" for the evaluated code. If self is any other + * object, then it is indirect load call. With indirect load call, the + * properties of scope are available to evaluated script as variables. Also, + * global scope properties are accessible. Any var, function definition in + * evaluated script goes into an object that is not accessible to user scripts. + *
+ * Thus the indirect load call is equivalent to the following: + *
+ *
+ * (function (scope, source) {
+ * with(scope) {
+ * eval(<script_from_source>);
+ * }
+ * })(self, source);
+ *
+ *
*
- * @return result of load (undefined)
+ * @param self scope to use for the script evaluation
+ * @param source script source
+ *
+ * @return result of load (may be undefined)
*
* @throws IOException if source could not be read
*/
public static Object load(final Object self, final Object source) throws IOException {
final Global global = Global.instanceFrom(self);
- final ScriptObject scope = self instanceof ScriptObject ? (ScriptObject)self : global;
- return global.getContext().load(scope, source);
+ return global.getContext().load(self, source);
}
/**
- * Global loadWithNewGlobal implementation - Nashorn extension
+ * Global loadWithNewGlobal implementation - Nashorn extension.
*
- * @param self scope
- * @param args from plus (optional) arguments to be passed to the loaded script
+ * loadWithNewGlobal builtin loads the given script from a URL or a File
+ * or a script object with name and script properties. Evaluated code gets
+ * new global object "this" and uses that new global object as scope for evaluation.
+ *
+ * @param self self This value is ignored by this function
+ * @param args optional arguments to be passed to the loaded script
*
* @return result of load (may be undefined)
*
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
index e7a1b4c6ab9..abca4d6c15a 100644
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
@@ -777,7 +777,7 @@ public final class Context {
*
* @throws IOException if source cannot be found or loaded
*/
- public Object load(final ScriptObject scope, final Object from) throws IOException {
+ public Object load(final Object scope, final Object from) throws IOException {
final Object src = from instanceof ConsString ? from.toString() : from;
Source source = null;
@@ -829,7 +829,42 @@ public final class Context {
}
if (source != null) {
- return evaluateSource(source, scope, scope);
+ if (scope instanceof ScriptObject && ((ScriptObject)scope).isScope()) {
+ final ScriptObject sobj = (ScriptObject)scope;
+ // passed object is a script object
+ // Global is the only user accessible scope ScriptObject
+ assert sobj.isGlobal() : "non-Global scope object!!";
+ return evaluateSource(source, sobj, sobj);
+ } else if (scope == null || scope == UNDEFINED) {
+ // undefined or null scope. Use current global instance.
+ final Global global = getGlobal();
+ return evaluateSource(source, global, global);
+ } else {
+ /*
+ * Arbitrary object passed for scope.
+ * Indirect load that is equivalent to:
+ *
+ * (function(scope, source) {
+ * with (scope) {
+ * eval(