summaryrefslogtreecommitdiffhomepage
path: root/types.c
AgeCommit message (Collapse)Author
2024-02-21vm: rework object iterationJo-Philipp Wich
Ensure that deleting object keys during iteration is safe by keeping a global chain of per-object iterators which are advanced to the next key when the entry that is about to be iterated is deleted. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-10-09types: ensure double serializatiion with decimal placesJo-Philipp Wich
The `%g` printf format used for serializing double values into strings will not include any decimal place if the value happens to be integral, leading to an unwanted double to integer conversion when serializing and subsequently deserializing an integral double value as JSON. Solve this issue by checking the serialized string result for a decimal point or exponential notation and appending `.0` if neither is found. Ref: #173 Suggested-by: Felix Fietkau <nbd@nbd.name> Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-08-22types: improve comparison reliability of binary stringsJo-Philipp Wich
The existing `ucv_compare()` implementation utilized `strcmp()` to compare two ucode string values, which may lead to incorrect results for strings containing null bytes as the comparison prematurely aborts when encountering the first null. Rework the string comparison logic to use `memcmp()` for comparing both ucv strings with each other in order to ensure that expressions such as `"" == "\u0000"` lead to the expected `false` result. Ref: https://github.com/openwrt/luci/issues/6530 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-08-09treewide: consolidate platform specific code in platform.cJo-Philipp Wich
Get rid of most __APPLE__ guards by introducing a central platform.c unit providing drop-in replacements for missing APIs. Also move system signal definitions into the new platform file to be able to share them with the upcoming debug library. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-07-27types: treat signal handler array as GC rootJo-Philipp Wich
In order to prevent a premature release of the managed ucode signal handler callbacks, ensure to treat the containing array as GC root to mark the function values as reachable. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-07-25types: don't rely on implicit type conversion in ucv_compare()Jo-Philipp Wich
- Don't implicitly convert intptr_t difference to int8_t but perform explicit comparisons - Don't implicitly convert strcmp() int result to int8_t Fixes: #165 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-05-30types: implement ucv_object_sort()Jo-Philipp Wich
Introduce a new function `ucv_object_sort()` which works similar to `ucv_array_sort()` and allows reordering the keys of an object. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-05-27vm: clear vm->alloc_refs in uc_gc_commonFelix Fietkau
This avoids unnecessary gc calls when configuring a gc interval while also explicitly calling ucv_gc from C code embedding a vm at convenient points in time. Signed-off-by: Felix Fietkau <nbd@nbd.name>
2023-01-23types: fix array unshift operations and add test coverageJo-Philipp Wich
- Fix `ucv_array_unshift()` improperly rejecting operation on empty arrays - Fix `uc_unshift()` improperly reversing maintaining argument order - Add missing test coverage for `push()`, `pop()`, `unshift()` and `shift()` array operations. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-01-06include: add uc_fn_thisval()Felix Fietkau
Can be used to get rid of a layer of pointer indirection in resource type handlers. Signed-off-by: Felix Fietkau <nbd@nbd.name>
2022-12-02types: adjust double printing formatJo-Philipp Wich
Change the printf format for formatting doubles from `%g` to `%.14g`. This matches the behaviour of Lua 5.1.5 with LNUM used on OpenWrt and ensures that expressions such as `print(31764740.0 / 100)` yield the expected `317647.4` result and not a truncated (rounded down) value of `317647`. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-10-04lexer: improve regex literal handlingJo-Philipp Wich
- Do not treat slashes within bracket expressions as delimitters - Do not escape slashes when stringifying regex sources - Allow all escape sequence types in regex literals Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-09-30vm: maintain export symbol tables per programJo-Philipp Wich
Instead of having one global export table per VM instance maintain one table per program instance. This is required to avoid clobbering the export list in case `import` using code is loaded at runtime through `require()`, `loadfile()` etc. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-08-24types: gracefully handle unpatched upvalues in ucv_free()Jo-Philipp Wich
When a module function with unpatched upvalues is freed, ucv_free() might access a NULL pointer through ucv_put_value(), so make sure to skip those unset upvalue slots when freeing a closure. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-08-12vm: support automatic periodic GC runsJo-Philipp Wich
Introduce two new VM api functions uc_vm_gc_start() and uc_vm_gc_stop() which allow starting and stopping automatic periodic garbage collection of cyclic objects in the VM context. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-08-12types: treat vm->exports as GC rootsJo-Philipp Wich
We must not free objects being in the module export registry. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-07-30vm, cli: move search path into global configuration structureJo-Philipp Wich
The upcoming compile-time module support will require the configured extension search path in the compiler as well, so move it to the already shared uc_parse_config_t structure and add the appropriate utility functions to initialize, append and free the search path vector. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-07-30program: add infrastructure to handle multiple sources per programJo-Philipp Wich
The upcoming module support requires maintaining multiple source objects within the same program, so add the necessary infrastructure for it. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-07-30source: add tracking of exported symbolsJo-Philipp Wich
Extend abstract source objects to maintain a list of exported symbols and add functions to append and lookup exported names. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-07-30types: resolve upvalue references on stringificationJo-Philipp Wich
When stringifying upvalue references, resolve their target value and convert it to a string. Only yield the abstract string representation if the target value cannot be resolved. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-07-28types: add ability to mark array and object values as constantJo-Philipp Wich
The upcoming module import support requires constant object values to implement module wildcard import. Reuse the existing u64 bit in ucv heads to mark array or object values as constant and add corresponding `ucv_is_constant()` and `ucv_set_constant()` helpers. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-04-07treewide: move json-c compat shims into internal header fileJo-Philipp Wich
Do not expose the json-c compat functions in ucode's public headers to avoid clashes when building on systems with modern json-c. Also remove some explicit json-c/json-c.h includes in places where it is not needed. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-31types: fix escape sequence encoding of high byte values in JSON stringsJo-Philipp Wich
Treat the char value as unsigned when testing its value to yield consistent results on both platforms with signed chars and those with unsigned chars by default (e.g. ARM ones). This also avoids encoding byte values > 127 as \uXXXX escape sequences, potentially breaking the strng contents. Fixes: #62 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-03types: treat resource type prototypes as GC rootsJo-Philipp Wich
Mark reachable resource type prototype objects during incremental GC steps in order to avoid freeing them prematurely. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-02-07source: convert source objects into proper uc_value_t typeJo-Philipp Wich
Instead of implementing a custom limited refcount logic, turn uc_source_t instances into proper uc_value_t objects. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-02-07treewide: rework function memory modelJo-Philipp Wich
- Instead of treating individual program functions as managed ucode types, demote uc_function_t values to pointers into a uc_program_t entity - Promote uc_program_t to a managed type - Let uc_closure_t claim references to the owning program of the enclosed uc_function_t - Redefine public APIs uc_compile() and uc_vm_execute() APIs to return and expect an uc_program_t object respectively - Remove vallist indirection for function loading and let the compiler emit the function id directly when producing function construction code Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-01-26vm: fix NaN strict equality testsJo-Philipp Wich
A performance shortcut in `ucv_is_equal()` incorrectly led to `NaN === NaN` being true. Fix the issue by only comparing pointers when the involved types are not doubles. Due to fixing `NaN !== NaN`, the `uniq()` function now requires a special case to treat multiple NaNs equal for the sake of generating an array of unique values. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-01-26vm: fix `null` loose equality/inequality checksJo-Philipp Wich
The current implementation incorrectly yielded `true` for `0 == null` but only `null` must be equal to `null`. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-01-18source: refactor source file handlingJo-Philipp Wich
- Move source object pointer into program entity which is referenced by each function - Move lineinfo related routines into source.c and use them from lexer.c since lineinfo encoding does not belong into the lexical analyzer. - Implement initial infrastructure for detecting source file type, this is required later to differentiate between plaintext and precompiled bytecode files Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-01-18types: add initial infrastructure for function serializationJo-Philipp Wich
- Introduce a new "program" entity which holds the list of functions created during compilation - Instead of storing pointers to the in-memory function representation in the constant list, store the index of the function within the program's function list - When loading functions from the constant list, retrieve the function by index from the program entity Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-01-04treewide: rework numeric value handlingJo-Philipp Wich
- Parse integer literals as unsigned numeric values in order to be able to represent the entire unsigned 64bit value range - Stop parsing minus-prefixed integer literals as negative numbers but treat them as separate minus operator followed by a positive integer instead - Only store unsigned numeric constants in bytecode - Rework numeric comparison logic to be able to handle full 64bit unsigned integers - If possible, yield unsigned 64 bit results for additions - Simplify numeric value conversion API - Compile code with -fwrapv for defined signed overflow semantics Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-12-08vm: introduce value registryJo-Philipp Wich
Introduce a new, lazily allocated value registry which can be used by C code to store values which should not be garbage collected. The registry is a plain ucode object internally and treated as GC root but not exposed to ucode script code, this allows it to retain references to values which are otherwise completely unreachable from ucode scripts. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-12-07treewide: fix "resource" misspellingsJo-Philipp Wich
Fix various misspelling of "resource". This commit changes the exported libucode ABI. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-12-07treewide: fix upvalue reference type nameJo-Philipp Wich
No functional changes. Fixes: ff52440 ("treewide: consolidate typedef naming") Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-12-06types: consider resource prototypes when marking reachable objectsJo-Philipp Wich
The prevents garbage collecting properties which are stored in resource value prototype objects. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-11-01treewide: fix typo in exported function names and typesJo-Philipp Wich
Fix instances of misspelled "resource". This commit breaks the exported libucode ABI. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-09-21types: fix invalid memory access on setting non-contiguous array indexesJo-Philipp Wich
When setting an array index which is beyond the end of the last currently preallocated chunk and not evenly divisible by the chunk size, the array entries list was not properly reallocated, resulting in invalid memory writes. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-09-17types: fix formatting escape sequences for 8 bit charsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-27types: mark further GC rootsJo-Philipp Wich
Mark reachable native function and ucode function objects during incremental GC steps in order to avoid freeing them prematurely. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-27types: fix comparison of differently signed integersJo-Philipp Wich
Fixes: c79ff39 ("types: handle conversion errors when dealing with negative error indexes") Fixes: 3315b1f ("types: allow negative array indexes") Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-27types: handle conversion errors when dealing with negative error indexesJo-Philipp Wich
Also apply the same logic to array set operations. Fixes: 3315b1f ("types: allow negative array indexes") Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-27types: allow negative array indexesJo-Philipp Wich
Support array[-1], array[-2] etc. to retrieve elements relative to the end of the array values. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: harmonize function namingJo-Philipp Wich
- Ensure that most functions follow the subject_verb naming schema - Move type related function from value.c to types.c - Rename value.c to vallist.c Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11types, vm: adjust GC apiJo-Philipp Wich
Provide separate public ucv_gc() and ucv_freeall() functions to perform an incremental and complete GC run respectively. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: move header files into dedicated directoryJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11vm: fix invalid memory access on GC'ing uninitialized VM contextJo-Philipp Wich
When attempting to invoke uc_vm_free() or uc_gc() on a uc_vm_t struct that has not been initialized by uc_vm_init(), the circular double linked value list is not set up, causing the GC to read invalid memory locations when attempting to traverse the object list. Back out early when the list heads are not properly set up in order to prevent this issue. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: consolidate typedef namingJo-Philipp Wich
Ensure that all custom typedef and vector declaration type names end with a "_t" suffix. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: eliminate dead code and unused functionsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: replace a number of unnecessary type castsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: move ressource type registry into vm instanceJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>