1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | #include "ByteReaderWithPositionDriver.h"
|
21 | #include "IByteReaderWithPosition.h"
|
22 | #include "ConstructorsHolder.h"
|
23 |
|
24 | using namespace v8;
|
25 |
|
26 | ByteReaderWithPositionDriver::ByteReaderWithPositionDriver()
|
27 | {
|
28 | mInstance = NULL;
|
29 | mOwns = false;
|
30 | }
|
31 |
|
32 | ByteReaderWithPositionDriver::~ByteReaderWithPositionDriver()
|
33 | {
|
34 | if(mOwns)
|
35 | delete mInstance;
|
36 | }
|
37 |
|
38 | DEF_SUBORDINATE_INIT(ByteReaderWithPositionDriver::Init)
|
39 | {
|
40 |
|
41 | CREATE_ISOLATE_CONTEXT;
|
42 |
|
43 |
|
44 | Local<FunctionTemplate> t = NEW_FUNCTION_TEMPLATE(New);
|
45 |
|
46 | t->SetClassName(NEW_STRING("ByteReaderWithPosition"));
|
47 | t->InstanceTemplate()->SetInternalFieldCount(1);
|
48 |
|
49 |
|
50 | SET_PROTOTYPE_METHOD(t, "read", Read);
|
51 | SET_PROTOTYPE_METHOD(t, "notEnded", NotEnded);
|
52 | SET_PROTOTYPE_METHOD(t, "setPosition", SetPosition);
|
53 | SET_PROTOTYPE_METHOD(t, "getCurrentPosition", GetCurrentPosition);
|
54 | SET_PROTOTYPE_METHOD(t, "setPositionFromEnd", SetPositionFromEnd);
|
55 | SET_PROTOTYPE_METHOD(t, "skip", Skip);
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | EXPOSE_EXTERNAL_FOR_INIT(ConstructorsHolder, holder)
|
61 | SET_CONSTRUCTOR(holder->ByteReaderWithPosition_constructor, t);
|
62 | SET_CONSTRUCTOR_TEMPLATE(holder->ByteReaderWithPosition_constructor_template, t);
|
63 | }
|
64 |
|
65 | METHOD_RETURN_TYPE ByteReaderWithPositionDriver::New(const ARGS_TYPE& args)
|
66 | {
|
67 | CREATE_ISOLATE_CONTEXT;
|
68 | CREATE_ESCAPABLE_SCOPE;
|
69 |
|
70 | ByteReaderWithPositionDriver* driver = new ByteReaderWithPositionDriver();
|
71 | driver->Wrap(args.This());
|
72 | SET_FUNCTION_RETURN_VALUE(args.This())
|
73 | }
|
74 |
|
75 | void ByteReaderWithPositionDriver::SetStream(IByteReaderWithPosition* inReader,bool inOwns)
|
76 | {
|
77 | if(mOwns)
|
78 | delete mInstance;
|
79 | mInstance = inReader;
|
80 | mOwns = inOwns;
|
81 | }
|
82 |
|
83 | IByteReaderWithPosition* ByteReaderWithPositionDriver::GetStream()
|
84 | {
|
85 | return mInstance;
|
86 | }
|
87 |
|
88 | using namespace IOBasicTypes;
|
89 |
|
90 | METHOD_RETURN_TYPE ByteReaderWithPositionDriver::Read(const ARGS_TYPE& args)
|
91 | {
|
92 | CREATE_ISOLATE_CONTEXT;
|
93 | CREATE_ESCAPABLE_SCOPE;
|
94 |
|
95 |
|
96 | if(args.Length() != 1 ||
|
97 | !args[0]->IsNumber())
|
98 | {
|
99 | THROW_EXCEPTION("Wrong arguments. pass the number of bytes to read");
|
100 | SET_FUNCTION_RETURN_VALUE(UNDEFINED)
|
101 | }
|
102 |
|
103 | ByteReaderWithPositionDriver* element = ObjectWrap::Unwrap<ByteReaderWithPositionDriver>(args.This());
|
104 | IOBasicTypes::LongBufferSizeType bufferSize = TO_UINT32(args[0])->Value();
|
105 | IOBasicTypes::Byte* buffer = new IOBasicTypes::Byte[bufferSize];
|
106 |
|
107 | bufferSize = element->mInstance->Read(buffer,(int)bufferSize);
|
108 |
|
109 | Local<Array> outBuffer = NEW_ARRAY((int)bufferSize);
|
110 |
|
111 | for(LongBufferSizeType i=0;i<bufferSize;++i)
|
112 | outBuffer->Set(GET_CURRENT_CONTEXT, NEW_NUMBER(i),NEW_NUMBER(buffer[i]));
|
113 |
|
114 | SET_FUNCTION_RETURN_VALUE(outBuffer)
|
115 | }
|
116 |
|
117 | METHOD_RETURN_TYPE ByteReaderWithPositionDriver::NotEnded(const ARGS_TYPE& args)
|
118 | {
|
119 | CREATE_ISOLATE_CONTEXT;
|
120 | CREATE_ESCAPABLE_SCOPE;
|
121 |
|
122 | ByteReaderWithPositionDriver* element = ObjectWrap::Unwrap<ByteReaderWithPositionDriver>(args.This());
|
123 |
|
124 | SET_FUNCTION_RETURN_VALUE(NEW_BOOLEAN(element->mInstance->NotEnded()))
|
125 | }
|
126 |
|
127 | METHOD_RETURN_TYPE ByteReaderWithPositionDriver::GetCurrentPosition(const ARGS_TYPE& args)
|
128 | {
|
129 | CREATE_ISOLATE_CONTEXT;
|
130 | CREATE_ESCAPABLE_SCOPE;
|
131 |
|
132 | ByteReaderWithPositionDriver* element = ObjectWrap::Unwrap<ByteReaderWithPositionDriver>(args.This());
|
133 |
|
134 | SET_FUNCTION_RETURN_VALUE(NEW_NUMBER(element->mInstance->GetCurrentPosition()))
|
135 | }
|
136 |
|
137 | METHOD_RETURN_TYPE ByteReaderWithPositionDriver::Skip(const ARGS_TYPE& args)
|
138 | {
|
139 | CREATE_ISOLATE_CONTEXT;
|
140 | CREATE_ESCAPABLE_SCOPE;
|
141 |
|
142 | if(args.Length() != 1 ||
|
143 | !args[0]->IsNumber())
|
144 | {
|
145 | THROW_EXCEPTION("Wrong arguments. pass the number of bytes to skip");
|
146 | SET_FUNCTION_RETURN_VALUE(UNDEFINED)
|
147 | }
|
148 |
|
149 |
|
150 | ByteReaderWithPositionDriver* element = ObjectWrap::Unwrap<ByteReaderWithPositionDriver>(args.This());
|
151 | element->mInstance->Skip(TO_UINT32(args[0])->Value());
|
152 |
|
153 | SET_FUNCTION_RETURN_VALUE(args.This())
|
154 | }
|
155 |
|
156 | METHOD_RETURN_TYPE ByteReaderWithPositionDriver::SetPosition(const ARGS_TYPE& args)
|
157 | {
|
158 | CREATE_ISOLATE_CONTEXT;
|
159 | CREATE_ESCAPABLE_SCOPE;
|
160 |
|
161 | if(args.Length() != 1 ||
|
162 | !args[0]->IsNumber())
|
163 | {
|
164 | THROW_EXCEPTION("Wrong arguments. pass the position");
|
165 | SET_FUNCTION_RETURN_VALUE(UNDEFINED)
|
166 | }
|
167 |
|
168 |
|
169 | ByteReaderWithPositionDriver* element = ObjectWrap::Unwrap<ByteReaderWithPositionDriver>(args.This());
|
170 | element->mInstance->SetPosition(TO_UINT32(args[0])->Value());
|
171 |
|
172 | SET_FUNCTION_RETURN_VALUE(args.This())
|
173 | }
|
174 |
|
175 | METHOD_RETURN_TYPE ByteReaderWithPositionDriver::SetPositionFromEnd(const ARGS_TYPE& args)
|
176 | {
|
177 | CREATE_ISOLATE_CONTEXT;
|
178 | CREATE_ESCAPABLE_SCOPE;
|
179 |
|
180 | if(args.Length() != 1 ||
|
181 | !args[0]->IsNumber())
|
182 | {
|
183 | THROW_EXCEPTION("Wrong arguments. pass the position");
|
184 | SET_FUNCTION_RETURN_VALUE(UNDEFINED)
|
185 | }
|
186 |
|
187 |
|
188 | ByteReaderWithPositionDriver* element = ObjectWrap::Unwrap<ByteReaderWithPositionDriver>(args.This());
|
189 | element->mInstance->SetPositionFromEnd(TO_UINT32(args[0])->Value());
|
190 |
|
191 | SET_FUNCTION_RETURN_VALUE(args.This())
|
192 | }
|