Skip to content

Commit 5bcccec

Browse files
authored
Merge pull request #1103 from lightpanda-io/text_decode_view
Text decode view
2 parents 74dc7b2 + 20ae9c3 commit 5bcccec

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

.github/actions/install/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ inputs:
1717
zig-v8:
1818
description: 'zig v8 version to install'
1919
required: false
20-
default: 'v0.1.30'
20+
default: 'v0.1.33'
2121
v8:
2222
description: 'v8 version to install'
2323
required: false

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ARG MINISIG=0.12
44
ARG ZIG=0.15.1
55
ARG ZIG_MINISIG=RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U
66
ARG V8=14.0.365.4
7-
ARG ZIG_V8=v0.1.30
7+
ARG ZIG_V8=v0.1.33
88
ARG TARGETPLATFORM
99

1010
RUN apt-get update -yq && \

build.zig.zon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
.fingerprint = 0xda130f3af836cea0,
66
.dependencies = .{
77
.v8 = .{
8-
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/c25223900587005c9cf13f25e56a7e0be26a533c.tar.gz",
9-
.hash = "v8-0.0.0-xddH6x_DAwBh0gSbFFv1fyiExhExXKzZpbmj5sFH4MRY",
8+
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/7a1beb016efcb2bd66c0b46b5fc6bcd04fa72db8.tar.gz",
9+
.hash = "v8-0.0.0-xddH6_PFAwAqwLMWbF7S0rAZzRbN8zmf2GhLH_ThdMSR",
1010
},
11-
// .v8 = .{ .path = "../zig-v8-fork" }
11+
//.v8 = .{ .path = "../zig-v8-fork" }
1212
},
1313
}

src/browser/encoding/TextDecoder.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const std = @import("std");
2020
const log = @import("../../log.zig");
2121

22+
const Env = @import("../env.zig").Env;
2223
const Page = @import("../page.zig").Page;
2324

