reduce verbosity
[gnash.git] / libcore / as_value.cpp
blobe0303cb177add9f1cbe55565868ff246e2151231
1 // as_value.cpp: ActionScript values, for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "smart_ptr.h"
22 #include "as_value.h"
23 #include "as_object.h"
24 #include "as_function.h" // for as_function
25 #include "MovieClip.h" // for DISPLAYOBJECT values
26 #include "DisplayObject.h" // for DISPLAYOBJECT values
27 #include "as_environment.h" // for DISPLAYOBJECT values
28 #include "VM.h" // for DISPLAYOBJECT values
29 #include "movie_root.h" // for DISPLAYOBJECT values
30 #include "utility.h" // for typeName()
31 #include "GnashNumeric.h"
32 #include "namedStrings.h"
33 #include "GnashException.h"
34 #include "Array_as.h"
35 #include "Date_as.h" // for Date type (readAMF0)
36 #include "SimpleBuffer.h"
37 #include "StringPredicates.h"
38 #include "Global_as.h"
39 #include "String_as.h"
40 #include "AMFConverter.h"
42 #include <boost/shared_ptr.hpp>
43 #include <cmath>
44 #include <cctype>
45 #include <boost/algorithm/string/case_conv.hpp>
46 #include <boost/lexical_cast.hpp>
47 #include <boost/format.hpp>
48 #include <locale>
49 #include <sstream>
50 #include <iomanip>
51 #include <string>
52 #include <algorithm>
54 // Define the macro below to make abstract equality operator verbose
55 //#define GNASH_DEBUG_EQUALITY 1
57 // Define the macro below to make to_primitive verbose
58 //#define GNASH_DEBUG_CONVERSION_TO_PRIMITIVE 1
60 // Define this macro to make soft references activity verbose
61 #define GNASH_DEBUG_SOFT_REFERENCES
63 namespace gnash {
65 namespace {
66 bool objectEqualsPrimitive(const as_value& obj, const as_value& prim,
67 int version);
68 bool stringEqualsNumber(const as_value& str, const as_value& num,
69 int version);
70 bool compareBoolean(const as_value& boolean, const as_value& other,
71 int version);
72 inline bool findMethod(as_object& obj, const ObjectURI& m, as_value& ret);
73 template<typename T> as_object* constructObject(VM& vm, const T& arg,
74 const ObjectURI& className);
77 namespace {
79 enum Base
81 BASE_OCT,
82 BASE_HEX
86 /// Converts a string to a uint32_t cast to an int32_t.
88 /// @param whole When true, any string that isn't wholly valid is rejected.
89 /// @param base The base (8 or 16) to use.
90 /// @param s The string to parse.
91 /// @return The converted number.
92 boost::int32_t
93 parsePositiveInt(const std::string& s, Base base, bool whole = true)
96 std::istringstream is(s);
97 boost::uint32_t target;
99 switch (base)
101 case BASE_OCT:
102 is >> std::oct;
103 break;
104 case BASE_HEX:
105 is >> std::hex;
106 break;
109 char c;
111 // If the conversion fails, or if the whole string must be convertible and
112 // some DisplayObjects are left, throw an exception.
113 if (!(is >> target) || (whole && is.get(c))) {
114 throw boost::bad_lexical_cast();
117 return target;
120 struct
121 NonNumericChar
123 bool operator()(char c) {
124 return (!std::isdigit(c) && c != '.' && c != '-' && c != '+');
128 /// Omit an empty exponent that is valid in ActionScript but not in C++.
130 /// This function throws a boost::bad_lexical_cast if it finds an invalid
131 /// exponent to avoid attempting an extraction when it will definitely fail.
133 /// A successful return from this function does not mean the exponent is
134 /// valid, only that the result of stringstream's conversion will mirror
135 /// AS behaviour.
137 /// @param si An iterator pointing to the position after an exponent sign.
138 /// @param last The end of the string to extract. If we have an exponent
139 /// with no following digit, this iterator is moved to
140 /// a position before the exponent sign.
141 void
142 validateExponent(std::string::const_iterator si,
143 std::string::const_iterator& last)
146 // Check for exponent with no following character. Depending on the
147 // version of gcc, extraction may be rejected (probably more correct) or
148 // accepted as a valid exponent (what ActionScript wants).
149 // In this case we remove the exponent to get the correct behaviour
150 // on all compilers.
151 if (si == last) {
152 --last;
153 return;
156 // Exponents with a following '-' or '+' are also valid if they end the
157 // string. It's unlikely that any version of gcc allowed this.
158 if (*si == '-' || *si == '+') {
159 ++si;
160 if (si == last) {
161 last -= 2;
162 return;
166 // An exponent ("e", "e-", or "e+") followed by a non digit is invalid.
167 if (!std::isdigit(*si)) {
168 throw boost::bad_lexical_cast();
173 /// Convert a string to a double if the complete string can be converted.
175 /// This follows the conditions of the standard C locale for numbers except
176 /// that an exponent signifier with no following digit (e.g. "2e") is
177 /// considered valid. Moreover, "2e-" is also considered valid.
179 /// This function scans the string twice (once for verification, once for
180 /// extraction) and copies it once (for extraction).
181 double
182 parseDecimalNumber(std::string::const_iterator start,
183 std::string::const_iterator last)
185 assert(start != last);
187 // Find the first position that is not a numeric character ('e' or 'E' not
188 // included). Even if no invalid character is found, it does not mean
189 // that the number is valid ("++++---" would pass the test).
190 std::string::const_iterator si =
191 std::find_if(start, last, NonNumericChar());
193 if (si != last) {
194 // If this character is not an exponent sign, the number is malformed.
195 if (*si != 'e' && *si != 'E') throw boost::bad_lexical_cast();
196 /// Move the last iterator to point before empty exponents.
197 else validateExponent(si + 1, last);
200 return boost::lexical_cast<double>(std::string(start, last));
203 } // anonymous namespace
205 // Conversion to const std::string&.
206 std::string
207 as_value::to_string(int version) const
209 switch (_type)
211 case STRING:
212 return getStr();
213 case DISPLAYOBJECT:
215 const CharacterProxy& sp = getCharacterProxy();
216 if (!sp.get()) return "";
217 return sp.getTarget();
219 case NUMBER:
220 return doubleToString(getNum());
221 case UNDEFINED:
222 if (version <= 6) return "";
223 return "undefined";
224 case NULLTYPE:
225 return "null";
226 case BOOLEAN:
227 return getBool() ? "true" : "false";
228 case OBJECT:
230 as_object* obj = getObj();
231 String_as* s;
232 if (isNativeType(obj, s)) return s->value();
234 try {
235 as_value ret = to_primitive(STRING);
236 // This additional is_string test is NOT compliant with ECMA-262
237 // specification, but seems required for compatibility with the
238 // reference player.
239 if (ret.is_string()) return ret.getStr();
241 catch (const ActionTypeError& e) {}
243 return is_function() ? "[type Function]" : "[type Object]";
247 default:
248 return "[exception]";
253 as_value::AsType
254 as_value::defaultPrimitive(int version) const
256 if (_type == OBJECT && version > 5) {
257 Date_as* d;
258 if (isNativeType(getObj(), d)) return STRING;
260 return NUMBER;
263 // Conversion to primitive value.
264 as_value
265 as_value::to_primitive(AsType hint) const
267 if (_type != OBJECT) return *this;
269 #if GNASH_DEBUG_CONVERSION_TO_PRIMITIVE
270 log_debug("to_primitive(%s)", hint==NUMBER ? "NUMBER" : "STRING");
271 #endif
273 // TODO: implement as_object::DefaultValue (ECMA-262 - 8.6.2.6)
275 as_value method;
276 as_object* obj(0);
278 if (hint == NUMBER) {
279 assert(_type == OBJECT);
280 obj = getObj();
282 if (!findMethod(*obj, NSV::PROP_VALUE_OF, method)) {
283 // Returning undefined here instead of throwing
284 // a TypeError passes tests in actionscript.all/Object.as
285 // and many swfdec tests, with no new failures (though
286 // perhaps we aren't testing enough).
287 return as_value();
290 else {
291 assert(hint == STRING);
292 assert(_type == OBJECT);
293 obj = getObj();
295 // @@ Moock says, "the value that results from
296 // calling toString() on the object".
297 if (!findMethod(*obj, NSV::PROP_TO_STRING, method) &&
298 !findMethod(*obj, NSV::PROP_VALUE_OF, method)) {
299 throw ActionTypeError();
303 assert(obj);
305 as_environment env(getVM(*obj));
306 fn_call::Args args;
307 as_value ret = invoke(method, env, obj, args);
309 #if GNASH_DEBUG_CONVERSION_TO_PRIMITIVE
310 log_debug("to_primitive: method call returned %s", ret);
311 #endif
313 if (ret._type == OBJECT) {
314 throw ActionTypeError();
316 return ret;
319 double
320 as_value::to_number(const int version) const
323 switch (_type) {
324 case STRING:
326 const std::string& s = getStr();
327 if ( s.empty() ) {
328 return version >= 5 ? NaN : 0.0;
331 if (version <= 4)
333 // For SWF4, any valid number before non-numerical
334 // DisplayObjects is returned, including exponent, positive
335 // and negative signs and whitespace before.
336 double d = 0;
337 std::istringstream is(s);
338 is >> d;
339 return d;
342 try {
344 if (version > 5) {
345 double d;
346 // Will throw if invalid.
347 if (parseNonDecimalInt(s, d)) return d;
350 // @@ Moock says the rule here is: if the
351 // string is a valid float literal, then it
352 // gets converted; otherwise it is set to NaN.
353 // Valid for SWF5 and above.
354 const std::string::size_type pos =
355 s.find_first_not_of(" \r\n\t");
357 if (pos == std::string::npos) return NaN;
359 // Will throw a boost::bad_lexical_cast if it fails.
360 return parseDecimalNumber(s.begin() + pos, s.end());
363 catch (const boost::bad_lexical_cast&) {
364 // There is no standard textual representation of infinity
365 // in the C++ standard, so our conversion function an
366 // exception for 'inf', just like for any other
367 // non-numerical text. This is correct behaviour.
368 return NaN;
372 case NULLTYPE:
373 case UNDEFINED:
375 // Evan: from my tests
376 // Martin: FlashPlayer6 gives 0; FP9 gives NaN.
377 return (version >= 7 ? NaN : 0);
380 case BOOLEAN:
381 return getBool() ? 1 : 0;
383 case NUMBER:
384 return getNum();
386 case OBJECT:
388 // @@ Moock says the result here should be
389 // "the return value of the object's valueOf()
390 // method".
392 // Arrays and Movieclips should return NaN.
393 try {
394 as_value ret = to_primitive(NUMBER);
395 return ret.to_number(version);
397 catch (const ActionTypeError& e) {
398 #if GNASH_DEBUG_CONVERSION_TO_PRIMITIVE
399 log_debug(_("to_primitive(%s, NUMBER) threw an "
400 "ActionTypeError %s"), *this, e.what());
401 #endif
402 if (is_function() && version < 6) {
403 return 0;
406 return NaN;
410 case DISPLAYOBJECT:
412 // This is tested, no valueOf is going
413 // to be invoked for movieclips.
414 return NaN;
417 default:
418 // Other object types should return NaN.
419 return NaN;
423 // Conversion to boolean
424 bool
425 as_value::to_bool(const int version) const
427 switch (_type)
429 case STRING:
431 if (version >= 7) return !getStr().empty();
432 const double num = to_number(version);
433 return num && !isNaN(num);
435 case NUMBER:
437 const double d = getNum();
438 // see testsuite/swfdec/if-6.swf
439 return d && ! isNaN(d);
441 case BOOLEAN:
442 return getBool();
443 case OBJECT:
444 return true;
445 case DISPLAYOBJECT:
446 return true;
447 default:
448 assert(_type == UNDEFINED || _type == NULLTYPE || is_exception());
449 return false;
453 // Return value as an object.
454 as_object*
455 as_value::to_object(VM& vm) const
458 switch (_type)
460 case OBJECT:
461 return getObj();
463 case DISPLAYOBJECT:
464 return getObject(toDisplayObject());
466 case STRING:
467 return constructObject(vm, getStr(), NSV::CLASS_STRING);
469 case NUMBER:
470 return constructObject(vm, getNum(), NSV::CLASS_NUMBER);
472 case BOOLEAN:
473 return constructObject(vm, getBool(), NSV::CLASS_BOOLEAN);
475 default:
476 // Invalid to convert exceptions.
477 return NULL;
481 MovieClip*
482 as_value::toMovieClip(bool allowUnloaded) const
484 if (_type != DISPLAYOBJECT) return 0;
486 DisplayObject *ch = getCharacter(allowUnloaded);
487 if (!ch) return 0;
488 return ch->to_movie();
491 DisplayObject*
492 as_value::toDisplayObject(bool allowUnloaded) const
494 if (_type != DISPLAYOBJECT) return 0;
495 return getCharacter(allowUnloaded);
498 // Return value as an ActionScript function. Returns NULL if value is
499 // not an ActionScript function.
500 as_function*
501 as_value::to_function() const
503 if (_type == OBJECT) {
504 return getObj()->to_function();
507 return 0;
510 as_object*
511 as_value::get_object() const
513 if (_type == OBJECT) {
514 return getObj();
517 return 0;
520 void
521 as_value::set_undefined()
523 _type = UNDEFINED;
524 _value = boost::blank();
527 void
528 as_value::set_null()
530 _type = NULLTYPE;
531 _value = boost::blank();
534 void
535 as_value::set_as_object(as_object* obj)
537 if (!obj)
539 set_null();
540 return;
542 if (obj->displayObject()) {
543 // The static cast is fine as long as the as_object is genuinely
544 // a DisplayObject.
545 _type = DISPLAYOBJECT;
546 _value = CharacterProxy(obj->displayObject(), getRoot(*obj));
547 return;
550 if (_type != OBJECT || getObj() != obj) {
551 _type = OBJECT;
552 _value = obj;
556 bool
557 as_value::equals(const as_value& v, int version) const
560 // First compare values of the same type.
561 if (_type == v._type) return equalsSameType(v);
563 // Then compare booleans.
564 if (is_bool()) return compareBoolean(*this, v, version);
565 if (v.is_bool()) return compareBoolean(v, *this, version);
567 // Then compare any other primitive, including null and undefined, with
568 // an object.
569 if (!is_object() && v.is_object()) {
570 return objectEqualsPrimitive(v, *this, version);
573 if (is_object() && !v.is_object()) {
574 return objectEqualsPrimitive(*this, v, version);
577 // Remaining null or undefined values only equate to other null or
578 // undefined values.
579 const bool null = (is_undefined() || is_null());
580 const bool v_null = (v.is_undefined() || v.is_null());
581 if (null || v_null) return null == v_null;
583 // Now compare a number with a string.
584 if (is_number() && v.is_string()) {
585 return stringEqualsNumber(v, *this, version);
587 if (is_string() && v.is_number()) {
588 return stringEqualsNumber(*this, v, version);
591 // Finally compare non-identical objects.
592 as_value p = *this;
593 as_value vp = v;
595 try {
596 p = to_primitive(NUMBER);
598 catch (const ActionTypeError& e) {}
600 try {
601 vp = v.to_primitive(NUMBER);
603 catch (const ActionTypeError& e) {}
605 // No conversion took place; the result is false
606 if (strictly_equals(p) && v.strictly_equals(vp)) {
607 return false;
610 return p.equals(vp, version);
613 const char*
614 as_value::typeOf() const
616 switch (_type)
618 case UNDEFINED:
619 return "undefined";
621 case STRING:
622 return "string";
624 case NUMBER:
625 return "number";
627 case BOOLEAN:
628 return "boolean";
630 case OBJECT:
631 return is_function() ? "function" : "object";
633 case DISPLAYOBJECT:
635 DisplayObject* ch = getCharacter();
636 if ( ! ch ) return "movieclip"; // dangling
637 if ( ch->to_movie() ) return "movieclip"; // bound to movieclip
638 return "object"; // bound to some other DisplayObject
641 case NULLTYPE:
642 return "null";
644 default:
645 if (is_exception()) return "exception";
646 std::abort();
647 return 0;
651 bool
652 as_value::equalsSameType(const as_value& v) const
654 assert(_type == v._type);
656 switch (_type)
658 case UNDEFINED:
659 case NULLTYPE:
660 return true;
662 case OBJECT:
663 case BOOLEAN:
664 case STRING:
665 return _value == v._value;
667 case DISPLAYOBJECT:
668 return toDisplayObject() == v.toDisplayObject();
670 case NUMBER:
672 const double a = getNum();
673 const double b = v.getNum();
674 if (isNaN(a) && isNaN(b)) return true;
675 return a == b;
677 default:
678 if (is_exception()) return false;
681 std::abort();
682 return false;
685 bool
686 as_value::strictly_equals(const as_value& v) const
688 if ( _type != v._type ) return false;
689 return equalsSameType(v);
692 void
693 as_value::setReachable() const
695 switch (_type)
697 case OBJECT:
699 as_object* op = getObj();
700 if (op) op->setReachable();
701 break;
703 case DISPLAYOBJECT:
705 CharacterProxy sp = getCharacterProxy();
706 sp.setReachable();
707 break;
709 default: break;
713 as_object*
714 as_value::getObj() const
716 assert(_type == OBJECT);
717 return boost::get<as_object*>(_value);
720 CharacterProxy
721 as_value::getCharacterProxy() const
723 assert(_type == DISPLAYOBJECT);
724 return boost::get<CharacterProxy>(_value);
727 DisplayObject*
728 as_value::getCharacter(bool allowUnloaded) const
730 return getCharacterProxy().get(allowUnloaded);
733 void
734 as_value::set_string(const std::string& str)
736 _type = STRING;
737 _value = str;
740 void
741 as_value::set_double(double val)
743 _type = NUMBER;
744 _value = val;
747 void
748 as_value::set_bool(bool val)
750 _type = BOOLEAN;
751 _value = val;
754 bool
755 as_value::is_function() const
757 return _type == OBJECT && getObj()->to_function();
760 bool
761 as_value::writeAMF0(amf::Writer& w) const
764 assert (!is_exception());
766 switch (_type)
768 default:
769 log_unimpl(_("serialization of as_value of type %d"), _type);
770 return false;
772 case OBJECT:
773 if (is_function()) return false;
774 return w.writeObject(getObj());
776 case STRING:
777 return w.writeString(getStr());
779 case NUMBER:
780 return w.writeNumber(getNum());
782 case DISPLAYOBJECT:
783 case UNDEFINED:
784 return w.writeUndefined();
786 case NULLTYPE:
787 return w.writeNull();
789 case BOOLEAN:
790 return w.writeBoolean(getBool());
794 bool
795 parseNonDecimalInt(const std::string& s, double& d, bool whole)
797 const std::string::size_type slen = s.length();
799 // "0#" would still be octal, but has the same value as a decimal.
800 if (slen < 3) return false;
802 bool negative = false;
804 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
805 // The only legitimate place for a '-' is after 0x. If it's a
806 // '+' we don't care, as it won't disturb the conversion.
807 std::string::size_type start = 2;
808 if (s[2] == '-') {
809 negative = true;
810 ++start;
812 d = parsePositiveInt(s.substr(start), BASE_HEX, whole);
813 if (negative) d = -d;
814 return true;
816 else if ((s[0] == '0' || ((s[0] == '-' || s[0] == '+') && s[1] == '0')) &&
817 s.find_first_not_of("01234567", 1) == std::string::npos) {
819 std::string::size_type start = 0;
820 if (s[0] == '-') {
821 negative = true;
822 ++start;
824 d = parsePositiveInt(s.substr(start), BASE_OCT, whole);
825 if (negative) d = -d;
826 return true;
829 return false;
833 std::string
834 doubleToString(double val, int radix)
836 // Examples:
838 // e.g. for 9*.1234567890123456789:
839 // 9999.12345678901
840 // 99999.123456789
841 // 999999.123456789
842 // 9999999.12345679
843 // [...]
844 // 999999999999.123
845 // 9999999999999.12
846 // 99999999999999.1
847 // 999999999999999
848 // 1e+16
849 // 1e+17
851 // For 1*.111111111111111111111111111111111111:
852 // 1111111111111.11
853 // 11111111111111.1
854 // 111111111111111
855 // 1.11111111111111e+15
856 // 1.11111111111111e+16
858 // For 1.234567890123456789 * 10^-i:
859 // 1.23456789012346
860 // 0.123456789012346
861 // 0.0123456789012346
862 // 0.00123456789012346
863 // 0.000123456789012346
864 // 0.0000123456789012346
865 // 0.00000123456789012346
866 // 1.23456789012346e-6
867 // 1.23456789012346e-7
869 // Handle non-numeric values.
870 if (isNaN(val)) return "NaN";
872 if (isInf(val)) return val < 0 ? "-Infinity" : "Infinity";
874 if (val == 0.0 || val == -0.0) return "0";
876 std::ostringstream ostr;
878 if (radix == 10) {
880 // ActionScript always expects dot as decimal point.
881 ostr.imbue(std::locale::classic());
883 // force to decimal notation for this range (because the
884 // reference player does)
885 if (std::abs(val) < 0.0001 && std::abs(val) >= 0.00001) {
887 // All nineteen digits (4 zeros + up to 15 significant digits)
888 ostr << std::fixed << std::setprecision(19) << val;
890 std::string str = ostr.str();
892 // Because 'fixed' also adds trailing zeros, remove them.
893 std::string::size_type pos = str.find_last_not_of('0');
894 if (pos != std::string::npos) {
895 str.erase(pos + 1);
897 return str;
900 ostr << std::setprecision(15) << val;
902 std::string str = ostr.str();
904 // Remove a leading zero from 2-digit exponent if any
905 std::string::size_type pos = str.find("e", 0);
907 if (pos != std::string::npos && str.at(pos + 2) == '0') {
908 str.erase(pos + 2, 1);
911 return str;
914 // Radix isn't 10
915 bool negative = (val < 0);
916 if (negative) val = -val;
918 double left = std::floor(val);
919 if (left < 1) return "0";
921 std::string str;
922 const std::string digits = "0123456789abcdefghijklmnopqrstuvwxyz";
924 // Construct the string backwards for speed, then reverse.
925 while (left) {
926 double n = left;
927 left = std::floor(left / radix);
928 n -= (left * radix);
929 str.push_back(digits[static_cast<int>(n)]);
931 if (negative) str.push_back('-');
933 std::reverse(str.begin(), str.end());
935 return str;
938 namespace {
940 /// Checks for equality between an object value and a primitive value
942 /// @param obj An as_value of object type. Callers must ensure this
943 /// condition is met.
944 /// @param prim An as_value of primitive type. Callers must ensure this
945 /// condition is met.
947 /// This is a function try-block.
948 bool
949 objectEqualsPrimitive(const as_value& obj, const as_value& prim, int version)
950 try {
952 assert(obj.is_object());
953 assert(!prim.is_object());
955 as_value tmp = obj.to_primitive(as_value::NUMBER);
956 if (obj.strictly_equals(tmp)) return false;
957 return tmp.equals(prim, version);
959 catch (const ActionTypeError&) {
960 return false;
963 /// @param boolean A boolean as_value
964 /// @param other An as_value of any type.
965 bool
966 compareBoolean(const as_value& boolean, const as_value& other, int version)
968 assert(boolean.is_bool());
969 return as_value(boolean.to_number(version)).equals(other, version);
972 bool
973 stringEqualsNumber(const as_value& str, const as_value& num, int version)
975 assert(num.is_number());
976 assert(str.is_string());
977 const double n = str.to_number(version);
978 if (!isFinite(n)) return false;
979 return num.strictly_equals(n);
983 /// Returns a member only if it is an object.
984 inline bool
985 findMethod(as_object& obj, const ObjectURI& m, as_value& ret)
987 return obj.get_member(m, &ret) && ret.is_object();
990 /// Construct an instance of the specified global class.
992 /// If the class is not present or is not a constructor function, this
993 /// function throws an ActionTypeError.
995 /// TODO: consider whether ActionTypeError is an appropriate exception.
996 /// TODO: test the other failure cases.
997 template<typename T>
998 as_object*
999 constructObject(VM& vm, const T& arg, const ObjectURI& className)
1002 as_object& gl = *vm.getGlobal();
1004 as_value clval;
1006 // This is tested in actionscript.all/Object.as to return an
1007 // undefined value. We throw the exception back to the VM, which pushes
1008 // an undefined value onto the stack.
1009 if (!gl.get_member(className, &clval) ) {
1010 throw ActionTypeError();
1013 // This is not properly tested.
1014 if (!clval.is_function()) {
1015 throw ActionTypeError();
1018 as_function* ctor = clval.to_function();
1020 // This is also not properly tested.
1021 if (!ctor) throw ActionTypeError();
1023 fn_call::Args args;
1024 args += arg;
1026 as_environment env(vm);
1027 as_object* ret = constructInstance(*ctor, env, args);
1029 return ret;
1033 } // unnamed namespace
1035 std::ostream&
1036 operator<<(std::ostream& o, const as_value& v)
1039 switch (v._type)
1041 case as_value::UNDEFINED:
1042 return o << "[undefined]";
1043 case as_value::NULLTYPE:
1044 return o << "[null]";
1045 case as_value::BOOLEAN:
1047 return o << "[bool:" << std::boolalpha << v.getBool() << "]";
1049 case as_value::OBJECT:
1051 as_object* obj = v.getObj();
1052 assert(obj);
1053 const std::string desc = obj->array() ? "array" :
1054 obj->relay() ? typeName(*obj->relay()) : typeName(*obj);
1055 return o << "[object(" << desc << "):" << static_cast<void*>(obj)
1056 << "]";
1058 case as_value::STRING:
1059 return o << "[string:" + v.getStr() + "]";
1060 case as_value::NUMBER:
1061 return o << "[number:" << v.getNum() << "]";
1062 case as_value::DISPLAYOBJECT:
1064 boost::format ret;
1065 const CharacterProxy& sp = v.getCharacterProxy();
1066 if (sp.isDangling()) {
1067 DisplayObject* rebound = sp.get();
1068 if (rebound) {
1069 ret = boost::format("[rebound %s(%s):%p]") %
1070 typeName(*rebound) % sp.getTarget() %
1071 static_cast<void*>(rebound);
1073 else {
1074 ret = boost::format("[dangling DisplayObject:%s]") %
1075 sp.getTarget();
1078 else {
1079 DisplayObject* ch = sp.get();
1080 ret = boost::format("[%s(%s):%p]") % typeName(*ch) %
1081 sp.getTarget() % static_cast<void*>(ch);
1083 return o << ret.str();
1085 default:
1086 assert(v.is_exception());
1087 return o << "[exception]";
1092 } // namespace gnash
1095 // Local Variables:
1096 // mode: C++
1097 // indent-tabs-mode: nil
1098 // End: