Gnash  0.8.11dev
Machine.h
Go to the documentation of this file.
1 // Machine.h A VM to run AS3 code, and AS2 code in the future.
2 //
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 #ifndef GNASH_MACHINE_H
21 #define GNASH_MACHINE_H
22 
23 #include <string>
24 #include <vector>
25 #include "SafeStack.h"
26 #include "as_value.h"
27 #include "log.h"
28 
29 namespace gnash {
30  namespace abc {
31  class AbcBlock;
32  class MultiName;
33  class Class;
34  class abc_function;
35  class Method;
36  class Namespace;
37  }
38  class Global_as;
39  class DisplayObject;
40  class as_object;
41  class Property;
42  class CodeStream;
43  class VM;
44  template <typename T> class FunctionArgs;
45  class string_table;
46 }
47 
48 
49 namespace gnash {
50 
51 namespace abc {
52 
54 //
73 class Machine
74 {
75 public:
76 
78  Machine(VM& vm);
79 
81  //
88  //
90  void init();
91 
92 
93  // Flash specific members.
95  DisplayObject *getTarget();
96 
99  void setTarget(DisplayObject* target);
100 
115  int completeName(MultiName& name, int initial = 0);
116 
127  Class* findSuper(as_value& obj, bool find_primitive);
128 
145  void getMember(Class* pDefinition, MultiName& name, as_value& source);
146 
163  void setMember(Class*, MultiName&, as_value& target, as_value& val);
164 
165  Property* findProperty(MultiName&) { return NULL; }
166 
167  void execute();
168 
188  void pushGet(as_object *this_obj, as_value& return_slot, Property *prop);
189 
204  void pushSet(as_object *this_obj, as_value& value, Property *prop);
205 
231  void pushCall(as_function *func, as_object *pThis, as_value& return_slot,
232  unsigned char stack_in, short stack_out);
233 
234  void immediateFunction(const as_function *to_call, as_object* pThis,
235  as_value& storage, unsigned char stack_in, short stack_out);
236 
237  void immediateProcedure(const as_function *to_call, as_object *pthis,
238  unsigned char stack_in, short stack_out) {
239  immediateFunction(to_call, pthis, mIgnoreReturn, stack_in, stack_out);
240  }
241 
242  void initMachine(AbcBlock* pool_block);
243 
244  as_value executeFunction(Method* function, const fn_call& fn);
245 
246  void instantiateClass(std::string className, as_object* global);
248  //
253  Global_as* global();
254 
255  void markReachableResources() const;
256 
257 private:
259  class State
260  {
261  public:
262  unsigned int _stackDepth;
263  unsigned int _stackTotalSize;
264  unsigned int _scopeStackDepth;
265  unsigned int mScopeTotalSize;
266  bool mReturn;
267  CodeStream *mStream;
268  Namespace *mDefaultXMLNamespace;
269  as_object *mCurrentScope;
270  as_value *mGlobalReturn;
271  as_object *mThis;
272  std::vector<as_value> _registers;
273  abc_function* mFunction;
274  void to_debug_string(){
275  log_abc("StackDepth=%u StackTotalSize=%u ScopeStackDepth=%u ScopeTotalSize=%u",_stackDepth,_stackTotalSize,_scopeStackDepth,mScopeTotalSize);
276 
277  }
278  };
279 
280  class Scope
281  {
282  public:
283  unsigned int mHeightAfterPop;
284  as_object *mScope;
285 
286  Scope() : mHeightAfterPop(0), mScope(NULL) {}
287  Scope(unsigned int i, as_object *o) : mHeightAfterPop(i),
288  mScope(o)
289  {}
290  };
291 
292  void saveState();
293  void restoreState();
294 
295  as_value find_prop_strict(MultiName multiname);
296 
297  void print_stack();
298 
299  void print_scope_stack();
300 
301  void get_args(size_t argc, FunctionArgs<as_value>& args);
302 
303  void load_function(CodeStream* stream, std::uint32_t maxRegisters);
304 
305  void executeCodeblock(CodeStream* stream);
306 
307  void clearRegisters(std::uint32_t maxRegsiters);
308 
309  const as_value& getRegister(int index){
310  log_abc("Getting value at a register %d ", index);
311  return _registers[index];
312  }
313 
314  void setRegister(size_t index, const as_value& val) {
315  log_abc("Putting %s in register %s", val, index);
316  if (_registers.size() <= index) {
317  log_abc("Register doesn't exist! Adding new registers!");
318  _registers.resize(index + 1);
319  }
320  _registers[index] = val;
321  }
322 
323  void push_stack(as_value object){
324  log_abc("Pushing value %s onto stack.", object);
325  _stack.push(object);
326  }
327 
328  as_value pop_stack(){
329  as_value value = _stack.pop();
330  log_abc("Popping value %s off the stack.", value);
331  return value;
332  }
333 
334  void push_scope_stack(as_value object);
335 
336  as_object* pop_scope_stack() {
337  log_abc("Popping value %s off the scope stack. There will be "
338  "%u items left.", as_value(_scopeStack.top(0)),
339  _scopeStack.size()-1);
340  return _scopeStack.pop();
341  }
342 
343  as_object* get_scope_stack(std::uint8_t depth) const {
344  log_abc("Getting value from scope stack %u from the bottom.",
345  depth | 0x0);
346  return _scopeStack.value(depth);
347  }
348 
349  SafeStack<as_value> _stack;
350  SafeStack<State> mStateStack;
351  std::vector<as_value> _registers;
352 
354  //
361  SafeStack<as_object*> _scopeStack;
362 
363  CodeStream *mStream;
364 
365  string_table& mST;
366 
367  Namespace* mDefaultXMLNamespace;
368  as_object* mCurrentScope;
369  as_object* mGlobalScope;
370  as_object* mDefaultThis;
371  as_object* mThis;
372 
374  Global_as* _global;
375 
376  as_value mGlobalReturn;
377  as_value mIgnoreReturn; // Throw away returns go here.
378 
379  bool mExitWithReturn;
380  AbcBlock* mPoolObject; // Where all of the pools are stored.
381 
382  abc_function* mCurrentFunction;
383 
384  VM& _vm;
385 };
386 } // namespace abc
387 } // namespace gnash
388 #endif
The ActionScript bytecode of a single ABC tag in a SWF.
Definition: AbcBlock.h:208
Definition: klash_part.cpp:329
Property * findProperty(MultiName &)
Definition: Machine.h:165
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
ActionScript value type.
Definition: as_value.h:94
An abstract property.
Definition: Property.h:276
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
Represent an ActionScript Namespace.
Definition: Namespace.h:48
Definition: SafeStack.h:41
The base class for all ActionScript objects.
Definition: as_object.h:161
Definition: GnashKey.h:161
A general use string table.
Definition: string_table.h:41
as_value getMember(as_object &o, const ObjectURI &uri)
Get a member of an object using AS lookup rules.
Definition: as_object.h:756
Definition: CodeStream.h:40
A class to represent AS3 Classes.
Definition: Class.h:75
A class to contain transferable arguments for a fn_call.
Definition: as_function.h:30
void log_abc(StringType msg, Args... args)
Definition: log.h:337
void immediateProcedure(const as_function *to_call, as_object *pthis, unsigned char stack_in, short stack_out)
Definition: Machine.h:237
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:49
The virtual machine for executing ABC (ActionScript Bytecode).
Definition: Machine.h:73
The AVM1 virtual machine.
Definition: VM.h:71
Definition: GnashKey.h:155
An MultiName represents a single ABC multiname.
Definition: MultiName.h:51
ABC-defined Function.
Definition: abc_function.h:40
Definition: Method.h:53
Parameters/environment for builtin or user-defined functions callable from ActionScript.
Definition: fn_call.h:117
std::string name
Definition: LocalConnection_as.cpp:149
ActionScript Function, either builtin or SWF-defined.
Definition: as_function.h:62