Closed GoogleCodeExporter closed 9 years ago
[deleted comment]
I'm not sure what's your real problems, could you give me some example or unit
test code?
In fact, _PyV8.JSArray support the python Sequence Protocol. It means, you
could
use its predefined methods such as __iter__, __len__ or __getitem__ etc.
>>> list(ctxt.eval("new Array(1, 2, 3)"))
[1, 2, 3]
>>>
On the other hand, javascript doesn't have build-in dictionary, even you could
create object with JSON syntax, such as
>>> r = ctxt.eval("var r = {'a' : 1, 'b' : 2}; r ")
>>> r
<_PyV8.JSObject object at 0x00AE9170>
>>> dir(r)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__float__', '__ge
tattr__', '__getattribute__', '__hash__', '__init__', '__int__', '__js__', '__me
mbers__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__red
uce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'a', 'b']
>>> r.a
1
>>> r.b
2
Original comment by flier...@gmail.com
on 5 May 2009 at 4:02
[deleted comment]
hi flier,
thanks for pointing this out. it wasn't clear for that i can use the builtin
__members__ to get all attributes to
iterate over.
i've written a small function that converts a cx.eval()-result into a python
dict. maybe someone needs this
too.
cheers marc
Original comment by marc.boe...@gmail.com
on 5 May 2009 at 11:04
Attachments:
Thanks, I have merged your contribution in pyv8.py
http://code.google.com/p/pyv8/source/detail?r=123
on the other hand, I add some code to simulate a dict in JSObject class
def testDict(self):
import UserDict
with JSContext() as ctxt:
obj = ctxt.eval("var r = { 'a' : 1, 'b' : 2 }; r")
self.assertEqual(1, obj.a)
self.assertEqual(2, obj.b)
self.assertEqual({ 'a' : 1, 'b' : 2 }, dict(obj))
self.assertEqual({ 'a': 1,
'b': [1, 2, 3],
'c': { 'str' : 'goofy',
'float' : 1.234,
'obj' : { 'name': 'john doe' }},
'd': True,
'e': None },
convert(ctxt.eval("""var x =
{ a: 1,
b: [1, 2, 3],
c: { str: 'goofy',
float: 1.234,
obj: { name: 'john doe' }},
d: true,
e: null }; x""")))
Original comment by flier...@gmail.com
on 6 May 2009 at 3:45
Original issue reported on code.google.com by
marc.boe...@gmail.com
on 5 May 2009 at 7:56