summaryrefslogtreecommitdiffhomepage
path: root/eval.c
AgeCommit message (Collapse)Author
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>
2020-09-06treewide: refactor internal AST structuresJo-Philipp Wich
- unify operand and value tag structures - use a contiguous array for storing opcodes - use relative offsets for next and children ops - defer function creation to runtime - rework "this" context handling by storing context pointer in scope tags Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-02treewide: rename double and null value constructor functionsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-02eval, lib: add loadable module infrastructureJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-02eval: properly increase/decrease refcounts when dealing with scopesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-02eval: traverse prototype chain when resolving object propertiesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-02eval, lib: use tagged JSON objects to register C functionsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-02eval: treat exception values as falseJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-02ast, eval: add tagged object handlingJo-Philipp Wich
In order to implement prototype chains later on, introduce a tagged object value type and use it when processing object declarations. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-25treewide: add proper null value handlingJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-25eval.c: fix segfault when attempting to resolve invalid lhsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-25treewide: introduce this keywordJo-Philipp Wich
Support a new keyword `this` which allows functions to access the context they're called upon. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-25eval.c: properly propagate exceptions when resolving referencesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-25treewide: add dedicated T_CFUNC typeJo-Philipp Wich
Tag C function values with a dedicated T_CFUNC type to clearly differentiate them from runtime declared functions. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-25lexer.c, eval.c: move T_EXCEPTION definition to lexer headerJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-24treewide: remove unneeded libubox dependencyJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-24eval.c, lib.c: allow invoking functions with existing scopeJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-23eval.c: fix segfault when invoking a tagged non-function lvalueJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-08-21Initial commitJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>