Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global arrays #93

Merged
merged 3 commits into from
Mar 3, 2018
Merged
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

This file was deleted.

Binary file not shown.
140 changes: 40 additions & 100 deletions packages/walt-compiler/src/__tests__/compiler-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,118 +10,58 @@ test("empty module compilation", t =>
t.is(module instanceof WebAssembly.Module, true);
}));

test("global declaration compilation", t =>
compileAndRun("let answer: i32 = 42;").then(({ module, instance }) => {
t.is(instance instanceof WebAssembly.Instance, true);
t.is(module instanceof WebAssembly.Module, true);
}));
test("invalid imports throw", t =>
t.throws(() => compile("import foo from 'bar'")));

test("global indexes compile correctly", t =>
test("compiler basics", t =>
compileAndRun(
`import { two: TwoType, alsoTwo: TwoType } from 'env';
type TwoType = () => i32;
// Memory
const memory: Memory = { 'initial': 1 };
const bar: i32 = 2;
// Const globals, export
export const bar: i32 = 2;
let foo: i32 = 3;
let baz: i32 = 0;
let x: i32;

// Function export
export function test(): i32 {
x = 1;
foo = two();
// Local vs global, test scope
const foo: i32 = two();
// set global
baz = alsoTwo();
return foo + baz;
}`,
{ env: { two: () => 2, alsoTwo: () => 2 } }
).then(module => t.is(module.instance.exports.test(), 4)));

test("global constant exports", t =>
compileAndRun(`
export const a: i32 = 42;
export const b: f64 = 42.6;
`).then(result => {
t.is(result.instance.exports.a, 42);
t.is(result.instance.exports.b, 42.6);
}));
// global references, math
return 2 * 2 + foo + baz;
}

test("function exports", t =>
compileAndRun(`
export function echo() : i32 {
return 48;
}
`).then(result => {
t.is(result.instance.exports.echo(), 48);
}));
export function testLargeSignedConstant(): i32 {
return 126;
}
function number(): i64 {
const x: i64 = 42;
return x;
}
function two() : i64 {
return 2;
}
export function test64BitConstants(): i32 {
return number(): i32;
}

test("function locals", t =>
compileAndRun(`
export function echo() : i32 {
const x : i32 = 42;
return x;
}
`).then(result => {
t.is(result.instance.exports.echo(), 42);
const gArray: i32[] = 0;
export function testGlobalArray(): i32 {
gArray[0] = 2;
gArray[1] = 2;
return gArray[0] + gArray[1];
}
`,
{ env: { two: () => 2, alsoTwo: () => 2 } }
).then(module => {
t.is(module.instance.exports.bar, 2);
t.is(module.instance.exports.test(), 8);
t.is(module.instance.exports.testLargeSignedConstant(), 126);
t.is(module.instance.exports.testGlobalArray(), 4);
}));

test.skip("exports must have a value", t =>
t.throws(() =>
compileAndRun(`
export const x: i32;
`)
));

test("function scope", t =>
compileAndRun(`
const x : i32 = 11;
export function test() : i32 {
const x : i32 = 42;
return x;
}
`).then(result => t.is(result.instance.exports.test(), 42)));

test("global reference in function scope", t =>
compileAndRun(`
const x : i32 = 42;
export function test() : i32 {
return x;
}
`).then(result => t.is(result.instance.exports.test(), 42)));

test("compiles large signed consants correctly", t =>
compileAndRun(`
export function test(): i32 {
return 126;
}
`).then(result => t.is(result.instance.exports.test(), 126)));

test("compiles large signed global consants correctly", t =>
compileAndRun(`
const x: i32 = 126;
export function test(): i32 {
return x;
}
`).then(result => t.is(result.instance.exports.test(), 126)));

test("compiles math", t =>
compileAndRun(`
export function test(): i32 {
return 2 + 2 - 4;
}
`).then(result => t.is(result.instance.exports.test(), 0)));

test("invalid imports throw", t =>
t.throws(() => compile("import foo from 'bar'")));

test("64 bit constant encoding", t => {
return compileAndRun(`
function number(): i64 {
const x: i64 = 42;
return x;
}
function two() : i64 {
return 2;
}
export function test(): i32 {
return number(): i32;
}
`).then(result => t.is(result.instance.exports.test(), 42));
});
Loading