gitggh / pyv8

Automatically exported from code.google.com/p/pyv8
0 stars 0 forks source link

typeof(<non existent property of global object>) should be undefined. #71

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

import PyV8

# This should raise a reference error.
try:

    with PyV8.JSContext() as ctx:

        ctx.eval("x")

except ReferenceError:
    print "ref error"

# This should be True.
with PyV8.JSContext() as ctx:

    ctx.eval("testcase = (typeof(x) === 'undefined')")
    print ctx.locals.testcase

# This should also be True
try:

    class Global(PyV8.JSClass):
        pass

    with PyV8.JSContext(Global()) as ctx:

        ctx.eval("testcase = typeof(x) === 'undefined'")
        print ctx.locals.testcase
except ReferenceError:
    print "ref error"

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

Expected:
ref error
True
True

Saw:
ref error
True
ref error

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

PyV8 trunk on x64 linux.

Please provide any additional information below.

It would also be nice to be able to use and return JavaScript undefined from 
python.  Perhaps a new variable in PyV8 called PyV8.jsundefined?  I'm not sure 
if the implementation of that would even be possible.

Original issue reported on code.google.com by mbc8...@gmail.com on 3 Feb 2011 at 10:53

GoogleCodeExporter commented 8 years ago

Original comment by flier...@gmail.com on 3 Feb 2011 at 11:01

GoogleCodeExporter commented 8 years ago
I will check it a few day later, because I'm in a short vacation with limited 
Internet access :)

Original comment by flier...@gmail.com on 3 Feb 2011 at 11:02

GoogleCodeExporter commented 8 years ago
Ok after looking into it more it seems my suggestion in Issue 61 was wrong.

This article has a pretty good description of whats going on:

http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-pre
venting-referenceerrors/

From what I gather:

<global object>.foo  // undefined
foo                  // reference error
typeof(foo)          // undefined
foo.x                // reference error

I have a global object defined in PyV8 and I have a __getattr__ method on it, 
how is it possible to return either undefined or raise a reference error 
depending on how it was called?  

Original comment by ATM1...@gmail.com on 5 Feb 2011 at 4:16

GoogleCodeExporter commented 8 years ago
I found this to be a related issue:

import PyV8

class Global(PyV8.JSClass):

    def __init__(self):
        self.s = self

    def write(self, val):
        print val

with PyV8.JSContext(Global()) as ctx:
    ctx.eval(""" 
        write(String)
        write(s.String)       
    """)

Output being:

function String() { [native code] }
Traceback (most recent call last):
  File "window2.py", line 15, in <module>
    """)
ReferenceError: ReferenceError: String (  @ 3 : 15 )  ->         
write(s.String)     

I found this in the ecma 262 spec:

10.1.5 Global Object
There is a unique global object (15.1), which is created before control enters 
any execution context.
Initially the global object has the following properties:
•  Built-in objects such as Math, String, Date, parseInt, etc. These have 
attributes { DontEnum }.
•  Additional host defined properties. This may include a property whose 
value is the global object
itself; for example, in the HTML document object model the window property of 
the global object is
the global object itself.
As control enters execution contexts, and as ECMAScript code is executed, 
additional properties may be
added to the global object and the initial properties may be changed

Original comment by ATM1...@gmail.com on 9 Feb 2011 at 9:41

GoogleCodeExporter commented 8 years ago
Fixed, please verify it with SVN trunk code after r337

Thanks for your sharing, the blog is great :)

Original comment by flier...@gmail.com on 13 Feb 2011 at 1:36

GoogleCodeExporter commented 8 years ago

Original comment by flier...@gmail.com on 13 Feb 2011 at 1:36

GoogleCodeExporter commented 8 years ago
This works great.  Thank you.

Original comment by ATM1...@gmail.com on 14 Feb 2011 at 4:06

GoogleCodeExporter commented 8 years ago

Original comment by flier...@gmail.com on 15 Feb 2011 at 2:07