mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-24 12:11:50 +00:00
CString/CArray was an illusion; Update and test tsconfig files
This commit is contained in:
@ -1,50 +0,0 @@
|
||||
/// <reference path="../lib/binaryen.d.ts" />
|
||||
|
||||
import "../src/glue/js";
|
||||
import { NativeType, Module, MemorySegment, BinaryOp } from "../src/module";
|
||||
import { Target } from "../src/compiler";
|
||||
import { U64 } from "../src/util";
|
||||
|
||||
const mod = Module.create();
|
||||
|
||||
mod.setMemory(1, Module.MAX_MEMORY_WASM32, [
|
||||
MemorySegment.create(new Uint8Array(4), U64.fromI32(8)),
|
||||
MemorySegment.create(new Uint8Array(4), U64.fromI32(12))
|
||||
], Target.WASM32, "memory");
|
||||
|
||||
const add = mod.addFunctionType("iii", NativeType.I32, [ NativeType.I32, NativeType.I32 ]);
|
||||
mod.addFunction("add", add, [], mod.createReturn(
|
||||
mod.createBinary(BinaryOp.AddI32,
|
||||
mod.createGetLocal(0, NativeType.I32),
|
||||
mod.createGetLocal(1, NativeType.I32)
|
||||
)
|
||||
));
|
||||
mod.addFunctionExport("add", "add");
|
||||
|
||||
const lit = mod.addFunctionType("I", NativeType.I64, []);
|
||||
mod.addFunction("lit", lit, [], mod.createReturn(
|
||||
mod.createI64(0, 0x80000000) // I64_MIN
|
||||
));
|
||||
mod.addFunctionExport("lit", "lit");
|
||||
|
||||
mod.addGlobal("42", NativeType.I32, false, mod.createI32(42));
|
||||
|
||||
const aSwitch = mod.addFunctionType("ii", NativeType.I32, [ NativeType.I32 ]);
|
||||
const rl = mod.createRelooper();
|
||||
const b0 = rl.addBlockWithSwitch(mod.createNop(), mod.createGetLocal(0, NativeType.I32));
|
||||
let b1, b2, b3;
|
||||
rl.addBranchForSwitch(b0, b2 = rl.addBlock(mod.createReturn(mod.createI32(1))), [1]); // indexed branch
|
||||
rl.addBranchForSwitch(b0, b3 = rl.addBlock(mod.createReturn(mod.createI32(2))), [2]); // indexed branch
|
||||
rl.addBranch(b0, b1 = rl.addBlock(mod.createDrop(mod.createI32(0)))); // default branch
|
||||
rl.addBranch(b1, b2);
|
||||
|
||||
mod.addFunction("aSwitch", aSwitch, [ NativeType.I32 ], mod.createBlock(null, [
|
||||
rl.renderAndDispose(b0, 1),
|
||||
mod.createUnreachable()
|
||||
]));
|
||||
mod.addFunctionExport("aSwitch", "aSwitch");
|
||||
|
||||
// mod.optimize();
|
||||
if (mod.validate())
|
||||
_BinaryenModulePrint(mod.ref);
|
||||
_BinaryenModuleDispose(mod.ref);
|
@ -1,16 +1,18 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import * as chalk from "chalk";
|
||||
import * as glob from "glob";
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var chalk = require("chalk");
|
||||
var glob = require("glob");
|
||||
var diff = require("./util/diff");
|
||||
|
||||
import "../src/glue/js";
|
||||
import { Compiler } from "../src/compiler";
|
||||
import { Module } from "../src/module";
|
||||
import { Parser } from "../src/parser";
|
||||
import { diff } from "./util/diff";
|
||||
require("ts-node").register({ project: require("path").join(__dirname, "..", "src") });
|
||||
require("../src/glue/js");
|
||||
|
||||
const isCreate = process.argv[2] === "--create";
|
||||
const filter = process.argv.length > 2 && !isCreate ? "*" + process.argv[2] + "*.ts" : "*.ts";
|
||||
var Compiler = require("../src/compiler").Compiler;
|
||||
var Module = require("../src/module").Module;
|
||||
var Parser = require("../src/parser").Parser;
|
||||
|
||||
var isCreate = process.argv[2] === "--create";
|
||||
var filter = process.argv.length > 2 && !isCreate ? "*" + process.argv[2] + "*.ts" : "*.ts";
|
||||
|
||||
glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
|
||||
if (filename.charAt(0) == "_" || filename.endsWith(".fixture.ts"))
|
||||
@ -18,12 +20,12 @@ glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
|
||||
|
||||
console.log(chalk.default.whiteBright("Testing compiler/" + filename));
|
||||
|
||||
const parser = new Parser();
|
||||
const sourceText = fs.readFileSync(__dirname + "/compiler/" + filename, { encoding: "utf8" });
|
||||
var parser = new Parser();
|
||||
var sourceText = fs.readFileSync(__dirname + "/compiler/" + filename, { encoding: "utf8" });
|
||||
parser.parseFile(sourceText, filename, true);
|
||||
let nextFile;
|
||||
var nextFile;
|
||||
while ((nextFile = parser.nextFile()) !== null) {
|
||||
let nextSourceText: string;
|
||||
var nextSourceText;
|
||||
try {
|
||||
nextSourceText = fs.readFileSync(path.join(__dirname, "compiler", nextFile + ".ts"), { encoding: "utf8" });
|
||||
} catch (e) {
|
||||
@ -31,12 +33,12 @@ glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
|
||||
}
|
||||
parser.parseFile(nextSourceText, nextFile, false);
|
||||
}
|
||||
const program = parser.finish();
|
||||
const module = Compiler.compile(program);
|
||||
const actual = module.toText() + "(;\n[program.elements]\n " + iterate(program.elements.keys()).join("\n ") + "\n[program.exports]\n " + iterate(program.exports.keys()).join("\n ") + "\n;)\n";
|
||||
let actualOptimized: string | null = null;
|
||||
let actualInlined: string | null = null;
|
||||
const fixture = path.basename(filename, ".ts") + ".wast";
|
||||
var program = parser.finish();
|
||||
var module = Compiler.compile(program);
|
||||
var actual = module.toText() + "(;\n[program.elements]\n " + iterate(program.elements.keys()).join("\n ") + "\n[program.exports]\n " + iterate(program.exports.keys()).join("\n ") + "\n;)\n";
|
||||
var actualOptimized = null;
|
||||
var actualInlined = null;
|
||||
var fixture = path.basename(filename, ".ts") + ".wast";
|
||||
|
||||
if (module.validate()) {
|
||||
console.log(chalk.default.green("validate OK"));
|
||||
@ -75,8 +77,8 @@ glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const expected = fs.readFileSync(__dirname + "/compiler/" + fixture, { encoding: "utf8" });
|
||||
const diffs = diff("compiler/" + fixture, expected, actual);
|
||||
var expected = fs.readFileSync(__dirname + "/compiler/" + fixture, { encoding: "utf8" });
|
||||
var diffs = diff("compiler/" + fixture, expected, actual);
|
||||
if (diffs !== null) {
|
||||
process.exitCode = 1;
|
||||
console.log(diffs);
|
||||
@ -90,11 +92,10 @@ glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
|
||||
console.log();
|
||||
});
|
||||
|
||||
function iterate<T>(it: IterableIterator<T>): T[] {
|
||||
let current: IteratorResult<T>;
|
||||
var arr: T[] = [];
|
||||
while ((current = it.next()) && !current.done) {
|
||||
function iterate(it) {
|
||||
var current;
|
||||
var arr = [];
|
||||
while ((current = it.next()) && !current.done)
|
||||
arr.push(current.value);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
45
tests/parser.js
Normal file
45
tests/parser.js
Normal file
@ -0,0 +1,45 @@
|
||||
var fs = require("fs");
|
||||
var chalk = require("chalk");
|
||||
var glob = require("glob");
|
||||
var diff = require("./util/diff");
|
||||
|
||||
require("ts-node").register({ project: require("path").join(__dirname, "..", "src", "tsconfig.json") });
|
||||
require("../src/glue/js");
|
||||
|
||||
var Parser = require("../src/parser").Parser;
|
||||
|
||||
var isCreate = process.argv[2] === "--create";
|
||||
var filter = process.argv.length > 2 && !isCreate ? "*" + process.argv[2] + "*.ts" : "**.ts";
|
||||
|
||||
glob.sync(filter, { cwd: __dirname + "/parser" }).forEach(filename => {
|
||||
if (filename.charAt(0) == "_" || filename.endsWith(".fixture.ts"))
|
||||
return;
|
||||
|
||||
console.log(chalk.default.whiteBright("Testing parser/" + filename));
|
||||
|
||||
var parser = new Parser();
|
||||
var sourceText = fs.readFileSync(__dirname + "/parser/" + filename, { encoding: "utf8" }).replace(/\r?\n/g, "\n").replace(/^\/\/.*\r?\n/mg, "");
|
||||
parser.parseFile(sourceText, filename, true);
|
||||
|
||||
var sb = [];
|
||||
parser.program.sources[0].serialize(sb);
|
||||
var actual = sb.join("");
|
||||
var fixture = filename + ".fixture.ts";
|
||||
|
||||
if (isCreate) {
|
||||
fs.writeFileSync(__dirname + "/parser/" + fixture, actual, { encoding: "utf8" });
|
||||
console.log("Created\n");
|
||||
} else {
|
||||
var expected = fs.readFileSync(__dirname + "/parser/" + fixture, { encoding: "utf8" });
|
||||
var diffs = diff("parser/" + fixture, expected, actual);
|
||||
if (diffs !== null) {
|
||||
process.exitCode = 1;
|
||||
console.log(diffs);
|
||||
console.log(chalk.default.red("diff ERROR"));
|
||||
} else {
|
||||
console.log(chalk.default.green("diff OK"));
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
});
|
@ -1,43 +0,0 @@
|
||||
import * as fs from "fs";
|
||||
import * as chalk from "chalk";
|
||||
import * as glob from "glob";
|
||||
|
||||
import "../src/glue/js";
|
||||
import { Parser } from "../src/parser";
|
||||
import { diff } from "./util/diff";
|
||||
|
||||
const isCreate = process.argv[2] === "--create";
|
||||
const filter = process.argv.length > 2 && !isCreate ? "*" + process.argv[2] + "*.ts" : "**.ts";
|
||||
|
||||
glob.sync(filter, { cwd: __dirname + "/parser" }).forEach(filename => {
|
||||
if (filename.charAt(0) == "_" || filename.endsWith(".fixture.ts"))
|
||||
return;
|
||||
|
||||
console.log(chalk.default.whiteBright("Testing parser/" + filename));
|
||||
|
||||
const parser = new Parser();
|
||||
const sourceText = fs.readFileSync(__dirname + "/parser/" + filename, { encoding: "utf8" }).replace(/\r?\n/g, "\n").replace(/^\/\/.*\r?\n/mg, "");
|
||||
parser.parseFile(sourceText, filename, true);
|
||||
|
||||
var sb: string[] = [];
|
||||
parser.program.sources[0].serialize(sb);
|
||||
const actual = sb.join("");
|
||||
const fixture = filename + ".fixture.ts";
|
||||
|
||||
if (isCreate) {
|
||||
fs.writeFileSync(__dirname + "/parser/" + fixture, actual, { encoding: "utf8" });
|
||||
console.log("Created\n");
|
||||
} else {
|
||||
const expected = fs.readFileSync(__dirname + "/parser/" + fixture, { encoding: "utf8" });
|
||||
const diffs = diff("parser/" + fixture, expected, actual);
|
||||
if (diffs !== null) {
|
||||
process.exitCode = 1;
|
||||
console.log(diffs);
|
||||
console.log(chalk.default.red("diff ERROR"));
|
||||
} else {
|
||||
console.log(chalk.default.green("diff OK"));
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
});
|
@ -1,17 +1,17 @@
|
||||
import * as JsDiff from "diff";
|
||||
import * as chalk from "chalk";
|
||||
var JsDiff = require("diff");
|
||||
var chalk = require("chalk");
|
||||
|
||||
export function diff(filename: string, expected: string, actual: string): string | null {
|
||||
const diff = JsDiff.structuredPatch(filename, filename, expected, actual, "expected", "actual", { context: 2 });
|
||||
module.exports = function diff(filename, expected, actual) {
|
||||
var diff = JsDiff.structuredPatch(filename, filename, expected, actual, "expected", "actual", { context: 2 });
|
||||
if (!diff.hunks.length)
|
||||
return null;
|
||||
|
||||
const ret = [];
|
||||
var ret = [];
|
||||
ret.push('--- ' + diff.oldHeader);
|
||||
ret.push('+++ ' + diff.newHeader);
|
||||
|
||||
for (let i = 0; i < diff.hunks.length; i++) {
|
||||
const hunk = diff.hunks[i];
|
||||
for (var i = 0; i < diff.hunks.length; i++) {
|
||||
var hunk = diff.hunks[i];
|
||||
ret.push(
|
||||
'@@ -' + hunk.oldStart + ',' + hunk.oldLines
|
||||
+ ' +' + hunk.newStart + ',' + hunk.newLines
|
||||
@ -27,4 +27,4 @@ export function diff(filename: string, expected: string, actual: string): string
|
||||
}
|
||||
|
||||
return ret.join('\n') + '\n';
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user