Commit Graph
716 Commits
Author SHA1 Message Date
Calvin Rose d2560d538b Add bytecode slot compaction pass.
Codegen may allocate and then never use slots. Free up those slots
and rewrite bytecode to be smaller. The resulting bytecode will be
slightly faster, result in better inlining, and be somewhat normalized
for further optimizations.
2026-09-24 06:19:53 -05:00
Calvin Rose b0acda4fa5 Add bytecode inlining at optimization >= 2.
Currently very simple hueristic, and not supported
on many functions that instatiate closures or load/set upvalues.

We also now, more than ever, need a slot-compaction routine to reduce
the number of slots used by functions.
2026-09-24 06:19:38 -05:00
Calvin Rose 6e79ac9f62 Expose optimization level
This exposes the optimization level for a few reasons.

First, Local value numbering exposed some bad codegen. Playing with
optimiaztion level exposes this more easily.

When compiling dead code in a known unused scope, we
would generate code that tried to load invalid slots.

Make sure that unused code will still pass `janet_verify()` as
invalid code could trigger bad behavior in our optimizer or garbage
collector.

Since the code was proven dead, this would not trigger any runtime
behavior issues though.

Second, even trivial optimizations tend to break debug information. For
example, constant propogation will eliminated symbol mappings which can
be useful for debugging. By default, don't eliminate any symbol map
debug info.
2026-09-19 11:22:33 -05:00
Calvin Rose 0432110572 Improve the constant propogation.
However, we will likely need to gate some of these optimizations
behind a switch as they are beginning to destruct debug info. For
example, local value number maybe should be off by default or at the
very least turned off for named loads.
2026-09-19 11:22:33 -05:00
Calvin Rose 02f0718460 Expose bytecode optimization in the assembler.
Also fix value numbering missing some cases due to bad encoding
routines.
2026-09-19 11:22:32 -05:00
Pietro Cerutti d8be849671 Don't hardcode 127.0.0.1 in tests (#1849)
How about we resolve through the special host name "localhost." instead of assuming that the loopback interface is bound to 127.0.0.1 (which might not be the case, especially in FreeBSD jails)?

See RFC6761 for how "localhost." is treated, especially:

