ThrowException(Exception::TypeError(String::New("Bad argument")))
static Persistent<String> encoding_symbol;
static Persistent<String> errno_symbol;
+static Persistent<String> buf_symbol;
// Buffer for readlink() and other misc callers; keep this scoped at
// file-level rather than method-level to avoid excess stack usage.
Local<Value> cb = args[5];
if (cb->IsFunction()) {
+ // Grab a reference to buffer so it isn't GCed
+ Local<Object> cb_obj = cb->ToObject();
+ cb_obj->Set(buf_symbol, buffer_obj);
+
ASYNC_CALL(write, cb, fd, buf, len, pos)
} else {
ssize_t written = pos < 0 ? write(fd, buf, len) : pwrite(fd, buf, len, pos);
cb = args[5];
if (cb->IsFunction()) {
+ // Grab a reference to buffer so it isn't GCed
+ // TODO: need test coverage
+ Local<Object> cb_obj = cb->ToObject();
+ cb_obj->Set(buf_symbol, buffer_obj);
+
ASYNC_CALL(read, cb, fd, buf, len, pos);
} else {
// SYNC
errno_symbol = NODE_PSYMBOL("errno");
encoding_symbol = NODE_PSYMBOL("node:encoding");
+ buf_symbol = NODE_PSYMBOL("__buf");
}
void InitFs(Handle<Object> target) {
--- /dev/null
+var common = require('../common');
+var fs = require("fs");
+var assert = require("assert");
+var join = require('path').join;
+
+var filename = join(common.tmpDir, 'out.txt');
+
+try {
+ fs.unlinkSync(filename);
+} catch (e) {
+ // might not exist, that's okay.
+}
+
+var fd = fs.openSync(filename, "w");
+
+var line = "aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n";
+
+var N = 10240, complete = 0;
+for (var i = 0; i < N; i ++) {
+ // Create a new buffer for each write. Before the write is actually
+ // executed by the thread pool, the buffer will be collected.
+ var buffer = new Buffer(line);
+ fs.write(fd, buffer, 0, buffer.length, null, function (er, written) {
+ complete++;
+ if (complete === N) {
+ fs.closeSync(fd);
+ var s = fs.createReadStream(filename);
+ s.on("data", testBuffer);
+ }
+ });
+}
+
+var bytesChecked = 0;
+
+function testBuffer (b) {
+ for (var i = 0; i < b.length; i++) {
+ bytesChecked++;
+ if (b[i] !== 'a'.charCodeAt(0) && b[i] !== '\n'.charCodeAt(0)) {
+ throw new Error("invalid char "+i+","+b[i]);
+ }
+ }
+}
+
+process.on('exit', function () {
+ // Probably some of the writes are going to overlap, so we can't assume
+ // that we get (N * line.length). Let's just make sure we've checked a
+ // few...
+ assert.ok(bytesChecked > 1000);
+});
+