You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

4 years ago
  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;