@@ -106,18 +106,9 @@ static int After(eio_req *req) {
106
106
107
107
case EIO_READ:
108
108
{
109
- if (req->int3 ) {
110
- // legacy interface
111
- Local<Object> obj = Local<Object>::New (*callback);
112
- Local<Value> enc_val = obj->GetHiddenValue (encoding_symbol);
113
- argv[1 ] = Encode (req->ptr2 , req->result , ParseEncoding (enc_val));
114
- argv[2 ] = Integer::New (req->result );
115
- argc = 3 ;
116
- } else {
117
- // Buffer interface
118
- argv[1 ] = Integer::New (req->result );
119
- argc = 2 ;
120
- }
109
+ // Buffer interface
110
+ argv[1 ] = Integer::New (req->result );
111
+ argc = 2 ;
121
112
break ;
122
113
}
123
114
@@ -584,16 +575,6 @@ static Handle<Value> Write(const Arguments& args) {
584
575
* 3 length integer. length to read
585
576
* 4 position file position - null for current position
586
577
*
587
- * - OR -
588
- *
589
- * [string, bytesRead] = fs.read(fd, length, position, encoding)
590
- *
591
- * 0 fd integer. file descriptor
592
- * 1 length integer. length to read
593
- * 2 position if integer, position to read from in the file.
594
- * if null, read from the current position
595
- * 3 encoding
596
- *
597
578
*/
598
579
static Handle<Value> Read (const Arguments& args) {
599
580
HandleScope scope;
@@ -605,105 +586,47 @@ static Handle<Value> Read(const Arguments& args) {
605
586
int fd = args[0 ]->Int32Value ();
606
587
607
588
Local<Value> cb;
608
- bool legacy;
609
589
610
590
size_t len;
611
591
off_t pos;
612
- enum encoding encoding;
613
592
614
593
char * buf = NULL ;
615
594
616
- if (Buffer::HasInstance (args[1 ])) {
617
- legacy = false ;
618
- // 0 fd integer. file descriptor
619
- // 1 buffer instance of Buffer
620
- // 2 offset integer. offset to start reading into inside buffer
621
- // 3 length integer. length to read
622
- // 4 position file position - null for current position
623
- Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[1 ]->ToObject ());
624
-
625
- size_t off = args[2 ]->Int32Value ();
626
- if (off >= buffer->length ()) {
627
- return ThrowException (Exception::Error (
628
- String::New (" Offset is out of bounds" )));
629
- }
630
-
631
- len = args[3 ]->Int32Value ();
632
- if (off + len > buffer->length ()) {
633
- return ThrowException (Exception::Error (
634
- String::New (" Length is extends beyond buffer" )));
635
- }
636
-
637
- pos = GET_OFFSET (args[4 ]);
638
-
639
- buf = (char *)buffer->data () + off;
640
-
641
- cb = args[5 ];
642
-
643
- } else {
644
- legacy = true ;
645
- // 0 fd integer. file descriptor
646
- // 1 length integer. length to read
647
- // 2 position if integer, position to read from in the file.
648
- // if null, read from the current position
649
- // 3 encoding
650
- len = args[1 ]->IntegerValue ();
651
- pos = GET_OFFSET (args[2 ]);
652
- encoding = ParseEncoding (args[3 ]);
595
+ if (!Buffer::HasInstance (args[1 ])) {
596
+ return ThrowException (Exception::Error (
597
+ String::New (" Second argument needs to be a buffer" )));
598
+ }
653
599
654
- buf = NULL ; // libeio will allocate and free it.
600
+ Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[ 1 ]-> ToObject ());
655
601
656
- cb = args[4 ];
602
+ size_t off = args[2 ]->Int32Value ();
603
+ if (off >= buffer->length ()) {
604
+ return ThrowException (Exception::Error (
605
+ String::New (" Offset is out of bounds" )));
657
606
}
658
607
608
+ len = args[3 ]->Int32Value ();
609
+ if (off + len > buffer->length ()) {
610
+ return ThrowException (Exception::Error (
611
+ String::New (" Length is extends beyond buffer" )));
612
+ }
659
613
660
- if (cb->IsFunction ()) {
661
- // WARNING: HACK AGAIN, PROCEED WITH CAUTION
662
- // Normally here I would do
663
- // ASYNC_CALL(read, args[4], fd, NULL, len, offset)
664
- // but I'm trying to support a legacy interface where it's acceptable to
665
- // return a string in the callback. As well as a new Buffer interface
666
- // which reads data into a user supplied buffer.
667
-
668
- // Set the encoding on the callback
669
- if (legacy) {
670
- Local<Object> obj = cb->ToObject ();
671
- obj->SetHiddenValue (encoding_symbol, args[3 ]);
672
- }
673
-
674
- if (legacy) assert (buf == NULL );
614
+ pos = GET_OFFSET (args[4 ]);
675
615
676
-
677
- eio_req *req = eio_read (fd, buf, len, pos,
678
- EIO_PRI_DEFAULT,
679
- After,
680
- cb_persist (cb));
681
- assert (req);
616
+ buf = (char *)buffer->data () + off;
682
617
683
- req->int3 = legacy ? 1 : 0 ;
684
- ev_ref (EV_DEFAULT_UC);
685
- return Undefined ();
618
+ cb = args[5 ];
686
619
620
+ if (cb->IsFunction ()) {
621
+ ASYNC_CALL (read, cb, fd, buf, len, pos);
687
622
} else {
688
623
// SYNC
689
624
ssize_t ret;
690
625
691
- if (legacy) {
692
- #define READ_BUF_LEN (16 *1024 )
693
- char buf2[READ_BUF_LEN];
694
- ret = pos < 0 ? read (fd, buf2, MIN (len, READ_BUF_LEN))
695
- : pread (fd, buf2, MIN (len, READ_BUF_LEN), pos);
696
- if (ret < 0 ) return ThrowException (ErrnoException (errno));
697
- Local<Array> a = Array::New (2 );
698
- a->Set (Integer::New (0 ), Encode (buf2, ret, encoding));
699
- a->Set (Integer::New (1 ), Integer::New (ret));
700
- return scope.Close (a);
701
- } else {
702
- ret = pos < 0 ? read (fd, buf, len) : pread (fd, buf, len, pos);
703
- if (ret < 0 ) return ThrowException (ErrnoException (errno));
704
- Local<Integer> bytesRead = Integer::New (ret);
705
- return scope.Close (bytesRead);
706
- }
626
+ ret = pos < 0 ? read (fd, buf, len) : pread (fd, buf, len, pos);
627
+ if (ret < 0 ) return ThrowException (ErrnoException (errno));
628
+ Local<Integer> bytesRead = Integer::New (ret);
629
+ return scope.Close (bytesRead);
707
630
}
708
631
}
709
632
0 commit comments