aboutsummaryrefslogtreecommitdiff
path: root/src/napi/napi.zig
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2023-03-08 08:29:29 +0200
committerGravatar GitHub <noreply@github.com> 2023-03-07 22:29:29 -0800
commit28346e4a390036270e393192550175a01d0e7f41 (patch)
tree123e509a186d58a99266fdad81539114a44d687b /src/napi/napi.zig
parent95b59ea0ef637abde76dad4a100cae6c496c1470 (diff)
downloadbun-28346e4a390036270e393192550175a01d0e7f41.tar.gz
bun-28346e4a390036270e393192550175a01d0e7f41.tar.zst
bun-28346e4a390036270e393192550175a01d0e7f41.zip
improve `Buffer` compatibility with Node.js (#2341)
* improve `Buffer` compatibility with Node.js * use `memmove()` allow `encoding` to be `undefined`
Diffstat (limited to 'src/napi/napi.zig')
-rw-r--r--src/napi/napi.zig14
1 files changed, 4 insertions, 10 deletions
diff --git a/src/napi/napi.zig b/src/napi/napi.zig
index b06e25d7e..99d7c82fb 100644
--- a/src/napi/napi.zig
+++ b/src/napi/napi.zig
@@ -384,11 +384,8 @@ pub export fn napi_get_value_string_latin1(env: napi_env, value: napi_value, buf
if (zig_str.is16Bit()) {
const utf16 = zig_str.utf16SliceAligned();
- const wrote = JSC.WebCore.Encoder.writeU16(utf16.ptr, utf16.len, buf, buf_.len, .latin1, false);
- if (wrote < 0) {
- return genericFailure();
- }
- maybeAppendNull(&buf[@intCast(usize, wrote)], bufsize == 0);
+ const wrote = JSC.WebCore.Encoder.writeU16(utf16.ptr, utf16.len, buf, buf_.len, .latin1, false) catch return genericFailure();
+ maybeAppendNull(&buf[wrote], bufsize == 0);
// if zero terminated, report the length of the string without the null
result.* = @intCast(@TypeOf(result.*), wrote);
return .ok;
@@ -448,11 +445,8 @@ pub export fn napi_get_value_string_utf8(env: napi_env, value: napi_value, buf_p
if (zig_str.is16Bit()) {
const utf16 = zig_str.utf16SliceAligned();
- const wrote = JSC.WebCore.Encoder.writeU16(utf16.ptr, utf16.len, buf, buf_.len, .utf8, false);
- if (wrote < 0) {
- return genericFailure();
- }
- buf[@intCast(usize, wrote)] = 0;
+ const wrote = JSC.WebCore.Encoder.writeU16(utf16.ptr, utf16.len, buf, buf_.len, .utf8, false) catch return genericFailure();
+ buf[wrote] = 0;
if (result_ptr) |result| {
result.* = @intCast(@TypeOf(result.*), wrote);
}