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:
gnash::VM::setConstantPool
void setConstantPool(const ConstantPool *pool)
Definition: VM.h:247
SharedObject_as.h
gnash::VM::getConstantPool
const ConstantPool * getConstantPool() const
Definition: VM.h:249
gnash::VM::getSharedObjectLibrary
SharedObjectLibrary & getSharedObjectLibrary() const
Return the Shared Object Library.
Definition: VM.h:162
gnash::toNumber
double toNumber(const as_value &v, const VM &vm)
Convert an as_value to a double.
Definition: VM.cpp:451
Movie.h
movie_root.h
string_table.h
gnash::VM::setSWFVersion
void setSWFVersion(int v)
Set SWF version of the currently executing code.
Definition: VM.cpp:74
gnash::isNaN
bool isNaN(const T &num)
Definition: GnashNumeric.h:62
gnash::CallStack
std::vector< CallFrame > CallStack
Definition: CallStack.h:146
gnash::toBool
bool toBool(const as_value &v, const VM &vm)
Convert an as_value to boolean type.
Definition: VM.cpp:445
gnash::toInt
std::int32_t toInt(const as_value &v, const VM &vm)
AS2-compatible conversion to 32bit integer.
Definition: VM.cpp:463
gnash::key::d
@ d
Definition: GnashKey.h:150
ObjectURI.h
gnash::getOwnProperty
as_value getOwnProperty(as_object &o, const ObjectURI &uri)
Get an own member of an object.
Definition: as_object.h:777
gnash::toObject
as_object * toObject(const as_value &v, VM &vm)
Convert an as_value to an object.
Definition: VM.cpp:457
gnash::log_action
void log_action(StringType msg, Args... args)
Definition: log.h:307
gnash::string_table
A general use string table.
Definition: string_table.h:42
gnash::VirtualClock
A class used to virtualize time flow.
Definition: VirtualClock.h:34
dsodefs.h
gnash::VM::getStringTable
string_table & getStringTable() const
Get a reference to the string table used by the VM.
Definition: VM.h:117
gnash::FrameGuard::callFrame
CallFrame & callFrame()
Get the CallFrame we've just pushed.
Definition: VM.h:329
gnash::log_debug
void log_debug(StringType msg, Args... args)
Definition: log.h:301
y
std::int32_t y
Definition: BitmapData_as.cpp:435
gnash::key::i
@ i
Definition: GnashKey.h:155
VirtualClock.h
gnash::NSV::NamedStrings
NamedStrings
Definition: namedStrings.h:57
rc.h
CallStack.h
gnash::NSV::CLASS_FUNCTION
@ CLASS_FUNCTION
Definition: namedStrings.h:216
_
#define _(String)
Definition: log.h:44
gnash::VM::getRoot
movie_root & getRoot() const
Get a pointer to this VM's Root movie (stage)
Definition: VM.cpp:143
gnash::VM
The AVM1 virtual machine.
Definition: VM.h:72
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::VM::getGlobal
Global_as * getGlobal() const
Get a pointer to this VM's _global Object.
Definition: VM.cpp:149
gnash::VM::popCallFrame
void popCallFrame()
Remove a function call from the call frame.
Definition: VM.cpp:261
utility.h
gnash::key::s
@ s
Definition: GnashKey.h:165
as_value.h
NativeFunction.h
gnash::key::n
@ n
Definition: GnashKey.h:160
gnash::as_value::set_double
void set_double(double val)
Set to a primitive number.
Definition: as_value.cpp:739
gnash::as_value::AsType
AsType
Definition: as_value.h:101
Global_as.h
gnash::getGlobal
Global_as & getGlobal(const as_environment &env)
Definition: as_environment.cpp:651
gnash::as_value::to_primitive
as_value to_primitive(AsType hint) const
Return value as a primitive type, with a preference.
Definition: as_value.cpp:263
gnash::NSV::PROP_CONSTRUCTOR
@ PROP_CONSTRUCTOR
Definition: namedStrings.h:68
gnash::FrameGuard::~FrameGuard
~FrameGuard()
Definition: VM.h:333
gnash::CallFrame::setLocalRegister
void setLocalRegister(size_t i, const as_value &val)
Set a specific register in this CallFrame.
Definition: CallStack.cpp:57
gnash::VM::getPlayerVersion
const std::string & getPlayerVersion() const
Get version of the player, in a compatible representation.
Definition: VM.cpp:86
gnash::convertToBoolean
as_value & convertToBoolean(as_value &v, const VM &vm)
Force type to bool.
Definition: VM.cpp:501
UNUSED
#define UNUSED(x)
Definition: utility.h:113
gnash::NSV::loadStrings
void loadStrings(string_table &table)
Load the prenamed strings.
Definition: namedStrings.cpp:254
gnash::getMember
as_value getMember(as_object &o, const ObjectURI &uri)
Get a member of an object using AS lookup rules.
Definition: as_object.h:756
gnash::VirtualClock::elapsed
virtual unsigned long int elapsed() const =0
Return number of milliseconds elapsed since start.
gnash::VM::getClock
VirtualClock & getClock()
Get the VM clock.
Definition: VM.h:98
gnash::VM::RNG
boost::mt11213b RNG
Definition: VM.h:152
gnash::as_value::set_string
void set_string(const std::string &str)
Set to a primitive string.
Definition: as_value.cpp:732
gnash::VM::getOSName
std::string getOSName() const
Definition: VM.cpp:94
gnash::isFinite
bool isFinite(double d)
Definition: GnashNumeric.h:47
gnash::fn_call
Parameters/environment for builtin or user-defined functions callable from ActionScript.
Definition: fn_call.h:118
gnash::as_object::DefaultFlags
static const int DefaultFlags
The most common flags for built-in properties.
Definition: as_object.h:192
gnash::key::t
@ t
Definition: GnashKey.h:166
gnash::key::r
@ r
Definition: GnashKey.h:164
gnash::renderer::opengl::for_each
void for_each(C &container, R(T::*pmf)(const A &), const A &arg)
Definition: Renderer_ogl.cpp:690
gnash::key::a
@ a
Definition: GnashKey.h:147
gnash::VM::markReachableResources
void markReachableResources() const
Mark all reachable resources (for GC)
Definition: VM.cpp:161
gnash::UserFunction
A UserFunction is a callable function defined in ActionScript.
Definition: UserFunction.h:38
gnash::as_value::NUMBER
@ NUMBER
Definition: as_value.h:110
gnash::VM::VM
VM(movie_root &root, VirtualClock &clock)
Initializes the VM.
Definition: VM.cpp:53
gnash::ActionTypeError
An ActionScript type error.
Definition: GnashException.h:161
gnash::as_value::to_function
as_function * to_function() const
Return the value as a function only if it is a function.
Definition: as_value.cpp:499
gnash::as_value::to_string
DSOTEXPORT std::string to_string(int version=7) const
Get a std::string representation for this value.
Definition: as_value.cpp:205
gnash::VM::pushCallFrame
CallFrame & pushCallFrame(UserFunction &f)
Add a function call to the call frame.
Definition: VM.cpp:236
gnash::movie_root::getRecursionLimit
std::uint16_t getRecursionLimit() const
Definition: movie_root.h:771
gnash::VM::setRegister
void setRegister(size_t index, const as_value &val)
Set value of a register (local or global).
Definition: VM.cpp:206
gnash::VM::getRegister
const as_value * getRegister(size_t index)
Get value of a register (local or global).
Definition: VM.cpp:191
gnash::VM::registerNative
void registerNative(as_c_function_ptr fun, unsigned int x, unsigned int y)
Definition: VM.cpp:268
gnash::ConstantPool
std::vector< const char * > ConstantPool
An indexed list of strings (must match the definition in action_buffer.h)
Definition: ConstantPool.h:27
DEFAULT_FLASH_SYSTEM_OS
#define DEFAULT_FLASH_SYSTEM_OS
Definition: gnashconfig.h:60
gnash::subtract
void subtract(as_value &op1, const as_value &op2, const VM &vm)
Carry out ActionSubtract.
Definition: VM.cpp:392
gnash::VM::getSWFVersion
int getSWFVersion() const
Get SWF version context for the currently running actions.
Definition: VM.h:106
gnash::VM::getNative
NativeFunction * getNative(unsigned int x, unsigned int y) const
Return a native function or null.
Definition: VM.cpp:276
gnash::VM::calling
bool calling() const
Whether a function call is in progress.
Definition: VM.h:240
GnashNumeric.h
gnash::as_value
ActionScript value type.
Definition: as_value.h:95
gnash::as_c_function_ptr
as_value(* as_c_function_ptr)(const fn_call &fn)
Definition: Property.h:34
gnash::SafeStack::StackSize
StackType::size_type StackSize
Definition: SafeStack.h:49
gnash::convertToNumber
as_value & convertToNumber(as_value &v, const VM &vm)
Force type to number.
Definition: VM.cpp:485
VM.h
gnash::VM::randomNumberGenerator
RNG & randomNumberGenerator()
Definition: VM.cpp:80
gnash::string_table::find
key find(const std::string &to_find, bool insert_unfound=true)
Find a key for a string.
Definition: string_table.cpp:40
gnash::FrameGuard
Definition: VM.h:318
ConstantPool.h
gnash::CallFrame::hasRegisters
bool hasRegisters() const
Set the number of registers for this CallFrame.
Definition: CallStack.h:104
gnash::getRoot
movie_root & getRoot(const as_environment &env)
Definition: as_environment.cpp:645
gnash::VirtualClock::restart
virtual void restart()=0
Restart the clock.
gnash::as_value::setReachable
void setReachable() const
Set any object value as reachable (for the GC)
Definition: as_value.cpp:691
IF_VERBOSE_ACTION
#define IF_VERBOSE_ACTION(x)
Definition: log.h:384
gnash::ActionLimitException
An ActionScript limit exception.
Definition: GnashException.h:136
gnash::PropFlags::onlySWF6Up
@ onlySWF6Up
Only visible by VM initialized for version 6 or higher.
Definition: PropFlags.h:45
gnash::convertToString
as_value & convertToString(as_value &v, const VM &vm)
Force type to string.
Definition: VM.cpp:493
gnash::FrameGuard::FrameGuard
FrameGuard(VM &vm, UserFunction &func)
Definition: VM.h:321
gnash::NativeFunction
This class implements functions native to the player.
Definition: NativeFunction.h:41
gnash::Global_as::ASFunction
as_value(* ASFunction)(const fn_call &fn)
Definition: Global_as.h:53
gnash::key::f
@ f
Definition: GnashKey.h:152
gnash::VM::getTime
unsigned long int getTime() const
Get the number of milliseconds since VM was started.
Definition: VM.cpp:155
gnash::as_value::is_string
bool is_string() const
Return true if this value is a string.
Definition: as_value.h:207
gnashconfig.h
gnash::RcInitFile::getDefaultInstance
static RcInitFile & getDefaultInstance()
Return the default instance of RC file.
Definition: rc.cpp:61
gnash::movie_root
This class represents the 'Stage' and top-level movie.
Definition: movie_root.h:151
gnash::Global_as::registerClasses
void registerClasses()
Definition: Global_as.cpp:235
DSOTEXPORT
#define DSOTEXPORT
Definition: dsodefs.h:63
namedStrings.h
gnash::Global_as
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:50
gnash::VM::dumpState
void dumpState(std::ostream &o, size_t limit=0)
Print stack, call stack, and registers to the specified ostream.
Definition: VM.cpp:299
test.uri
uri
Definition: test.py:12
gnash::SharedObjectLibrary
Definition: SharedObject_as.h:37
DSOEXPORT
#define DSOEXPORT
Definition: dsodefs.h:55
gnash::NSV::PROP_uuPROTOuu
@ PROP_uuPROTOuu
Definition: namedStrings.h:118
gnash::getURI
ObjectURI getURI(const VM &vm, const std::string &str, bool lowerCaseHint=false)
Definition: VM.h:290
movie_definition.h
gnash::as_value::is_sprite
bool is_sprite() const
Return true if this value is a DISPLAYOBJECT.
Definition: as_value.h:224
gnash::CallFrame
A CallFrame is an element of a CallStack.
Definition: CallStack.h:44
gnash::newLessThan
as_value newLessThan(const as_value &op1, const as_value &op2, const VM &vm)
Carry out ActionSubtract.
Definition: VM.cpp:400
gnash::toString
const std::string & toString(VM &vm, const ObjectURI &uri)
Definition: VM.h:308
gnash::key::o
@ o
Definition: GnashKey.h:161
gnash::NSV::PROP_PROTOTYPE
@ PROP_PROTOTYPE
Definition: namedStrings.h:87
gnash::ObjectURI
A URI for describing as_objects.
Definition: ObjectURI.h:45
gnash::as_value::is_object
bool is_object() const
Return true if this value is an object.
Definition: as_value.h:219
x
std::int32_t x
Definition: BitmapData_as.cpp:434
gnash::CallFrame::getLocalRegister
const as_value * getLocalRegister(size_t i) const
Get a specific register in this CallFrame.
Definition: CallStack.h:87
SafeStack.h
gnash::VM::getSystemLanguage
std::string getSystemLanguage() const
Definition: VM.cpp:124
gnash::key::b
@ b
Definition: GnashKey.h:148
test.v
v
Definition: test.py:11
gnash::VM::getStack
SafeStack< as_value > & getStack()
Accessor for the VM's stack.
Definition: VM.h:88
gnash::key::e
@ e
Definition: GnashKey.h:151
gnash::equals
bool equals(const as_value &a, const as_value &b, const VM &vm)
Check if two values are equal.
Definition: VM.cpp:439
gnash::convertToPrimitive
as_value & convertToPrimitive(as_value &v, const VM &vm)
Convert to the appropriate primitive type.
Definition: VM.cpp:508
gnash::RcInitFile
Definition: rc.h:44
gnash::as_function
ActionScript Function, either builtin or SWF-defined.
Definition: as_function.h:63
gnash::VM::currentCall
CallFrame & currentCall()
Return the CallFrame of the currently executing function.
Definition: VM.cpp:229
gnash::GcResource::setReachable
void setReachable() const
Mark this resource as being reachable.
Definition: GC.h:92
gnash::SafeStack
Definition: SafeStack.h:42
gnash::newAdd
void newAdd(as_value &op1, const as_value &op2, const VM &vm)
Carry out ActionNewAdd.
Definition: VM.cpp:356
gnash::VM::~VM
~VM()
Definition: VM.cpp:69