Functions must resolve (null|undefined)->global on their own invocation
diff --git a/src/org/mozilla/javascript/Interpreter.java b/src/org/mozilla/javascript/Interpreter.java
index 175f841..f7ad8cf 100644
--- a/src/org/mozilla/javascript/Interpreter.java
+++ b/src/org/mozilla/javascript/Interpreter.java
@@ -94,7 +94,7 @@
             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;
@@ -2782,10 +2782,6 @@
         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;
diff --git a/src/org/mozilla/javascript/ScriptRuntime.java b/src/org/mozilla/javascript/ScriptRuntime.java
index 4e6b133..e09360a 100644
--- a/src/org/mozilla/javascript/ScriptRuntime.java
+++ b/src/org/mozilla/javascript/ScriptRuntime.java
@@ -3479,6 +3479,19 @@
         }
         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
diff --git a/src/org/mozilla/javascript/optimizer/Codegen.java b/src/org/mozilla/javascript/optimizer/Codegen.java
index 5858fd6..d6184e2 100644
--- a/src/org/mozilla/javascript/optimizer/Codegen.java
+++ b/src/org/mozilla/javascript/optimizer/Codegen.java
@@ -1526,6 +1526,19 @@
             }
         }
 
+        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) {