Optional scope (works for expressions without identifiers)

This commit is contained in:
Tim Schaub
2013-06-11 13:22:35 -06:00
parent d920d8e578
commit 6d0badcf2a
4 changed files with 46 additions and 40 deletions

View File

@@ -29,8 +29,9 @@ ol.expression.Expression = function() {};
/**
* Evaluate the expression and return the result.
*
* @param {Object} scope Evaluation scope. All properties of this object
* will be available as variables when evaluating the expression.
* @param {Object=} opt_scope Evaluation scope. All properties of this object
* will be available as variables when evaluating the expression. If not
* provided, `null` will be used.
* @param {Object=} opt_fns Optional scope for looking up functions. If not
* provided, functions will be looked in the evaluation scope.
* @param {Object=} opt_this Object to use as this when evaluating call
@@ -71,8 +72,8 @@ goog.inherits(ol.expression.Call, ol.expression.Expression);
/**
* @inheritDoc
*/
ol.expression.Call.prototype.evaluate = function(scope, opt_fns, opt_this) {
var fnScope = goog.isDefAndNotNull(opt_fns) ? opt_fns : scope;
ol.expression.Call.prototype.evaluate = function(opt_scope, opt_fns, opt_this) {
var fnScope = goog.isDefAndNotNull(opt_fns) ? opt_fns : opt_scope;
var fn = this.expr_.evaluate(fnScope);
if (!fn || !goog.isFunction(fn)) {
throw new Error('Expected function but found ' + fn);
@@ -82,7 +83,7 @@ ol.expression.Call.prototype.evaluate = function(scope, opt_fns, opt_this) {
var len = this.args_.length;
var values = new Array(len);
for (var i = 0; i < len; ++i) {
values[i] = this.args_[i].evaluate(scope, opt_fns, opt_this);
values[i] = this.args_[i].evaluate(opt_scope, opt_fns, opt_this);
}
return fn.apply(thisArg, values);
};
@@ -156,11 +157,11 @@ ol.expression.Comparison.isValidOp = (function() {
/**
* @inheritDoc
*/
ol.expression.Comparison.prototype.evaluate = function(scope, opt_this,
ol.expression.Comparison.prototype.evaluate = function(opt_scope, opt_this,
opt_fns) {
var result;
var rightVal = this.right_.evaluate(scope, opt_fns, opt_this);
var leftVal = this.left_.evaluate(scope, opt_fns, opt_this);
var rightVal = this.right_.evaluate(opt_scope, opt_fns, opt_this);
var leftVal = this.left_.evaluate(opt_scope, opt_fns, opt_this);
switch (this.operator_) {
case ol.expression.ComparisonOp.EQ:
@@ -217,8 +218,11 @@ goog.inherits(ol.expression.Identifier, ol.expression.Expression);
/**
* @inheritDoc
*/
ol.expression.Identifier.prototype.evaluate = function(scope) {
return scope[this.name_];
ol.expression.Identifier.prototype.evaluate = function(opt_scope) {
if (!goog.isDefAndNotNull(opt_scope)) {
throw new Error('Attempt to evaluate identifier with no scope');
}
return opt_scope[this.name_];
};
@@ -312,10 +316,11 @@ ol.expression.Logical.isValidOp = (function() {
/**
* @inheritDoc
*/
ol.expression.Logical.prototype.evaluate = function(scope, opt_fns, opt_this) {
ol.expression.Logical.prototype.evaluate = function(opt_scope, opt_fns,
opt_this) {
var result;
var rightVal = this.right_.evaluate(scope, opt_fns, opt_this);
var leftVal = this.left_.evaluate(scope, opt_fns, opt_this);
var rightVal = this.right_.evaluate(opt_scope, opt_fns, opt_this);
var leftVal = this.left_.evaluate(opt_scope, opt_fns, opt_this);
if (this.operator_ === ol.expression.LogicalOp.AND) {
result = leftVal && rightVal;
@@ -393,10 +398,10 @@ ol.expression.Math.isValidOp = (function() {
/**
* @inheritDoc
*/
ol.expression.Math.prototype.evaluate = function(scope, opt_fns, opt_this) {
ol.expression.Math.prototype.evaluate = function(opt_scope, opt_fns, opt_this) {
var result;
var rightVal = this.right_.evaluate(scope, opt_fns, opt_this);
var leftVal = this.left_.evaluate(scope, opt_fns, opt_this);
var rightVal = this.right_.evaluate(opt_scope, opt_fns, opt_this);
var leftVal = this.left_.evaluate(opt_scope, opt_fns, opt_this);
/**
* TODO: throw if rightVal, leftVal not numbers - this would require the use
* of a concat function for strings but it would let us serialize these as
@@ -457,8 +462,9 @@ goog.inherits(ol.expression.Member, ol.expression.Expression);
/**
* @inheritDoc
*/
ol.expression.Member.prototype.evaluate = function(scope, opt_fns, opt_this) {
var obj = this.expr_.evaluate(scope, opt_fns, opt_this);
ol.expression.Member.prototype.evaluate = function(opt_scope, opt_fns,
opt_this) {
var obj = this.expr_.evaluate(opt_scope, opt_fns, opt_this);
if (!goog.isObject(obj)) {
throw new Error('Expected member expression to evaluate to an object ' +
'but got ' + obj);
@@ -490,6 +496,6 @@ goog.inherits(ol.expression.Not, ol.expression.Expression);
/**
* @inheritDoc
*/
ol.expression.Not.prototype.evaluate = function(scope, opt_fns, opt_this) {
return !this.expr_.evaluate(scope, opt_fns, opt_this);
ol.expression.Not.prototype.evaluate = function(opt_scope, opt_fns, opt_this) {
return !this.expr_.evaluate(opt_scope, opt_fns, opt_this);
};

View File

@@ -26,7 +26,7 @@ describe('ol.expression.parse', function() {
it('parses string literal expressions', function() {
var expr = ol.expression.parse('"foo"');
expect(expr).to.be.a(ol.expression.Literal);
expect(expr.evaluate({})).to.be('foo');
expect(expr.evaluate()).to.be('foo');
});
it('throws on unterminated string', function() {
@@ -38,7 +38,7 @@ describe('ol.expression.parse', function() {
it('parses numeric literal expressions', function() {
var expr = ol.expression.parse('.42e+2');
expect(expr).to.be.a(ol.expression.Literal);
expect(expr.evaluate({})).to.be(42);
expect(expr.evaluate()).to.be(42);
});
it('throws on invalid number', function() {
@@ -50,13 +50,13 @@ describe('ol.expression.parse', function() {
it('parses boolean literal expressions', function() {
var expr = ol.expression.parse('false');
expect(expr).to.be.a(ol.expression.Literal);
expect(expr.evaluate({})).to.be(false);
expect(expr.evaluate()).to.be(false);
});
it('parses null literal expressions', function() {
var expr = ol.expression.parse('null');
expect(expr).to.be.a(ol.expression.Literal);
expect(expr.evaluate({})).to.be(null);
expect(expr.evaluate()).to.be(null);
});
});

View File

@@ -250,22 +250,22 @@ describe('ol.expression.Literal', function() {
describe('#evaluate()', function() {
it('works for numeric literal', function() {
var expr = new ol.expression.Literal(42e-11);
expect(expr.evaluate({})).to.be(4.2e-10);
expect(expr.evaluate()).to.be(4.2e-10);
});
it('works for string literal', function() {
var expr = new ol.expression.Literal('asdf');
expect(expr.evaluate({})).to.be('asdf');
expect(expr.evaluate()).to.be('asdf');
});
it('works for boolean literal', function() {
var expr = new ol.expression.Literal(true);
expect(expr.evaluate({})).to.be(true);
expect(expr.evaluate()).to.be(true);
});
it('works for null literal', function() {
var expr = new ol.expression.Literal(null);
expect(expr.evaluate({})).to.be(null);
expect(expr.evaluate()).to.be(null);
});
});
});
@@ -344,7 +344,7 @@ describe('ol.expression.Math', function() {
new ol.expression.Literal(40),
new ol.expression.Literal(2));
expect(expr.evaluate({})).to.be(42);
expect(expr.evaluate()).to.be(42);
});
it('does + with string literal (note: subject to change)', function() {
@@ -353,7 +353,7 @@ describe('ol.expression.Math', function() {
new ol.expression.Literal('foo'),
new ol.expression.Literal('bar'));
expect(expr.evaluate({})).to.be('foobar');
expect(expr.evaluate()).to.be('foobar');
});
it('does + with identifiers', function() {
@@ -480,30 +480,30 @@ describe('ol.expression.Not', function() {
describe('#evaluate()', function() {
it('returns the logical complement', function() {
var expr = new ol.expression.Not(new ol.expression.Literal(true));
expect(expr.evaluate({})).to.be(false);
expect(expr.evaluate()).to.be(false);
expr = new ol.expression.Not(new ol.expression.Literal(false));
expect(expr.evaluate({})).to.be(true);
expect(expr.evaluate()).to.be(true);
});
it('negates a truthy string', function() {
var expr = new ol.expression.Not(new ol.expression.Literal('asdf'));
expect(expr.evaluate({})).to.be(false);
expect(expr.evaluate()).to.be(false);
});
it('negates a falsy string', function() {
var expr = new ol.expression.Not(new ol.expression.Literal(''));
expect(expr.evaluate({})).to.be(true);
expect(expr.evaluate()).to.be(true);
});
it('negates a truthy number', function() {
var expr = new ol.expression.Not(new ol.expression.Literal(42));
expect(expr.evaluate({})).to.be(false);
expect(expr.evaluate()).to.be(false);
});
it('negates a falsy number', function() {
var expr = new ol.expression.Not(new ol.expression.Literal(NaN));
expect(expr.evaluate({})).to.be(true);
expect(expr.evaluate()).to.be(true);
});
});

View File

@@ -36,25 +36,25 @@ describe('ol.expression.Parser', function() {
it('parses string literal', function() {
var expr = parse('"foo"');
expect(expr).to.be.a(ol.expression.Literal);
expect(expr.evaluate({})).to.be('foo');
expect(expr.evaluate()).to.be('foo');
});
it('parses numeric literal', function() {
var expr = parse('.42e2');
expect(expr).to.be.a(ol.expression.Literal);
expect(expr.evaluate({})).to.be(42);
expect(expr.evaluate()).to.be(42);
});
it('parses boolean literal', function() {
var expr = parse('.42e2');
expect(expr).to.be.a(ol.expression.Literal);
expect(expr.evaluate({})).to.be(42);
expect(expr.evaluate()).to.be(42);
});
it('parses null literal', function() {
var expr = parse('null');
expect(expr).to.be.a(ol.expression.Literal);
expect(expr.evaluate({})).to.be(null);
expect(expr.evaluate()).to.be(null);
});
});