src/jdk/nashorn/internal/runtime/Context.java
Print this page
*** 154,188 ****
}
/** Is Context global debug mode enabled ? */
public static final boolean DEBUG = Options.getBooleanProperty("nashorn.debug");
! private static final ThreadLocal<ScriptObject> currentGlobal = new ThreadLocal<>();
// class cache
private ClassCache classCache;
/**
* Get the current global scope
* @return the current global scope
*/
! public static ScriptObject getGlobal() {
// This class in a package.access protected package.
// Trusted code only can call this method.
! return getGlobalTrusted();
}
/**
* Set the current global scope
* @param global the global scope
*/
public static void setGlobal(final ScriptObject global) {
if (global != null && !(global instanceof Global)) {
! throw new IllegalArgumentException("global is not an instance of Global!");
}
! setGlobalTrusted(global);
}
/**
* Get context of the current global
* @return current global scope's context.
--- 154,197 ----
}
/** Is Context global debug mode enabled ? */
public static final boolean DEBUG = Options.getBooleanProperty("nashorn.debug");
! private static final ThreadLocal<Global> currentGlobal = new ThreadLocal<>();
// class cache
private ClassCache classCache;
/**
* Get the current global scope
* @return the current global scope
*/
! public static Global getGlobal() {
// This class in a package.access protected package.
// Trusted code only can call this method.
! return currentGlobal.get();
}
/**
* Set the current global scope
* @param global the global scope
*/
public static void setGlobal(final ScriptObject global) {
if (global != null && !(global instanceof Global)) {
! throw new IllegalArgumentException("not a global!");
! }
! setGlobal((Global)global);
}
! /**
! * Set the current global scope
! * @param global the global scope
! */
! public static void setGlobal(final Global global) {
! // This class in a package.access protected package.
! // Trusted code only can call this method.
! currentGlobal.set(global);
}
/**
* Get context of the current global
* @return current global scope's context.
*** 199,209 ****
* Get current context's error writer
*
* @return error writer of the current context
*/
public static PrintWriter getCurrentErr() {
! final ScriptObject global = getGlobalTrusted();
return (global != null)? global.getContext().getErr() : new PrintWriter(System.err);
}
/**
* Output text to this Context's error stream
--- 208,218 ----
* Get current context's error writer
*
* @return error writer of the current context
*/
public static PrintWriter getCurrentErr() {
! final ScriptObject global = getGlobal();
return (global != null)? global.getContext().getErr() : new PrintWriter(System.err);
}
/**
* Output text to this Context's error stream
*** 404,414 ****
/**
* Get the PropertyMap of the current global scope
* @return the property map of the current global scope
*/
public static PropertyMap getGlobalMap() {
! return Context.getGlobalTrusted().getMap();
}
/**
* Compile a top level script.
*
--- 413,423 ----
/**
* Get the PropertyMap of the current global scope
* @return the property map of the current global scope
*/
public static PropertyMap getGlobalMap() {
! return Context.getGlobal().getMap();
}
/**
* Compile a top level script.
*
*** 434,444 ****
*/
public Object eval(final ScriptObject initialScope, final String string, final Object callThis, final Object location, final boolean strict) {
final String file = (location == UNDEFINED || location == null) ? "<eval>" : location.toString();
final Source source = new Source(file, string);
final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
! final ScriptObject global = Context.getGlobalTrusted();
ScriptObject scope = initialScope;
// ECMA section 10.1.1 point 2 says eval code is strict if it begins
// with "use strict" directive or eval direct call itself is made
--- 443,453 ----
*/
public Object eval(final ScriptObject initialScope, final String string, final Object callThis, final Object location, final boolean strict) {
final String file = (location == UNDEFINED || location == null) ? "<eval>" : location.toString();
final Source source = new Source(file, string);
final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
! final Global global = Context.getGlobal();
ScriptObject scope = initialScope;
// ECMA section 10.1.1 point 2 says eval code is strict if it begins
// with "use strict" directive or eval direct call itself is made
*** 466,476 ****
// In strict mode, eval does not instantiate variables and functions
// in the caller's environment. A new environment is created!
if (strictFlag) {
// Create a new scope object
! final ScriptObject strictEvalScope = ((GlobalObject)global).newObject();
// bless it as a "scope"
strictEvalScope.setIsScope();
// set given scope to be it's proto so that eval can still
--- 475,485 ----
// In strict mode, eval does not instantiate variables and functions
// in the caller's environment. A new environment is created!
if (strictFlag) {
// Create a new scope object
! final ScriptObject strictEvalScope = global.newObject();
// bless it as a "scope"
strictEvalScope.setIsScope();
// set given scope to be it's proto so that eval can still
*** 591,604 ****
* @return return value for load call (undefined)
*
* @throws IOException if source cannot be found or loaded
*/
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
! final ScriptObject oldGlobal = getGlobalTrusted();
! final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
@Override
! public ScriptObject run() {
try {
return newGlobal();
} catch (final RuntimeException e) {
if (Context.DEBUG) {
e.printStackTrace();
--- 600,613 ----
* @return return value for load call (undefined)
*
* @throws IOException if source cannot be found or loaded
*/
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
! final Global oldGlobal = getGlobal();
! final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
@Override
! public Global run() {
try {
return newGlobal();
} catch (final RuntimeException e) {
if (Context.DEBUG) {
e.printStackTrace();
*** 607,627 ****
}
}
}, CREATE_GLOBAL_ACC_CTXT);
// initialize newly created Global instance
initGlobal(newGlobal);
! setGlobalTrusted(newGlobal);
final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY : ScriptObjectMirror.wrapArray(args, oldGlobal);
! newGlobal.put("arguments", ((GlobalObject)newGlobal).wrapAsObject(wrapped), env._strict);
try {
// wrap objects from newGlobal's world as mirrors - but if result
// is from oldGlobal's world, unwrap it!
return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
} finally {
! setGlobalTrusted(oldGlobal);
}
}
/**
* Load or get a structure class. Structure class names are based on the number of parameter fields
--- 616,636 ----
}
}
}, CREATE_GLOBAL_ACC_CTXT);
// initialize newly created Global instance
initGlobal(newGlobal);
! setGlobal(newGlobal);
final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY : ScriptObjectMirror.wrapArray(args, oldGlobal);
! newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);
try {
// wrap objects from newGlobal's world as mirrors - but if result
// is from oldGlobal's world, unwrap it!
return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
} finally {
! setGlobal(oldGlobal);
}
}
/**
* Load or get a structure class. Structure class names are based on the number of parameter fields
*** 792,864 ****
/**
* Create and initialize a new global scope object.
*
* @return the initialized global scope object.
*/
! public ScriptObject createGlobal() {
return initGlobal(newGlobal());
}
/**
* Create a new uninitialized global scope object
* @return the global script object
*/
! public ScriptObject newGlobal() {
return new Global(this);
}
/**
* Initialize given global scope object.
*
* @param global the global
* @return the initialized global scope object.
*/
! public ScriptObject initGlobal(final ScriptObject global) {
! if (! (global instanceof GlobalObject)) {
! throw new IllegalArgumentException("not a global object!");
! }
!
// Need only minimal global object, if we are just compiling.
if (!env._compile_only) {
! final ScriptObject oldGlobal = Context.getGlobalTrusted();
try {
! Context.setGlobalTrusted(global);
// initialize global scope with builtin global objects
! ((GlobalObject)global).initBuiltinObjects();
} finally {
! Context.setGlobalTrusted(oldGlobal);
}
}
return global;
}
/**
! * Trusted variants - package-private
*/
/**
- * Return the current global scope
- * @return current global scope
- */
- static ScriptObject getGlobalTrusted() {
- return currentGlobal.get();
- }
-
- /**
- * Set the current global scope
- */
- static void setGlobalTrusted(ScriptObject global) {
- currentGlobal.set(global);
- }
-
- /**
* Return the current global's context
* @return current global's context
*/
static Context getContextTrusted() {
! return Context.getGlobalTrusted().getContext();
}
/**
* Try to infer Context instance from the Class. If we cannot,
* then get it from the thread local variable.
--- 801,854 ----
/**
* Create and initialize a new global scope object.
*
* @return the initialized global scope object.
*/
! public Global createGlobal() {
return initGlobal(newGlobal());
}
/**
* Create a new uninitialized global scope object
* @return the global script object
*/
! public Global newGlobal() {
return new Global(this);
}
/**
* Initialize given global scope object.
*
* @param global the global
* @return the initialized global scope object.
*/
! public Global initGlobal(final Global global) {
// Need only minimal global object, if we are just compiling.
if (!env._compile_only) {
! final Global oldGlobal = Context.getGlobal();
try {
! Context.setGlobal(global);
// initialize global scope with builtin global objects
! global.initBuiltinObjects();
} finally {
! Context.setGlobal(oldGlobal);
}
}
return global;
}
/**
! * Trusted variant - package-private
*/
/**
* Return the current global's context
* @return current global's context
*/
static Context getContextTrusted() {
! return ((ScriptObject)Context.getGlobal()).getContext();
}
/**
* Try to infer Context instance from the Class. If we cannot,
* then get it from the thread local variable.
*** 923,933 ****
} catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
strict = false;
}
// Package as a JavaScript function and pass function back to shell.
! return ((GlobalObject)Context.getGlobalTrusted()).newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, scope, strict);
}
private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
return getRunScriptFunction(compile(source, errMan, this._strict), scope);
}
--- 913,923 ----
} catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
strict = false;
}
// Package as a JavaScript function and pass function back to shell.
! return Context.getGlobal().newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, scope, strict);
}
private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
return getRunScriptFunction(compile(source, errMan, this._strict), scope);
}