diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f42a156..da897093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog All notable changes to this project will be documented in this file. +## Unreleased - ??? +- Make `ffi/write` append to a buffer instead of insert at 0 by default. + ## 1.38.0 - 2025-03-18 - Add `bundle/replace` - Add CLI flags for the `bundle/` module to install and manage bundles. diff --git a/src/core/ffi.c b/src/core/ffi.c index 81483b6a..f14ff2f7 100644 --- a/src/core/ffi.c +++ b/src/core/ffi.c @@ -1686,7 +1686,7 @@ JANET_CORE_FN(cfun_ffi_buffer_write, JanetFFIType type = decode_ffi_type(argv[0]); uint32_t el_size = (uint32_t) type_size(type); JanetBuffer *buffer = janet_optbuffer(argv, argc, 2, el_size); - int32_t index = janet_optnat(argv, argc, 3, 0); + int32_t index = janet_optnat(argv, argc, 3, buffer->count); int32_t old_count = buffer->count; if (index > old_count) janet_panic("index out of bounds"); buffer->count = index; diff --git a/test/suite-ffi.janet b/test/suite-ffi.janet index 3d0a4d7e..c6237736 100644 --- a/test/suite-ffi.janet +++ b/test/suite-ffi.janet @@ -55,4 +55,11 @@ (compwhen has-ffi (assert-error "bad struct issue #1512" (ffi/struct :void))) +(compwhen has-ffi + (def buf @"") + (ffi/write :u8 10 buf) + (assert (= 1 (length buf))) + (ffi/write :u8 10 buf) + (assert (= 2 (length buf)))) + (end-suite)