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

Importing functions with Multiple ways to call it #77

Closed zap8600 closed 9 months ago

zap8600 commented 9 months ago

I'm trying to implement console.log in my standard library. However, there are different ways I can call it. How can I implement this?

coder-mike commented 9 months ago

If you're talking about calling it at compile time and runtime, you can actually do this:

// guest.js

vmExport(1, run);
console.log = vmImport(1, console.log);

console.log('console.log still works at compile time!')

function run() {
  console.log('console.log works at runtime too!')
}

The second argument to vmImport is like a "default" value that is used as a fallback if the current host doesn't have that numbered import. The default compile-time host doesn't have any numbered imports available so it will use the default, which here is the compile-time console.log function.

Is that what you were asking?

PS Here's an example host running the above guest:

// host.mjs

import Microvium from '@microvium/runtime';

const snapshot = [0x08,0x1c,0x00,0x00,0x88,0x00,0x8d,0x49,0x03,0x00,0x00,0x00,0x1c,0x00,0x1e,0x00,0x22,0x00,0x22,0x00,0x30,0x00,0x34,0x00,0x7a,0x00,0x7e,0x00,0x01,0x00,0x01,0x00,0x69,0x00,0x7d,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x45,0x00,0x39,0x00,0x00,0x00,0x04,0x40,0x6c,0x6f,0x67,0x00,0x00,0x00,0x02,0x60,0x00,0x00,0x22,0x40,0x63,0x6f,0x6e,0x73,0x6f,0x6c,0x65,0x2e,0x6c,0x6f,0x67,0x20,0x77,0x6f,0x72,0x6b,0x73,0x20,0x61,0x74,0x20,0x72,0x75,0x6e,0x74,0x69,0x6d,0x65,0x20,0x74,0x6f,0x6f,0x21,0x00,0x04,0x50,0x01,0x89,0x00,0x00,0x10,0x88,0x39,0x00,0x6b,0xa1,0x88,0x45,0x00,0x78,0x82,0x01,0x60,0x00,0x02,0x00,0x01,0x00,0x08,0xc0,0x05,0x00,0x05,0x00,0x39,0x00,0x41,0x00];

const imports = { 1: console.log };
const vm = await Microvium.restore(snapshot, imports);
const { 1: run } = vm.exports;

run();
$ microvium guest.js --output-bytes
console.log still works at compile time!
Output generated: guest.mvm-bc
136 bytes
{0x08,0x1c...,0x41,0x00}
$ node host.mjs
console.log works at runtime too!
zap8600 commented 9 months ago

I mean with arguments. For example, I can call it with multiple arguments or not, so the example for console.log provided in the native section of the getting started guide won't work due to assert and the lack of code to handle this. I'm wondering how I can handle a dynamic amount of arguments.

zap8600 commented 9 months ago

Nevermind. I think I have it figured out.

coder-mike commented 9 months ago

Ok, I'm glad you figured it out. Do you want to post your solution here so others can see it if they come across a similar issue?

zap8600 commented 9 months ago

Ok, I'm glad you figured it out. Do you want to post your solution here so others can see it if they come across a similar issue?

What I did is remove assert, go through each argument, get the type, and print it in the correct order.