ronantakizawa/codereview-bench huggingface dataset:
Repository: mozilla/pdf.js
File: src/core/jpx.js (JavaScript)

Code before:
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        const wasmFile = "openjpeg.wasm";
        if (this.#openjpegWasmUrl) {
          const response = await fetch(`${this.#openjpegWasmUrl}/${wasmFile}`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise(
            "FetchOpenjpegWasm",
            {
              filename: wasmFile,
            }
          );
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm ${e}`);
      return false;
    } finally {
      this.#handler = null;
      this.#openjpegWasmUrl = "";
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate");
    }

    this.#modulePromise ||= OpenJPEG({
      warn,
      instantiateWasm: this.#instantiateWasm.bind(this),
    });

    const module = await this.#modulePromise;
    let ptr;

    try {
      const size = bytes.length;

Review comment (suggestion):
```suggestion
      warn(`Cannot load ${wasmFile}: "${e}".`);
```

Code after:
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        if (this.#wasmUrl !== null) {
          const response = await fetch(`${this.#wasmUrl}openjpeg.wasm`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise("FetchWasm", {
            filename: "openjpeg.wasm",
          });
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm: "${e}".`);
      return false;
    } finally {
      this.#handler = null;
      this.#wasmUrl = null;
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate.");
    }

    this.#modulePromise ||= OpenJPEG({
      warn,
      instantiateWasm: this.#instantiateWasm.bind(this),
    });

    const module = await this.#modulePromise;
    let ptr;

    try {
      const size = bytes.length;
      ptr = module._malloc(size);
      module.HEAPU8.set(bytes, ptr);
      const ret = module._jp2_decode(
        ptr,
Repository: mozilla/pdf.js
File: src/core/jpx.js (JavaScript)

Code before:
class JpxImage {
  static #buffer = null;

  static #handler = null;

  static #instantiationFailed = false;

  static #modulePromise = null;

  static #openjpegWasmUrl = "";

  static setOptions({ handler, openjpegWasmUrl }) {
    if (!this.#buffer) {
      this.#openjpegWasmUrl = openjpegWasmUrl;
      if (!openjpegWasmUrl) {
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        const wasmFile = "openjpeg.wasm";
        if (this.#openjpegWasmUrl) {
          const response = await fetch(`${this.#openjpegWasmUrl}/${wasmFile}`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise(
            "FetchOpenjpegWasm",
            {
              filename: wasmFile,
            }
          );
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm ${e}`);
      return false;
    } finally {
      this.#handler = null;
      this.#openjpegWasmUrl = "";
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}

Review comment (bug):
This looks wrong, since these kinds of URLs all should include a trailing slash.
```suggestion
          const response = await fetch(`${this.#openjpegWasmUrl}${wasmFile}`);
```

Code after:
class JpxImage {
  static #buffer = null;

  static #handler = null;

  static #instantiationFailed = false;

  static #modulePromise = null;

  static #wasmUrl = null;

  static setOptions({ handler, wasmUrl }) {
    if (!this.#buffer) {
      this.#wasmUrl = wasmUrl || null;
      if (wasmUrl === null) {
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        if (this.#wasmUrl !== null) {
          const response = await fetch(`${this.#wasmUrl}openjpeg.wasm`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise("FetchWasm", {
            filename: "openjpeg.wasm",
          });
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm: "${e}".`);
      return false;
    } finally {
      this.#handler = null;
      this.#wasmUrl = null;
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate.");
    }
Repository: mozilla/pdf.js
File: src/core/jpx.js (JavaScript)

Code before:
  static #modulePromise = null;

  static #openjpegWasmUrl = "";

  static setOptions({ handler, openjpegWasmUrl }) {
    if (!this.#buffer) {
      this.#openjpegWasmUrl = openjpegWasmUrl;
      if (!openjpegWasmUrl) {
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        const wasmFile = "openjpeg.wasm";
        if (this.#openjpegWasmUrl) {
          const response = await fetch(`${this.#openjpegWasmUrl}/${wasmFile}`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise(
            "FetchOpenjpegWasm",
            {
              filename: wasmFile,
            }
          );
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm ${e}`);
      return false;
    } finally {
      this.#handler = null;
      this.#openjpegWasmUrl = "";
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate");
    }

    this.#modulePromise ||= OpenJPEG({
      warn,

Review comment (suggestion):
Nit:
```suggestion
            { filename: wasmFile }
