1717import java .util .HashMap ;
1818import java .util .List ;
1919import java .util .Map ;
20- import org .mozilla .javascript .Callable ;
2120import org .mozilla .javascript .Context ;
2221import org .mozilla .javascript .ContextAction ;
2322import org .mozilla .javascript .ContextFactory ;
2423import org .mozilla .javascript .ImporterTopLevel ;
2524import org .mozilla .javascript .Kit ;
2625import org .mozilla .javascript .NativeCall ;
2726import org .mozilla .javascript .NativeObject ;
27+ import org .mozilla .javascript .Script ;
2828import org .mozilla .javascript .ScriptRuntime ;
2929import org .mozilla .javascript .Scriptable ;
3030import org .mozilla .javascript .ScriptableObject ;
@@ -745,8 +745,15 @@ private static String do_eval(Context cx, StackFrame frame, String expr) {
745745 cx .setInterpretedMode (false );
746746 cx .setGeneratingDebug (false );
747747 try {
748- Callable script = (Callable ) cx .compileString (expr , "" , 0 , null );
749- Object result = script .call (cx , frame .scope , frame .thisObj , ScriptRuntime .emptyArgs );
748+ Scriptable scope = frame .scope ;
749+ if (!frame .isFunction && scope != null ) {
750+ Scriptable parentScope = scope .getParentScope ();
751+ if (parentScope != null ) {
752+ scope = parentScope ;
753+ }
754+ }
755+ Script script = cx .compileString (expr , "" , 0 , null );
756+ Object result = script .exec (cx , scope , frame .thisObj );
750757 if (result == Undefined .instance ) {
751758 resultString = "" ;
752759 } else {
@@ -896,7 +903,7 @@ public DebugFrame getFrame(Context cx, DebuggableScript fnOrScript) {
896903 // Can not debug if source is not available
897904 return null ;
898905 }
899- return new StackFrame (cx , dim , item );
906+ return new StackFrame (cx , dim , item , fnOrScript . isFunction () );
900907 }
901908
902909 /** Called when compilation is finished. */
@@ -974,6 +981,9 @@ public static class StackFrame implements DebugFrame {
974981 /** The 'this' object. */
975982 private Scriptable thisObj ;
976983
984+ /** Whether this frame represents a function (vs a top-level script). */
985+ private boolean isFunction ;
986+
977987 /** Information about the function. */
978988 private FunctionSource fsource ;
979989
@@ -984,10 +994,11 @@ public static class StackFrame implements DebugFrame {
984994 private int lineNumber ;
985995
986996 /** Creates a new StackFrame. */
987- private StackFrame (Context cx , Dim dim , FunctionSource fsource ) {
997+ private StackFrame (Context cx , Dim dim , FunctionSource fsource , boolean isFunction ) {
988998 this .dim = dim ;
989999 this .contextData = ContextData .get (cx );
9901000 this .fsource = fsource ;
1001+ this .isFunction = isFunction ;
9911002 this .breakpoints = fsource .sourceInfo ().breakpoints ;
9921003 this .lineNumber = fsource .firstLine ();
9931004 }
0 commit comments