bun icon indicating copy to clipboard operation
bun copied to clipboard

Bun is unable to run llvm-bindings library

Open rishavs opened this issue 3 years ago • 1 comments
trafficstars

Version

0.2.0

Platform

wsl ubuntu

What steps will reproduce the bug?

create a new bun project add llvm-bindings to bun run the same program

import llvm from 'llvm-bindings';

function main(): void {
    const context = new llvm.LLVMContext();
    const module = new llvm.Module('demo', context);
    const builder = new llvm.IRBuilder(context);

    const returnType = builder.getInt32Ty();
    const paramTypes = [builder.getInt32Ty(), builder.getInt32Ty()];
    const functionType = llvm.FunctionType.get(returnType, paramTypes, false);
    const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'add', module);

    const entryBB = llvm.BasicBlock.Create(context, 'entry', func);
    builder.SetInsertPoint(entryBB);
    const a = func.getArg(0);
    const b = func.getArg(1);
    const result = builder.CreateAdd(a, b);
    builder.CreateRet(result);

    if (llvm.verifyFunction(func)) {
        console.error('Verifying function failed');
        return;
    }
    if (llvm.verifyModule(module)) {
        console.error('Verifying module failed');
        return;
    }
    console.log(module.print());
}

main();

How often does it reproduce? Is there a required condition?

Reproduces consistently

What is the expected behavior?

llvm-bindings runs correctly. the sample programs runs and prints the generated IR.

What do you see instead?

rishav@Rishav-SB3:~/testapp$ bun run testapp.ts 
164 |       }
165 |     }
166 |   };
167 | 
168 |   // run the 'prepareStackTrace' function above
169 |   Error.captureStackTrace(dummy);
      ^
TypeError: Error.captureStackTrace is not a function. (In 'Error.captureStackTrace(dummy)', 'Error.captureStackTrace' is undefined)
      at getFileName (/home/rishav/testapp/node_modules/bindings/bindings.js:169:2)
      at bindings (/home/rishav/testapp/node_modules/bindings/bindings.js:82:39)
      at /home/rishav/testapp/node_modules/llvm-bindings/index.js:1:25
      at bun:wrap:1:16360
      at /home/rishav/testapp/testapp.ts:1:0

Additional information

No response

rishavs avatar Oct 17 '22 06:10 rishavs

After some searching through the existing issues, I think my issue is blocked on this https://github.com/oven-sh/bun/issues/258

rishavs avatar Oct 17 '22 09:10 rishavs

with https://github.com/oven-sh/bun/issues/258#issuecomment-1309317337 fixed, I will check this in the new bun release and close it.

rishavs avatar Nov 10 '22 09:11 rishavs

You can try it now via

bun upgrade --canary

Jarred-Sumner avatar Nov 10 '22 09:11 Jarred-Sumner

that error is no longer coming. Got a new one now; bun: symbol lookup error: /home/rishav/bunllvm/node_modules/llvm-bindings/build/Debug/llvm-bindings.node: undefined symbol: napi_create_external

not sure if this has anything to do with bun.

rishavs avatar Nov 10 '22 15:11 rishavs

We need to implement napi_create_external

Jarred-Sumner avatar Nov 10 '22 21:11 Jarred-Sumner

Implemented napi_create_external in https://github.com/oven-sh/bun/commit/dddbce8a41aa266af106b19e583c152126900730

Please try again after the canary builds

Jarred-Sumner avatar Nov 10 '22 23:11 Jarred-Sumner

Thank you @Jarred-Sumner ! There is a new symbol missing error now;

bun: symbol lookup error: /home/rishav/bunllvm/node_modules/llvm-bindings/build/Debug/llvm-bindings.node: undefined symbol: napi_open_escapable_handle_scope

is there a way get a list of all symbols in the .node file in one go?

rishavs avatar Nov 12 '22 05:11 rishavs

Can you try again? That one is stubbed out.

Jarred-Sumner avatar Jun 04 '23 21:06 Jarred-Sumner

You'll need to add llvm-bindings to your package.json, like so:

{
  "dependencies": {
    "llvm-bindings": "^0.4.2"
  },
  "trustedDependencies": [
    "llvm-bindings"
  ]
}

Then run bun install, and the code should work. If you're still experiencing an error, let us know and we'll re-open this issue.

Electroid avatar Feb 05 '24 17:02 Electroid