707 clazz = compile(source, new ThrowErrorManager(), strictFlag); 708 } catch (final ParserException e) { 709 e.throwAsEcmaException(global); 710 return null; 711 } 712 713 if (!strictFlag) { 714 // We need to get strict mode flag from compiled class. This is 715 // because eval code may start with "use strict" directive. 716 try { 717 strictFlag = clazz.getField(STRICT_MODE.symbolName()).getBoolean(null); 718 } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { 719 //ignored 720 strictFlag = false; 721 } 722 } 723 724 // In strict mode, eval does not instantiate variables and functions 725 // in the caller's environment. A new environment is created! 726 if (strictFlag) { 727 // Create a new scope object 728 final ScriptObject strictEvalScope = global.newObject(); 729 730 // bless it as a "scope" 731 strictEvalScope.setIsScope(); 732 733 // set given scope to be it's proto so that eval can still 734 // access caller environment vars in the new environment. 735 strictEvalScope.setProto(scope); 736 scope = strictEvalScope; 737 } 738 739 final ScriptFunction func = getProgramFunction(clazz, scope); 740 Object evalThis; 741 if (directEval) { 742 evalThis = (callThis != UNDEFINED && callThis != null) || strictFlag ? callThis : global; 743 } else { 744 // either indirect evalCall or non-eval (Function, engine.eval, ScriptObjectMirror.eval..) 745 evalThis = callThis; 746 } 747 748 return ScriptRuntime.apply(func, evalThis); 749 } 750 751 private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) { 752 if (srcStr.startsWith(prefix)) { 753 final String resource = resourcePath + srcStr.substring(prefix.length()); 754 // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme 755 // These scripts are always available and are loaded from nashorn.jar's resources. 756 return AccessController.doPrivileged( 757 new PrivilegedAction<Source>() { 758 @Override 759 public Source run() { 760 try { 761 final URL resURL = Context.class.getResource(resource); 762 return resURL != null ? sourceFor(srcStr, resURL) : null; 763 } catch (final IOException exp) { 764 return null; 765 } 766 } 767 }); 768 } | 707 clazz = compile(source, new ThrowErrorManager(), strictFlag); 708 } catch (final ParserException e) { 709 e.throwAsEcmaException(global); 710 return null; 711 } 712 713 if (!strictFlag) { 714 // We need to get strict mode flag from compiled class. This is 715 // because eval code may start with "use strict" directive. 716 try { 717 strictFlag = clazz.getField(STRICT_MODE.symbolName()).getBoolean(null); 718 } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { 719 //ignored 720 strictFlag = false; 721 } 722 } 723 724 // In strict mode, eval does not instantiate variables and functions 725 // in the caller's environment. A new environment is created! 726 if (strictFlag) { 727 // Create a new scope object with given scope as its prototype 728 scope = newScope(scope); 729 } 730 731 final ScriptFunction func = getProgramFunction(clazz, scope); 732 Object evalThis; 733 if (directEval) { 734 evalThis = (callThis != UNDEFINED && callThis != null) || strictFlag ? callThis : global; 735 } else { 736 // either indirect evalCall or non-eval (Function, engine.eval, ScriptObjectMirror.eval..) 737 evalThis = callThis; 738 } 739 740 return ScriptRuntime.apply(func, evalThis); 741 } 742 743 private static ScriptObject newScope(final ScriptObject callerScope) { 744 return new FunctionScope(PropertyMap.newMap(FunctionScope.class), callerScope); 745 } 746 747 private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) { 748 if (srcStr.startsWith(prefix)) { 749 final String resource = resourcePath + srcStr.substring(prefix.length()); 750 // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme 751 // These scripts are always available and are loaded from nashorn.jar's resources. 752 return AccessController.doPrivileged( 753 new PrivilegedAction<Source>() { 754 @Override 755 public Source run() { 756 try { 757 final URL resURL = Context.class.getResource(resource); 758 return resURL != null ? sourceFor(srcStr, resURL) : null; 759 } catch (final IOException exp) { 760 return null; 761 } 762 } 763 }); 764 } |