
js.prompto.declaration.BaseMethodDeclaration.js Maven / Gradle / Ivy
var BaseDeclaration = require("./BaseDeclaration").BaseDeclaration;
var ArgumentList = require("../grammar/ArgumentList").ArgumentList;
var CategoryType = null;
var PromptoError = require("../error/PromptoError").PromptoError;
var ArgumentAssignmentList = require("../grammar/ArgumentAssignmentList").ArgumentAssignmentList;
var ArgumentAssignment = require("../grammar/ArgumentAssignment").ArgumentAssignment;
var Specificity = require("../grammar/Specificity").Specificity;
exports.resolve = function() {
CategoryType = require("../type/CategoryType").CategoryType;
}
function BaseMethodDeclaration(id, args, returnType) {
BaseDeclaration.call(this, id);
this.memberOf = null;
this.args = args || new ArgumentList();
this.returnType = returnType || null;
return this;
}
BaseMethodDeclaration.prototype = Object.create(BaseDeclaration.prototype);
BaseMethodDeclaration.prototype.constructor = BaseMethodDeclaration;
BaseMethodDeclaration.prototype.getDeclarationType = function() {
return "Method";
};
BaseMethodDeclaration.prototype.getSignature = function(context) {
var s = [];
this.args.map(function(arg) {
s.push(arg.getProto());
});
return "(" + s.join(", ") + ")";
};
BaseMethodDeclaration.prototype.getProto = function(context) {
return this.args.map(function(arg) {
return arg.getProto(context);
}).join("/");
};
BaseMethodDeclaration.prototype.unregister = function(context) {
context.unregisterMethodDeclaration (this, this.getProto(context));
};
BaseMethodDeclaration.prototype.register = function(context) {
context.registerMethodDeclaration(this);
};
BaseMethodDeclaration.prototype.registerArguments = function(context) {
if(this.args!=null) {
this.args.register(context);
}
};
BaseMethodDeclaration.prototype.isAssignableTo = function(context, assignments, checkInstance) {
try {
var local = context.newLocalContext();
this.registerArguments(local);
var assignmentsList = new ArgumentAssignmentList(assignments);
for(var i=0;i=0 ? assignmentsList[idx] : null;
if(assignment==null) { // missing argument
if(argument.defaultExpression!=null)
assignment = new ArgumentAssignment(argument, argument.defaultExpression);
else
return false;
}
if(!this.isAssignableToArgument(local, argument, assignment, checkInstance)) {
return false;
}
if(idx>=0)
assignmentsList.remove(idx);
}
return assignmentsList.length===0;
} catch (e) {
if(e instanceof SyntaxError) {
return false;
} else {
throw e;
}
}
};
BaseMethodDeclaration.prototype.isAssignableToArgument = function(context, argument, assignment, checkInstance) {
return this.computeSpecificity(context, argument, assignment, checkInstance)!==Specificity.INCOMPATIBLE;
};
BaseMethodDeclaration.prototype.computeSpecificity = function(context, argument, assignment, checkInstance) {
try {
var required = argument.getType(context);
var actual = assignment.expression.check(context);
// retrieve actual runtime type
if(checkInstance && (actual instanceof CategoryType)) {
var value = assignment.expression.interpret(context.getCallingContext());
if(value && value.getType) {
actual = value.getType();
}
}
if(actual.equals(required)) {
return Specificity.EXACT;
} else if(required.isAssignableFrom(context, actual)) {
return Specificity.INHERITED;
}
actual = assignment.resolve(context,this,checkInstance).check(context);
if(required.isAssignableFrom(context, actual)) {
return Specificity.RESOLVED;
}
} catch(error) {
if(!(error instanceof PromptoError )) {
throw error;
}
}
return Specificity.INCOMPATIBLE;
};
BaseMethodDeclaration.prototype.isEligibleAsMain = function() {
return false;
};
exports.BaseMethodDeclaration = BaseMethodDeclaration;
© 2015 - 2025 Weber Informatics LLC | Privacy Policy