src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java

Print this page




  37 import java.lang.invoke.MethodHandles;
  38 import java.lang.invoke.MethodType;
  39 import java.lang.invoke.SwitchPoint;
  40 import java.lang.reflect.Field;
  41 import java.util.ArrayList;
  42 import java.util.Arrays;
  43 import java.util.List;
  44 import java.util.Map;
  45 import java.util.Objects;
  46 import java.util.concurrent.Callable;
  47 import java.util.concurrent.ConcurrentHashMap;
  48 import javax.script.ScriptContext;
  49 import javax.script.ScriptEngine;
  50 import jdk.internal.dynalink.CallSiteDescriptor;
  51 import jdk.internal.dynalink.linker.GuardedInvocation;
  52 import jdk.internal.dynalink.linker.LinkRequest;
  53 import jdk.nashorn.api.scripting.ClassFilter;
  54 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  55 import jdk.nashorn.internal.lookup.Lookup;
  56 import jdk.nashorn.internal.objects.annotations.Attribute;

  57 import jdk.nashorn.internal.objects.annotations.Property;
  58 import jdk.nashorn.internal.objects.annotations.ScriptClass;

  59 import jdk.nashorn.internal.runtime.Context;
  60 import jdk.nashorn.internal.runtime.ECMAErrors;
  61 import jdk.nashorn.internal.runtime.GlobalConstants;
  62 import jdk.nashorn.internal.runtime.GlobalFunctions;
  63 import jdk.nashorn.internal.runtime.JSType;
  64 import jdk.nashorn.internal.runtime.NativeJavaPackage;
  65 import jdk.nashorn.internal.runtime.PropertyDescriptor;
  66 import jdk.nashorn.internal.runtime.PropertyMap;
  67 import jdk.nashorn.internal.runtime.Scope;
  68 import jdk.nashorn.internal.runtime.ScriptEnvironment;
  69 import jdk.nashorn.internal.runtime.ScriptFunction;
  70 import jdk.nashorn.internal.runtime.ScriptObject;
  71 import jdk.nashorn.internal.runtime.ScriptRuntime;
  72 import jdk.nashorn.internal.runtime.ScriptingFunctions;
  73 import jdk.nashorn.internal.runtime.Specialization;
  74 import jdk.nashorn.internal.runtime.arrays.ArrayData;
  75 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  76 import jdk.nashorn.internal.runtime.linker.InvokeByName;
  77 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
  78 import jdk.nashorn.internal.runtime.regexp.RegExpResult;
  79 import jdk.nashorn.internal.scripts.JO;
  80 import jdk.nashorn.tools.ShellFunctions;
  81 
  82 /**
  83  * Representation of global scope.
  84  */
  85 @ScriptClass("Global")
  86 public final class Global extends ScriptObject implements Scope {
  87     // Placeholder value used in place of a location property (__FILE__, __DIR__, __LINE__)
  88     private static final Object LOCATION_PROPERTY_PLACEHOLDER = new Object();
  89     private final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
  90     private final InvokeByName VALUE_OF  = new InvokeByName("valueOf",  ScriptObject.class);
  91 



  92     /**
  93      * Optimistic builtin names that require switchpoint invalidation
  94      * upon assignment. Overly conservative, but works for now, to avoid
  95      * any complicated scope checks and especially heavy weight guards
  96      * like
  97      *
  98      * <pre>
  99      *     public boolean setterGuard(final Object receiver) {
 100      *         final Global          global = Global.instance();
 101      *         final ScriptObject    sobj   = global.getFunctionPrototype();
 102      *         final Object          apply  = sobj.get("apply");
 103      *         return apply == receiver;
 104      *     }
 105      * </pre>
 106      *
 107      * Naturally, checking for builtin classes like NativeFunction is cheaper,
 108      * it's when you start adding property checks for said builtins you have
 109      * problems with guard speed.
 110      */
 111 


 197     @Property(name = "Function", attributes = Attribute.NOT_ENUMERABLE)
 198     public volatile Object function;
 199 
 200     /** ECMA 15.1.4.3 Array constructor. */
 201     @Property(name = "Array", attributes = Attribute.NOT_ENUMERABLE)
 202     public volatile Object array;
 203 
 204     /** ECMA 15.1.4.4 String constructor */
 205     @Property(name = "String", attributes = Attribute.NOT_ENUMERABLE)
 206     public volatile Object string;
 207 
 208     /** ECMA 15.1.4.5 Boolean constructor */
 209     @Property(name = "Boolean", attributes = Attribute.NOT_ENUMERABLE)
 210     public volatile Object _boolean;
 211 
 212     /** ECMA 15.1.4.6 - Number constructor */
 213     @Property(name = "Number", attributes = Attribute.NOT_ENUMERABLE)
 214     public volatile Object number;
 215 
 216     /** ECMA 15.1.4.7 Date constructor */
 217     @Property(name = "Date", attributes = Attribute.NOT_ENUMERABLE)
 218     public volatile Object date;














 219 
 220     /** ECMA 15.1.4.8 RegExp constructor */
 221     @Property(name = "RegExp", attributes = Attribute.NOT_ENUMERABLE)
 222     public volatile Object regexp;














 223 
 224     /** ECMA 15.12 - The JSON object */
 225     @Property(name = "JSON", attributes = Attribute.NOT_ENUMERABLE)
 226     public volatile Object json;














 227 
 228     /** Nashorn extension: global.JSAdapter */
 229     @Property(name = "JSAdapter", attributes = Attribute.NOT_ENUMERABLE)
 230     public volatile Object jsadapter;














 231 
 232     /** ECMA 15.8 - The Math object */
 233     @Property(name = "Math", attributes = Attribute.NOT_ENUMERABLE)
 234     public volatile Object math;
 235 
 236     /** Error object */
 237     @Property(name = "Error", attributes = Attribute.NOT_ENUMERABLE)
 238     public volatile Object error;
 239 
 240     /** EvalError object */
 241     @Property(name = "EvalError", attributes = Attribute.NOT_ENUMERABLE)
 242     public volatile Object evalError;














 243 
 244     /** RangeError object */
 245     @Property(name = "RangeError", attributes = Attribute.NOT_ENUMERABLE)
 246     public volatile Object rangeError;














 247 
 248     /** ReferenceError object */
 249     @Property(name = "ReferenceError", attributes = Attribute.NOT_ENUMERABLE)
 250     public volatile Object referenceError;
 251 
 252     /** SyntaxError object */
 253     @Property(name = "SyntaxError", attributes = Attribute.NOT_ENUMERABLE)
 254     public volatile Object syntaxError;
 255 
 256     /** TypeError object */
 257     @Property(name = "TypeError", attributes = Attribute.NOT_ENUMERABLE)
 258     public volatile Object typeError;
 259 
 260     /** URIError object */
 261     @Property(name = "URIError", attributes = Attribute.NOT_ENUMERABLE)
 262     public volatile Object uriError;














 263 
 264     /** ArrayBuffer object */
 265     @Property(name = "ArrayBuffer", attributes = Attribute.NOT_ENUMERABLE)
 266     public volatile Object arrayBuffer;














 267 
 268     /** DataView object */
 269     @Property(name = "DataView", attributes = Attribute.NOT_ENUMERABLE)
 270     public volatile Object dataView;














 271 
 272     /** TypedArray (int8) */
 273     @Property(name = "Int8Array", attributes = Attribute.NOT_ENUMERABLE)
 274     public volatile Object int8Array;














 275 
 276     /** TypedArray (uint8) */
 277     @Property(name = "Uint8Array", attributes = Attribute.NOT_ENUMERABLE)
 278     public volatile Object uint8Array;














 279 
 280     /** TypedArray (uint8) - Clamped */
 281     @Property(name = "Uint8ClampedArray", attributes = Attribute.NOT_ENUMERABLE)
 282     public volatile Object uint8ClampedArray;














 283 
 284     /** TypedArray (int16) */
 285     @Property(name = "Int16Array", attributes = Attribute.NOT_ENUMERABLE)
 286     public volatile Object int16Array;














 287 
 288     /** TypedArray (uint16) */
 289     @Property(name = "Uint16Array", attributes = Attribute.NOT_ENUMERABLE)
 290     public volatile Object uint16Array;














 291 
 292     /** TypedArray (int32) */
 293     @Property(name = "Int32Array", attributes = Attribute.NOT_ENUMERABLE)
 294     public volatile Object int32Array;














 295 
 296     /** TypedArray (uint32) */
 297     @Property(name = "Uint32Array", attributes = Attribute.NOT_ENUMERABLE)
 298     public volatile Object uint32Array;














 299 
 300     /** TypedArray (float32) */
 301     @Property(name = "Float32Array", attributes = Attribute.NOT_ENUMERABLE)
 302     public volatile Object float32Array;














 303 
 304     /** TypedArray (float64) */
 305     @Property(name = "Float64Array", attributes = Attribute.NOT_ENUMERABLE)
 306     public volatile Object float64Array;














 307 
 308     /** Nashorn extension: Java access - global.Packages */
 309     @Property(name = "Packages", attributes = Attribute.NOT_ENUMERABLE)
 310     public volatile Object packages;
 311 
 312     /** Nashorn extension: Java access - global.com */
 313     @Property(attributes = Attribute.NOT_ENUMERABLE)
 314     public volatile Object com;
 315 
 316     /** Nashorn extension: Java access - global.edu */
 317     @Property(attributes = Attribute.NOT_ENUMERABLE)
 318     public volatile Object edu;
 319 
 320     /** Nashorn extension: Java access - global.java */
 321     @Property(attributes = Attribute.NOT_ENUMERABLE)
 322     public volatile Object java;
 323 
 324     /** Nashorn extension: Java access - global.javafx */
 325     @Property(attributes = Attribute.NOT_ENUMERABLE)
 326     public volatile Object javafx;
 327 
 328     /** Nashorn extension: Java access - global.javax */
 329     @Property(attributes = Attribute.NOT_ENUMERABLE)
 330     public volatile Object javax;
 331 
 332     /** Nashorn extension: Java access - global.org */
 333     @Property(attributes = Attribute.NOT_ENUMERABLE)
 334     public volatile Object org;
 335 
 336     /** Nashorn extension: Java access - global.javaImporter */
 337     @Property(name = "JavaImporter", attributes = Attribute.NOT_ENUMERABLE)
 338     public volatile Object javaImporter;














 339 
 340     /** Nashorn extension: global.Java Object constructor. */
 341     @Property(name = "Java", attributes = Attribute.NOT_ENUMERABLE)
 342     public volatile Object javaApi;














 343 
 344     /** Nashorn extension: current script's file name */
 345     @Property(name = "__FILE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
 346     public final Object __FILE__ = LOCATION_PROPERTY_PLACEHOLDER;
 347 
 348     /** Nashorn extension: current script's directory */
 349     @Property(name = "__DIR__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
 350     public final Object __DIR__ = LOCATION_PROPERTY_PLACEHOLDER;
 351 
 352     /** Nashorn extension: current source line number being executed */
 353     @Property(name = "__LINE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
 354     public final Object __LINE__ = LOCATION_PROPERTY_PLACEHOLDER;
 355 


 356     /** Used as Date.prototype's default value */
 357     public NativeDate   DEFAULT_DATE;




 358 
 359     /** Used as RegExp.prototype's default value */
 360     public NativeRegExp DEFAULT_REGEXP;


 361 
 362     /*
 363      * Built-in constructor objects: Even if user changes dynamic values of
 364      * "Object", "Array" etc., we still want to keep original values of these
 365      * constructors here. For example, we need to be able to create array,
 366      * regexp literals even after user overwrites global "Array" or "RegExp"
 367      * constructor - see also ECMA 262 spec. Annex D.
 368      */
 369     private ScriptFunction builtinFunction;
 370     private ScriptFunction builtinObject;
 371     private ScriptFunction builtinArray;
 372     private ScriptFunction builtinBoolean;
 373     private ScriptFunction builtinDate;
 374     private ScriptObject   builtinJSON;
 375     private ScriptFunction builtinJSAdapter;
 376     private ScriptObject   builtinMath;
 377     private ScriptFunction builtinNumber;
 378     private ScriptFunction builtinRegExp;
 379     private ScriptFunction builtinString;
 380     private ScriptFunction builtinError;


1040         return ScriptFunction.getPrototype(builtinObject);
1041     }
1042 
1043     ScriptObject getFunctionPrototype() {
1044         return ScriptFunction.getPrototype(builtinFunction);
1045     }
1046 
1047     ScriptObject getArrayPrototype() {
1048         return ScriptFunction.getPrototype(builtinArray);
1049     }
1050 
1051     ScriptObject getBooleanPrototype() {
1052         return ScriptFunction.getPrototype(builtinBoolean);
1053     }
1054 
1055     ScriptObject getNumberPrototype() {
1056         return ScriptFunction.getPrototype(builtinNumber);
1057     }
1058 
1059     ScriptObject getDatePrototype() {
1060         return ScriptFunction.getPrototype(builtinDate);
1061     }
1062 
1063     ScriptObject getRegExpPrototype() {
1064         return ScriptFunction.getPrototype(builtinRegExp);
1065     }
1066 
1067     ScriptObject getStringPrototype() {
1068         return ScriptFunction.getPrototype(builtinString);
1069     }
1070 
1071     ScriptObject getErrorPrototype() {
1072         return ScriptFunction.getPrototype(builtinError);
1073     }
1074 
1075     ScriptObject getEvalErrorPrototype() {
1076         return ScriptFunction.getPrototype(builtinEvalError);
1077     }
1078 
1079     ScriptObject getRangeErrorPrototype() {
1080         return ScriptFunction.getPrototype(builtinRangeError);
1081     }
1082 
1083     ScriptObject getReferenceErrorPrototype() {
1084         return ScriptFunction.getPrototype(builtinReferenceError);
1085     }
1086 
1087     ScriptObject getSyntaxErrorPrototype() {
1088         return ScriptFunction.getPrototype(builtinSyntaxError);
1089     }
1090 
1091     ScriptObject getTypeErrorPrototype() {
1092         return ScriptFunction.getPrototype(builtinTypeError);
1093     }
1094 
1095     ScriptObject getURIErrorPrototype() {
1096         return ScriptFunction.getPrototype(builtinURIError);
1097     }
1098 
1099     ScriptObject getJavaImporterPrototype() {
1100         return ScriptFunction.getPrototype(builtinJavaImporter);
1101     }
1102 
1103     ScriptObject getJSAdapterPrototype() {
1104         return ScriptFunction.getPrototype(builtinJSAdapter);







1105     }
1106 
1107     ScriptObject getArrayBufferPrototype() {
1108         return ScriptFunction.getPrototype(builtinArrayBuffer);







1109     }
1110 
1111     ScriptObject getDataViewPrototype() {
1112         return ScriptFunction.getPrototype(builtinDataView);







1113     }
1114 
1115     ScriptObject getInt8ArrayPrototype() {
1116         return ScriptFunction.getPrototype(builtinInt8Array);







1117     }
1118 
1119     ScriptObject getUint8ArrayPrototype() {
1120         return ScriptFunction.getPrototype(builtinUint8Array);







1121     }
1122 
1123     ScriptObject getUint8ClampedArrayPrototype() {
1124         return ScriptFunction.getPrototype(builtinUint8ClampedArray);







1125     }
1126 
1127     ScriptObject getInt16ArrayPrototype() {
1128         return ScriptFunction.getPrototype(builtinInt16Array);







1129     }
1130 
1131     ScriptObject getUint16ArrayPrototype() {
1132         return ScriptFunction.getPrototype(builtinUint16Array);







1133     }
1134 
1135     ScriptObject getInt32ArrayPrototype() {
1136         return ScriptFunction.getPrototype(builtinInt32Array);







1137     }
1138 
1139     ScriptObject getUint32ArrayPrototype() {
1140         return ScriptFunction.getPrototype(builtinUint32Array);







1141     }
1142 
1143     ScriptObject getFloat32ArrayPrototype() {
1144         return ScriptFunction.getPrototype(builtinFloat32Array);







1145     }
1146 
1147     ScriptObject getFloat64ArrayPrototype() {
1148         return ScriptFunction.getPrototype(builtinFloat64Array);
1149     }
1150 
1151     private ScriptFunction getBuiltinArray() {
1152         return builtinArray;
1153     }
1154 
1155     ScriptFunction getTypeErrorThrower() {
1156         return typeErrorThrower;
1157     }
1158 
1159     /**
1160      * Called from compiled script code to test if builtin has been overridden
1161      *
1162      * @return true if builtin array has not been overridden
1163      */
1164     public static boolean isBuiltinArray() {
1165         final Global instance = Global.instance();
1166         return instance.array == instance.getBuiltinArray();
1167     }
1168 
1169     private ScriptFunction getBuiltinBoolean() {
1170         return builtinBoolean;
1171     }
1172 
1173     /**
1174      * Called from compiled script code to test if builtin has been overridden
1175      *
1176      * @return true if builtin boolean has not been overridden
1177      */
1178     public static boolean isBuiltinBoolean() {
1179         final Global instance = Global.instance();
1180         return instance._boolean == instance.getBuiltinBoolean();
1181     }
1182 
1183     private ScriptFunction getBuiltinDate() {
1184         return builtinDate;






1185     }
1186 
1187     /**
1188      * Called from compiled script code to test if builtin has been overridden
1189      *
1190      * @return true if builtin date has not been overridden
1191      */
1192     public static boolean isBuiltinDate() {
1193         final Global instance = Global.instance();
1194         return instance.date == instance.getBuiltinDate();
1195     }
1196 
1197     private ScriptFunction getBuiltinError() {
1198         return builtinError;
1199     }
1200 
1201     /**
1202      * Called from compiled script code to test if builtin has been overridden
1203      *
1204      * @return true if builtin error has not been overridden
1205      */
1206     public static boolean isBuiltinError() {
1207         final Global instance = Global.instance();
1208         return instance.error == instance.getBuiltinError();
1209     }
1210 
1211     private ScriptFunction getBuiltinEvalError() {
1212         return builtinEvalError;



1213     }
1214 
1215     /**
1216      * Called from compiled script code to test if builtin has been overridden
1217      *
1218      * @return true if builtin eval error has not been overridden
1219      */
1220     public static boolean isBuiltinEvalError() {
1221         final Global instance = Global.instance();
1222         return instance.evalError == instance.getBuiltinEvalError();
1223     }
1224 
1225     private ScriptFunction getBuiltinFunction() {
1226         return builtinFunction;
1227     }
1228 
1229     /**
1230      * Called from compiled script code to test if builtin has been overridden
1231      *
1232      * @return true if builtin function has not been overridden
1233      */
1234     public static boolean isBuiltinFunction() {
1235         final Global instance = Global.instance();
1236         return instance.function == instance.getBuiltinFunction();
1237     }
1238 
1239     /**
1240      * Get the switchpoint used to check property changes for Function.prototype.apply
1241      * @return the switchpoint guarding apply (same as guarding call, and everything else in function)
1242      */


1253         final boolean isBuiltinFunction = instance.function == builtinFunction;
1254         return isBuiltinFunction && ScriptFunction.getPrototype(builtinFunction).getProperty(name).isBuiltin();
1255     }
1256 
1257     /**
1258      * Check if the Function.prototype.apply has not been replaced
1259      * @return true if Function.prototype.apply has been replaced
1260      */
1261     public static boolean isBuiltinFunctionPrototypeApply() {
1262         return isBuiltinFunctionProperty("apply");
1263     }
1264 
1265     /**
1266      * Check if the Function.prototype.apply has not been replaced
1267      * @return true if Function.prototype.call has been replaced
1268      */
1269     public static boolean isBuiltinFunctionPrototypeCall() {
1270         return isBuiltinFunctionProperty("call");
1271     }
1272 
1273     private ScriptFunction getBuiltinJSAdapter() {



1274         return builtinJSAdapter;
1275     }
1276 
1277     /**
1278      * Called from compiled script code to test if builtin has been overridden
1279      *
1280      * @return true if builtin JSAdapter has not been overridden
1281      */
1282     public static boolean isBuiltinJSAdapter() {
1283         final Global instance = Global.instance();
1284         return instance.jsadapter == instance.getBuiltinJSAdapter();
1285     }
1286 
1287     private ScriptObject getBuiltinJSON() {
1288         return builtinJSON;



1289     }
1290 
1291     /**
1292      * Called from compiled script code to test if builtin has been overridden
1293      *
1294      * @return true if builtin JSON has has not been overridden
1295      */
1296     public static boolean isBuiltinJSON() {
1297         final Global instance = Global.instance();
1298         return instance.json == instance.getBuiltinJSON();
1299     }
1300 
1301     private ScriptObject getBuiltinJava() {
1302         return builtinJava;
1303     }
1304 
1305     /**
1306      * Called from compiled script code to test if builtin has been overridden
1307      *
1308      * @return true if builtin Java has not been overridden
1309      */
1310     public static boolean isBuiltinJava() {
1311         final Global instance = Global.instance();
1312         return instance.java == instance.getBuiltinJava();
1313     }
1314 
1315     private ScriptObject getBuiltinJavax() {
1316         return builtinJavax;
1317     }
1318 
1319     /**
1320      * Called from compiled script code to test if builtin has been overridden
1321      *
1322      * @return true if builtin Javax has not been overridden
1323      */
1324     public static boolean isBuiltinJavax() {
1325         final Global instance = Global.instance();
1326         return instance.javax == instance.getBuiltinJavax();
1327     }
1328 
1329     private ScriptObject getBuiltinJavaImporter() {
1330         return builtinJavaImporter;










1331     }
1332 
1333     /**
1334      * Called from compiled script code to test if builtin has been overridden
1335      *
1336      * @return true if builtin Java importer has not been overridden
1337      */
1338     public static boolean isBuiltinJavaImporter() {
1339         final Global instance = Global.instance();
1340         return instance.javaImporter == instance.getBuiltinJavaImporter();
1341     }
1342 
1343     private ScriptObject getBuiltinMath() {
1344         return builtinMath;
1345     }
1346 
1347     /**
1348      * Called from compiled script code to test if builtin has been overridden
1349      *
1350      * @return true if builtin math has not been overridden
1351      */
1352     public static boolean isBuiltinMath() {
1353         final Global instance = Global.instance();
1354         return instance.math == instance.getBuiltinMath();
1355     }
1356 
1357     private ScriptFunction getBuiltinNumber() {
1358         return builtinNumber;
1359     }
1360 
1361     /**
1362      * Called from compiled script code to test if builtin has been overridden
1363      *
1364      * @return true if builtin number has not been overridden
1365      */
1366     public static boolean isBuiltinNumber() {
1367         final Global instance = Global.instance();
1368         return instance.number == instance.getBuiltinNumber();
1369     }
1370 
1371     private ScriptFunction getBuiltinObject() {
1372         return builtinObject;
1373     }
1374 


1379      */
1380     public static boolean isBuiltinObject() {
1381         final Global instance = Global.instance();
1382         return instance.object == instance.getBuiltinObject();
1383     }
1384 
1385     private ScriptObject getBuiltinPackages() {
1386         return builtinPackages;
1387     }
1388 
1389     /**
1390      * Called from compiled script code to test if builtin has been overridden
1391      *
1392      * @return true if builtin package has not been overridden
1393      */
1394     public static boolean isBuiltinPackages() {
1395         final Global instance = Global.instance();
1396         return instance.packages == instance.getBuiltinPackages();
1397     }
1398 
1399     private ScriptFunction getBuiltinRangeError() {



1400         return builtinRangeError;
1401     }
1402 
1403     /**
1404      * Called from compiled script code to test if builtin has been overridden
1405      *
1406      * @return true if builtin range error has not been overridden
1407      */
1408     public static boolean isBuiltinRangeError() {
1409         final Global instance = Global.instance();
1410         return instance.rangeError == instance.getBuiltinRangeError();
1411     }
1412 
1413     private ScriptFunction getBuiltinReferenceError() {
1414         return builtinReferenceError;
1415     }
1416 
1417     /**
1418      * Called from compiled script code to test if builtin has been overridden
1419      *
1420      * @return true if builtin reference error has not been overridden
1421      */
1422     public static boolean isBuiltinReferenceError() {
1423         final Global instance = Global.instance();
1424         return instance.referenceError == instance.getBuiltinReferenceError();
1425     }
1426 
1427     private ScriptFunction getBuiltinRegExp() {









1428         return builtinRegExp;
1429     }
1430 
1431     /**
1432      * Called from compiled script code to test if builtin has been overridden
1433      *
1434      * @return true if builtin regexp has not been overridden
1435      */
1436     public static boolean isBuiltinRegExp() {
1437         final Global instance = Global.instance();
1438         return instance.regexp == instance.getBuiltinRegExp();
1439     }
1440 
1441     private ScriptFunction getBuiltinString() {
1442         return builtinString;
1443     }
1444 
1445     /**
1446      * Called from compiled script code to test if builtin has been overridden
1447      *
1448      * @return true if builtin Java has not been overridden
1449      */
1450     public static boolean isBuiltinString() {
1451         final Global instance = Global.instance();
1452         return instance.string == instance.getBuiltinString();
1453     }
1454 
1455     private ScriptFunction getBuiltinSyntaxError() {
1456         return builtinSyntaxError;
1457     }
1458 


1463      */
1464     public static boolean isBuiltinSyntaxError() {
1465         final Global instance = Global.instance();
1466         return instance.syntaxError == instance.getBuiltinSyntaxError();
1467     }
1468 
1469     private ScriptFunction getBuiltinTypeError() {
1470         return builtinTypeError;
1471     }
1472 
1473     /**
1474      * Called from compiled script code to test if builtin has been overridden
1475      *
1476      * @return true if builtin type error has not been overridden
1477      */
1478     public static boolean isBuiltinTypeError() {
1479         final Global instance = Global.instance();
1480         return instance.typeError == instance.getBuiltinTypeError();
1481     }
1482 
1483     private ScriptFunction getBuiltinURIError() {
1484         return builtinURIError;



1485     }
1486 
1487     /**
1488      * Called from compiled script code to test if builtin has been overridden
1489      *
1490      * @return true if builtin URI error has not been overridden
1491      */
1492     public static boolean isBuiltinURIError() {
1493         final Global instance = Global.instance();
1494         return instance.uriError == instance.getBuiltinURIError();
1495     }
1496 
1497     @Override
1498     public String getClassName() {
1499         return "global";
1500     }
1501 
1502     /**
1503      * Copy function used to clone NativeRegExp objects.
1504      *
1505      * @param regexp a NativeRegExp to clone
1506      *
1507      * @return copy of the given regexp object
1508      */
1509     public static Object regExpCopy(final Object regexp) {
1510         return new NativeRegExp((NativeRegExp)regexp);
1511     }
1512 
1513     /**
1514      * Convert given object to NativeRegExp type.


1886                         new Specialization(GlobalFunctions.IS_NAN_J),
1887                         new Specialization(GlobalFunctions.IS_NAN_D) });
1888         this.parseFloat         = ScriptFunctionImpl.makeFunction("parseFloat", GlobalFunctions.PARSEFLOAT);
1889         this.isNaN              = ScriptFunctionImpl.makeFunction("isNaN",      GlobalFunctions.IS_NAN);
1890         this.isFinite           = ScriptFunctionImpl.makeFunction("isFinite",   GlobalFunctions.IS_FINITE);
1891         this.encodeURI          = ScriptFunctionImpl.makeFunction("encodeURI",  GlobalFunctions.ENCODE_URI);
1892         this.encodeURIComponent = ScriptFunctionImpl.makeFunction("encodeURIComponent", GlobalFunctions.ENCODE_URICOMPONENT);
1893         this.decodeURI          = ScriptFunctionImpl.makeFunction("decodeURI",  GlobalFunctions.DECODE_URI);
1894         this.decodeURIComponent = ScriptFunctionImpl.makeFunction("decodeURIComponent", GlobalFunctions.DECODE_URICOMPONENT);
1895         this.escape             = ScriptFunctionImpl.makeFunction("escape",     GlobalFunctions.ESCAPE);
1896         this.unescape           = ScriptFunctionImpl.makeFunction("unescape",   GlobalFunctions.UNESCAPE);
1897         this.print              = ScriptFunctionImpl.makeFunction("print",      env._print_no_newline ? PRINT : PRINTLN);
1898         this.load               = ScriptFunctionImpl.makeFunction("load",       LOAD);
1899         this.loadWithNewGlobal  = ScriptFunctionImpl.makeFunction("loadWithNewGlobal", LOAD_WITH_NEW_GLOBAL);
1900         this.exit               = ScriptFunctionImpl.makeFunction("exit",       EXIT);
1901         this.quit               = ScriptFunctionImpl.makeFunction("quit",       EXIT);
1902 
1903         // built-in constructors
1904         this.builtinArray     = initConstructorAndSwitchPoint("Array", ScriptFunction.class);
1905         this.builtinBoolean   = initConstructorAndSwitchPoint("Boolean", ScriptFunction.class);
1906         this.builtinDate      = initConstructorAndSwitchPoint("Date", ScriptFunction.class);
1907         this.builtinJSON      = initConstructorAndSwitchPoint("JSON", ScriptObject.class);
1908         this.builtinJSAdapter = initConstructorAndSwitchPoint("JSAdapter", ScriptFunction.class);
1909         this.builtinMath      = initConstructorAndSwitchPoint("Math", ScriptObject.class);
1910         this.builtinNumber    = initConstructorAndSwitchPoint("Number", ScriptFunction.class);
1911         this.builtinRegExp    = initConstructorAndSwitchPoint("RegExp", ScriptFunction.class);
1912         this.builtinString    = initConstructorAndSwitchPoint("String", ScriptFunction.class);

1913 
1914         // initialize String.prototype.length to 0
1915         // add String.prototype.length
1916         final ScriptObject stringPrototype = getStringPrototype();
1917         stringPrototype.addOwnProperty("length", Attribute.NON_ENUMERABLE_CONSTANT, 0.0);
1918 
1919         // set isArray flag on Array.prototype
1920         final ScriptObject arrayPrototype = getArrayPrototype();
1921         arrayPrototype.setIsArray();
1922 
1923         this.DEFAULT_DATE = new NativeDate(Double.NaN, this);
1924 
1925         // initialize default regexp object
1926         this.DEFAULT_REGEXP = new NativeRegExp("(?:)", this);
1927 
1928         // RegExp.prototype should behave like a RegExp object. So copy the
1929         // properties.
1930         final ScriptObject regExpProto = getRegExpPrototype();
1931         regExpProto.addBoundProperties(DEFAULT_REGEXP);
1932 
1933         // Error stuff
1934         initErrorObjects();
1935 
1936         // java access
1937         if (! env._no_java) {


1938             initJavaAccess();
1939         }
1940 
1941         if (! env._no_typed_arrays) {
1942             initTypedArray();










1943         }
1944 
1945         if (env._scripting) {
1946             initScripting(env);
1947         }
1948 
1949         if (Context.DEBUG) {
1950             boolean debugOkay;
1951             final SecurityManager sm = System.getSecurityManager();
1952             if (sm != null) {
1953                 try {
1954                     sm.checkPermission(new RuntimePermission(Context.NASHORN_DEBUG_MODE));
1955                     debugOkay = true;
1956                 } catch (final SecurityException ignored) {
1957                     // if no permission, don't initialize Debug object
1958                     debugOkay = false;
1959                 }
1960 
1961             } else {
1962                 debugOkay = true;


1996         errorProto.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
1997         final ScriptFunction getLineNumber = ScriptFunctionImpl.makeFunction("getLineNumber", NativeError.GET_LINENUMBER);
1998         final ScriptFunction setLineNumber = ScriptFunctionImpl.makeFunction("setLineNumber", NativeError.SET_LINENUMBER);
1999         errorProto.addOwnProperty("lineNumber", Attribute.NOT_ENUMERABLE, getLineNumber, setLineNumber);
2000         final ScriptFunction getColumnNumber = ScriptFunctionImpl.makeFunction("getColumnNumber", NativeError.GET_COLUMNNUMBER);
2001         final ScriptFunction setColumnNumber = ScriptFunctionImpl.makeFunction("setColumnNumber", NativeError.SET_COLUMNNUMBER);
2002         errorProto.addOwnProperty("columnNumber", Attribute.NOT_ENUMERABLE, getColumnNumber, setColumnNumber);
2003         final ScriptFunction getFileName = ScriptFunctionImpl.makeFunction("getFileName", NativeError.GET_FILENAME);
2004         final ScriptFunction setFileName = ScriptFunctionImpl.makeFunction("setFileName", NativeError.SET_FILENAME);
2005         errorProto.addOwnProperty("fileName", Attribute.NOT_ENUMERABLE, getFileName, setFileName);
2006 
2007         // ECMA 15.11.4.2 Error.prototype.name
2008         // Error.prototype.name = "Error";
2009         errorProto.set(NativeError.NAME, "Error", 0);
2010         // ECMA 15.11.4.3 Error.prototype.message
2011         // Error.prototype.message = "";
2012         errorProto.set(NativeError.MESSAGE, "", 0);
2013 
2014         tagBuiltinProperties("Error", builtinError);
2015 
2016         this.builtinEvalError = initErrorSubtype("EvalError", errorProto);
2017         this.builtinRangeError = initErrorSubtype("RangeError", errorProto);
2018         this.builtinReferenceError = initErrorSubtype("ReferenceError", errorProto);
2019         this.builtinSyntaxError = initErrorSubtype("SyntaxError", errorProto);
2020         this.builtinTypeError = initErrorSubtype("TypeError", errorProto);
2021         this.builtinURIError = initErrorSubtype("URIError", errorProto);
2022     }
2023 
2024     private ScriptFunction initErrorSubtype(final String name, final ScriptObject errorProto) {
2025         final ScriptFunction cons = initConstructor(name, ScriptFunction.class);
2026         final ScriptObject prototype = ScriptFunction.getPrototype(cons);
2027         prototype.set(NativeError.NAME, name, 0);
2028         prototype.set(NativeError.MESSAGE, "", 0);
2029         prototype.setInitialProto(errorProto);
2030         tagBuiltinProperties(name, cons);
2031         return cons;
2032     }
2033 
2034     private void initJavaAccess() {
2035         final ScriptObject objectProto = getObjectPrototype();
2036         this.builtinPackages = new NativeJavaPackage("", objectProto);
2037         this.builtinCom = new NativeJavaPackage("com", objectProto);
2038         this.builtinEdu = new NativeJavaPackage("edu", objectProto);
2039         this.builtinJava = new NativeJavaPackage("java", objectProto);
2040         this.builtinJavafx = new NativeJavaPackage("javafx", objectProto);
2041         this.builtinJavax = new NativeJavaPackage("javax", objectProto);
2042         this.builtinOrg = new NativeJavaPackage("org", objectProto);
2043         this.builtinJavaImporter = initConstructor("JavaImporter", ScriptFunction.class);
2044         this.builtinJavaApi = initConstructor("Java", ScriptObject.class);
2045     }
2046 
2047     private void initScripting(final ScriptEnvironment scriptEnv) {
2048         Object value;
2049         value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
2050         addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);
2051 
2052         value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
2053         addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);
2054 
2055         final String execName = ScriptingFunctions.EXEC_NAME;
2056         value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
2057         addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);
2058 
2059         // Nashorn extension: global.echo (scripting-mode-only)
2060         // alias for "print"
2061         value = get("print");
2062         addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);
2063 
2064         // Nashorn extension: global.$OPTIONS (scripting-mode-only)


2076         } else {
2077             addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
2078         }
2079 
2080         // add other special properties for exec support
2081         addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
2082         addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
2083         addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
2084     }
2085 
2086     private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
2087         for (final Field f : scriptEnv.getClass().getFields()) {
2088             try {
2089                 options.set(f.getName(), f.get(scriptEnv), 0);
2090             } catch (final IllegalArgumentException | IllegalAccessException exp) {
2091                 throw new RuntimeException(exp);
2092             }
2093         }
2094     }
2095 
2096     private void initTypedArray() {
2097         this.builtinArrayBuffer       = initConstructorAndSwitchPoint("ArrayBuffer", ScriptFunction.class);
2098         this.builtinDataView          = initConstructorAndSwitchPoint("DataView", ScriptFunction.class);
2099         this.builtinInt8Array         = initConstructorAndSwitchPoint("Int8Array", ScriptFunction.class);
2100         this.builtinUint8Array        = initConstructorAndSwitchPoint("Uint8Array", ScriptFunction.class);
2101         this.builtinUint8ClampedArray = initConstructorAndSwitchPoint("Uint8ClampedArray", ScriptFunction.class);
2102         this.builtinInt16Array        = initConstructorAndSwitchPoint("Int16Array", ScriptFunction.class);
2103         this.builtinUint16Array       = initConstructorAndSwitchPoint("Uint16Array", ScriptFunction.class);
2104         this.builtinInt32Array        = initConstructorAndSwitchPoint("Int32Array", ScriptFunction.class);
2105         this.builtinUint32Array       = initConstructorAndSwitchPoint("Uint32Array", ScriptFunction.class);
2106         this.builtinFloat32Array      = initConstructorAndSwitchPoint("Float32Array", ScriptFunction.class);
2107         this.builtinFloat64Array      = initConstructorAndSwitchPoint("Float64Array", ScriptFunction.class);
2108 
2109     }
2110 
2111     private void copyBuiltins() {
2112         this.array             = this.builtinArray;
2113         this._boolean          = this.builtinBoolean;
2114         this.date              = this.builtinDate;
2115         this.error             = this.builtinError;
2116         this.evalError         = this.builtinEvalError;
2117         this.function          = this.builtinFunction;
2118         this.jsadapter         = this.builtinJSAdapter;
2119         this.json              = this.builtinJSON;
2120         this.com               = this.builtinCom;
2121         this.edu               = this.builtinEdu;
2122         this.java              = this.builtinJava;
2123         this.javafx            = this.builtinJavafx;
2124         this.javax             = this.builtinJavax;
2125         this.org               = this.builtinOrg;
2126         this.javaImporter      = this.builtinJavaImporter;
2127         this.javaApi           = this.builtinJavaApi;
2128         this.math              = this.builtinMath;
2129         this.number            = this.builtinNumber;
2130         this.object            = this.builtinObject;
2131         this.packages          = this.builtinPackages;
2132         this.rangeError        = this.builtinRangeError;
2133         this.referenceError    = this.builtinReferenceError;
2134         this.regexp            = this.builtinRegExp;
2135         this.string            = this.builtinString;
2136         this.syntaxError       = this.builtinSyntaxError;
2137         this.typeError         = this.builtinTypeError;
2138         this.uriError          = this.builtinURIError;
2139         this.arrayBuffer       = this.builtinArrayBuffer;
2140         this.dataView          = this.builtinDataView;
2141         this.int8Array         = this.builtinInt8Array;
2142         this.uint8Array        = this.builtinUint8Array;
2143         this.uint8ClampedArray = this.builtinUint8ClampedArray;
2144         this.int16Array        = this.builtinInt16Array;
2145         this.uint16Array       = this.builtinUint16Array;
2146         this.int32Array        = this.builtinInt32Array;
2147         this.uint32Array       = this.builtinUint32Array;
2148         this.float32Array      = this.builtinFloat32Array;
2149         this.float64Array      = this.builtinFloat64Array;
2150     }
2151 
2152     private void initDebug() {
2153         this.addOwnProperty("Debug", Attribute.NOT_ENUMERABLE, initConstructor("Debug", ScriptObject.class));
2154     }
2155 
2156     private Object printImpl(final boolean newLine, final Object... objects) {
2157         @SuppressWarnings("resource")
2158         final PrintWriter out = scontext != null? new PrintWriter(scontext.getWriter()) : getContext().getEnv().getOut();
2159         final StringBuilder sb = new StringBuilder();
2160 
2161         for (final Object obj : objects) {
2162             if (sb.length() != 0) {
2163                 sb.append(' ');
2164             }
2165 
2166             sb.append(JSType.toString(obj));
2167         }
2168 
2169         // Print all at once to ensure thread friendly result.




  37 import java.lang.invoke.MethodHandles;
  38 import java.lang.invoke.MethodType;
  39 import java.lang.invoke.SwitchPoint;
  40 import java.lang.reflect.Field;
  41 import java.util.ArrayList;
  42 import java.util.Arrays;
  43 import java.util.List;
  44 import java.util.Map;
  45 import java.util.Objects;
  46 import java.util.concurrent.Callable;
  47 import java.util.concurrent.ConcurrentHashMap;
  48 import javax.script.ScriptContext;
  49 import javax.script.ScriptEngine;
  50 import jdk.internal.dynalink.CallSiteDescriptor;
  51 import jdk.internal.dynalink.linker.GuardedInvocation;
  52 import jdk.internal.dynalink.linker.LinkRequest;
  53 import jdk.nashorn.api.scripting.ClassFilter;
  54 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  55 import jdk.nashorn.internal.lookup.Lookup;
  56 import jdk.nashorn.internal.objects.annotations.Attribute;
  57 import jdk.nashorn.internal.objects.annotations.Getter;
  58 import jdk.nashorn.internal.objects.annotations.Property;
  59 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  60 import jdk.nashorn.internal.objects.annotations.Setter;
  61 import jdk.nashorn.internal.runtime.Context;
  62 import jdk.nashorn.internal.runtime.ECMAErrors;
  63 import jdk.nashorn.internal.runtime.GlobalConstants;
  64 import jdk.nashorn.internal.runtime.GlobalFunctions;
  65 import jdk.nashorn.internal.runtime.JSType;
  66 import jdk.nashorn.internal.runtime.NativeJavaPackage;
  67 import jdk.nashorn.internal.runtime.PropertyDescriptor;
  68 import jdk.nashorn.internal.runtime.PropertyMap;
  69 import jdk.nashorn.internal.runtime.Scope;
  70 import jdk.nashorn.internal.runtime.ScriptEnvironment;
  71 import jdk.nashorn.internal.runtime.ScriptFunction;
  72 import jdk.nashorn.internal.runtime.ScriptObject;
  73 import jdk.nashorn.internal.runtime.ScriptRuntime;
  74 import jdk.nashorn.internal.runtime.ScriptingFunctions;
  75 import jdk.nashorn.internal.runtime.Specialization;
  76 import jdk.nashorn.internal.runtime.arrays.ArrayData;
  77 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  78 import jdk.nashorn.internal.runtime.linker.InvokeByName;
  79 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
  80 import jdk.nashorn.internal.runtime.regexp.RegExpResult;
  81 import jdk.nashorn.internal.scripts.JO;
  82 import jdk.nashorn.tools.ShellFunctions;
  83 
  84 /**
  85  * Representation of global scope.
  86  */
  87 @ScriptClass("Global")
  88 public final class Global extends ScriptObject implements Scope {
  89     // Placeholder value used in place of a location property (__FILE__, __DIR__, __LINE__)
  90     private static final Object LOCATION_PROPERTY_PLACEHOLDER = new Object();
  91     private final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
  92     private final InvokeByName VALUE_OF  = new InvokeByName("valueOf",  ScriptObject.class);
  93 
  94     // placeholder value for lazily initialized global objects
  95     private static final Object LAZY_SENTINEL = new Object();
  96 
  97     /**
  98      * Optimistic builtin names that require switchpoint invalidation
  99      * upon assignment. Overly conservative, but works for now, to avoid
 100      * any complicated scope checks and especially heavy weight guards
 101      * like
 102      *
 103      * <pre>
 104      *     public boolean setterGuard(final Object receiver) {
 105      *         final Global          global = Global.instance();
 106      *         final ScriptObject    sobj   = global.getFunctionPrototype();
 107      *         final Object          apply  = sobj.get("apply");
 108      *         return apply == receiver;
 109      *     }
 110      * </pre>
 111      *
 112      * Naturally, checking for builtin classes like NativeFunction is cheaper,
 113      * it's when you start adding property checks for said builtins you have
 114      * problems with guard speed.
 115      */
 116 


 202     @Property(name = "Function", attributes = Attribute.NOT_ENUMERABLE)
 203     public volatile Object function;
 204 
 205     /** ECMA 15.1.4.3 Array constructor. */
 206     @Property(name = "Array", attributes = Attribute.NOT_ENUMERABLE)
 207     public volatile Object array;
 208 
 209     /** ECMA 15.1.4.4 String constructor */
 210     @Property(name = "String", attributes = Attribute.NOT_ENUMERABLE)
 211     public volatile Object string;
 212 
 213     /** ECMA 15.1.4.5 Boolean constructor */
 214     @Property(name = "Boolean", attributes = Attribute.NOT_ENUMERABLE)
 215     public volatile Object _boolean;
 216 
 217     /** ECMA 15.1.4.6 - Number constructor */
 218     @Property(name = "Number", attributes = Attribute.NOT_ENUMERABLE)
 219     public volatile Object number;
 220 
 221     /** ECMA 15.1.4.7 Date constructor */
 222     @Getter(name = "Date", attributes = Attribute.NOT_ENUMERABLE)
 223     public static Object getDate(final Object self) {
 224         final Global global = Global.instanceFrom(self);
 225         if (global.date == LAZY_SENTINEL) {
 226             global.date = global.getBuiltinDate();
 227         }
 228         return global.date;
 229     }
 230 
 231     @Setter(name = "Date", attributes = Attribute.NOT_ENUMERABLE)
 232     public static void setDate(final Object self, final Object value) {
 233         final Global global = Global.instanceFrom(self);
 234         global.date = value;
 235     }
 236 
 237     private volatile Object date = LAZY_SENTINEL;
 238 
 239     /** ECMA 15.1.4.8 RegExp constructor */
 240     @Getter(name = "RegExp", attributes = Attribute.NOT_ENUMERABLE)
 241     public static Object getRegExp(final Object self) {
 242         final Global global = Global.instanceFrom(self);
 243         if (global.regexp == LAZY_SENTINEL) {
 244             global.regexp = global.getBuiltinRegExp();
 245         }
 246         return global.regexp;
 247     }
 248 
 249     @Setter(name = "RegExp", attributes = Attribute.NOT_ENUMERABLE)
 250     public static void setRegExp(final Object self, final Object value) {
 251         final Global global = Global.instanceFrom(self);
 252         global.regexp = value;
 253     }
 254 
 255     private volatile Object regexp = LAZY_SENTINEL;
 256 
 257     /** ECMA 15.12 - The JSON object */
 258     @Getter(name = "JSON", attributes = Attribute.NOT_ENUMERABLE)
 259     public static Object getJSON(final Object self) {
 260         final Global global = Global.instanceFrom(self);
 261         if (global.json == LAZY_SENTINEL) {
 262             global.json = global.getBuiltinJSON();
 263         }
 264         return global.json;
 265     }
 266 
 267     @Setter(name = "JSON", attributes = Attribute.NOT_ENUMERABLE)
 268     public static void setJSON(final Object self, final Object value) {
 269         final Global global = Global.instanceFrom(self);
 270         global.json = value;
 271     }
 272 
 273     private volatile Object json = LAZY_SENTINEL;
 274 
 275     /** Nashorn extension: global.JSAdapter */
 276     @Getter(name = "JSAdapter", attributes = Attribute.NOT_ENUMERABLE)
 277     public static Object getJSAdapter(final Object self) {
 278         final Global global = Global.instanceFrom(self);
 279         if (global.jsadapter == LAZY_SENTINEL) {
 280             global.jsadapter = global.getBuiltinJSAdapter();
 281         }
 282         return global.jsadapter;
 283     }
 284 
 285     @Setter(name = "JSAdapter", attributes = Attribute.NOT_ENUMERABLE)
 286     public static void setJSAdapter(final Object self, final Object value) {
 287         final Global global = Global.instanceFrom(self);
 288         global.jsadapter = value;
 289     }
 290 
 291     private volatile Object jsadapter = LAZY_SENTINEL;
 292 
 293     /** ECMA 15.8 - The Math object */
 294     @Property(name = "Math", attributes = Attribute.NOT_ENUMERABLE)
 295     public volatile Object math;
 296 
 297     /** Error object */
 298     @Property(name = "Error", attributes = Attribute.NOT_ENUMERABLE)
 299     public volatile Object error;
 300 
 301     /** EvalError object */
 302     @Getter(name = "EvalError", attributes = Attribute.NOT_ENUMERABLE)
 303     public static Object getEvalError(final Object self) {
 304         final Global global = Global.instanceFrom(self);
 305         if (global.evalError == LAZY_SENTINEL) {
 306             global.evalError = global.getBuiltinEvalError();
 307         }
 308         return global.evalError;
 309     }
 310 
 311     @Setter(name = "EvalError", attributes = Attribute.NOT_ENUMERABLE)
 312     public static void setEvalError(final Object self, final Object value) {
 313         final Global global = Global.instanceFrom(self);
 314         global.evalError = value;
 315     }
 316 
 317     private volatile Object evalError = LAZY_SENTINEL;
 318 
 319     /** RangeError object */
 320     @Getter(name = "RangeError", attributes = Attribute.NOT_ENUMERABLE)
 321     public static Object getRangeError(final Object self) {
 322         final Global global = Global.instanceFrom(self);
 323         if (global.rangeError == LAZY_SENTINEL) {
 324             global.rangeError = global.getBuiltinRangeError();
 325         }
 326         return global.rangeError;
 327     }
 328 
 329     @Setter(name = "RangeError", attributes = Attribute.NOT_ENUMERABLE)
 330     public static void setRangeError(final Object self, final Object value) {
 331         final Global global = Global.instanceFrom(self);
 332         global.rangeError = value;
 333     }
 334 
 335     private volatile Object rangeError = LAZY_SENTINEL;
 336 
 337     /** ReferenceError object */
 338     @Property(name = "ReferenceError", attributes = Attribute.NOT_ENUMERABLE)
 339     public volatile Object referenceError;
 340 
 341     /** SyntaxError object */
 342     @Property(name = "SyntaxError", attributes = Attribute.NOT_ENUMERABLE)
 343     public volatile Object syntaxError;
 344 
 345     /** TypeError object */
 346     @Property(name = "TypeError", attributes = Attribute.NOT_ENUMERABLE)
 347     public volatile Object typeError;
 348 
 349     /** URIError object */
 350     @Getter(name = "URIError", attributes = Attribute.NOT_ENUMERABLE)
 351     public static Object getURIError(final Object self) {
 352         final Global global = Global.instanceFrom(self);
 353         if (global.uriError == LAZY_SENTINEL) {
 354             global.uriError = global.getBuiltinURIError();
 355         }
 356         return global.uriError;
 357     }
 358 
 359     @Setter(name = "URIError", attributes = Attribute.NOT_ENUMERABLE)
 360     public static void setURIError(final Object self, final Object value) {
 361         final Global global = Global.instanceFrom(self);
 362         global.uriError = value;
 363     }
 364 
 365     private volatile Object uriError = LAZY_SENTINEL;
 366 
 367     /** ArrayBuffer object */
 368     @Getter(name = "ArrayBuffer", attributes = Attribute.NOT_ENUMERABLE)
 369     public static Object getArrayBuffer(final Object self) {
 370         final Global global = Global.instanceFrom(self);
 371         if (global.arrayBuffer == LAZY_SENTINEL) {
 372             global.arrayBuffer = global.getBuiltinArrayBuffer();
 373         }
 374         return global.arrayBuffer;
 375     }
 376 
 377     @Setter(name = "ArrayBuffer", attributes = Attribute.NOT_ENUMERABLE)
 378     public static void setArrayBuffer(final Object self, final Object value) {
 379         final Global global = Global.instanceFrom(self);
 380         global.arrayBuffer = value;
 381     }
 382 
 383     private volatile Object arrayBuffer;
 384 
 385     /** DataView object */
 386     @Getter(name = "DataView", attributes = Attribute.NOT_ENUMERABLE)
 387     public static Object getDataView(final Object self) {
 388         final Global global = Global.instanceFrom(self);
 389         if (global.dataView == LAZY_SENTINEL) {
 390             global.dataView = global.getBuiltinDataView();
 391         }
 392         return global.dataView;
 393     }
 394 
 395     @Setter(name = "DataView", attributes = Attribute.NOT_ENUMERABLE)
 396     public static void setDataView(final Object self, final Object value) {
 397         final Global global = Global.instanceFrom(self);
 398         global.dataView = value;
 399     }
 400 
 401     private volatile Object dataView;
 402 
 403     /** TypedArray (int8) */
 404     @Getter(name = "Int8Array", attributes = Attribute.NOT_ENUMERABLE)
 405     public static Object getInt8Array(final Object self) {
 406         final Global global = Global.instanceFrom(self);
 407         if (global.int8Array == LAZY_SENTINEL) {
 408             global.int8Array = global.getBuiltinInt8Array();
 409         }
 410         return global.int8Array;
 411     }
 412 
 413     @Setter(name = "Int8Array", attributes = Attribute.NOT_ENUMERABLE)
 414     public static void setInt8Array(final Object self, final Object value) {
 415         final Global global = Global.instanceFrom(self);
 416         global.int8Array = value;
 417     }
 418 
 419     private volatile Object int8Array;
 420 
 421     /** TypedArray (uint8) */
 422     @Getter(name = "Uint8Array", attributes = Attribute.NOT_ENUMERABLE)
 423     public static Object getUint8Array(final Object self) {
 424         final Global global = Global.instanceFrom(self);
 425         if (global.uint8Array == LAZY_SENTINEL) {
 426             global.uint8Array = global.getBuiltinUint8Array();
 427         }
 428         return global.uint8Array;
 429     }
 430 
 431     @Setter(name = "Uint8Array", attributes = Attribute.NOT_ENUMERABLE)
 432     public static void setUint8Array(final Object self, final Object value) {
 433         final Global global = Global.instanceFrom(self);
 434         global.uint8Array = value;
 435     }
 436 
 437     private volatile Object uint8Array;
 438 
 439     /** TypedArray (uint8) - Clamped */
 440     @Getter(name = "Uint8ClampedArray", attributes = Attribute.NOT_ENUMERABLE)
 441     public static Object getUint8ClampedArray(final Object self) {
 442         final Global global = Global.instanceFrom(self);
 443         if (global.uint8ClampedArray == LAZY_SENTINEL) {
 444             global.uint8ClampedArray = global.getBuiltinUint8ClampedArray();
 445         }
 446         return global.uint8ClampedArray;
 447     }
 448 
 449     @Setter(name = "Uint8ClampedArray", attributes = Attribute.NOT_ENUMERABLE)
 450     public static void setUint8ClampedArray(final Object self, final Object value) {
 451         final Global global = Global.instanceFrom(self);
 452         global.uint8ClampedArray = value;
 453     }
 454 
 455     private volatile Object uint8ClampedArray;
 456 
 457     /** TypedArray (int16) */
 458     @Getter(name = "Int16Array", attributes = Attribute.NOT_ENUMERABLE)
 459     public static Object getInt16Array(final Object self) {
 460         final Global global = Global.instanceFrom(self);
 461         if (global.int16Array == LAZY_SENTINEL) {
 462             global.int16Array = global.getBuiltinInt16Array();
 463         }
 464         return global.int16Array;
 465     }
 466 
 467     @Setter(name = "Int16Array", attributes = Attribute.NOT_ENUMERABLE)
 468     public static void setInt16Array(final Object self, final Object value) {
 469         final Global global = Global.instanceFrom(self);
 470         global.int16Array = value;
 471     }
 472 
 473     private volatile Object int16Array;
 474 
 475     /** TypedArray (uint16) */
 476     @Getter(name = "Uint16Array", attributes = Attribute.NOT_ENUMERABLE)
 477     public static Object getUint16Array(final Object self) {
 478         final Global global = Global.instanceFrom(self);
 479         if (global.uint16Array == LAZY_SENTINEL) {
 480             global.uint16Array = global.getBuiltinUint16Array();
 481         }
 482         return global.uint16Array;
 483     }
 484 
 485     @Setter(name = "Uint16Array", attributes = Attribute.NOT_ENUMERABLE)
 486     public static void setUint16Array(final Object self, final Object value) {
 487         final Global global = Global.instanceFrom(self);
 488         global.uint16Array = value;
 489     }
 490 
 491     private volatile Object uint16Array;
 492 
 493     /** TypedArray (int32) */
 494     @Getter(name = "Int32Array", attributes = Attribute.NOT_ENUMERABLE)
 495     public static Object getInt32Array(final Object self) {
 496         final Global global = Global.instanceFrom(self);
 497         if (global.int32Array == LAZY_SENTINEL) {
 498             global.int32Array = global.getBuiltinInt32Array();
 499         }
 500         return global.int32Array;
 501     }
 502 
 503     @Setter(name = "Int32Array", attributes = Attribute.NOT_ENUMERABLE)
 504     public static void setInt32Array(final Object self, final Object value) {
 505         final Global global = Global.instanceFrom(self);
 506         global.int32Array = value;
 507     }
 508 
 509     private volatile Object int32Array;
 510 
 511     /** TypedArray (uint32) */
 512     @Getter(name = "Uint32Array", attributes = Attribute.NOT_ENUMERABLE)
 513     public static Object getUint32Array(final Object self) {
 514         final Global global = Global.instanceFrom(self);
 515         if (global.uint32Array == LAZY_SENTINEL) {
 516             global.uint32Array = global.getBuiltinUint32Array();
 517         }
 518         return global.uint32Array;
 519     }
 520 
 521     @Setter(name = "Uint32Array", attributes = Attribute.NOT_ENUMERABLE)
 522     public static void setUint32Array(final Object self, final Object value) {
 523         final Global global = Global.instanceFrom(self);
 524         global.uint32Array = value;
 525     }
 526 
 527     private volatile Object uint32Array;
 528 
 529     /** TypedArray (float32) */
 530     @Getter(name = "Float32Array", attributes = Attribute.NOT_ENUMERABLE)
 531     public static Object getFloat32Array(final Object self) {
 532         final Global global = Global.instanceFrom(self);
 533         if (global.float32Array == LAZY_SENTINEL) {
 534             global.float32Array = global.getBuiltinFloat32Array();
 535         }
 536         return global.float32Array;
 537     }
 538 
 539     @Setter(name = "Float32Array", attributes = Attribute.NOT_ENUMERABLE)
 540     public static void setFloat32Array(final Object self, final Object value) {
 541         final Global global = Global.instanceFrom(self);
 542         global.float32Array = value;
 543     }
 544 
 545     private volatile Object float32Array;
 546 
 547     /** TypedArray (float64) */
 548     @Getter(name = "Float64Array", attributes = Attribute.NOT_ENUMERABLE)
 549     public static Object getFloat64Array(final Object self) {
 550         final Global global = Global.instanceFrom(self);
 551         if (global.float64Array == LAZY_SENTINEL) {
 552             global.float64Array = global.getBuiltinFloat64Array();
 553         }
 554         return global.float64Array;
 555     }
 556 
 557     @Setter(name = "Float64Array", attributes = Attribute.NOT_ENUMERABLE)
 558     public static void setFloat64Array(final Object self, final Object value) {
 559         final Global global = Global.instanceFrom(self);
 560         global.float64Array = value;
 561     }
 562 
 563     private volatile Object float64Array;
 564 
 565     /** Nashorn extension: Java access - global.Packages */
 566     @Property(name = "Packages", attributes = Attribute.NOT_ENUMERABLE)
 567     public volatile Object packages;
 568 
 569     /** Nashorn extension: Java access - global.com */
 570     @Property(attributes = Attribute.NOT_ENUMERABLE)
 571     public volatile Object com;
 572 
 573     /** Nashorn extension: Java access - global.edu */
 574     @Property(attributes = Attribute.NOT_ENUMERABLE)
 575     public volatile Object edu;
 576 
 577     /** Nashorn extension: Java access - global.java */
 578     @Property(attributes = Attribute.NOT_ENUMERABLE)
 579     public volatile Object java;
 580 
 581     /** Nashorn extension: Java access - global.javafx */
 582     @Property(attributes = Attribute.NOT_ENUMERABLE)
 583     public volatile Object javafx;
 584 
 585     /** Nashorn extension: Java access - global.javax */
 586     @Property(attributes = Attribute.NOT_ENUMERABLE)
 587     public volatile Object javax;
 588 
 589     /** Nashorn extension: Java access - global.org */
 590     @Property(attributes = Attribute.NOT_ENUMERABLE)
 591     public volatile Object org;
 592 
 593     /** Nashorn extension: Java access - global.javaImporter */
 594     @Getter(name = "JavaImporter", attributes = Attribute.NOT_ENUMERABLE)
 595     public static Object getJavaImporter(final Object self) {
 596         final Global global = Global.instanceFrom(self);
 597         if (global.javaImporter == LAZY_SENTINEL) {
 598             global.javaImporter = global.getBuiltinJavaImporter();
 599         }
 600         return global.javaImporter;
 601     }
 602 
 603     @Setter(name = "JavaImporter", attributes = Attribute.NOT_ENUMERABLE)
 604     public static void setJavaImporter(final Object self, final Object value) {
 605         final Global global = Global.instanceFrom(self);
 606         global.javaImporter = value;
 607     }
 608 
 609     private volatile Object javaImporter;
 610 
 611     /** Nashorn extension: global.Java Object constructor. */
 612     @Getter(name = "Java", attributes = Attribute.NOT_ENUMERABLE)
 613     public static Object getJavaApi(final Object self) {
 614         final Global global = Global.instanceFrom(self);
 615         if (global.javaApi == LAZY_SENTINEL) {
 616             global.javaApi = global.getBuiltinJavaApi();
 617         }
 618         return global.javaApi;
 619     }
 620 
 621     @Setter(name = "Java", attributes = Attribute.NOT_ENUMERABLE)
 622     public static void setJavaApi(final Object self, final Object value) {
 623         final Global global = Global.instanceFrom(self);
 624         global.javaApi = value;
 625     }
 626 
 627     private volatile Object javaApi;
 628 
 629     /** Nashorn extension: current script's file name */
 630     @Property(name = "__FILE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
 631     public final Object __FILE__ = LOCATION_PROPERTY_PLACEHOLDER;
 632 
 633     /** Nashorn extension: current script's directory */
 634     @Property(name = "__DIR__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
 635     public final Object __DIR__ = LOCATION_PROPERTY_PLACEHOLDER;
 636 
 637     /** Nashorn extension: current source line number being executed */
 638     @Property(name = "__LINE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
 639     public final Object __LINE__ = LOCATION_PROPERTY_PLACEHOLDER;
 640 
 641     private volatile NativeDate DEFAULT_DATE;
 642 
 643     /** Used as Date.prototype's default value */
 644     NativeDate getDefaultDate() {
 645         return DEFAULT_DATE;
 646     }
 647 
 648     private volatile NativeRegExp DEFAULT_REGEXP;
 649 
 650     /** Used as RegExp.prototype's default value */
 651     NativeRegExp getDefaultRegExp() {
 652         return DEFAULT_REGEXP;
 653     }
 654 
 655     /*
 656      * Built-in constructor objects: Even if user changes dynamic values of
 657      * "Object", "Array" etc., we still want to keep original values of these
 658      * constructors here. For example, we need to be able to create array,
 659      * regexp literals even after user overwrites global "Array" or "RegExp"
 660      * constructor - see also ECMA 262 spec. Annex D.
 661      */
 662     private ScriptFunction builtinFunction;
 663     private ScriptFunction builtinObject;
 664     private ScriptFunction builtinArray;
 665     private ScriptFunction builtinBoolean;
 666     private ScriptFunction builtinDate;
 667     private ScriptObject   builtinJSON;
 668     private ScriptFunction builtinJSAdapter;
 669     private ScriptObject   builtinMath;
 670     private ScriptFunction builtinNumber;
 671     private ScriptFunction builtinRegExp;
 672     private ScriptFunction builtinString;
 673     private ScriptFunction builtinError;


1333         return ScriptFunction.getPrototype(builtinObject);
1334     }
1335 
1336     ScriptObject getFunctionPrototype() {
1337         return ScriptFunction.getPrototype(builtinFunction);
1338     }
1339 
1340     ScriptObject getArrayPrototype() {
1341         return ScriptFunction.getPrototype(builtinArray);
1342     }
1343 
1344     ScriptObject getBooleanPrototype() {
1345         return ScriptFunction.getPrototype(builtinBoolean);
1346     }
1347 
1348     ScriptObject getNumberPrototype() {
1349         return ScriptFunction.getPrototype(builtinNumber);
1350     }
1351 
1352     ScriptObject getDatePrototype() {
1353         return ScriptFunction.getPrototype(getBuiltinDate());
1354     }
1355 
1356     ScriptObject getRegExpPrototype() {
1357         return ScriptFunction.getPrototype(getBuiltinRegExp());
1358     }
1359 
1360     ScriptObject getStringPrototype() {
1361         return ScriptFunction.getPrototype(builtinString);
1362     }
1363 
1364     ScriptObject getErrorPrototype() {
1365         return ScriptFunction.getPrototype(builtinError);
1366     }
1367 
1368     ScriptObject getEvalErrorPrototype() {
1369         return ScriptFunction.getPrototype(getBuiltinEvalError());
1370     }
1371 
1372     ScriptObject getRangeErrorPrototype() {
1373         return ScriptFunction.getPrototype(getBuiltinRangeError());
1374     }
1375 
1376     ScriptObject getReferenceErrorPrototype() {
1377         return ScriptFunction.getPrototype(builtinReferenceError);
1378     }
1379 
1380     ScriptObject getSyntaxErrorPrototype() {
1381         return ScriptFunction.getPrototype(builtinSyntaxError);
1382     }
1383 
1384     ScriptObject getTypeErrorPrototype() {
1385         return ScriptFunction.getPrototype(builtinTypeError);
1386     }
1387 
1388     ScriptObject getURIErrorPrototype() {
1389         return ScriptFunction.getPrototype(getBuiltinURIError());
1390     }
1391 
1392     ScriptObject getJavaImporterPrototype() {
1393         return ScriptFunction.getPrototype(getBuiltinJavaImporter());
1394     }
1395 
1396     ScriptObject getJSAdapterPrototype() {
1397         return ScriptFunction.getPrototype(getBuiltinJSAdapter());
1398     }
1399 
1400     private synchronized ScriptFunction getBuiltinArrayBuffer() {
1401         if (this.builtinArrayBuffer == null) {
1402             this.builtinArrayBuffer = initConstructorAndSwitchPoint("ArrayBuffer", ScriptFunction.class);
1403         }
1404         return this.builtinArrayBuffer;
1405     }
1406 
1407     ScriptObject getArrayBufferPrototype() {
1408         return ScriptFunction.getPrototype(getBuiltinArrayBuffer());
1409     }
1410 
1411     private synchronized ScriptFunction getBuiltinDataView() {
1412         if (this.builtinDataView == null) {
1413             this.builtinDataView = initConstructorAndSwitchPoint("DataView", ScriptFunction.class);
1414         }
1415         return this.builtinDataView;
1416     }
1417 
1418     ScriptObject getDataViewPrototype() {
1419         return ScriptFunction.getPrototype(getBuiltinDataView());
1420     }
1421 
1422     private synchronized ScriptFunction getBuiltinInt8Array() {
1423         if (this.builtinInt8Array == null) {
1424             this.builtinInt8Array = initConstructorAndSwitchPoint("Int8Array", ScriptFunction.class);
1425         }
1426         return this.builtinInt8Array;
1427     }
1428 
1429     ScriptObject getInt8ArrayPrototype() {
1430         return ScriptFunction.getPrototype(getBuiltinInt8Array());
1431     }
1432 
1433     private synchronized ScriptFunction getBuiltinUint8Array() {
1434         if (this.builtinUint8Array == null) {
1435             this.builtinUint8Array = initConstructorAndSwitchPoint("Uint8Array", ScriptFunction.class);
1436         }
1437         return this.builtinUint8Array;
1438     }
1439 
1440     ScriptObject getUint8ArrayPrototype() {
1441         return ScriptFunction.getPrototype(getBuiltinUint8Array());
1442     }
1443 
1444     private synchronized ScriptFunction getBuiltinUint8ClampedArray() {
1445         if (this.builtinUint8ClampedArray == null) {
1446             this.builtinUint8ClampedArray = initConstructorAndSwitchPoint("Uint8ClampedArray", ScriptFunction.class);
1447         }
1448         return this.builtinUint8ClampedArray;
1449     }
1450 
1451     ScriptObject getUint8ClampedArrayPrototype() {
1452         return ScriptFunction.getPrototype(getBuiltinUint8ClampedArray());
1453     }
1454 
1455     private synchronized ScriptFunction getBuiltinInt16Array() {
1456         if (this.builtinInt16Array == null) {
1457             this.builtinInt16Array = initConstructorAndSwitchPoint("Int16Array", ScriptFunction.class);
1458         }
1459         return this.builtinInt16Array;
1460     }
1461 
1462     ScriptObject getInt16ArrayPrototype() {
1463         return ScriptFunction.getPrototype(getBuiltinInt16Array());
1464     }
1465 
1466     private synchronized ScriptFunction getBuiltinUint16Array() {
1467         if (this.builtinUint16Array == null) {
1468             this.builtinUint16Array = initConstructorAndSwitchPoint("Uint16Array", ScriptFunction.class);
1469         }
1470         return this.builtinUint16Array;
1471     }
1472 
1473     ScriptObject getUint16ArrayPrototype() {
1474         return ScriptFunction.getPrototype(getBuiltinUint16Array());
1475     }
1476 
1477     private synchronized ScriptFunction getBuiltinInt32Array() {
1478         if (this.builtinInt32Array == null) {
1479             this.builtinInt32Array = initConstructorAndSwitchPoint("Int32Array", ScriptFunction.class);
1480         }
1481         return this.builtinInt32Array;
1482     }
1483 
1484     ScriptObject getInt32ArrayPrototype() {
1485         return ScriptFunction.getPrototype(getBuiltinInt32Array());
1486     }
1487 
1488     private synchronized ScriptFunction getBuiltinUint32Array() {
1489         if (this.builtinUint32Array == null) {
1490             this.builtinUint32Array = initConstructorAndSwitchPoint("Uint32Array", ScriptFunction.class);
1491         }
1492         return this.builtinUint32Array;
1493     }
1494 
1495     ScriptObject getUint32ArrayPrototype() {
1496         return ScriptFunction.getPrototype(getBuiltinUint32Array());
1497     }
1498 
1499     private synchronized ScriptFunction getBuiltinFloat32Array() {
1500         if (this.builtinFloat32Array == null) {
1501             this.builtinFloat32Array = initConstructorAndSwitchPoint("Float32Array", ScriptFunction.class);
1502         }
1503         return this.builtinFloat32Array;
1504     }
1505 
1506     ScriptObject getFloat32ArrayPrototype() {
1507         return ScriptFunction.getPrototype(getBuiltinFloat32Array());
1508     }
1509 
1510     private synchronized ScriptFunction getBuiltinFloat64Array() {
1511         if (this.builtinFloat64Array == null) {
1512             this.builtinFloat64Array = initConstructorAndSwitchPoint("Float64Array", ScriptFunction.class);
1513         }
1514         return this.builtinFloat64Array;
1515     }
1516 
1517     ScriptObject getFloat64ArrayPrototype() {
1518         return ScriptFunction.getPrototype(getBuiltinFloat64Array());
1519     }
1520 
1521     private ScriptFunction getBuiltinArray() {
1522         return builtinArray;
1523     }
1524 
1525     ScriptFunction getTypeErrorThrower() {
1526         return typeErrorThrower;
1527     }
1528 
1529     /**
1530      * Called from compiled script code to test if builtin has been overridden
1531      *
1532      * @return true if builtin array has not been overridden
1533      */
1534     public static boolean isBuiltinArray() {
1535         final Global instance = Global.instance();
1536         return instance.array == instance.getBuiltinArray();
1537     }
1538 
1539     private ScriptFunction getBuiltinBoolean() {
1540         return builtinBoolean;
1541     }
1542 
1543     /**
1544      * Called from compiled script code to test if builtin has been overridden
1545      *
1546      * @return true if builtin boolean has not been overridden
1547      */
1548     public static boolean isBuiltinBoolean() {
1549         final Global instance = Global.instance();
1550         return instance._boolean == instance.getBuiltinBoolean();
1551     }
1552 
1553     private synchronized ScriptFunction getBuiltinDate() {
1554         if (this.builtinDate == null) {
1555             this.builtinDate = initConstructorAndSwitchPoint("Date", ScriptFunction.class);
1556             final ScriptObject dateProto = ScriptFunction.getPrototype(builtinDate);
1557             // initialize default date
1558             this.DEFAULT_DATE = new NativeDate(NaN, dateProto);
1559         }
1560         return this.builtinDate;
1561     }
1562 
1563     /**
1564      * Called from compiled script code to test if builtin has been overridden
1565      *
1566      * @return true if builtin date has not been overridden
1567      */
1568     public static boolean isBuiltinDate() {
1569         final Global instance = Global.instance();
1570         return instance.date == LAZY_SENTINEL || instance.date == instance.getBuiltinDate();
1571     }
1572 
1573     private ScriptFunction getBuiltinError() {
1574         return builtinError;
1575     }
1576 
1577     /**
1578      * Called from compiled script code to test if builtin has been overridden
1579      *
1580      * @return true if builtin error has not been overridden
1581      */
1582     public static boolean isBuiltinError() {
1583         final Global instance = Global.instance();
1584         return instance.error == instance.getBuiltinError();
1585     }
1586 
1587     private synchronized ScriptFunction getBuiltinEvalError() {
1588         if (this.builtinEvalError == null) {
1589             this.builtinEvalError = initErrorSubtype("EvalError", getErrorPrototype());
1590         }
1591         return this.builtinEvalError;
1592     }
1593 
1594     /**
1595      * Called from compiled script code to test if builtin has been overridden
1596      *
1597      * @return true if builtin eval error has not been overridden
1598      */
1599     public static boolean isBuiltinEvalError() {
1600         final Global instance = Global.instance();
1601         return instance.evalError == LAZY_SENTINEL || instance.evalError == instance.getBuiltinEvalError();
1602     }
1603 
1604     private ScriptFunction getBuiltinFunction() {
1605         return builtinFunction;
1606     }
1607 
1608     /**
1609      * Called from compiled script code to test if builtin has been overridden
1610      *
1611      * @return true if builtin function has not been overridden
1612      */
1613     public static boolean isBuiltinFunction() {
1614         final Global instance = Global.instance();
1615         return instance.function == instance.getBuiltinFunction();
1616     }
1617 
1618     /**
1619      * Get the switchpoint used to check property changes for Function.prototype.apply
1620      * @return the switchpoint guarding apply (same as guarding call, and everything else in function)
1621      */


1632         final boolean isBuiltinFunction = instance.function == builtinFunction;
1633         return isBuiltinFunction && ScriptFunction.getPrototype(builtinFunction).getProperty(name).isBuiltin();
1634     }
1635 
1636     /**
1637      * Check if the Function.prototype.apply has not been replaced
1638      * @return true if Function.prototype.apply has been replaced
1639      */
1640     public static boolean isBuiltinFunctionPrototypeApply() {
1641         return isBuiltinFunctionProperty("apply");
1642     }
1643 
1644     /**
1645      * Check if the Function.prototype.apply has not been replaced
1646      * @return true if Function.prototype.call has been replaced
1647      */
1648     public static boolean isBuiltinFunctionPrototypeCall() {
1649         return isBuiltinFunctionProperty("call");
1650     }
1651 
1652     private synchronized ScriptFunction getBuiltinJSAdapter() {
1653         if (this.builtinJSAdapter == null) {
1654             this.builtinJSAdapter = initConstructorAndSwitchPoint("JSAdapter", ScriptFunction.class);
1655         }
1656         return builtinJSAdapter;
1657     }
1658 
1659     /**
1660      * Called from compiled script code to test if builtin has been overridden
1661      *
1662      * @return true if builtin JSAdapter has not been overridden
1663      */
1664     public static boolean isBuiltinJSAdapter() {
1665         final Global instance = Global.instance();
1666         return instance.jsadapter == LAZY_SENTINEL || instance.jsadapter == instance.getBuiltinJSAdapter();
1667     }
1668 
1669     private synchronized ScriptObject getBuiltinJSON() {
1670         if (this.builtinJSON == null) {
1671             this.builtinJSON = initConstructorAndSwitchPoint("JSON", ScriptObject.class);
1672         }
1673         return this.builtinJSON;
1674     }
1675 
1676     /**
1677      * Called from compiled script code to test if builtin has been overridden
1678      *
1679      * @return true if builtin JSON has has not been overridden
1680      */
1681     public static boolean isBuiltinJSON() {
1682         final Global instance = Global.instance();
1683         return instance.json == LAZY_SENTINEL || instance.json == instance.getBuiltinJSON();
1684     }
1685 
1686     private ScriptObject getBuiltinJava() {
1687         return builtinJava;
1688     }
1689 
1690     /**
1691      * Called from compiled script code to test if builtin has been overridden
1692      *
1693      * @return true if builtin Java has not been overridden
1694      */
1695     public static boolean isBuiltinJava() {
1696         final Global instance = Global.instance();
1697         return instance.java == instance.getBuiltinJava();
1698     }
1699 
1700     private ScriptObject getBuiltinJavax() {
1701         return builtinJavax;
1702     }
1703 
1704     /**
1705      * Called from compiled script code to test if builtin has been overridden
1706      *
1707      * @return true if builtin Javax has not been overridden
1708      */
1709     public static boolean isBuiltinJavax() {
1710         final Global instance = Global.instance();
1711         return instance.javax == instance.getBuiltinJavax();
1712     }
1713 
1714     private synchronized ScriptFunction getBuiltinJavaImporter() {
1715         if (this.builtinJavaImporter == null) {
1716             this.builtinJavaImporter = initConstructor("JavaImporter", ScriptFunction.class);
1717         }
1718         return this.builtinJavaImporter;
1719     }
1720 
1721     private synchronized ScriptObject getBuiltinJavaApi() {
1722         if (this.builtinJavaApi == null) {
1723             this.builtinJavaApi = initConstructor("Java", ScriptObject.class);
1724         }
1725         return this.builtinJavaApi;
1726     }
1727 
1728     /**
1729      * Called from compiled script code to test if builtin has been overridden
1730      *
1731      * @return true if builtin Java importer has not been overridden
1732      */
1733     public static boolean isBuiltinJavaImporter() {
1734         final Global instance = Global.instance();
1735         return instance.javaImporter == LAZY_SENTINEL || instance.javaImporter == instance.getBuiltinJavaImporter();




1736     }
1737 
1738     /**
1739      * Called from compiled script code to test if builtin has been overridden
1740      *
1741      * @return true if builtin math has not been overridden
1742      */
1743     public static boolean isBuiltinMath() {
1744         final Global instance = Global.instance();
1745         return instance.math == instance.builtinMath;
1746     }
1747 
1748     private ScriptFunction getBuiltinNumber() {
1749         return builtinNumber;
1750     }
1751 
1752     /**
1753      * Called from compiled script code to test if builtin has been overridden
1754      *
1755      * @return true if builtin number has not been overridden
1756      */
1757     public static boolean isBuiltinNumber() {
1758         final Global instance = Global.instance();
1759         return instance.number == instance.getBuiltinNumber();
1760     }
1761 
1762     private ScriptFunction getBuiltinObject() {
1763         return builtinObject;
1764     }
1765 


1770      */
1771     public static boolean isBuiltinObject() {
1772         final Global instance = Global.instance();
1773         return instance.object == instance.getBuiltinObject();
1774     }
1775 
1776     private ScriptObject getBuiltinPackages() {
1777         return builtinPackages;
1778     }
1779 
1780     /**
1781      * Called from compiled script code to test if builtin has been overridden
1782      *
1783      * @return true if builtin package has not been overridden
1784      */
1785     public static boolean isBuiltinPackages() {
1786         final Global instance = Global.instance();
1787         return instance.packages == instance.getBuiltinPackages();
1788     }
1789 
1790     private synchronized ScriptFunction getBuiltinRangeError() {
1791         if (this.builtinRangeError == null) {
1792             this.builtinRangeError = initErrorSubtype("RangeError", getErrorPrototype());
1793         }
1794         return builtinRangeError;
1795     }
1796 
1797     /**
1798      * Called from compiled script code to test if builtin has been overridden
1799      *
1800      * @return true if builtin range error has not been overridden
1801      */
1802     public static boolean isBuiltinRangeError() {
1803         final Global instance = Global.instance();
1804         return instance.rangeError == LAZY_SENTINEL || instance.rangeError == instance.getBuiltinRangeError();
1805     }
1806 
1807     private synchronized ScriptFunction getBuiltinReferenceError() {
1808         return builtinReferenceError;
1809     }
1810 
1811     /**
1812      * Called from compiled script code to test if builtin has been overridden
1813      *
1814      * @return true if builtin reference error has not been overridden
1815      */
1816     public static boolean isBuiltinReferenceError() {
1817         final Global instance = Global.instance();
1818         return instance.referenceError == instance.getBuiltinReferenceError();
1819     }
1820 
1821     private synchronized ScriptFunction getBuiltinRegExp() {
1822         if (this.builtinRegExp == null) {
1823             this.builtinRegExp = initConstructorAndSwitchPoint("RegExp", ScriptFunction.class);
1824             final ScriptObject regExpProto = ScriptFunction.getPrototype(builtinRegExp);
1825             // initialize default regexp object
1826             this.DEFAULT_REGEXP = new NativeRegExp("(?:)", "", this, regExpProto);
1827             // RegExp.prototype should behave like a RegExp object. So copy the
1828             // properties.
1829             regExpProto.addBoundProperties(DEFAULT_REGEXP);
1830         }
1831         return builtinRegExp;
1832     }
1833 
1834     /**
1835      * Called from compiled script code to test if builtin has been overridden
1836      *
1837      * @return true if builtin regexp has not been overridden
1838      */
1839     public static boolean isBuiltinRegExp() {
1840         final Global instance = Global.instance();
1841         return instance.regexp == LAZY_SENTINEL || instance.regexp == instance.getBuiltinRegExp();
1842     }
1843 
1844     private ScriptFunction getBuiltinString() {
1845         return builtinString;
1846     }
1847 
1848     /**
1849      * Called from compiled script code to test if builtin has been overridden
1850      *
1851      * @return true if builtin Java has not been overridden
1852      */
1853     public static boolean isBuiltinString() {
1854         final Global instance = Global.instance();
1855         return instance.string == instance.getBuiltinString();
1856     }
1857 
1858     private ScriptFunction getBuiltinSyntaxError() {
1859         return builtinSyntaxError;
1860     }
1861 


1866      */
1867     public static boolean isBuiltinSyntaxError() {
1868         final Global instance = Global.instance();
1869         return instance.syntaxError == instance.getBuiltinSyntaxError();
1870     }
1871 
1872     private ScriptFunction getBuiltinTypeError() {
1873         return builtinTypeError;
1874     }
1875 
1876     /**
1877      * Called from compiled script code to test if builtin has been overridden
1878      *
1879      * @return true if builtin type error has not been overridden
1880      */
1881     public static boolean isBuiltinTypeError() {
1882         final Global instance = Global.instance();
1883         return instance.typeError == instance.getBuiltinTypeError();
1884     }
1885 
1886     private synchronized ScriptFunction getBuiltinURIError() {
1887         if (this.builtinURIError == null) {
1888             this.builtinURIError = initErrorSubtype("URIError", getErrorPrototype());
1889         }
1890         return this.builtinURIError;
1891     }
1892 
1893     /**
1894      * Called from compiled script code to test if builtin has been overridden
1895      *
1896      * @return true if builtin URI error has not been overridden
1897      */
1898     public static boolean isBuiltinURIError() {
1899         final Global instance = Global.instance();
1900         return instance.uriError == LAZY_SENTINEL || instance.uriError == instance.getBuiltinURIError();
1901     }
1902 
1903     @Override
1904     public String getClassName() {
1905         return "global";
1906     }
1907 
1908     /**
1909      * Copy function used to clone NativeRegExp objects.
1910      *
1911      * @param regexp a NativeRegExp to clone
1912      *
1913      * @return copy of the given regexp object
1914      */
1915     public static Object regExpCopy(final Object regexp) {
1916         return new NativeRegExp((NativeRegExp)regexp);
1917     }
1918 
1919     /**
1920      * Convert given object to NativeRegExp type.


2292                         new Specialization(GlobalFunctions.IS_NAN_J),
2293                         new Specialization(GlobalFunctions.IS_NAN_D) });
2294         this.parseFloat         = ScriptFunctionImpl.makeFunction("parseFloat", GlobalFunctions.PARSEFLOAT);
2295         this.isNaN              = ScriptFunctionImpl.makeFunction("isNaN",      GlobalFunctions.IS_NAN);
2296         this.isFinite           = ScriptFunctionImpl.makeFunction("isFinite",   GlobalFunctions.IS_FINITE);
2297         this.encodeURI          = ScriptFunctionImpl.makeFunction("encodeURI",  GlobalFunctions.ENCODE_URI);
2298         this.encodeURIComponent = ScriptFunctionImpl.makeFunction("encodeURIComponent", GlobalFunctions.ENCODE_URICOMPONENT);
2299         this.decodeURI          = ScriptFunctionImpl.makeFunction("decodeURI",  GlobalFunctions.DECODE_URI);
2300         this.decodeURIComponent = ScriptFunctionImpl.makeFunction("decodeURIComponent", GlobalFunctions.DECODE_URICOMPONENT);
2301         this.escape             = ScriptFunctionImpl.makeFunction("escape",     GlobalFunctions.ESCAPE);
2302         this.unescape           = ScriptFunctionImpl.makeFunction("unescape",   GlobalFunctions.UNESCAPE);
2303         this.print              = ScriptFunctionImpl.makeFunction("print",      env._print_no_newline ? PRINT : PRINTLN);
2304         this.load               = ScriptFunctionImpl.makeFunction("load",       LOAD);
2305         this.loadWithNewGlobal  = ScriptFunctionImpl.makeFunction("loadWithNewGlobal", LOAD_WITH_NEW_GLOBAL);
2306         this.exit               = ScriptFunctionImpl.makeFunction("exit",       EXIT);
2307         this.quit               = ScriptFunctionImpl.makeFunction("quit",       EXIT);
2308 
2309         // built-in constructors
2310         this.builtinArray     = initConstructorAndSwitchPoint("Array", ScriptFunction.class);
2311         this.builtinBoolean   = initConstructorAndSwitchPoint("Boolean", ScriptFunction.class);




2312         this.builtinNumber    = initConstructorAndSwitchPoint("Number", ScriptFunction.class);

2313         this.builtinString    = initConstructorAndSwitchPoint("String", ScriptFunction.class);
2314         this.builtinMath      = initConstructorAndSwitchPoint("Math", ScriptObject.class);
2315 
2316         // initialize String.prototype.length to 0
2317         // add String.prototype.length
2318         final ScriptObject stringPrototype = getStringPrototype();
2319         stringPrototype.addOwnProperty("length", Attribute.NON_ENUMERABLE_CONSTANT, 0.0);
2320 
2321         // set isArray flag on Array.prototype
2322         final ScriptObject arrayPrototype = getArrayPrototype();
2323         arrayPrototype.setIsArray();
2324 










2325         // Error stuff
2326         initErrorObjects();
2327 
2328         // java access
2329         if (! env._no_java) {
2330             this.javaApi = LAZY_SENTINEL;
2331             this.javaImporter = LAZY_SENTINEL;
2332             initJavaAccess();
2333         }
2334 
2335         if (! env._no_typed_arrays) {
2336             this.arrayBuffer       = LAZY_SENTINEL;
2337             this.dataView          = LAZY_SENTINEL;
2338             this.int8Array         = LAZY_SENTINEL;
2339             this.uint8Array        = LAZY_SENTINEL;
2340             this.uint8ClampedArray = LAZY_SENTINEL;
2341             this.int16Array        = LAZY_SENTINEL;
2342             this.uint16Array       = LAZY_SENTINEL;
2343             this.int32Array        = LAZY_SENTINEL;
2344             this.uint32Array       = LAZY_SENTINEL;
2345             this.float32Array      = LAZY_SENTINEL;
2346             this.float64Array      = LAZY_SENTINEL;
2347         }
2348 
2349         if (env._scripting) {
2350             initScripting(env);
2351         }
2352 
2353         if (Context.DEBUG) {
2354             boolean debugOkay;
2355             final SecurityManager sm = System.getSecurityManager();
2356             if (sm != null) {
2357                 try {
2358                     sm.checkPermission(new RuntimePermission(Context.NASHORN_DEBUG_MODE));
2359                     debugOkay = true;
2360                 } catch (final SecurityException ignored) {
2361                     // if no permission, don't initialize Debug object
2362                     debugOkay = false;
2363                 }
2364 
2365             } else {
2366                 debugOkay = true;


2400         errorProto.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
2401         final ScriptFunction getLineNumber = ScriptFunctionImpl.makeFunction("getLineNumber", NativeError.GET_LINENUMBER);
2402         final ScriptFunction setLineNumber = ScriptFunctionImpl.makeFunction("setLineNumber", NativeError.SET_LINENUMBER);
2403         errorProto.addOwnProperty("lineNumber", Attribute.NOT_ENUMERABLE, getLineNumber, setLineNumber);
2404         final ScriptFunction getColumnNumber = ScriptFunctionImpl.makeFunction("getColumnNumber", NativeError.GET_COLUMNNUMBER);
2405         final ScriptFunction setColumnNumber = ScriptFunctionImpl.makeFunction("setColumnNumber", NativeError.SET_COLUMNNUMBER);
2406         errorProto.addOwnProperty("columnNumber", Attribute.NOT_ENUMERABLE, getColumnNumber, setColumnNumber);
2407         final ScriptFunction getFileName = ScriptFunctionImpl.makeFunction("getFileName", NativeError.GET_FILENAME);
2408         final ScriptFunction setFileName = ScriptFunctionImpl.makeFunction("setFileName", NativeError.SET_FILENAME);
2409         errorProto.addOwnProperty("fileName", Attribute.NOT_ENUMERABLE, getFileName, setFileName);
2410 
2411         // ECMA 15.11.4.2 Error.prototype.name
2412         // Error.prototype.name = "Error";
2413         errorProto.set(NativeError.NAME, "Error", 0);
2414         // ECMA 15.11.4.3 Error.prototype.message
2415         // Error.prototype.message = "";
2416         errorProto.set(NativeError.MESSAGE, "", 0);
2417 
2418         tagBuiltinProperties("Error", builtinError);
2419 


2420         this.builtinReferenceError = initErrorSubtype("ReferenceError", errorProto);
2421         this.builtinSyntaxError = initErrorSubtype("SyntaxError", errorProto);
2422         this.builtinTypeError = initErrorSubtype("TypeError", errorProto);

2423     }
2424 
2425     private ScriptFunction initErrorSubtype(final String name, final ScriptObject errorProto) {
2426         final ScriptFunction cons = initConstructor(name, ScriptFunction.class);
2427         final ScriptObject prototype = ScriptFunction.getPrototype(cons);
2428         prototype.set(NativeError.NAME, name, 0);
2429         prototype.set(NativeError.MESSAGE, "", 0);
2430         prototype.setInitialProto(errorProto);
2431         tagBuiltinProperties(name, cons);
2432         return cons;
2433     }
2434 
2435     private void initJavaAccess() {
2436         final ScriptObject objectProto = getObjectPrototype();
2437         this.builtinPackages = new NativeJavaPackage("", objectProto);
2438         this.builtinCom = new NativeJavaPackage("com", objectProto);
2439         this.builtinEdu = new NativeJavaPackage("edu", objectProto);
2440         this.builtinJava = new NativeJavaPackage("java", objectProto);
2441         this.builtinJavafx = new NativeJavaPackage("javafx", objectProto);
2442         this.builtinJavax = new NativeJavaPackage("javax", objectProto);
2443         this.builtinOrg = new NativeJavaPackage("org", objectProto);


2444     }
2445 
2446     private void initScripting(final ScriptEnvironment scriptEnv) {
2447         Object value;
2448         value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
2449         addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);
2450 
2451         value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
2452         addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);
2453 
2454         final String execName = ScriptingFunctions.EXEC_NAME;
2455         value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
2456         addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);
2457 
2458         // Nashorn extension: global.echo (scripting-mode-only)
2459         // alias for "print"
2460         value = get("print");
2461         addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);
2462 
2463         // Nashorn extension: global.$OPTIONS (scripting-mode-only)


2475         } else {
2476             addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
2477         }
2478 
2479         // add other special properties for exec support
2480         addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
2481         addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
2482         addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
2483     }
2484 
2485     private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
2486         for (final Field f : scriptEnv.getClass().getFields()) {
2487             try {
2488                 options.set(f.getName(), f.get(scriptEnv), 0);
2489             } catch (final IllegalArgumentException | IllegalAccessException exp) {
2490                 throw new RuntimeException(exp);
2491             }
2492         }
2493     }
2494 















2495     private void copyBuiltins() {
2496         this.array             = this.builtinArray;
2497         this._boolean          = this.builtinBoolean;

2498         this.error             = this.builtinError;

2499         this.function          = this.builtinFunction;


2500         this.com               = this.builtinCom;
2501         this.edu               = this.builtinEdu;
2502         this.java              = this.builtinJava;
2503         this.javafx            = this.builtinJavafx;
2504         this.javax             = this.builtinJavax;
2505         this.org               = this.builtinOrg;


2506         this.math              = this.builtinMath;
2507         this.number            = this.builtinNumber;
2508         this.object            = this.builtinObject;
2509         this.packages          = this.builtinPackages;

2510         this.referenceError    = this.builtinReferenceError;

2511         this.string            = this.builtinString;
2512         this.syntaxError       = this.builtinSyntaxError;
2513         this.typeError         = this.builtinTypeError;












2514     }
2515 
2516     private void initDebug() {
2517         this.addOwnProperty("Debug", Attribute.NOT_ENUMERABLE, initConstructor("Debug", ScriptObject.class));
2518     }
2519 
2520     private Object printImpl(final boolean newLine, final Object... objects) {
2521         @SuppressWarnings("resource")
2522         final PrintWriter out = scontext != null? new PrintWriter(scontext.getWriter()) : getContext().getEnv().getOut();
2523         final StringBuilder sb = new StringBuilder();
2524 
2525         for (final Object obj : objects) {
2526             if (sb.length() != 0) {
2527                 sb.append(' ');
2528             }
2529 
2530             sb.append(JSType.toString(obj));
2531         }
2532 
2533         // Print all at once to ensure thread friendly result.