1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | #include "ByteWriterWithPositionDriver.h"
|
21 | #include "IByteWriterWithPosition.h"
|
22 | #include "ConstructorsHolder.h"
|
23 |
|
24 | using namespace v8;
|
25 |
|
26 | ByteWriterWithPositionDriver::ByteWriterWithPositionDriver()
|
27 | {
|
28 | mInstance = NULL;
|
29 | mOwns = false;
|
30 | }
|
31 |
|
32 | ByteWriterWithPositionDriver::~ByteWriterWithPositionDriver()
|
33 | {
|
34 | if(mOwns)
|
35 | delete mInstance;
|
36 | }
|
37 |
|
38 | DEF_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 |
|
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 |
|
57 | METHOD_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 |
|
67 | void ByteWriterWithPositionDriver::SetStream(IByteWriterWithPosition* inWriterWithPosition,bool inOwns)
|
68 | {
|
69 | if(mOwns)
|
70 | delete mInstance;
|
71 | mInstance = inWriterWithPosition;
|
72 | mOwns = inOwns;
|
73 | }
|
74 |
|
75 | IByteWriterWithPosition* ByteWriterWithPositionDriver::GetStream()
|
76 | {
|
77 | return mInstance;
|
78 | }
|
79 |
|
80 | using namespace IOBasicTypes;
|
81 |
|
82 | METHOD_RETURN_TYPE ByteWriterWithPositionDriver::Write(const ARGS_TYPE& args)
|
83 | {
|
84 | CREATE_ISOLATE_CONTEXT;
|
85 | CREATE_ESCAPABLE_SCOPE;
|
86 |
|
87 |
|
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 |
|
109 | METHOD_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 |
|