UNPKG

6.01 kBtext/x-cView Raw
1/*
2 Source File : ByteReaderWithPositionDriver.h
3
4
5 Copyright 2013 Gal Kahana HummusJS
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19 */
20#include "ByteReaderWithPositionDriver.h"
21#include "IByteReaderWithPosition.h"
22#include "ConstructorsHolder.h"
23
24using namespace v8;
25
26ByteReaderWithPositionDriver::ByteReaderWithPositionDriver()
27{
28 mInstance = NULL;
29 mOwns = false;
30}
31
32ByteReaderWithPositionDriver::~ByteReaderWithPositionDriver()
33{
34 if(mOwns)
35 delete mInstance;
36}
37
38DEF_SUBORDINATE_INIT(ByteReaderWithPositionDriver::Init)
39{
40
41 CREATE_ISOLATE_CONTEXT;
42
43 // prepare the page interfrace template
44 Local<FunctionTemplate> t = NEW_FUNCTION_TEMPLATE(New);
45
46 t->SetClassName(NEW_STRING("ByteReaderWithPosition"));
47 t->InstanceTemplate()->SetInternalFieldCount(1);
48
49 // prepare the page interfrace template
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 // save in factory
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
65METHOD_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
75void ByteReaderWithPositionDriver::SetStream(IByteReaderWithPosition* inReader,bool inOwns)
76{
77 if(mOwns)
78 delete mInstance;
79 mInstance = inReader;
80 mOwns = inOwns;
81}
82
83IByteReaderWithPosition* ByteReaderWithPositionDriver::GetStream()
84{
85 return mInstance;
86}
87
88using namespace IOBasicTypes;
89
90METHOD_RETURN_TYPE ByteReaderWithPositionDriver::Read(const ARGS_TYPE& args)
91{
92 CREATE_ISOLATE_CONTEXT;
93 CREATE_ESCAPABLE_SCOPE;
94
95 // k. i'll just read the number of bytes and return an array of them
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); // reading int cause that's the maximum that can read (use should read till notended anyways)
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
117METHOD_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
127METHOD_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
137METHOD_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
156METHOD_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
175METHOD_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}