Gnash  0.8.11dev
AbcBlock.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 // The AS3 abc block format reader.
20 //
21 
22 #ifndef GNASH_ABC_BLOCK_H
23 #define GNASH_ABC_BLOCK_H
24 
25 #include "string_table.h"
26 #include "MultiName.h"
27 #include "Namespace.h"
28 #include "as_value.h"
29 
30 #include <vector>
31 #include <string>
32 #include <stdexcept>
33 
34 namespace gnash {
35  namespace abc {
36  class AbcBlock;
37  class Machine;
38  class Class;
39  class Method;
40  }
41  class SWFStream; // for read signature
42  class ClasstHierarchy;
43 }
44 
45 namespace gnash {
46 
48 namespace abc {
49 
51 //
56 //
59 //
68 //
71 class Trait
72 {
73 public:
74 
75  enum Kind
76  {
77  KIND_SLOT = 0,
78  KIND_CONST = 6,
79  KIND_METHOD = 1,
80  KIND_GETTER = 2,
81  KIND_SETTER = 3,
82  KIND_CLASS = 4,
83  KIND_FUNCTION = 5
84  };
85 
87  :
88  _hasValue(false),
89  _kind(KIND_SLOT),
90  _slotID(0),
91  _typeIndex(0),
92  _classInfoIndex(0),
93  _value(),
94  _name(0),
95  _globalName(),
96  _namespace(0),
97  _method(0),
98  _valueSet(false),
99  _classTarget(0),
100  _methodTarget(0),
101  _static(false)
102  {}
103 
104  bool read(SWFStream* in, AbcBlock *block);
105 
106  bool finalize(AbcBlock* block, abc::Class* cl, bool do_static);
107 
108  bool finalize_mbody(AbcBlock* block, Method* m);
109 
110  void set_target(abc::Class* cl, bool do_static) {
111  _classTarget = cl;
112  _static = do_static;
113  }
114 
115  void set_target(Method *m) {
116  _classTarget = 0;
117  _methodTarget = m;
118  }
119 
120  bool finalize(AbcBlock* block)
121  {
122  if (_classTarget) {
123  return finalize(block, _classTarget, _static);
124  }
125  return finalize_mbody(block, _methodTarget);
126  }
127 
128 private:
129 
130  friend class AbcBlock;
131 
132  bool _hasValue;
133  Kind _kind;
134  std::uint32_t _slotID;
135  std::uint32_t _typeIndex;
136  std::uint32_t _classInfoIndex;
137  as_value _value;
138 
139  URI _name;
140  string_table::key _globalName;
141 
142  Namespace* _namespace;
143  Method* _method;
144  bool _valueSet;
145 
146  abc::Class* _classTarget;
147  Method* _methodTarget;
148  bool _static;
149 
150 };
151 
153 std::ostream& operator<<(std::ostream& o, const Trait::Kind k);
154 
155 namespace {
156 
157 template<typename T>
158 inline void checkBounds(size_t i, const T& container)
159 {
160  if (i >= container.size()) {
161  throw std::range_error("Attempt to access pool out of range");
162  }
163 }
164 
165 }
166 
167 
169 //
173 //
176 //
185 //
187 //
195 //
197 //
202 //
208 class AbcBlock
209 {
210 public:
211 
213  {
214  PRIVATE_NS = 0x05,
215  CONSTANT_NS = 0x08,
216  PACKAGE_NS = 0x16,
217  PACKAGE_INTERNAL_NS = 0x17,
218  PROTECTED_NS = 0x18,
219  EXPLICIT_NS = 0x19,
220  STATIC_PROTECTED_NS = 0x1A
221  };
222 
224  {
225  METHOD_ARGS = 0x01,
226  METHOD_ACTIVATION = 0x02,
227  METHOD_MORE = 0x04,
228  METHOD_OPTIONAL_ARGS = 0x08,
229  METHOD_IGNORE = 0x10,
230  METHOD_NATIVE = 0x20,
231  METHOD_DEFAULT_NS = 0x40,
232  METHOD_ARG_NAMES = 0x80
233  };
234 
236  {
237  INSTANCE_SEALED = 0x01,
238  INSTANCE_FINAL = 0x02,
239  INSTANCE_INTERFACE = 0x04,
240  INSTANCE_DYNAMIC = 0x00,
241  INSTANCE_PROTECTED_NS = 0x08
242  };
243 
245  {
246  POOL_STRING = 0x01,
247  POOL_INTEGER = 0x03,
248  POOL_UINTEGER = 0x04,
249  POOL_DOUBLE = 0x06,
250  POOL_NAMESPACE = 0x08,
251  POOL_FALSE = 0x0A,
252  POOL_TRUE = 0x0B,
253  POOL_NULL = 0x0C
254  };
255 
256  typedef std::vector<Namespace*> NamespaceSet;
257 
258  AbcBlock();
259 
260  abc::Class* locateClass(MultiName &m);
261 
262  abc::Class* locateClass(const std::string& className);
263 
264  bool read(SWFStream& in);
265 
266  void update_global_name(unsigned int multiname_index);
267 
269  //
271  const std::vector<abc::Class*>& scripts() const {
272  return _scripts;
273  }
274 
275  std::uint32_t uIntegerPoolAt(size_t i) const {
276  checkBounds(i, _uIntegerPool);
277  return _uIntegerPool[i];
278  }
279 
280  const std::string& stringPoolAt(size_t i) const {
281  checkBounds(i, _stringPool);
282  return _stringPool[i];
283  }
284 
285  std::int32_t integerPoolAt(size_t i) const {
286  checkBounds(i, _integerPool);
287  return _integerPool[i];
288  }
289 
290  double doublePoolAt(size_t i) const {
291  checkBounds(i, _doublePool);
292  return _doublePool[i];
293  }
294 
295  Method* methodPoolAt(size_t i) const {
296  checkBounds(i, _methods);
297  return _methods[i];
298  }
299 
300  MultiName multinamePoolAt(size_t i) const {
301  checkBounds(i, _multinamePool);
302  return _multinamePool[i];
303  }
304 
305  abc::Class* classPoolAt(size_t i) const {
306  checkBounds(i, _classes);
307  return _classes[i];
308  }
309 
310  Namespace* namespacePoolAt(size_t i) const {
311  checkBounds(i, _namespacePool);
312  return _namespacePool[i];
313  }
314 
315  void prepare(Machine* mach);
316 
317 private:
318 
319  friend class abc::Trait;
320 
321  bool pool_value(std::uint32_t index, PoolConstant type, as_value &v);
322 
323  bool read_version();
324  bool read_integer_constants();
325  bool read_unsigned_integer_constants();
326  bool read_double_constants();
327  bool read_string_constants();
328  bool read_namespaces();
329  bool read_namespace_sets();
330  bool read_multinames();
331  bool read_method_infos();
332  bool skip_metadata();
333  bool read_instances();
334  bool read_classes();
335  bool read_scripts();
336  bool read_method_bodies();
337 
338  void check_multiname_name(std::uint32_t name);
339 
340  void check_multiname_namespace(std::uint32_t ns);
341 
342  void check_multiname_namespaceset(std::uint32_t nsset);
343 
344  void setMultinameNames(MultiName *n, abc::URI ABCName);
345 
346  void setNamespaceURI(Namespace *ns, abc::URI ABCName);
347 
348  std::vector<std::int32_t> _integerPool;
349  std::vector<std::uint32_t> _uIntegerPool;
350  std::vector<double> _doublePool;
351  std::vector<std::string> _stringPool;
352  std::vector<Namespace*> _namespacePool;
353  std::vector<NamespaceSet> _namespaceSetPool;
354  std::vector<Method*> _methods;
355  std::vector<MultiName> _multinamePool;
356  std::vector<Class*> _classes;
357  std::vector<Class*> _scripts;
358 
359  string_table* _stringTable;
360  SWFStream* _stream; // Not stored beyond one read.
361 
362  abc::Class *mTheObject;
363  ClassHierarchy *mCH;
364 
365  std::uint32_t mVersion;
366 
367 
368 };
369 
370 std::ostream& operator<<(std::ostream& o, AbcBlock::NamespaceConstant c);
371 std::ostream& operator<<(std::ostream& o, AbcBlock::MethodConstant c);
372 std::ostream& operator<<(std::ostream& o, AbcBlock::InstanceConstant c);
373 std::ostream& operator<<(std::ostream& o, AbcBlock::PoolConstant c);
374 
375 } // namespace abc
376 } // namespace gnash
377 
378 
379 #endif
380 
Trait()
Definition: AbcBlock.h:86
std::ostream & operator<<(std::ostream &o, const Trait::Kind k)
Output stream operator for abc::Trait::Kind.
Definition: AbcBlock.cpp:289
InstanceConstant
Definition: AbcBlock.h:235
size_t URI
This type should always be used for the index of AbcBlocks&#39; names.
Definition: MultiName.h:42
std::int32_t integerPoolAt(size_t i) const
Definition: AbcBlock.h:285
NamespaceConstant
Definition: AbcBlock.h:212
abc::Class * classPoolAt(size_t i) const
Definition: AbcBlock.h:305
The ActionScript bytecode of a single ABC tag in a SWF.
Definition: AbcBlock.h:208
Class describing a static property.
Definition: AbcBlock.h:71
void set_target(Method *m)
Definition: AbcBlock.h:115
Kind
Definition: AbcBlock.h:75
PoolConstant
Definition: AbcBlock.h:244
std::map< NPIdentifier, NPInvokeFunctionPtr > _methods
Definition: gshell.cpp:402
ActionScript value type.
Definition: as_value.h:94
void set_target(abc::Class *cl, bool do_static)
Definition: AbcBlock.h:110
Method * methodPoolAt(size_t i) const
Definition: AbcBlock.h:295
std::uint32_t uIntegerPoolAt(size_t i) const
Definition: AbcBlock.h:275
std::vector< Namespace * > NamespaceSet
Definition: AbcBlock.h:256
Definition: GnashKey.h:157
v
Definition: test.py:11
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
type
Definition: GnashKey.h:329
Represent an ActionScript Namespace.
Definition: Namespace.h:48
Definition: GnashKey.h:149
double doublePoolAt(size_t i) const
Definition: AbcBlock.h:290
Definition: GnashKey.h:161
const std::string & stringPoolAt(size_t i) const
Definition: AbcBlock.h:280
Register all of the ActionScript classes, with their dependencies.
Definition: ClassHierarchy.h:40
A general use string table.
Definition: string_table.h:41
Definition: GnashKey.h:160
Namespace * namespacePoolAt(size_t i) const
Definition: AbcBlock.h:310
A class to represent AS3 Classes.
Definition: Class.h:75
MultiName multinamePoolAt(size_t i) const
Definition: AbcBlock.h:300
The virtual machine for executing ABC (ActionScript Bytecode).
Definition: Machine.h:73
Definition: GnashKey.h:132
Definition: GnashKey.h:155
bool finalize(AbcBlock *block)
Definition: AbcBlock.h:120
An MultiName represents a single ABC multiname.
Definition: MultiName.h:51
Definition: GnashKey.h:159
const std::vector< abc::Class * > & scripts() const
Scripts can contain several classes.
Definition: AbcBlock.h:271
Definition: Method.h:53
std::size_t key
Definition: string_table.h:83
std::string name
Definition: LocalConnection_as.cpp:149
SWF stream wrapper class.
Definition: SWFStream.h:58
MethodConstant
Definition: AbcBlock.h:223