Name resolution APIs and libraries SHOULD recognize localhost
names as special and SHOULD always return the IP loopback address
for address queries and negative responses for all other query
types.  Name resolution APIs SHOULD NOT send queries for
localhost names to their configured caching DNS server(s).
2026-09-13 07:53:42 -05:00
Calvin Rose c8c8afedd2 Reduce spurious error messages in test suite.
Prevent the possibility of closing the server socket
before calling net/accept-loop in net/server by adding
a call to (ev/sleep 0).
2026-09-12 14:11:05 -05:00
Pietro Cerutti 0bfcc9fc97 Fix a couple of imprecisions in the file/ docs (#1848)
* Fix a couple of imprecisions in the file/ docs

The `+` flag to `(file/open)` was incorrectly documented as being for
appending. Instead, this allows to read to files opened with `:w` or
`:a`, and to write to files opened with `:r`. This is the same as the
`+` flag in `fopen(3)`.

The `(file/seek)` function was documented as having both `whence` and
`n` as optional arguments. It also didn't say what the default for `n`
was. Anyway, this doesn't make sense, as a default of `:cur` and `0`
would effectively make `(file/seek f)` a no-op. I also amended a test so
both `(file/seek f :set 0)` and `(file/seek f :set)` are covered and
checked to move to the beginning of the file.

* Remove useless check for argc >= 2

We already determined it is between 2 and 3 with `janet_arity(argc, 2, 3)`.

* Dump args in assertion

* Unroll loop in test

* Differentiate failure messages
2026-09-10 17:12:54 -05:00
Calvin Rose ee5d2cd99c Address #1838
Add testcase for os/shell to test/suite-ev.janet

While we may not want to completely deprecate os/shell, we should at
least try to redirect users to better and safer interfaces like
os/execute.
2026-08-30 10:49:05 -05:00
Calvin Rose 6e04aaaa59 Add test-case for os/shell.
At the very least, add test case for valgrind/sanitizer targets.
2026-08-30 10:41:23 -05:00
Yohanan 710bfd0b2b add tests for nested declarations (#1837) 2026-08-29 18:58:24 -05:00
Calvin Rose cea6dd2261 Handle degnerate range case gracefully. 2026-08-23 13:25:54 -05:00
Calvin Rose d177131c84 Handle empty tuple as lhs in def/var without fatal error.
While an odd construct, this should not cause an abort in the
interpeter. Instead, we should just create no bindings and compile as
normal.
2026-08-23 13:14:47 -05:00
Calvin Rose 5fb06039f3 Correct flags in fiber.h and add comments!
Flag collisions was causing some unexpected behavior in some situations.
Removing the source of collisions resolve odd cancelation behavior in
the ev/ module where multiple calls to ev/cancel on the same fiber would
mess with the internal fiber flags and cause bad behavior and the wrong
error message return (but still otherwise behave correctly, so unless
you were parsing the returned error messages, things likely worked).

Also leave comments and test to avoid regressions.
2026-08-18 16:03:46 -05:00
c6f44b9c11 Add tests for partition (#1814)
Co-authored-by: sogaiu <983021772@users.noreply.github.com>
Co-authored-by: Calvin Rose <calsrose@gmail.com>
2026-08-18 08:16:20 -05:00
f590dac4f9 Add tests for filter (#1812)
Co-authored-by: sogaiu <983021772@users.noreply.github.com>
Co-authored-by: Calvin Rose <calsrose@gmail.com>
2026-08-18 08:15:46 -05:00
sogaiuandsogaiu eb1a7e2b20 Add tests for interpose (#1813)
Co-authored-by: sogaiu <983021772@users.noreply.github.com>
2026-08-18 08:14:47 -05:00
Calvin Rose 65745d0237 Fix some fuzzing bugs and add asserts.
Passing in the wrong number of keyword arguments could be problematic
and cause compialtion issues. Handle that more gracefully and at least
be clean in valgrind.
2026-08-17 11:05:18 -05:00
sogaiuandsogaiu c6ca76044d Add tests for find (#1808)
Co-authored-by: sogaiu <983021772@users.noreply.github.com>
2026-08-17 07:55:40 -05:00
sogaiuandsogaiu 82b2bdc8e2 Add tests for find-index (#1807)
Co-authored-by: sogaiu <983021772@users.noreply.github.com>
2026-08-16 20:49:01 -05:00
0888a6dfb0 Add tests for partition-by (#1795)
Co-authored-by: sogaiu <983021772@users.noreply.github.com>
Co-authored-by: Calvin Rose <calsrose@gmail.com>
2026-08-15 12:05:01 -05:00
ffb6648742 Add tests for update (#1796)
Co-authored-by: sogaiu <983021772@users.noreply.github.com>
Co-authored-by: Calvin Rose <calsrose@gmail.com>
2026-08-15 12:04:18 -05:00
Calvin Rose 27000deed5 Address #1790 - limit number of automatic parameters.
This also improves the error behavior of malformed automatic parameters
so that the error conditions are better defined. Previously, large
indices like 88888888888888 would actually simply be converted to
symbols and fail later. This change puts strict limitations on this
conversion process - If a symbol is of the form "$<str>" and
(scan-number str) returns non-nil, the result must be a valid index. In
this way, symbols like `$1.1` will result in an immediate macro expansion error
rather than (usually) and unknown binding error.
2026-08-15 11:35:15 -05:00
sogaiuandsogaiu e5e13489c2 Add tests for distinct (#1801)
Co-authored-by: sogaiu <983021772@users.noreply.github.com>
2026-08-15 08:38:43 -05:00
sogaiuandsogaiu 331e8544bc Simplify frequencies implementation (#1793)
* Add tests for frequencies

* Simplify frequencies implementation

---------

Co-authored-by: sogaiu <983021772@users.noreply.github.com>
2026-08-08 09:30:20 -05:00
Calvin Rose 8dbe828504 Add casts to u32 before left shifts to address ubsan edge cases.
Common idiom that wasn't always applied, especially in emit.c and
cfuns.c
2026-06-27 14:26:57 -05:00
Arthur Chan 0e0cbc3ab0 OSS-Fuzz: Add fuzzer targets compile process (#1767)
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
2026-06-25 21:13:02 -05:00
Calvin Rose ffb83301c5 Handle bad ev/thread construction better.
We were incorrectly handling bad arguments to create a new thread
resulting in corruption.
2026-06-21 11:46:57 -05:00
Arthur Chan f2f77a4bd5 OSS-Fuzz: Add fuzzer targets dobytes function (#1765)
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
2026-06-18 18:53:11 -05:00
Calvin Rose 0d3635f9f4 Format. 2026-06-10 17:22:22 -05:00
Arthur Chan 8b883d1304 OSS-Fuzz: Add new fuzzer targets unmarshal (#1762)
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
2026-06-10 17:16:37 -05:00
Calvin Rose c6b802b082 Add #1737
Add some test cases and asserts for new pretty printing - we were not
properly handing the case when print formatter was not at the start of
the buffer.

A simple fix is to add a lookback_barrier, which is an index into the
buffer that backtracking print formatter can't go past. This ensures
that, whatever newlines are replace with spaces, we don't mess with any
explicit newlines that should be printed.

Also some tweaks to logic to try an make invariants more obvious and
simplify looping and indexing.
2026-04-03 08:38:12 -05:00
McSinyx 0c512ab128 Increase pretty printing line density (#1733)
* Increase pretty printing line density

* Allow specifying width for multiline pretty format
2026-04-01 18:55:22 -05:00
Calvin Rose 29474b915d Begin work on adding kqueue functionality to filewatch.
kqueue-based filewatch is more limited than inotify, but is still
useful in many cases. Notably absent is any ability to monitor
directories automatically without wasting a lot of file descriptors.
2026-03-25 22:16:30 -05:00
Calvin Rose 6c2f08ef49 Add unit tests for variable shadowing. 2026-03-15 13:34:12 -05:00
Calvin Rose 980999c97b Make test suite not shadow any variables. 2026-03-15 13:14:10 -05:00
Calvin Rose c63c6740d9 First pass at linting binding shadowing.
Prevent redefining bindings by accident. There are
a few cases where we want to allow this, such as the `default` macro, so
we allow a keyword :shadow to be included in the `def` expression to
turn off this lint.

TODO:
* Clean up test suite to remove binding shadowing
* Make sure that we don't get lints with *redef* turned on
* Add positive and negative tests for lint messages.
* Add location of shadowed binding in message
2026-03-15 11:24:07 -05:00
McSinyx b96350f132 Align items in multiline pretty format (%m and %p) (#1721)
Indentation levels are not sufficiently clear for deeply nested
data structures.  Instead, align items in a collection
with its opening bracket, also known as hanging indentation.

To avoid guessing the length of a "short" collection to print it
on one line, items are now always printed on separate lines.
2026-03-05 19:34:05 -06:00
Calvin Rose 7fc12ff167 Use ConnectEx for non-blocking connect on windows when available.
Still fallback to blocking connect with WSAConnect when ConnectEx is not
available or applicable, but ConnectEx is preferred and recommended by
Microsoft.

Also make some changes to our use of OVERLAPPED in various places in the
ev code, replacing all uses with JanetOverlapped. This also let's us
avoid reusing internal fields for OVERLAPPED which may or may not be
used in various places.
2026-03-02 19:39:21 -06:00
Calvin Rose 32d75c9e49 Dup io file descriptors when marshalling closable files.
For unclosable files, no need to dup, but for closable files we can get
a resource leak. Valgrind and similar tools won't catch this but IO will
unexpectedly start going to the wrong descriptor if a file was
transferred to a new thread, closed, and then a new file was created.
2026-03-01 10:39:50 -06:00
Calvin Rose 5fec2aa9df Move some files around code more defensively for mitigation. 2026-02-27 18:19:40 -06:00
BrettandBrett Adams 54fbd7607f Fix GC collecting active fiber during nested janet_continue (#1720)
janet_collect() marks janet_vm.root_fiber but not janet_vm.fiber.
When janet_pcall (or janet_continue) is called from a C function,
the inner fiber becomes janet_vm.fiber while root_fiber still points
to the outer fiber. If GC triggers inside the inner fiber's execution,
the inner fiber is not in any GC root set and can be collected —
including its stack memory — while actively running.

This also affects deeply nested cases: F1 -> C func -> janet_pcall ->
F2 -> C func -> janet_pcall -> F3, where F2 is saved only in a C
stack local (tstate.vm_fiber), invisible to GC.

Fix: in janet_continue_no_check, root the fiber with janet_gcroot
when this is a nested call (root_fiber already set). Each nesting
level roots its own fiber, handling arbitrary depth. Top-level calls
(event loop, REPL) skip the root/unroot entirely since root_fiber
is NULL.

Add test/test-gc-pcall.c: standalone C test covering both single
and deep nesting cases.

Co-authored-by: Brett Adams <brett@bletia-9.local>
2026-02-27 18:13:46 -06:00
Calvin Rose b2c9fc123c Generate JOP_PUT_INDEX in the compiler when possible. 2026-02-17 09:07:01 -06:00
Calvin Rose fa0c039cd3 Add regression test for issue #1714 2026-02-17 07:59:08 -06:00
Calvin Rose c7c3821aa6 Remove extra output from peg test. 2026-02-06 00:23:45 -06:00
Calvin Rose d2685594f9 VERBOSE=1 caused tests to fail as we did more asserts inside the assert.
Thenc checked stderr for messages. Since the helper makes assert write
to stderr, this caused extra cruft in our test output.
2026-02-06 00:17:11 -06:00
Calvin Rose ca5c617fba More tweaks to peg suite for arm32 failures. 2026-02-06 00:04:18 -06:00
Calvin Rose 16b449a137 Print "actual" output on verbose mode for suite-peg 2026-02-05 23:52:28 -06:00
Calvin Rose 2e8dd90a51 Line endings in tests. 2026-02-05 23:38:55 -06:00
Calvin Rose 0194115412 Auto-format janet. 2026-02-05 00:33:52 -06:00