395
396 // Flag to indicate that a split method issued a return statement
397 private int splitState = -1;
398
399 // class cache
400 private ClassCache classCache;
401
402 // Used to store the last RegExp result to support deprecated RegExp constructor properties
403 private RegExpResult lastRegExpResult;
404
405 private static final MethodHandle EVAL = findOwnMH("eval", Object.class, Object.class, Object.class);
406 private static final MethodHandle PRINT = findOwnMH("print", Object.class, Object.class, Object[].class);
407 private static final MethodHandle PRINTLN = findOwnMH("println", Object.class, Object.class, Object[].class);
408 private static final MethodHandle LOAD = findOwnMH("load", Object.class, Object.class, Object.class);
409 private static final MethodHandle LOADWITHNEWGLOBAL = findOwnMH("loadWithNewGlobal", Object.class, Object.class, Object[].class);
410 private static final MethodHandle EXIT = findOwnMH("exit", Object.class, Object.class, Object.class);
411
412 // initialized by nasgen
413 private static PropertyMap $nasgenmap$;
414
415 // performs initialization checks for Global constructor and returns the
416 // PropertyMap, if everything is fine.
417 private static PropertyMap checkAndGetMap(final Context context) {
418 // security check first
419 final SecurityManager sm = System.getSecurityManager();
420 if (sm != null) {
421 sm.checkPermission(new RuntimePermission(Context.NASHORN_CREATE_GLOBAL));
422 }
423
424 // null check on context
425 context.getClass();
426
427 /*
428 * Duplicate global's map and use it. This way the initial Map filled
429 * by nasgen (referenced from static field in this class) is retained
430 * 'as is' (as that one is process wide singleton.
431 */
432 return $nasgenmap$.duplicate();
433 }
434
435 /**
436 * Constructor
437 *
438 * @param context the context
439 */
440 public Global(final Context context) {
441 super(checkAndGetMap(context));
442 this.setContext(context);
443 this.setIsScope();
444
445 final int cacheSize = context.getEnv()._class_cache_size;
446 if (cacheSize > 0) {
447 classCache = new ClassCache(cacheSize);
448 }
449 }
450
451 /**
452 * Script access to "current" Global instance
453 *
454 * @return the global singleton
455 */
456 public static Global instance() {
457 ScriptObject global = Context.getGlobal();
458 if (! (global instanceof Global)) {
459 throw new IllegalStateException("no current global instance");
460 }
461 return (Global)global;
462 }
465 * Script access to {@link ScriptEnvironment}
466 *
467 * @return the script environment
468 */
469 static ScriptEnvironment getEnv() {
470 return instance().getContext().getEnv();
471 }
472
473 /**
474 * Script access to {@link Context}
475 *
476 * @return the context
477 */
478 static Context getThisContext() {
479 return instance().getContext();
480 }
481
482 // GlobalObject interface implementation
483
484 @Override
485 public void initBuiltinObjects() {
486 if (this.builtinObject != null) {
487 // already initialized, just return
488 return;
489 }
490
491 init();
492 }
493
494 @Override
495 public ScriptFunction newScriptFunction(final String name, final MethodHandle handle, final ScriptObject scope, final boolean strict) {
496 return new ScriptFunctionImpl(name, handle, scope, null, strict, false, true);
497 }
498
499 @Override
500 public Object wrapAsObject(final Object obj) {
501 if (obj instanceof Boolean) {
502 return new NativeBoolean((Boolean)obj, this);
503 } else if (obj instanceof Number) {
504 return new NativeNumber(((Number)obj).doubleValue(), this);
1748
1749 final String execName = ScriptingFunctions.EXEC_NAME;
1750 value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
1751 addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);
1752
1753 // Nashorn extension: global.echo (scripting-mode-only)
1754 // alias for "print"
1755 value = get("print");
1756 addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);
1757
1758 // Nashorn extension: global.$OPTIONS (scripting-mode-only)
1759 final ScriptObject options = newObject();
1760 copyOptions(options, scriptEnv);
1761 addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);
1762
1763 // Nashorn extension: global.$ENV (scripting-mode-only)
1764 if (System.getSecurityManager() == null) {
1765 // do not fill $ENV if we have a security manager around
1766 // Retrieve current state of ENV variables.
1767 final ScriptObject env = newObject();
1768 env.putAll(System.getenv());
1769 addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
1770 } else {
1771 addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
1772 }
1773
1774 // add other special properties for exec support
1775 addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
1776 addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
1777 addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
1778 }
1779
1780 private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
1781 for (Field f : scriptEnv.getClass().getFields()) {
1782 try {
1783 options.set(f.getName(), f.get(scriptEnv), false);
1784 } catch (final IllegalArgumentException | IllegalAccessException exp) {
1785 throw new RuntimeException(exp);
1786 }
1787 }
1788 }
|
395
396 // Flag to indicate that a split method issued a return statement
397 private int splitState = -1;
398
399 // class cache
400 private ClassCache classCache;
401
402 // Used to store the last RegExp result to support deprecated RegExp constructor properties
403 private RegExpResult lastRegExpResult;
404
405 private static final MethodHandle EVAL = findOwnMH("eval", Object.class, Object.class, Object.class);
406 private static final MethodHandle PRINT = findOwnMH("print", Object.class, Object.class, Object[].class);
407 private static final MethodHandle PRINTLN = findOwnMH("println", Object.class, Object.class, Object[].class);
408 private static final MethodHandle LOAD = findOwnMH("load", Object.class, Object.class, Object.class);
409 private static final MethodHandle LOADWITHNEWGLOBAL = findOwnMH("loadWithNewGlobal", Object.class, Object.class, Object[].class);
410 private static final MethodHandle EXIT = findOwnMH("exit", Object.class, Object.class, Object.class);
411
412 // initialized by nasgen
413 private static PropertyMap $nasgenmap$;
414
415 // context to which this global belongs to
416 private final Context context;
417
418 @Override
419 protected Context getContext() {
420 return context;
421 }
422
423 // performs initialization checks for Global constructor and returns the
424 // PropertyMap, if everything is fine.
425 private static PropertyMap checkAndGetMap(final Context context) {
426 // security check first
427 final SecurityManager sm = System.getSecurityManager();
428 if (sm != null) {
429 sm.checkPermission(new RuntimePermission(Context.NASHORN_CREATE_GLOBAL));
430 }
431
432 // null check on context
433 context.getClass();
434
435 /*
436 * Duplicate global's map and use it. This way the initial Map filled
437 * by nasgen (referenced from static field in this class) is retained
438 * 'as is' (as that one is process wide singleton.
439 */
440 return $nasgenmap$.duplicate();
441 }
442
443 /**
444 * Constructor
445 *
446 * @param context the context
447 */
448 public Global(final Context context) {
449 super(checkAndGetMap(context));
450 this.context = context;
451 this.setIsScope();
452
453 final int cacheSize = context.getEnv()._class_cache_size;
454 if (cacheSize > 0) {
455 classCache = new ClassCache(cacheSize);
456 }
457 }
458
459 /**
460 * Script access to "current" Global instance
461 *
462 * @return the global singleton
463 */
464 public static Global instance() {
465 ScriptObject global = Context.getGlobal();
466 if (! (global instanceof Global)) {
467 throw new IllegalStateException("no current global instance");
468 }
469 return (Global)global;
470 }
473 * Script access to {@link ScriptEnvironment}
474 *
475 * @return the script environment
476 */
477 static ScriptEnvironment getEnv() {
478 return instance().getContext().getEnv();
479 }
480
481 /**
482 * Script access to {@link Context}
483 *
484 * @return the context
485 */
486 static Context getThisContext() {
487 return instance().getContext();
488 }
489
490 // GlobalObject interface implementation
491
492 @Override
493 public boolean isOfContext(final Context context) {
494 return this.context == context;
495 }
496
497 @Override
498 public boolean isStrictContext() {
499 return context.getEnv()._strict;
500 }
501
502 @Override
503 public void initBuiltinObjects() {
504 if (this.builtinObject != null) {
505 // already initialized, just return
506 return;
507 }
508
509 init();
510 }
511
512 @Override
513 public ScriptFunction newScriptFunction(final String name, final MethodHandle handle, final ScriptObject scope, final boolean strict) {
514 return new ScriptFunctionImpl(name, handle, scope, null, strict, false, true);
515 }
516
517 @Override
518 public Object wrapAsObject(final Object obj) {
519 if (obj instanceof Boolean) {
520 return new NativeBoolean((Boolean)obj, this);
521 } else if (obj instanceof Number) {
522 return new NativeNumber(((Number)obj).doubleValue(), this);
1766
1767 final String execName = ScriptingFunctions.EXEC_NAME;
1768 value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
1769 addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);
1770
1771 // Nashorn extension: global.echo (scripting-mode-only)
1772 // alias for "print"
1773 value = get("print");
1774 addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);
1775
1776 // Nashorn extension: global.$OPTIONS (scripting-mode-only)
1777 final ScriptObject options = newObject();
1778 copyOptions(options, scriptEnv);
1779 addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);
1780
1781 // Nashorn extension: global.$ENV (scripting-mode-only)
1782 if (System.getSecurityManager() == null) {
1783 // do not fill $ENV if we have a security manager around
1784 // Retrieve current state of ENV variables.
1785 final ScriptObject env = newObject();
1786 env.putAll(System.getenv(), scriptEnv._strict);
1787 addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
1788 } else {
1789 addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
1790 }
1791
1792 // add other special properties for exec support
1793 addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
1794 addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
1795 addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
1796 }
1797
1798 private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
1799 for (Field f : scriptEnv.getClass().getFields()) {
1800 try {
1801 options.set(f.getName(), f.get(scriptEnv), false);
1802 } catch (final IllegalArgumentException | IllegalAccessException exp) {
1803 throw new RuntimeException(exp);
1804 }
1805 }
1806 }
|