rewrite the constructor for ast

This commit is contained in:
yimi 2017-09-18 06:47:05 +00:00
parent aceb0af36b
commit 6546aaa704

View File

@ -47,25 +47,28 @@ function DEFNODE(type, props, methods, base) {
if (arguments.length < 4) base = AST_Node;
if (!props) props = [];
else props = props.split(/\s+/);
var self_props = props;
if (base && base.PROPS)
props = props.concat(base.PROPS);
var code = "return function AST_" + type + "(props){ if (props) { ";
for (var i = props.length; --i >= 0;) {
code += "this." + props[i] + " = props." + props[i] + ";";
var ctor = function (props) {
if (props) {
var ctor = this.CTOR;
do {
var self_props = ctor.SELF_PROPS;
for (var i = self_props.length; i;) {
var k = self_props[--i];
this[k] = props[k];
}
} while (ctor = ctor.BASE)
if (this.initialize) {
this.initialize();
}
}
}
var proto = base && new base;
if (proto && proto.initialize || (methods && methods.initialize))
code += "this.initialize();";
code += "}}";
var ctor = new Function(code)();
if (proto) {
ctor.prototype = proto;
ctor.BASE = base;
}
if (base) base.SUBCLASSES.push(ctor);
ctor.prototype.CTOR = ctor;
ctor.PROPS = props || null;
ctor.SELF_PROPS = self_props;
ctor.SUBCLASSES = [];
if (type) {