Skip to content

Commit dfcdccb

Browse files
authoredMar 8, 2021
Merge pull request #11 from piotr-oles/fix/asc-options
Fix passing options to AssemblyScript
2 parents 4276d2c + e164abe commit dfcdccb

11 files changed

+140
-34
lines changed
 

‎.github/workflows/main.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ jobs:
8080
name: lib
8181
path: lib
8282

83-
- name: Run tests
84-
run: yarn test
83+
- name: Run unit tests
84+
run: yarn test unit
85+
86+
- name: Run e2e tests
87+
run: yarn test e2e
8588

8689
release:
8790
runs-on: ubuntu-latest

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
## Installation
1616

17-
This loader requires minimum AssemblyScript 0.18, Node.js 10 and webpack 4 or webpack 5
17+
This loader requires minimum [AssemblyScript 0.18](https://github.com/AssemblyScript/assemblyscript),
18+
Node.js 10 and [webpack 4 or webpack 5](https://github.com/webpack/webpack)
1819

1920
```sh
2021
# with npm

‎src/error.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import path from "path";
22
import { DiagnosticMessage } from "assemblyscript/cli/asc";
3-
import { getLineColumnFromIndex } from "./line-column";
4-
import { Location } from "./location";
3+
import { getLineColumnFromIndex, LineColumn } from "./line-column";
54
import { CompilerHost } from "./compiler-host";
65

6+
interface Location {
7+
start?: LineColumn;
8+
end?: LineColumn;
9+
}
10+
711
interface ArtificialModule {
812
identifier(): string;
913
readableIdentifier(): string;

‎src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Schema } from "schema-utils/declarations/validate";
77
import { createCompilerHost } from "./compiler-host";
88
import { mapAscOptionsToArgs } from "./options";
99
import { AssemblyScriptError } from "./error";
10-
import schema from "./options.json";
10+
import schema from "./schema.json";
1111
import { addErrorToModule, addWarningToModule } from "./webpack";
1212

1313
// eslint-disable-next-line @typescript-eslint/no-explicit-any

‎src/line-column.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
interface LineColumn {
2+
// 1-based line
23
line: number;
4+
// 1-based column
35
column: number;
46
}
57

@@ -12,8 +14,8 @@ function getLineColumnFromIndex(
1214
}
1315

1416
let line = 1;
15-
let prevLineIndex = 0;
16-
let nextLineIndex = source.indexOf("\n", prevLineIndex);
17+
let prevLineIndex = -1;
18+
let nextLineIndex = source.indexOf("\n");
1719

1820
while (nextLineIndex !== -1 && index > nextLineIndex) {
1921
prevLineIndex = nextLineIndex;

‎src/location.ts

-25
This file was deleted.

‎src/options.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ function mapAscOptionsToArgs(options: Options): string[] {
88
const value = options[key];
99

1010
if (typeof value === "boolean") {
11-
args.push("--" + key);
11+
if (value) {
12+
// add flag only if value is true
13+
args.push("--" + key);
14+
}
1215
} else if (typeof value === "string" || typeof value === "number") {
1316
args.push("--" + key, String(value));
1417
} else if (Array.isArray(value)) {

‎src/options.json ‎src/schema.json

File renamed without changes.

‎test/e2e/main.spec.ts

+52
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,56 @@ describe("as-loader", () => {
243243
}
244244
);
245245
});
246+
247+
describe("options", () => {
248+
it("passes options to assemblyscript compiler", async () => {
249+
await sandbox.load(path.resolve(__dirname, "fixtures/main"));
250+
await sandbox.install("yarn", { webpack: WEBPACK_5 });
251+
252+
await sandbox.patch(
253+
"webpack.config.js",
254+
' name: "[name].wasm",',
255+
[
256+
' name: "[name].wasm",',
257+
" optimizeLevel: 2,",
258+
" shrinkLevel: 1,",
259+
" coverage: true,",
260+
" noAssert: true,",
261+
' runtime: "stub",',
262+
" debug: true,",
263+
' trapMode: "allow",',
264+
" noValidate: true,",
265+
" importMemory: false,",
266+
" noExportMemory: true,",
267+
" initialMemory: 1000,",
268+
" maximumMemory: 2000,",
269+
" sharedMemory: true,",
270+
" importTable: false,",
271+
" exportTable: false,",
272+
" explicitStart: false,",
273+
' enable: ["simd", "threads"],',
274+
' disable: ["mutable-globals"],',
275+
" lowMemoryLimit: false,",
276+
" memoryBase: 1024,",
277+
" tableBase: 0,",
278+
].join("\n")
279+
);
280+
281+
const results = await sandbox.exec("yarn webpack");
282+
expect(results).toContain("simple.wasm");
283+
expect(results).toContain("simple.wasm.map");
284+
expect(results).toContain("main.js");
285+
286+
const simpleWasmInstance = await instantiate<
287+
typeof import("./fixtures/main/src/assembly/correct/simple")
288+
>(await sandbox.read("dist/simple.wasm"));
289+
290+
expect(simpleWasmInstance.exports.run()).toEqual(15);
291+
292+
const simpleWasmMap = await sandbox.read("dist/simple.wasm.map", "utf8");
293+
expect(Object.keys(JSON.parse(simpleWasmMap))).toEqual(
294+
expect.arrayContaining(["version", "sources", "names", "mappings"])
295+
);
296+
});
297+
});
246298
});

‎test/unit/line-column.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { getLineColumnFromIndex } from "../../src/line-column";
2+
3+
const AS_SOURCE = [
4+
"export function add(a: i32, b: i32): i32 {",
5+
" return a + b;",
6+
"}",
7+
"",
8+
].join("\n");
9+
10+
describe("line-column", () => {
11+
it.each([
12+
[0, AS_SOURCE, 1, 1],
13+
[10, AS_SOURCE, 1, 11],
14+
[42, AS_SOURCE, 1, 43],
15+
[43, AS_SOURCE, 2, 1],
16+
[45, AS_SOURCE, 2, 3],
17+
[58, AS_SOURCE, 2, 16],
18+
[59, AS_SOURCE, 3, 1],
19+
[60, AS_SOURCE, 3, 2],
20+
])(
21+
"get line and column for index %p in %p",
22+
(index, source, line, column) => {
23+
expect(getLineColumnFromIndex(source, index)).toEqual({ line, column });
24+
}
25+
);
26+
27+
it.each([
28+
[-1, AS_SOURCE],
29+
[10000, AS_SOURCE],
30+
[NaN, AS_SOURCE],
31+
])("returns undefined for invalid index %p in %p", (index, source) => {
32+
expect(getLineColumnFromIndex(source, index)).toBeUndefined();
33+
});
34+
});

‎test/unit/options.spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { mapAscOptionsToArgs } from "../../src/options";
2+
3+
describe("options", () => {
4+
it.each([
5+
[{}, []],
6+
[{ optimizeLevel: 3 }, ["--optimizeLevel", "3"]],
7+
[{ coverage: true }, ["--coverage"]],
8+
[{ coverage: false }, []],
9+
[{ runtime: "half" }, ["--runtime", "half"]],
10+
[{ enable: ["bulk-memory", "simd"] }, ["--enable", "bulk-memory,simd"]],
11+
[
12+
{
13+
optimizeLevel: 2,
14+
shrinkLevel: 1,
15+
noValidate: true,
16+
sharedMemory: false,
17+
disable: ["mutable-globals"],
18+
},
19+
[
20+
"--optimizeLevel",
21+
"2",
22+
"--shrinkLevel",
23+
"1",
24+
"--noValidate",
25+
"--disable",
26+
"mutable-globals",
27+
],
28+
],
29+
])("maps options %p to args %p", (options, args) => {
30+
expect(mapAscOptionsToArgs(options)).toEqual(args);
31+
});
32+
});

0 commit comments

Comments
 (0)
Please sign in to comment.