Gnash  0.8.11dev
VM.h
Go to the documentation of this file.
1 // VM.h: the Virtual Machine class, for Gnash
2 //
3 // Copyright (C) 2005, 2006, 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_VM_H
21 #define GNASH_VM_H
22 
23 #ifdef HAVE_CONFIG_H
24 #include "gnashconfig.h"
25 #endif
26 
27 #include <map>
28 #include <memory>
29 #include <array>
30 #include <cstdint>
31 #include <boost/random/mersenne_twister.hpp> // for mt11213b
32 #include <boost/noncopyable.hpp>
33 
34 #include "string_table.h"
35 #include "SafeStack.h"
36 #include "CallStack.h"
37 #include "as_value.h"
38 #include "namedStrings.h"
39 #include "ObjectURI.h"
40 #include "ConstantPool.h"
41 #include "dsodefs.h"
42 #include "utility.h" // for UNUSED
43 
44 // Forward declarations
45 namespace gnash {
46  class Global_as;
47  class fn_call;
48  class movie_root;
49  class NativeFunction;
50  class SharedObjectLibrary;
51  class as_object;
52  class VirtualClock;
53  class UserFunction;
54 }
55 
56 namespace gnash {
57 
59 //
62 //
66 //
68 //
71 class DSOEXPORT VM : boost::noncopyable
72 {
73 public:
74 
75  typedef as_value (*as_c_function_ptr)(const fn_call& fn);
76 
78  //
81  VM(movie_root& root, VirtualClock& clock);
82 
83  ~VM();
84 
86  //
89  return _stack;
90  }
91 
93  //
99  return _clock;
100  }
101 
103  //
106  int getSWFVersion() const {
107  return _swfversion;
108  }
109 
111  void setSWFVersion(int v);
112 
114  unsigned long int getTime() const;
115 
117  string_table& getStringTable() const { return _stringTable; }
118 
120  //
124  const std::string& getPlayerVersion() const;
125 
130  std::string getOSName() const;
131 
135  std::string getSystemLanguage() const;
136 
137  // The boost Random Number Generator to use.
138  //
139  // http://www.boost.org/libs/random/random-generators.html
140  //
141  // TODO: boost/nondet_random.hpp provides access to a random device,
142  // which can be used in preference to a pseudo-RNG. It is only
143  // presently available on some platforms.
144  // http://www.boost.org/libs/random/nondet_random.html
145  //
146  // Generators have different limits on the size of the seed. Please
147  // check if replacing the generator.
148  //
149  // The mt11213b provides a pseudo-random number cycle
150  // of length 2^11213-1 and requires approx 352*sizeof(uint32_t) memory
151  // once initialized. It is more than adequate for most purposes.
152  typedef boost::mt11213b RNG;
153 
154  // Get a pointer to the random number generator for
155  // use by Math.random() and random().
156  RNG& randomNumberGenerator();
157 
159  movie_root& getRoot() const;
160 
163  assert(_shLib.get());
164  return *_shLib;
165  }
166 
168  Global_as* getGlobal() const;
169 
171  //
175  void markReachableResources() const;
176 
177  void registerNative(as_c_function_ptr fun, unsigned int x, unsigned int y);
178 
180  NativeFunction* getNative(unsigned int x, unsigned int y) const;
181 
183  //
195  //
199  const as_value* getRegister(size_t index);
200 
202  //
218  void setRegister(size_t index, const as_value& val);
219 
221  //
224  //
226  CallFrame& pushCallFrame(UserFunction& f);
227 
229  //
231  void popCallFrame();
232 
234  //
237  CallFrame& currentCall();
238 
240  bool calling() const {
241  return !_callStack.empty();
242  }
243 
245  void dumpState(std::ostream& o, size_t limit = 0);
246 
247  void setConstantPool(const ConstantPool *pool) { _constantPool = pool; }
248 
249  const ConstantPool *getConstantPool() const { return _constantPool; }
250 
251 private:
252 
254  movie_root& _rootMovie;
255 
257  Global_as* _global;
258 
260  int _swfversion;
261 
262  typedef std::map<unsigned int, as_c_function_ptr> FuncMap;
263  typedef std::map<unsigned int, FuncMap> AsNativeTable;
264  AsNativeTable _asNativeTable;
265 
267  mutable string_table _stringTable;
268 
269  VirtualClock& _clock;
270 
271  SafeStack<as_value> _stack;
272 
273  typedef std::array<as_value, 4> GlobalRegisters;
274  GlobalRegisters _globalRegisters;
275 
276  CallStack _callStack;
277 
279  std::unique_ptr<SharedObjectLibrary> _shLib;
280 
281  RNG _rng;
282 
283  const ConstantPool* _constantPool;
284 };
285 
286 // @param lowerCaseHint if true the caller guarantees
287 // that the lowercase equivalent of `str' is `str' again
288 //
289 inline ObjectURI
290 getURI(const VM& vm, const std::string& str, bool lowerCaseHint=false)
291 {
292  UNUSED(lowerCaseHint); // TODO pass hint to ObjectURI ctor
293  // Possible optimization here is to directly compute
294  // noCase value if VM version is < 7
295  return ObjectURI((NSV::NamedStrings)vm.getStringTable().find(str));
296 }
297 
298 inline ObjectURI
300 {
301  // Possible optimization here is to directly
302  // compute noCase values if VM version is < 7
303  // (using the known information in NSV)
304  return ObjectURI(s);
305 }
306 
307 inline const std::string&
308 toString(VM& vm, const ObjectURI& uri)
309 {
310  return uri.toString(vm.getStringTable());
311 }
312 
313 
318 {
319 public:
320 
322  :
323  _vm(vm),
324  _callFrame(_vm.pushCallFrame(func))
325  {
326  }
327 
330  return _callFrame;
331  }
332 
334  _vm.popCallFrame();
335  }
336 
337 private:
338  VM& _vm;
339  CallFrame& _callFrame;
340 };
341 
355 
357 //
361 //
365 void newAdd(as_value& op1, const as_value& op2, const VM& vm);
366 
368 //
372 void subtract(as_value& op1, const as_value& op2, const VM& vm);
373 
375 //
379 as_value newLessThan(const as_value& op1, const as_value& op2, const VM& vm);
380 
382 //
387 //
389 //
394 bool equals(const as_value& a, const as_value& b, const VM& vm);
395 
397 //
401 bool toBool(const as_value& v, const VM& vm);
402 
404 //
408 DSOTEXPORT double toNumber(const as_value& v, const VM& vm);
409 
411 //
415 as_object* toObject(const as_value& v, VM& vm);
416 
418 //
422 //
424 //
428 std::int32_t toInt(const as_value& val, const VM& vm);
429 
431 //
435 as_value& convertToNumber(as_value& v, const VM& vm);
436 
438 //
442 as_value& convertToString(as_value& v, const VM& vm);
443 
445 //
449 as_value& convertToBoolean(as_value& v, const VM& vm);
450 
452 //
456 as_value& convertToPrimitive(as_value& v, const VM& vm);
457 
458 } // namespace gnash
459 
460 #endif
461 
462 // Local Variables:
463 // mode: C++
464 // indent-tabs-mode: t
465 // End:
Definition: GnashKey.h:147
double toNumber(const as_value &v, const VM &vm)
Convert an as_value to a double.
Definition: VM.cpp:451
as_object * toObject(const as_value &v, VM &vm)
Convert an as_value to an object.
Definition: VM.cpp:457
FrameGuard(VM &vm, UserFunction &func)
Definition: VM.h:321
SharedObjectLibrary & getSharedObjectLibrary() const
Return the Shared Object Library.
Definition: VM.h:162
A class used to virtualize time flow.
Definition: VirtualClock.h:33
bool toBool(const as_value &v, const VM &vm)
Convert an as_value to boolean type.
Definition: VM.cpp:445
ActionScript value type.
Definition: as_value.h:94
void setConstantPool(const ConstantPool *pool)
Definition: VM.h:247
#define UNUSED(x)
Definition: utility.h:113
string_table & getStringTable() const
Get a reference to the string table used by the VM.
Definition: VM.h:117
CallFrame & callFrame()
Get the CallFrame we&#39;ve just pushed.
Definition: VM.h:329
Global_as & getGlobal(const as_environment &env)
Definition: as_environment.cpp:651
A CallFrame is an element of a CallStack.
Definition: CallStack.h:43
as_value & convertToPrimitive(as_value &v, const VM &vm)
Convert to the appropriate primitive type.
Definition: VM.cpp:508
uri
Definition: test.py:12
v
Definition: test.py:11
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
void newAdd(as_value &op1, const as_value &op2, const VM &vm)
Carry out ActionNewAdd.
Definition: VM.cpp:356
as_value(* as_c_function_ptr)(const fn_call &fn)
Definition: Property.h:34
Definition: SafeStack.h:41
as_value newLessThan(const as_value &op1, const as_value &op2, const VM &vm)
Carry out ActionSubtract.
Definition: VM.cpp:400
Definition: GnashKey.h:152
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
boost::mt11213b RNG
Definition: VM.h:152
bool calling() const
Whether a function call is in progress.
Definition: VM.h:240
Definition: VM.h:317
A URI for describing as_objects.
Definition: ObjectURI.h:44
NamedStrings
Definition: namedStrings.h:57
Definition: SharedObject_as.h:36
VirtualClock & getClock()
Get the VM clock.
Definition: VM.h:98
const std::string & toString(VM &vm, const ObjectURI &uri)
Definition: VM.h:308
ObjectURI getURI(const VM &vm, const std::string &str, bool lowerCaseHint=false)
Definition: VM.h:290
as_value & convertToBoolean(as_value &v, const VM &vm)
Force type to bool.
Definition: VM.cpp:501
~FrameGuard()
Definition: VM.h:333
A UserFunction is a callable function defined in ActionScript.
Definition: UserFunction.h:37
std::int32_t x
Definition: BitmapData_as.cpp:434
bool equals(const as_value &a, const as_value &b, const VM &vm)
Check if two values are equal.
Definition: VM.cpp:439
void subtract(as_value &op1, const as_value &op2, const VM &vm)
Carry out ActionSubtract.
Definition: VM.cpp:392
Definition: GnashKey.h:148
#define DSOEXPORT
Definition: dsodefs.h:55
as_value & convertToString(as_value &v, const VM &vm)
Force type to string.
Definition: VM.cpp:493
This class implements functions native to the player.
Definition: NativeFunction.h:40
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:49
as_value & convertToNumber(as_value &v, const VM &vm)
Force type to number.
Definition: VM.cpp:485
int getSWFVersion() const
Get SWF version context for the currently running actions.
Definition: VM.h:106
The AVM1 virtual machine.
Definition: VM.h:71
std::vector< CallFrame > CallStack
Definition: CallStack.h:146
std::int32_t y
Definition: BitmapData_as.cpp:435
SafeStack< as_value > & getStack()
Accessor for the VM&#39;s stack.
Definition: VM.h:88
#define DSOTEXPORT
Definition: dsodefs.h:63
key find(const std::string &to_find, bool insert_unfound=true)
Find a key for a string.
Definition: string_table.cpp:40
const std::string & toString(string_table &st) const
Definition: ObjectURI.h:80
Definition: GnashKey.h:165
Parameters/environment for builtin or user-defined functions callable from ActionScript.
Definition: fn_call.h:117
movie_root & getRoot(const as_environment &env)
Definition: as_environment.cpp:645
std::vector< const char * > ConstantPool
An indexed list of strings (must match the definition in action_buffer.h)
Definition: ConstantPool.h:27
const ConstantPool * getConstantPool() const
Definition: VM.h:249
std::int32_t toInt(const as_value &v, const VM &vm)
AS2-compatible conversion to 32bit integer.
Definition: VM.cpp:463
This class represents the &#39;Stage&#39; and top-level movie.
Definition: movie_root.h:150