Juan571 / js2-mode

Automatically exported from code.google.com/p/js2-mode
0 stars 0 forks source link

instanceOf can have side effects #32

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
function foo(req)
{
  req instanceOf Components.interfaces.nsIChannel;
}

What is the expected output? What do you see instead?

  The first line of this function foo is flagged with a warning, because it
thinks that the expression will have no effect. This is not always the
case. In any xulrunner app this will cause the object 'req' to acquire new
properties and methods taken from the nsIChannel interface if req supports
that interface. It's equivalent to
req.QueryInterface(Components.interfaces.nsIChannel);

What version of the product are you using? On what operating system?

  20080406 in Emacs 23.0.60.2 on Linux

Original issue reported on code.google.com by dlb...@gmail.com on 14 Apr 2008 at 6:26

GoogleCodeExporter commented 8 years ago
I am reminded that instanceOf is not exactly equivalant to calling 
QueryInterface,
since QI will throw if the object doesn't support that interface, where 
instanceOf
won't. That's not entirely relevant, however.

Original comment by dlb...@gmail.com on 14 Apr 2008 at 6:31

GoogleCodeExporter commented 8 years ago
bah, and instanceof is always all lowercase, in spite of my muscle memory to the
contrary.

Original comment by dlb...@gmail.com on 14 Apr 2008 at 6:32

GoogleCodeExporter commented 8 years ago
This patch does the trick:

--- /home/db48x/elisp/js2-20080406.el.orig      2008-04-14 01:49:35.000000000 
-0500
+++ /home/db48x/elisp/js2-20080406.el   2008-04-14 01:49:50.000000000 -0500
@@ -5932,7 +5932,8 @@
                       js2-SETELEM_OP
                       js2-LOCAL_BLOCK
                       js2-SET_REF_OP
-                      js2-YIELD))
+                      js2-YIELD
+                      js2-INSTANCEOF))
       (aset tokens tt t))
     tokens))

Original comment by dlb...@gmail.com on 14 Apr 2008 at 6:50

GoogleCodeExporter commented 8 years ago
I hesitate to make this change universally unless "instanceof" can have side 
effects
in normal browser JavaScript - not counting the possible TypeError.

I'm happy to add it as a configuration option.  Does that seem reasonable?

Original comment by steve.ye...@gmail.com on 14 Apr 2008 at 10:26

GoogleCodeExporter commented 8 years ago
Maybe. The ECMA spec says that the instanceof operator results in a function 
call to
the HasInstance method on the object, so that sorta counts as a side effect. Of
course, that method isn't deleteable or overridable so for a given 
implementation it
will either always cause side effects or never cause side effects. See section 
11.8.6
of the spec.

Original comment by dlb...@gmail.com on 14 Apr 2008 at 10:43

GoogleCodeExporter commented 8 years ago
Oh, also. Isn't this message worded a little oddly? Sometimes not causing a side
effect is a virtue. The real error is that in this case I'm executing this 
statement
solely for it's side effect, and then throwing away its return value. A 
different
wording could be in order.

Original comment by dlb...@gmail.com on 14 Apr 2008 at 10:50

GoogleCodeExporter commented 8 years ago

Original comment by steve.ye...@gmail.com on 22 Apr 2008 at 4:28