1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | #include "ByteReaderDriver.h"
|
21 | #include "IByteReader.h"
|
22 | #include "ConstructorsHolder.h"
|
23 |
|
24 | using namespace v8;
|
25 |
|
26 | ByteReaderDriver::ByteReaderDriver()
|
27 | {
|
28 | mInstance = NULL;
|
29 | mOwns = false;
|
30 | }
|
31 |
|
32 | ByteReaderDriver::~ByteReaderDriver()
|
33 | {
|
34 | if(mOwns)
|
35 | delete mInstance;
|
36 | }
|
37 |
|
38 |
|
39 | DEF_SUBORDINATE_INIT(ByteReaderDriver::Init)
|
40 | {
|
41 | CREATE_ISOLATE_CONTEXT;
|
42 |
|
43 |
|
44 | Local<FunctionTemplate> t = NEW_FUNCTION_TEMPLATE(New);
|
45 | t->SetClassName(NEW_STRING("ByteReader"));
|
46 | t->InstanceTemplate()->SetInternalFieldCount(1);
|
47 |
|
48 | SET_PROTOTYPE_METHOD(t, "read", Read);
|
49 | SET_PROTOTYPE_METHOD(t, "notEnded", NotEnded);
|
50 |
|
51 |
|
52 |
|
53 | EXPOSE_EXTERNAL_FOR_INIT(ConstructorsHolder, holder)
|
54 | SET_CONSTRUCTOR(holder->ByteReader_constructor, t);
|
55 | SET_CONSTRUCTOR_TEMPLATE(holder->ByteReader_constructor_template, t);
|
56 | }
|
57 |
|
58 | METHOD_RETURN_TYPE ByteReaderDriver::New(const ARGS_TYPE& args)
|
59 | {
|
60 | CREATE_ISOLATE_CONTEXT;
|
61 | CREATE_ESCAPABLE_SCOPE;
|
62 |
|
63 | ByteReaderDriver* driver = new ByteReaderDriver();
|
64 | driver->Wrap(args.This());
|
65 | SET_FUNCTION_RETURN_VALUE(args.This())
|
66 | }
|
67 |
|
68 | void ByteReaderDriver::SetStream(IByteReader* inReader,bool inOwns)
|
69 | {
|
70 | if(mOwns)
|
71 | delete mInstance;
|
72 | mInstance = inReader;
|
73 | mOwns = inOwns;
|
74 | }
|
75 |
|
76 | IByteReader* ByteReaderDriver::GetStream()
|
77 | {
|
78 | return mInstance;
|
79 | }
|
80 |
|
81 | using namespace IOBasicTypes;
|
82 |
|
83 | METHOD_RETURN_TYPE ByteReaderDriver::Read(const ARGS_TYPE& args)
|
84 | {
|
85 | CREATE_ISOLATE_CONTEXT;
|
86 | CREATE_ESCAPABLE_SCOPE;
|
87 |
|
88 |
|
89 | if(args.Length() != 1 ||
|
90 | !args[0]->IsNumber())
|
91 | {
|
92 | THROW_EXCEPTION("Wrong arguments. pass the number of bytes to read");
|
93 | SET_FUNCTION_RETURN_VALUE(UNDEFINED)
|
94 | }
|
95 |
|
96 | ByteReaderDriver* element = ObjectWrap::Unwrap<ByteReaderDriver>(args.This());
|
97 | IOBasicTypes::LongBufferSizeType bufferSize = TO_UINT32(args[0])->Value();
|
98 | IOBasicTypes::Byte* buffer = new IOBasicTypes::Byte[bufferSize];
|
99 |
|
100 | bufferSize = element->mInstance->Read(buffer,(int)bufferSize);
|
101 |
|
102 | Local<Array> outBuffer = NEW_ARRAY((int)bufferSize);
|
103 |
|
104 | for(LongBufferSizeType i=0;i<bufferSize;++i)
|
105 | outBuffer->Set(GET_CURRENT_CONTEXT, NEW_NUMBER(i), NEW_NUMBER(buffer[i]));
|
106 |
|
107 | delete[] buffer;
|
108 |
|
109 | SET_FUNCTION_RETURN_VALUE(outBuffer)
|
110 | }
|
111 |
|
112 | METHOD_RETURN_TYPE ByteReaderDriver::NotEnded(const ARGS_TYPE& args)
|
113 | {
|
114 | CREATE_ISOLATE_CONTEXT;
|
115 | CREATE_ESCAPABLE_SCOPE;
|
116 |
|
117 | ByteReaderDriver* element = ObjectWrap::Unwrap<ByteReaderDriver>(args.This());
|
118 |
|
119 | SET_FUNCTION_RETURN_VALUE(NEW_BOOLEAN(element->mInstance->NotEnded()))
|
120 | }
|
121 |
|
122 |
|