Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Oniguruma LoadWasm function, node fs buffer works fine but vscode.workspace.fs buffer does not work. #143471

Closed
leodevbro opened this issue Feb 19, 2022 · 2 comments
Assignees
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)

Comments

@leodevbro
Copy link

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.65.0-insider (user setup)
  • OS Version: Windows 10 Pro 21H2

I'm not sure that it is a bug, but it is a very strange behavior. I'm building a VSCode extension. I'm trying to load oniguruma wasm with vscode.workspace.fs buffer, it does not work, but node fs buffer works fine. I don't want to use node fs, because I want my extension to be a web extension (as well as desktop extension).

Steps to Reproduce (See full code down below, after the gif):

buffer-problem

The code:

import * as vscode from "vscode";
import * as path from "path";
import * as nodeFs from "fs";

import myOniguruma = require("vscode-oniguruma");
const myOnigurumaPath = require.resolve("vscode-oniguruma");

const myWasmPath = path.join(
    myOnigurumaPath.slice(0, myOnigurumaPath.length - 7),
    "onig.wasm",
);
const myWasmPathUri = vscode.Uri.file(myWasmPath);

vscode.workspace.fs.readFile(myWasmPathUri).then((myUint8Array) => {
    const vscodeFsBuffer = myUint8Array.buffer;
    console.log("vscodeFsBuffer: ", vscodeFsBuffer);

    const nodeFsBuffer = nodeFs.readFileSync(myWasmPath).buffer;
    console.log("nodeFsBuffer: ", nodeFsBuffer);

    myOniguruma.loadWASM(nodeFsBuffer) // this works fine --------------------------------
    // myOniguruma.loadWASM(vscodeFsBuffer) // this does not work ----------------------------
        .then((x) => {
            console.log("successful");
        })
        .catch((error) => {
            console.log("error: ", error);
        });
});

The error:

message:'WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 01 00 00 03 @+0'
stack:'CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 01 00 00 03 @+0'

rejected promise not handled within 1 second: Error: Must invoke loadWASM first.
@alexdima
Copy link
Member

alexdima commented Feb 25, 2022

@leodevbro Uint8Array is a view on an ArrayBuffer. That means that the Uint8Array can use only a part of its underlying .buffer, namely the part from .byteOffset and of length .byteLength:

interface ArrayBufferView {
    /**
     * The ArrayBuffer instance referenced by the array.
     */
    buffer: ArrayBufferLike;

    /**
     * The length in bytes of the array.
     */
    byteLength: number;

    /**
     * The offset in bytes of the array.
     */
    byteOffset: number;
}

So in this particular case this is a programming mistake. To correct it, you would need to create a new Buffer of the right size and copy the parts of the other Buffer into the new one.

To allow supporting 0-copy, I have pushed a change to vscode-oniguruma to accept an ArrayBufferView, so with the latest vscode-oniguruma, you will be able to use myOniguruma.loadWASM(myUint8Array)

@alexdima alexdima added the *question Issue represents a question, should be posted to StackOverflow (VS Code) label Feb 25, 2022
@leodevbro
Copy link
Author

@alexdima, huge thanks, it's working, really appreciate that you updated vscode-oniguruma package.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)
Projects
None yet
Development

No branches or pull requests

3 participants