forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructseq.js
98 lines (88 loc) · 3.23 KB
/
structseq.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Sk.builtin.structseq_types = {};
Sk.builtin.make_structseq = function (module, name, fields, doc) {
var nm = module + "." + name;
var flds = [];
var docs = [];
var i;
for (var key in fields) {
flds.push(key);
docs.push(fields[key]);
}
/**
* @constructor
* @extends Sk.builtin.tuple
* @param {!Array<Object>|Object} arg
*/
var cons = function structseq_constructor(arg) {
Sk.builtin.pyCheckArgsLen(nm, arguments.length, 1, 1);
var o;
var it, i, /** @type {!Array<Object>} */v;
if (!(this instanceof Sk.builtin.structseq_types[nm])) {
o = Object.create(Sk.builtin.structseq_types[nm].prototype);
o.constructor.apply(o, arguments);
return o;
}
if (Array.isArray(arg)) {
v = arg;
} else {
v = [];
for (it = Sk.abstr.iter(arg), i = it.tp$iternext(); i !== undefined; i = it.tp$iternext()) {
v.push(i);
}
if (v.length != flds.length) {
throw new Sk.builtin.TypeError(nm + "() takes a " + flds.length + "-sequence (" + v.length + "-sequence given)");
}
}
Sk.builtin.tuple.call(this, v);
this.__class__ = Sk.builtin.structseq_types[nm];
};
Sk.builtin.structseq_types[nm] = cons;
Sk.abstr.inherits(cons, Sk.builtin.tuple);
if (doc) {
cons.prototype.__doc__ = doc;
}
cons.prototype.tp$name = nm;
cons.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj(nm, Sk.builtin.structseq_types[nm]);
cons.prototype.ob$type["$d"] = new Sk.builtin.dict([]);
cons.prototype.ob$type["$d"].mp$ass_subscript(Sk.builtin.type.basesStr_, new Sk.builtin.tuple([Sk.builtin.tuple]));
//var mro = Sk.builtin.type.buildMRO(cons.prototype.ob$type);
//cons.prototype.ob$type["$d"].mp$ass_subscript(Sk.builtin.type.mroStr_, mro);
//cons.prototype.ob$type.tp$mro = mro;
cons.prototype.__getitem__ = new Sk.builtin.func(function (self, index) {
return Sk.builtin.tuple.prototype.mp$subscript.call(self, index);
});
cons.prototype.__reduce__ = new Sk.builtin.func(function (self) {
throw new Sk.builtin.Exception("__reduce__ is not implemented");
});
cons.prototype["$r"] = function () {
var ret;
var i;
var bits;
if (this.v.length === 0) {
return new Sk.builtin.str(nm + "()");
}
bits = [];
for (i = 0; i < this.v.length; ++i) {
bits[i] = flds[i] + "=" + Sk.misceval.objectRepr(this.v[i]).v;
}
ret = bits.join(", ");
if (this.v.length === 1) {
ret += ",";
}
return new Sk.builtin.str(nm + "(" + ret + ")");
};
cons.prototype.tp$setattr = function (pyName, value) {
throw new Sk.builtin.AttributeError("readonly property");
};
cons.prototype.tp$getattr = function (pyName) {
var jsName = pyName.$jsstr();
var i = flds.indexOf(jsName);
if (i >= 0) {
return this.v[i];
} else {
return Sk.builtin.object.prototype.GenericGetAttr(pyName);
}
};
return cons;
};
Sk.exportSymbol("Sk.builtin.make_structseq", Sk.builtin.make_structseq);