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 "utility.h" // for UNUSED
30 #include "dsodefs.h" // for DSOTEXPORT
31 #include "CharacterProxy.h"
32 #include "GnashNumeric.h" // for isNaN
33 
34 
35 // Forward declarations
36 namespace gnash {
37  class VM;
38  class as_object;
39  class Global_as;
40  class fn_call;
41  class as_function;
42  class MovieClip;
43  class DisplayObject;
44  namespace amf {
45  class Writer;
46  }
47 }
48 
49 namespace gnash {
50 
51 
52 // NaN constant for use in as_value implementation
53 static const double NaN = std::numeric_limits<double>::quiet_NaN();
54 
55 template <typename T>
56 inline bool
57 isInf(const T& num)
58 {
59  return isNaN(num - num);
60 }
61 
62 
65 {
69 };
70 
72 //
74 //
77 //
80 //
86 //
92 //
95 class as_value
96 {
97 
98 public:
99 
100  // The exception type should always be one greater than the normal type.
101  enum AsType
102  {
117  };
118 
121  :
122  _type(UNDEFINED),
123  _value(boost::blank())
124  {
125  }
126 
129  :
130  _type(v._type),
131  _value(v._value)
132  {
133  }
134 
137  : _type(other._type),
138  _value(std::move(other._value))
139  {
140  other._type = UNDEFINED;
141  }
142 
144 
146  DSOEXPORT as_value(const char* str)
147  :
148  _type(STRING),
149  _value(std::string(str))
150  {}
151 
153  DSOEXPORT as_value(std::string str)
154  :
155  _type(STRING),
156  _value(std::move(str))
157  {}
158 
160  template <typename T>
161  as_value(T val, typename std::enable_if<std::is_same<bool, T>::value>::type*
162  dummy = 0)
163  :
164  _type(BOOLEAN),
165  _value(val)
166  {
167  UNUSED(dummy);
168  }
169 
171  as_value(double num)
172  :
173  _type(NUMBER),
174  _value(num)
175  {}
176 
179  :
180  _type(UNDEFINED)
181  {
182  set_as_object(obj);
183  }
184 
187  {
188  _type = v._type;
189  _value = v._value;
190  return *this;
191  }
192 
194  {
195  _type = other._type;
196  _value = std::move(other._value);
197  other._type = UNDEFINED;
198  return *this;
199  }
200 
201  friend std::ostream& operator<<(std::ostream& o, const as_value&);
202 
204  const char* typeOf() const;
205 
207  bool is_function() const;
208 
210  bool is_string() const {
211  return _type == STRING;
212  }
213 
215  bool is_number() const {
216  return _type == NUMBER;
217  }
218 
220  //
222  bool is_object() const {
223  return _type == OBJECT || _type == DISPLAYOBJECT;
224  }
225 
227  bool is_sprite() const {
228  return _type == DISPLAYOBJECT;
229  }
230 
232  //
237  //
239  DSOTEXPORT std::string to_string(int version = 7) const;
240 
242  //
244  double to_number(int version) const;
245 
247  //
249  DSOTEXPORT bool to_bool(int version) const;
250 
252  //
254  //
261  //
265  as_object* to_object(VM& vm) const;
266 
268  //
271  as_object* get_object() const;
272 
274  //
277  //
280  MovieClip* toMovieClip(bool skipRebinding = false) const;
281 
283  //
286  //
297  DisplayObject* toDisplayObject(bool skipRebinding = false) const;
298 
300  //
303  as_function* to_function() const;
304 
305  AsType defaultPrimitive(int version) const;
306 
308  //
310  //
319  as_value to_primitive(AsType hint) const;
320 
322  void set_string(const std::string& str);
323 
325  void set_double(double val);
326 
328  void set_bool(bool val);
329 
331  void set_as_object(as_object* obj);
332 
334  void set_undefined();
335 
337  void set_null();
338 
339  bool is_undefined() const {
340  return (_type == UNDEFINED);
341  }
342 
343  bool is_null() const {
344  return (_type == NULLTYPE);
345  }
346 
347  bool is_bool() const {
348  return (_type == BOOLEAN);
349  }
350 
351  bool is_exception() const {
352  return (_type == UNDEFINED_EXCEPT || _type == NULLTYPE_EXCEPT
353  || _type == BOOLEAN_EXCEPT || _type == NUMBER_EXCEPT
354  || _type == OBJECT_EXCEPT || _type == DISPLAYOBJECT_EXCEPT
355  || _type == STRING_EXCEPT);
356  }
357 
358  // Flag or unflag an as_value as an exception -- this gets flagged
359  // when an as_value is 'thrown'.
360  void flag_exception() {
361  if (!is_exception()) {
362  _type = static_cast<AsType>(static_cast<int>(_type) + 1);
363  }
364  }
365 
367  if (is_exception()) {
368  _type = static_cast<AsType>(static_cast<int>(_type) - 1);
369  }
370  }
371 
373  //
376  DSOTEXPORT bool strictly_equals(const as_value& v) const;
377 
379  //
389  DSOEXPORT bool equals(const as_value& v, int version) const;
390 
392  //
394  void setReachable() const;
395 
397  //
412  bool writeAMF0(amf::Writer& w) const;
413 
414 private:
415 
417  //
424  typedef boost::variant<boost::blank,
425  double,
426  bool,
427  as_object*,
429  std::string>
430  AsValueType;
431 
433  bool operator==(const as_value& v) const;
434 
436  bool operator!=(const as_value& v) const;
437 
439  //
442  bool equalsSameType(const as_value& v) const;
443 
444  AsType _type;
445 
446  AsValueType _value;
447 
449  //
451  as_object* getObj() const;
452 
454  //
456  DisplayObject* getCharacter(bool skipRebinding = false) const;
457 
459  //
461  CharacterProxy getCharacterProxy() const;
462 
464  //
466  double getNum() const {
467  assert(_type == NUMBER);
468  return boost::get<double>(_value);
469  }
470 
472  //
474  bool getBool() const {
475  assert(_type == BOOLEAN);
476  return boost::get<bool>(_value);
477  }
478 
480  //
482  const std::string& getStr() const {
483  assert(_type == STRING);
484  return boost::get<std::string>(_value);
485  }
486 
487 };
488 
490 DSOTEXPORT std::ostream& operator<<(std::ostream& os, const as_value& v);
491 
493 //
494 // Printing formats:
495 //
496 // If _val > 1, Print up to 15 significant digits, then switch
497 // to scientific notation, rounding at the last place and
498 // omitting trailing zeroes.
499 // For values < 1, print up to 4 leading zeroes after the
500 // decimal point, then switch to scientific notation with up
501 // to 15 significant digits, rounding with no trailing zeroes
502 // If the value is negative, just add a '-' to the start; this
503 // does not affect the precision of the printed value.
504 //
505 // This almost corresponds to iomanip's std::setprecision(15)
506 // format, except that iomanip switches to scientific notation
507 // at e-05 not e-06, and always prints at least two digits for the exponent.
508 std::string doubleToString(double val, int radix = 10);
509 
513 //
523 bool parseNonDecimalInt(const std::string& s, double& d, bool whole = true);
524 
526 inline void
528  v.set_double(NaN);
529 }
530 
531 } // namespace gnash
532 
533 #endif // GNASH_AS_VALUE_H
534 
535 // Local Variables:
536 // mode: C++
537 // indent-tabs-mode: nil
538 // End:
539 
Definition: as_value.h:116
primitive_types
These are the primitive types, see the ECMAScript reference.
Definition: as_value.h:64
DSOTEXPORT bool to_bool(int version) const
Conversion to boolean.
Definition: as_value.cpp:423
void set_bool(bool val)
Set to a primitive boolean.
Definition: as_value.cpp:746
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
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
SWFStream & s
Definition: DefineBitsTag.cpp:71
DSOEXPORT bool equals(const as_value &v, int version) const
Return true if this value is abstractly equal to the given one.
Definition: as_value.cpp:555
ActionScript value type.
Definition: as_value.h:95
#define UNUSED(x)
Definition: utility.h:113
Definition: as_value.h:109
void setNaN(as_value &v)
Set a value to NaN.
Definition: as_value.h:527
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:105
DSOEXPORT as_value & operator=(const as_value &v)
Assign to an as_value.
Definition: as_value.h:186
bool is_null() const
Definition: as_value.h:343
DisplayObject * toDisplayObject(bool skipRebinding=false) const
Return value as a DisplayObject or NULL if this is not possible.
Definition: as_value.cpp:490
Definition: as_value.h:107
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:171
type
Definition: GnashKey.h:329
DSOEXPORT as_value(std::string str)
Construct a primitive String value.
Definition: as_value.h:153
Definition: as_value.h:103
Definition: as_value.h:106
Definition: as_value.h:104
as_value to_primitive(AsType hint) const
Return value as a primitive type, with a preference.
Definition: as_value.cpp:263
The base class for all ActionScript objects.
Definition: as_object.h:161
~as_value()
Definition: as_value.h:143
Definition: GnashKey.h:161
Definition: as_value.h:67
bool is_number() const
Return true if this value is strictly a number.
Definition: as_value.h:215
Definition: as_value.h:108
AsType
Definition: as_value.h:101
as_object * get_object() const
Return the value as an as_object only if it is an as_object.
Definition: as_value.cpp:509
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:136
bool isInf(const T &num)
Definition: as_value.h:57
bool writeAMF0(amf::Writer &w) const
Serialize value in AMF0 format.
Definition: as_value.cpp:759
A proxy for DisplayObject pointers.
Definition: CharacterProxy.h:43
bool is_bool() const
Definition: as_value.h:347
friend std::ostream & operator<<(std::ostream &o, const as_value &)
Stream operator.
Definition: as_value.cpp:1034
Definition: as_value.h:114
DSOTEXPORT bool strictly_equals(const as_value &v) const
Return true if this value is strictly equal to the given one.
Definition: as_value.cpp:684
AsType defaultPrimitive(int version) const
Definition: as_value.cpp:252
Definition: as_value.h:111
Definition: as_value.h:115
bool is_object() const
Return true if this value is an object.
Definition: as_value.h:222
void set_undefined()
Set to undefined.
Definition: as_value.cpp:519
tuple v
Definition: test.py:11
bool is_undefined() const
Definition: as_value.h:339
as_value(as_object *obj)
Construct a null, Object, or DisplayObject value.
Definition: as_value.h:178
Definition: as_value.h:110
DSOEXPORT as_value()
Construct an undefined value.
Definition: as_value.h:120
#define DSOEXPORT
Definition: dsodefs.h:55
const char * typeOf() const
Return the primitive type of this value as a string.
Definition: as_value.cpp:612
as_value(T val, typename std::enable_if< std::is_same< bool, T >::value >::type *dummy=0)
Construct a primitive Boolean value.
Definition: as_value.h:161
Definition: as_value.h:68
bool is_sprite() const
Return true if this value is a DISPLAYOBJECT.
Definition: as_value.h:227
void set_as_object(as_object *obj)
Make this value a NULL, OBJECT, DISPLAYOBJECT value.
Definition: as_value.cpp:533
MovieClip * toMovieClip(bool skipRebinding=false) const
Returns value as a MovieClip if it is a MovieClip.
Definition: as_value.cpp:480
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:112
void set_double(double val)
Set to a primitive number.
Definition: as_value.cpp:739
double to_number(int version) const
Get a number representation for this value.
Definition: as_value.cpp:318
void flag_exception()
Definition: as_value.h:360
void set_string(const std::string &str)
Set to a primitive string.
Definition: as_value.cpp:732
as_object * to_object(VM &vm) const
Return value as an object, converting primitive values as needed.
Definition: as_value.cpp:453
#define DSOTEXPORT
Definition: dsodefs.h:63
void unflag_exception()
Definition: as_value.h:366
bool is_function() const
Return true if this value is a function.
Definition: as_value.cpp:753
tuple w
Definition: test.py:8
bool is_string() const
Return true if this value is a string.
Definition: as_value.h:210
DSOEXPORT as_value(const char *str)
Construct a primitive String value.
Definition: as_value.h:146
as_function * to_function() const
Return the value as a function only if it is a function.
Definition: as_value.cpp:499
Definition: as_value.h:66
DSOTEXPORT std::string to_string(int version=7) const
Get a std::string representation for this value.
Definition: as_value.cpp:205
Definition: as_value.h:113
DSOEXPORT as_value(const as_value &v)
Copy constructor.
Definition: as_value.h:128
void setReachable() const
Set any object value as reachable (for the GC)
Definition: as_value.cpp:691
DSOEXPORT as_value & operator=(as_value &&other)
Definition: as_value.h:193
ActionScript Function, either builtin or SWF-defined.
Definition: as_function.h:62
void set_null()
Set this value to the NULL value.
Definition: as_value.cpp:526
bool is_exception() const
Definition: as_value.h:351