├── README.md
├── add.wasm
├── build.js
├── example.js
├── factorial.wasm
├── fibonacci.wasm
├── index.html
├── js
├── 0.output.js
├── 1.output.js
├── 1d2268b99656e9575a63.wasm
├── 3d28950d91bc7246f5af.wasm
├── 80925f35a6f1cf550d38.wasm
└── output.js
├── math.js
├── template.md
└── webpack.config.js
/README.md:
--------------------------------------------------------------------------------
1 | This very simple example shows usage of WebAssembly.
2 |
3 | WebAssembly modules can be imported like other modules. Their download and compilation happens in parallel to the download and evaluation of the javascript chunk.
4 |
5 | # example.js
6 |
7 | ``` javascript
8 | import("./add.wasm").then(addModule => {
9 | console.log(addModule.add(22, 2200));
10 | import("./math").then(math => {
11 | console.log(math.add(10, 101));
12 | console.log(math.factorial(15));
13 | console.log(math.factorialJavascript(15));
14 | console.log(math.fibonacci(15));
15 | console.log(math.fibonacciJavascript(15));
16 | timed("wasm factorial", () => math.factorial(1500));
17 | timed("js factorial", () => math.factorialJavascript(1500));
18 | timed("wasm fibonacci", () => math.fibonacci(22));
19 | timed("js fibonacci", () => math.fibonacciJavascript(22));
20 | });
21 | });
22 |
23 | function timed(name, fn) {
24 | if(!console.time || !console.timeEnd)
25 | return fn();
26 | // warmup
27 | for(var i = 0; i < 10; i++)
28 | fn();
29 | console.time(name)
30 | for(var i = 0; i < 5000; i++)
31 | fn();
32 | console.timeEnd(name)
33 | }
34 | ```
35 |
36 | # math.js
37 |
38 | ``` javascript
39 | import { add } from "./add.wasm";
40 | import { factorial } from "./factorial.wasm";
41 | import { fibonacci } from "./fibonacci.wasm";
42 |
43 | export { add, factorial, fibonacci };
44 |
45 | export function factorialJavascript(i) {
46 | if(i < 1) return 1;
47 | return i * factorialJavascript(i - 1);
48 | }
49 |
50 | export function fibonacciJavascript(i) {
51 | if(i < 2) return 1;
52 | return fibonacciJavascript(i - 1) + fibonacciJavascript(i - 2);
53 | }
54 | ```
55 |
56 | # js/output.js
57 |
58 | /******/ (function(modules) { /* webpackBootstrap */ })
59 |
60 | ``` javascript
61 | /******/ (function(modules) { // webpackBootstrap
62 | /******/ // install a JSONP callback for chunk loading
63 | /******/ function webpackJsonpCallback(data) {
64 | /******/ var chunkIds = data[0], moreModules = data[1], executeModules = data[2];
65 | /******/ // add "moreModules" to the modules object,
66 | /******/ // then flag all "chunkIds" as loaded and fire callback
67 | /******/ var moduleId, chunkId, i = 0, resolves = [], result;
68 | /******/ for(;i < chunkIds.length; i++) {
69 | /******/ chunkId = chunkIds[i];
70 | /******/ if(installedChunks[chunkId]) {
71 | /******/ resolves.push(installedChunks[chunkId][0]);
72 | /******/ }
73 | /******/ installedChunks[chunkId] = 0;
74 | /******/ }
75 | /******/ for(moduleId in moreModules) {
76 | /******/ if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {
77 | /******/ modules[moduleId] = moreModules[moduleId];
78 | /******/ }
79 | /******/ }
80 | /******/ if(parentJsonpFunction) parentJsonpFunction(data);
81 | /******/ while(resolves.length) {
82 | /******/ resolves.shift()();
83 | /******/ }
84 | /******/
85 | /******/ };
86 | /******/
87 | /******/ // The module cache
88 | /******/ var installedModules = {};
89 | /******/
90 | /******/ // object to store loaded and loading chunks
91 | /******/ var installedChunks = {
92 | /******/ 2: 0
93 | /******/ };
94 | /******/
95 | /******/ var scheduledModules = [];
96 | /******/
97 | /******/ // object to store loaded and loading wasm modules
98 | /******/ var installedWasmModules = {};
99 | /******/
100 | /******/ // The require function
101 | /******/ function __webpack_require__(moduleId) {
102 | /******/
103 | /******/ // Check if module is in cache
104 | /******/ if(installedModules[moduleId]) {
105 | /******/ return installedModules[moduleId].exports;
106 | /******/ }
107 | /******/ // Create a new module (and put it into the cache)
108 | /******/ var module = installedModules[moduleId] = {
109 | /******/ i: moduleId,
110 | /******/ l: false,
111 | /******/ exports: {}
112 | /******/ };
113 | /******/
114 | /******/ // Execute the module function
115 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
116 | /******/
117 | /******/ // Flag the module as loaded
118 | /******/ module.l = true;
119 | /******/
120 | /******/ // Return the exports of the module
121 | /******/ return module.exports;
122 | /******/ }
123 | /******/
124 | /******/ // This file contains only the entry chunk.
125 | /******/ // The chunk loading function for additional chunks
126 | /******/ __webpack_require__.e = function requireEnsure(chunkId) {
127 | /******/ var promises = [];
128 | /******/
129 | /******/
130 | /******/ // JSONP chunk loading for javascript
131 | /******/
132 | /******/ var installedChunkData = installedChunks[chunkId];
133 | /******/ if(installedChunkData !== 0) { // 0 means "already installed".
134 | /******/
135 | /******/ // a Promise means "currently loading".
136 | /******/ if(installedChunkData) {
137 | /******/ promises.push(installedChunkData[2]);
138 | /******/ } else {
139 | /******/ // setup Promise in chunk cache
140 | /******/ var promise = new Promise(function(resolve, reject) {
141 | /******/ installedChunkData = installedChunks[chunkId] = [resolve, reject];
142 | /******/ installedChunkData[2] = promise;
143 | /******/ });
144 | /******/
145 | /******/ // start chunk loading
146 | /******/ var head = document.getElementsByTagName('head')[0];
147 | /******/ var script = document.createElement('script');
148 | /******/ script.charset = 'utf-8';
149 | /******/ script.timeout = 120000;
150 | /******/
151 | /******/ if (__webpack_require__.nc) {
152 | /******/ script.setAttribute("nonce", __webpack_require__.nc);
153 | /******/ }
154 | /******/ script.src = __webpack_require__.p + "" + chunkId + ".output.js";
155 | /******/ var timeout = setTimeout(function(){
156 | /******/ onScriptComplete({ type: 'timeout', target: script });
157 | /******/ }, 120000);
158 | /******/ script.onerror = script.onload = onScriptComplete;
159 | /******/ function onScriptComplete(event) {
160 | /******/ // avoid mem leaks in IE.
161 | /******/ script.onerror = script.onload = null;
162 | /******/ clearTimeout(timeout);
163 | /******/ var chunk = installedChunks[chunkId];
164 | /******/ if(chunk !== 0) {
165 | /******/ if(chunk) {
166 | /******/ var errorType = event && (event.type === 'load' ? 'missing' : event.type);
167 | /******/ var realSrc = event && event.target && event.target.src;
168 | /******/ var error = new Error('Loading chunk ' + chunkId + ' failed.\n(' + errorType + ': ' + realSrc + ')');
169 | /******/ error.type = errorType;
170 | /******/ error.request = realSrc;
171 | /******/ chunk[1](error);
172 | /******/ }
173 | /******/ installedChunks[chunkId] = undefined;
174 | /******/ }
175 | /******/ };
176 | /******/ head.appendChild(script);
177 | /******/ promises.push(promise);
178 | /******/ }
179 | /******/ }
180 | /******/
181 | /******/ // Fetch + compile chunk loading for webassembly
182 | /******/
183 | /******/ var wasmModules = {"0":[1,3,4],"1":[1]}[chunkId] || [];
184 | /******/
185 | /******/ wasmModules.forEach(function(wasmModuleId) {
186 | /******/ var installedWasmModuleData = installedWasmModules[wasmModuleId];
187 | /******/
188 | /******/ // a Promise means "currently loading" or "already loaded".
189 | /******/ if(installedWasmModuleData) {
190 | /******/ promises.push(installedWasmModuleData);
191 | /******/ } else {
192 | /******/ var promise = installedWasmModules[wasmModuleId] = fetch(__webpack_require__.p + "" + {"1":"80925f35a6f1cf550d38","3":"3d28950d91bc7246f5af","4":"1d2268b99656e9575a63"}[wasmModuleId] + ".wasm")
193 | /******/ .then(function(response) { return response.arrayBuffer(); })
194 | /******/ .then(function(bytes) { return WebAssembly.compile(bytes); })
195 | /******/ .then(function(module) { __webpack_require__.w[wasmModuleId] = module; })
196 | /******/ promises.push(promise);
197 | /******/ }
198 | /******/ });
199 | /******/ return Promise.all(promises);
200 | /******/ };
201 | /******/
202 | /******/ // expose the modules object (__webpack_modules__)
203 | /******/ __webpack_require__.m = modules;
204 | /******/
205 | /******/ // expose the module cache
206 | /******/ __webpack_require__.c = installedModules;
207 | /******/
208 | /******/ // define getter function for harmony exports
209 | /******/ __webpack_require__.d = function(exports, name, getter) {
210 | /******/ if(!__webpack_require__.o(exports, name)) {
211 | /******/ Object.defineProperty(exports, name, {
212 | /******/ configurable: false,
213 | /******/ enumerable: true,
214 | /******/ get: getter
215 | /******/ });
216 | /******/ }
217 | /******/ };
218 | /******/
219 | /******/ // getDefaultExport function for compatibility with non-harmony modules
220 | /******/ __webpack_require__.n = function(module) {
221 | /******/ var getter = module && module.__esModule ?
222 | /******/ function getDefault() { return module['default']; } :
223 | /******/ function getModuleExports() { return module; };
224 | /******/ __webpack_require__.d(getter, 'a', getter);
225 | /******/ return getter;
226 | /******/ };
227 | /******/
228 | /******/ // Object.prototype.hasOwnProperty.call
229 | /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
230 | /******/
231 | /******/ // __webpack_public_path__
232 | /******/ __webpack_require__.p = "js/";
233 | /******/
234 | /******/ // on error function for async loading
235 | /******/ __webpack_require__.oe = function(err) { console.error(err); throw err; };
236 | /******/
237 | /******/ // object with all compiled WebAssmbly.Modules
238 | /******/ __webpack_require__.w = {};
239 | /******/
240 | /******/ var jsonpArray = window["webpackJsonp"] = window["webpackJsonp"] || [];
241 | /******/ var parentJsonpFunction = jsonpArray.push.bind(jsonpArray);
242 | /******/ jsonpArray.push = webpackJsonpCallback;
243 | /******/ jsonpArray = jsonpArray.slice();
244 | /******/ for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);
245 | /******/
246 | /******/ // Load entry module and return exports
247 | /******/ return __webpack_require__(__webpack_require__.s = 0);
248 | /******/ })
249 | /************************************************************************/
250 | ```
251 |
252 |
253 |
254 | ``` javascript
255 | /******/ ([
256 | /* 0 */
257 | /*!********************!*\
258 | !*** ./example.js ***!
259 | \********************/
260 | /*! no static exports found */
261 | /*! all exports used */
262 | /***/ (function(module, exports, __webpack_require__) {
263 |
264 | __webpack_require__.e/* import() */(1).then(__webpack_require__.bind(null, /*! ./add.wasm */1)).then(addModule => {
265 | console.log(addModule.add(22, 2200));
266 | __webpack_require__.e/* import() */(0).then(__webpack_require__.bind(null, /*! ./math */2)).then(math => {
267 | console.log(math.add(10, 101));
268 | console.log(math.factorial(15));
269 | console.log(math.factorialJavascript(15));
270 | console.log(math.fibonacci(15));
271 | console.log(math.fibonacciJavascript(15));
272 | timed("wasm factorial", () => math.factorial(1500));
273 | timed("js factorial", () => math.factorialJavascript(1500));
274 | timed("wasm fibonacci", () => math.fibonacci(22));
275 | timed("js fibonacci", () => math.fibonacciJavascript(22));
276 | });
277 | });
278 |
279 | function timed(name, fn) {
280 | if(!console.time || !console.timeEnd)
281 | return fn();
282 | // warmup
283 | for(var i = 0; i < 10; i++)
284 | fn();
285 | console.time(name)
286 | for(var i = 0; i < 5000; i++)
287 | fn();
288 | console.timeEnd(name)
289 | }
290 |
291 |
292 | /***/ })
293 | /******/ ]);
294 | ```
295 |
296 | # js/0.output.js
297 |
298 | ``` javascript
299 | (window["webpackJsonp"] = window["webpackJsonp"] || []).push([[0,1],[
300 | /* 0 */,
301 | /* 1 */
302 | /*!******************!*\
303 | !*** ./add.wasm ***!
304 | \******************/
305 | /*! no static exports found */
306 | /*! all exports used */
307 | /***/ (function(module, exports, __webpack_require__) {
308 |
309 | "use strict";
310 |
311 | // Instanciate WebAssembly module
312 | var instance = new WebAssembly.Instance(__webpack_require__.w[module.i], {});
313 |
314 | // export exports from WebAssmbly module
315 | module.exports = instance.exports;
316 |
317 | /***/ }),
318 | /* 2 */
319 | /*!*****************!*\
320 | !*** ./math.js ***!
321 | \*****************/
322 | /*! exports provided: add, factorial, fibonacci, factorialJavascript, fibonacciJavascript */
323 | /*! all exports used */
324 | /***/ (function(module, __webpack_exports__, __webpack_require__) {
325 |
326 | "use strict";
327 | Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
328 | /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "factorialJavascript", function() { return factorialJavascript; });
329 | /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "fibonacciJavascript", function() { return fibonacciJavascript; });
330 | /* harmony import */ var _add_wasm__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./add.wasm */1);
331 | /* harmony import */ var _add_wasm__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_add_wasm__WEBPACK_IMPORTED_MODULE_0__);
332 | /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "add", function() { return _add_wasm__WEBPACK_IMPORTED_MODULE_0__["add"]; });
333 |
334 | /* harmony import */ var _factorial_wasm__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./factorial.wasm */3);
335 | /* harmony import */ var _factorial_wasm__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_factorial_wasm__WEBPACK_IMPORTED_MODULE_1__);
336 | /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "factorial", function() { return _factorial_wasm__WEBPACK_IMPORTED_MODULE_1__["factorial"]; });
337 |
338 | /* harmony import */ var _fibonacci_wasm__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./fibonacci.wasm */4);
339 | /* harmony import */ var _fibonacci_wasm__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_fibonacci_wasm__WEBPACK_IMPORTED_MODULE_2__);
340 | /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "fibonacci", function() { return _fibonacci_wasm__WEBPACK_IMPORTED_MODULE_2__["fibonacci"]; });
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 | function factorialJavascript(i) {
349 | if(i < 1) return 1;
350 | return i * factorialJavascript(i - 1);
351 | }
352 |
353 | function fibonacciJavascript(i) {
354 | if(i < 2) return 1;
355 | return fibonacciJavascript(i - 1) + fibonacciJavascript(i - 2);
356 | }
357 |
358 |
359 | /***/ }),
360 | /* 3 */
361 | /*!************************!*\
362 | !*** ./factorial.wasm ***!
363 | \************************/
364 | /*! no static exports found */
365 | /*! exports used: factorial */
366 | /***/ (function(module, exports, __webpack_require__) {
367 |
368 | "use strict";
369 |
370 | // Instanciate WebAssembly module
371 | var instance = new WebAssembly.Instance(__webpack_require__.w[module.i], {});
372 |
373 | // export exports from WebAssmbly module
374 | module.exports = instance.exports;
375 |
376 | /***/ }),
377 | /* 4 */
378 | /*!************************!*\
379 | !*** ./fibonacci.wasm ***!
380 | \************************/
381 | /*! no static exports found */
382 | /*! exports used: fibonacci */
383 | /***/ (function(module, exports, __webpack_require__) {
384 |
385 | "use strict";
386 |
387 | // Instanciate WebAssembly module
388 | var instance = new WebAssembly.Instance(__webpack_require__.w[module.i], {});
389 |
390 | // export exports from WebAssmbly module
391 | module.exports = instance.exports;
392 |
393 | /***/ })
394 | ]]);
395 | ```
396 |
397 | # js/1.output.js
398 |
399 | ``` javascript
400 | (window["webpackJsonp"] = window["webpackJsonp"] || []).push([[1],[
401 | /* 0 */,
402 | /* 1 */
403 | /*!******************!*\
404 | !*** ./add.wasm ***!
405 | \******************/
406 | /*! no static exports found */
407 | /*! all exports used */
408 | /***/ (function(module, exports, __webpack_require__) {
409 |
410 | "use strict";
411 |
412 | // Instanciate WebAssembly module
413 | var instance = new WebAssembly.Instance(__webpack_require__.w[module.i], {});
414 |
415 | // export exports from WebAssmbly module
416 | module.exports = instance.exports;
417 |
418 | /***/ })
419 | ]]);
420 | ```
421 |
422 | # Info
423 |
424 | ## Uncompressed
425 |
426 | ```
427 | Hash: d5ea01069914247a51e0
428 | Version: webpack 3.8.1
429 | Asset Size Chunks Chunk Names
430 | 0.output.js 3.53 kB 0, 1 [emitted]
431 | 80925f35a6f1cf550d38.wasm 41 bytes 0, 1, 1 [emitted]
432 | 3d28950d91bc7246f5af.wasm 62 bytes 0, 1 [emitted]
433 | 1d2268b99656e9575a63.wasm 67 bytes 0, 1 [emitted]
434 | 1.output.js 486 bytes 1 [emitted]
435 | output.js 8.88 kB 2 [emitted] main
436 | Entrypoint main = output.js
437 | chunk {0} 0.output.js, 80925f35a6f1cf550d38.wasm, 3d28950d91bc7246f5af.wasm, 1d2268b99656e9575a63.wasm 570 bytes {2} [rendered]
438 | > [0] ./example.js 3:1-17
439 | [1] ./add.wasm 41 bytes {0} {1} [built]
440 | import() ./add.wasm [0] ./example.js 1:0-20
441 | harmony side effect evaluation ./add.wasm [2] ./math.js 1:0-33
442 | harmony export imported specifier ./add.wasm [2] ./math.js 5:0-37
443 | [2] ./math.js 400 bytes {0} [built]
444 | [exports: add, factorial, fibonacci, factorialJavascript, fibonacciJavascript]
445 | import() ./math [0] ./example.js 3:1-17
446 | [3] ./factorial.wasm 62 bytes {0} [built]
447 | [only some exports used: factorial]
448 | harmony side effect evaluation ./factorial.wasm [2] ./math.js 2:0-45
449 | harmony export imported specifier ./factorial.wasm [2] ./math.js 5:0-37
450 | [4] ./fibonacci.wasm 67 bytes {0} [built]
451 | [only some exports used: fibonacci]
452 | harmony side effect evaluation ./fibonacci.wasm [2] ./math.js 3:0-45
453 | harmony export imported specifier ./fibonacci.wasm [2] ./math.js 5:0-37
454 | chunk {1} 1.output.js, 80925f35a6f1cf550d38.wasm 41 bytes {2} [rendered]
455 | > [0] ./example.js 1:0-20
456 | [1] ./add.wasm 41 bytes {0} {1} [built]
457 | import() ./add.wasm [0] ./example.js 1:0-20
458 | harmony side effect evaluation ./add.wasm [2] ./math.js 1:0-33
459 | harmony export imported specifier ./add.wasm [2] ./math.js 5:0-37
460 | chunk {2} output.js (main) 762 bytes [entry] [rendered]
461 | > main [0] ./example.js
462 | [0] ./example.js 762 bytes {2} [built]
463 | ```
464 |
465 | ## Minimized (uglify-js, no zip)
466 |
467 | ```
468 |
469 | ```
470 |
--------------------------------------------------------------------------------
/add.wasm:
--------------------------------------------------------------------------------
1 | asm ` add
2 | j
--------------------------------------------------------------------------------
/build.js:
--------------------------------------------------------------------------------
1 | require("../build-common");
--------------------------------------------------------------------------------
/example.js:
--------------------------------------------------------------------------------
1 | import("./add.wasm").then(addModule => {
2 | console.log(addModule.add(22, 2200));
3 | import("./math").then(math => {
4 | console.log(math.add(10, 101));
5 | console.log(math.factorial(15));
6 | console.log(math.factorialJavascript(15));
7 | console.log(math.fibonacci(15));
8 | console.log(math.fibonacciJavascript(15));
9 | timed("wasm factorial", () => math.factorial(1500));
10 | timed("js factorial", () => math.factorialJavascript(1500));
11 | timed("wasm fibonacci", () => math.fibonacci(22));
12 | timed("js fibonacci", () => math.fibonacciJavascript(22));
13 | });
14 | });
15 |
16 | function timed(name, fn) {
17 | if(!console.time || !console.timeEnd)
18 | return fn();
19 | // warmup
20 | for(var i = 0; i < 10; i++)
21 | fn();
22 | console.time(name)
23 | for(var i = 0; i < 5000; i++)
24 | fn();
25 | console.timeEnd(name)
26 | }
27 |
--------------------------------------------------------------------------------
/factorial.wasm:
--------------------------------------------------------------------------------
1 | asm `
factorial
2 | AHA Ak l
--------------------------------------------------------------------------------
/fibonacci.wasm:
--------------------------------------------------------------------------------
1 | asm `
fibonacci
2 | AHA Ak Ak j
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/js/0.output.js:
--------------------------------------------------------------------------------
1 | (window["webpackJsonp"] = window["webpackJsonp"] || []).push([[0,1],[
2 | /* 0 */,
3 | /* 1 */
4 | /*!******************!*\
5 | !*** ./add.wasm ***!
6 | \******************/
7 | /*! no static exports found */
8 | /*! all exports used */
9 | /***/ (function(module, exports, __webpack_require__) {
10 |
11 | "use strict";
12 |
13 | // Instanciate WebAssembly module
14 | var instance = new WebAssembly.Instance(__webpack_require__.w[module.i], {});
15 |
16 | // export exports from WebAssmbly module
17 | module.exports = instance.exports;
18 |
19 | /***/ }),
20 | /* 2 */
21 | /*!*****************!*\
22 | !*** ./math.js ***!
23 | \*****************/
24 | /*! exports provided: add, factorial, fibonacci, factorialJavascript, fibonacciJavascript */
25 | /*! all exports used */
26 | /***/ (function(module, __webpack_exports__, __webpack_require__) {
27 |
28 | "use strict";
29 | Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
30 | /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "factorialJavascript", function() { return factorialJavascript; });
31 | /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "fibonacciJavascript", function() { return fibonacciJavascript; });
32 | /* harmony import */ var _add_wasm__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./add.wasm */1);
33 | /* harmony import */ var _add_wasm__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_add_wasm__WEBPACK_IMPORTED_MODULE_0__);
34 | /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "add", function() { return _add_wasm__WEBPACK_IMPORTED_MODULE_0__["add"]; });
35 |
36 | /* harmony import */ var _factorial_wasm__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./factorial.wasm */3);
37 | /* harmony import */ var _factorial_wasm__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_factorial_wasm__WEBPACK_IMPORTED_MODULE_1__);
38 | /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "factorial", function() { return _factorial_wasm__WEBPACK_IMPORTED_MODULE_1__["factorial"]; });
39 |
40 | /* harmony import */ var _fibonacci_wasm__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./fibonacci.wasm */4);
41 | /* harmony import */ var _fibonacci_wasm__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_fibonacci_wasm__WEBPACK_IMPORTED_MODULE_2__);
42 | /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "fibonacci", function() { return _fibonacci_wasm__WEBPACK_IMPORTED_MODULE_2__["fibonacci"]; });
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | function factorialJavascript(i) {
51 | if(i < 1) return 1;
52 | return i * factorialJavascript(i - 1);
53 | }
54 |
55 | function fibonacciJavascript(i) {
56 | if(i < 2) return 1;
57 | return fibonacciJavascript(i - 1) + fibonacciJavascript(i - 2);
58 | }
59 |
60 |
61 | /***/ }),
62 | /* 3 */
63 | /*!************************!*\
64 | !*** ./factorial.wasm ***!
65 | \************************/
66 | /*! no static exports found */
67 | /*! exports used: factorial */
68 | /***/ (function(module, exports, __webpack_require__) {
69 |
70 | "use strict";
71 |
72 | // Instanciate WebAssembly module
73 | var instance = new WebAssembly.Instance(__webpack_require__.w[module.i], {});
74 |
75 | // export exports from WebAssmbly module
76 | module.exports = instance.exports;
77 |
78 | /***/ }),
79 | /* 4 */
80 | /*!************************!*\
81 | !*** ./fibonacci.wasm ***!
82 | \************************/
83 | /*! no static exports found */
84 | /*! exports used: fibonacci */
85 | /***/ (function(module, exports, __webpack_require__) {
86 |
87 | "use strict";
88 |
89 | // Instanciate WebAssembly module
90 | var instance = new WebAssembly.Instance(__webpack_require__.w[module.i], {});
91 |
92 | // export exports from WebAssmbly module
93 | module.exports = instance.exports;
94 |
95 | /***/ })
96 | ]]);
--------------------------------------------------------------------------------
/js/1.output.js:
--------------------------------------------------------------------------------
1 | (window["webpackJsonp"] = window["webpackJsonp"] || []).push([[1],[
2 | /* 0 */,
3 | /* 1 */
4 | /*!******************!*\
5 | !*** ./add.wasm ***!
6 | \******************/
7 | /*! no static exports found */
8 | /*! all exports used */
9 | /***/ (function(module, exports, __webpack_require__) {
10 |
11 | "use strict";
12 |
13 | // Instanciate WebAssembly module
14 | var instance = new WebAssembly.Instance(__webpack_require__.w[module.i], {});
15 |
16 | // export exports from WebAssmbly module
17 | module.exports = instance.exports;
18 |
19 | /***/ })
20 | ]]);
--------------------------------------------------------------------------------
/js/1d2268b99656e9575a63.wasm:
--------------------------------------------------------------------------------
1 | asm `
fibonacci
2 | AHA Ak Ak j
--------------------------------------------------------------------------------
/js/3d28950d91bc7246f5af.wasm:
--------------------------------------------------------------------------------
1 | asm `
factorial
2 | AHA Ak l
--------------------------------------------------------------------------------
/js/80925f35a6f1cf550d38.wasm:
--------------------------------------------------------------------------------
1 | asm ` add
2 | j
--------------------------------------------------------------------------------
/js/output.js:
--------------------------------------------------------------------------------
1 | /******/ (function(modules) { // webpackBootstrap
2 | /******/ // install a JSONP callback for chunk loading
3 | /******/ function webpackJsonpCallback(data) {
4 | /******/ var chunkIds = data[0], moreModules = data[1], executeModules = data[2];
5 | /******/ // add "moreModules" to the modules object,
6 | /******/ // then flag all "chunkIds" as loaded and fire callback
7 | /******/ var moduleId, chunkId, i = 0, resolves = [], result;
8 | /******/ for(;i < chunkIds.length; i++) {
9 | /******/ chunkId = chunkIds[i];
10 | /******/ if(installedChunks[chunkId]) {
11 | /******/ resolves.push(installedChunks[chunkId][0]);
12 | /******/ }
13 | /******/ installedChunks[chunkId] = 0;
14 | /******/ }
15 | /******/ for(moduleId in moreModules) {
16 | /******/ if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {
17 | /******/ modules[moduleId] = moreModules[moduleId];
18 | /******/ }
19 | /******/ }
20 | /******/ if(parentJsonpFunction) parentJsonpFunction(data);
21 | /******/ while(resolves.length) {
22 | /******/ resolves.shift()();
23 | /******/ }
24 | /******/
25 | /******/ };
26 | /******/
27 | /******/ // The module cache
28 | /******/ var installedModules = {};
29 | /******/
30 | /******/ // object to store loaded and loading chunks
31 | /******/ var installedChunks = {
32 | /******/ 2: 0
33 | /******/ };
34 | /******/
35 | /******/ var scheduledModules = [];
36 | /******/
37 | /******/ // object to store loaded and loading wasm modules
38 | /******/ var installedWasmModules = {};
39 | /******/
40 | /******/ // The require function
41 | /******/ function __webpack_require__(moduleId) {
42 | /******/
43 | /******/ // Check if module is in cache
44 | /******/ if(installedModules[moduleId]) {
45 | /******/ return installedModules[moduleId].exports;
46 | /******/ }
47 | /******/ // Create a new module (and put it into the cache)
48 | /******/ var module = installedModules[moduleId] = {
49 | /******/ i: moduleId,
50 | /******/ l: false,
51 | /******/ exports: {}
52 | /******/ };
53 | /******/
54 | /******/ // Execute the module function
55 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
56 | /******/
57 | /******/ // Flag the module as loaded
58 | /******/ module.l = true;
59 | /******/
60 | /******/ // Return the exports of the module
61 | /******/ return module.exports;
62 | /******/ }
63 | /******/
64 | /******/ // This file contains only the entry chunk.
65 | /******/ // The chunk loading function for additional chunks
66 | /******/ __webpack_require__.e = function requireEnsure(chunkId) {
67 | /******/ var promises = [];
68 | /******/
69 | /******/
70 | /******/ // JSONP chunk loading for javascript
71 | /******/
72 | /******/ var installedChunkData = installedChunks[chunkId];
73 | /******/ if(installedChunkData !== 0) { // 0 means "already installed".
74 | /******/
75 | /******/ // a Promise means "currently loading".
76 | /******/ if(installedChunkData) {
77 | /******/ promises.push(installedChunkData[2]);
78 | /******/ } else {
79 | /******/ // setup Promise in chunk cache
80 | /******/ var promise = new Promise(function(resolve, reject) {
81 | /******/ installedChunkData = installedChunks[chunkId] = [resolve, reject];
82 | /******/ installedChunkData[2] = promise;
83 | /******/ });
84 | /******/
85 | /******/ // start chunk loading
86 | /******/ var head = document.getElementsByTagName('head')[0];
87 | /******/ var script = document.createElement('script');
88 | /******/ script.charset = 'utf-8';
89 | /******/ script.timeout = 120000;
90 | /******/
91 | /******/ if (__webpack_require__.nc) {
92 | /******/ script.setAttribute("nonce", __webpack_require__.nc);
93 | /******/ }
94 | /******/ script.src = __webpack_require__.p + "" + chunkId + ".output.js";
95 | /******/ var timeout = setTimeout(function(){
96 | /******/ onScriptComplete({ type: 'timeout', target: script });
97 | /******/ }, 120000);
98 | /******/ script.onerror = script.onload = onScriptComplete;
99 | /******/ function onScriptComplete(event) {
100 | /******/ // avoid mem leaks in IE.
101 | /******/ script.onerror = script.onload = null;
102 | /******/ clearTimeout(timeout);
103 | /******/ var chunk = installedChunks[chunkId];
104 | /******/ if(chunk !== 0) {
105 | /******/ if(chunk) {
106 | /******/ var errorType = event && (event.type === 'load' ? 'missing' : event.type);
107 | /******/ var realSrc = event && event.target && event.target.src;
108 | /******/ var error = new Error('Loading chunk ' + chunkId + ' failed.\n(' + errorType + ': ' + realSrc + ')');
109 | /******/ error.type = errorType;
110 | /******/ error.request = realSrc;
111 | /******/ chunk[1](error);
112 | /******/ }
113 | /******/ installedChunks[chunkId] = undefined;
114 | /******/ }
115 | /******/ };
116 | /******/ head.appendChild(script);
117 | /******/ promises.push(promise);
118 | /******/ }
119 | /******/ }
120 | /******/
121 | /******/ // Fetch + compile chunk loading for webassembly
122 | /******/
123 | /******/ var wasmModules = {"0":[1,3,4],"1":[1]}[chunkId] || [];
124 | /******/
125 | /******/ wasmModules.forEach(function(wasmModuleId) {
126 | /******/ var installedWasmModuleData = installedWasmModules[wasmModuleId];
127 | /******/
128 | /******/ // a Promise means "currently loading" or "already loaded".
129 | /******/ if(installedWasmModuleData) {
130 | /******/ promises.push(installedWasmModuleData);
131 | /******/ } else {
132 | /******/ var promise = installedWasmModules[wasmModuleId] = fetch(__webpack_require__.p + "" + {"1":"80925f35a6f1cf550d38","3":"3d28950d91bc7246f5af","4":"1d2268b99656e9575a63"}[wasmModuleId] + ".wasm")
133 | /******/ .then(function(response) { return response.arrayBuffer(); })
134 | /******/ .then(function(bytes) { return WebAssembly.compile(bytes); })
135 | /******/ .then(function(module) { __webpack_require__.w[wasmModuleId] = module; })
136 | /******/ promises.push(promise);
137 | /******/ }
138 | /******/ });
139 | /******/ return Promise.all(promises);
140 | /******/ };
141 | /******/
142 | /******/ // expose the modules object (__webpack_modules__)
143 | /******/ __webpack_require__.m = modules;
144 | /******/
145 | /******/ // expose the module cache
146 | /******/ __webpack_require__.c = installedModules;
147 | /******/
148 | /******/ // define getter function for harmony exports
149 | /******/ __webpack_require__.d = function(exports, name, getter) {
150 | /******/ if(!__webpack_require__.o(exports, name)) {
151 | /******/ Object.defineProperty(exports, name, {
152 | /******/ configurable: false,
153 | /******/ enumerable: true,
154 | /******/ get: getter
155 | /******/ });
156 | /******/ }
157 | /******/ };
158 | /******/
159 | /******/ // getDefaultExport function for compatibility with non-harmony modules
160 | /******/ __webpack_require__.n = function(module) {
161 | /******/ var getter = module && module.__esModule ?
162 | /******/ function getDefault() { return module['default']; } :
163 | /******/ function getModuleExports() { return module; };
164 | /******/ __webpack_require__.d(getter, 'a', getter);
165 | /******/ return getter;
166 | /******/ };
167 | /******/
168 | /******/ // Object.prototype.hasOwnProperty.call
169 | /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
170 | /******/
171 | /******/ // __webpack_public_path__
172 | /******/ __webpack_require__.p = "js/";
173 | /******/
174 | /******/ // on error function for async loading
175 | /******/ __webpack_require__.oe = function(err) { console.error(err); throw err; };
176 | /******/
177 | /******/ // object with all compiled WebAssmbly.Modules
178 | /******/ __webpack_require__.w = {};
179 | /******/
180 | /******/ var jsonpArray = window["webpackJsonp"] = window["webpackJsonp"] || [];
181 | /******/ var parentJsonpFunction = jsonpArray.push.bind(jsonpArray);
182 | /******/ jsonpArray.push = webpackJsonpCallback;
183 | /******/ jsonpArray = jsonpArray.slice();
184 | /******/ for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);
185 | /******/
186 | /******/ // Load entry module and return exports
187 | /******/ return __webpack_require__(__webpack_require__.s = 0);
188 | /******/ })
189 | /************************************************************************/
190 | /******/ ([
191 | /* 0 */
192 | /*!********************!*\
193 | !*** ./example.js ***!
194 | \********************/
195 | /*! no static exports found */
196 | /*! all exports used */
197 | /***/ (function(module, exports, __webpack_require__) {
198 |
199 | __webpack_require__.e/* import() */(1).then(__webpack_require__.bind(null, /*! ./add.wasm */1)).then(addModule => {
200 | console.log(addModule.add(22, 2200));
201 | __webpack_require__.e/* import() */(0).then(__webpack_require__.bind(null, /*! ./math */2)).then(math => {
202 | console.log(math.add(10, 101));
203 | console.log(math.factorial(15));
204 | console.log(math.factorialJavascript(15));
205 | console.log(math.fibonacci(15));
206 | console.log(math.fibonacciJavascript(15));
207 | timed("wasm factorial", () => math.factorial(1500));
208 | timed("js factorial", () => math.factorialJavascript(1500));
209 | timed("wasm fibonacci", () => math.fibonacci(22));
210 | timed("js fibonacci", () => math.fibonacciJavascript(22));
211 | });
212 | });
213 |
214 | function timed(name, fn) {
215 | if(!console.time || !console.timeEnd)
216 | return fn();
217 | // warmup
218 | for(var i = 0; i < 10; i++)
219 | fn();
220 | console.time(name)
221 | for(var i = 0; i < 5000; i++)
222 | fn();
223 | console.timeEnd(name)
224 | }
225 |
226 |
227 | /***/ })
228 | /******/ ]);
--------------------------------------------------------------------------------
/math.js:
--------------------------------------------------------------------------------
1 | import { add } from "./add.wasm";
2 | import { factorial } from "./factorial.wasm";
3 | import { fibonacci } from "./fibonacci.wasm";
4 |
5 | export { add, factorial, fibonacci };
6 |
7 | export function factorialJavascript(i) {
8 | if(i < 1) return 1;
9 | return i * factorialJavascript(i - 1);
10 | }
11 |
12 | export function fibonacciJavascript(i) {
13 | if(i < 2) return 1;
14 | return fibonacciJavascript(i - 1) + fibonacciJavascript(i - 2);
15 | }
16 |
--------------------------------------------------------------------------------
/template.md:
--------------------------------------------------------------------------------
1 | This very simple example shows usage of WebAssembly.
2 |
3 | WebAssembly modules can be imported like other modules. Their download and compilation happens in parallel to the download and evaluation of the javascript chunk.
4 |
5 | # example.js
6 |
7 | ``` javascript
8 | {{example.js}}
9 | ```
10 |
11 | # math.js
12 |
13 | ``` javascript
14 | {{math.js}}
15 | ```
16 |
17 | # js/output.js
18 |
19 | ``` javascript
20 | {{js/output.js}}
21 | ```
22 |
23 | # js/0.output.js
24 |
25 | ``` javascript
26 | {{js/0.output.js}}
27 | ```
28 |
29 | # js/1.output.js
30 |
31 | ``` javascript
32 | {{js/1.output.js}}
33 | ```
34 |
35 | # Info
36 |
37 | ## Uncompressed
38 |
39 | ```
40 | {{stdout}}
41 | ```
42 |
43 | ## Minimized (uglify-js, no zip)
44 |
45 | ```
46 | {{min:stdout}}
47 | ```
48 |
--------------------------------------------------------------------------------
/webpack.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | output: {
3 | webassemblyModuleFilename: "[modulehash].wasm",
4 | publicPath: "js/"
5 | },
6 | module: {
7 | rules: [
8 | {
9 | test: /\.wasm$/,
10 | type: "webassembly/experimental"
11 | }
12 | ]
13 | }
14 | };
15 |
--------------------------------------------------------------------------------