List to Array conversion

156 views
Skip to first unread message

markus

unread,
Mar 27, 2011, 1:13:02 AM3/27/11
to PyV8
Hi,

I'm trying to get JSV [1] working in PyV8, and run in a list/Array
conversion issue with PyV8 r356 and latest JSV

>>> with PyV8.JSContext() as ctx:
... print ctx.eval('(function(arg) { return arg[1]; })')(['a',
'b', 'c'])
... print ctx.eval('(function(arg) { return arg instanceof
Array; })')(['a', 'b', 'c'])
... print ctx.eval('(function(arg) { return
Object.prototype.toString.call(arg); })')(['a', 'b', 'c'])
...
b <- Ok
False <- should be True
[object Object] <- should be "[object Array]"

The last sample is how JSV determine a value type [2] which currently
dosn't work correct for arrays.

>>> with PyV8.JSContext() as ctx:
... print ctx.eval('(function (a1, a2) { return a1.concat(a2); })')
([1,2], [3,4])
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: TypeError: Object [object Object] has no method
'concat' ( @ 1 : 31 ) -> (function (a1, a2) { return
a1.concat(a2); })

... looks like that lists/arrays are treated as js 'indexed' objects?

btw - I had this already working 2 or 3 months ago using PyV8 0.9
precompiled bin, but since my systems libboost changed to 1.42.0 I
have to build PyV8 myself, which works great with V8 3.1.8.3

So is this a PyV8 issue and can that be changed/fixed?

thanks
Markus

[1] https://github.com/garycourt/JSV
[2] https://github.com/garycourt/JSV/blob/master/lib/jsv.js#L61

Flier Lu

unread,
Mar 28, 2011, 1:17:36 PM3/28/11
to PyV8
Hi Markus,

As you know, pyv8 will automatic convert the primitive type
between Python and Javascript, including bool, int, long, string/
unicode, float, datetime/date/time. The other Python object, such as
list, tuple and dict, could be access with index or name. So, if you
want to explicit convert a Python list to Javascript array, I think
you could use PyV8.JSArray class, which will wrap a list as a pure
Javascript Array, but it will COPY the Python list content to a new
Javascript array. On the other hand, you could modify the list with
index if you don't use JSArray.

with PyV8.JSContext(Globals()) as ctx:
print ctx.eval("""array[1]""")
print ctx.eval('(function(arg) { return arg instanceof Array; })')
(ctx.locals.array)
print ctx.eval('(function(arg) { return arg instanceof Array; })')
(['a', 'b', 'c'])
print ctx.eval('(function(arg) { return arg instanceof Array; })')
(PyV8.JSArray(['a', 'b', 'c']))
print ctx.eval('(function(arg) { return
Object.prototype.toString.call(arg); })')(['a', 'b', 'c'])
print ctx.eval('(function(arg) { return
Object.prototype.toString.call(arg); })')(PyV8.JSArray(['a', 'b',
'c']))
Reply all
Reply to author
Forward
0 new messages