You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think the documentation needs more thorough explanation. I'm working using a google colab https://colab.research.google.com, to create a test environment to build this project and it is giving me trouble "out of the box". I can share that colab with you if requested(but not publicly to protect my account),
Here's what I'm doing so far at a basic level. Following the readme from this repo. This is assuming we are limited to working with just NodeJS as the base dependencies.
The following source code sections all belong to the same script.mvm.js file. It is split into sections for reading.
Compile some stringified Javascript into a snapshot
//script.mvm.jsconstMicrovium=require('microvium');constSnapshot=Microvium.Snapshot;constfs=require("fs");//I forget where I got this example, but disclosure, this isn't my creation. Found it in passing.console.log("\nCompile Test");functioncompile(sourceText){// Create an empty VMconstvm=Microvium.create();// Add Microvium's default globals such as `vmExport` and `vmImport`Microvium.addDefaultGlobals(vm);try{// Evaluate the scriptvm.evaluateModule({ sourceText,debugFilename: 'firmware.js'});// Create the snapshotconstsnapshot=vm.createSnapshot();// If you want the disassembly for debug purposesconst{ disassembly }=Microvium.decodeSnapshot(snapshot);//console.log("disassembly: ",disassembly);// The `snapshot.data` is a Node Buffer (Uint8Array) containing the snapshot bytesreturn{"result": snapshot.data,"error": null}}catch(e){return{"result": null,"error": e.message}}}varbin=compile("var i = 0; i+= 100; i /= 2; i; var s = \"Some data\"; console.log(s+i); console = console;");console.log("compile: ",Array.from(bin.result).map(v=>v.toString(16)).join(","));console.log("ASCII: "+Array.from(bin.result).map(v=>{letc=String.fromCharCode(v);if(v==0){return".";}if(c.match(/[^A-z\d\w\(\)\{\}\.`~\*@\+=\/\:\-&,;]/)){return".";}returnc;}).join(""));
Expected output:
Compile Test
Some data50
compile: 8,1c,0,0,2e,0,50,10,3,0,0,0,1c,0,1c,0,1c,0,1c,0,2a,0,2a,0,2a,0,2e,0,2d,0,1,0,1,0,1,0,1,0,1,0,1,0,19,0,1,0
ASCII: ......P.............*.*.*...-.................
Test Two, Evaluating Javascript Strings
console.log("\nEval Module Test");varvm_eval=Microvium.create();// Create a "print" function in the global scope that refers to this lambda function in the hostvm_eval.globalThis.print=(s)=>{console.log(s);}// Run some module source codevm_eval.evaluateModule({sourceText: 'print("Something Evaled!");'});//console.log(JSON.stringify(vm_eval.vm));
Expected output:
Eval Module Test
Something Evaled!
Test Three, Taking a Snapshot, and Loading a Snapshot
console.log("\nSnapshot Test");constsnapshotFilename='script.mvm-bc';varvms=Microvium.create();//fs.writeFileSync("microvium.txt",JSON.stringify([Microvium,Microvium.Microvium,Snapshot,vms,vms.vm].map(v=>Object.keys(v)),null,"\t"));vms.globalThis.print=console.warn;vms.globalThis.console={warn: console.warn};console.warn=vms.vmImport(1);console.log("vms.vmImport console.warn: ",console.warn);functionprint(s){console.warn(s);}functionsayHello(){print('Hello, World!');}vms.vmExport(1,sayHello);//Attempted to abstract once morevms.vmExport(2,print);console.log("vms.globalThis: ",vms.globalThis);//console.log( "vms.createSnapshot:", vms.createSnapshot.toString() );varsnapshot=vms.createSnapshot();console.log("Snapshot: ",snapshot);fs.writeFileSync(snapshotFilename,snapshot.data);fs.writeFileSync(snapshotFilename+".json",JSON.stringify(snapshot.reconstructionInfo));console.log("BIN["+snapshot.data.length+"]: ",Array.from(snapshot.data).map(v=>v.toString()).join(","));console.log("ASCII: "+Array.from(snapshot.data).map(v=>{letc=String.fromCharCode(v);if(v==0){return".";}if(c.match(/[^A-z\d\w\(\)\{\}\.`~\*@\+=\/\:\-&,;]/)){return".";}returnc;}).join(""));//vms.globalThis.print = s => console.log(s);//vms.evaluateModule({ sourceText: 'print("Hello, World!");' });
This is where things begin to break down. No matter what I try, I cannot get the snapshot feature to function properly. There's no documentation on that portion of the library, so I've gone to looking at the source and adding things I find there. None of the custom options work.
Load the Snapshot output from above. (Note the "Not available on this host (detached)" in the hexdump.)
console.log("\nSnapshot Load Test");// Load the snapshot from fileconstsnapshot2=Snapshot.fromFileSync(snapshotFilename);snapshot2.reconstructionInfo=JSON.parse(fs.readFileSync(snapshotFilename+".json"));console.log("snapshot2: ",snapshot2);// Restore the virtual machine from the snapshotvarvm2=Microvium.restore(snapshot2,{[1]: console.log});// Locate the function with ID 1234. This is the `sayHello` function that the script exportedconstsayHello2=vm2.resolveExport(1);// Call the `sayHello` function in the scriptsayHello2();// "Hello, World!"
I would like to see an update made to the documentation showing more about the snapshotting and the logic behind that functionalities byte codes. Preferrably not leaving out any source code to reduce user error. If you feel it would fit that is.. At the moment it leaves a lot of questions open for the user, and many pitfalls without any real explanation to the classes, objects, and methods. That said, this is an amazing project and I am eager to see it grow. I'm working on a single-file minified source for compiling using a streamlined c# compiler written for use inside of this JS-Engine itself, it has some amazing potential.
Thank you for your amazing work.
The text was updated successfully, but these errors were encountered:
I think the documentation needs more thorough explanation. I'm working using a google colab https://colab.research.google.com, to create a test environment to build this project and it is giving me trouble "out of the box". I can share that colab with you if requested(but not publicly to protect my account),
Here's what I'm doing so far at a basic level. Following the readme from this repo. This is assuming we are limited to working with just NodeJS as the base dependencies.
admin$ npm i node-gyp microvium
== Microvium ^8.0.0admin$ node -v
== v16.20.2admin$ touch script.mvm.js
The following source code sections all belong to the same script.mvm.js file. It is split into sections for reading.
Compile some stringified Javascript into a snapshot
Expected output:
Test Two, Evaluating Javascript Strings
Expected output:
Test Three, Taking a Snapshot, and Loading a Snapshot
This is where things begin to break down. No matter what I try, I cannot get the snapshot feature to function properly. There's no documentation on that portion of the library, so I've gone to looking at the source and adding things I find there. None of the custom options work.
Output:
Load the Snapshot output from above. (Note the "Not available on this host (detached)" in the hexdump.)
Output:
I would like to see an update made to the documentation showing more about the snapshotting and the logic behind that functionalities byte codes. Preferrably not leaving out any source code to reduce user error. If you feel it would fit that is.. At the moment it leaves a lot of questions open for the user, and many pitfalls without any real explanation to the classes, objects, and methods. That said, this is an amazing project and I am eager to see it grow. I'm working on a single-file minified source for compiling using a streamlined c# compiler written for use inside of this JS-Engine itself, it has some amazing potential.
Thank you for your amazing work.
The text was updated successfully, but these errors were encountered: