wasm.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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()
  21. .then(buffer => {
  22. return WebAssembly.instantiate(buffer, {
  23. env: {
  24. mapping_callback(
  25. generatedLine,
  26. generatedColumn,
  27. hasLastGeneratedColumn,
  28. lastGeneratedColumn,
  29. hasOriginal,
  30. source,
  31. originalLine,
  32. originalColumn,
  33. hasName,
  34. name
  35. ) {
  36. const mapping = new Mapping();
  37. // JS uses 1-based line numbers, wasm uses 0-based.
  38. mapping.generatedLine = generatedLine + 1;
  39. mapping.generatedColumn = generatedColumn;
  40. if (hasLastGeneratedColumn) {
  41. // JS uses inclusive last generated column, wasm uses exclusive.
  42. mapping.lastGeneratedColumn = lastGeneratedColumn - 1;
  43. }
  44. if (hasOriginal) {
  45. mapping.source = source;
  46. // JS uses 1-based line numbers, wasm uses 0-based.
  47. mapping.originalLine = originalLine + 1;
  48. mapping.originalColumn = originalColumn;
  49. if (hasName) {
  50. mapping.name = name;
  51. }
  52. }
  53. callbackStack[callbackStack.length - 1](mapping);
  54. },
  55. start_all_generated_locations_for() {
  56. console.time("all_generated_locations_for");
  57. },
  58. end_all_generated_locations_for() {
  59. console.timeEnd("all_generated_locations_for");
  60. },
  61. start_compute_column_spans() {
  62. console.time("compute_column_spans");
  63. },
  64. end_compute_column_spans() {
  65. console.timeEnd("compute_column_spans");
  66. },
  67. start_generated_location_for() {
  68. console.time("generated_location_for");
  69. },
  70. end_generated_location_for() {
  71. console.timeEnd("generated_location_for");
  72. },
  73. start_original_location_for() {
  74. console.time("original_location_for");
  75. },
  76. end_original_location_for() {
  77. console.timeEnd("original_location_for");
  78. },
  79. start_parse_mappings() {
  80. console.time("parse_mappings");
  81. },
  82. end_parse_mappings() {
  83. console.timeEnd("parse_mappings");
  84. },
  85. start_sort_by_generated_location() {
  86. console.time("sort_by_generated_location");
  87. },
  88. end_sort_by_generated_location() {
  89. console.timeEnd("sort_by_generated_location");
  90. },
  91. start_sort_by_original_location() {
  92. console.time("sort_by_original_location");
  93. },
  94. end_sort_by_original_location() {
  95. console.timeEnd("sort_by_original_location");
  96. },
  97. },
  98. });
  99. })
  100. .then(Wasm => {
  101. return {
  102. exports: Wasm.instance.exports,
  103. withMappingCallback: (mappingCallback, f) => {
  104. callbackStack.push(mappingCallback);
  105. try {
  106. f();
  107. } finally {
  108. callbackStack.pop();
  109. }
  110. },
  111. };
  112. })
  113. .then(null, e => {
  114. cachedWasm = null;
  115. throw e;
  116. });
  117. return cachedWasm;
  118. };