navcat / pyv8

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

exception IndexError when accessing trailing or leading undefined elements of a JSArray #91

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
given the following 3 lines of code:

code1 = "a = Array(7); for(i=0; i<a.length; i++) a[i] = i; a[3] = undefined; 
a[a.length-1]; a"
that should return: [0, 1, 2, undefined, 4, 5, 6]

code2 = "a = Array(7); for(i=0; i<a.length - 1; i++) a[i] = i; a[a.length-1]; a"
that should return: [0, 1, 2, 3, 4, 5, undefined]

code3 = "a = Array(7); for(i=1; i<a.length; i++) a[i] = i; a[a.length-1]; a"
that should return: [undefined, 1, 2, 3, 4, 5, 6]

Just converting "a" to a Python list or simply iterating over their elements 
throws a IndexError ('[object Array]' index out of range) when accessing 
trailing or leading elements if their vale is "undefined".

Original issue reported on code.google.com by jvil...@gmail.com on 22 Jun 2011 at 7:59

GoogleCodeExporter commented 9 years ago

Original comment by flier...@gmail.com on 23 Jun 2011 at 1:31

GoogleCodeExporter commented 9 years ago
I can't reproduce your issue, what's version of PyV8 that you are using? Could 
you try the SVN trunk code? Thanks

from PyV8 import *

with JSContext() as ctxt:
    a = ctxt.eval("a = Array(7); for(i=0; i<a.length; i++) a[i] = i; a[3] = undefined; a[a.length-1]; a")

    print a # 0,1,2,,4,5,6
    print [a[i] for i in range(len(a))] # [0, 1, 2, None, 4, 5, 6]
    print list(a) # [0, 1, 2, None, 4, 5, 6]

Original comment by flier...@gmail.com on 24 Jun 2011 at 4:20

GoogleCodeExporter commented 9 years ago
Hi, sorry for the delay.
I have built PyV8 from the svn repository and got the same problem.
Using Python 2.6.5 on Ubuntu 10.04 64 bits:

I keep getting exceptions with code2 and code3. Of course, code1 works well and 
don't raise any exception as it is supposed according to the title of the 
issue. Only have problems when undefineds are at the leading or trailing 
positions of the JSarray.

Thank you and regards!
Jose.

Here is the test I used:

>>> import PyV8
>>> ct = PyV8.JSContext() 
>>> code1 = "a = Array(7); for(i=0; i<a.length; i++) a[i] = i; a[3] = 
undefined; a[a.length-1]; a"
>>> code2 = "a = Array(7); for(i=0; i<a.length - 1; i++) a[i] = i; 
a[a.length-1]; a"
>>> code3 = "a = Array(7); for(i=1; i<a.length; i++) a[i] = i; a[a.length-1]; a"
>>> ct.enter()
>>> a  ct.eval(code1)
>>> for i in a:
...     print str(i)
... 
0
1
2
None
4
5
6
>>> a = ct.eval(code2)
>>> for i in a:
...     print str(i)
... 
0
1
2
3
4
5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: '[object Array]' index out of range
>>> a = ct.eval(code3)
>>> for i in a:
...     print str(i)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: '[object Array]' index out of range

Original comment by jvil...@gmail.com on 20 Jul 2011 at 8:44

GoogleCodeExporter commented 9 years ago
Fixed, please verify it with SVN code after r391

Original comment by flier...@gmail.com on 10 Aug 2011 at 5:24