Dapeng-Liu / pyv8

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

Feature Request - javascript for/in loop and python properties. #77

Closed GoogleCodeExporter closed 8 years ago

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

import PyV8

class NewObject(PyV8.JSClass):

    def __init__(self):
        self._prop1 = 10
        self._prop2 = 20
        self._prop3 = 30
        self._prop4 = 40

    def getProp1(self):
        return self._prop1

    def getProp2(self):
        return self._prop2

    def getProp3(self):
        return self._prop3

    def getProp4(self):
        return self._prop4

    prop1 = property(getProp1)
    prop2 = property(getProp2)
    prop3 = property(getProp3)
    prop4 = property(getProp4)

class Global(PyV8.JSClass):

    def __init__(self):
        self.newObj = NewObject()

    def write(self, val):
        print val

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

        for(i in newObj) {
            write(i)        
        }

    """)

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

This prints:

_prop1
_prop2
_prop3
_prop4

I would like to see:

prop1
prop2
prop3
prop4

or maybe:

_prop1
_prop2
_prop3
_prop4
prop1
prop2
prop3
prop4

Original issue reported on code.google.com by ATM1...@gmail.com on 15 Mar 2011 at 7:01

GoogleCodeExporter commented 8 years ago

Original comment by flier...@gmail.com on 16 Mar 2011 at 6:28

GoogleCodeExporter commented 8 years ago
The root cause is pyv8 used __dict__ to enumerate the object properties. 

Now, I changed it to use dir() function, which will return all the properties 
including the _prop1, prop1 and getProp1 method except the name which 
starts/end with '__' like __getattr__, I think it may make sense because 
Javascript did the similar behaviors

Please verify the issue with SVN r356 or later, thanks

Original comment by flier...@gmail.com on 16 Mar 2011 at 5:15

GoogleCodeExporter commented 8 years ago
I am also seeing these methods showing up in the for/in loop.

hasOwnProperty
isPrototypeOf
toLocaleString
toString
unwatch
valueOf
watch

Typically you won't see those in native JS:

import PyV8

class NewObject(PyV8.JSClass):

    def __init__(self):
        pass

class Global(PyV8.JSClass):

    def __init__(self):
        self.newObj = NewObject()

    def write(self, val):
        print val

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

        for(i in newObj) {
            write(i)        
        }
    """) 

Output Is:

hasOwnProperty
isPrototypeOf
toLocaleString
toString
unwatch
valueOf
watch

vs. Native JS (SpiderMonkey Shell):

js> x = {}
[object Object]
js> x.a = 10
10
js> for(i in x) { print(i) }
a
js> x.toString()
[object Object]
js> 

Original comment by ATM1...@gmail.com on 16 Mar 2011 at 8:56