summaryrefslogtreecommitdiffhomepage
path: root/eval.c
AgeCommit message (Collapse)Author
2020-10-06eval: properly break out of switch/case on return/continue/exceptionJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-06eval: restore context pointer of first evaluated dot/bracket expressionJo-Philipp Wich
When an expression such as `foo.bar(a.b, c.d)` is evaluated, the state context pointer will point to `c` while we need `foo` when invoking functions. Since the context pointer is only relevant for function calls and since for function call opcodes, the lhs expression is always the first operand, there is no need to store the context of subsequent ops. Adjust the ut_get_operands() procedure to restore state->ctx to the result of the first evaluated operand. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-06treewide: rework exception context formattingJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-06ast, eval: track current file name across function invocationsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-05eval: restore correct scope after leaving functionJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-04treewide: rework function scopingJo-Philipp Wich
- Implement proper closure scoping for function - Avoid circular references when managing scopes pointers - Eliminate ut_putval() in favor to json_object_put() - Fix function return value handling - Change internal function structure Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-02eval: avoid null pointer access in ut_invoke()Jo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-02eval: properly forward execeptions in for-in loop value evaluationJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-02ast: store function declarations as opcode offsetsJo-Philipp Wich
We cannot use direct pointers since the opcode array might be reallocated resulting in potentially changed memory addresses. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-02eval: free previous exception when storing a new oneJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-02eval: fix potential uninitialized memory access in ut_getref()Jo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-02Revert "eval: release root scope after finishing the execution"Jo-Philipp Wich
This reverts commit 206630d7141f32594e9110081f6710446f5aebd3. Revert this commit to fix a double-free. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-02treewide: rework handling of memory allocation failuresJo-Philipp Wich
Instead of propagating failures to the caller, print a generic error message and terminate program execution through abort(). Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-02eval: release root scope after finishing the executionJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-24syntax: add regular expression supportJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-23eval: ensure that argument array is initialized when invoking C functionsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-23eval: propagate exceptions when resolving referencesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-22syntax: introduce case statement supportJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-21eval: fix potential null pointer access when computing operandsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-21syntax: introduce try/catch blocksJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-21eval: don't escape slashes when outputting array or object JSONJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-21treewide: ensure to properly propagate exceptionsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-20eval: fix leaking calculated object keysJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-20eval: fix double free of env valuesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-20eval: implement -m option to preload modulesJo-Philipp Wich
The -m option instructs the interpreter to automatically require the named module and to register the module context as global variable. The following two commands are equivalent, with the former one serving as a shortcut for the latter: utpl -m fs -s '{{ fs.open("test.txt").read("all") }}' utpl -s '{% fs = require("fs"); print(fs.open("test.txt").read("all")) %}' Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-20eval: improve reference error reportingJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-20Revert "treewide: rework exception handling"Jo-Philipp Wich
This reverts commit 54bb15b2be3656e91386b80074f45591b20fed3f. Relying on setjmp() / longjmp() causes too many headaches trying to track and properly release intermediate values. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-17eval: sanitize variable namesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-16eval: implement -e and -E options to set global vars from JSONJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-16treewide: implement strict assignment modeJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-16treewide: rework exception handlingJo-Philipp Wich
Use setjmp() and longjmp() to deal with runtime exceptions. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-15eval: only set "this" scope for functions being object membersJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-14eval: increase refcount when reading function contextJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-13syntax: fix for() loop quirks and variable scopingJo-Philipp Wich
- Support `for (var x in ...)` syntax - Support `for (var i = 0; i < ...; i++)` syntax - Properly handle "for" loops without condition and increment expression - Reject for-in loops with invalid lhs Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-13eval: use ut_getscope() in ut_execute_local()Jo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-13eval: make key argument to ut_getref() optionalJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-12eval: improve error messages on null dereferenceJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-12eval: keep references to function contextsJo-Philipp Wich
Otherwise the "this" context might be gc'ed before the function can access it. This bug manifested itself with long chained expressions such as: require("fs").open("file.txt").read(10) The file handle produced by open() was gc'ed before invoking read() on it due to the evaluation not increasing its refcount. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-11syntax: introduce !== and === operatorsJo-Philipp Wich
Also treat "in" as relational operator. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-11eval: eliminate use of ceil()Jo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-10treewide: eliminate unused function argumentsJo-Philipp Wich
Also introduce convenience macro for registering function arrays in modules. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-10eval: fix function return value handling in expressionsJo-Philipp Wich
Since ut_invoke() takes care of unwrapping magic null, we don't need any special treatment for T_RETURN tags when processing expressions. Also drop T_BREAK case while we're at it since it cannot happen in this context. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-10eval: fix [] subscript operatorJo-Philipp Wich
Fixes: c735882 ("parser, eval: use an ut_op flag to denote postfix access") Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-10eval: rename "main" to "entry"Jo-Philipp Wich
Fixes the following gcc error: .../eval.c:1435:22: error: 'main' is usually a function [-Werror=main] struct json_object *main, *scope, *args, *rv; ^~~~ cc1: all warnings being treated as errors Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-08parser, eval: use an ut_op flag to denote postfix accessJo-Philipp Wich
The current code still abused the JSON value pointer to denote postfix access for certain operations which led to a crash when freeing the parser state due to an attempt to put a (void *)1 pointer. Since we do have the ability to set flags on operations since the AST rework, use this much cleaner approach and avoid the invalid pointer hackery. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-07eval: ensure that dividing Infinity by Infinity yields NaNJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-07eval: ensure that x / NaN yields NaNJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-07eval: fix segmentation when attempting to concat a string with nullJo-Philipp Wich
A typo in the code lead to a null pointer dereference. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-07ast, eval, lexer: keep track of overflows when parsing numbersJo-Philipp Wich
This allows number literals that exceed the range INT64_MIN..INT64_MAX to be truncated to the respective min and max values in a defined manner. It also makes it possible to have the expression `{{ -9223372036854775808 }}` actually result in `-9223372036854775808`. Since negation and number declaration are separate operations, the value would be first truncated to `9223372036854775807` and then negated, making it impossible to write a literal INT64_MIN value without tracking the overflow. Also fix the number parsing logic to not trucate intergers to 32bit. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-07eval: adjust results for arithmetic division by nan, infinityJo-Philipp Wich
Follow ECMAScript logic. Division by zero yields infinity, division by infinity yields zero. Signed-off-by: Jo-Philipp Wich <jo@mein.io>