Verified Commit b81d7026 authored by Stefan Brunecker's avatar Stefan Brunecker
Browse files

Add type check for scalar values

parent c9970776
......@@ -30,6 +30,11 @@ function setPower(_power) {
throw "power: Could not evaluate input";
}
//check type
if (math.typeof(value) !== "Unit") {
throw "power: Expected type Unit";
}
//check unit
var expectedUnit = math.eval("kg*m^2/s^3");
if (math.typeof(expectedUnit) !== math.typeof(value) || !expectedUnit.equalBase(value)) {
......
......@@ -29,6 +29,11 @@ function setInNoRangeNoUnit(_inNoRangeNoUnit) {
throw "inNoRangeNoUnit: Could not evaluate input";
}
//check type
if (math.typeof(value) !== "number") {
throw "inNoRangeNoUnit: Expected type number";
}
var value_num = value;
Module.setInNoRangeNoUnit(value_num);
}
......@@ -31,6 +31,11 @@ function setInRangeNoUnit(_inRangeNoUnit) {
throw "inRangeNoUnit: Could not evaluate input";
}
//check type
if (math.typeof(value) !== "number") {
throw "inRangeNoUnit: Expected type number";
}
var value_num = value;
//check range
if (math.smaller(value_num, lower)) {
......
......@@ -31,6 +31,10 @@ function setInRangeUnit(_inRangeUnit) {
throw "inRangeUnit: Could not evaluate input";
}
//check type
if (math.typeof(value) !== "Unit") {
throw "inRangeUnit: Expected type Unit";
}
//check unit
var expectedUnit = math.eval("m/s");
if (math.typeof(expectedUnit) !== math.typeof(value) || !expectedUnit.equalBase(value)) {
......
......@@ -118,6 +118,11 @@ function setC(_c) {
throw "c: Could not evaluate input";
}
//check type
if (math.typeof(value) !== "Unit") {
throw "c: Expected type Unit";
}
//check unit
var expectedUnit = math.eval("mm/h");
if (math.typeof(expectedUnit) !== math.typeof(value) || !expectedUnit.equalBase(value)) {
......
......@@ -28,6 +28,8 @@ function execute() {
<#assign lowerBoundVar = "lower">
<#assign upperBoundVar = "upper">
<#assign portName = setter.parameterName>
<#assign typeUnit = "Unit">
<#assign typeNumber = "number">
<#--prefix parameter with "_" so ports can be named Javascript keywords (e.g. "undefined", "var")-->
function ${setter.methodName}(_${setter.parameterName}) {
......@@ -74,6 +76,7 @@ function execute() {
</@forloop>
Module.${setter.delegateMethodName}(array);
<#else>
<@checkType identifier=portName var=varName type=setter.lowerBoundUnit?has_content?then(typeUnit, typeNumber)/>
<@checkUnit identifier=portName unit=setter.lowerBoundUnit!"" var=varName/>
<#if setter.lowerBoundUnit?has_content>
var ${varNumber} = ${varName}.toSI().toNumber();
......@@ -125,4 +128,11 @@ function execute() {
}
</#if>
</#if>
</#macro>
<#macro checkType identifier var type>
//check type
if (math.typeof(${var}) !== "${type}") {
throw "${identifier}: Expected type ${type}";
}
</#macro>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment