Skip to content

Commit

Permalink
Cleanup, refactoring, types
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Apr 27, 2018
1 parent f876564 commit 82a71be
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 140 deletions.
60 changes: 60 additions & 0 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,66 @@ declare module "chrome-trace-event" {
}
}

// There are no typings for @webassemblyjs/ast
declare module "@webassemblyjs/ast" {
export function traverse(
ast: any,
visitor: { [name: string]: (context: { node: Node }) => void }
);
export class Node {
index: number;
}
export class Identifier extends Node {
value: string;
}
export class ModuleImport extends Node {
module: string;
descr: {
type: string;
valtype: string;
};
name: string;
}
export class ModuleExport extends Node {
name: string;
}
export class IndexLiteral extends Node {}
export class NumberLiteral extends Node {}
export class Global extends Node {}
export class FuncParam extends Node {}
export class Instruction extends Node {}
export class CallInstruction extends Instruction {}
export class ObjectInstruction extends Instruction {}
export class Func extends Node {
params: any;
result: any;
}
export class TypeInstructionFunc extends Node {}
export class IndexInFuncSection extends Node {}
export function indexLiteral(index: number): IndexLiteral;
export function numberLiteral(num: number): NumberLiteral;
export function global(globalType: string, nodes: Node[]): Global;
export function identifier(indentifier: string): Identifier;
export function funcParam(valType: string, id: Identifier): FuncParam;
export function instruction(inst: string, args: Node[]): Instruction;
export function callInstruction(funcIndex: IndexLiteral): CallInstruction;
export function objectInstruction(
kind: string,
type: string,
init: Node[]
): ObjectInstruction;
export function func(initFuncId, funcParams, funcResults, funcBody): Func;
export function typeInstructionFunc(params, result): TypeInstructionFunc;
export function indexInFuncSection(index: IndexLiteral): IndexInFuncSection;
export function moduleExport(
identifier: string,
type: string,
index: IndexLiteral
): ModuleExport;

export function getSectionMetadata(ast: any, section: string);
}

/**
* Global variable declarations
* @todo Once this issue is resolved, remove these globals and add JSDoc onsite instead
Expand Down
4 changes: 2 additions & 2 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ class Compilation extends Tapable {
this.requestShortener
);
this.moduleTemplates = {
javascript: new ModuleTemplate(this.runtimeTemplate),
webassembly: new ModuleTemplate(this.runtimeTemplate)
javascript: new ModuleTemplate(this.runtimeTemplate, "javascript"),
webassembly: new ModuleTemplate(this.runtimeTemplate, "webassembly")
};

this.semaphore = new Semaphore(options.parallelism || 100);
Expand Down
52 changes: 52 additions & 0 deletions lib/Generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";

/** @typedef {import("./Module")} Module */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("webpack-sources").Source} Source */

/**
*
*/
class Generator {
static byType(map) {
return new ByTypeGenerator(map);
}

/**
* @abstract
* @param {Module} module module for which the code should be generated
* @param {Map<Function, any>} dependencyTemplates mapping from dependencies to templates
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {string} type which kind of code should be generated
* @returns {Source} generated code
*/
generate(module, dependencyTemplates, runtimeTemplate, type) {
throw new Error("Generator.generate: must be overriden");
}
}

class ByTypeGenerator extends Generator {
constructor(map) {
super();
this.map = map;
}

generate(module, dependencyTemplates, runtimeTemplate, type) {
const generator = this.map[type];
if (!generator) {
throw new Error(`Generator.byType: no generator specified for ${type}`);
}
return generator.generate(
module,
dependencyTemplates,
runtimeTemplate,
type
);
}
}

module.exports = Generator;
6 changes: 4 additions & 2 deletions lib/ModuleTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable");

module.exports = class ModuleTemplate extends Tapable {
constructor(runtimeTemplate) {
constructor(runtimeTemplate, type) {
super();
this.runtimeTemplate = runtimeTemplate;
this.type = type;
this.hooks = {
content: new SyncWaterfallHook([
"source",
Expand Down Expand Up @@ -42,7 +43,8 @@ module.exports = class ModuleTemplate extends Tapable {
render(module, dependencyTemplates, options) {
const moduleSource = module.source(
dependencyTemplates,
this.runtimeTemplate
this.runtimeTemplate,
this.type
);
const moduleSourcePostContent = this.hooks.content.call(
moduleSource,
Expand Down
29 changes: 19 additions & 10 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class NonErrorEmittedError extends WebpackError {
}
}

/**
* @typedef {Object} CachedSourceEntry
* @property {any} source the generated source
* @property {string} hash the hash value
*/

class NormalModule extends Module {
constructor({
type,
Expand Down Expand Up @@ -90,8 +96,8 @@ class NormalModule extends Module {
this.error = null;
this._source = null;
this.buildTimestamp = undefined;
this._cachedSource = undefined;
this._cachedSourceHash = undefined;
/** @private @type {Map<string, CachedSourceEntry>} */
this._cachedSources = new Map();

// Options for the NormalModule set by plugins
// TODO refactor this -> options object filled from Factory
Expand Down Expand Up @@ -343,8 +349,7 @@ class NormalModule extends Module {
};

return this.doBuild(options, compilation, resolver, fs, err => {
this._cachedSource = undefined;
this._cachedSourceHash = undefined;
this._cachedSources.clear();

// if we have an error mark module as failed and exit
if (err) {
Expand Down Expand Up @@ -403,22 +408,26 @@ class NormalModule extends Module {
return `${this.hash}-${dtHash}`;
}

source(dependencyTemplates, runtimeTemplate) {
source(dependencyTemplates, runtimeTemplate, type = "javascript") {
const hashDigest = this.getHashDigest(dependencyTemplates);
if (this._cachedSourceHash === hashDigest) {
const cacheEntry = this._cachedSources.get(type);
if (cacheEntry !== undefined && cacheEntry.hash === hashDigest) {
// We can reuse the cached source
return this._cachedSource;
return cacheEntry.source;
}

const source = this.generator.generate(
this,
dependencyTemplates,
runtimeTemplate
runtimeTemplate,
type
);

const cachedSource = new CachedSource(source);
this._cachedSource = cachedSource;
this._cachedSourceHash = hashDigest;
this._cachedSources.set(type, {
source: cachedSource,
hash: hashDigest
});
return cachedSource;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/WebpackOptionsApply.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const OptionsApply = require("./OptionsApply");

const JavascriptModulesPlugin = require("./JavascriptModulesPlugin");
const JsonModulesPlugin = require("./JsonModulesPlugin");
const WebAssemblyModulesPlugin = require("./WebAssemblyModulesPlugin");
const WebAssemblyModulesPlugin = require("./wasm/WebAssemblyModulesPlugin");

const LoaderTargetPlugin = require("./LoaderTargetPlugin");
const FunctionModulePlugin = require("./FunctionModulePlugin");
Expand Down
4 changes: 0 additions & 4 deletions lib/node/ReadFileCompileWasmTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"use strict";

const BaseWasmMainTemplatePlugin = require("../BaseWasmMainTemplatePlugin");
const WasmModuleTemplatePlugin = require("../wasm/WasmModuleTemplatePlugin");

class ReadFileCompileWasmTemplatePlugin {
apply(compiler) {
Expand Down Expand Up @@ -37,9 +36,6 @@ class ReadFileCompileWasmTemplatePlugin {
compilation.mainTemplate,
generateLoadBinaryCode
);
new WasmModuleTemplatePlugin().apply(
compilation.moduleTemplates.javascript
);
}
);
}
Expand Down
18 changes: 18 additions & 0 deletions lib/wasm/UnsupportedWebAssemblyFeatureError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
*/
"use strict";

const WebpackError = require("../WebpackError");

module.exports = class UnsupportedWebAssemblyFeatureError extends WebpackError {
/** @param {string} message Error message */
constructor(message) {
super();
this.name = "UnsupportedWebAssemblyFeatureError";
this.message = message;
this.hideStack = true;

Error.captureStackTrace(this, this.constructor);
}
};
94 changes: 0 additions & 94 deletions lib/wasm/WasmModuleTemplatePlugin.js

This file was deleted.

Loading

0 comments on commit 82a71be

Please sign in to comment.