coder-mike / microvium

A compact, embeddable scripting engine for applications and microcontrollers for executing programs written in a subset of the JavaScript language.
MIT License
569 stars 25 forks source link

Getting started, but MVM_E_INVALID_BYTECODE #36

Closed Fordi closed 1 year ago

Fordi commented 1 year ago

Starting with what I believe to be the goal of Getting Started, I get no output, and an error code of 18 (MVM_E_INVALID_BYTECODE).

Code attached, running:

$ make && ./main; echo "Exit code: $?"
gcc -c main.c -o main.o
gcc -c microvium.c -o microvium.o
gcc main.o microvium.o -lm -o main
microvium script.mvm.js
Output generated: script.mvm-bc
142 bytes
Exit code: 18

microvium-issue-sample.zip

microvium.c, microvium.h, and microvium_port.h are pulled from the latest commit off main as of this writing

main.c and script.mvm.js are copied verbatim from the Getting Started doc.

Makefile's my own.

The same test works with the current release code (v0.0.19)

Fordi commented 1 year ago

Just a version mismatch; v0.0.20 doesn't like mvm-bc files created with v0.0.19. Makes sense. Cloning main, using its dist-c files, and using the repo's CLI script in-place instead of the global microvium from npm fixed the problem:

[Ninja edit: Yay, try/catch works!]

// script.mvm.js
console.log = vmImport(1);
function sayHello() {
  try {
    console.log('Hello, world!');
    throw { message: 'An error' };
  } catch (e) {
    console.log(`Caught: ${e.message}`);
  }
}
vmExport(1234, sayHello);
$ make clean; make && ./main; echo "Exit code: $?"
rm main *.o *.mvm-bc
gcc -c main.c -o main.o
gcc -c microvium.c -o microvium.o
gcc main.o microvium.o -lm -o main
node ../microvium/dist/cli.js script.mvm.js
Output generated: script.mvm-bc
218 bytes
Hello, world!
Caught: An error
Exit code: 0