Fix this->window confusion in DevMode (issue #3069).

Review at: http://gwt-code-reviews.appspot.com/1620805
Patch by: stephenh

Review by: scottb@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10944 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/shell/JavaScriptHost.java b/dev/core/src/com/google/gwt/dev/shell/JavaScriptHost.java
index 17a1b5c..8f3ecad 100644
--- a/dev/core/src/com/google/gwt/dev/shell/JavaScriptHost.java
+++ b/dev/core/src/com/google/gwt/dev/shell/JavaScriptHost.java
@@ -23,6 +23,19 @@
 
   private static ShellJavaScriptHost sHost;
 
+  /**
+   * Throws {@code NullPointerException} if {@code o} is null.
+   *<p>
+   * Used by {@code RewriteJsniMethods} to ensure JSO instance methods
+   * are not called against null instances.
+   */
+  public static Object checkNotNull(Object o) {
+    if (o == null) {
+      throw new NullPointerException("JSO instance was null");
+    }
+    return o;
+  }
+
   public static void exceptionCaught(Object exception) {
     sHost.exceptionCaught(exception);
   }
diff --git a/dev/core/src/com/google/gwt/dev/shell/rewrite/RewriteJsniMethods.java b/dev/core/src/com/google/gwt/dev/shell/rewrite/RewriteJsniMethods.java
index da8a029..9760f06 100644
--- a/dev/core/src/com/google/gwt/dev/shell/rewrite/RewriteJsniMethods.java
+++ b/dev/core/src/com/google/gwt/dev/shell/rewrite/RewriteJsniMethods.java
@@ -255,6 +255,13 @@
         visitInsn(Opcodes.ACONST_NULL);
       } else {
         loadThis();
+        // Ensure the instance method is not invoked against null, otherwise
+        // JS's apply will turn it into the window object. See Issue 3069.
+        visitMethodInsn(
+            Opcodes.INVOKESTATIC,
+            "com/google/gwt/dev/shell/JavaScriptHost",
+            "checkNotNull",
+            "(Ljava/lang/Object;)Ljava/lang/Object;");
       }
       // Stack is at 2