From 6546aaa704ad0dfe693e1c06f98e00ef69bceb57 Mon Sep 17 00:00:00 2001 From: yimi Date: Mon, 18 Sep 2017 06:47:05 +0000 Subject: [PATCH] rewrite the constructor for `ast` --- lib/ast.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/ast.js b/lib/ast.js index 0918574d..d253b09a 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -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) {