Skip to content

Commit 00b0364

Browse files
authored
Merge pull request webpack#7144 from webpack/next
Upgrade to the WASM toolchain
2 parents 4d00edc + f2910fc commit 00b0364

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1349
-541
lines changed

declarations.d.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,69 @@ declare module "chrome-trace-event" {
2929
}
3030
}
3131

32+
// There are no typings for @webassemblyjs/ast
33+
declare module "@webassemblyjs/ast" {
34+
export function traverse(
35+
ast: any,
36+
visitor: { [name: string]: (context: { node: Node }) => void }
37+
);
38+
export class Node {
39+
index: number;
40+
}
41+
export class Identifier extends Node {
42+
value: string;
43+
}
44+
export class ModuleImport extends Node {
45+
module: string;
46+
descr: {
47+
type: string;
48+
valtype: string;
49+
};
50+
name: string;
51+
}
52+
export class ModuleExport extends Node {
53+
name: string;
54+
}
55+
export class IndexLiteral extends Node {}
56+
export class NumberLiteral extends Node {}
57+
export class Global extends Node {}
58+
export class FuncParam extends Node {}
59+
export class Instruction extends Node {}
60+
export class CallInstruction extends Instruction {}
61+
export class ObjectInstruction extends Instruction {}
62+
export class Func extends Node {
63+
signature: Signature;
64+
}
65+
export class Signature {
66+
params: any;
67+
result: any;
68+
}
69+
export class TypeInstructionFunc extends Node {}
70+
export class IndexInFuncSection extends Node {}
71+
export function indexLiteral(index: number): IndexLiteral;
72+
export function numberLiteral(num: number): NumberLiteral;
73+
export function global(globalType: string, nodes: Node[]): Global;
74+
export function identifier(indentifier: string): Identifier;
75+
export function funcParam(valType: string, id: Identifier): FuncParam;
76+
export function instruction(inst: string, args: Node[]): Instruction;
77+
export function callInstruction(funcIndex: IndexLiteral): CallInstruction;
78+
export function objectInstruction(
79+
kind: string,
80+
type: string,
81+
init: Node[]
82+
): ObjectInstruction;
83+
export function func(initFuncId, funcParams, funcResults, funcBody): Func;
84+
export function typeInstructionFunc(params, result): TypeInstructionFunc;
85+
export function indexInFuncSection(index: IndexLiteral): IndexInFuncSection;
86+
export function moduleExport(
87+
identifier: string,
88+
type: string,
89+
index: IndexLiteral
90+
): ModuleExport;
91+
92+
export function getSectionMetadata(ast: any, section: string);
93+
}
94+
3295
/**
3396
* Global variable declarations
3497
* @todo Once this issue is resolved, remove these globals and add JSDoc onsite instead

lib/Compilation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ class Compilation extends Tapable {
230230
this.requestShortener
231231
);
232232
this.moduleTemplates = {
233-
javascript: new ModuleTemplate(this.runtimeTemplate),
234-
webassembly: new ModuleTemplate(this.runtimeTemplate)
233+
javascript: new ModuleTemplate(this.runtimeTemplate, "javascript"),
234+
webassembly: new ModuleTemplate(this.runtimeTemplate, "webassembly")
235235
};
236236

237237
this.semaphore = new Semaphore(options.parallelism || 100);

lib/Generator.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
/** @typedef {import("./Module")} Module */
8+
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
9+
/** @typedef {import("webpack-sources").Source} Source */
10+
11+
/**
12+
*
13+
*/
14+
class Generator {
15+
static byType(map) {
16+
return new ByTypeGenerator(map);
17+
}
18+
19+
/**
20+
* @abstract
21+
* @param {Module} module module for which the code should be generated
22+
* @param {Map<Function, any>} dependencyTemplates mapping from dependencies to templates
23+
* @param {RuntimeTemplate} runtimeTemplate the runtime template
24+
* @param {string} type which kind of code should be generated
25+
* @returns {Source} generated code
26+
*/
27+
generate(module, dependencyTemplates, runtimeTemplate, type) {
28+
throw new Error("Generator.generate: must be overriden");
29+
}
30+
}
31+
32+
class ByTypeGenerator extends Generator {
33+
constructor(map) {
34+
super();
35+
this.map = map;
36+
}
37+
38+
generate(module, dependencyTemplates, runtimeTemplate, type) {
39+
const generator = this.map[type];
40+
if (!generator) {
41+
throw new Error(`Generator.byType: no generator specified for ${type}`);
42+
}
43+
return generator.generate(
44+
module,
45+
dependencyTemplates,
46+
runtimeTemplate,
47+
type
48+
);
49+
}
50+
}
51+
52+
module.exports = Generator;

lib/ModuleTemplate.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable");
88

