Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ Vp.returnType = function returnType(stmt) {
return ty.Double;
});

when({
type: 'UnaryExpression',
operator: '-',
argument: {
type: 'Literal',
value: match.number,
raw: hasDot
}
}, function() {
return ty.Double;
});

when({
type: 'Literal',
value: match.all(match.number,
Expand All @@ -256,6 +268,19 @@ Vp.returnType = function returnType(stmt) {
}, function() {
return ty.Signed;
});

when({
type: 'UnaryExpression',
operator: '-',
argument: {
type: 'Literal',
value: match.all(match.number,
match.range(0, 0x80000001)),
raw: dotless
}
}, function() {
return ty.Signed;
});
});
};

Expand Down Expand Up @@ -471,12 +496,36 @@ Vp.local = function local(x, rhs) {
return ty.Double;
}, this);

when({
type: 'UnaryExpression',
operator: '-',
argument: {
type: 'Literal',
value: match.number,
raw: hasDot
}
}, function() {
return ty.Double;
});

when({
type: 'Literal',
value: match.all(match.integer, match.range(-0x80000000, 0x100000000))
}, function(vars) {
return ty.Int;
}, this);

when({
type: 'UnaryExpression',
operator: '-',
argument: {
type: 'Literal',
value: match.all(match.number, match.range(0, 0x80000001)),
raw: dotless
}
}, function() {
return ty.Int;
});
});
};

Expand Down Expand Up @@ -894,6 +943,19 @@ Vp.expression = function expression(e) {
return this.expression(last);
}, this);

when({
type: 'UnaryExpression',
operator: '-',
argument: {
type: 'Literal',
value: match.all(match.number,
match.range(0, 0x80000001)),
raw: dotless
}
}, function(vars) {
return ty.Signed;
}, this);

when({
type: 'UnaryExpression',
operator: '~',
Expand Down
28 changes: 28 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,31 @@ exports.testForWithoutUpdate = asmAssert.one(
}
},
{ pass: true });

exports.testNegativeIntReturn = asmAssert.one(
"negative integer literal as return value",
function f() {
return -42;
},
{ pass: true });

exports.testNegativeDoubleReturn = asmAssert.one(
"negative double literal as return value",
function f() {
return -42.1;
},
{ pass: true });

exports.testNegativeIntVar = asmAssert.one(
"negative integer literal as variable declaration",
function f() {
var i = -42;
},
{ pass: true });

exports.testNegativeDoubleVar = asmAssert.one(
"negative double literal as variable declaration",
function f() {
var i = -42.1;
},
{ pass: true });