Permalink
Browse files

[Vector] Vector.<x> can be coerced to Vector.<y> only if x == y

  • Loading branch information...
1 parent 1a13848 commit ce8c4dbdaecb9d9fa7a42cf8df1b32fc6ac76418 @aajanki aajanki committed Mar 13, 2013
Showing with 47 additions and 2 deletions.
  1. +24 −0 src/scripting/class.h
  2. +18 −0 src/scripting/toplevel/Vector.cpp
  3. +5 −2 src/scripting/toplevel/Vector.h
View
@@ -21,6 +21,7 @@
#include "scripting/toplevel/Number.h"
#include "scripting/toplevel/Integer.h"
#include "scripting/toplevel/UInteger.h"
+#include "scripting/toplevel/Vector.h"
#include "compat.h"
#include "asobject.h"
#include "swf.h"
@@ -420,6 +421,29 @@ class TemplatedClass : public Class<T>
{
return types;
}
+
+ ASObject* coerce(ASObject* o) const
+ {
+ if (o->is<Undefined>())
+ {
+ o->decRef();
+ return getSys()->getNullRef();
+ }
+ else if ((o->is<Vector>() && o->as<Vector>()->sameType(types)) ||
+ o->is<Null>())
+ {
+ // Vector.<x> can be coerced to Vector.<y>
+ // only if x and y are the same type
+ return o;
+ }
+ else
+ {
+ o->decRef();
+ throwError<TypeError>(kCheckTypeFailedError, o->getClassName(),
+ Class<T>::getQualifiedClassName());
+ return NULL; // not reached
+ }
+ }
};
/* this is modeled closely after the Class/Class_base pattern */
@@ -104,6 +104,13 @@ void Vector::setTypes(const std::vector<Type*>& types)
vec_type = types[0];
}
+bool Vector::sameType(const std::vector<Type*>& types) const
+{
+ return (types.size() == 1) && ((types[0] == vec_type) ||
+ (vec_type == Type::anyType) ||
+ (types[0] == Type::anyType));
+}
+
ASObject* Vector::generator(TemplatedClass<Vector>* o_class, ASObject* const* args, const unsigned int argslen)
{
assert_and_throw(argslen == 1);
@@ -345,6 +352,17 @@ ASFUNCTIONBODY(Vector, every)
return abstract_b(true);
}
+void Vector::append(ASObject *o)
+{
+ if (fixed)
+ {
+ o->decRef();
+ throwError<RangeError>(kVectorFixedError);
+ }
+
+ vec.push_back(vec_type->coerce(o));
+}
+
ASFUNCTIONBODY(Vector,push)
{
Vector* th=static_cast<Vector*>(obj);
@@ -50,6 +50,7 @@ class Vector: public ASObject
static ASObject* generator(TemplatedClass<Vector>* o_class, ASObject* const* args, const unsigned int argslen);
void setTypes(const std::vector<Type*>& types);
+ bool sameType(const std::vector<Type*>& types) const;
//Overloads
tiny_string toString(bool debugMsg=false);
@@ -71,6 +72,10 @@ class Vector: public ASObject
return vec.at(index);
}
+ //Appends an object to the Vector. o is coerced to vec_type.
+ //Takes ownership of o.
+ void append(ASObject *o);
+
//TODO: do we need to implement generator?
ASFUNCTION(_constructor);
ASFUNCTION(_applytype);
@@ -101,7 +106,5 @@ class Vector: public ASObject
ASFUNCTION(some);
};
-
-
}
#endif /* SCRIPTING_TOPLEVEL_VECTOR_H */

0 comments on commit ce8c4db

Please sign in to comment.