Gnash  0.8.11dev
as_value.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // 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 
19 #ifndef GNASH_AS_VALUE_H
20 #define GNASH_AS_VALUE_H
21 
22 #include <limits>
23 #include <string>
24 #include <boost/variant.hpp>
25 #include <iosfwd> // for inlined output operator
26 #include <type_traits>
27 #include <cstdint>
28 
29 #include "dsodefs.h" // for DSOTEXPORT
30 #include "CharacterProxy.h"
31 #include "GnashNumeric.h" // for isNaN
32 
33 
34 // Forward declarations
35 namespace gnash {
36  class VM;
37  class as_object;
38  class Global_as;
39  class fn_call;
40  class as_function;
41  class MovieClip;
42  class DisplayObject;
43  namespace amf {
44  class Writer;
45  }
46 }
47 
48 namespace gnash {
49 
50 
51 // NaN constant for use in as_value implementation
52 static const double NaN = std::numeric_limits<double>::quiet_NaN();
53 
54 template <typename T>
55 inline bool
56 isInf(const T& num)
57 {
58  return isNaN(num - num);
59 }
60 
61 
64 {
68 };
69 
71 //
73 //
76 //
79 //
85 //
91 //
94 class as_value
95 {
96 
97 public:
98 
99  // The exception type should always be one greater than the normal type.
100  enum AsType
101  {
115  DISPLAYOBJECT_EXCEPT
116  };
117 
120  :
121  _type(UNDEFINED),
122  _value(boost::blank())
123  {
124  }
125 
128  :
129  _type(v._type),
130  _value(v._value)
131  {
132  }
133 
136  : _type(other._type),
137  _value(std::move(other._value))
138  {
139  other._type = UNDEFINED;
140  }
141 
143 
145  DSOEXPORT as_value(const char* str)
146  :
147  _type(STRING),
148  _value(std::string(str))
149  {}
150 
152  DSOEXPORT as_value(std::string str)
153  :
154  _type(STRING),
155  _value(std::move(str))
156  {}
157 
159  template <typename T, typename U =
160  typename std::enable_if<std::is_same<bool, T>::value>::type>
161  as_value(T val)
162  :
163  _type(BOOLEAN),
164  _value(val)
165  {}
166 
168  as_value(double num)
169  :
170  _type(NUMBER),
171  _value(num)
172  {}
173 
176  :
177  _type(UNDEFINED)
178  {
179  set_as_object(obj);
180  }
181 
184  {
185  _type = v._type;
186  _value = v._value;
187  return *this;
188  }
189 
191  {
192  _type = other._type;
193  _value = std::move(other._value);
194  other._type = UNDEFINED;
195  return *this;
196  }
197 
198  friend std::ostream& operator<<(std::ostream& o, const as_value&);
199 
201  const char* typeOf() const;
202 
204  bool is_function() const;
205 
207  bool is_string() const {
208  return _type == STRING;
209  }
210 
212  bool is_number() const {
213  return _type == NUMBER;
214  }
215 
217  //
219  bool is_object() const {
220  return _type == OBJECT || _type == DISPLAYOBJECT;
221  }
222 
224  bool is_sprite() const {
225  return _type == DISPLAYOBJECT;
226  }
227 
229  //
234  //
236  DSOTEXPORT std::string to_string(int version = 7) const;
237 
239  //
241  double to_number(int version) const;
242 
244  //
246  DSOTEXPORT bool to_bool(int version) const;
247 
249  //
251  //
258  //
262  as_object* to_object(VM& vm) const;
263 
265  //
268  as_object* get_object() const;
269 
271  //
274  //
277  MovieClip* toMovieClip(bool skipRebinding = false) const;
278 
280  //
283  //
294  DisplayObject* toDisplayObject(bool skipRebinding = false) const;
295 
297  //
300  as_function* to_function() const;
301 
302  AsType defaultPrimitive(int version) const;
303 
305  //
307  //
316  as_value to_primitive(AsType hint) const;
317 
319  void set_string(const std::string& str);
320 
322  void set_double(double val);
323 
325  void set_bool(bool val);
326 
328  void set_as_object(as_object* obj);
329 
331  void set_undefined();
332 
334  void set_null();
335 
336  bool is_undefined() const {
337  return (_type == UNDEFINED);
338  }
339 
340  bool is_null() const {
341  return (_type == NULLTYPE);
342  }
343 
344  bool is_bool() const {
345  return (_type == BOOLEAN);
346  }
347 
348  bool is_exception() const {
349  return (_type == UNDEFINED_EXCEPT || _type == NULLTYPE_EXCEPT
350  || _type == BOOLEAN_EXCEPT || _type == NUMBER_EXCEPT
351  || _type == OBJECT_EXCEPT || _type == DISPLAYOBJECT_EXCEPT
352  || _type == STRING_EXCEPT);
353  }
354 
355  // Flag or unflag an as_value as an exception -- this gets flagged
356  // when an as_value is 'thrown'.
357  void flag_exception() {
358  if (!is_exception()) {
359  _type = static_cast<AsType>(static_cast<int>(_type) + 1);
360  }
361  }
362 
364  if (is_exception()) {
365  _type = static_cast<AsType>(static_cast<int>(_type) - 1);
366  }
367  }
368 
370  //
373  DSOTEXPORT bool strictly_equals(const as_value& v) const;
374 
376  //
386  DSOEXPORT bool equals(const as_value& v, int version) const;
387 
389  //
391  void setReachable() const;
392 
394  //
409  bool writeAMF0(amf::Writer& w) const;
410 
411 private:
412 
414  //
421  typedef boost::variant<boost::blank,
422  double,
423  bool,
424  as_object*,
426  std::string>
427  AsValueType;
428 
430  bool operator==(const as_value& v) const;
431 
433  bool operator!=(const as_value& v) const;
434 
436  //
439  bool equalsSameType(const as_value& v) const;
440 
441  AsType _type;
442 
443  AsValueType _value;
444 
446  //
448  as_object* getObj() const;
449 
451  //
453  DisplayObject* getCharacter(bool skipRebinding = false) const;
454 
456  //
458  CharacterProxy getCharacterProxy() const;
459 
461  //
463  double getNum() const {
464  assert(_type == NUMBER);
465  return boost::get<double>(_value);
466  }
467 
469  //
471  bool getBool() const {
472  assert(_type == BOOLEAN);
473  return boost::get<bool>(_value);
474  }
475 
477  //
479  const std::string& getStr() const {
480  assert(_type == STRING);
481  return boost::get<std::string>(_value);
482  }
483 
484 };
485 
487 DSOTEXPORT std::ostream& operator<<(std::ostream& os, const as_value& v);
488 
490 //
491 // Printing formats:
492 //
493 // If _val > 1, Print up to 15 significant digits, then switch
494 // to scientific notation, rounding at the last place and
495 // omitting trailing zeroes.
496 // For values < 1, print up to 4 leading zeroes after the
497 // decimal point, then switch to scientific notation with up
498 // to 15 significant digits, rounding with no trailing zeroes
499 // If the value is negative, just add a '-' to the start; this
500 // does not affect the precision of the printed value.
501 //
502 // This almost corresponds to iomanip's std::setprecision(15)
503 // format, except that iomanip switches to scientific notation
504 // at e-05 not e-06, and always prints at least two digits for the exponent.
505 std::string doubleToString(double val, int radix = 10);
506 
510 //
520 bool parseNonDecimalInt(const std::string& s, double& d, bool whole = true);
521 
523 inline void
525  v.set_double(NaN);
526 }
527 
528 } // namespace gnash
529 
530 #endif // GNASH_AS_VALUE_H
531 
532 // Local Variables:
533 // mode: C++
534 // indent-tabs-mode: nil
535 // End:
536 
primitive_types
These are the primitive types, see the ECMAScript reference.
Definition: as_value.h:63
Definition: GnashKey.h:150
bool isNaN(const T &num)
Definition: GnashNumeric.h:62
Definition: gui.h:74
A MovieClip is a container for DisplayObjects.
Definition: MovieClip.h:83
bool is_string() const
Return true if this value is a string.
Definition: as_value.h:207
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
bool is_number() const
Return true if this value is strictly a number.
Definition: as_value.h:212
bool is_sprite() const
Return true if this value is a DISPLAYOBJECT.
Definition: as_value.h:224
ActionScript value type.
Definition: as_value.h:94
Definition: as_value.h:108
void setNaN(as_value &v)
Set a value to NaN.
Definition: as_value.h:524
std::string doubleToString(double val, int radix)
Convert numeric value to string value, following ECMA-262 specification.
Definition: as_value.cpp:832
Definition: as_value.h:104
DSOEXPORT as_value & operator=(const as_value &v)
Assign to an as_value.
Definition: as_value.h:183
Definition: as_value.h:106
v
Definition: test.py:11
bool operator==(const event_id &a, const event_id &b)
Return whether two event_ids are equal.
Definition: event_id.h:163
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
A class to compose AMF buffers.
Definition: AMFConverter.h:55
as_value(double num)
Construct a primitive Number value.
Definition: as_value.h:168
type
Definition: GnashKey.h:329
DSOEXPORT as_value(std::string str)
Construct a primitive String value.
Definition: as_value.h:152
Definition: as_value.h:102
Definition: as_value.h:105
Definition: as_value.h:103
bool is_bool() const
Definition: as_value.h:344
The base class for all ActionScript objects.
Definition: as_object.h:161
~as_value()
Definition: as_value.h:142
Definition: GnashKey.h:161
Definition: as_value.h:66
bool is_null() const
Definition: as_value.h:340
bool operator!=(const SWFCxForm &a, const SWFCxForm &b)
Definition: SWFCxForm.h:91
Definition: as_value.h:107
AsType
Definition: as_value.h:100
std::ostream & operator<<(std::ostream &o, const URL &u)
Definition: URL.cpp:447
DSOEXPORT as_value(as_value &&other)
Move constructor.
Definition: as_value.h:135
bool isInf(const T &num)
Definition: as_value.h:56
A proxy for DisplayObject pointers.
Definition: CharacterProxy.h:43
Definition: as_value.h:113
Definition: as_value.h:110
Definition: as_value.h:114
bool equals(const as_value &a, const as_value &b, const VM &vm)
Check if two values are equal.
Definition: VM.cpp:439
as_value(as_object *obj)
Construct a null, Object, or DisplayObject value.
Definition: as_value.h:175
Definition: as_value.h:109
DSOEXPORT as_value()
Construct an undefined value.
Definition: as_value.h:119
#define DSOEXPORT
Definition: dsodefs.h:55
Definition: GnashKey.h:133
Definition: as_value.h:67
The AVM1 virtual machine.
Definition: VM.h:71
Definition: GnashKey.h:132
bool parseNonDecimalInt(const std::string &s, double &d, bool whole)
Definition: as_value.cpp:793
Definition: as_value.h:111
w
Definition: test.py:8
void set_double(double val)
Set to a primitive number.
Definition: as_value.cpp:739
void flag_exception()
Definition: as_value.h:357
bool is_undefined() const
Definition: as_value.h:336
#define DSOTEXPORT
Definition: dsodefs.h:63
void unflag_exception()
Definition: as_value.h:363
bool is_object() const
Return true if this value is an object.
Definition: as_value.h:219
DSOEXPORT as_value(const char *str)
Construct a primitive String value.
Definition: as_value.h:145
as_value(T val)
Construct a primitive Boolean value.
Definition: as_value.h:161
Definition: GnashKey.h:165
Definition: as_value.h:65
bool is_exception() const
Definition: as_value.h:348
Definition: as_value.h:112
DSOEXPORT as_value(const as_value &v)
Copy constructor.
Definition: as_value.h:127
DSOEXPORT as_value & operator=(as_value &&other)
Definition: as_value.h:190
ActionScript Function, either builtin or SWF-defined.
Definition: as_function.h:62