[UP] py.js for Object and Array converters to py types

bzr revid: xmo@openerp.com-20120404114647-t81pw90940canfwg
This commit is contained in:
Xavier Morel 2012-04-04 13:46:47 +02:00
parent d70220631a
commit 2a8d78c88c
3 changed files with 32 additions and 5 deletions

View File

@ -1,5 +1,5 @@
repo: 076b192d0d8ab2b92d1dbcfa3da055382f30eaea
node: 5adb2d9c89e53a6445e3799f9c4dc9110458c149
node: 1758bfec1ec1dcff95dcc4c72269cc0e3d000afd
branch: default
latesttag: 0.5
latesttagdistance: 9
latesttagdistance: 11

View File

@ -384,6 +384,21 @@ var py = {};
return new py.def(val);
}
switch(val.constructor) {
case Object:
var o = new py.object();
for (var prop in val) {
if (val.hasOwnProperty(prop)) {
o[prop] = val[prop];
}
}
return o;
case Array:
var a = new py.list();
a.values = val;
return a;
}
throw new Error("Could not convert " + val + " to a pyval");
}
// Builtins
@ -448,7 +463,7 @@ var py = {};
__getattribute__: function (name) {
if (name in this) {
var val = this[name];
if ('__get__' in val) {
if (typeof val === 'object' && '__get__' in val) {
// TODO: second argument should be class
return val.__get__(this);
}
@ -627,6 +642,9 @@ var py = {};
}
return py.False;
},
__getitem__: function (index) {
return PY_ensurepy(this.values[index.toJSON()]);
},
toJSON: function () {
var out = [];
for (var i=0; i<this.values.length; ++i) {
@ -846,7 +864,7 @@ var py = {};
case '[':
if (expr.second) {
return py.evaluate(expr.first, context)
.__getitem__(expr.evaluate(expr.second, context));
.__getitem__(py.evaluate(expr.second, context));
}
var list_exprs = expr.first, list_values = [];
for (var k=0; k<list_exprs.length; ++k) {

View File

@ -376,4 +376,13 @@ describe('numerical protocols', function () {
});
});
});
});
});
describe('Type converter', function () {
it('should convert bare objects to objects', function () {
expect(py.eval('foo.bar', {foo: {bar: 3}})).to.be(3);
});
it('should convert arrays to lists', function () {
expect(py.eval('foo[3]', {foo: [9, 8, 7, 6, 5]}))
.to.be(6);
});
});