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;
111 * }
112 * </pre>
113 *
114 * Naturally, checking for builtin classes like NativeFunction is cheaper,
115 * it's when you start adding property checks for said builtins you have
116 * problems with guard speed.
117 */
118
165 public Object print;
166
167 /** Nashorn extension: global.load */
168 @Property(attributes = Attribute.NOT_ENUMERABLE)
169 public Object load;
170
171 /** Nashorn extension: global.loadWithNewGlobal */
172 @Property(attributes = Attribute.NOT_ENUMERABLE)
173 public Object loadWithNewGlobal;
174
175 /** Nashorn extension: global.exit */
176 @Property(attributes = Attribute.NOT_ENUMERABLE)
177 public Object exit;
178
179 /** Nashorn extension: global.quit */
180 @Property(attributes = Attribute.NOT_ENUMERABLE)
181 public Object quit;
182
183 /** Value property NaN of the Global Object - ECMA 15.1.1.1 NaN */
184 @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
185 public final double NaN = Double.NaN;
186
187 /** Value property Infinity of the Global Object - ECMA 15.1.1.2 Infinity */
188 @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
189 public final double Infinity = Double.POSITIVE_INFINITY;
190
191 /** Value property Undefined of the Global Object - ECMA 15.1.1.3 Undefined */
192 @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
193 public final Object undefined = UNDEFINED;
194
195 /** ECMA 15.1.2.1 eval(x) */
196 @Property(attributes = Attribute.NOT_ENUMERABLE)
197 public Object eval;
198
199 /** ECMA 15.1.4.1 Object constructor. */
200 @Property(name = "Object", attributes = Attribute.NOT_ENUMERABLE)
201 public volatile Object object;
202
203 /** ECMA 15.1.4.2 Function constructor. */
204 @Property(name = "Function", attributes = Attribute.NOT_ENUMERABLE)
205 public volatile Object function;
206
207 /** ECMA 15.1.4.3 Array constructor. */
208 @Property(name = "Array", attributes = Attribute.NOT_ENUMERABLE)
209 public volatile Object array;
210
211 /** ECMA 15.1.4.4 String constructor */
212 @Property(name = "String", attributes = Attribute.NOT_ENUMERABLE)
213 public volatile Object string;
813 }
814 return global.javaApi;
815 }
816
817 /**
818 * Setter for the Nashorn extension: global.Java property.
819 *
820 * @param self self reference
821 * @param value value of the Java property
822 */
823 @Setter(name = "Java", attributes = Attribute.NOT_ENUMERABLE)
824 public static void setJavaApi(final Object self, final Object value) {
825 final Global global = Global.instanceFrom(self);
826 global.javaApi = value;
827 }
828
829 private volatile Object javaApi;
830
831 /** Nashorn extension: current script's file name */
832 @Property(name = "__FILE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
833 public final Object __FILE__ = LOCATION_PROPERTY_PLACEHOLDER;
834
835 /** Nashorn extension: current script's directory */
836 @Property(name = "__DIR__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
837 public final Object __DIR__ = LOCATION_PROPERTY_PLACEHOLDER;
838
839 /** Nashorn extension: current source line number being executed */
840 @Property(name = "__LINE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
841 public final Object __LINE__ = LOCATION_PROPERTY_PLACEHOLDER;
842
843 private volatile NativeDate DEFAULT_DATE;
844
845 /** Used as Date.prototype's default value */
846 NativeDate getDefaultDate() {
847 return DEFAULT_DATE;
848 }
849
850 private volatile NativeRegExp DEFAULT_REGEXP;
851
852 /** Used as RegExp.prototype's default value */
853 NativeRegExp getDefaultRegExp() {
854 return DEFAULT_REGEXP;
855 }
856
857 /*
858 * Built-in constructor objects: Even if user changes dynamic values of
859 * "Object", "Array" etc., we still want to keep original values of these
860 * constructors here. For example, we need to be able to create array,
861 * regexp literals even after user overwrites global "Array" or "RegExp"
2003 return fn == Global.instance().builtinEval;
2004 }
2005
2006 /**
2007 * Called from generated to replace a location property placeholder with the actual location property value.
2008 *
2009 * @param placeholder the value tested for being a placeholder for a location property
2010 * @param locationProperty the actual value for the location property
2011 * @return locationProperty if placeholder is indeed a placeholder for a location property, the placeholder otherwise
2012 */
2013 public static Object replaceLocationPropertyPlaceholder(final Object placeholder, final Object locationProperty) {
2014 return isLocationPropertyPlaceholder(placeholder) ? locationProperty : placeholder;
2015 }
2016
2017 /**
2018 * Called from runtime internals to check if the passed value is a location property placeholder.
2019 * @param placeholder the value tested for being a placeholder for a location property
2020 * @return true if the value is a placeholder, false otherwise.
2021 */
2022 public static boolean isLocationPropertyPlaceholder(final Object placeholder) {
2023 return placeholder == LOCATION_PROPERTY_PLACEHOLDER;
2024 }
2025
2026 /**
2027 * Create a new RegExp object.
2028 *
2029 * @param expression Regular expression.
2030 * @param options Search options.
2031 *
2032 * @return New RegExp object.
2033 */
2034 public static Object newRegExp(final String expression, final String options) {
2035 if (options == null) {
2036 return new NativeRegExp(expression);
2037 }
2038 return new NativeRegExp(expression, options);
2039 }
2040
2041 /**
2042 * Get the object prototype
2043 *
|
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 // This special value is used to flag a lazily initialized global property.
92 // This also serves as placeholder value used in place of a location property
93 // (__FILE__, __DIR__, __LINE__)
94 private static final Object LAZY_SENTINEL = new Object();
95
96 private final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
97 private final InvokeByName VALUE_OF = new InvokeByName("valueOf", ScriptObject.class);
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;
111 * }
112 * </pre>
113 *
114 * Naturally, checking for builtin classes like NativeFunction is cheaper,
115 * it's when you start adding property checks for said builtins you have
116 * problems with guard speed.
117 */
118
165 public Object print;
166
167 /** Nashorn extension: global.load */
168 @Property(attributes = Attribute.NOT_ENUMERABLE)
169 public Object load;
170
171 /** Nashorn extension: global.loadWithNewGlobal */
172 @Property(attributes = Attribute.NOT_ENUMERABLE)
173 public Object loadWithNewGlobal;
174
175 /** Nashorn extension: global.exit */
176 @Property(attributes = Attribute.NOT_ENUMERABLE)
177 public Object exit;
178
179 /** Nashorn extension: global.quit */
180 @Property(attributes = Attribute.NOT_ENUMERABLE)
181 public Object quit;
182
183 /** Value property NaN of the Global Object - ECMA 15.1.1.1 NaN */
184 @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
185 public static final double NaN = Double.NaN;
186
187 /** Value property Infinity of the Global Object - ECMA 15.1.1.2 Infinity */
188 @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
189 public static final double Infinity = Double.POSITIVE_INFINITY;
190
191 /** Value property Undefined of the Global Object - ECMA 15.1.1.3 Undefined */
192 @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
193 public static final Object undefined = UNDEFINED;
194
195 /** ECMA 15.1.2.1 eval(x) */
196 @Property(attributes = Attribute.NOT_ENUMERABLE)
197 public Object eval;
198
199 /** ECMA 15.1.4.1 Object constructor. */
200 @Property(name = "Object", attributes = Attribute.NOT_ENUMERABLE)
201 public volatile Object object;
202
203 /** ECMA 15.1.4.2 Function constructor. */
204 @Property(name = "Function", attributes = Attribute.NOT_ENUMERABLE)
205 public volatile Object function;
206
207 /** ECMA 15.1.4.3 Array constructor. */
208 @Property(name = "Array", attributes = Attribute.NOT_ENUMERABLE)
209 public volatile Object array;
210
211 /** ECMA 15.1.4.4 String constructor */
212 @Property(name = "String", attributes = Attribute.NOT_ENUMERABLE)
213 public volatile Object string;
813 }
814 return global.javaApi;
815 }
816
817 /**
818 * Setter for the Nashorn extension: global.Java property.
819 *
820 * @param self self reference
821 * @param value value of the Java property
822 */
823 @Setter(name = "Java", attributes = Attribute.NOT_ENUMERABLE)
824 public static void setJavaApi(final Object self, final Object value) {
825 final Global global = Global.instanceFrom(self);
826 global.javaApi = value;
827 }
828
829 private volatile Object javaApi;
830
831 /** Nashorn extension: current script's file name */
832 @Property(name = "__FILE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
833 public static final Object __FILE__ = LAZY_SENTINEL;
834
835 /** Nashorn extension: current script's directory */
836 @Property(name = "__DIR__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
837 public static final Object __DIR__ = LAZY_SENTINEL;
838
839 /** Nashorn extension: current source line number being executed */
840 @Property(name = "__LINE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
841 public static final Object __LINE__ = LAZY_SENTINEL;
842
843 private volatile NativeDate DEFAULT_DATE;
844
845 /** Used as Date.prototype's default value */
846 NativeDate getDefaultDate() {
847 return DEFAULT_DATE;
848 }
849
850 private volatile NativeRegExp DEFAULT_REGEXP;
851
852 /** Used as RegExp.prototype's default value */
853 NativeRegExp getDefaultRegExp() {
854 return DEFAULT_REGEXP;
855 }
856
857 /*
858 * Built-in constructor objects: Even if user changes dynamic values of
859 * "Object", "Array" etc., we still want to keep original values of these
860 * constructors here. For example, we need to be able to create array,
861 * regexp literals even after user overwrites global "Array" or "RegExp"
2003 return fn == Global.instance().builtinEval;
2004 }
2005
2006 /**
2007 * Called from generated to replace a location property placeholder with the actual location property value.
2008 *
2009 * @param placeholder the value tested for being a placeholder for a location property
2010 * @param locationProperty the actual value for the location property
2011 * @return locationProperty if placeholder is indeed a placeholder for a location property, the placeholder otherwise
2012 */
2013 public static Object replaceLocationPropertyPlaceholder(final Object placeholder, final Object locationProperty) {
2014 return isLocationPropertyPlaceholder(placeholder) ? locationProperty : placeholder;
2015 }
2016
2017 /**
2018 * Called from runtime internals to check if the passed value is a location property placeholder.
2019 * @param placeholder the value tested for being a placeholder for a location property
2020 * @return true if the value is a placeholder, false otherwise.
2021 */
2022 public static boolean isLocationPropertyPlaceholder(final Object placeholder) {
2023 return placeholder == LAZY_SENTINEL;
2024 }
2025
2026 /**
2027 * Create a new RegExp object.
2028 *
2029 * @param expression Regular expression.
2030 * @param options Search options.
2031 *
2032 * @return New RegExp object.
2033 */
2034 public static Object newRegExp(final String expression, final String options) {
2035 if (options == null) {
2036 return new NativeRegExp(expression);
2037 }
2038 return new NativeRegExp(expression, options);
2039 }
2040
2041 /**
2042 * Get the object prototype
2043 *
|