Skip to content

Commit

Permalink
Functions must resolve (null|undefined)->global on their own invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
szegedi committed Jan 16, 2019
1 parent 7e961a3 commit 49e8586
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/org/mozilla/javascript/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static class CallFrame implements Cloneable, Serializable
this.fnOrScript = fnOrScript;
varSource = this;
localShift = idata.itsMaxVars;
this.thisObj = thisObj;
this.thisObj = idata.isStrict ? thisObj : ScriptRuntime.getNonStrictThis(cx, thisObj);

this.parentFrame = parentFrame;
frameIndex = (parentFrame == null) ? 0 : parentFrame.frameIndex + 1;
Expand Down Expand Up @@ -2782,10 +2782,6 @@ private static CallFrame initFrameForApplyOrCall(Context cx, CallFrame frame,
else {
applyThis = null;
}
if (applyThis == null) {
// This covers the case of args[0] == (null|undefined) as well.
applyThis = ScriptRuntime.getTopCallScope(cx);
}
if(op == Icode_TAIL_CALL) {
exitFrame(cx, frame, null);
frame = frame.parentFrame;
Expand Down
13 changes: 13 additions & 0 deletions src/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -3479,6 +3479,19 @@ public static Scriptable getTopCallScope(Context cx)
}
return scope;
}

/**
* ES5.1, section 10.4.3 step 2 "Else if thisArg is null or undefined, set the ThisBinding to the global object."
* @param cx current context, for top scope
* @param thisArg the this argument
* @return appropriate this binding
*/
public static Scriptable getNonStrictThis(Context cx, Scriptable thisArg) {
if (thisArg == null || thisArg == Undefined.SCRIPTABLE_UNDEFINED) {
return getTopCallScope(cx);
}
return thisArg;
}

/**
* @deprecated Use {@link #doTopCall(Callable, Context, Scriptable, Scriptable, Object[], boolean)} instead
Expand Down
13 changes: 13 additions & 0 deletions src/org/mozilla/javascript/optimizer/Codegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,19 @@ private void generatePrologue()
}
}

if (!scriptOrFn.isInStrictMode()) {
// ES5.1, section 10.4.3 step 2 "Else if thisArg is null or undefined, set the ThisBinding to the global object."
cfw.addALoad(1);
cfw.addALoad(3);
cfw.addInvoke(ByteCode.INVOKESTATIC,
"org/mozilla/javascript/ScriptRuntime",
"getNonStrictThis",
"(Lorg/mozilla/javascript/Context;"
+"Lorg/mozilla/javascript/Scriptable;"
+")Lorg/mozilla/javascript/Scriptable;");
cfw.addAStore(3);
}

// Compile RegExp literals if this is a script. For functions
// this is performed during instantiation in functionInit
if (fnCurrent == null && scriptOrFn.getRegexpCount() != 0) {
Expand Down

0 comments on commit 49e8586

Please sign in to comment.