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 <boost/array.hpp>
30 #include <boost/cstdint.hpp>
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 boost::array<as_value, 4> GlobalRegisters;
274  GlobalRegisters _globalRegisters;
275 
276  CallStack _callStack;
277 
279  std::auto_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 boost::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: