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  {
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 
gnash::CharacterProxy
A proxy for DisplayObject pointers.
Definition: CharacterProxy.h:44
gnash::as_value::BOOLEAN_EXCEPT
@ BOOLEAN_EXCEPT
Definition: as_value.h:107
gnash::isInf
bool isInf(const T &num)
Definition: as_value.h:56
gnash::Date_as
Definition: Date_as.h:34
gnash::isNativeType
bool isNativeType(const as_object *obj, T *&relay)
Check whether the object is an instance of a known type.
Definition: as_object.h:875
gnash::setNaN
void setNaN(as_value &v)
Set a value to NaN.
Definition: as_value.h:524
gnash::as_value::flag_exception
void flag_exception()
Definition: as_value.h:357
gnash::parseNonDecimalInt
bool parseNonDecimalInt(const std::string &s, double &d, bool whole)
Definition: as_value.cpp:793
gnash::as_value::operator<<
friend std::ostream & operator<<(std::ostream &o, const as_value &)
Stream operator.
Definition: as_value.cpp:1034
gnash::as_value::to_bool
DSOTEXPORT bool to_bool(int version) const
Conversion to boolean.
Definition: as_value.cpp:423
gnash::as_value::equals
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
movie_root.h
gnash::CharacterProxy::get
DisplayObject * get(bool skipRebinding=false) const
Get the pointed sprite, either original or rebound.
Definition: CharacterProxy.h:97
gnash::isNaN
bool isNaN(const T &num)
Definition: GnashNumeric.h:62
gnash::as_value::is_exception
bool is_exception() const
Definition: as_value.h:348
gnash::key::d
@ d
Definition: GnashKey.h:150
gnash::NSV::PROP_TO_STRING
@ PROP_TO_STRING
Definition: namedStrings.h:191
gnash::as_object
The base class for all ActionScript objects.
Definition: as_object.h:162
gnash::doubleToString
std::string doubleToString(double val, int radix)
Convert numeric value to string value, following ECMA-262 specification.
Definition: as_value.cpp:832
as_object.h
SimpleBuffer.h
gnash::amf::Writer
A class to compose AMF buffers.
Definition: AMFConverter.h:56
gnash::key::U
@ U
Definition: GnashKey.h:133
AMFConverter.h
gnash::as_value::is_bool
bool is_bool() const
Definition: as_value.h:344
gnash::key::T
@ T
Definition: GnashKey.h:132
gnash::as_value::is_null
bool is_null() const
Definition: as_value.h:340
dsodefs.h
gnash::log_debug
void log_debug(StringType msg, Args... args)
Definition: log.h:301
gnash::typeName
std::string typeName(const T &inst)
Definition: utility.h:93
_
#define _(String)
Definition: log.h:44
gnash::as_environment
Provides information about timeline context.
Definition: as_environment.h:51
gnash::as_value::NUMBER_EXCEPT
@ NUMBER_EXCEPT
Definition: as_value.h:111
gnash::VM
The AVM1 virtual machine.
Definition: VM.h:72
gnash::as_object::to_function
virtual as_function * to_function()
Cast to a as_function, or return NULL.
Definition: as_object.h:471
gnash::as_value::defaultPrimitive
AsType defaultPrimitive(int version) const
Definition: as_value.cpp:252
gnash::PTYPE_BOOLEAN
@ PTYPE_BOOLEAN
Definition: as_value.h:67
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::as_value::as_value
DSOEXPORT as_value()
Construct an undefined value.
Definition: as_value.h:119
gnash::as_value::typeOf
const char * typeOf() const
Return the primitive type of this value as a string.
Definition: as_value.cpp:612
gnash::as_value::as_value
DSOEXPORT as_value(const char *str)
Construct a primitive String value.
Definition: as_value.h:145
gnash::as_value::strictly_equals
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
boost
Definition: gui.h:74
gnash::key::m
@ m
Definition: GnashKey.h:159
utility.h
gnash::as_value::operator=
DSOEXPORT as_value & operator=(as_value &&other)
Definition: as_value.h:190
gnash::key::s
@ s
Definition: GnashKey.h:165
as_value.h
gnash::key::n
@ n
Definition: GnashKey.h:160
gnash::as_value::set_double
void set_double(double val)
Set to a primitive number.
Definition: as_value.cpp:739
start
@ start
Definition: klash_part.cpp:330
gnash::as_value::AsType
AsType
Definition: as_value.h:101
Global_as.h
gnash::primitive_types
primitive_types
These are the primitive types, see the ECMAScript reference.
Definition: as_value.h:64
gnash::as_value::to_primitive
as_value to_primitive(AsType hint) const
Return value as a primitive type, with a preference.
Definition: as_value.cpp:263
gnash::MovieClip
A MovieClip is a container for DisplayObjects.
Definition: MovieClip.h:84
gnash::getVM
VM & getVM(const as_environment &env)
Definition: as_environment.h:222
as_function.h
gnash::as_object::array
bool array() const
Return true if this object should be treated as an array.
Definition: as_object.h:625
gnash::as_value::operator=
DSOEXPORT as_value & operator=(const as_value &v)
Assign to an as_value.
Definition: as_value.h:183
gnash::as_value::DISPLAYOBJECT_EXCEPT
@ DISPLAYOBJECT_EXCEPT
Definition: as_value.h:115
gnash::as_value::set_string
void set_string(const std::string &str)
Set to a primitive string.
Definition: as_value.cpp:732
gnash::isFinite
bool isFinite(double d)
Definition: GnashNumeric.h:47
gnash::as_value::get_object
as_object * get_object() const
Return the value as an as_object only if it is an as_object.
Definition: as_value.cpp:509
gnash::as_value::NULLTYPE
@ NULLTYPE
Definition: as_value.h:104
gnash::as_value::STRING_EXCEPT
@ STRING_EXCEPT
Definition: as_value.h:109
gnash::fn_call::Args
FunctionArgs< as_value > Args
Definition: fn_call.h:121
gnash::key::a
@ a
Definition: GnashKey.h:147
gnash::as_value::NUMBER
@ NUMBER
Definition: as_value.h:110
gnash::as_value::to_number
double to_number(int version) const
Get a number representation for this value.
Definition: as_value.cpp:318
StringPredicates.h
gnash::as_value::toMovieClip
MovieClip * toMovieClip(bool skipRebinding=false) const
Returns value as a MovieClip if it is a MovieClip.
Definition: as_value.cpp:480
gnash::invoke
DSOEXPORT as_value invoke(const as_value &method, const as_environment &env, as_object *this_ptr, fn_call::Args &args, as_object *super=nullptr, const movie_definition *callerDef=nullptr)
Call an as_value on an as_object.
Definition: Global_as.h:166
gnash::ActionTypeError
An ActionScript type error.
Definition: GnashException.h:161
gnash::as_value::to_function
as_function * to_function() const
Return the value as a function only if it is a function.
Definition: as_value.cpp:499
gnash::key::type
type
Definition: GnashKey.h:330
gnash::as_value::to_string
DSOTEXPORT std::string to_string(int version=7) const
Get a std::string representation for this value.
Definition: as_value.cpp:205
gnash::FunctionArgs
A class to contain transferable arguments for a fn_call.
Definition: fn_call.h:57
gnash::DisplayObject::to_movie
virtual MovieClip * to_movie()
Definition: DisplayObject.h:266
gnash::as_value::writeAMF0
bool writeAMF0(amf::Writer &w) const
Serialize value in AMF0 format.
Definition: as_value.cpp:759
gnash::String_as
Definition: String_as.h:34
gnash::constructInstance
as_object * constructInstance(as_function &ctor, const as_environment &env, fn_call::Args &args)
Definition: as_function.cpp:47
gnash::as_value::is_number
bool is_number() const
Return true if this value is strictly a number.
Definition: as_value.h:212
gnash::as_value::set_as_object
void set_as_object(as_object *obj)
Make this value a NULL, OBJECT, DISPLAYOBJECT value.
Definition: as_value.cpp:533
gnash::as_value::set_undefined
void set_undefined()
Set to undefined.
Definition: as_value.cpp:519
gnash::as_object::displayObject
DisplayObject * displayObject() const
Return the DisplayObject associated with this object.
Definition: as_object.h:638
MovieClip.h
gnash::key::p
@ p
Definition: GnashKey.h:162
test.w
w
Definition: test.py:8
gnash::as_value::to_object
as_object * to_object(VM &vm) const
Return value as an object, converting primitive values as needed.
Definition: as_value.cpp:453
gnash::NSV::CLASS_STRING
@ CLASS_STRING
Definition: namedStrings.h:243
gnash::NSV::CLASS_BOOLEAN
@ CLASS_BOOLEAN
Definition: namedStrings.h:201
GnashNumeric.h
gnash::as_value::unflag_exception
void unflag_exception()
Definition: as_value.h:363
gnash::as_value
ActionScript value type.
Definition: as_value.h:95
VM.h
gnash::as_value::DISPLAYOBJECT
@ DISPLAYOBJECT
Definition: as_value.h:114
gnash::NSV::PROP_VALUE_OF
@ PROP_VALUE_OF
Definition: namedStrings.h:194
gnash::as_value::is_undefined
bool is_undefined() const
Definition: as_value.h:336
gnash::as_value::set_bool
void set_bool(bool val)
Set to a primitive boolean.
Definition: as_value.cpp:746
DisplayObject.h
gnash::getRoot
movie_root & getRoot(const as_environment &env)
Definition: as_environment.cpp:645
gnash::as_value::setReachable
void setReachable() const
Set any object value as reachable (for the GC)
Definition: as_value.cpp:691
gnash::as_value::NULLTYPE_EXCEPT
@ NULLTYPE_EXCEPT
Definition: as_value.h:105
gnash::as_object::relay
Relay * relay() const
Access the as_object's Relay object.
Definition: as_object.h:620
as_environment.h
gnash::getObject
as_object * getObject(const DisplayObject *d)
Return the as_object associated with a DisplayObject if it exists.
Definition: DisplayObject.h:1160
gnash::key::f
@ f
Definition: GnashKey.h:152
gnash::as_value::is_string
bool is_string() const
Return true if this value is a string.
Definition: as_value.h:207
gnash::as_value::as_value
DSOEXPORT as_value(const as_value &v)
Copy constructor.
Definition: as_value.h:127
DSOTEXPORT
#define DSOTEXPORT
Definition: dsodefs.h:63
gnash::as_value::as_value
DSOEXPORT as_value(std::string str)
Construct a primitive String value.
Definition: as_value.h:152
namedStrings.h
gnash::key::c
@ c
Definition: GnashKey.h:149
gnash::NSV::CLASS_NUMBER
@ CLASS_NUMBER
Definition: namedStrings.h:232
gnash::as_value::is_function
bool is_function() const
Return true if this value is a function.
Definition: as_value.cpp:753
gnash::as_value::toDisplayObject
DisplayObject * toDisplayObject(bool skipRebinding=false) const
Return value as a DisplayObject or NULL if this is not possible.
Definition: as_value.cpp:490
DSOEXPORT
#define DSOEXPORT
Definition: dsodefs.h:55
gnash::DisplayObject
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:169
gnash::CharacterProxy::setReachable
void setReachable() const
Set the original sprite (if any) as reachable.
Definition: CharacterProxy.cpp:55
Array_as.h
gnash::as_value::~as_value
~as_value()
Definition: as_value.h:142
gnash::as_value::is_sprite
bool is_sprite() const
Return true if this value is a DISPLAYOBJECT.
Definition: as_value.h:224
gnash::CharacterProxy::getTarget
std::string getTarget() const
bound one.
Definition: CharacterProxy.cpp:46
CharacterProxy.h
gnash::as_value::set_null
void set_null()
Set this value to the NULL value.
Definition: as_value.cpp:526
gnash::as_value::BOOLEAN
@ BOOLEAN
Definition: as_value.h:106
gnash::as_value::as_value
DSOEXPORT as_value(as_value &&other)
Move constructor.
Definition: as_value.h:135
gnash::key::o
@ o
Definition: GnashKey.h:161
Date_as.h
gnash::as_value::UNDEFINED_EXCEPT
@ UNDEFINED_EXCEPT
Definition: as_value.h:103
gnash::log_unimpl
void log_unimpl(StringType msg, Args... args)
Definition: log.h:289
gnash::as_value::is_object
bool is_object() const
Return true if this value is an object.
Definition: as_value.h:219
gnash::as_value::as_value
as_value(T val)
Construct a primitive Boolean value.
Definition: as_value.h:161
gnash::PTYPE_NUMBER
@ PTYPE_NUMBER
Definition: as_value.h:66
GnashException.h
gnash::as_value::as_value
as_value(double num)
Construct a primitive Number value.
Definition: as_value.h:168
gnash::as_value::as_value
as_value(as_object *obj)
Construct a null, Object, or DisplayObject value.
Definition: as_value.h:175
gnash::as_value::OBJECT_EXCEPT
@ OBJECT_EXCEPT
Definition: as_value.h:113
gnash::key::b
@ b
Definition: GnashKey.h:148
test.v
v
Definition: test.py:11
gnash::key::e
@ e
Definition: GnashKey.h:151
gnash::as_value::UNDEFINED
@ UNDEFINED
Definition: as_value.h:102
gnash::as_value::OBJECT
@ OBJECT
Definition: as_value.h:112
gnash::as_value::STRING
@ STRING
Definition: as_value.h:108
String_as.h
gnash::CharacterProxy::isDangling
bool isDangling() const
Return true if this sprite is dangling.
Definition: CharacterProxy.h:118
gnash::PTYPE_STRING
@ PTYPE_STRING
Definition: as_value.h:65
gnash::as_function
ActionScript Function, either builtin or SWF-defined.
Definition: as_function.h:63
gnash::operator<<
std::ostream & operator<<(std::ostream &o, const URL &u)
Definition: URL.cpp:447
gnash::GcResource::setReachable
void setReachable() const
Mark this resource as being reachable.
Definition: GC.h:92