• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

VM.h

Go to the documentation of this file.
00001 // VM.h: the Virtual Machine class, for Gnash
00002 // 
00003 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
00004 //   Foundation, Inc
00005 // 
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 3 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 #ifndef GNASH_VM_H
00021 #define GNASH_VM_H
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include "gnashconfig.h"
00025 #endif
00026 
00027 #include <map>
00028 #include <vector>
00029 #include <memory> 
00030 #include <locale>
00031 #include <boost/cstdint.hpp> 
00032 #include <boost/random.hpp>
00033 #include <boost/noncopyable.hpp>
00034 #include <boost/intrusive_ptr.hpp>
00035 #include <boost/array.hpp>
00036 
00037 #include "GC.h"
00038 #include "string_table.h"
00039 #include "SafeStack.h"
00040 #include "CallStack.h"
00041 #include "smart_ptr.h"
00042 #include "as_value.h"
00043 
00044 // Forward declarations
00045 namespace gnash {
00046         class Global_as;
00047         class VM;
00048         class fn_call;
00049         class movie_root;
00050         class NativeFunction;
00051     class SharedObjectLibrary;
00052         class as_value;
00053         class as_object;
00054         class VirtualClock;
00055     class UserFunction;
00056 }
00057 
00058 namespace gnash {
00059 
00061 class VmGcRoot : public GcRoot 
00062 {
00063 public:
00064         VmGcRoot(VM& vm) : _vm(vm) {}
00065         virtual void markReachableResources() const;
00066 
00067 private:
00068     VM& _vm;
00069 };
00070 
00072 //
00075 //
00079 //
00081 //
00084 //
00087 class DSOEXPORT VM : boost::noncopyable
00088 {
00089 
00090 public:
00091 
00092         typedef as_value (*as_c_function_ptr)(const fn_call& fn);
00093 
00097         //
00111         static VM& init(int version, movie_root& root, VirtualClock& clock);
00112 
00113         SafeStack<as_value>& getStack() {
00114                 return _stack;
00115         }
00116 
00118     //
00124     VirtualClock& getClock() {
00125         return _clock;
00126     }
00127 
00129         static bool isInitialized();
00130 
00132     //
00140     void clear();
00141 
00143         //
00149         static VM& get();
00150 
00152         //
00155         int getSWFVersion() const;
00156 
00158         void setSWFVersion(int v);
00159 
00161         unsigned long int getTime() const;
00162 
00164         string_table& getStringTable() const { return _stringTable; }
00165 
00167         //
00171         const std::string& getPlayerVersion() const;
00172         
00177         const std::string getOSName();
00178         
00182         const std::string getSystemLanguage();
00183         
00184         // The boost Random Number Generator to use.
00185         //
00186         // http://www.boost.org/libs/random/random-generators.html
00187         //
00188         // TODO: boost/nondet_random.hpp provides access to a random device,
00189         // which can be used in preference to a pseudo-RNG. It is only
00190         // presently available on some platforms.
00191         // http://www.boost.org/libs/random/nondet_random.html
00192         //
00193         // Generators have different limits on the size of the seed. Please
00194         // check if replacing the generator.
00195         //
00196         // The mt11213b provides a pseudo-random number cycle
00197         // of length 2^11213-1 and requires approx 352*sizeof(uint32_t) memory
00198         // once initialized. It is more than adequate for most purposes.
00199         typedef boost::mt11213b RNG;    
00200 
00201         // Get a pointer to the random number generator for
00202         // use by Math.random() and random().
00203         //
00204         // The seed is the system time in milliseconds at the first call
00205         // to a random function. This allows a potentially variable amount
00206         // of time to elapse between starting gnash and initialization of
00207         // the generator, so decreasing predictability.
00208         RNG& randomNumberGenerator() const;
00209 
00211         movie_root& getRoot() const;
00212 
00214     //
00217     SharedObjectLibrary& getSharedObjectLibrary() const {
00218         assert(_shLib.get());
00219         return *_shLib;
00220     }
00221 
00223         Global_as* getGlobal() const;
00224 
00226         //
00233         void markReachableResources() const;
00234 
00235         void registerNative(as_c_function_ptr fun, unsigned int x, unsigned int y);
00236 
00238         NativeFunction* getNative(unsigned int x, unsigned int y) const;
00239 
00241     //
00253     //
00257     const as_value* getRegister(size_t index);
00258 
00260     //
00276     void setRegister(size_t index, const as_value& val);
00277 
00279     //
00282     //
00284     CallFrame& pushCallFrame(UserFunction& f);
00285 
00287     //
00289     void popCallFrame();
00290 
00292     //
00295     CallFrame& currentCall();
00296 
00298     bool calling() const {
00299         return !_callStack.empty();
00300     }
00301 
00303     void dumpState(std::ostream& o, size_t limit = 0);
00304 
00305 #ifdef GNASH_USE_GC
00306         void addStatic(GcResource* res)
00307         {
00308                 _statics.push_back(res);
00309         }
00310 #else  // ndef GNASH_USE_GC
00311         // placeholder to avoid adding lots of
00312         // compile-time switches in callers
00313         void addStatic(as_object*) {}
00314 #endif
00315 
00316 private:
00317 
00318         friend class VmGcRoot;
00319 
00321         //
00324         VM(int version, movie_root& root, VirtualClock& clock);
00325 
00328         ~VM();
00329 
00330         // We use an auto_ptr here to allow constructing
00331         // the singleton when the init() function is called.
00332         friend class std::auto_ptr<VM>;
00333         static std::auto_ptr<VM> _singleton;
00334 
00336         movie_root& _rootMovie;
00337 
00339         Global_as* _global;
00340 
00342         int _swfversion;
00343 
00345         //
00348         void setGlobal(Global_as*);
00349 
00350 #ifdef GNASH_USE_GC
00351 
00352 
00353         //
00356         typedef std::vector< boost::intrusive_ptr<GcResource> > ResVect;
00357         ResVect _statics;
00358 #endif
00359 
00360         typedef std::map<unsigned int, as_c_function_ptr> FuncMap;
00361         typedef std::map<unsigned int, FuncMap> AsNativeTable;
00362         AsNativeTable _asNativeTable;
00363 
00365         mutable string_table _stringTable;
00366 
00367         VirtualClock& _clock;
00368 
00369         SafeStack<as_value>     _stack;
00370 
00371     typedef boost::array<as_value, 4> GlobalRegisters;
00372     GlobalRegisters _globalRegisters;
00373 
00374         CallStack _callStack;
00375 
00377     std::auto_ptr<SharedObjectLibrary> _shLib;
00378 
00379 };
00380 
00384 class FrameGuard
00385 {
00386 public:
00387 
00388     FrameGuard(VM& vm, UserFunction& func)
00389         :
00390         _vm(vm),
00391         _callFrame(_vm.pushCallFrame(func))
00392     {
00393     }
00394 
00396     CallFrame& callFrame() {
00397         return _callFrame;
00398     }
00399 
00400     ~FrameGuard() {
00401         _vm.popCallFrame();
00402     }
00403 
00404 private:
00405     VM& _vm;
00406     CallFrame& _callFrame;
00407 };
00408 
00422 
00424 //
00428 //
00432 void newAdd(as_value& op1, const as_value& op2, VM& vm);
00433 
00435 //
00439 void subtract(as_value& op1, const as_value& op2, VM& vm);
00440 
00442 //
00446 as_value newLessThan(const as_value& op1, const as_value& op2, VM& vm);
00447 
00448 } // namespace gnash
00449 
00450 #endif // GNASH_VM_H
00451 
00452 // Local Variables:
00453 // mode: C++
00454 // indent-tabs-mode: t
00455 // End:

Generated on Thu Sep 30 2010 14:35:04 for Gnash by  doxygen 1.7.1