RoamUniverse / pyv8

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

Is it possible to use python generators as arrays in PyV8 ? #41

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Tried to implement a simple generator, but i cant loop with either
for(var k in arr) / for(var i=0;i<arr.length...)
Seems that __len__/__getitem__ are not called.

Is it something i'm doing wrong or its not supported?

What version of the product are you using? On what operating system?
PyV8 0.9/winxp (with py2.6 x86)

Original issue reported on code.google.com by Liubomir...@gmail.com on 10 Mar 2010 at 3:11

GoogleCodeExporter commented 9 years ago
Ok, i got more tests (attached file).

1) Seems that after i added:
@property
def length(self):
 return len(self.arr)

it works.. But i guess it will be a lot more easier, the wrapper to check 
__len__,
when length property is used in the JS context.

2) I got the __getitem__ working :)
3) for(var item in arr) is still not working, any ideas why?

Original comment by Liubomir...@gmail.com on 10 Mar 2010 at 3:44

Attachments:

GoogleCodeExporter commented 9 years ago
I think it is a great idea, I will try to support it more smooth later :)

Thanks

Original comment by flier...@gmail.com on 10 Mar 2010 at 3:47

GoogleCodeExporter commented 9 years ago
Please check out latest SVN code after r242 to verify it, thanks

def gen(x):
    yield 1
    yield 2
    yield 3

with JSContext(Global()) as ctxt:
    func = ctxt.eval("""(function (k) {
                            var result = [];    
                            for (var prop in k) {
                              result.push(prop);
                              alert("key=" + prop + ", value=" + k[prop]);
                            }
                            return result;
                        })""")

print "gen", func(gen(3))

gen key=1, value=undefined
key=2, value=undefined
key=3, value=undefined
1,2,3

Original comment by flier...@gmail.com on 14 Mar 2010 at 4:10

Attachments: