FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
JSFakeArray.cpp
1 /**********************************************************\
2 Original Author: Georg Fritzsche
3 
4 Created: December 13, 2009
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2009 Georg Fritzsche, Firebreath development team
13 \**********************************************************/
14 
15 #include <iterator>
16 #include "precompiled_headers.h"
17 
18 #include "JSFakeArray.h"
19 
20 FB::JSFakeArray::JSFakeArray()
21  : m_values()
22 {
23  RegisterMethods();
24 }
25 
26 FB::JSFakeArray::JSFakeArray(const FB::JSObjectPtr& obj)
27  : m_values()
28 {
29  if(!obj->HasProperty("length"))
30  throw FB::script_error("object does not have property length");
31 
32  ExtractList(obj);
33  RegisterMethods();
34 }
35 
36 FB::JSFakeArray::JSFakeArray(const FB::VariantList& values)
37 {
38  m_values = values;
39  RegisterMethods();
40 }
41 
42 void FB::JSFakeArray::RegisterMethods()
43 {
44  registerProperty("length", make_property(this, &JSFakeArray::GetLength));
45 }
46 
47 void FB::JSFakeArray::ExtractList(const FB::JSObjectPtr& obj)
48 {
49  try
50  {
51  int length = obj->GetProperty("length").convert_cast<int>();
52  std::back_insert_iterator<FB::VariantList> inserter(m_values);
53 
54  for(int i=0; i<length; ++i) {
55  inserter = obj->GetProperty(i);
56  }
57  }
58  catch(const FB::script_error& e)
59  {
60  throw e;
61  }
62 }
63 
64 size_t FB::JSFakeArray::GetLength() const
65 {
66  return m_values.size();
67 }
68 
70 {
71  return (*this)[index];
72 }
73 
74 FB::variant& FB::JSFakeArray::operator[](size_t index)
75 {
76  return m_values[index];
77 }
78 
79 const FB::variant& FB::JSFakeArray::operator[](size_t index) const
80 {
81  return m_values[index];
82 }
83 
84 FB::VariantList& FB::JSFakeArray::Values()
85 {
86  return m_values;
87 }
88 
89 const FB::VariantList& FB::JSFakeArray::Values() const
90 {
91  return m_values;
92 }
93 
boost::shared_ptr< FB::JSObject > JSObjectPtr
Defines an alias representing a JSObject shared_ptr (you should never use a JSObject* directly) ...
Definition: APITypes.h:109
PropertyFunctors make_property(C *instance, F1 getter, F2 setter)
Generate read-write property functors for use with registerProperty() of FB::JSAPIAuto.
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
std::vector< variant > VariantList
Defines an alias representing list of variants.
Definition: APITypes.h:64
Exception type; when thrown in a JSAPI method, a javascript exception will be thrown.
Definition: JSExceptions.h:28
virtual variant GetProperty(const std::string &propertyName)
Gets a property value.
Definition: JSAPIAuto.cpp:191