Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lib/internal/ffi/fast-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ function getStringConversionPointer(state, value, index) {
const size = value.length * 3 + 1;
const buffers = state.buffers[state.depth - 1];
let entry = buffers[index];
if (entry !== undefined && entry.string === value) {
return entry.pointer;
}
if (StringPrototypeIncludes(value, '\0')) {
throwFFIArgError(`Argument ${index} must not contain null bytes`);
}
Expand All @@ -146,15 +143,13 @@ function getStringConversionPointer(state, value, index) {
__proto__: null,
buffer,
pointer: getRawPointer(buffer),
string: undefined,
};
buffers[index] = entry;
}

const buffer = entry.buffer;
const written = buffer.write(value, 0, size - 1, 'utf8');
buffer[written] = 0;
entry.string = value;
return entry.pointer;
}

Expand Down
4 changes: 4 additions & 0 deletions test/ffi/fixture_library/ffi_test_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ FFI_EXPORT uint8_t string_equals_hello(const char* str) {
return str && strcmp(str, "hello") == 0;
}

FFI_EXPORT char* overwrite_string(char* str, int32_t value, uint64_t length) {
return memset(str, value, (size_t)length);
}

FFI_EXPORT char* string_concat(const char* a, const char* b) {
if (!a || !b) {
// NOLINTNEXTLINE (readability/null_usage)
Expand Down
18 changes: 18 additions & 0 deletions test/ffi/test-ffi-fast-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ test('fast FFI string buffers survive reentrant callbacks', {
}
});

test('fast FFI refreshes cached temporary string buffers', () => {
const lib = new ffi.DynamicLibrary(libraryPath);
const overwriteString = lib.getFunction('overwrite_string', {
arguments: ['string', 'i32', 'u64'],
return: 'pointer',
});

try {
const mutated = overwriteString('hello', 0x79, 1n);
assert.strictEqual(ffi.toString(mutated), 'yello');

const refreshed = overwriteString('hello', 0x79, 0n);
assert.strictEqual(ffi.toString(refreshed), 'hello');
} finally {
lib.close();
}
});

test('optimized buffer signatures preserve pointer-like conversions', () => {
const lib = new ffi.DynamicLibrary(libraryPath);
const asBuffer = lib.getFunction('pointer_to_usize', {
Expand Down
Loading