2015-12-11 12:26:16 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
// Flags: --validate-asm --allow-natives-syntax
|
2015-12-17 10:54:08 +00:00
|
|
|
|
2016-08-19 18:25:39 +00:00
|
|
|
var stdlib = this;
|
2018-08-17 12:05:25 +00:00
|
|
|
let kMinHeapSize = 4096;
|
2016-08-19 18:25:39 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function assertValidAsm(func) {
|
2017-12-18 14:32:57 +00:00
|
|
|
assertTrue(%IsAsmWasmCode(func), "must be valid asm code");
|
2016-08-23 04:06:52 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
function assertWasm(expected, func, ffi) {
|
|
|
|
print("Testing " + func.name + "...");
|
2016-08-23 04:06:52 +00:00
|
|
|
assertEquals(
|
2018-08-17 12:05:25 +00:00
|
|
|
expected, func(stdlib, ffi, new ArrayBuffer(kMinHeapSize)).caller());
|
2016-08-23 04:06:52 +00:00
|
|
|
assertValidAsm(func);
|
2016-03-23 23:18:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function EmptyTest(a, b, c) {
|
2016-01-12 13:33:56 +00:00
|
|
|
"use asm";
|
|
|
|
function caller() {
|
|
|
|
empty();
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
function empty() {
|
|
|
|
}
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(11, EmptyTest);
|
2016-01-12 13:33:56 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function VoidReturnTest(a, b, c) {
|
2016-04-29 09:15:26 +00:00
|
|
|
"use asm";
|
|
|
|
function caller() {
|
|
|
|
empty();
|
|
|
|
return 19;
|
|
|
|
}
|
|
|
|
function empty() {
|
|
|
|
var x = 0;
|
|
|
|
if (x) return;
|
|
|
|
}
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(19, VoidReturnTest);
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function IntTest(a, b, c) {
|
2015-12-11 12:26:16 +00:00
|
|
|
"use asm";
|
|
|
|
function sum(a, b) {
|
|
|
|
a = a|0;
|
|
|
|
b = b|0;
|
2016-07-13 19:38:13 +00:00
|
|
|
var c = 0;
|
2016-01-12 13:45:35 +00:00
|
|
|
var d = 3.0;
|
2016-07-13 19:38:13 +00:00
|
|
|
var e = 0;
|
|
|
|
e = ~~d; // double conversion
|
|
|
|
c = (b + 1)|0
|
2015-12-11 12:26:16 +00:00
|
|
|
return (a + c + 1)|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
return sum(77,22) | 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(101,IntTest);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function Float64Test() {
|
|
|
|
"use asm";
|
|
|
|
function sum(a, b) {
|
|
|
|
a = +a;
|
|
|
|
b = +b;
|
|
|
|
return +(a + b);
|
|
|
|
}
|
|
|
|
|
|
|
|
function caller() {
|
2016-07-13 19:38:13 +00:00
|
|
|
var a = 0.0;
|
2017-03-14 20:17:04 +00:00
|
|
|
var ret = 0;
|
2016-07-13 19:38:13 +00:00
|
|
|
a = +sum(70.1,10.2);
|
2015-12-11 12:26:16 +00:00
|
|
|
if (a == 80.3) {
|
|
|
|
ret = 1|0;
|
|
|
|
} else {
|
|
|
|
ret = 0|0;
|
|
|
|
}
|
|
|
|
return ret|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(1, Float64Test);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function BadModule() {
|
|
|
|
"use asm";
|
|
|
|
function caller(a, b) {
|
|
|
|
a = a|0;
|
|
|
|
b = b+0;
|
2016-07-13 19:38:13 +00:00
|
|
|
var c = 0;
|
|
|
|
c = (b + 1)|0
|
2015-12-11 12:26:16 +00:00
|
|
|
return (a + c + 1)|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
return call(1, 2)|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(BadModule));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestReturnInBlock() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
if(1) {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(1, TestReturnInBlock);
|
|
|
|
|
|
|
|
|
|
|
|
function TestAddSimple() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 0;
|
|
|
|
x = (x + 1)|0;
|
|
|
|
return x|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(1, TestAddSimple);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestWhileSimple() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
while((x|0) < 5) {
|
2015-12-11 12:26:16 +00:00
|
|
|
x = (x + 1)|0;
|
|
|
|
}
|
|
|
|
return x|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(5, TestWhileSimple);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestWhileWithoutBraces() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
while((x|0) <= 3)
|
2015-12-11 12:26:16 +00:00
|
|
|
x = (x + 1)|0;
|
|
|
|
return x|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(4, TestWhileWithoutBraces);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestReturnInWhile() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
while((x|0) < 10) {
|
2015-12-11 12:26:16 +00:00
|
|
|
x = (x + 6)|0;
|
|
|
|
return x|0;
|
|
|
|
}
|
|
|
|
return x|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(6, TestReturnInWhile);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestReturnInWhileWithoutBraces() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
while((x|0) < 5)
|
2015-12-11 12:26:16 +00:00
|
|
|
return 7;
|
|
|
|
return x|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(7, TestReturnInWhileWithoutBraces);
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-04-29 09:15:26 +00:00
|
|
|
function TestBreakInIf() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
label: {
|
|
|
|
if(1) break label;
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(12, TestBreakInIf);
|
|
|
|
|
|
|
|
function TestBreakInIfInDoWhileFalse() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
do {
|
|
|
|
if(1) break;
|
|
|
|
return 11;
|
|
|
|
} while(0);
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(12, TestBreakInIfInDoWhileFalse);
|
|
|
|
|
|
|
|
function TestBreakInElse() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
do {
|
|
|
|
if(0) ;
|
|
|
|
else break;
|
|
|
|
return 14;
|
|
|
|
} while(0);
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(15, TestBreakInElse);
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestBreakInWhile() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
while(1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(8, TestBreakInWhile);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-04-29 09:15:26 +00:00
|
|
|
|
|
|
|
function TestBreakInIfInWhile() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
while(1) {
|
|
|
|
if (1) break;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(8, TestBreakInIfInWhile);
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestBreakInNestedWhile() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 1.0;
|
2016-07-13 19:38:13 +00:00
|
|
|
var ret = 0;
|
2015-12-11 12:26:16 +00:00
|
|
|
while(x < 1.5) {
|
|
|
|
while(1)
|
|
|
|
break;
|
|
|
|
x = +(x + 0.25);
|
|
|
|
}
|
|
|
|
if (x == 1.5) {
|
|
|
|
ret = 9;
|
|
|
|
}
|
|
|
|
return ret|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(9, TestBreakInNestedWhile);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestBreakInBlock() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 0;
|
|
|
|
abc: {
|
|
|
|
x = 10;
|
2016-06-28 23:48:45 +00:00
|
|
|
if ((x|0) == 10) {
|
2015-12-11 12:26:16 +00:00
|
|
|
break abc;
|
|
|
|
}
|
|
|
|
x = 20;
|
|
|
|
}
|
|
|
|
return x|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(10, TestBreakInBlock);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestBreakInNamedWhile() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 0;
|
|
|
|
outer: while (1) {
|
|
|
|
x = (x + 1)|0;
|
2016-06-28 23:48:45 +00:00
|
|
|
while ((x|0) == 11) {
|
2015-12-11 12:26:16 +00:00
|
|
|
break outer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(11, TestBreakInNamedWhile);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestContinue() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 5;
|
|
|
|
var ret = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
while ((x|0) >= 0) {
|
2015-12-11 12:26:16 +00:00
|
|
|
x = (x - 1)|0;
|
2016-06-28 23:48:45 +00:00
|
|
|
if ((x|0) == 2) {
|
2015-12-11 12:26:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ret = (ret - 1)|0;
|
|
|
|
}
|
|
|
|
return ret|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(-5, TestContinue);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestContinueInNamedWhile() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 5;
|
|
|
|
var y = 0;
|
|
|
|
var ret = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
outer: while ((x|0) > 0) {
|
2015-12-11 12:26:16 +00:00
|
|
|
x = (x - 1)|0;
|
|
|
|
y = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
while ((y|0) < 5) {
|
|
|
|
if ((x|0) == 3) {
|
2015-12-11 12:26:16 +00:00
|
|
|
continue outer;
|
|
|
|
}
|
|
|
|
ret = (ret + 1)|0;
|
|
|
|
y = (y + 1)|0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(20, TestContinueInNamedWhile);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2017-02-14 08:57:35 +00:00
|
|
|
function TestContinueInDoWhileFalse() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
do {
|
|
|
|
continue;
|
2017-03-14 20:17:04 +00:00
|
|
|
} while (0);
|
2017-02-14 08:57:35 +00:00
|
|
|
return 47;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(47, TestContinueInDoWhileFalse);
|
|
|
|
|
|
|
|
|
2017-12-18 14:32:57 +00:00
|
|
|
function TestContinueInForLoop() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var i = 0;
|
|
|
|
for (; (i|0) < 10; i = (i+1)|0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return 4711;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(4711, TestContinueInForLoop);
|
|
|
|
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestNot() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
2016-07-13 19:38:13 +00:00
|
|
|
var a = 0;
|
|
|
|
a = !(2 > 3);
|
2015-12-11 12:26:16 +00:00
|
|
|
return a | 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(1, TestNot);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestNotEquals() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = 3;
|
2016-06-28 23:48:45 +00:00
|
|
|
if ((a|0) != 2) {
|
2015-12-11 12:26:16 +00:00
|
|
|
return 21;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(21, TestNotEquals);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestUnsignedComparison() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = 0xffffffff;
|
|
|
|
if ((a>>>0) > (0>>>0)) {
|
|
|
|
return 22;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(22, TestUnsignedComparison);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestMixedAdd() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = 0x80000000;
|
|
|
|
var b = 0x7fffffff;
|
|
|
|
var c = 0;
|
|
|
|
c = ((a>>>0) + b)|0;
|
|
|
|
if ((c >>> 0) > (0>>>0)) {
|
2016-06-28 23:48:45 +00:00
|
|
|
if ((c|0) < 0) {
|
2015-12-11 12:26:16 +00:00
|
|
|
return 23;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(23, TestMixedAdd);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2016-01-14 23:08:32 +00:00
|
|
|
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestConvertI32() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = 1.5;
|
|
|
|
if ((~~(a + a)) == 3) {
|
|
|
|
return 24;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(24, TestConvertI32);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestConvertF64FromInt() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = 1;
|
2016-03-23 23:19:54 +00:00
|
|
|
if ((+((a + a)|0)) > 1.5) {
|
2015-12-11 12:26:16 +00:00
|
|
|
return 25;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(25, TestConvertF64FromInt);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestConvertF64FromUnsigned() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = 0xffffffff;
|
|
|
|
if ((+(a>>>0)) > 0.0) {
|
2016-03-23 23:19:54 +00:00
|
|
|
if((+(a|0)) < 0.0) {
|
2015-12-11 12:26:16 +00:00
|
|
|
return 26;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(26, TestConvertF64FromUnsigned);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestModInt() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = -83;
|
|
|
|
var b = 28;
|
|
|
|
return ((a|0)%(b|0))|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(-27,TestModInt);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestModUnsignedInt() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = 0x80000000; //2147483648
|
|
|
|
var b = 10;
|
|
|
|
return ((a>>>0)%(b>>>0))|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(8, TestModUnsignedInt);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestModDouble() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = 5.25;
|
|
|
|
var b = 2.5;
|
|
|
|
if (a%b == 0.25) {
|
|
|
|
return 28;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(28, TestModDouble);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestModDoubleNegative() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var a = -34359738368.25;
|
|
|
|
var b = 2.5;
|
|
|
|
if (a%b == -0.75) {
|
|
|
|
return 28;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(28, TestModDoubleNegative);
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2016-05-25 16:33:29 +00:00
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
(function () {
|
2015-12-11 14:09:19 +00:00
|
|
|
function TestNamedFunctions() {
|
2015-12-11 12:26:16 +00:00
|
|
|
"use asm";
|
|
|
|
|
|
|
|
var a = 0.0;
|
|
|
|
var b = 0.0;
|
|
|
|
|
|
|
|
function add() {
|
2015-12-11 14:09:19 +00:00
|
|
|
return +(a + b);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 14:09:19 +00:00
|
|
|
function init() {
|
|
|
|
a = 43.25;
|
|
|
|
b = 34.25;
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 14:09:19 +00:00
|
|
|
return {init:init,
|
|
|
|
add:add};
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
var module_decl = eval('(' + TestNamedFunctions.toString() + ')');
|
|
|
|
var module = module_decl(stdlib);
|
|
|
|
assertValidAsm(module_decl);
|
2015-12-11 14:09:19 +00:00
|
|
|
module.init();
|
|
|
|
assertEquals(77.5, module.add());
|
2016-03-23 23:18:44 +00:00
|
|
|
})();
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-25 16:33:29 +00:00
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
(function () {
|
2015-12-11 12:26:16 +00:00
|
|
|
function TestGlobalsWithInit() {
|
|
|
|
"use asm";
|
|
|
|
|
2015-12-11 14:09:19 +00:00
|
|
|
var a = 43.25;
|
|
|
|
var b = 34.25;
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
function add() {
|
|
|
|
return +(a + b);
|
|
|
|
}
|
|
|
|
|
2015-12-11 14:09:19 +00:00
|
|
|
return {add:add};
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
var module_decl = eval('(' + TestGlobalsWithInit.toString() + ')');
|
|
|
|
var module = module_decl(stdlib);
|
|
|
|
assertValidAsm(module_decl);
|
2015-12-11 12:26:16 +00:00
|
|
|
assertEquals(77.5, module.add());
|
2016-03-23 23:18:44 +00:00
|
|
|
})();
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-16 12:10:13 +00:00
|
|
|
function TestForLoop() {
|
|
|
|
"use asm"
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var ret = 0;
|
|
|
|
var i = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
for (i = 2; (i|0) <= 10; i = (i+1)|0) {
|
2015-12-16 12:10:13 +00:00
|
|
|
ret = (ret + i) | 0;
|
|
|
|
}
|
|
|
|
return ret|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(54, TestForLoop);
|
2015-12-16 12:10:13 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-16 12:10:13 +00:00
|
|
|
function TestForLoopWithoutInit() {
|
|
|
|
"use asm"
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var ret = 0;
|
|
|
|
var i = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
for (; (i|0) < 10; i = (i+1)|0) {
|
2015-12-16 12:10:13 +00:00
|
|
|
ret = (ret + 10) | 0;
|
|
|
|
}
|
|
|
|
return ret|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(100,TestForLoopWithoutInit);
|
2015-12-16 12:10:13 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-16 12:10:13 +00:00
|
|
|
function TestForLoopWithoutCondition() {
|
|
|
|
"use asm"
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var ret = 0;
|
|
|
|
var i = 0;
|
|
|
|
for (i=1;; i = (i+1)|0) {
|
|
|
|
ret = (ret + i) | 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
if ((i|0) == 11) {
|
2015-12-16 12:10:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(66, TestForLoopWithoutCondition);
|
2015-12-16 12:10:13 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-16 12:10:13 +00:00
|
|
|
function TestForLoopWithoutNext() {
|
|
|
|
"use asm"
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var i = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
for (i=1; (i|0) < 41;) {
|
2015-12-16 12:10:13 +00:00
|
|
|
i = (i + 1) | 0;
|
|
|
|
}
|
|
|
|
return i|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(41, TestForLoopWithoutNext);
|
2015-12-16 12:10:13 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2015-12-16 12:10:13 +00:00
|
|
|
function TestForLoopWithoutBody() {
|
|
|
|
"use asm"
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var i = 0;
|
2016-06-28 23:48:45 +00:00
|
|
|
for (i=1; (i|0) < 45 ; i = (i+1)|0) {
|
2015-12-16 12:10:13 +00:00
|
|
|
}
|
|
|
|
return i|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(45, TestForLoopWithoutBody);
|
2016-01-04 23:46:18 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2016-01-04 23:46:18 +00:00
|
|
|
function TestDoWhile() {
|
|
|
|
"use asm"
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var i = 0;
|
|
|
|
var ret = 21;
|
|
|
|
do {
|
|
|
|
ret = (ret + ret)|0;
|
|
|
|
i = (i + 1)|0;
|
2016-06-28 23:48:45 +00:00
|
|
|
} while ((i|0) < 2);
|
2016-01-04 23:46:18 +00:00
|
|
|
return ret|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(84, TestDoWhile);
|
2016-01-04 23:46:18 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2016-01-04 23:46:18 +00:00
|
|
|
function TestConditional() {
|
|
|
|
"use asm"
|
|
|
|
|
|
|
|
function caller() {
|
|
|
|
var x = 1;
|
2016-06-28 23:48:45 +00:00
|
|
|
return (((x|0) > 0) ? 41 : 71)|0;
|
2016-01-04 23:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(41, TestConditional);
|
2016-01-12 02:02:33 +00:00
|
|
|
|
2016-01-14 22:09:46 +00:00
|
|
|
|
2016-01-13 01:23:43 +00:00
|
|
|
function TestInitFunctionWithNoGlobals() {
|
|
|
|
"use asm";
|
|
|
|
function caller() {
|
|
|
|
return 51;
|
|
|
|
}
|
2017-03-14 20:17:04 +00:00
|
|
|
return {caller:caller};
|
2016-01-13 01:23:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
assertWasm(51, TestInitFunctionWithNoGlobals);
|
|
|
|
|
2016-01-13 01:23:43 +00:00
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
(function () {
|
2016-01-13 01:23:43 +00:00
|
|
|
function TestExportNameDifferentFromFunctionName() {
|
|
|
|
"use asm";
|
|
|
|
function caller() {
|
|
|
|
return 55;
|
|
|
|
}
|
|
|
|
return {alt_caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
var module_decl = eval(
|
|
|
|
'(' + TestExportNameDifferentFromFunctionName.toString() + ')');
|
|
|
|
var module = module_decl(stdlib);
|
|
|
|
assertValidAsm(module_decl);
|
2016-01-13 01:23:43 +00:00
|
|
|
assertEquals(55, module.alt_caller());
|
2016-03-23 23:18:44 +00:00
|
|
|
})();
|
2016-01-20 23:36:42 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
|
2016-01-20 23:36:42 +00:00
|
|
|
function TestFunctionTableSingleFunction() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function dummy() {
|
|
|
|
return 71;
|
|
|
|
}
|
|
|
|
|
|
|
|
function caller() {
|
2016-07-13 19:38:13 +00:00
|
|
|
// TODO(jpp): the parser optimizes function_table[0&0] to function table[0].
|
|
|
|
var v = 0;
|
|
|
|
return function_table[v&0]() | 0;
|
2016-01-20 23:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var function_table = [dummy]
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(71, TestFunctionTableSingleFunction);
|
2016-01-20 23:36:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
function TestFunctionTableMultipleFunctions() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function inc1(x) {
|
|
|
|
x = x|0;
|
|
|
|
return (x+1)|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function inc2(x) {
|
|
|
|
x = x|0;
|
|
|
|
return (x+2)|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function caller() {
|
2016-07-13 19:38:13 +00:00
|
|
|
var i = 0, j = 1;
|
|
|
|
if ((function_table[i&1](50)|0) == 51) {
|
|
|
|
if ((function_table[j&1](60)|0) == 62) {
|
2016-01-20 23:36:42 +00:00
|
|
|
return 73;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var function_table = [inc1, inc2]
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(73, TestFunctionTableMultipleFunctions);
|
2016-01-20 23:36:42 +00:00
|
|
|
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
(function () {
|
2016-08-23 04:06:52 +00:00
|
|
|
function TestFunctionTable(stdlib, foreign, buffer) {
|
2016-01-20 23:36:42 +00:00
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function add(a, b) {
|
|
|
|
a = a|0;
|
|
|
|
b = b|0;
|
|
|
|
return (a+b)|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sub(a, b) {
|
|
|
|
a = a|0;
|
|
|
|
b = b|0;
|
|
|
|
return (a-b)|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function inc(a) {
|
|
|
|
a = a|0;
|
|
|
|
return (a+1)|0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function caller(table_id, fun_id, arg1, arg2) {
|
|
|
|
table_id = table_id|0;
|
|
|
|
fun_id = fun_id|0;
|
|
|
|
arg1 = arg1|0;
|
|
|
|
arg2 = arg2|0;
|
2016-06-28 23:48:45 +00:00
|
|
|
if ((table_id|0) == 0) {
|
2016-01-20 23:36:42 +00:00
|
|
|
return funBin[fun_id&3](arg1, arg2)|0;
|
2016-06-28 23:48:45 +00:00
|
|
|
} else if ((table_id|0) == 1) {
|
2016-01-20 23:36:42 +00:00
|
|
|
return fun[fun_id&0](arg1)|0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var funBin = [add, sub, sub, add];
|
|
|
|
var fun = [inc];
|
|
|
|
|
|
|
|
return {caller:caller};
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
print("TestFunctionTable...");
|
2016-08-23 04:06:52 +00:00
|
|
|
var module = TestFunctionTable(stdlib);
|
2016-01-20 23:36:42 +00:00
|
|
|
assertEquals(55, module.caller(0, 0, 33, 22));
|
|
|
|
assertEquals(11, module.caller(0, 1, 33, 22));
|
|
|
|
assertEquals(9, module.caller(0, 2, 54, 45));
|
|
|
|
assertEquals(99, module.caller(0, 3, 54, 45));
|
|
|
|
assertEquals(23, module.caller(0, 4, 12, 11));
|
|
|
|
assertEquals(31, module.caller(1, 0, 30, 11));
|
2016-03-23 23:18:44 +00:00
|
|
|
})();
|
2016-02-05 02:07:23 +00:00
|
|
|
|
|
|
|
|
2016-02-16 18:34:07 +00:00
|
|
|
(function TestComma() {
|
|
|
|
function CommaModule() {
|
|
|
|
"use asm";
|
|
|
|
|
|
|
|
function ifunc(a, b) {
|
|
|
|
a = +a;
|
|
|
|
b = b | 0;
|
|
|
|
return (a, b) | 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dfunc(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = +b;
|
|
|
|
return +(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {ifunc: ifunc, dfunc: dfunc};
|
|
|
|
}
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
var module_decl = eval('(' + CommaModule.toString() + ')');
|
|
|
|
var m = module_decl(stdlib);
|
|
|
|
assertValidAsm(module_decl);
|
2016-02-16 18:34:07 +00:00
|
|
|
assertEquals(123, m.ifunc(456.7, 123));
|
|
|
|
assertEquals(123.4, m.dfunc(456, 123.4));
|
|
|
|
})();
|
2016-02-22 05:55:27 +00:00
|
|
|
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
function TestFloatAsDouble(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
var fround = stdlib.Math.fround;
|
|
|
|
function func() {
|
|
|
|
var x = fround(1.0);
|
|
|
|
return +fround(x);
|
2016-03-14 17:29:08 +00:00
|
|
|
}
|
2016-03-23 23:18:44 +00:00
|
|
|
return {caller: func};
|
|
|
|
}
|
|
|
|
assertWasm(1, TestFloatAsDouble);
|
2016-03-14 17:29:08 +00:00
|
|
|
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
function TestOr() {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var x = 1;
|
|
|
|
var y = 2;
|
|
|
|
return (x | y) | 0;
|
2016-02-22 05:55:27 +00:00
|
|
|
}
|
2016-03-23 23:18:44 +00:00
|
|
|
return {caller: func};
|
|
|
|
}
|
2016-02-22 05:55:27 +00:00
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(3, TestOr);
|
2016-02-22 05:55:27 +00:00
|
|
|
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
function TestAnd() {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var x = 3;
|
|
|
|
var y = 2;
|
|
|
|
return (x & y) | 0;
|
2016-02-22 05:55:27 +00:00
|
|
|
}
|
2016-03-23 23:18:44 +00:00
|
|
|
return {caller: func};
|
|
|
|
}
|
2016-02-22 05:55:27 +00:00
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(2, TestAnd);
|
2016-02-22 05:55:27 +00:00
|
|
|
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
function TestXor() {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var x = 3;
|
|
|
|
var y = 2;
|
|
|
|
return (x ^ y) | 0;
|
2016-02-22 05:55:27 +00:00
|
|
|
}
|
2016-03-23 23:18:44 +00:00
|
|
|
return {caller: func};
|
|
|
|
}
|
2016-02-22 05:55:27 +00:00
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
assertWasm(1, TestXor);
|
2016-02-23 06:18:37 +00:00
|
|
|
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function TestIntegerMultiplyBothWays(stdlib, foreign, heap) {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var a = 1;
|
|
|
|
return (((a * 3)|0) + ((4 * a)|0)) | 0;
|
2016-02-23 19:03:50 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
return {caller: func};
|
|
|
|
}
|
2016-02-23 19:03:50 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
assertWasm(7, TestIntegerMultiplyBothWays);
|
2016-02-23 19:03:50 +00:00
|
|
|
|
|
|
|
|
2016-05-25 16:33:29 +00:00
|
|
|
(function TestBadAssignDoubleFromIntish() {
|
|
|
|
function Module(stdlib, foreign, heap) {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var a = 1;
|
|
|
|
var b = 3.0;
|
|
|
|
b = a;
|
|
|
|
}
|
|
|
|
return {func: func};
|
|
|
|
}
|
2016-11-30 00:25:21 +00:00
|
|
|
print("TestBadAssignDoubleFromIntish...");
|
2016-08-23 04:06:52 +00:00
|
|
|
Module(stdlib);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-05-25 16:33:29 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
(function TestBadAssignIntFromDouble() {
|
|
|
|
function Module(stdlib, foreign, heap) {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var a = 1;
|
|
|
|
var b = 3.0;
|
|
|
|
a = b;
|
|
|
|
}
|
|
|
|
return {func: func};
|
|
|
|
}
|
2016-11-30 00:25:21 +00:00
|
|
|
print("TestBadAssignIntFromDouble...");
|
2016-08-23 04:06:52 +00:00
|
|
|
Module(stdlib);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-05-25 16:33:29 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
2016-02-23 19:03:50 +00:00
|
|
|
(function TestBadMultiplyIntish() {
|
|
|
|
function Module(stdlib, foreign, heap) {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var a = 1;
|
|
|
|
return ((a + a) * 4) | 0;
|
|
|
|
}
|
|
|
|
return {func: func};
|
|
|
|
}
|
2016-11-30 00:25:21 +00:00
|
|
|
print("TestBadMultiplyIntish...");
|
2016-08-23 04:06:52 +00:00
|
|
|
Module(stdlib);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-02-23 19:03:50 +00:00
|
|
|
})();
|
2016-02-23 21:09:43 +00:00
|
|
|
|
|
|
|
|
2016-03-23 23:19:54 +00:00
|
|
|
(function TestBadCastFromInt() {
|
|
|
|
function Module(stdlib, foreign, heap) {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var a = 1;
|
|
|
|
return +a;
|
|
|
|
}
|
|
|
|
return {func: func};
|
|
|
|
}
|
2016-11-30 00:25:21 +00:00
|
|
|
print("TestBadCastFromInt...");
|
2016-08-23 04:06:52 +00:00
|
|
|
Module(stdlib);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-03-23 23:19:54 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function TestAndNegative() {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var x = 1;
|
|
|
|
var y = 2;
|
|
|
|
var z = 0;
|
|
|
|
z = x + y & -1;
|
|
|
|
return z | 0;
|
2016-02-23 21:09:43 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
return {caller: func};
|
|
|
|
}
|
2016-02-23 21:09:43 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
assertWasm(3, TestAndNegative);
|
2016-02-23 21:09:43 +00:00
|
|
|
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function TestNegativeDouble() {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
2017-03-14 20:17:04 +00:00
|
|
|
var x = -34359738368.25;
|
2016-08-23 04:06:52 +00:00
|
|
|
var y = -2.5;
|
|
|
|
return +(x + y);
|
2016-02-23 21:09:43 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
return {caller: func};
|
|
|
|
}
|
2016-02-23 21:09:43 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
assertWasm(-34359738370.75, TestNegativeDouble);
|
2016-02-23 21:09:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
(function TestBadAndDouble() {
|
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
var x = 1.0;
|
|
|
|
var y = 2.0;
|
|
|
|
return (x & y) | 0;
|
|
|
|
}
|
|
|
|
return {func: func};
|
|
|
|
}
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
Module(stdlib);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-02-23 21:09:43 +00:00
|
|
|
})();
|
2016-02-24 07:35:09 +00:00
|
|
|
|
|
|
|
|
2016-12-07 15:55:23 +00:00
|
|
|
(function TestBadExportKey() {
|
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
}
|
|
|
|
return {123: func};
|
|
|
|
}
|
|
|
|
|
|
|
|
Module(stdlib);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-12-07 15:55:23 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
2017-03-14 20:17:04 +00:00
|
|
|
/*
|
|
|
|
// TODO(bradnelson): Technically invalid, but useful to cover unicode, revises
|
|
|
|
// and re-enable.
|
2016-12-08 08:52:33 +00:00
|
|
|
(function TestUnicodeExportKey() {
|
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
function func() {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
return {"\u00d1\u00e6": func};
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = Module(stdlib);
|
|
|
|
assertEquals(42, m.Ñæ());
|
|
|
|
assertValidAsm(Module);
|
|
|
|
})();
|
2017-03-14 20:17:04 +00:00
|
|
|
*/
|
2016-12-08 08:52:33 +00:00
|
|
|
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function TestAndIntAndHeapValue(stdlib, foreign, buffer) {
|
|
|
|
"use asm";
|
|
|
|
var HEAP32 = new stdlib.Int32Array(buffer);
|
|
|
|
function func() {
|
|
|
|
var x = 0;
|
|
|
|
x = HEAP32[0] & -1;
|
|
|
|
return x | 0;
|
2016-02-24 07:35:09 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
return {caller: func};
|
|
|
|
}
|
2016-02-24 07:35:09 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
assertWasm(0, TestAndIntAndHeapValue);
|
2016-02-27 20:38:55 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
|
|
|
|
function TestOutOfBoundsConversion($a,$b,$c){'use asm';
|
|
|
|
function aaa() {
|
|
|
|
var f = 0.0;
|
|
|
|
var a = 0;
|
|
|
|
f = 5616315000.000001;
|
|
|
|
a = ~~f >>>0;
|
|
|
|
return a | 0;
|
2016-02-27 20:38:55 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
return { caller : aaa };
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(1321347704, TestOutOfBoundsConversion);
|
|
|
|
|
2016-03-23 23:19:54 +00:00
|
|
|
|
|
|
|
(function TestUnsignedLiterals() {
|
|
|
|
function asmModule() {
|
|
|
|
"use asm";
|
|
|
|
function u0xffffffff() {
|
|
|
|
var f = 0xffffffff;
|
|
|
|
return +(f >>> 0);
|
|
|
|
}
|
|
|
|
function u0x80000000() {
|
|
|
|
var f = 0x80000000;
|
|
|
|
return +(f >>> 0);
|
|
|
|
}
|
|
|
|
function u0x87654321() {
|
|
|
|
var f = 0x87654321;
|
|
|
|
return +(f >>> 0);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
u0xffffffff: u0xffffffff,
|
|
|
|
u0x80000000: u0x80000000,
|
|
|
|
u0x87654321: u0x87654321,
|
|
|
|
};
|
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
var decl = eval('(' + asmModule.toString() + ')');
|
|
|
|
var wasm = decl(stdlib);
|
|
|
|
assertValidAsm(decl);
|
2016-03-23 23:19:54 +00:00
|
|
|
assertEquals(0xffffffff, wasm.u0xffffffff());
|
|
|
|
assertEquals(0x80000000, wasm.u0x80000000());
|
|
|
|
assertEquals(0x87654321, wasm.u0x87654321());
|
|
|
|
})();
|
2016-06-28 20:57:23 +00:00
|
|
|
|
2016-06-29 00:39:49 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function TestIfWithUnsigned() {
|
|
|
|
"use asm";
|
|
|
|
function main() {
|
|
|
|
if (2147483658) { // 2^31 + 10
|
|
|
|
return 231;
|
2016-06-29 00:39:49 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
return 0;
|
2016-06-29 00:39:49 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
return {caller:main};
|
|
|
|
}
|
2016-06-29 00:39:49 +00:00
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
assertWasm(231, TestIfWithUnsigned);
|
|
|
|
|
|
|
|
|
|
|
|
function TestLoopsWithUnsigned() {
|
|
|
|
"use asm";
|
|
|
|
function main() {
|
|
|
|
var val = 1;
|
|
|
|
var count = 0;
|
|
|
|
for (val = 2147483648; 2147483648;) {
|
|
|
|
val = 2147483649;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (val>>>0) {
|
|
|
|
val = (val + 1) | 0;
|
|
|
|
count = (count + 1)|0;
|
|
|
|
if ((count|0) == 9) {
|
2016-06-29 00:39:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
}
|
|
|
|
count = 0;
|
|
|
|
do {
|
|
|
|
val = (val + 2) | 0;
|
|
|
|
count = (count + 1)|0;
|
|
|
|
if ((count|0) == 5) {
|
|
|
|
break;
|
2016-06-29 00:39:49 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
} while (0xffffffff);
|
|
|
|
if ((val>>>0) == 2147483668) {
|
|
|
|
return 323;
|
2016-06-29 00:39:49 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
return 0;
|
2016-06-29 00:39:49 +00:00
|
|
|
}
|
2016-08-23 04:06:52 +00:00
|
|
|
return {caller:main};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(323, TestLoopsWithUnsigned);
|
2016-09-19 20:39:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
function TestSingleFunctionModule() {
|
|
|
|
"use asm";
|
|
|
|
function add(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
return (a + b) | 0;
|
|
|
|
}
|
|
|
|
return add;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(7, TestSingleFunctionModule()(3, 4));
|
2016-09-27 23:19:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
function TestNotZero() {
|
|
|
|
"use asm";
|
|
|
|
function caller() {
|
|
|
|
if (!0) {
|
|
|
|
return 44;
|
|
|
|
} else {
|
|
|
|
return 55;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(44, TestNotZero);
|
|
|
|
|
|
|
|
|
|
|
|
function TestNotOne() {
|
|
|
|
"use asm";
|
|
|
|
function caller() {
|
|
|
|
if (!1) {
|
|
|
|
return 44;
|
|
|
|
} else {
|
|
|
|
return 55;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(55, TestNotOne);
|
2016-10-05 19:46:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
function TestDotfulFloat(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
var fround = stdlib.Math.fround;
|
|
|
|
var foo = fround(55.0);
|
|
|
|
function caller() {
|
|
|
|
return +foo;
|
|
|
|
}
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(55, TestDotfulFloat);
|
|
|
|
|
|
|
|
|
2016-12-06 10:42:59 +00:00
|
|
|
function TestDotfulLocalFloat(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
var fround = stdlib.Math.fround;
|
|
|
|
function caller() {
|
|
|
|
var foo = fround(55.0);
|
|
|
|
return +foo;
|
|
|
|
}
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(55, TestDotfulLocalFloat);
|
|
|
|
|
|
|
|
|
2016-10-05 19:46:06 +00:00
|
|
|
function TestDotlessFloat(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
var fround = stdlib.Math.fround;
|
|
|
|
var foo = fround(55);
|
|
|
|
function caller() {
|
|
|
|
return +foo;
|
|
|
|
}
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(55, TestDotlessFloat);
|
|
|
|
|
|
|
|
|
2016-12-06 10:42:59 +00:00
|
|
|
function TestDotlessLocalFloat(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
var fround = stdlib.Math.fround;
|
|
|
|
function caller() {
|
|
|
|
var foo = fround(55);
|
|
|
|
return +foo;
|
|
|
|
}
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(55, TestDotlessLocalFloat);
|
|
|
|
|
|
|
|
|
2016-10-05 19:46:06 +00:00
|
|
|
function TestFloatGlobals(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
var fround = stdlib.Math.fround;
|
|
|
|
var foo = fround(1.25);
|
|
|
|
function caller() {
|
|
|
|
foo = fround(foo + fround(1.0));
|
|
|
|
foo = fround(foo + fround(1.0));
|
|
|
|
return +foo;
|
|
|
|
}
|
|
|
|
return {caller: caller};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertWasm(3.25, TestFloatGlobals);
|
2016-12-01 10:12:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
(function TestExportTwice() {
|
|
|
|
function asmModule() {
|
|
|
|
"use asm";
|
|
|
|
function foo() {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
return {bar: foo, baz: foo};
|
|
|
|
}
|
|
|
|
var m = asmModule();
|
|
|
|
assertEquals(42, m.bar());
|
|
|
|
assertEquals(42, m.baz());
|
|
|
|
})();
|
2020-03-30 16:06:07 +00:00
|
|
|
|
|
|
|
(function TestGenerator() {
|
|
|
|
function* asmModule() {
|
|
|
|
"use asm";
|
|
|
|
function foo() {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
return {foo: foo};
|
|
|
|
}
|
|
|
|
asmModule();
|
|
|
|
assertFalse(%IsAsmWasmCode(asmModule));
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestAsyncFunction() {
|
|
|
|
async function asmModule() {
|
|
|
|
"use asm";
|
|
|
|
function foo() {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
return {foo: foo};
|
|
|
|
}
|
|
|
|
asmModule();
|
|
|
|
assertFalse(%IsAsmWasmCode(asmModule));
|
|
|
|
})();
|