Skip to content

Commit

Permalink
Implement Vector.length
Browse files Browse the repository at this point in the history
  • Loading branch information
mgehre committed Nov 29, 2011
1 parent 1882b88 commit 8ed5353
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/scripting/toplevel/Vector.cpp
Expand Up @@ -21,6 +21,7 @@
#include "abc.h"
#include "class.h"
#include "parsing/amf3_generator.h"
#include "argconv.h"

using namespace std;
using namespace lightspark;
Expand All @@ -33,6 +34,8 @@ void Vector::sinit(Class_base* c)
c->setConstructor(Class<IFunction>::getFunction(_constructor));
c->setSuper(Class<ASObject>::getRef());
c->setDeclaredMethodByQName("push",AS3,Class<IFunction>::getFunction(push),NORMAL_METHOD,true);
c->setDeclaredMethodByQName("length",AS3,Class<IFunction>::getFunction(getLength),GETTER_METHOD,true);
c->setDeclaredMethodByQName("length",AS3,Class<IFunction>::getFunction(setLength),SETTER_METHOD,true);
}

void Vector::setTypes(const std::vector<Type*>& types)
Expand Down Expand Up @@ -110,6 +113,34 @@ ASFUNCTIONBODY(Vector,push)
return abstract_ui(th->vec.size());
}

ASFUNCTIONBODY(Vector,getLength)
{
return abstract_ui(obj->as<Vector>()->vec.size());
}

ASFUNCTIONBODY(Vector,setLength)
{
Vector* th = obj->as<Vector>();
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<ASObject> Vector::getVariableByMultiname(const multiname& name, GET_VARIABLE_OPTION opt)
{
Expand Down
2 changes: 2 additions & 0 deletions src/scripting/toplevel/Vector.h
Expand Up @@ -48,6 +48,8 @@ class Vector: public ASObject
ASFUNCTION(_applytype);

ASFUNCTION(push);
ASFUNCTION(getLength);
ASFUNCTION(setLength);
};


Expand Down

0 comments on commit 8ed5353

Please sign in to comment.