Skip to content

Commit

Permalink
Miscellaneous cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Aug 31, 2017
1 parent 354011e commit 2ab1313
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/style-spec/function/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ function compileExpression(

const compiled = parsed.compile();
if (typeof compiled === 'string') {
const fn = new Function('$globalProperties', '$feature', `
const fn = (new Function('$this', '$globalProperties', '$feature', `
$globalProperties = $globalProperties || {};
var $props = $feature && $feature.properties || {};
var $this = this;
return this.unwrap(${compiled})
`);
return $this.unwrap(${compiled})
`) : any);

return {
result: 'success',
function: fn.bind(evaluationContext()),
function: (globalProperties, feature) =>
fn(evaluationContext(), globalProperties, feature),
functionSource: compiled,
isFeatureConstant: isFeatureConstant(parsed),
isZoomConstant: isZoomConstant(parsed),
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/function/definitions/at.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class At implements Expression {
}

compile() {
return `this.at(${this.index.compile()}, ${this.input.compile()})`;
return `$this.at(${this.index.compile()}, ${this.input.compile()})`;
}

serialize() {
Expand Down
14 changes: 8 additions & 6 deletions src/style-spec/function/definitions/coalesce.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ class Coalesce implements Expression {
}

compile() {
return `(function coalesce() { ${this.args.slice(0, -1).map(a => `
try {
var result = ${a.compile()};
const compiledArgs = [];
for (let i = 0; i < this.args.length - 1; i++) {
compiledArgs.push(`try {
var result = ${this.args[i].compile()};
if (result !== null) return result;
} catch (e) { }`).join('\n')}
return ${this.args[this.args.length - 1].compile()}
})()`;
} catch (e) {}`);
}
compiledArgs.push(`return ${this.args[this.args.length - 1].compile()};`);
return `(function coalesce() {\n${compiledArgs.join('\n')}\n})()`;
}

serialize() {
Expand Down
11 changes: 9 additions & 2 deletions src/style-spec/function/evaluation_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ const types = {
Value: ValueType
};

const jsTypes = {
Number: 'number',
String: 'string',
Boolean: 'boolean',
Object: 'object'
};

class RuntimeError {
name: string;
message: string;
Expand Down Expand Up @@ -100,8 +107,8 @@ module.exports = () => ({
typeError = value === null;
} else if (expectedType.kind === 'Value') {
typeError = false;
} else if (expectedType.kind !== 'Array' && expectedType.kind !== 'Color') {
typeError = typeof value !== expectedType.kind.toLowerCase();
} else if (expectedType.kind !== 'Array' && expectedType.kind !== 'Color' && expectedType.kind !== 'Error') {
typeError = typeof value !== jsTypes[expectedType.kind];
} else {
type = typeOf(value);
typeError = checkSubtype(expectedType, type);
Expand Down

0 comments on commit 2ab1313

Please sign in to comment.