70 import jdk.nashorn.internal.runtime.Scope;
71 import jdk.nashorn.internal.runtime.ScriptEnvironment;
72 import jdk.nashorn.internal.runtime.ScriptFunction;
73 import jdk.nashorn.internal.runtime.ScriptObject;
74 import jdk.nashorn.internal.runtime.ScriptRuntime;
75 import jdk.nashorn.internal.runtime.ScriptingFunctions;
76 import jdk.nashorn.internal.runtime.Specialization;
77 import jdk.nashorn.internal.runtime.arrays.ArrayData;
78 import jdk.nashorn.internal.runtime.linker.Bootstrap;
79 import jdk.nashorn.internal.runtime.linker.InvokeByName;
80 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
81 import jdk.nashorn.internal.runtime.regexp.RegExpResult;
82 import jdk.nashorn.internal.scripts.JD;
83 import jdk.nashorn.internal.scripts.JO;
84 import jdk.nashorn.tools.ShellFunctions;
85
86 /**
87 * Representation of global scope.
88 */
89 @ScriptClass("Global")
90 public final class Global extends ScriptObject implements Scope {
91 // Placeholder value used in place of a location property (__FILE__, __DIR__, __LINE__)
92 private static final Object LOCATION_PROPERTY_PLACEHOLDER = new Object();
93 private final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
94 private final InvokeByName VALUE_OF = new InvokeByName("valueOf", ScriptObject.class);
95
96 // placeholder value for lazily initialized global objects
97 private static final Object LAZY_SENTINEL = new Object();
98
99 /**
100 * Optimistic builtin names that require switchpoint invalidation
101 * upon assignment. Overly conservative, but works for now, to avoid
102 * any complicated scope checks and especially heavy weight guards
103 * like
104 *
105 * <pre>
106 * public boolean setterGuard(final Object receiver) {
107 * final Global global = Global.instance();
108 * final ScriptObject sobj = global.getFunctionPrototype();
109 * final Object apply = sobj.get("apply");
110 * return apply == receiver;
889 private ScriptObject builtinOrg;
890 private ScriptFunction builtinJavaImporter;
891 private ScriptObject builtinJavaApi;
892 private ScriptFunction builtinArrayBuffer;
893 private ScriptFunction builtinDataView;
894 private ScriptFunction builtinInt8Array;
895 private ScriptFunction builtinUint8Array;
896 private ScriptFunction builtinUint8ClampedArray;
897 private ScriptFunction builtinInt16Array;
898 private ScriptFunction builtinUint16Array;
899 private ScriptFunction builtinInt32Array;
900 private ScriptFunction builtinUint32Array;
901 private ScriptFunction builtinFloat32Array;
902 private ScriptFunction builtinFloat64Array;
903
904 /*
905 * ECMA section 13.2.3 The [[ThrowTypeError]] Function Object
906 */
907 private ScriptFunction typeErrorThrower;
908
909 // Flag to indicate that a split method issued a return statement
910 private int splitState = -1;
911
912 // Used to store the last RegExp result to support deprecated RegExp constructor properties
913 private RegExpResult lastRegExpResult;
914
915 private static final MethodHandle EVAL = findOwnMH_S("eval", Object.class, Object.class, Object.class);
916 private static final MethodHandle NO_SUCH_PROPERTY = findOwnMH_S(NO_SUCH_PROPERTY_NAME, Object.class, Object.class, Object.class);
917 private static final MethodHandle PRINT = findOwnMH_S("print", Object.class, Object.class, Object[].class);
918 private static final MethodHandle PRINTLN = findOwnMH_S("println", Object.class, Object.class, Object[].class);
919 private static final MethodHandle LOAD = findOwnMH_S("load", Object.class, Object.class, Object.class);
920 private static final MethodHandle LOAD_WITH_NEW_GLOBAL = findOwnMH_S("loadWithNewGlobal", Object.class, Object.class, Object[].class);
921 private static final MethodHandle EXIT = findOwnMH_S("exit", Object.class, Object.class, Object.class);
922 private static final MethodHandle LEXICAL_SCOPE_FILTER = findOwnMH_S("lexicalScopeFilter", Object.class, Object.class);
923
924 // initialized by nasgen
925 private static PropertyMap $nasgenmap$;
926
927 // context to which this global belongs to
928 private final Context context;
929
930 // current ScriptContext to use - can be null.
931 private ThreadLocal<ScriptContext> scontext;
978 private static PropertyMap checkAndGetMap(final Context context) {
979 // security check first
980 final SecurityManager sm = System.getSecurityManager();
981 if (sm != null) {
982 sm.checkPermission(new RuntimePermission(Context.NASHORN_CREATE_GLOBAL));
983 }
984
985 Objects.requireNonNull(context);
986
987 return $nasgenmap$;
988 }
989
990 /**
991 * Constructor
992 *
993 * @param context the context
994 */
995 public Global(final Context context) {
996 super(checkAndGetMap(context));
997 this.context = context;
998 this.setIsScope();
999 this.lexicalScope = context.getEnv()._es6 ? new LexicalScope(this) : null;
1000 }
1001
1002 /**
1003 * Script access to "current" Global instance
1004 *
1005 * @return the global singleton
1006 */
1007 public static Global instance() {
1008 return Objects.requireNonNull(Context.getGlobal());
1009 }
1010
1011 private static Global instanceFrom(final Object self) {
1012 return self instanceof Global? (Global)self : instance();
1013 }
1014
1015 /**
1016 * Check if we have a Global instance
1017 * @return true if one exists
1018 */
2335 * @param obj and object to check
2336 * @return the script object
2337 */
2338 public static ScriptObject checkObject(final Object obj) {
2339 if (!(obj instanceof ScriptObject)) {
2340 throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
2341 }
2342 return (ScriptObject)obj;
2343 }
2344
2345 /**
2346 * ECMA 9.10 - implementation of CheckObjectCoercible, i.e. raise an exception
2347 * if this object is null or undefined.
2348 *
2349 * @param obj an object to check
2350 */
2351 public static void checkObjectCoercible(final Object obj) {
2352 if (obj == null || obj == UNDEFINED) {
2353 throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
2354 }
2355 }
2356
2357 /**
2358 * Get the current split state.
2359 *
2360 * @return current split state
2361 */
2362 @Override
2363 public int getSplitState() {
2364 return splitState;
2365 }
2366
2367 /**
2368 * Set the current split state.
2369 *
2370 * @param state current split state
2371 */
2372 @Override
2373 public void setSplitState(final int state) {
2374 splitState = state;
2375 }
2376
2377 /**
2378 * Return the ES6 global scope for lexically declared bindings.
2379 * @return the ES6 lexical global scope.
2380 */
2381 public final ScriptObject getLexicalScope() {
2382 assert context.getEnv()._es6;
2383 return lexicalScope;
2384 }
2385
2386 @Override
2387 public void addBoundProperties(final ScriptObject source, final jdk.nashorn.internal.runtime.Property[] properties) {
2388 PropertyMap ownMap = getMap();
2389 LexicalScope lexScope = null;
2390 PropertyMap lexicalMap = null;
2391 boolean hasLexicalDefinitions = false;
2392
2393 if (context.getEnv()._es6) {
2394 lexScope = (LexicalScope) getLexicalScope();
|
70 import jdk.nashorn.internal.runtime.Scope;
71 import jdk.nashorn.internal.runtime.ScriptEnvironment;
72 import jdk.nashorn.internal.runtime.ScriptFunction;
73 import jdk.nashorn.internal.runtime.ScriptObject;
74 import jdk.nashorn.internal.runtime.ScriptRuntime;
75 import jdk.nashorn.internal.runtime.ScriptingFunctions;
76 import jdk.nashorn.internal.runtime.Specialization;
77 import jdk.nashorn.internal.runtime.arrays.ArrayData;
78 import jdk.nashorn.internal.runtime.linker.Bootstrap;
79 import jdk.nashorn.internal.runtime.linker.InvokeByName;
80 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
81 import jdk.nashorn.internal.runtime.regexp.RegExpResult;
82 import jdk.nashorn.internal.scripts.JD;
83 import jdk.nashorn.internal.scripts.JO;
84 import jdk.nashorn.tools.ShellFunctions;
85
86 /**
87 * Representation of global scope.
88 */
89 @ScriptClass("Global")
90 public final class Global extends Scope {
91 // Placeholder value used in place of a location property (__FILE__, __DIR__, __LINE__)
92 private static final Object LOCATION_PROPERTY_PLACEHOLDER = new Object();
93 private final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
94 private final InvokeByName VALUE_OF = new InvokeByName("valueOf", ScriptObject.class);
95
96 // placeholder value for lazily initialized global objects
97 private static final Object LAZY_SENTINEL = new Object();
98
99 /**
100 * Optimistic builtin names that require switchpoint invalidation
101 * upon assignment. Overly conservative, but works for now, to avoid
102 * any complicated scope checks and especially heavy weight guards
103 * like
104 *
105 * <pre>
106 * public boolean setterGuard(final Object receiver) {
107 * final Global global = Global.instance();
108 * final ScriptObject sobj = global.getFunctionPrototype();
109 * final Object apply = sobj.get("apply");
110 * return apply == receiver;
889 private ScriptObject builtinOrg;
890 private ScriptFunction builtinJavaImporter;
891 private ScriptObject builtinJavaApi;
892 private ScriptFunction builtinArrayBuffer;
893 private ScriptFunction builtinDataView;
894 private ScriptFunction builtinInt8Array;
895 private ScriptFunction builtinUint8Array;
896 private ScriptFunction builtinUint8ClampedArray;
897 private ScriptFunction builtinInt16Array;
898 private ScriptFunction builtinUint16Array;
899 private ScriptFunction builtinInt32Array;
900 private ScriptFunction builtinUint32Array;
901 private ScriptFunction builtinFloat32Array;
902 private ScriptFunction builtinFloat64Array;
903
904 /*
905 * ECMA section 13.2.3 The [[ThrowTypeError]] Function Object
906 */
907 private ScriptFunction typeErrorThrower;
908
909 // Used to store the last RegExp result to support deprecated RegExp constructor properties
910 private RegExpResult lastRegExpResult;
911
912 private static final MethodHandle EVAL = findOwnMH_S("eval", Object.class, Object.class, Object.class);
913 private static final MethodHandle NO_SUCH_PROPERTY = findOwnMH_S(NO_SUCH_PROPERTY_NAME, Object.class, Object.class, Object.class);
914 private static final MethodHandle PRINT = findOwnMH_S("print", Object.class, Object.class, Object[].class);
915 private static final MethodHandle PRINTLN = findOwnMH_S("println", Object.class, Object.class, Object[].class);
916 private static final MethodHandle LOAD = findOwnMH_S("load", Object.class, Object.class, Object.class);
917 private static final MethodHandle LOAD_WITH_NEW_GLOBAL = findOwnMH_S("loadWithNewGlobal", Object.class, Object.class, Object[].class);
918 private static final MethodHandle EXIT = findOwnMH_S("exit", Object.class, Object.class, Object.class);
919 private static final MethodHandle LEXICAL_SCOPE_FILTER = findOwnMH_S("lexicalScopeFilter", Object.class, Object.class);
920
921 // initialized by nasgen
922 private static PropertyMap $nasgenmap$;
923
924 // context to which this global belongs to
925 private final Context context;
926
927 // current ScriptContext to use - can be null.
928 private ThreadLocal<ScriptContext> scontext;
975 private static PropertyMap checkAndGetMap(final Context context) {
976 // security check first
977 final SecurityManager sm = System.getSecurityManager();
978 if (sm != null) {
979 sm.checkPermission(new RuntimePermission(Context.NASHORN_CREATE_GLOBAL));
980 }
981
982 Objects.requireNonNull(context);
983
984 return $nasgenmap$;
985 }
986
987 /**
988 * Constructor
989 *
990 * @param context the context
991 */
992 public Global(final Context context) {
993 super(checkAndGetMap(context));
994 this.context = context;
995 this.lexicalScope = context.getEnv()._es6 ? new LexicalScope(this) : null;
996 }
997
998 /**
999 * Script access to "current" Global instance
1000 *
1001 * @return the global singleton
1002 */
1003 public static Global instance() {
1004 return Objects.requireNonNull(Context.getGlobal());
1005 }
1006
1007 private static Global instanceFrom(final Object self) {
1008 return self instanceof Global? (Global)self : instance();
1009 }
1010
1011 /**
1012 * Check if we have a Global instance
1013 * @return true if one exists
1014 */
2331 * @param obj and object to check
2332 * @return the script object
2333 */
2334 public static ScriptObject checkObject(final Object obj) {
2335 if (!(obj instanceof ScriptObject)) {
2336 throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
2337 }
2338 return (ScriptObject)obj;
2339 }
2340
2341 /**
2342 * ECMA 9.10 - implementation of CheckObjectCoercible, i.e. raise an exception
2343 * if this object is null or undefined.
2344 *
2345 * @param obj an object to check
2346 */
2347 public static void checkObjectCoercible(final Object obj) {
2348 if (obj == null || obj == UNDEFINED) {
2349 throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
2350 }
2351 }
2352
2353 /**
2354 * Return the ES6 global scope for lexically declared bindings.
2355 * @return the ES6 lexical global scope.
2356 */
2357 public final ScriptObject getLexicalScope() {
2358 assert context.getEnv()._es6;
2359 return lexicalScope;
2360 }
2361
2362 @Override
2363 public void addBoundProperties(final ScriptObject source, final jdk.nashorn.internal.runtime.Property[] properties) {
2364 PropertyMap ownMap = getMap();
2365 LexicalScope lexScope = null;
2366 PropertyMap lexicalMap = null;
2367 boolean hasLexicalDefinitions = false;
2368
2369 if (context.getEnv()._es6) {
2370 lexScope = (LexicalScope) getLexicalScope();
|