UNPKG

3.65 kBtext/x-cView Raw
1/*
2 Source File : ByteWriterWithPositionDriver.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 "ByteWriterWithPositionDriver.h"
21#include "IByteWriterWithPosition.h"
22#include "ConstructorsHolder.h"
23
24using namespace v8;
25
26ByteWriterWithPositionDriver::ByteWriterWithPositionDriver()
27{
28 mInstance = NULL;
29 mOwns = false;
30}
31
32ByteWriterWithPositionDriver::~ByteWriterWithPositionDriver()
33{
34 if(mOwns)
35 delete mInstance;
36}
37
38DEF_SUBORDINATE_INIT(ByteWriterWithPositionDriver::Init)
39{
40 CREATE_ISOLATE_CONTEXT;
41
42 Local<FunctionTemplate> t = NEW_FUNCTION_TEMPLATE(New);
43
44 t->SetClassName(NEW_STRING("ByteWriterWithPosition"));
45 t->InstanceTemplate()->SetInternalFieldCount(1);
46
47 SET_PROTOTYPE_METHOD(t, "write", Write);
48 SET_PROTOTYPE_METHOD(t, "getCurrentPosition", GetCurrentPosition);
49
50
51 // save in factory
52 EXPOSE_EXTERNAL_FOR_INIT(ConstructorsHolder, holder)
53 SET_CONSTRUCTOR(holder->ByteWriterWithPosition_constructor, t);
54 SET_CONSTRUCTOR_TEMPLATE(holder->ByteWriterWithPosition_constructor_template, t);
55}
56
57METHOD_RETURN_TYPE ByteWriterWithPositionDriver::New(const ARGS_TYPE& args)
58{
59 CREATE_ISOLATE_CONTEXT;
60 CREATE_ESCAPABLE_SCOPE;
61
62 ByteWriterWithPositionDriver* driver = new ByteWriterWithPositionDriver();
63 driver->Wrap(args.This());
64 SET_FUNCTION_RETURN_VALUE(args.This())
65}
66
67void ByteWriterWithPositionDriver::SetStream(IByteWriterWithPosition* inWriterWithPosition,bool inOwns)
68{
69 if(mOwns)
70 delete mInstance;
71 mInstance = inWriterWithPosition;
72 mOwns = inOwns;
73}
74
75IByteWriterWithPosition* ByteWriterWithPositionDriver::GetStream()
76{
77 return mInstance;
78}
79
80using namespace IOBasicTypes;
81
82METHOD_RETURN_TYPE ByteWriterWithPositionDriver::Write(const ARGS_TYPE& args)
83{
84 CREATE_ISOLATE_CONTEXT;
85 CREATE_ESCAPABLE_SCOPE;
86
87 // k. i'll just read the number of bytes and return an array of them
88 if(args.Length() != 1 ||
89 !args[0]->IsArray())
90 {
91 THROW_EXCEPTION("Wrong arguments. pass an array of bytes to write");
92 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
93 }
94
95 ByteWriterWithPositionDriver* element = ObjectWrap::Unwrap<ByteWriterWithPositionDriver>(args.This());
96 int bufferSize = args[0]->TO_OBJECT()->Get(GET_CURRENT_CONTEXT, NEW_STRING("length")).ToLocalChecked()->TO_UINT32Value();
97 IOBasicTypes::Byte* buffer = new IOBasicTypes::Byte[bufferSize];
98
99 for(int i=0;i<bufferSize;++i)
100 buffer[i] = args[0]->TO_OBJECT()->Get(GET_CURRENT_CONTEXT, i).ToLocalChecked()->TO_UINT32Value();
101
102 bufferSize = (int)element->mInstance->Write(buffer,bufferSize);
103
104 delete[] buffer;
105
106 SET_FUNCTION_RETURN_VALUE(NEW_NUMBER(bufferSize))
107}
108
109METHOD_RETURN_TYPE ByteWriterWithPositionDriver::GetCurrentPosition(const ARGS_TYPE& args)
110{
111 CREATE_ISOLATE_CONTEXT;
112 CREATE_ESCAPABLE_SCOPE;
113
114 ByteWriterWithPositionDriver* element = ObjectWrap::Unwrap<ByteWriterWithPositionDriver>(args.This());
115
116 SET_FUNCTION_RETURN_VALUE(NEW_NUMBER(element->mInstance->GetCurrentPosition()))
117}
118