| 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | |
| 7 | package org.mozilla.javascript; |
| 8 | |
| 9 | import java.util.EnumMap; |
| 10 | |
| 11 | /** |
| 12 | * A top-level scope object that provides special means to cache and preserve |
| 13 | * the initial values of the built-in constructor properties for better |
| 14 | * ECMAScript compliance. |
| 15 | * |
| 16 | * <p>ECMA 262 requires that most constructors used internally construct |
| 17 | * objects with the original prototype object as value of their [[Prototype]] |
| 18 | * internal property. Since built-in global constructors are defined as |
| 19 | * writable and deletable, this means they should be cached to protect against |
| 20 | * redefinition at runtime.</p> |
| 21 | * |
| 22 | * <p>In order to implement this efficiently, this class provides a mechanism |
| 23 | * to access the original built-in global constructors and their prototypes |
| 24 | * via numeric class-ids. To make use of this, the new |
| 25 | * {@link ScriptRuntime#newBuiltinObject ScriptRuntime.newBuiltinObject} and |
| 26 | * {@link ScriptRuntime#setBuiltinProtoAndParent ScriptRuntime.setBuiltinProtoAndParent} |
| 27 | * methods should be used to create and initialize objects of built-in classes |
| 28 | * instead of their generic counterparts.</p> |
| 29 | * |
| 30 | * <p>Calling {@link org.mozilla.javascript.Context#initStandardObjects()} |
| 31 | * with an instance of this class as argument will automatically cache |
| 32 | * built-in classes after initialization. For other setups involving |
| 33 | * top-level scopes that inherit global properties from their proptotypes |
| 34 | * (e.g. with dynamic scopes) embeddings should explicitly call |
| 35 | * {@link #cacheBuiltins()} to initialize the class cache for each top-level |
| 36 | * scope.</p> |
| 37 | */ |
| 38 | public class TopLevel extends IdScriptableObject { |
| 39 | |
| 40 | static final long serialVersionUID = -4648046356662472260L; |
| 41 | |
| 42 | /** |
| 43 | * An enumeration of built-in ECMAScript objects. |
| 44 | */ |
| 45 | public enum Builtins { |
| 46 | /** The built-in Object type. */ |
| 47 | Object, |
| 48 | /** The built-in Array type. */ |
| 49 | Array, |
| 50 | /** The built-in Function type. */ |
| 51 | Function, |
| 52 | /** The built-in String type. */ |
| 53 | String, |
| 54 | /** The built-in Number type. */ |
| 55 | Number, |
| 56 | /** The built-in Boolean type. */ |
| 57 | Boolean, |
| 58 | /** The built-in RegExp type. */ |
| 59 | RegExp, |
| 60 | /** The built-in Error type. */ |
| 61 | Error, |
| 62 | /** The built-in Symbol type. */ |
| 63 | Symbol |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * An enumeration of built-in native errors. [ECMAScript 5 - 15.11.6] |
| 68 | */ |
| 69 | enum NativeErrors { |
| 70 | /** Basic Error */ |
| 71 | Error, |
| 72 | /** The native EvalError. */ |
| 73 | EvalError, |
| 74 | /** The native RangeError. */ |
| 75 | RangeError, |
| 76 | /** The native ReferenceError. */ |
| 77 | ReferenceError, |
| 78 | /** The native SyntaxError. */ |
| 79 | SyntaxError, |
| 80 | /** The native TypeError. */ |
| 81 | TypeError, |
| 82 | /** The native URIError. */ |
| 83 | URIError, |
| 84 | /** The native InternalError (non-standard). */ |
| 85 | InternalError, |
| 86 | /** The native JavaException (non-standard). */ |
| 87 | JavaException |
| 88 | } |
| 89 | |
| 90 | private EnumMap<Builtins, BaseFunction> ctors; |
| 91 | private EnumMap<NativeErrors, BaseFunction> errors; |
| 92 | |
| 93 | @Override |
| 94 | public String getClassName() { |
| 95 | return "global"; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Cache the built-in ECMAScript objects to protect them against |
| 100 | * modifications by the script. This method is called automatically by |
| 101 | * {@link ScriptRuntime#initStandardObjects ScriptRuntime.initStandardObjects} |
| 102 | * if the scope argument is an instance of this class. It only has to be |
| 103 | * called by the embedding if a top-level scope is not initialized through |
| 104 | * <code>initStandardObjects()</code>. |
| 105 | */ |
| 106 | public void cacheBuiltins() { |
| 107 | ctors = new EnumMap<Builtins, BaseFunction>(Builtins.class); |
| 108 | for (Builtins builtin : Builtins.values()) { |
| 109 | Object value = ScriptableObject.getProperty(this, builtin.name()); |
| 110 | if (value instanceof BaseFunction) { |
| 111 | ctors.put(builtin, (BaseFunction)value); |
| 112 | } |
| 113 | } |
| 114 | errors = new EnumMap<NativeErrors, BaseFunction>(NativeErrors.class); |
| 115 | for (NativeErrors error : NativeErrors.values()) { |
| 116 | Object value = ScriptableObject.getProperty(this, error.name()); |
| 117 | if (value instanceof BaseFunction) { |
| 118 | errors.put(error, (BaseFunction)value); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Static helper method to get a built-in object constructor with the given |
| 125 | * <code>type</code> from the given <code>scope</code>. If the scope is not |
| 126 | * an instance of this class or does have a cache of built-ins, |
| 127 | * the constructor is looked up via normal property lookup. |
| 128 | * |
| 129 | * @param cx the current Context |
| 130 | * @param scope the top-level scope |
| 131 | * @param type the built-in type |
| 132 | * @return the built-in constructor |
| 133 | */ |
| 134 | public static Function getBuiltinCtor(Context cx, |
| 135 | Scriptable scope, |
| 136 | Builtins type) { |
| 137 | // must be called with top level scope |
| 138 | assert scope.getParentScope() == null; |
| 139 | if (scope instanceof TopLevel) { |
| 140 | Function result = ((TopLevel)scope).getBuiltinCtor(type); |
| 141 | if (result != null) { |
| 142 | return result; |
| 143 | } |
| 144 | } |
| 145 | // fall back to normal constructor lookup |
| 146 | return ScriptRuntime.getExistingCtor(cx, scope, type.name()); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Static helper method to get a native error constructor with the given |
| 151 | * <code>type</code> from the given <code>scope</code>. If the scope is not |
| 152 | * an instance of this class or does have a cache of native errors, |
| 153 | * the constructor is looked up via normal property lookup. |
| 154 | * |
| 155 | * @param cx the current Context |
| 156 | * @param scope the top-level scope |
| 157 | * @param type the native error type |
| 158 | * @return the native error constructor |
| 159 | */ |
| 160 | static Function getNativeErrorCtor(Context cx, Scriptable scope, |
| 161 | NativeErrors type) { |
| 162 | // must be called with top level scope |
| 163 | assert scope.getParentScope() == null; |
| 164 | if (scope instanceof TopLevel) { |
| 165 | Function result = ((TopLevel)scope).getNativeErrorCtor(type); |
| 166 | if (result != null) { |
| 167 | return result; |
| 168 | } |
| 169 | } |
| 170 | // fall back to normal constructor lookup |
| 171 | return ScriptRuntime.getExistingCtor(cx, scope, type.name()); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Static helper method to get a built-in object prototype with the given |
| 176 | * <code>type</code> from the given <code>scope</code>. If the scope is not |
| 177 | * an instance of this class or does have a cache of built-ins, |
| 178 | * the prototype is looked up via normal property lookup. |
| 179 | * |
| 180 | * @param scope the top-level scope |
| 181 | * @param type the built-in type |
| 182 | * @return the built-in prototype |
| 183 | */ |
| 184 | public static Scriptable getBuiltinPrototype(Scriptable scope, |
| 185 | Builtins type) { |
| 186 | // must be called with top level scope |
| 187 | assert scope.getParentScope() == null; |
| 188 | if (scope instanceof TopLevel) { |
| 189 | Scriptable result = ((TopLevel)scope) |
| 190 | .getBuiltinPrototype(type); |
| 191 | if (result != null) { |
| 192 | return result; |
| 193 | } |
| 194 | } |
| 195 | // fall back to normal prototype lookup |
| 196 | return ScriptableObject.getClassPrototype(scope, type.name()); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get the cached built-in object constructor from this scope with the |
| 201 | * given <code>type</code>. Returns null if {@link #cacheBuiltins()} has not |
| 202 | * been called on this object. |
| 203 | * @param type the built-in type |
| 204 | * @return the built-in constructor |
| 205 | */ |
| 206 | public BaseFunction getBuiltinCtor(Builtins type) { |
| 207 | return ctors != null ? ctors.get(type) : null; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Get the cached native error constructor from this scope with the |
| 212 | * given <code>type</code>. Returns null if {@link #cacheBuiltins()} has not |
| 213 | * been called on this object. |
| 214 | * @param type the native error type |
| 215 | * @return the native error constructor |
| 216 | */ |
| 217 | BaseFunction getNativeErrorCtor(NativeErrors type) { |
| 218 | return errors != null ? errors.get(type) : null; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get the cached built-in object prototype from this scope with the |
| 223 | * given <code>type</code>. Returns null if {@link #cacheBuiltins()} has not |
| 224 | * been called on this object. |
| 225 | * @param type the built-in type |
| 226 | * @return the built-in prototype |
| 227 | */ |
| 228 | public Scriptable getBuiltinPrototype(Builtins type) { |
| 229 | BaseFunction func = getBuiltinCtor(type); |
| 230 | Object proto = func != null ? func.getPrototypeProperty() : null; |
| 231 | return proto instanceof Scriptable ? (Scriptable) proto : null; |
| 232 | } |
| 233 | |
| 234 | } |