create account

The SmartScript Virtual Machine is now available! by harpagon

View this thread on: hive.blogpeakd.comecency.com
· @harpagon ·
$64.76
The SmartScript Virtual Machine is now available!
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:

```js
/*
 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:

```js
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](https://stemgeeks.net)
๐Ÿ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 191 others
properties (23)
authorharpagon
permlinkthe-smartscript-virtual-machine-is-now-available
categoryhive-163521
json_metadata{"tags":["smart-contracts","smartscript","stem"],"links":["https://github.com/harpagon210/smartscript-vm"],"app":"stemgeeks/0.1","format":"markdown","canonical_url":"https://stemgeeks.net/@harpagon/the-smartscript-virtual-machine-is-now-available"}
created2020-12-01 03:08:48
last_update2020-12-01 03:08:48
depth0
children9
last_payout2020-12-08 03:08:48
cashout_time1969-12-31 23:59:59
total_payout_value33.104 HBD
curator_payout_value31.655 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,856
author_reputation20,287,642,226,146
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,758,111
net_rshares261,745,996,037,261
author_curate_reward""
vote details (255)
@ali-h ·
$0.03
Looking forward to test this, sounds very interesting.
๐Ÿ‘  ,
properties (23)
authorali-h
permlinkre-harpagon-qknbdb
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2020.11.4"}
created2020-12-01 05:31:57
last_update2020-12-01 05:31:57
depth1
children0
last_payout2020-12-08 05:31:57
cashout_time1969-12-31 23:59:59
total_payout_value0.016 HBD
curator_payout_value0.017 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length54
author_reputation3,042,798,885,185
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,759,128
net_rshares268,614,096,592
author_curate_reward""
vote details (2)
@asimo ·
$tangent
properties (22)
authorasimo
permlinkre-harpagon-qkpyuc
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2020.11.4"}
created2020-12-02 15:54:18
last_update2020-12-02 15:54:18
depth1
children1
last_payout2020-12-09 15:54:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8
author_reputation136,357,321,252
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,781,109
net_rshares0
@tan.bot ·
<center>
<sup>Congratulations, @asimo You Successfully Trended The Post Shared By @harpagon.</sup>
<sup>You Utilized <b>2/3</b> Daily Summon Bot Calls.</sup>
---
<sup>[TAN Current Market Price](https://hive-engine.com/?p=market&t=TAN) : <b>0.705 HIVE</b></sup>
</center>
properties (22)
authortan.bot
permlink20201202t155428968z
categoryhive-163521
json_metadata{"tags":["tan","tangent"],"app":"tan-bot/1.1","format":"markdown"}
created2020-12-02 15:54:30
last_update2020-12-02 15:54:30
depth2
children0
last_payout2020-12-09 15:54:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length270
author_reputation44,172,891,181
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,781,114
net_rshares0
@davidmuhammad ·
Keep spirit my bro
๐Ÿ‘  
properties (23)
authordavidmuhammad
permlinkre-harpagon-2020122t212942493z
categoryhive-163521
json_metadata{"tags":["ecency"],"app":"ecency/3.0.9-mobile","format":"markdown+html"}
created2020-12-02 14:29:42
last_update2020-12-02 14:29:42
depth1
children0
last_payout2020-12-09 14:29:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length18
author_reputation6,813,184,461,391
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,779,796
net_rshares7,727,330
author_curate_reward""
vote details (1)
@french-tech ·
$0.03
Bravo! beau boulot ๐Ÿ‘

Moi aussi je suis dans ma pรฉriode full TypeScript ๐Ÿ˜

<hr>
<sub>

[@mintrawa: Witness FR - Gรฉnรฉration X - Geek ๐Ÿค“ Gamer ๐ŸŽฎ voyageur โ›ฉ๏ธ](/@french-tech/witness-fr-generation-x-geek-gamer-voyageur)
Ne loupez pas le **Hive Power UP Day**! plus d'info [ici](/hivebuzz/@hivebuzz-fr/pud-202011) 

</sub>
๐Ÿ‘  ,
properties (23)
authorfrench-tech
permlinkre-harpagon-2020121t11854128z
categoryhive-163521
json_metadata{"tags":["smart-contracts","smartscript","stem"],"app":"ecency/3.0.10-vision","format":"markdown+html"}
created2020-12-01 04:08:54
last_update2020-12-01 04:08:54
depth1
children0
last_payout2020-12-08 04:08:54
cashout_time1969-12-31 23:59:59
total_payout_value0.016 HBD
curator_payout_value0.017 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length315
author_reputation10,920,316,584,102
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,758,556
net_rshares263,248,922,426
author_curate_reward""
vote details (2)
@hivebuzz ·
Congratulations @harpagon! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@harpagon/posts.png?202012010325"></td><td>You published more than 50 posts. Your next target is to reach 60 posts.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@harpagon) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>

properties (22)
authorhivebuzz
permlinkhivebuzz-notify-harpagon-20201201t034005000z
categoryhive-163521
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2020-12-01 03:40:03
last_update2020-12-01 03:40:03
depth1
children0
last_payout2020-12-08 03:40:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length617
author_reputation369,203,081,481,287
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,758,324
net_rshares0
@julianhorack ·
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.
๐Ÿ‘  
properties (23)
authorjulianhorack
permlinkre-harpagon-qkp87v
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2020.11.4"}
created2020-12-02 06:20:09
last_update2020-12-02 06:20:09
depth1
children0
last_payout2020-12-09 06:20:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length201
author_reputation501,236,766,078,050
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,774,672
net_rshares7,842,186
author_curate_reward""
vote details (1)
@tan.app ·
<center>
<sup>Congratulations @harpagon, You Earned <b>0.597 TAN</b> & Curators Made <b>0.417 TAN</b>.</sup>

<img src="https://files.peakd.com/file/peakd-hive/theguruasia/DWYqznFx-TANToken70.png" alt="tangent.token">

---
<sup>Join [CORE / VAULT Token Discord Channel](https://discord.gg/XYMrvyr) or Trade [TANGENT Token](https://hive-engine.com/?p=market&t=TAN)</sup>
<sup>[TAN Current Market Price](https://hive-engine.com/?p=market&t=TAN) : <b>0.725 HIVE</b></sup>
</center>
properties (22)
authortan.app
permlink20201205t021833617z
categoryhive-163521
json_metadata{"tags":["tan","tangent"],"app":"tan-bot/1.1","format":"markdown"}
created2020-12-05 02:18:33
last_update2020-12-05 02:18:33
depth1
children0
last_payout2020-12-12 02:18:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length486
author_reputation164,160,871,793
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,816,445
net_rshares0
@vsc.network ·
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 
properties (22)
authorvsc.network
permlinkre-harpagon-rx1k0k
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2023.6.5"}
created2023-06-30 00:58:00
last_update2023-06-30 00:58:00
depth1
children0
last_payout2023-07-07 00:58:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length170
author_reputation192,969,127,486,285
root_title"The SmartScript Virtual Machine is now available!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id124,897,346
net_rshares0