Skip to content

Commit b6012fb

Browse files
committed
Add test for FS.write() with canOwn=true to a view that is a subview of a larger buffer and improve comments.
1 parent 6c22952 commit b6012fb

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/library_memfs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,16 @@ mergeInto(LibraryManager.library, {
276276
// Writes the byte range (buffer[offset], buffer[offset+length]) to offset 'position' into the file pointed by 'stream'
277277
// canOwn: A boolean that tells if this function can take ownership of the passed in buffer from the subbuffer portion
278278
// that the typed array view 'buffer' points to. The underlying ArrayBuffer can be larger than that, but
279-
// canOwn=true will not take ownership of the portion outside the bytes addressed by the view.
279+
// canOwn=true will not take ownership of the portion outside the bytes addressed by the view. This means that
280+
// with canOwn=true, creating a copy of the bytes is avoided, but the caller shouldn't touch the passed in range
281+
// of bytes anymore since their contents now represent file data inside the filesystem.
280282
write: function(stream, buffer, offset, length, position, canOwn) {
281283
if (!length) return 0;
282284
var node = stream.node;
283285
node.timestamp = Date.now();
284286

285287
if (buffer.subarray && (!node.contents || node.contents.subarray)) { // This write is from a typed array to a typed array?
286-
if (canOwn) { // Can we just reuse the buffer we are given?
288+
if (canOwn) {
287289
#if ASSERTIONS
288290
assert(position === 0, 'canOwn must imply no weird position inside the file');
289291
#endif

tests/fs/test_write.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// https://github.com/kripken/emscripten/pull/4705: Test that FS.write() with canOwn=true works.
2+
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string>
6+
7+
#include <emscripten/emscripten.h>
8+
9+
int main()
10+
{
11+
EM_ASM(
12+
var stream = FS.open('testfile', 'w+');
13+
14+
var data = new Uint8Array(128);
15+
var str = "Hello! ";
16+
stringToUTF8Array(str, data, 0, lengthBytesUTF8(str)+1);
17+
data = data.subarray(0, lengthBytesUTF8(str));
18+
FS.write(stream, data, 0, lengthBytesUTF8(str), 0, /*canOwn=*/true);
19+
var pos = lengthBytesUTF8(str);
20+
21+
str = '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890';
22+
data = new Uint8Array(100);
23+
stringToUTF8Array(str, data, 0, lengthBytesUTF8(str)+1);
24+
FS.write(stream, data, 0, lengthBytesUTF8(str)+1, pos, /*canOwn=*/false);
25+
26+
FS.close(stream);
27+
);
28+
29+
std::ifstream file("testfile");
30+
std::string line;
31+
getline(file, line);
32+
std::cout << "read " << line << std::endl;
33+
34+
return 0;
35+
}

tests/fs/test_write.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
read Hello! 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

tests/test_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4451,6 +4451,12 @@ def test_fs_writeFile(self):
44514451
out = path_from_root('tests', 'fs', 'test_writeFile.out')
44524452
self.do_run_from_file(src, out)
44534453

4454+
def test_fs_write(self):
4455+
self.emcc_args = ['-s', 'MEMFS_APPEND_TO_TYPED_ARRAYS=1']
4456+
src = path_from_root('tests', 'fs', 'test_write.cpp')
4457+
out = path_from_root('tests', 'fs', 'test_write.out')
4458+
self.do_run_from_file(src, out)
4459+
44544460
def test_fs_emptyPath(self):
44554461
src = path_from_root('tests', 'fs', 'test_emptyPath.c')
44564462
out = path_from_root('tests', 'fs', 'test_emptyPath.out')

0 commit comments

Comments
 (0)