1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "fxjs/cjs_globalvariablearray.h"
8 
9 #include "fxjs/cjs_keyvalue.h"
10 
CJS_GlobalVariableArray()11 CJS_GlobalVariableArray::CJS_GlobalVariableArray() {}
12 
~CJS_GlobalVariableArray()13 CJS_GlobalVariableArray::~CJS_GlobalVariableArray() {}
14 
Copy(const CJS_GlobalVariableArray & array)15 void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array) {
16   m_Array.clear();
17   for (int i = 0, sz = array.Count(); i < sz; i++) {
18     CJS_KeyValue* pOldObjData = array.GetAt(i);
19     switch (pOldObjData->nType) {
20       case JS_GlobalDataType::NUMBER: {
21         CJS_KeyValue* pNewObjData = new CJS_KeyValue;
22         pNewObjData->sKey = pOldObjData->sKey;
23         pNewObjData->nType = pOldObjData->nType;
24         pNewObjData->dData = pOldObjData->dData;
25         Add(pNewObjData);
26       } break;
27       case JS_GlobalDataType::BOOLEAN: {
28         CJS_KeyValue* pNewObjData = new CJS_KeyValue;
29         pNewObjData->sKey = pOldObjData->sKey;
30         pNewObjData->nType = pOldObjData->nType;
31         pNewObjData->bData = pOldObjData->bData;
32         Add(pNewObjData);
33       } break;
34       case JS_GlobalDataType::STRING: {
35         CJS_KeyValue* pNewObjData = new CJS_KeyValue;
36         pNewObjData->sKey = pOldObjData->sKey;
37         pNewObjData->nType = pOldObjData->nType;
38         pNewObjData->sData = pOldObjData->sData;
39         Add(pNewObjData);
40       } break;
41       case JS_GlobalDataType::OBJECT: {
42         CJS_KeyValue* pNewObjData = new CJS_KeyValue;
43         pNewObjData->sKey = pOldObjData->sKey;
44         pNewObjData->nType = pOldObjData->nType;
45         pNewObjData->objData.Copy(pOldObjData->objData);
46         Add(pNewObjData);
47       } break;
48       case JS_GlobalDataType::NULLOBJ: {
49         CJS_KeyValue* pNewObjData = new CJS_KeyValue;
50         pNewObjData->sKey = pOldObjData->sKey;
51         pNewObjData->nType = pOldObjData->nType;
52         Add(pNewObjData);
53       } break;
54     }
55   }
56 }
57 
Add(CJS_KeyValue * p)58 void CJS_GlobalVariableArray::Add(CJS_KeyValue* p) {
59   m_Array.push_back(std::unique_ptr<CJS_KeyValue>(p));
60 }
61 
Count() const62 int CJS_GlobalVariableArray::Count() const {
63   return m_Array.size();
64 }
65 
GetAt(int index) const66 CJS_KeyValue* CJS_GlobalVariableArray::GetAt(int index) const {
67   return m_Array.at(index).get();
68 }
69