Skip to content

Add tests #1408

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

Closed
wants to merge 10 commits into from
Closed
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
4 changes: 2 additions & 2 deletions cli/asc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface MemoryStream extends OutputStream {
}

/** Compiler options. */
interface CompilerOptions {
export interface CompilerOptions {
/** Prints just the compiler's version and exits. */
version?: boolean;
/** Prints the help message and exits. */
Expand Down Expand Up @@ -146,7 +146,7 @@ interface CompilerOptions {
}

/** Compiler API options. */
interface APIOptions {
export interface APIOptions {
/** Standard output stream to use. */
stdout?: OutputStream;
/** Standard error stream to use. */
Expand Down
312 changes: 146 additions & 166 deletions cli/asc.js

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions cli/asc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
"category": "General",
"description": "Configuration file to apply. CLI arguments take precedence.",
"type": "s",
"default": "asconfig.json"
"cliOnly": true
},
"target": {
"category": "General",
"description": "Target configuration to use. Defaults to 'release'.",
"type": "s",
"default": "release"
"cliOnly": true
},

"optimize": {
Expand Down Expand Up @@ -247,7 +247,9 @@
"transform": {
"category": "API",
"description": "Specifies the path to a custom transform to 'require'.",
"type": "S"
"type": "S",
"isPath": true,
"useNodeResolution": true
},

"trapMode": {
Expand Down Expand Up @@ -285,7 +287,8 @@
},
"extension": {
"description": "Specifies an alternative file extension to use.",
"type": "s"
"type": "s",
"cliOnly": true
},
"noUnsafe": {
"description": [
Expand Down Expand Up @@ -320,15 +323,17 @@
"Adds one or multiple paths to custom library components and",
"uses exports of all top-level files at this path as globals."
],
"type": "s"
"type": "S",
"isPath": true
},
"path": {
"description": [
"Adds one or multiple paths to package resolution, similar",
"to node_modules. Prefers an 'ascMain' entry in a package's",
"package.json and falls back to an inner 'assembly/' folder."
],
"type": "S"
"type": "S",
"isPath": true
},
"traceResolution": {
"description": "Enables tracing of package resolution.",
Expand Down
7 changes: 5 additions & 2 deletions cli/util/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ interface HelpOptions {
export function help(config: Config, options?: HelpOptions): string;

/** Merges two sets of options into one, preferring the current over the parent set. */
export function merge(config: Config, currentOptions: OptionSet, parentOptions: OptionSet): OptionSet;
export function merge(config: Config, currentOptions: OptionSet, parentOptions: OptionSet, parentBaseDir: string): OptionSet;

/** Resolves a single relative path. Keeps absolute paths, otherwise prepends baseDir. */
export function resolvePath(path: string, baseDir: string, useNodeResolution?: boolean): string;

/** Populates default values on a parsed options result. */
export function addDefaults(config: Config, options: OptionSet): OptionSet;
export function addDefaults(config: Config, options: OptionSet): void;
33 changes: 29 additions & 4 deletions cli/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @license Apache-2.0
*/

const path = require("path");
const colorsUtil = require("./colors");

// type | meaning
Expand Down Expand Up @@ -31,7 +32,7 @@ function parse(argv, config, propagateDefaults = true) {
if (typeof option.alias === "string") aliases[option.alias] = key;
else if (Array.isArray(option.alias)) option.alias.forEach(alias => aliases[alias] = key);
}
if (option.default != null) options[key] = option.default;
if (propagateDefaults && option.default != null) options[key] = option.default;
});

// iterate over argv
Expand Down Expand Up @@ -171,22 +172,29 @@ function sanitizeValue(value, type) {
}

/** Merges two sets of options into one, preferring the current over the parent set. */
function merge(config, currentOptions, parentOptions) {
function merge(config, currentOptions, parentOptions, parentBaseDir) {
const mergedOptions = {};
for (const [key, { type, mutuallyExclusive }] of Object.entries(config)) {
for (const [key, { type, mutuallyExclusive, isPath, useNodeResolution, cliOnly }] of Object.entries(config)) {
let currentValue = sanitizeValue(currentOptions[key], type);
let parentValue = sanitizeValue(parentOptions[key], type);
if (currentValue == null) {
if (parentValue != null) {
// only parent value present
if (cliOnly) continue;
if (Array.isArray(parentValue)) {
let exclude;
if (isPath) {
parentValue = parentValue.map(value => resolvePath(value, parentBaseDir, useNodeResolution));
}
if (mutuallyExclusive != null && (exclude = currentOptions[mutuallyExclusive])) {
mergedOptions[key] = parentValue.filter(value => !exclude.includes(value));
} else {
mergedOptions[key] = parentValue.slice();
}
} else {
if (isPath) {
parentValue = resolvePath(parentValue, parentBaseDir, useNodeResolution);
}
mergedOptions[key] = parentValue;
}
}
Expand All @@ -200,7 +208,14 @@ function merge(config, currentOptions, parentOptions) {
} else {
// both current and parent values present
if (Array.isArray(currentValue)) {
if (cliOnly) {
mergedOptions[key] = currentValue.slice();
continue;
}
let exclude;
if (isPath) {
parentValue = parentValue.map(value => resolvePath(value, parentBaseDir, useNodeResolution));
}
if (mutuallyExclusive != null && (exclude = currentOptions[mutuallyExclusive])) {
mergedOptions[key] = [
...currentValue,
Expand All @@ -222,14 +237,24 @@ function merge(config, currentOptions, parentOptions) {

exports.merge = merge;

/** Resolves a single possibly relative path. Keeps absolute paths, otherwise prepends baseDir. */
function resolvePath(p, baseDir, useNodeResolution = false) {
if (path.isAbsolute(p)) return p;
if (useNodeResolution && !p.startsWith(".")) {
return require.resolve(p, { paths: [ baseDir ] });
}
return path.join(baseDir, p);
}

exports.resolvePath = resolvePath;

/** Populates default values on a parsed options result. */
function addDefaults(config, options) {
for (const [key, { default: defaultValue }] of Object.entries(config)) {
if (options[key] == null && defaultValue != null) {
options[key] = defaultValue;
}
}
return options;
}

exports.addDefaults = addDefaults;
47 changes: 24 additions & 23 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ import {
writeF32,
writeF64,
uniqueMap,
isPowerOf2
isPowerOf2,
v128_zero
} from "./util";

/** Compiler options. */
Expand Down Expand Up @@ -3058,10 +3059,11 @@ export class Compiler extends DiagnosticEmitter {
if (initExpr) {
let precomp = module.runExpression(initExpr, ExpressionRunnerFlags.PreserveSideeffects);
if (precomp) {
initExpr = precomp;
let local = new Local(name, -1, type, flow.parentFunction);
initExpr = precomp; // always use precomputed initExpr
let local: Local | null = null;
switch (<u32>getExpressionType(initExpr)) {
case <u32>NativeType.I32: {
local = new Local(name, -1, type, flow.parentFunction);
local.setConstantIntegerValue(
i64_new(
getConstValueI32(initExpr),
Expand All @@ -3072,6 +3074,7 @@ export class Compiler extends DiagnosticEmitter {
break;
}
case <u32>NativeType.I64: {
local = new Local(name, -1, type, flow.parentFunction);
local.setConstantIntegerValue(
i64_new(
getConstValueI64Low(initExpr),
Expand All @@ -3082,33 +3085,33 @@ export class Compiler extends DiagnosticEmitter {
break;
}
case <u32>NativeType.F32: {
local = new Local(name, -1, type, flow.parentFunction);
local.setConstantFloatValue(<f64>getConstValueF32(initExpr), type);
break;
}
case <u32>NativeType.F64: {
local = new Local(name, -1, type, flow.parentFunction);
local.setConstantFloatValue(getConstValueF64(initExpr), type);
break;
}
default: {
assert(false);
return module.unreachable();
}
}
// Create a virtual local that doesn't actually exist in WebAssembly
let scopedLocals = flow.scopedLocals;
if (!scopedLocals) flow.scopedLocals = scopedLocals = new Map();
else if (scopedLocals.has(name)) {
let existing = assert(scopedLocals.get(name));
this.errorRelated(
DiagnosticCode.Duplicate_identifier_0,
declaration.name.range,
existing.declaration.name.range,
name
);
return this.module.unreachable();
if (local) {
// Add as a virtual local that doesn't actually exist in WebAssembly
let scopedLocals = flow.scopedLocals;
if (!scopedLocals) flow.scopedLocals = scopedLocals = new Map();
else if (scopedLocals.has(name)) {
let existing = assert(scopedLocals.get(name));
this.errorRelated(
DiagnosticCode.Duplicate_identifier_0,
declaration.name.range,
existing.declaration.name.range,
name
);
return this.module.unreachable();
}
scopedLocals.set(name, local);
isStatic = true;
}
scopedLocals.set(name, local);
isStatic = true;
}
} else {
this.error(
Expand Down Expand Up @@ -11035,8 +11038,6 @@ export class Compiler extends DiagnosticEmitter {

// helpers

const v128_zero = new Uint8Array(16);

function mangleImportName(
element: Element,
declaration: DeclarationStatement
Expand Down
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./collections";
export * from "./math";
export * from "./path";
export * from "./text";
export * from "./vector";
7 changes: 7 additions & 0 deletions src/util/vector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @fileoverview Various vector utility.
* @license Apache-2.0
*/

/** v128 zero constant. */
export const v128_zero = new Uint8Array(16);
1 change: 1 addition & 0 deletions tests/asconfig/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!entry-points/**/*/node_modules/
8 changes: 4 additions & 4 deletions tests/asconfig/complicated/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
assert(ASC_OPTIMIZE_LEVEL == 3);
assert(ASC_SHRINK_LEVEL == 1);
assert(ASC_FEATURE_SIMD);
assert(ASC_OPTIMIZE_LEVEL == 3, "expected optimize level == 3");
assert(ASC_SHRINK_LEVEL == 1, "expected shrink level == 1");
assert(ASC_FEATURE_SIMD, "expected SIMD enabled");
let size = memory.size();
trace("size", 1, size);
assert(size == 30);
assert(size == 30, "expected 30 got " + size.toString());
3 changes: 3 additions & 0 deletions tests/asconfig/entry-points/node-resolution/asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "entry-points/asconfig.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert(answerToLife == 42);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/asconfig/entry-points/node-resolution/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"scripts": {
"test": "node ../../index.js"
}
}
4 changes: 3 additions & 1 deletion tests/asconfig/entry-points/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"private": true,
"scripts": {
"test": "node ../index.js && cd nested && npm run test"
"test": "node ../index.js && npm run test:nested && npm run test:node",
"test:nested": "cd nested && npm run test",
"test:node": "cd node-resolution && npm run test"
}
}
4 changes: 3 additions & 1 deletion tests/asconfig/extends/asconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
}
},
"options": {
"enable": ["simd"]
"enable": ["simd"],
"runtime": "half",
"noEmit": false
},
"extends": "./extends.json"
}
8 changes: 8 additions & 0 deletions tests/asconfig/extends/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"options": {
"runtime": "half",
"noEmit": false,
"noAssert": true,
"enable": ["simd"]
}
}
4 changes: 3 additions & 1 deletion tests/asconfig/extends/extends.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
}
},
"options": {
"disable": ["simd"]
"disable": ["simd"],
"noEmit": true,
"noAssert": true
}
}
2 changes: 1 addition & 1 deletion tests/asconfig/extends/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"scripts": {
"test": "node ../index.js"
"test": "node ../index.js --showConfig && node ../index.js"
}
}
Loading