| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent eb095a9 commit 9b4f349
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -148,9 +148,6 @@ function getStringConversionPointer(state, value, index) { | |||
| 148 | 148 | const size = value.length * 3 + 1; | |
| 149 | 149 | const buffers = state.buffers[state.depth - 1]; | |
| 150 | 150 | let entry = buffers[index]; | |
| 151 | - if (entry !== undefined && entry.string === value) { | ||
| 152 | - return entry.pointer; | ||
| 153 | - } | ||
| 154 | 151 | if (StringPrototypeIncludes(value, '\0')) { | |
| 155 | 152 | throwFFIArgError(`Argument ${index} must not contain null bytes`); | |
| 156 | 153 | } | |
@@ -160,15 +157,13 @@ function getStringConversionPointer(state, value, index) { | |||
| 160 | 157 | __proto__: null, | |
| 161 | 158 | buffer, | |
| 162 | 159 | pointer: getRawPointer(buffer), | |
| 163 | - string: undefined, | ||
| 164 | 160 | }; | |
| 165 | 161 | buffers[index] = entry; | |
| 166 | 162 | } | |
| 167 | 163 | ||
| 168 | 164 | const buffer = entry.buffer; | |
| 169 | 165 | const written = buffer.write(value, 0, size - 1, 'utf8'); | |
| 170 | 166 | buffer[written] = 0; | |
| 171 | - entry.string = value; | ||
| 172 | 167 | return entry.pointer; | |
| 173 | 168 | } | |
| 174 | 169 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -108,6 +108,11 @@ FFI_EXPORT uint8_t string_equals_hello(const char* str) { | |||
| 108 | 108 | return str && strcmp(str, "hello") == 0; | |
| 109 | 109 | } | |
| 110 | 110 | ||
| 111 | + FFI_EXPORT uint8_t overwrite_string(char* str, int32_t value, uint64_t length) { | ||
| 112 | + memset(str, value, (size_t)length); | ||
| 113 | + return (uint8_t)str[0]; | ||
| 114 | + } | ||
| 115 | + | ||
| 111 | 116 | FFI_EXPORT char* string_concat(const char* a, const char* b) { | |
| 112 | 117 | if (!a || !b) { | |
| 113 | 118 | // NOLINTNEXTLINE (readability/null_usage) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -96,6 +96,24 @@ test('fast FFI string buffers survive reentrant callbacks', { | |||
| 96 | 96 | } | |
| 97 | 97 | }); | |
| 98 | 98 | ||
| 99 | + test('fast FFI refreshes cached temporary string buffers', () => { | ||
| 100 | + const lib = new ffi.DynamicLibrary(libraryPath); | ||
| 101 | + const overwriteString = lib.getFunction('overwrite_string', { | ||
| 102 | + arguments: ['string', 'i32', 'u64'], | ||
| 103 | + return: 'u8', | ||
| 104 | + }); | ||
| 105 | + | ||
| 106 | + try { | ||
| 107 | + const mutated = overwriteString('hello', 0x79, 1n); | ||
| 108 | + assert.strictEqual(mutated, 0x79); | ||
| 109 | + | ||
| 110 | + const refreshed = overwriteString('hello', 0x79, 0n); | ||
| 111 | + assert.strictEqual(refreshed, 0x68); | ||
| 112 | + } finally { | ||
| 113 | + lib.close(); | ||
| 114 | + } | ||
| 115 | + }); | ||
| 116 | + | ||
| 99 | 117 | test('optimized buffer signatures preserve pointer-like conversions', () => { | |
| 100 | 118 | const lib = new ffi.DynamicLibrary(libraryPath); | |
| 101 | 119 | const asPointer = lib.getFunction('pointer_to_usize', { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments