summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib
AgeCommit message (Collapse)Author
2024-06-17fs: add lock() file methodFelix Fietkau
Implements file based locking on a given file handle. Signed-off-by: Felix Fietkau <nbd@nbd.name>
2024-06-17fs: add truncate() file methodFelix Fietkau
Trunates the file referenced by a file handle Signed-off-by: Felix Fietkau <nbd@nbd.name>
2023-07-27main: enable signal dispatching in the standalone cli interpreterJo-Philipp Wich
Enable signal dispatching by default for standalone ucode programs. Also adjust the `gc()` testcase output as the default number of allocations with enabled signal dispatching changes slightly. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-07-12source: fix source offset accountingJo-Philipp Wich
- When skipping the interpreter line, don't count it's newline twice - Fix reporting byte offsets beyond the end of line Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-05-30lib: support object ordering in `uc_sort()`Jo-Philipp Wich
Extend `uc_sort()` to utilize `ucv_object_sort()` in order to support reordering object keys. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
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-09fs: add `isatty()` functionPetr Štetiar
Expose the `isatty(3)` libc function in the fs module to allow checking whether a file descriptor refers to a terminal. Signed-off-by: Petr Štetiar <ynezz@true.cz>
2022-12-02tests: fixup testcasesJo-Philipp Wich
Adjust expected testcase outputs after double format change in the previous commit. Fixes: 4c654df ("types: adjust double printing format") Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-11-29tests: relax sleep() testJo-Philipp Wich
Invoking `sleep(1000)` in the CI container often sleeps slightly longer than exactly 1000ms, causing the test output to mismatch. Relax the test requirement to simply ensure that t2 > t1. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-10-04lib: implement slice() functionJo-Philipp Wich
Implement a new function `slice()` to complement the existing `splice()` function and model it's semantics after the ES6 `Array.slice()` version. Fixes: #106 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-09-05lib: add limit support to split() and replace()Jo-Philipp Wich
Extend the split() and replace() functions to accept an additional optional `limit` argument which limits the amount of split operations / substitutions performed by these functions. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-08-29lib: extend render() to support function valuesJo-Philipp Wich
Extend the `render()` function to accept a function value as first argument, which allows running arbitrary ucode functions and capturing their output. This is especially useful in conjunction with `loadfile()` or `loadstring()` to dynamically compile templates and rendering their output into a string. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-08-29lib: improve getenv() and split() implementationsJo-Philipp Wich
- getenv(): Allow querying the entire environment by omiting variable name - split(): Properly handle null bytes in subject and separator strings Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-08-24lib: introduce three new functions call(), loadstring() and loadfile()Jo-Philipp Wich
Introduce new functions dealing with on-the-fly compilation of code and execution of functions with different global scope. The `loadstring()` and `loadfile()` functions will compile the given ucode source string or ucode file path respectively and return the entry function of the resulting program. An optional dictionary specifying parse options may be given as second argument. Both functions return `null` on invalid arguments and throw an exception in case of compilation errors. The `call()` function allows invoking a given function value with a different `this` context and/or a different global environment. Finally refactor the existing `uc_require_ucode()` implementation to reuse the new `uc_loadfile()` and `uc_call()` implementations and adjust as well as simplify affected testcases. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-08-12lib: implement gc()Jo-Philipp Wich
Introduce a new stdlib function `gc()` which allows controlling the periodic garbage collector from ucode. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-06-01syntax: adjust number literal parsing and string to number conversionJo-Philipp Wich
- Recognize new number literal prefixes `0o` and `0O` for octal as well as `0b` and `0B` for binary number literals - Treat number literals with leading zeros as octal while parsing but as decimal ones on implicit number conversions, means `012` will yield `10` while `+"012"` or `"012" + 0` will yield `12` Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-06-01lib: refactor `uc_int()`Jo-Philipp Wich
For string cases, turn `int()` into a thin `strtoll()` wrapper which attempts to parse the initial portion of the string as a decimal integer literal, optionally preceded by white space and a sign character. Also introduce an optional `base` argument for string cases while we're at it and adjust the existing stdlib test case accordingly. The function now behaves mostly the same as ECMAScript `parseInt(val, 10)` for string cases, means it will recognize `012` as `12` and not `10` and it will accept trailing non-digit characters after the initial portition of the input string. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-05-30lib: rework uc_index() implementationJo-Philipp Wich
- Fix segfault on passing string haystack with non-string needle argument - Perform strict equality tests against array haystacks - Make string searches binary safe - Improve left index string search performance - Improve right index array search performance - Add missing test coverage for index() and rindex() Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-05-19lib: introduce hexenc() and hexdec()Jo-Philipp Wich
Add two new functions to deal with encoding and decoding of hexadecimal digit strings: - hexenc() - convert the given input value into a lower case hex digit string, implicitely converting the input argument to a string value if needed - hexdec() - decode the given input hex digit string into a byte string, skipping whitespace or optionally specified characters in the input Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-04-07lib: let `json()` accept input objects implementing `read()` methodJo-Philipp Wich
Extend the `uc_json()` implementation to accept readable objects in addition to plain input strings. This allows parsing JSON input directly from open file handles, sockets or other kinds of producer objects without the need to store the entire JSON source string intermediately in memory. 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-22lib: add date and time related functionsJo-Philipp Wich
Add five new functions to deal with date calculation and timing: - localtime(), gmtime() - return a broken down calendar date and time specification from the given epoch (or now, if absent) in local and UTC time respectively - timelocal(), timegm() - the inverse operation for the former functions, taking a date and time specification (interpreted as local or UTC time respectively) and turning it into an epoch value - clock() - return the second and nanosecond values of the system clock, useful for time/performance measurements Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-21Merge pull request #53 from jow-/string-format-argpos-supportJo-Philipp Wich
lib: add argument position support (`%m$`) to `sprintf()` and `printf()`
2022-03-20lib: add argument position support (`%m$`) to `sprintf()` and `printf()`Jo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-15tests: 16_sort: fix logic flaw exposed on OS XJo-Philipp Wich
A typo in the custom order function of the test case caused the test case to yield differently sorted results on OS X, triggered by differences in the libc's `qsort()` implementation. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-14lib: adjust require(), render() and include() raw mode semanticsJo-Philipp Wich
- Let `require()` always evaluate the executed code in raw mode - Let `render()` always evaluate the executed code in template mode - Let `include()` inherit the raw mode semantics of the calling scope Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-02-15tests: fix proto() testcaseJo-Philipp Wich
Fixes: 4ce69a8 ("fs: implement access(), mkstemp(), file.flush() and proc.flush()") Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-02-11lib: change `ord()` to always return single byte valueJo-Philipp Wich
The most common usecase is extracting the value of a single byte at a specific offset, e.g. to scan a string char-by-char to construct a hash. Furthermore, constructing an array which contains the results of multiple `ord()` invocations is trivial while efficiently extracting a single byte value without the overhead of an intermediate array is not. Due to that, change `ord()` to always return a single integer byte value at the offset specified as second argument or at offset 0 in case no argument was supplied. That means that `ord("Abc", 0, 1, 2)` will now return `65` instead of the former `[ 65, 98, 99 ]` result. Code relying on the former behaviour should either perform multiple calls to `ord()`, passing different offsets each time or switch to the `struct` module which allows efficient unpacking of string data. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-02-03tests: add functional tests for builtin functionsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>