UNPKG

2.8 kBtext/x-cView Raw
1/*
2 Source File : ObjectByteWriterWithPosition.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 "ObjectByteWriterWithPosition.h"
21
22using namespace v8;
23
24ObjectByteWriterWithPosition::ObjectByteWriterWithPosition(Local<Object> inObject)
25{
26 CREATE_ISOLATE_CONTEXT;
27
28 SET_PERSISTENT_OBJECT(mObject, Object, inObject);
29}
30
31ObjectByteWriterWithPosition::~ObjectByteWriterWithPosition()
32{
33 DISPOSE_PERSISTENT(mObject);
34}
35
36IOBasicTypes::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
73IOBasicTypes::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