Skip to content

Commit e592e31

Browse files
committed
Honor buffer.byteOffset in read/write for nodefs
1 parent 1cd1c2e commit e592e31

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/lib/libnodefs.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,14 @@ addToLibrary({
271271
stream.shared.refcount++;
272272
},
273273
read(stream, buffer, offset, length, position) {
274-
return NODEFS.tryFSOperation(() =>
275-
fs.readSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position)
276-
);
274+
return NODEFS.tryFSOperation(() => {
275+
return fs.readSync(stream.nfd, buffer, offset, length, position);
276+
});
277277
},
278278
write(stream, buffer, offset, length, position) {
279-
return NODEFS.tryFSOperation(() =>
280-
fs.writeSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position)
281-
);
279+
return NODEFS.tryFSOperation(() => {
280+
return fs.writeSync(stream.nfd, buffer, offset, length, position);
281+
});
282282
},
283283
llseek(stream, offset, whence) {
284284
var position = offset;

test/fs/test_writeFile.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88

99
int main() {
1010
EM_ASM(
11+
const buf = Uint8Array.from('c=3\nd=4\ne=5', x => x.charCodeAt(0));
1112
FS.writeFile("testfile", "a=1\nb=2\n");
12-
FS.writeFile("testfile", new Uint8Array([99, 61, 51]) /* c=3 */, { flags: "a" });
13+
FS.writeFile("testfile", buf.subarray(4, 7), { flags: "a" });
1314
);
1415

1516
std::ifstream file("testfile");
1617

1718
while (!file.eof() && !file.fail()) {
1819
std::string line;
1920
getline(file, line);
20-
std::string name;
21+
std::string key;
22+
std::string val;
2123

2224
std::cout << "read " << line << std::endl;
2325

@@ -35,7 +37,10 @@ int main() {
3537
continue;
3638
}
3739

38-
name = line.substr(0, equalsPos);
40+
key = line.substr(0, equalsPos);
41+
val = line.substr(equalsPos + 1);
42+
43+
std::cout << "parsed " << key << "=" << val << std::endl;
3944
}
4045

4146
return 0;

test/fs/test_writeFile.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
read a=1
2+
parsed a=1
23
read b=2
3-
read c=3
4+
parsed b=2
5+
read d=4
6+
parsed d=4

0 commit comments

Comments
 (0)