```

Code after:
  static #modulePromise = null;

  static #wasmUrl = null;

  static setOptions({ handler, wasmUrl }) {
    if (!this.#buffer) {
      this.#wasmUrl = wasmUrl || null;
      if (wasmUrl === null) {
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        if (this.#wasmUrl !== null) {
          const response = await fetch(`${this.#wasmUrl}openjpeg.wasm`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise("FetchWasm", {
            filename: "openjpeg.wasm",
          });
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm: "${e}".`);
      return false;
    } finally {
      this.#handler = null;
      this.#wasmUrl = null;
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate.");
    }

    this.#modulePromise ||= OpenJPEG({
      warn,
      instantiateWasm: this.#instantiateWasm.bind(this),
    });

    const module = await this.#modulePromise;
Repository: mozilla/pdf.js
File: src/core/jpx.js (JavaScript)

Code before:

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        const wasmFile = "openjpeg.wasm";
        if (this.#openjpegWasmUrl) {
          const response = await fetch(`${this.#openjpegWasmUrl}/${wasmFile}`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise(
            "FetchOpenjpegWasm",
            {
              filename: wasmFile,
            }
          );
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm ${e}`);
      return false;
    } finally {
      this.#handler = null;
      this.#openjpegWasmUrl = "";
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate");
    }

    this.#modulePromise ||= OpenJPEG({
      warn,
      instantiateWasm: this.#instantiateWasm.bind(this),
    });

    const module = await this.#modulePromise;
    let ptr;

    try {
      const size = bytes.length;
      ptr = module._malloc(size);
      module.HEAPU8.set(bytes, ptr);
      const ret = module._jp2_decode(
        ptr,

Review comment (suggestion):
```suggestion
      this.#openjpegWasmUrl = null;
