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.

107 lines
3.2 KiB

5 years ago
  1. const readWasm = require("../lib/read-wasm");
  2. /**
  3. * Provide the JIT with a nice shape / hidden class.
  4. */
  5. function Mapping() {
  6. this.generatedLine = 0;
  7. this.generatedColumn = 0;
  8. this.lastGeneratedColumn = null;
  9. this.source = null;
  10. this.originalLine = null;
  11. this.originalColumn = null;
  12. this.name = null;
  13. }
  14. let cachedWasm = null;
  15. module.exports = function wasm() {
  16. if (cachedWasm) {
  17. return cachedWasm;
  18. }
  19. const callbackStack = [];
  20. cachedWasm = readWasm().then(buffer => {
  21. return WebAssembly.instantiate(buffer, {
  22. env: {
  23. mapping_callback(
  24. generatedLine,
  25. generatedColumn,
  26. hasLastGeneratedColumn,
  27. lastGeneratedColumn,
  28. hasOriginal,
  29. source,
  30. originalLine,
  31. originalColumn,
  32. hasName,
  33. name
  34. ) {
  35. const mapping = new Mapping();
  36. // JS uses 1-based line numbers, wasm uses 0-based.
  37. mapping.generatedLine = generatedLine + 1;
  38. mapping.generatedColumn = generatedColumn;
  39. if (hasLastGeneratedColumn) {
  40. // JS uses inclusive last generated column, wasm uses exclusive.
  41. mapping.lastGeneratedColumn = lastGeneratedColumn - 1;
  42. }
  43. if (hasOriginal) {
  44. mapping.source = source;
  45. // JS uses 1-based line numbers, wasm uses 0-based.
  46. mapping.originalLine = originalLine + 1;
  47. mapping.originalColumn = originalColumn;
  48. if (hasName) {
  49. mapping.name = name;
  50. }
  51. }
  52. callbackStack[callbackStack.length - 1](mapping);
  53. },
  54. start_all_generated_locations_for() { console.time("all_generated_locations_for"); },
  55. end_all_generated_locations_for() { console.timeEnd("all_generated_locations_for"); },
  56. start_compute_column_spans() { console.time("compute_column_spans"); },
  57. end_compute_column_spans() { console.timeEnd("compute_column_spans"); },
  58. start_generated_location_for() { console.time("generated_location_for"); },
  59. end_generated_location_for() { console.timeEnd("generated_location_for"); },
  60. start_original_location_for() { console.time("original_location_for"); },
  61. end_original_location_for() { console.timeEnd("original_location_for"); },
  62. start_parse_mappings() { console.time("parse_mappings"); },
  63. end_parse_mappings() { console.timeEnd("parse_mappings"); },
  64. start_sort_by_generated_location() { console.time("sort_by_generated_location"); },
  65. end_sort_by_generated_location() { console.timeEnd("sort_by_generated_location"); },
  66. start_sort_by_original_location() { console.time("sort_by_original_location"); },
  67. end_sort_by_original_location() { console.timeEnd("sort_by_original_location"); },
  68. }
  69. });
  70. }).then(Wasm => {
  71. return {
  72. exports: Wasm.instance.exports,
  73. withMappingCallback: (mappingCallback, f) => {
  74. callbackStack.push(mappingCallback);
  75. try {
  76. f();
  77. } finally {
  78. callbackStack.pop();
  79. }
  80. }
  81. };
  82. }).then(null, e => {
  83. cachedWasm = null;
  84. throw e;
  85. });
  86. return cachedWasm;
  87. };