AlexAltea / capstone.js

Capstone disassembler framework for JavaScript
https://alexaltea.github.io/capstone.js/
BSD 3-Clause "New" or "Revised" License
172 stars 28 forks source link

"MCapstone is not a function" error after manual build #17

Open eternaleclipse opened 2 years ago

eternaleclipse commented 2 years ago

After manually building the library with Grunt and Emscripten, using both capstone.min.js and capstone-x86.min.js, without adding any extra JS code:

capstone-x86_new.min.js:7 Uncaught TypeError: MCapstone is not a function
    at capstone-x86_new.min.js:7:13
(anonymous) @ capstone-x86_new.min.js:7
capstone_new.min.js:535 Uncaught TypeError: MCapstone._malloc is not a function
    at new Capstone (capstone_new.min.js:535:37)
    at main.js:7:18
AlexAltea commented 2 years ago

Likely you are using an incompatible (newer?) Emscripten version.

Emscripten has changed a lot over the years which means quite some maintenance burden for me, so I'm not going to update the build scripts unless we have a significant reason to do so (PRs are welcome though!).

Try to use whichever Emcripten version was latest as of the date of the latest release: 2027-02-01

eternaleclipse commented 2 years ago

Using Python 3.8.10 and latest Emscripten (3.1.9).

Looks like there are no changes needed. the output JS is good, only usage is a bit different.

There were probably some changes to Emscripten usage for MODULARIZE=1 option - module usage is documented here.

cs.MCapstone.then(instance => {
    MCapstone = instance;

    // Continue normally from here
    const capstone = new cs.Capstone(cs.ARCH_X86, cs.MODE_64);
    const code = new Uint8Array([0x55, 0x31, 0xD2, 0x89, 0xE5, 0x8B, 0x45, 0x08]);
    const instructions = capstone.disasm(code, 0);
    console.log(instructions);

})

This works 🙂 I suggest updating the example in the Readme accordingly.