```

Code after:

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        if (this.#wasmUrl !== null) {
          const response = await fetch(`${this.#wasmUrl}openjpeg.wasm`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise("FetchWasm", {
            filename: "openjpeg.wasm",
          });
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm: "${e}".`);
      return false;
    } finally {
      this.#handler = null;
      this.#wasmUrl = null;
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate.");
    }

    this.#modulePromise ||= OpenJPEG({
      warn,
      instantiateWasm: this.#instantiateWasm.bind(this),
    });

    const module = await this.#modulePromise;
    let ptr;

    try {
      const size = bytes.length;
      ptr = module._malloc(size);
      module.HEAPU8.set(bytes, ptr);
      const ret = module._jp2_decode(
        ptr,
        size,
        numComponents > 0 ? numComponents : 0,
        !!isIndexedColormap,
        !!smaskInData
Repository: mozilla/pdf.js
File: src/core/jpx.js (JavaScript)

Code before:
          this.#buffer = await this.#handler.sendWithPromise(
            "FetchOpenjpegWasm",
            {
              filename: wasmFile,
            }
          );
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm ${e}`);
      return false;
    } finally {
      this.#handler = null;
      this.#openjpegWasmUrl = "";
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate");
    }

    this.#modulePromise ||= OpenJPEG({
      warn,
      instantiateWasm: this.#instantiateWasm.bind(this),
    });

    const module = await this.#modulePromise;
    let ptr;

    try {
      const size = bytes.length;
      ptr = module._malloc(size);
      module.HEAPU8.set(bytes, ptr);
      const ret = module._jp2_decode(
        ptr,
        size,
        numComponents > 0 ? numComponents : 0,
        !!isIndexedColormap,
        !!smaskInData
      );
      if (ret) {
        const { errorMessages } = module;
        if (errorMessages) {
          delete module.errorMessages;

Review comment (bug):
```suggestion
      throw new JpxError("OpenJPEG failed to instantiate.");
```

Code after:
            filename: "openjpeg.wasm",
          });
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm: "${e}".`);
      return false;
    } finally {
      this.#handler = null;
      this.#wasmUrl = null;
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate.");
    }

    this.#modulePromise ||= OpenJPEG({
      warn,
      instantiateWasm: this.#instantiateWasm.bind(this),
    });

    const module = await this.#modulePromise;
    let ptr;

    try {
      const size = bytes.length;
      ptr = module._malloc(size);
      module.HEAPU8.set(bytes, ptr);
      const ret = module._jp2_decode(
        ptr,
        size,
        numComponents > 0 ? numComponents : 0,
        !!isIndexedColormap,
        !!smaskInData
      );
      if (ret) {
        const { errorMessages } = module;
        if (errorMessages) {
          delete module.errorMessages;
          throw new JpxError(errorMessages);
        }
        throw new JpxError("Unknown error");
      }
Repository: mozilla/pdf.js
File: src/core/jpx.js (JavaScript)

Code before:

class JpxImage {
  static #buffer = null;

  static #handler = null;

  static #instantiationFailed = false;

  static #modulePromise = null;

  static #openjpegWasmUrl = "";

  static setOptions({ handler, openjpegWasmUrl }) {
    if (!this.#buffer) {
      this.#openjpegWasmUrl = openjpegWasmUrl;
      if (!openjpegWasmUrl) {
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        const wasmFile = "openjpeg.wasm";
        if (this.#openjpegWasmUrl) {
          const response = await fetch(`${this.#openjpegWasmUrl}/${wasmFile}`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise(
            "FetchOpenjpegWasm",
            {
              filename: wasmFile,
            }
          );
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm ${e}`);
      return false;
    } finally {
      this.#handler = null;
      this.#openjpegWasmUrl = "";
    }
  }

  static async decode(
    bytes,

Review comment (suggestion):
Please let's keep this consistent with the other URL parameters, for CMaps and StandardFonts, used in the evaluator.
```suggestion
        if (this.#openjpegWasmUrl !== null) {
```

Code after:

class JpxImage {
  static #buffer = null;

  static #handler = null;

  static #instantiationFailed = false;

  static #modulePromise = null;

  static #wasmUrl = null;

  static setOptions({ handler, wasmUrl }) {
    if (!this.#buffer) {
      this.#wasmUrl = wasmUrl || null;
      if (wasmUrl === null) {
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        if (this.#wasmUrl !== null) {
          const response = await fetch(`${this.#wasmUrl}openjpeg.wasm`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise("FetchWasm", {
            filename: "openjpeg.wasm",
          });
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm: "${e}".`);
      return false;
    } finally {
      this.#handler = null;
      this.#wasmUrl = null;
    }
  }

  static async decode(
    bytes,
    { numComponents = 4, isIndexedColormap = false, smaskInData = false } = {}
  ) {
    if (this.#instantiationFailed) {
      throw new JpxError("OpenJPEG failed to instantiate.");
Repository: mozilla/pdf.js
File: src/core/jpx.js (JavaScript)

Code before:

import { BaseException, warn } from "../shared/util.js";
import OpenJPEG from "../../external/openjpeg/openjpeg.js";
import { Stream } from "./stream.js";

class JpxError extends BaseException {
  constructor(msg) {
    super(msg, "JpxError");
  }
}

class JpxImage {
  static #buffer = null;

  static #handler = null;

  static #instantiationFailed = false;

  static #modulePromise = null;

  static #openjpegWasmUrl = "";

  static setOptions({ handler, openjpegWasmUrl }) {
    if (!this.#buffer) {
      this.#openjpegWasmUrl = openjpegWasmUrl;
      if (!openjpegWasmUrl) {
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        const wasmFile = "openjpeg.wasm";
        if (this.#openjpegWasmUrl) {
          const response = await fetch(`${this.#openjpegWasmUrl}/${wasmFile}`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise(
            "FetchOpenjpegWasm",
            {
              filename: wasmFile,
            }
          );
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;

Review comment (suggestion):
```suggestion
      if (openjpegWasmUrl === null) {
```

Code after:

import { BaseException, warn } from "../shared/util.js";
import OpenJPEG from "../../external/openjpeg/openjpeg.js";
import { Stream } from "./stream.js";

class JpxError extends BaseException {
  constructor(msg) {
    super(msg, "JpxError");
  }
}

class JpxImage {
  static #buffer = null;

  static #handler = null;

  static #instantiationFailed = false;

  static #modulePromise = null;

  static #wasmUrl = null;

  static setOptions({ handler, wasmUrl }) {
    if (!this.#buffer) {
      this.#wasmUrl = wasmUrl || null;
      if (wasmUrl === null) {
        this.#handler = handler;
      }
    }
  }

  static async #instantiateWasm(imports, successCallback) {
    try {
      if (!this.#buffer) {
        if (this.#wasmUrl !== null) {
          const response = await fetch(`${this.#wasmUrl}openjpeg.wasm`);
          this.#buffer = await response.arrayBuffer();
        } else {
          this.#buffer = await this.#handler.sendWithPromise("FetchWasm", {
            filename: "openjpeg.wasm",
          });
        }
      }
      const results = await WebAssembly.instantiate(this.#buffer, imports);
      return successCallback(results.instance);
    } catch (e) {
      this.#instantiationFailed = true;
      warn(`Cannot load openjpeg.wasm: "${e}".`);
      return false;
    } finally {
      this.#handler = null;
Repository: mozilla/pdf.js
File: gulpfile.mjs (JavaScript)

Code before:
    encoding: false,
  });
}

