?? context.java
字號:
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Rhino code, released * May 6, 1999. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1997-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Kemal Bayram * Patrick Beard * Norris Boyd * Igor Bukanov, igor@mir2.org * Brendan Eich * Ethan Hugg * Roger Lawrence * Terry Lucas * Mike McCabe * Milen Nankov * Attila Szegedi, szegedia@freemail.hu * Ian D. Stewart * Andi Vajda * Andrew Wason * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (the "GPL"), in which case the * provisions of the GPL are applicable instead of those above. * If you wish to allow use of your version of this file only * under the terms of the GPL and not to allow others to use your * version of this file under the NPL, indicate your decision by * deleting the provisions above and replace them with the notice * and other provisions required by the GPL. If you do not delete * the provisions above, a recipient may use your version of this * file under either the NPL or the GPL. */// API classpackage org.mozilla.javascript;import java.beans.*;import java.io.*;import java.util.Hashtable;import java.util.Locale;import java.lang.reflect.*;import org.mozilla.javascript.debug.*;import org.mozilla.javascript.xml.XMLLib;/** * This class represents the runtime context of an executing script. * * Before executing a script, an instance of Context must be created * and associated with the thread that will be executing the script. * The Context will be used to store information about the executing * of the script such as the call stack. Contexts are associated with * the current thread using the {@link #call(ContextAction)} * or {@link #enter()} methods.<p> * * Different forms of script execution are supported. Scripts may be * evaluated from the source directly, or first compiled and then later * executed. Interactive execution is also supported.<p> * * Some aspects of script execution, such as type conversions and * object creation, may be accessed directly through methods of * Context. * * @see Scriptable * @author Norris Boyd * @author Brendan Eich */public class Context{ /** * Language versions. * * All integral values are reserved for future version numbers. */ /** * The unknown version. */ public static final int VERSION_UNKNOWN = -1; /** * The default version. */ public static final int VERSION_DEFAULT = 0; /** * JavaScript 1.0 */ public static final int VERSION_1_0 = 100; /** * JavaScript 1.1 */ public static final int VERSION_1_1 = 110; /** * JavaScript 1.2 */ public static final int VERSION_1_2 = 120; /** * JavaScript 1.3 */ public static final int VERSION_1_3 = 130; /** * JavaScript 1.4 */ public static final int VERSION_1_4 = 140; /** * JavaScript 1.5 */ public static final int VERSION_1_5 = 150; /** * JavaScript 1.5 */ public static final int VERSION_1_6 = 160; /** * Controls behaviour of <tt>Date.prototype.getYear()</tt>. * If <tt>hasFeature(FEATURE_NON_ECMA_GET_YEAR)</tt> returns true, * Date.prototype.getYear subtructs 1900 only if 1900 <= date < 2000. * The default behavior of {@link #hasFeature(int)} is always to subtruct * 1900 as rquired by ECMAScript B.2.4. */ public static final int FEATURE_NON_ECMA_GET_YEAR = 1; /** * Control if member expression as function name extension is available. * If <tt>hasFeature(FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME)</tt> returns * true, allow <tt>function memberExpression(args) { body }</tt> to be * syntax sugar for <tt>memberExpression = function(args) { body }</tt>, * when memberExpression is not a simple identifier. * See ECMAScript-262, section 11.2 for definition of memberExpression. * By default {@link #hasFeature(int)} returns false. */ public static final int FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME = 2; /** * Control if reserved keywords are treated as identifiers. * If <tt>hasFeature(RESERVED_KEYWORD_AS_IDENTIFIER)</tt> returns true, * treat future reserved keyword (see Ecma-262, section 7.5.3) as ordinary * identifiers but warn about this usage. * * By default {@link #hasFeature(int)} returns false. */ public static final int FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER = 3; /** * Control if <tt>toString()</tt> should returns the same result * as <tt>toSource()</tt> when applied to objects and arrays. * If <tt>hasFeature(FEATURE_TO_STRING_AS_SOURCE)</tt> returns true, * calling <tt>toString()</tt> on JS objects gives the same result as * calling <tt>toSource()</tt>. That is it returns JS source with code * to create an object with all enumeratable fields of the original object * instead of printing <tt>[object <i>result of * {@link Scriptable#getClassName()}</i>]</tt>. * <p> * By default {@link #hasFeature(int)} returns true only if * the current JS version is set to {@link #VERSION_1_2}. */ public static final int FEATURE_TO_STRING_AS_SOURCE = 4; /** * Control if properties <tt>__proto__</tt> and <tt>__parent__</tt> * are treated specially. * If <tt>hasFeature(FEATURE_PARENT_PROTO_PROPRTIES)</tt> returns true, * treat <tt>__parent__</tt> and <tt>__proto__</tt> as special properties. * <p> * The properties allow to query and set scope and prototype chains for the * objects. The special meaning of the properties is available * only when they are used as the right hand side of the dot operator. * For example, while <tt>x.__proto__ = y</tt> changes the prototype * chain of the object <tt>x</tt> to point to <tt>y</tt>, * <tt>x["__proto__"] = y</tt> simply assigns a new value to the property * <tt>__proto__</tt> in <tt>x</tt> even when the feature is on. * * By default {@link #hasFeature(int)} returns true. */ public static final int FEATURE_PARENT_PROTO_PROPRTIES = 5; /** * Control if support for E4X(ECMAScript for XML) extension is available. * If hasFeature(FEATURE_E4X) returns true, the XML syntax is available. * <p> * By default {@link #hasFeature(int)} returns true if * the current JS version is set to {@link #VERSION_DEFAULT} * or is greater then {@link #VERSION_1_6}. * @since 1.6 Release 1 */ public static final int FEATURE_E4X = 6; /** * Control if dynamic scope should be used for name access. * If hasFeature(FEATURE_DYNAMIC_SCOPE) returns true, then the name lookup * during name resolution will use the top scope of the script or function * which is at the top of JS execution stack instead of the top scope of the * script or function from the current stack frame if the top scope of * the top stack frame contains the top scope of the current stack frame * on its prototype chain. * <p> * This is useful to define shared scope containing functions that can * be called from scripts and functions using private scopes. * <p> * By default {@link #hasFeature(int)} returns false. * @since 1.6 Release 1 */ public static final int FEATURE_DYNAMIC_SCOPE = 7; /** * Control if strict variable mode is enabled. * When the feature is on Rhino reports runtime errors if assignment * to a global variable that does not exist is executed. When the feature * is off such assignments creates new variable in the global scope as * required by ECMA 262. * <p> * By default {@link #hasFeature(int)} returns false. * @since 1.6 Release 1 */ public static final int FEATURE_STRICT_VARS = 8; /** * Control if strict eval mode is enabled. * When the feature is on Rhino reports runtime errors if non-string * argument is passed to the eval function. When the feature is off * eval simply return non-string argument as is without performing any * evaluation as required by ECMA 262. * <p> * By default {@link #hasFeature(int)} returns false. * @since 1.6 Release 1 */ public static final int FEATURE_STRICT_EVAL = 9; public static final String languageVersionProperty = "language version"; public static final String errorReporterProperty = "error reporter"; /** * Convinient value to use as zero-length array of objects. */ public static final Object[] emptyArgs = ScriptRuntime.emptyArgs; /** * Create a new Context. * * Note that the Context must be associated with a thread before * it can be used to execute a script. * * @see #enter() * @see #call(ContextAction) */ public Context() { setLanguageVersion(VERSION_DEFAULT); optimizationLevel = codegenClass != null ? 0 : -1; } /** * Get the current Context. * * The current Context is per-thread; this method looks up * the Context associated with the current thread. <p> * * @return the Context associated with the current thread, or * null if no context is associated with the current * thread. * @see org.mozilla.javascript.Context#enter() * @see org.mozilla.javascript.Context#exit() */ public static Context getCurrentContext() { Object helper = VMBridge.instance.getThreadContextHelper(); return VMBridge.instance.getContext(helper); } /** * Get a context associated with the current thread, creating * one if need be. * * The Context stores the execution state of the JavaScript * engine, so it is required that the context be entered * before execution may begin. Once a thread has entered * a Context, then getCurrentContext() may be called to find * the context that is associated with the current thread. * <p> * Calling <code>enter()</code> will * return either the Context currently associated with the * thread, or will create a new context and associate it * with the current thread. Each call to <code>enter()</code> * must have a matching call to <code>exit()</code>. For example, * <pre> * Context cx = Context.enter(); * try { * ... * cx.evaluateString(...); * } finally { * Context.exit(); * } * </pre> * Instead of using <tt>enter()</tt>, <tt>exit()</tt> pair consider using * {@link #call(ContextAction)} which guarantees proper * association of Context instances with the current thread and is faster. * With this method the above example becomes: * <pre> * Context.call(new ContextAction() { * public Object run(Context cx) { * ... * cx.evaluateString(...); * return null; * } * }); * </pre> * * @return a Context associated with the current thread * @see #getCurrentContext() * @see #exit() * @see #call(ContextAction) */ public static Context enter() { return enter(null); } /** * Get a Context associated with the current thread, using * the given Context if need be. * <p> * The same as <code>enter()</code> except that <code>cx</code> * is associated with the current thread and returned if * the current thread has no associated context and <code>cx</code> * is not associated with any other thread. * @param cx a Context to associate with the thread if possible * @return a Context associated with the current thread * * @see #enter() * @see #call(ContextAction) * @see ContextFactory#call(ContextAction) */ public static Context enter(Context cx) { Object helper = VMBridge.instance.getThreadContextHelper(); Context old = VMBridge.instance.getContext(helper); if (old != null) { if (cx != null && cx != old && cx.enterCount != 0) { // The suplied context must be the context for
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -