UNPKG

3.7 kBtext/x-cView Raw
1/*
2 Source File : PDFArrayDriver.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 "PDFArrayDriver.h"
21#include "RefCountPtr.h"
22#include "ConstructorsHolder.h"
23
24using namespace v8;
25
26
27
28DEF_SUBORDINATE_INIT(PDFArrayDriver::Init)
29{
30 CREATE_ISOLATE_CONTEXT;
31
32 Local<FunctionTemplate> t = NEW_FUNCTION_TEMPLATE_EXTERNAL(New);
33
34 t->SetClassName(NEW_STRING("PDFArray"));
35 t->InstanceTemplate()->SetInternalFieldCount(1);
36
37 SET_PROTOTYPE_METHOD(t, "toJSArray", ToJSArray);
38 SET_PROTOTYPE_METHOD(t, "queryObject", QueryObject);
39 SET_PROTOTYPE_METHOD(t, "getLength", GetLength);
40 PDFObjectDriver::Init(t);
41
42 // save in factory
43 EXPOSE_EXTERNAL_FOR_INIT(ConstructorsHolder, holder)
44 SET_CONSTRUCTOR(holder->PDFArray_constructor, t);
45 SET_CONSTRUCTOR_TEMPLATE(holder->PDFArray_constructor_template, t);
46}
47
48METHOD_RETURN_TYPE PDFArrayDriver::New(const ARGS_TYPE& args)
49{
50 CREATE_ISOLATE_CONTEXT;
51 CREATE_ESCAPABLE_SCOPE;
52 EXPOSE_EXTERNAL_ARGS(ConstructorsHolder, externalHolder)
53
54 PDFArrayDriver* array = new PDFArrayDriver();
55 array->holder = externalHolder;
56 array->Wrap(args.This());
57 SET_FUNCTION_RETURN_VALUE(args.This())
58}
59
60PDFObject* PDFArrayDriver::GetObject()
61{
62 return TheObject.GetPtr();
63}
64
65METHOD_RETURN_TYPE PDFArrayDriver::ToJSArray(const ARGS_TYPE& args)
66{
67 CREATE_ISOLATE_CONTEXT;
68 CREATE_ESCAPABLE_SCOPE;
69
70 PDFArrayDriver* arrayDriver = ObjectWrap::Unwrap<PDFArrayDriver>(args.This());
71
72 Local<Array> result = NEW_ARRAY((int)arrayDriver->TheObject->GetLength());
73
74 for(unsigned long i=0; i < arrayDriver->TheObject->GetLength();++i)
75 {
76 RefCountPtr<PDFObject> anObject(arrayDriver->TheObject->QueryObject(i));
77 result->Set(GET_CURRENT_CONTEXT, NEW_NUMBER(i),arrayDriver->holder->GetInstanceFor(anObject.GetPtr()));
78 }
79
80 SET_FUNCTION_RETURN_VALUE(result)
81}
82
83METHOD_RETURN_TYPE PDFArrayDriver::GetLength(const ARGS_TYPE& args)
84{
85 CREATE_ISOLATE_CONTEXT;
86 CREATE_ESCAPABLE_SCOPE;
87
88 PDFArrayDriver* arrayDriver = ObjectWrap::Unwrap<PDFArrayDriver>(args.This());
89
90 Local<Number> result = NEW_NUMBER(arrayDriver->TheObject->GetLength());
91
92 SET_FUNCTION_RETURN_VALUE(result)
93}
94
95METHOD_RETURN_TYPE PDFArrayDriver::QueryObject(const ARGS_TYPE& args)
96{
97 CREATE_ISOLATE_CONTEXT;
98 CREATE_ESCAPABLE_SCOPE;
99
100 if(args.Length() != 1 || !args[0]->IsNumber())
101 {
102 THROW_EXCEPTION("wrong arguments, pass 1 argument which is an index in the array");
103 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
104
105 }
106
107 PDFArrayDriver* arrayDriver = ObjectWrap::Unwrap<PDFArrayDriver>(args.This());
108 if(TO_UINT32(args[0])->Value() >= arrayDriver->TheObject->GetLength())
109 {
110 THROW_EXCEPTION("wrong arguments, pass 1 argument which is a valid index in the array");
111 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
112 }
113
114 RefCountPtr<PDFObject> anObject = arrayDriver->TheObject->QueryObject(TO_UINT32(args[0])->Value());
115 Local<Value> result = arrayDriver->holder->GetInstanceFor(anObject.GetPtr());
116
117 SET_FUNCTION_RETURN_VALUE(result)
118}
\No newline at end of file