Fixing several function calls with no argument errors.

+ Functions max and min now throw an error if no arguments are provided.
+ Objects called now throw an error if no argument is provided.
+ Fixing tokenizer's seek error reporting system (leads to very rare types of errors).
+ Removing expression simplifications that could make the app freeze when no arguments are provided to a function.
This commit is contained in:
Adsooi 2023-05-24 07:43:40 +02:00
parent 35ce1c4824
commit 9239eac78a
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
3 changed files with 22 additions and 15 deletions
LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js

View file

@ -241,7 +241,10 @@ function evaluate(tokens, expr, values) {
nstack.push(f.apply(undefined, args));
} else if(f.execute) {
// Objects & expressions execution
nstack.push(f.execute.apply(f, args));
if(args.length >= 1)
nstack.push(f.execute.apply(f, args));
else
throw new Error(qsTranslate('error', 'In order to be executed, object %1 must have at least one argument.').arg(f))
} else {
throw new Error(qsTranslate('error', '%1 cannot be executed.').arg(f));
}
@ -1614,16 +1617,20 @@ function arrayIndex(array, index) {
function max(array) {
if (arguments.length === 1 && Array.isArray(array)) {
return Math.max.apply(Math, array);
} else {
} else if(arguments.length >= 1) {
return Math.max.apply(Math, arguments);
} else {
throw new EvalError(qsTranslate('error', 'Function %1 must have at least one argument.').arg('max'))
}
}
function min(array) {
if (arguments.length === 1 && Array.isArray(array)) {
return Math.min.apply(Math, array);
} else {
} else if(arguments.length >= 1) {
return Math.min.apply(Math, arguments);
} else {
throw new EvalError(qsTranslate('error', 'Function %1 must have at least one argument.').arg('min'))
}
}

View file

@ -36,7 +36,7 @@ class InputExpression {
if(!this.atEnd() && this.peek() == char) {
this.position++;
} else {
this.raise("Unexpected character " + peek() + ". Expected character " + char);
this.raise("Unexpected character " + this.peek() + ". Expected character " + char);
}
}

View file

@ -216,16 +216,16 @@ function simplifyExpression(str) {
}
],
// Simple simplifications
[/(\s|^|\()0(\.0+)? \* (\([^)(]+\))/g, '$10'],
[/(\s|^|\()0(\.0+)? \* ([^)(+-]+)/g, '$10'],
[/(\([^)(]\)) \* 0(\.0+)?(\s|$|\))/g, '0$3'],
[/([^)(+-]) \* 0(\.0+)?(\s|$|\))/g, '0$3'],
[/(\s|^|\()1(\.0+)? (\*|\/) /g, '$1'],
[/(\s|^|\()0(\.0+)? (\+|\-) /g, '$1'],
[/ (\*|\/) 1(\.0+)?(\s|$|\))/g, '$3'],
[/ (\+|\-) 0(\.0+)?(\s|$|\))/g, '$3'],
[/(^| |\() /g, '$1'],
[/ ($|\))/g, '$1'],
// [/(\s|^|\()0(\.0+)? \* (\([^)(]+\))/g, '$10'],
// [/(\s|^|\()0(\.0+)? \* ([^)(+-]+)/g, '$10'],
// [/(\([^)(]\)) \* 0(\.0+)?(\s|$|\))/g, '0$3'],
// [/([^)(+-]) \* 0(\.0+)?(\s|$|\))/g, '0$3'],
// [/(\s|^|\()1(\.0+)? (\*|\/) /g, '$1'],
// [/(\s|^|\()0(\.0+)? (\+|\-) /g, '$1'],
// [/ (\*|\/) 1(\.0+)?(\s|$|\))/g, '$3'],
// [/ (\+|\-) 0(\.0+)?(\s|$|\))/g, '$3'],
// [/(^| |\() /g, '$1'],
// [/ ($|\))/g, '$1'],
]
// Replacements
@ -271,7 +271,7 @@ function makeExpressionReadable(str) {
}]
]
str = simplifyExpression(str)
// str = simplifyExpression(str)
// Replacements
for(var replacement of replacements)
while(replacement[0].test(str))