1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | #include "ObjectByteWriterWithPosition.h"
|
21 |
|
22 | using namespace v8;
|
23 |
|
24 | ObjectByteWriterWithPosition::ObjectByteWriterWithPosition(Local<Object> inObject)
|
25 | {
|
26 | CREATE_ISOLATE_CONTEXT;
|
27 |
|
28 | SET_PERSISTENT_OBJECT(mObject, Object, inObject);
|
29 | }
|
30 |
|
31 | ObjectByteWriterWithPosition::~ObjectByteWriterWithPosition()
|
32 | {
|
33 | DISPOSE_PERSISTENT(mObject);
|
34 | }
|
35 |
|
36 | IOBasicTypes::LongBufferSizeType ObjectByteWriterWithPosition::Write(const IOBasicTypes::Byte* inBuffer,IOBasicTypes::LongBufferSizeType inBufferSize)
|
37 | {
|
38 | CREATE_ISOLATE_CONTEXT;
|
39 | CREATE_ESCAPABLE_SCOPE;
|
40 |
|
41 | Local<Object> anArray = NEW_ARRAY((int)inBufferSize);
|
42 | for(int i=0;i<(int)inBufferSize;++i)
|
43 | anArray->Set(GET_CURRENT_CONTEXT, NEW_NUMBER(i),NEW_NUMBER(inBuffer[i]));
|
44 |
|
45 | Local<Value> value = OBJECT_FROM_PERSISTENT(mObject)->Get(GET_CURRENT_CONTEXT, NEW_STRING("write")).ToLocalChecked();
|
46 | if(value->IsUndefined() || !value->IsFunction())
|
47 | {
|
48 | THROW_EXCEPTION("write is not a function, it should be you know...");
|
49 | return 0;
|
50 | }
|
51 | Local<Function> func = Local<Function>::Cast(value);
|
52 |
|
53 | Local<Value> args[1];
|
54 | args[0] = anArray;
|
55 |
|
56 | Local<Value> result = func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args).ToLocalChecked();
|
57 | if(result.IsEmpty())
|
58 | {
|
59 | THROW_EXCEPTION("wrong return value. it's empty. return the number of written characters");
|
60 | return 0;
|
61 | }
|
62 | else if(result->IsNumber())
|
63 | {
|
64 | return TO_UINT32(result)->Value();
|
65 | }
|
66 | else
|
67 | {
|
68 | THROW_EXCEPTION("wrong return value. write should return the number of written characters");
|
69 | return 0;
|
70 | }
|
71 | }
|
72 |
|
73 | IOBasicTypes::LongFilePositionType ObjectByteWriterWithPosition::GetCurrentPosition()
|
74 | {
|
75 | CREATE_ISOLATE_CONTEXT;
|
76 | CREATE_ESCAPABLE_SCOPE;
|
77 |
|
78 | Local<Value> value = OBJECT_FROM_PERSISTENT(mObject)->Get(GET_CURRENT_CONTEXT, NEW_STRING("getCurrentPosition")).ToLocalChecked();
|
79 | if(value->IsUndefined())
|
80 | return true;
|
81 | Local<Function> func = Local<Function>::Cast(value);
|
82 |
|
83 | return TO_NUMBER(func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 0, NULL).ToLocalChecked())->Value();
|
84 | } |
\ | No newline at end of file |