xoreos  0.0.5
object.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/weak_ptr.hpp>
26 
27 #include "src/common/error.h"
28 
32 
33 namespace Aurora {
34 
35 namespace ActionScript {
36 
38 }
39 
41  _members = object->_members;
42 }
43 
45 }
46 
47 std::vector<Common::UString> Object::getSlots() const {
48  std::vector<Common::UString> slots;
49  for (std::map<Common::UString, Variable>::const_iterator iter = _members.begin(); iter != _members.end() ; iter++) {
50  slots.push_back(iter->first);
51  }
52  return slots;
53 }
54 
56  return _members.find(id) != _members.end();
57 }
58 
60  if (!id.isString())
61  throw Common::Exception("Object::getMember id is not a string");
62 
63  const Common::UString idString = id.asString();
64  if (_members.find(idString) != _members.end())
65  return _members[idString];
66  else {
67  _members.insert(std::make_pair(idString, ObjectPtr(new Object)));
68  return _members[idString];
69  }
70 }
71 
72 void Object::setMember(const Variable &id, const Variable &value) {
73  if (!id.isString())
74  throw Common::Exception("Object::setMember id is not a string");
75 
76  _members[id.asString()] = value;
77 }
78 
79 void Object::setMember(const Common::UString &id, Function *function) {
80  _members[id] = ObjectPtr(function);
81 }
82 
83 Variable Object::call(const Common::UString &function, AVM &avm, const std::vector<Variable> &arguments) {
84  if (!hasMember(function))
85  throw Common::Exception("object has no member %s", function.c_str());
86 
87  if (!getMember(function).isFunction())
88  throw Common::Exception("%s is no method", function.c_str());
89 
90  Function *f = reinterpret_cast<Function *>(getMember(function).asObject().get());
91 
92  byte counter = 1;
93  if (f->getPreloadRootFlag()) {
94  avm.storeRegister(avm.getVariable("_root"), counter);
95  counter += 1;
96  }
97 
98  if (f->getPreloadThisFlag()) {
99  avm.storeRegister(shared_from_this(), counter);
100  counter += 1;
101  }
102 
103  for (size_t i = 0; i < arguments.size(); ++i) {
104  avm.storeRegister(arguments[i], counter);
105  counter += 1;
106  }
107 
108  avm.setReturnValue();
109  (*f)(avm);
110  return avm.getReturnValue();
111 }
112 
113 } // End of namespace ActionScript
114 
115 } // End of namespace Aurora
void setReturnValue(Variable returnValue=Variable())
Definition: avm.cpp:156
A class holding an UTF-8 string.
Definition: ustring.h:48
void storeRegister(Variable value, byte index)
Definition: avm.cpp:69
Variable getVariable(const Common::UString &name)
Definition: avm.cpp:96
Context for executing ActionScript.
std::vector< Common::UString > getSlots() const
Definition: object.cpp:47
Basic exceptions to throw.
bool hasMember(const Common::UString &id)
Definition: object.cpp:55
virtual void setMember(const Variable &id, const Variable &value)
Definition: object.cpp:72
Function objects for ActionScript.
Variable call(const Common::UString &function, AVM &avm, const std::vector< Variable > &arguments=std::vector< Variable >())
Definition: object.cpp:83
StackException Exception
Definition: error.h:59
Abstract object which is inherited by every other class.
An action script variable.
Definition: variable.h:44
virtual Variable getMember(const Variable &id)
Definition: object.cpp:59
Variable getReturnValue()
Definition: avm.cpp:160
std::map< Common::UString, Variable > _members
Definition: object.h:64
boost::shared_ptr< Object > ObjectPtr
Definition: object.h:43
uint8 byte
Definition: types.h:209
The Action script virtual machine (AVM).
Definition: avm.h:46