The SmartScript Virtual Machine is now available!

avatar

I finally finished the very first version of my virtual machine. It took several months of on and off work to be able to finalize a working virtual machine. My goal was to create a language that is just a copy of JavaScript, or a subset of JavaScript I should say. For now, I've called it "SmartScript" because its sole purpose is to be the heart of the 2nd layer smart contracts platform I'm building for Hive. (Although, you could use it for any project that needs a sandboxed environment)

This whole project is open-source and you can find the source code of the virtual machine on github: https://github.com/harpagon210/smartscript-vm

I'm really having fun with this project because I've been learning new stuffs and I've been using new tools (TypeScript, Jest, code coverage)

SmartScript is easy to learn and easy to use. If you are already a JavaScript guru, then it'll take you little to no efforts to get up to speed with this language.

Here are some key elements of the SmartScript virtual machine and its language:

  • it's a Stack Based Virtual Machine
  • semi-colons ';' are required
  • you declare variables with "let" and "const" (no "var" available in SmartScript)
  • no decimals/floats (only big integers ("bigint") are available)
  • variables are initialized with the value "null" (no "undefined" available in SmartScript)
  • no "async" nor "await" (simply because SmartScript is a synchronous language)
  • no "console" (you can use "print" instead)
  • Maps/Objects and Arrays are a bit less permissive than JavaScript (trying to get a key that doesn't exist on a Map or an index that doesn't exist on an Array will throw an error (remember, there is no notion of "undefined" in SmartScript))
  • some grammar of the JavaScript language has not yet been implemented (and might never), but they are just syntactic sugar stuffs (for example, there are no "++" or "+=" available yet)
  • you can restrict code execution by applying a "gas" cost for each operation supported by the virtual machine

Here's a code snippet showing what you can do with the SmartScript language:

/*
 binary operations
*/
1 + 1;
1 - 1;
1 * 1;
1 / 1;
1 % 1;
/*
 bitwise operations
*/
1 & 1;
1 | 1;
1 ^ 1;
1 << 1;
1 >> 1;

/*
 negate operations
*/
-1;
!true;

// conditionals
if (1 == 1) {

} else if (1 > 1) {

} else if (1 < 1) {

} else {

}

// loops
while (false) {

}

do {

} while (false);

for (let i; i < 10; i = i + 1) {

}

// functions
function z(val) {
    return val;
}

// closures
function outer() {
    let a = 1;
    let b = 2;
    function middle() {
        let c = 3;
        let d = 4;
        function inner() {
            c = 3;
            b = 2;
            addResult(a + c + b + d);
        }

        inner();
    }

    middle();
}

// classes
class a {
    constructor() {
        this.aprop = 1;
    }

    testSuper() {

    }
}

class b extends a {
    constructor() {
        super();
        this.bprop = 2;
    }

    test() {
        super.testSuper();
    }
}

const cl = new b();
cl.aprop = "azerty";
cl.bprop = 3;
cl.test();

// Arrays

let t = [1, 2];
t[0] = t[1];
t.set(1, 9);
const g = t.get(1);
t.push(4);
const p = t.pop();
t.unshift(4);
const p = t.unshift();
t.clear();

// Maps

let t2 = { a: 1, b: 2 };
t2['a'] = t2['b'];
t2.set('a', 'test');
const g = t2.get('a');
const q = t2.has('a');
t2.delete('a');
t2.clear();

If you have a machine with nodejs, you can use the npm package to start using this virtual machine, the package is available at https://www.npmjs.com/package/smartscript-vm

You can also go play around with the SmartScript virtual machine on this codesandbox I created: https://codesandbox.io/s/brave-surf-i67jt?file=/src/index.js (although, you'll need an account to modify it).

And, here's a snippet to quickly bootstrap a project since there are no documentations on how to use the VM available at the moment:

const { VM, InterpretResult } = require("smartscript-vm");
  
  // set to "true" will display the execution trace
  process.env.DEBUG_TRACE_EXECUTION = "false";
  // set to "true" will display the code generated by the compiler
  process.env.DEBUG_PRINT_CODE = "false";
  
  async function run() {
    const vm = new VM();

  
    const source = 'print(1 + 1);';
  
    let result = VM.compile(source);
  
    if (result.result === InterpretResult.InterpretCompileError) {
      console.error(result.errors);
      process.exit(65);
    }
  
    result = await vm.interpret(result.func);
  
    if (result.result === InterpretResult.InterpretRuntimeError) {
      console.error(result.errors);
      process.exit(70);
    }
  
    process.exit();
  }
  
  run();

If you have some time to play with the VM, don't hesitate to send me feedbacks / bugs you find, etc... I'd really love to hear from the community.

Posted with STEMGeeks



0
0
0.000
9 comments
avatar

Congratulations @harpagon! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You published more than 50 posts. Your next target is to reach 60 posts.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

0
0
0.000
avatar

Looking forward to test this, sounds very interesting.

0
0
0.000
avatar

Brilliant, well done buddy. I don't know coding but love languages and can see that you have gone to a lot of trouble to design this, using your skill and education.

Best wishes and keep us informed.

0
0
0.000
avatar

This looked like a really cool project! Too bad it wasn't developed any further. We are working on a L2 smart contract system on HIVE, would loved to have made use of it

0
0
0.000