Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite the constructor for ast #2318

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,12 @@ function run() {
var result = {
_class: "AST_" + value.TYPE
};
value.CTOR.PROPS.forEach(function(prop) {
result[prop] = value[prop];
});
var ctor = value.CTOR;
do {
ctor.SELF_PROPS.forEach(function(prop) {
result[prop] = value[prop];
});
} while (ctor = ctor.BASE);
return result;
}
return value;
Expand Down
32 changes: 18 additions & 14 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,35 @@ 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 proto = base && new base;
if (proto && proto.initialize || (methods && methods.initialize))
code += "this.initialize();";
code += "}}";
var code = "return function AST_" + type + "(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 ctor = new Function(code)();
var proto = base && new base;
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.SELF_PROPS = props;
ctor.SUBCLASSES = [];
if (type) {
ctor.prototype.TYPE = ctor.TYPE = type;
}
if (methods) for (i in methods) if (HOP(methods, i)) {
if (methods) for (var i in methods) if (HOP(methods, i)) {
if (/^\$/.test(i)) {
ctor[i.substr(1)] = methods[i];
} else {
Expand Down