2425
// https://encoding.spec.whatwg.org/#interface-textdecoder
@@ -69,8 +70,8 @@ pub fn get_fatal(self: *const TextDecoder) bool {
6970
const DecodeOptions = struct {
7071
stream: bool = false,
7172
};
72-
pub fn _decode(self: *TextDecoder, input_: ?[]const u8, opts_: ?DecodeOptions, page: *Page) ![]const u8 {
73-
var str = input_ orelse return "";
73+
pub fn _decode(self: *TextDecoder, str_: ?[]const u8, opts_: ?DecodeOptions, page: *Page) ![]const u8 {
74+
var str = str_ orelse return "";
7475
const opts: DecodeOptions = opts_ orelse .{};
7576

7677
if (self.stream.items.len > 0) {

src/runtime/js.zig

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,80 +1251,88 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
12511251
fn jsValueToTypedArray(_: *JsContext, comptime T: type, js_value: v8.Value) !?[]T {
12521252
var force_u8 = false;
12531253
var array_buffer: ?v8.ArrayBuffer = null;
1254+
var byte_len: usize = undefined;
1255+
var byte_offset: usize = undefined;
1256+
12541257
if (js_value.isTypedArray()) {
12551258
const buffer_view = js_value.castTo(v8.ArrayBufferView);
1259+
byte_len = buffer_view.getByteLength();
1260+
byte_offset = buffer_view.getByteOffset();
12561261
array_buffer = buffer_view.getBuffer();
12571262
} else if (js_value.isArrayBufferView()) {
12581263
force_u8 = true;
12591264
const buffer_view = js_value.castTo(v8.ArrayBufferView);
1265+
byte_len = buffer_view.getByteLength();
1266+
byte_offset = buffer_view.getByteOffset();
12601267
array_buffer = buffer_view.getBuffer();
12611268
} else if (js_value.isArrayBuffer()) {
12621269
force_u8 = true;
12631270
array_buffer = js_value.castTo(v8.ArrayBuffer);
1271+
byte_len = array_buffer.?.getByteLength();
1272+
byte_offset = 0;
12641273
}
12651274

12661275
const buffer = array_buffer orelse return null;
12671276

12681277
const backing_store = v8.BackingStore.sharedPtrGet(&buffer.getBackingStore());
12691278
const data = backing_store.getData();
1270-
const byte_len = backing_store.getByteLength();
12711279

12721280
switch (T) {
12731281
u8 => {
12741282
// need this sentinel check to keep the compiler happy
12751283
if (force_u8 or js_value.isUint8Array() or js_value.isUint8ClampedArray()) {
12761284
if (byte_len == 0) return &[_]u8{};
12771285
const arr_ptr = @as([*]u8, @ptrCast(@alignCast(data)));
1278-
return arr_ptr[0..byte_len];
1286+
return arr_ptr[byte_offset .. byte_offset + byte_len];
12791287
}
12801288
},
12811289
i8 => {
12821290
if (js_value.isInt8Array()) {
12831291
if (byte_len == 0) return &[_]i8{};
12841292
const arr_ptr = @as([*]i8, @ptrCast(@alignCast(data)));
1285-
return arr_ptr[0..byte_len];
1293+
return arr_ptr[byte_offset .. byte_offset + byte_len];
12861294
}
12871295
},
12881296
u16 => {
12891297
if (js_value.isUint16Array()) {
12901298
if (byte_len == 0) return &[_]u16{};
12911299
const arr_ptr = @as([*]u16, @ptrCast(@alignCast(data)));
1292-
return arr_ptr[0 .. byte_len / 2];
1300+
return arr_ptr[byte_offset .. byte_offset + byte_len / 2];
12931301
}
12941302
},
12951303
i16 => {
12961304
if (js_value.isInt16Array()) {
12971305
if (byte_len == 0) return &[_]i16{};
12981306
const arr_ptr = @as([*]i16, @ptrCast(@alignCast(data)));
1299-
return arr_ptr[0 .. byte_len / 2];
1307+
return arr_ptr[byte_offset .. byte_offset + byte_len / 2];
13001308
}
13011309
},
13021310
u32 => {
13031311
if (js_value.isUint32Array()) {
13041312
if (byte_len == 0) return &[_]u32{};
13051313
const arr_ptr = @as([*]u32, @ptrCast(@alignCast(data)));
1306-
return arr_ptr[0 .. byte_len / 4];
1314+
return arr_ptr[byte_offset .. byte_offset + byte_len / 4];
13071315
}
13081316
},
13091317
i32 => {
13101318
if (js_value.isInt32Array()) {
13111319
if (byte_len == 0) return &[_]i32{};
13121320
const arr_ptr = @as([*]i32, @ptrCast(@alignCast(data)));
1313-
return arr_ptr[0 .. byte_len / 4];
1321+
return arr_ptr[byte_offset .. byte_offset + byte_len / 4];
13141322
}
13151323
},
13161324
u64 => {
13171325
if (js_value.isBigUint64Array()) {
13181326
if (byte_len == 0) return &[_]u64{};
13191327
const arr_ptr = @as([*]u64, @ptrCast(@alignCast(data)));
1320-
return arr_ptr[0 .. byte_len / 8];
1328+
return arr_ptr[byte_offset .. byte_offset + byte_len / 8];
13211329
}
13221330
},
13231331
i64 => {
13241332
if (js_value.isBigInt64Array()) {
13251333
if (byte_len == 0) return &[_]i64{};
13261334
const arr_ptr = @as([*]i64, @ptrCast(@alignCast(data)));
1327-
return arr_ptr[0 .. byte_len / 8];
1335+
return arr_ptr[byte_offset .. byte_offset + byte_len / 8];
13281336
}
13291337
},
13301338
else => {},

src/tests/encoding/decoder.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,16 @@
4545
testing.expectEqual('', d2.decode(new Uint8Array([226, 153]), { stream: true }));
4646
testing.expectEqual('♥', d2.decode(new Uint8Array([165]), { stream: true }));
4747
</script>
48+
49+
<script id=slice>
50+
const buf1 = new ArrayBuffer(7);
51+
const arr1 = new Uint8Array(buf1)
52+
arr1[0] = 80;
53+
arr1[1] = 81;
54+
arr1[2] = 82;
55+
arr1[3] = 83;
56+
arr1[4] = 84;
57+
arr1[5] = 85;
58+
arr1[6] = 86;
59+
testing.expectEqual('RST', d3.decode(new Uint8Array(buf1, 2, 3)));
60+
</script>

0 commit comments

Comments
 (0)