Skip to content

Commit

Permalink
Fix array element type calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
waTeim committed Dec 15, 2014
1 parent beec94b commit 599d533
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ nj::Result nj::Call::eval(vector<shared_ptr<nj::Value>> &args)

if(args.size() == 0) return res;

switch(args[0]->type()->getId())
switch(args[0]->type()->id())
{
case ascii_string_type:
case utf8_string_type:
Expand Down
6 changes: 3 additions & 3 deletions src/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace nj
{
protected:

int id;
int _id;

public:

Expand All @@ -24,10 +24,10 @@ namespace nj

Type(int id)
{
this->id = id;
this->_id = id;
}

virtual int getId() const { return id; }
virtual int id() const { return _id; }

};

Expand Down
20 changes: 10 additions & 10 deletions src/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ bool nj::operator<(const nj::Type &t1,const nj::Type &t2)
{
bool res = false;

if(t1.getId() == nj::boolean_type && t2.getId() != nj::boolean_type) res = true;
if(t1.id() == nj::boolean_type && t2.id() != nj::boolean_type) res = true;
else
{
switch(t1.getId())
switch(t1.id())
{
case int8_type:
{
switch(t2.getId())
switch(t2.id())
{
case int16_type:
case int32_type:
Expand All @@ -25,7 +25,7 @@ bool nj::operator<(const nj::Type &t1,const nj::Type &t2)
break;
case uint8_type:
{
switch(t2.getId())
switch(t2.id())
{
case int16_type:
case uint16_type:
Expand All @@ -42,7 +42,7 @@ bool nj::operator<(const nj::Type &t1,const nj::Type &t2)
break;
case int16_type:
{
switch(t2.getId())
switch(t2.id())
{
case int32_type:
case int64_type:
Expand All @@ -55,7 +55,7 @@ bool nj::operator<(const nj::Type &t1,const nj::Type &t2)
break;
case uint16_type:
{
switch(t2.getId())
switch(t2.id())
{
case int32_type:
case uint32_type:
Expand All @@ -70,7 +70,7 @@ bool nj::operator<(const nj::Type &t1,const nj::Type &t2)
break;
case int32_type:
{
switch(t2.getId())
switch(t2.id())
{
case int64_type:
case float64_type:
Expand All @@ -81,7 +81,7 @@ bool nj::operator<(const nj::Type &t1,const nj::Type &t2)
break;
case uint32_type:
{
switch(t2.getId())
switch(t2.id())
{
case int64_type:
case uint64_type:
Expand All @@ -93,7 +93,7 @@ bool nj::operator<(const nj::Type &t1,const nj::Type &t2)
break;
case float32_type:
{
switch(t2.getId())
switch(t2.id())
{
case float64_type:
res = true;
Expand All @@ -103,7 +103,7 @@ bool nj::operator<(const nj::Type &t1,const nj::Type &t2)
break;
case ascii_string_type:
{
switch(t2.getId())
switch(t2.id())
{
case utf8_string_type:
res = true;
Expand Down
6 changes: 3 additions & 3 deletions src/nj-v10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Handle<Value> callback(HandleScope &scope,const Local<Function> &cb,int argc,Loc
Local<Value> createPrimitiveRes(HandleScope &scope,const nj::Primitive &primitive)
{

switch(primitive.type()->getId())
switch(primitive.type()->id())
{
case nj::null_type:
{
Expand Down Expand Up @@ -193,7 +193,7 @@ template<typename V,typename E,Local<Value> getElement(const V &val)> Local<Arra

Local<Array> createArrayRes(HandleScope &scope,const shared_ptr<nj::Value> &value,const nj::Type *elementType)
{
switch(elementType->getId())
switch(elementType->id())
{
case nj::null_type: return createArrayRes<unsigned char,nj::Null_t,getNullValue>(scope,value); break;
case nj::float64_type: return createArrayRes<double,nj::Float64_t,getNumberFromValue<double>>(scope,value); break;
Expand Down Expand Up @@ -240,7 +240,7 @@ int createResponse(HandleScope &scope,const shared_ptr<nj::Result> &res,int argc
{
if(value.get())
{
if(value->type()->getId() == nj::julia_handle_type)
if(value->type()->id() == nj::julia_handle_type)
{
nj::JuliaHandle *handle = static_cast<nj::JuliaHandle*>(value.get());
int64_t hIndex = handle->intern();
Expand Down
6 changes: 3 additions & 3 deletions src/nj-v11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Local<Value> createPrimitiveRes(HandleScope &scope,const nj::Primitive &primitiv
{
Isolate *I = Isolate::GetCurrent();

switch(primitive.type()->getId())
switch(primitive.type()->id())
{
case nj::null_type:
{
Expand Down Expand Up @@ -199,7 +199,7 @@ template<typename V,typename E,Local<Value> getElement(Isolate *I,const V &val)>

Local<Array> createArrayRes(HandleScope &scope,const shared_ptr<nj::Value> &value,const nj::Type *elementType)
{
switch(elementType->getId())
switch(elementType->id())
{
case nj::null_type: return createArrayRes<unsigned char,nj::Null_t,getNullValue>(scope,value); break;
case nj::float64_type: return createArrayRes<double,nj::Float64_t,getNumberFromValue<double>>(scope,value); break;
Expand Down Expand Up @@ -244,7 +244,7 @@ int createResponse(HandleScope &scope,const shared_ptr<nj::Result> &res,int argc
{
if(value.get())
{
if(value->type()->getId() == nj::julia_handle_type)
if(value->type()->id() == nj::julia_handle_type)
{
nj::JuliaHandle *handle = static_cast<nj::JuliaHandle*>(value.get());
int64_t hIndex = handle->intern();
Expand Down
17 changes: 9 additions & 8 deletions src/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static shared_ptr<nj::Value> createJRefReq(const Local<Object> &obj)
return handle;
}

static void examineArray(Local<Array> &a,size_t level,vector<size_t> &dims,nj::Type *&etype,bool &determineDimensions) throw(nj::InvalidException)
static void examineArray(Local<Array> &a,size_t level,vector<size_t> &dims,nj::Type *&maxType,bool &determineDimensions) throw(nj::InvalidException)
{
size_t len = a->Length();

Expand All @@ -118,16 +118,17 @@ static void examineArray(Local<Array> &a,size_t level,vector<size_t> &dims,nj::T
{
Local<Array> sub = Local<Array>::Cast(el);

examineArray(sub,level + 1,dims,etype,determineDimensions);
examineArray(sub,level + 1,dims,maxType,determineDimensions);
}
else
{
nj::Type *etypeNarrow = getPrimitiveType(el);
nj::Type *etype = getPrimitiveType(el);

if(!etypeNarrow) throw(nj::InvalidException("unknown array element type"));
if(!etype || *etype < *etypeNarrow) etype = etypeNarrow;
if((etype->getId() == nj::int64_type || etype->getId() == nj::uint64_type) && etypeNarrow->getId() == nj::float64_type) etype = etypeNarrow;
if(etype != etypeNarrow && !(*etype < *etypeNarrow)) etype = nj::Any_t::instance();
if(!etype) throw(nj::InvalidException("unknown array element type"));
if(!maxType || *maxType < *etype) maxType = etype;
if((maxType->id() == nj::int64_type || maxType->id() == nj::uint64_type) && etype->id() == nj::float64_type) maxType = etype;
if(maxType->id() == nj::float64_type && (etype->id() == nj::int64_type || etype->id() == nj::uint64_type)) continue;
if(maxType != etype && !(*maxType < *etype)) maxType = nj::Any_t::instance();
}
}
}
Expand Down Expand Up @@ -251,7 +252,7 @@ static shared_ptr<nj::Value> createArrayReqFromArray(const Local<Value> &from)

if(etype)
{
switch(etype->getId())
switch(etype->id())
{
case nj::null_type:
to.reset(new nj::Array<unsigned char,nj::Null_t>(dims));
Expand Down
4 changes: 2 additions & 2 deletions src/rvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static jl_value_t *rPrimitive(const nj::Primitive &prim) throw(nj::JuliaExceptio
{
jl_value_t *res = 0;

switch(prim.type()->getId())
switch(prim.type()->id())
{
case nj::null_type: res = (jl_value_t*)jl_null; break;
case nj::boolean_type:
Expand Down Expand Up @@ -208,7 +208,7 @@ static jl_array_t *rArray(const shared_ptr<nj::Value> &array)
jl_array_t *res = 0;
const nj::Array_t *atype = static_cast<const nj::Array_t*>(array->type());

switch(atype->etype()->getId())
switch(atype->etype()->id())
{
case nj::null_type: res = arrayFromElements<unsigned char,nj::Null_t,getJuliaNullElement>(array,JVOID_T); break;
case nj::boolean_type: res = arrayFromBuffer<unsigned char,nj::Boolean_t>(array,jl_bool_type); break;
Expand Down
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ describe('Regression Tests',function()
expect(execA(julia,'typecheckArray',[1,1.1])).to.equal('float');
});

it('Typecheck [Float,Integer] -> [Float]',function()
{
expect(execA(julia,'typecheckArray',[1.1,1])).to.equal('float');
});

it('Prevent widening String elements',function()
{
expect(execA(julia,'typecheckArray',[true,"x",1,1.1])).to.equal('none');
Expand Down

0 comments on commit 599d533

Please sign in to comment.