Throw when parsing encounters an unexpected type

This commit is contained in:
Tim Schaub
2021-02-10 19:18:55 -07:00
parent 98ac74e0bf
commit c9a3b3bf92
2 changed files with 43 additions and 4 deletions

View File

@@ -260,24 +260,35 @@ export function expressionToGlsl(context, value, typeHint) {
} }
return operator.toGlsl(context, value.slice(1), typeHint); return operator.toGlsl(context, value.slice(1), typeHint);
} }
const valueType = getValueType(value); const valueType = getValueType(value);
if ((valueType & ValueTypes.NUMBER) > 0) { if ((valueType & ValueTypes.NUMBER) > 0) {
return numberToGlsl(/** @type {number} */ (value)); return numberToGlsl(/** @type {number} */ (value));
} else if ((valueType & ValueTypes.BOOLEAN) > 0) { }
if ((valueType & ValueTypes.BOOLEAN) > 0) {
return value.toString(); return value.toString();
} else if ( }
if (
(valueType & ValueTypes.STRING) > 0 && (valueType & ValueTypes.STRING) > 0 &&
(typeHint === undefined || typeHint == ValueTypes.STRING) (typeHint === undefined || typeHint == ValueTypes.STRING)
) { ) {
return stringToGlsl(context, value.toString()); return stringToGlsl(context, value.toString());
} else if ( }
if (
(valueType & ValueTypes.COLOR) > 0 && (valueType & ValueTypes.COLOR) > 0 &&
(typeHint === undefined || typeHint == ValueTypes.COLOR) (typeHint === undefined || typeHint == ValueTypes.COLOR)
) { ) {
return colorToGlsl(/** @type {Array<number> | string} */ (value)); return colorToGlsl(/** @type {Array<number> | string} */ (value));
} else if ((valueType & ValueTypes.NUMBER_ARRAY) > 0) { }
if ((valueType & ValueTypes.NUMBER_ARRAY) > 0) {
return arrayToGlsl(/** @type {Array<number>} */ (value)); return arrayToGlsl(/** @type {Array<number>} */ (value));
} }
throw new Error(`Unexpected expression ${value} (expected type ${typeHint})`);
} }
function assertNumber(value) { function assertNumber(value) {
@@ -366,6 +377,7 @@ Operators['get'] = {
return prefix + value; return prefix + value;
}, },
}; };
Operators['var'] = { Operators['var'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.ANY; return ValueTypes.ANY;
@@ -380,6 +392,7 @@ Operators['var'] = {
return `u_${value}`; return `u_${value}`;
}, },
}; };
Operators['time'] = { Operators['time'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.NUMBER; return ValueTypes.NUMBER;
@@ -389,6 +402,7 @@ Operators['time'] = {
return 'u_time'; return 'u_time';
}, },
}; };
Operators['zoom'] = { Operators['zoom'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.NUMBER; return ValueTypes.NUMBER;
@@ -398,6 +412,7 @@ Operators['zoom'] = {
return 'u_zoom'; return 'u_zoom';
}, },
}; };
Operators['resolution'] = { Operators['resolution'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.NUMBER; return ValueTypes.NUMBER;
@@ -421,6 +436,7 @@ Operators['*'] = {
)})`; )})`;
}, },
}; };
Operators['/'] = { Operators['/'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.NUMBER; return ValueTypes.NUMBER;
@@ -434,6 +450,7 @@ Operators['/'] = {
)})`; )})`;
}, },
}; };
Operators['+'] = { Operators['+'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.NUMBER; return ValueTypes.NUMBER;
@@ -447,6 +464,7 @@ Operators['+'] = {
)})`; )})`;
}, },
}; };
Operators['-'] = { Operators['-'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.NUMBER; return ValueTypes.NUMBER;
@@ -460,6 +478,7 @@ Operators['-'] = {
)})`; )})`;
}, },
}; };
Operators['clamp'] = { Operators['clamp'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.NUMBER; return ValueTypes.NUMBER;
@@ -472,6 +491,7 @@ Operators['clamp'] = {
return `clamp(${expressionToGlsl(context, args[0])}, ${min}, ${max})`; return `clamp(${expressionToGlsl(context, args[0])}, ${min}, ${max})`;
}, },
}; };
Operators['%'] = { Operators['%'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.NUMBER; return ValueTypes.NUMBER;
@@ -485,6 +505,7 @@ Operators['%'] = {
)})`; )})`;
}, },
}; };
Operators['^'] = { Operators['^'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.NUMBER; return ValueTypes.NUMBER;
@@ -498,6 +519,7 @@ Operators['^'] = {
)})`; )})`;
}, },
}; };
Operators['>'] = { Operators['>'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.BOOLEAN; return ValueTypes.BOOLEAN;
@@ -511,6 +533,7 @@ Operators['>'] = {
)})`; )})`;
}, },
}; };
Operators['>='] = { Operators['>='] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.BOOLEAN; return ValueTypes.BOOLEAN;
@@ -524,6 +547,7 @@ Operators['>='] = {
)})`; )})`;
}, },
}; };
Operators['<'] = { Operators['<'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.BOOLEAN; return ValueTypes.BOOLEAN;
@@ -537,6 +561,7 @@ Operators['<'] = {
)})`; )})`;
}, },
}; };
Operators['<='] = { Operators['<='] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.BOOLEAN; return ValueTypes.BOOLEAN;
@@ -580,7 +605,9 @@ function getEqualOperator(operator) {
}, },
}; };
} }
Operators['=='] = getEqualOperator('=='); Operators['=='] = getEqualOperator('==');
Operators['!='] = getEqualOperator('!='); Operators['!='] = getEqualOperator('!=');
Operators['!'] = { Operators['!'] = {
@@ -615,7 +642,9 @@ function getDecisionOperator(operator) {
} }
Operators['all'] = getDecisionOperator('&&'); Operators['all'] = getDecisionOperator('&&');
Operators['any'] = getDecisionOperator('||'); Operators['any'] = getDecisionOperator('||');
Operators['between'] = { Operators['between'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.BOOLEAN; return ValueTypes.BOOLEAN;
@@ -644,6 +673,7 @@ Operators['array'] = {
return `vec${args.length}(${parsedArgs.join(', ')})`; return `vec${args.length}(${parsedArgs.join(', ')})`;
}, },
}; };
Operators['color'] = { Operators['color'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.COLOR; return ValueTypes.COLOR;
@@ -720,6 +750,7 @@ Operators['interpolate'] = {
return result; return result;
}, },
}; };
Operators['match'] = { Operators['match'] = {
getReturnType: function (args) { getReturnType: function (args) {
let type = ValueTypes.ANY; let type = ValueTypes.ANY;
@@ -752,6 +783,7 @@ Operators['match'] = {
return result; return result;
}, },
}; };
Operators['case'] = { Operators['case'] = {
getReturnType: function (args) { getReturnType: function (args) {
let type = ValueTypes.ANY; let type = ValueTypes.ANY;

View File

@@ -273,6 +273,13 @@ describe('ol.style.expressions', function () {
).to.eql('vec4(a_attr4 / 255.0, 1.0 / 255.0, 2.0 / 255.0, 0.5)'); ).to.eql('vec4(a_attr4 / 255.0, 1.0 / 255.0, 2.0 / 255.0, 0.5)');
}); });
it('throws if the value does not match the type', function () {
const call = function () {
expressionToGlsl(context, '42', ValueTypes.NUMBER);
};
expect(call).to.throwException(/Unexpected expression/);
});
it('correctly adapts output for fragment shaders', function () { it('correctly adapts output for fragment shaders', function () {
context.inFragmentShader = true; context.inFragmentShader = true;
expect(expressionToGlsl(context, ['get', 'myAttr'])).to.eql('v_myAttr'); expect(expressionToGlsl(context, ['get', 'myAttr'])).to.eql('v_myAttr');