xoreos  0.0.5
array.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <boost/bind.hpp>
26 
28 
29 namespace Aurora {
30 
31 namespace ActionScript {
32 
33 static Variable as_push(AVM &avm){
34  ArrayPtr array = avm.getRegister(1).as<Array>();
35  if (!array)
36  throw Common::Exception("Array::pop this is not an Array object");
37  array->push(avm.getRegister(2));
38  return Variable();
39 }
40 
41 static Variable as_pop(AVM &avm) {
42  ArrayPtr array = avm.getRegister(1).as<Array>();
43  if (!array)
44  throw Common::Exception("Array::pop this is not an Array object");
45  return array->pop();
46 }
47 
48 Array::Array(const std::list<Variable> &values) : _values(values) {
49  setMember("push", new NativeFunction(boost::bind(as_push, _1), true, false, false));
50  setMember("pop", new NativeFunction(boost::bind(as_pop, _1), true, false, false));
51 }
52 
53 size_t Array::length() const {
54  return _values.size();
55 }
56 
57 void Array::push(const Variable &v) {
58  _values.push_back(v);
59 }
60 
62  Variable v = _values.back();
63  _values.pop_back();
64  return v;
65 }
66 
68  if (id.isNumber()) {
69  std::list<Variable>::iterator iter = _values.begin();
70  std::advance(iter, static_cast<size_t>(id.asNumber()));
71  return *iter;
72  }
73 
74  if (id.isString() && id.asString() == "length")
75  return Variable((unsigned long)_values.size());
76 
77  return Object::getMember(id);
78 }
79 
80 void Array::setMember(const Variable &id, const Variable &value) {
81  if (id.isNumber()) {
82  std::list<Variable>::iterator iter = _values.begin();
83  std::advance(iter, static_cast<size_t>(id.asNumber()));
84  *iter = value;
85  return;
86  }
87 
88  Object::setMember(id, value);
89 }
90 
91 } // End of namespace ActionScript
92 
93 } // End of namespace Aurora
void setMember(const Variable &id, const Variable &value)
Definition: array.cpp:80
static Variable as_push(AVM &avm)
Definition: array.cpp:33
Array(const std::list< Variable > &values=std::list< Variable >())
Definition: array.cpp:48
static Variable as_pop(AVM &avm)
Definition: array.cpp:41
std::list< Variable > _values
Definition: array.h:54
Implementation for an actionscript Array class.
virtual void setMember(const Variable &id, const Variable &value)
Definition: object.cpp:72
void push(const Variable &v)
Definition: array.cpp:57
boost::shared_ptr< T > as() const
Definition: variable.h:105
StackException Exception
Definition: error.h:59
size_t length() const
Definition: array.cpp:53
Variable getMember(const Variable &id)
Definition: array.cpp:67
An action script variable.
Definition: variable.h:44
virtual Variable getMember(const Variable &id)
Definition: object.cpp:59
boost::shared_ptr< Array > ArrayPtr
Definition: array.h:37
Variable getRegister(byte index)
Definition: avm.cpp:73
The Action script virtual machine (AVM).
Definition: avm.h:46