big merge from master, fix rpm creation, drop fetching swfdec
[gnash.git] / libcore / Timers.cpp
blob7795643df9b763b9a4b9d13f3c78f1d67f10ca23
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
3 // 2011 Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 //
22 #include "Timers.h"
23 #include "log.h"
24 #include "fn_call.h"
25 #include "VM.h"
26 #include "movie_root.h"
27 #include "Global_as.h"
28 #include "as_function.h"
30 #include <limits> // for numeric_limits
31 #include <functional>
32 #include <algorithm>
34 namespace gnash {
36 Timer::~Timer()
40 Timer::Timer(as_function& method, unsigned long ms,
41 as_object* this_ptr, const fn_call::Args& args, bool runOnce)
43 _interval(ms),
44 _start(std::numeric_limits<unsigned long>::max()),
45 _function(&method),
46 _methodName(),
47 _object(this_ptr),
48 _args(args),
49 _runOnce(runOnce)
51 start();
54 Timer::Timer(as_object* this_ptr, const ObjectURI& methodName,
55 unsigned long ms, const fn_call::Args& args, bool runOnce)
57 _interval(ms),
58 _start(std::numeric_limits<unsigned long>::max()),
59 _function(0),
60 _methodName(methodName),
61 _object(this_ptr),
62 _args(args),
63 _runOnce(runOnce)
65 start();
68 void
69 Timer::clearInterval()
71 _interval = 0;
72 _start = std::numeric_limits<unsigned long>::max();
75 void
76 Timer::start()
78 _start = getVM(*_object).getTime();
82 bool
83 Timer::expired(unsigned long now, unsigned long& elapsed)
85 if (cleared()) return false;
86 long unsigned expTime = _start + _interval;
87 if (now < expTime) return false;
88 elapsed = expTime-now;
89 return true;
92 void
93 Timer::executeAndReset()
95 if (cleared()) return;
96 execute();
97 if (_runOnce) clearInterval();
98 else _start += _interval; // reset the timer
101 void
102 Timer::execute()
105 // If _function is not 0, _methodName should be 0 anyway, but the
106 // ternary operator is there for clarity.
107 as_object* super = _function ? _object->get_super()
108 : _object->get_super(_methodName);
109 VM& vm = getVM(*_object);
111 as_value timer_method = _function ? _function :
112 getMember(*_object, _methodName);
114 as_environment env(vm);
116 // Copy args
117 fn_call::Args argsCopy(_args);
119 invoke(timer_method, env, _object, argsCopy, super);
123 void
124 Timer::markReachableResources() const
126 _args.setReachable();
128 if (_function) _function->setReachable();
129 if (_object) _object->setReachable();
132 } // namespace gnash