@@ -31,6 +31,7 @@ using namespace v8;
31
31
ThrowException (Exception::TypeError(String::New(" Bad argument" )))
32
32
static Persistent<String> encoding_symbol;
33
33
static Persistent<String> errno_symbol;
34
+ static Persistent<String> buf_symbol;
34
35
35
36
// Buffer for readlink() and other misc callers; keep this scoped at
36
37
// file-level rather than method-level to avoid excess stack usage.
@@ -638,6 +639,10 @@ static Handle<Value> Write(const Arguments& args) {
638
639
Local<Value> cb = args[5 ];
639
640
640
641
if (cb->IsFunction ()) {
642
+ // Grab a reference to buffer so it isn't GCed
643
+ Local<Object> cb_obj = cb->ToObject ();
644
+ cb_obj->Set (buf_symbol, buffer_obj);
645
+
641
646
ASYNC_CALL (write , cb, fd, buf, len, pos)
642
647
} else {
643
648
ssize_t written = pos < 0 ? write (fd, buf, len) : pwrite (fd, buf, len, pos);
@@ -702,6 +707,11 @@ static Handle<Value> Read(const Arguments& args) {
702
707
cb = args[5 ];
703
708
704
709
if (cb->IsFunction ()) {
710
+ // Grab a reference to buffer so it isn't GCed
711
+ // TODO: need test coverage
712
+ Local<Object> cb_obj = cb->ToObject ();
713
+ cb_obj->Set (buf_symbol, buffer_obj);
714
+
705
715
ASYNC_CALL (read , cb, fd, buf, len, pos);
706
716
} else {
707
717
// SYNC
@@ -792,6 +802,7 @@ void File::Initialize(Handle<Object> target) {
792
802
793
803
errno_symbol = NODE_PSYMBOL (" errno" );
794
804
encoding_symbol = NODE_PSYMBOL (" node:encoding" );
805
+ buf_symbol = NODE_PSYMBOL (" __buf" );
795
806
}
796
807
797
808
void InitFs (Handle <Object> target) {
0 commit comments