WebAssemblyImportDependency.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DependencyReference = require("./DependencyReference");
  7. const ModuleDependency = require("./ModuleDependency");
  8. const UnsupportedWebAssemblyFeatureError = require("../wasm/UnsupportedWebAssemblyFeatureError");
  9. /** @typedef {import("@webassemblyjs/ast").ModuleImportDescription} ModuleImportDescription */
  10. class WebAssemblyImportDependency extends ModuleDependency {
  11. /**
  12. * @param {string} request the request
  13. * @param {string} name the imported name
  14. * @param {ModuleImportDescription} description the WASM ast node
  15. * @param {false | string} onlyDirectImport if only direct imports are allowed
  16. */
  17. constructor(request, name, description, onlyDirectImport) {
  18. super(request);
  19. /** @type {string} */
  20. this.name = name;
  21. /** @type {ModuleImportDescription} */
  22. this.description = description;
  23. /** @type {false | string} */
  24. this.onlyDirectImport = onlyDirectImport;
  25. }
  26. getReference() {
  27. if (!this.module) return null;
  28. return new DependencyReference(this.module, [this.name], false);
  29. }
  30. getErrors() {
  31. if (
  32. this.onlyDirectImport &&
  33. this.module &&
  34. !this.module.type.startsWith("webassembly")
  35. ) {
  36. return [
  37. new UnsupportedWebAssemblyFeatureError(
  38. `Import "${this.name}" from "${this.request}" with ${this.onlyDirectImport} can only be used for direct wasm to wasm dependencies`
  39. )
  40. ];
  41. }
  42. }
  43. get type() {
  44. return "wasm import";
  45. }
  46. }
  47. module.exports = WebAssemblyImportDependency;