Skip to content

Commit

Permalink
Less awkward initialization of lib/def.js
Browse files Browse the repository at this point in the history
Only run the initializer when the infer module has been initialized

Removes need to lazy-init constraints in def.js.
  • Loading branch information
marijnh committed May 23, 2013
1 parent 83cc696 commit 2010f93
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
55 changes: 25 additions & 30 deletions lib/def.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,50 +383,45 @@
var customFunctions = Object.create(null);
infer.registerFunction = function(name, f) { customFunctions[name] = f; };

var _constraints;
function constraints() {
if (_constraints) return _constraints;
_constraints = {};
_constraints.IsCreated = infer.constraint("created, target, spec", {
addType: function(tp) {
if (tp instanceof infer.Obj && this.created++ < 5) {
var derived = new infer.Obj(tp), spec = this.spec;
if (spec instanceof infer.AVal) spec = spec.getType();
if (spec instanceof infer.Obj) for (var prop in spec.props) {
var cur = spec.props[prop].types[0];
var p = derived.defProp(prop);
if (cur && cur instanceof infer.Obj && cur.props.value) {
var vtp = cur.props.value.getType();
if (vtp) p.addType(vtp);
}
var IsCreated = infer.constraint("created, target, spec", {
addType: function(tp) {
if (tp instanceof infer.Obj && this.created++ < 5) {
var derived = new infer.Obj(tp), spec = this.spec;
if (spec instanceof infer.AVal) spec = spec.getType();
if (spec instanceof infer.Obj) for (var prop in spec.props) {
var cur = spec.props[prop].types[0];
var p = derived.defProp(prop);
if (cur && cur instanceof infer.Obj && cur.props.value) {
var vtp = cur.props.value.getType();
if (vtp) p.addType(vtp);
}
this.target.addType(derived)
}
this.target.addType(derived)
}
});
_constraints.IsBound = infer.constraint("args, target", {
addType: function(tp) {
if (!(tp instanceof infer.Fn)) return;
var cut = Math.max(0, this.args.length - 1);
this.target.addType(new infer.Fn(tp.name, this.args[0] || infer.ANull,
tp.args.slice(cut), tp.argNames.slice(cut), tp.retval));
}
});
return _constraints;
}
}
});

infer.registerFunction("Object_create", function(self, args, argNodes) {
if (argNodes.length && argNodes[0].type == "Literal" && argNodes[0].value == null)
return new infer.Obj();

var result = new infer.AVal;
if (args[0]) args[0].propagate(new (constraints().IsCreated)(0, result, args[1]));
if (args[0]) args[0].propagate(new IsCreated(0, result, args[1]));
return result;
});

var IsBound = infer.constraint("args, target", {
addType: function(tp) {
if (!(tp instanceof infer.Fn)) return;
var cut = Math.max(0, this.args.length - 1);
this.target.addType(new infer.Fn(tp.name, this.args[0] || infer.ANull,
tp.args.slice(cut), tp.argNames.slice(cut), tp.retval));
}
});

infer.registerFunction("Function_bind", function(self, args) {
var result = new infer.AVal;
self.propagate(new (constraints().IsBound)(args, result));
self.propagate(new IsBound(args, result));
return result;
});

Expand Down
8 changes: 5 additions & 3 deletions lib/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
})(function(exports, acorn, acorn_loose, walk, def, signal) {
"use strict";

// Delayed initialization because of cyclic dependencies.
def = exports.def = def.init({}, exports);

var toString = exports.toString = function(type, maxDepth, parent) {
return !type || type == parent ? "?": type.toString(maxDepth);
};
Expand Down Expand Up @@ -1403,4 +1400,9 @@
var scope = scopeAt(ast, pos, defaultScope), locals = [];
scope.gatherProperties(f, 0);
};

// INIT DEF MODULE

// Delayed initialization because of cyclic dependencies.
def = exports.def = def.init({}, exports);
});

0 comments on commit 2010f93

Please sign in to comment.