99
module.exports = class ModuleTemplate extends Tapable {
10-
constructor(runtimeTemplate) {
10+
constructor(runtimeTemplate, type) {
1111
super();
1212
this.runtimeTemplate = runtimeTemplate;
13+
this.type = type;
1314
this.hooks = {
1415
content: new SyncWaterfallHook([
1516
"source",
@@ -40,34 +41,40 @@ module.exports = class ModuleTemplate extends Tapable {
4041
}
4142

4243
render(module, dependencyTemplates, options) {
43-
const moduleSource = module.source(
44-
dependencyTemplates,
45-
this.runtimeTemplate
46-
);
47-
const moduleSourcePostContent = this.hooks.content.call(
48-
moduleSource,
49-
module,
50-
options,
51-
dependencyTemplates
52-
);
53-
const moduleSourcePostModule = this.hooks.module.call(
54-
moduleSourcePostContent,
55-
module,
56-
options,
57-
dependencyTemplates
58-
);
59-
const moduleSourcePostRender = this.hooks.render.call(
60-
moduleSourcePostModule,
61-
module,
62-
options,
63-
dependencyTemplates
64-
);
65-
return this.hooks.package.call(
66-
moduleSourcePostRender,
67-
module,
68-
options,
69-
dependencyTemplates
70-
);
44+
try {
45+
const moduleSource = module.source(
46+
dependencyTemplates,
47+
this.runtimeTemplate,
48+
this.type
49+
);
50+
const moduleSourcePostContent = this.hooks.content.call(
51+
moduleSource,
52+
module,
53+
options,
54+
dependencyTemplates
55+
);
56+
const moduleSourcePostModule = this.hooks.module.call(
57+
moduleSourcePostContent,
58+
module,
59+
options,
60+
dependencyTemplates
61+
);
62+
const moduleSourcePostRender = this.hooks.render.call(
63+
moduleSourcePostModule,
64+
module,
65+
options,
66+
dependencyTemplates
67+
);
68+
return this.hooks.package.call(
69+
moduleSourcePostRender,
70+
module,
71+
options,
72+
dependencyTemplates
73+
);
74+
} catch (e) {
75+
e.message = `${module.identifier()}\n${e.message}`;
76+
throw e;
77+
}
7178
}
7279

7380
updateHash(hash) {

lib/NormalModule.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ class NonErrorEmittedError extends WebpackError {
6262
}
6363
}
6464

65+
/**
66+
* @typedef {Object} CachedSourceEntry
67+
* @property {any} source the generated source
68+
* @property {string} hash the hash value
69+
*/
70+
6571
class NormalModule extends Module {
6672
constructor({
6773
type,
@@ -92,8 +98,8 @@ class NormalModule extends Module {
9298
this._source = null;
9399
this._buildHash = "";
94100
this.buildTimestamp = undefined;
95-
this._cachedSource = undefined;
96-
this._cachedSourceHash = undefined;
101+
/** @private @type {Map<string, CachedSourceEntry>} */
102+
this._cachedSources = new Map();
97103

98104
// Options for the NormalModule set by plugins
99105
// TODO refactor this -> options object filled from Factory
@@ -357,8 +363,7 @@ class NormalModule extends Module {
357363
};
358364

359365
return this.doBuild(options, compilation, resolver, fs, err => {
360-
this._cachedSource = undefined;
361-
this._cachedSourceHash = undefined;
366+
this._cachedSources.clear();
362367

363368
// if we have an error mark module as failed and exit
364369
if (err) {
@@ -421,22 +426,26 @@ class NormalModule extends Module {
421426
return `${this.hash}-${dtHash}`;
422427
}
423428

424-
source(dependencyTemplates, runtimeTemplate) {
429+
source(dependencyTemplates, runtimeTemplate, type = "javascript") {
425430
const hashDigest = this.getHashDigest(dependencyTemplates);
426-
if (this._cachedSourceHash === hashDigest) {
431+
const cacheEntry = this._cachedSources.get(type);
432+
if (cacheEntry !== undefined && cacheEntry.hash === hashDigest) {
427433
// We can reuse the cached source
428-
return this._cachedSource;
434+
return cacheEntry.source;
429435
}
430436

431437
const source = this.generator.generate(
432438
this,
433439
dependencyTemplates,
434-
runtimeTemplate
440+
runtimeTemplate,
441+
type
435442
);
436443

437444
const cachedSource = new CachedSource(source);
438-
this._cachedSource = cachedSource;
439-
this._cachedSourceHash = hashDigest;
445+
this._cachedSources.set(type, {
446+
source: cachedSource,
447+
hash: hashDigest
448+
});
440449
return cachedSource;
441450
}
442451

lib/WebAssemblyGenerator.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/WebAssemblyParser.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

lib/WebpackOptionsApply.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const OptionsApply = require("./OptionsApply");
88

99
const JavascriptModulesPlugin = require("./JavascriptModulesPlugin");
1010
const JsonModulesPlugin = require("./JsonModulesPlugin");
11-
const WebAssemblyModulesPlugin = require("./WebAssemblyModulesPlugin");
11+
const WebAssemblyModulesPlugin = require("./wasm/WebAssemblyModulesPlugin");
1212

1313
const LoaderTargetPlugin = require("./LoaderTargetPlugin");
1414
const FunctionModulePlugin = require("./FunctionModulePlugin");

lib/dependencies/WebAssemblyImportDependency.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,36 @@
66

77
const DependencyReference = require("./DependencyReference");
88
const ModuleDependency = require("./ModuleDependency");
9+
const UnsupportedWebAssemblyFeatureError = require("../wasm/UnsupportedWebAssemblyFeatureError");
910

1011
class WebAssemblyImportDependency extends ModuleDependency {
11-
constructor(request, name) {
12+
constructor(request, name, description, onlyDirectImport) {
1213
super(request);
1314
this.name = name;
15+
this.description = description;
16+
this.onlyDirectImport = onlyDirectImport;
1417
}
1518

1619
getReference() {
1720
if (!this.module) return null;
1821
return new DependencyReference(this.module, [this.name], false);
1922
}
2023

24+
getErrors() {
25+
if (
26+
this.onlyDirectImport &&
27+
this.module &&
28+
!this.module.type.startsWith("webassembly")
29+
) {
30+
const type = this.description.type;
31+
return [
32+
new UnsupportedWebAssemblyFeatureError(
33+
`${type} imports are only available for direct wasm to wasm dependencies`
34+
)
35+
];
36+
}
37+
}
38+
2139
get type() {
2240
return "wasm import";
2341
}

0 commit comments

Comments
 (0)