From 8ed53536a0418f906084bfd97371a2a1723b8243 Mon Sep 17 00:00:00 2001 From: Matthias Gehre Date: Fri, 28 Oct 2011 23:12:18 +0200 Subject: [PATCH] Implement Vector.length --- src/scripting/toplevel/Vector.cpp | 31 +++++++++++++++++++++++++++++++ src/scripting/toplevel/Vector.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/src/scripting/toplevel/Vector.cpp b/src/scripting/toplevel/Vector.cpp index a33ecbd97f..af7e556f69 100644 --- a/src/scripting/toplevel/Vector.cpp +++ b/src/scripting/toplevel/Vector.cpp @@ -21,6 +21,7 @@ #include "abc.h" #include "class.h" #include "parsing/amf3_generator.h" +#include "argconv.h" using namespace std; using namespace lightspark; @@ -33,6 +34,8 @@ void Vector::sinit(Class_base* c) c->setConstructor(Class::getFunction(_constructor)); c->setSuper(Class::getRef()); c->setDeclaredMethodByQName("push",AS3,Class::getFunction(push),NORMAL_METHOD,true); + c->setDeclaredMethodByQName("length",AS3,Class::getFunction(getLength),GETTER_METHOD,true); + c->setDeclaredMethodByQName("length",AS3,Class::getFunction(setLength),SETTER_METHOD,true); } void Vector::setTypes(const std::vector& types) @@ -110,6 +113,34 @@ ASFUNCTIONBODY(Vector,push) return abstract_ui(th->vec.size()); } +ASFUNCTIONBODY(Vector,getLength) +{ + return abstract_ui(obj->as()->vec.size()); +} + +ASFUNCTIONBODY(Vector,setLength) +{ + Vector* th = obj->as(); + uint32_t len; + ARG_UNPACK (len); + if(len <= th->vec.size()) + { + th->vec.resize(len); + } + else + { + /* We should enlarge the vector + * with the type's 'default' value. + * That's probably just 'Null' cast to that + * type. (Casting Undefined, would be wrong, + * because that gave 'NaN' for Number) + */ + while(th->vec.size() < len) + th->vec.push_back( th->vec_type->coerce( new Null ) ); + } + return NULL; +} + /* this handles the [] operator, because vec[12] becomes vec.12 in bytecode */ _NR Vector::getVariableByMultiname(const multiname& name, GET_VARIABLE_OPTION opt) { diff --git a/src/scripting/toplevel/Vector.h b/src/scripting/toplevel/Vector.h index 4de0e35371..681fed5932 100644 --- a/src/scripting/toplevel/Vector.h +++ b/src/scripting/toplevel/Vector.h @@ -48,6 +48,8 @@ class Vector: public ASObject ASFUNCTION(_applytype); ASFUNCTION(push); + ASFUNCTION(getLength); + ASFUNCTION(setLength); };