function createStandardFontBundle() {
  return gulp.src(
    [
      "external/standard_fonts/*.pfb",
      "external/standard_fonts/*.ttf",
      "external/standard_fonts/LICENSE_FOXIT",
      "external/standard_fonts/LICENSE_LIBERATION",
    ],
    {
      base: "external/standard_fonts",
      encoding: false,
    }
  );
}

function createOpenjpegWasmBundle() {
  return gulp.src(["external/openjpeg/openjpeg.wasm"], {
    base: "external/openjpeg",
    encoding: false,
  });
}

function checkFile(filePath) {
  try {
    const stat = fs.lstatSync(filePath);
    return stat.isFile();
  } catch {
    return false;
  }
}

function checkDir(dirPath) {
  try {
    const stat = fs.lstatSync(dirPath);
    return stat.isDirectory();
  } catch {
    return false;
  }
}

function replaceInFile(filePath, find, replacement) {
  let content = fs.readFileSync(filePath).toString();
  content = content.replace(find, replacement);
  fs.writeFileSync(filePath, content);
}

function getTempFile(prefix, suffix) {

Review comment (suggestion):
This should prepare for having more than one wasm file.
```suggestion
function createWasmBundle() {
  return gulp.src(["external/openjpeg/openjpeg.wasm"], {
    base: "external/openjpeg",
    encoding: false,
  });
}

```

Code after:
    encoding: false,
  });
}

function createStandardFontBundle() {
  return gulp.src(
    [
      "external/standard_fonts/*.pfb",
      "external/standard_fonts/*.ttf",
      "external/standard_fonts/LICENSE_FOXIT",
      "external/standard_fonts/LICENSE_LIBERATION",
    ],
    {
      base: "external/standard_fonts",
      encoding: false,
    }
  );
}

function createWasmBundle() {
  return gulp.src(["external/openjpeg/openjpeg.wasm"], {
    base: "external/openjpeg",
    encoding: false,
  });
}

function checkFile(filePath) {
  try {
    const stat = fs.lstatSync(filePath);
    return stat.isFile();
  } catch {
    return false;
  }
}

function checkDir(dirPath) {
  try {
    const stat = fs.lstatSync(dirPath);
    return stat.isDirectory();
  } catch {
    return false;
  }
}

function replaceInFile(filePath, find, replacement) {
  let content = fs.readFileSync(filePath).toString();
  content = content.replace(find, replacement);
  fs.writeFileSync(filePath, content);
}

function getTempFile(prefix, suffix) {