UNPKG

3.43 kBtext/x-cView Raw
1/*
2 Source File : ByteReaderDriver.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 "ByteReaderDriver.h"
21#include "IByteReader.h"
22#include "ConstructorsHolder.h"
23
24using namespace v8;
25
26ByteReaderDriver::ByteReaderDriver()
27{
28 mInstance = NULL;
29 mOwns = false;
30}
31
32ByteReaderDriver::~ByteReaderDriver()
33{
34 if(mOwns)
35 delete mInstance;
36}
37
38
39DEF_SUBORDINATE_INIT(ByteReaderDriver::Init)
40{
41 CREATE_ISOLATE_CONTEXT;
42
43 // prepare the page interfrace template
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 // save in factory
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
58METHOD_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
68void ByteReaderDriver::SetStream(IByteReader* inReader,bool inOwns)
69{
70 if(mOwns)
71 delete mInstance;
72 mInstance = inReader;
73 mOwns = inOwns;
74}
75
76IByteReader* ByteReaderDriver::GetStream()
77{
78 return mInstance;
79}
80
81using namespace IOBasicTypes;
82
83METHOD_RETURN_TYPE ByteReaderDriver::Read(const ARGS_TYPE& args)
84{
85 CREATE_ISOLATE_CONTEXT;
86 CREATE_ESCAPABLE_SCOPE;
87
88 // k. i'll just read the number of bytes and return an array of them
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); // reading int cause that's the maximum that can read (use should read till notended anyways)
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
112METHOD_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