Getting started

The following assumes you have installed the SDRVM binary (sdrvm) in your path (by default, sdrvm binary is installed in /opt/vmbase).

With your favorite text editor, copy and paste the following :

print('Hello World!');

Save to hello.js. now, from the Linux prompt type :

sdrvm -f hello.js

You should read :

---------------------------------------------------------------------------------
 SDRVM Version v1.0 - Build : 20220207
 (c) SDR-Technologies SAS - www.sdrtechnologies.fr
---------------------------------------------------------------------------------
Creating Radio Device factory
Disk free space : 44.1 % 
This license was generated for : F4GKR
License file successfully loaded.
Registering SDRNode Rack LCD Panel simulator
VM starting...

Loading boot task from file : [hello.js]

(boot:0)> Hello World!

No running task, ending.

Syntax errors

The virtual machine tries to estimate where the error are and provides information on the location of the errors.

For example the following code will not work (function prnt does not exist):

prnt('Hello World!');
Task 0 [boot] unexpected end: ReferenceError: identifier 'prnt' undefined

The constant string 'Hello world' is not correctly closed :

prnt('Hello World!);
Task 0 [boot] unexpected end: SyntaxError: unterminated string (line 1)

Functions

Javascript allows you to declare functions. A basic example is provided below :

function sayHello( name ) {
    print( 'Hello ' + name );
}

sayHello('world');

download example

You should read :

(boot:0)> Hello world

Javascript structures

In the following example we define a 'person' structure where we store different types of information.

var person = {
    'name': 'demo',
    'age' : 45,
    'gender' : 'male'
} ;

print('Name =' + person.name );
print('Age  = ' + person.age + ' years old.');

download example

You should read :

(boot:0)> Name =demo
(boot:0)> Age  = 45 years old.

Of course, this structure can be passed as an argument to a function:

var person = {
    'name': 'demo',
    'age' : 45,
    'gender' : 'male'
} ;

function describe( person_structure ) {
    print('Name =' + person_structure.name );
    print('Age  = ' + person_structure.age + ' years old.');
}

describe(person);

download example

You should read :

(boot:0)> Name =demo
(boot:0)> Age  = 45 years old.

Object Oriented Javascript

Assuming we want to handle multiple persons with the same structure, another way to handle this is to write an 'Object'.

  function Person( person_name, person_age, person_gender ) {
    this.name = person_name;
    this.age  = person_age ;
    this.gender = person_gender ;
  }

  Person.prototype = {
    describe: function() {    
       print('Name = ' + this.name );
       print('Age  = ' + this.age + ' years old.');
    }

  };


var team = new Array();
team.push( new Person('paul', 45, 'male') ) ;
team.push( new Person('marie', 30, 'female') );

print('The team has ' + team.length + ' persons.');
for( var i=0 ; i < team.length ; i++ ) {
     var person = team[i] ;
     person.describe();
}

download example

You should read :

(boot:0)> The team has 2 persons.
(boot:0)> Name = paul
(boot:0)> Age  = 45 years old.
(boot:0)> Name = marie
(boot:0)> Age  = 30 years old.

Methods can be added, for example :

  function Person( person_name, person_age, person_gender ) {
    this.name = person_name;
    this.age  = person_age ;
    this.gender = person_gender ;
  }

  Person.prototype = {
    describe: function() {    
       print('Name = ' + this.name );
       print('Age  = ' + this.age + ' years old.');
    },

    updateAge: function( increment ) {
        this.age = this.age + increment ;
    }
  };


var team = new Array();
team.push( new Person('paul', 45, 'male') ) ;
team.push( new Person('marie', 30, 'female') );


print('The team has ' + team.length + ' persons.');
for( var i=0 ; i < team.length ; i++ ) {
     var person = team[i] ;
     person.updateAge(10);
     person.describe();
}

download example

You should read :

(boot:0)> The team has 2 persons.
(boot:0)> Name = paul
(boot:0)> Age  = 55 years old.
(boot:0)> Name = marie
(boot:0)> Age  = 40 years old.

Using libraries

It is sometimes useful to move the frequently used code in a different file. We will create in the following example a first file , named personlib.js with the following content :

  function Person( person_name, person_age, person_gender ) {
    this.name = person_name;
    this.age  = person_age ;
    this.gender = person_gender ;
  }

  Person.prototype = {
    describe: function() {    
       print('Name = ' + this.name );
       print('Age  = ' + this.age + ' years old.');
    },

    updateAge: function( increment ) {
        this.age = this.age + increment ;
    }
  };

We can now have our main code using the Person object as follows:

include('personlib.js');

var team = new Array();
team.push( new Person('paul', 45, 'male') ) ;
team.push( new Person('marie', 30, 'female') );


print('The team has ' + team.length + ' persons.');
for( var i=0 ; i < team.length ; i++ ) {
     var person = team[i] ;
     person.updateAge(10);
     person.describe();
}

The virtual machine will include (copy and paste) the code provided in 'personlib.js' in the main code before running.

Note

The library can be downloaded from a remote web server. You can then use :

include('http://where_my_lib_is.com/path/personlib.js');

Multitasking

The SDR Virtual Machine is designed to run multiple tasks and offers different techniques to have data communication between the different tasks.

We will start with a very basic example, having two tasks adding numbers simultaneously. They share the same code but will start from a different number.

Task code, saved in task.js (download) :

// get the starting int 
var start = parseInt( argv(0)); 
// get the last 
var stop  = parseInt( argv(1));

print('Counting from ' + start + ' to ' + stop );

var total = 0 ;
for( var i=start ; i < stop ; i++ ) {
    total = total + i ;
}
print('Sum from ' + start + ' to ... ' + stop+ ' is ' + total );

Some comments:

  • Function argv( n ) returns the n-th argument passed in the createTask instruction
  • Returned value is a string, we use parseInt() to get an int

Main code, saved in maintask.js (download) :

 var t1 = createTask('task.js',  0,100);
 var t2 = createTask('task.js',100,200);

Some comments :

  • The calls to createTask returns immediatly,
  • The main task ends after the second call, but the SDRVM waits for all the running tasks before shutting down.

The output is :

(task:1)> Counting from 0 to 100
(task:1)> Sum from 0 to ... 100 is 4950
(task:2)> Counting from 100 to 200
(task:2)> Sum from 100 to ... 200 is 14950

Now let's assume we want the first task to end before we start the second one. We can do this by adding a simple waitTask() :

var t1 = createTask('task.js',0,100);
print('Wait for end of task 1');
waitTask(t1);
print('Task 1 finished');
var t2 = createTask('task.js',100,200);
waitTask(t2);
print('Task 2 finished');

The output is now :

(task:1)> Counting from 0 to 100
(task:1)> Sum from 0 to ... 100 is 4950
(boot:0)> Wait for end of task 1
(boot:0)> Task 1 finished
(task:2)> Counting from 100 to 200
(task:2)> Sum from 100 to ... 200 is 14950
(boot:0)> Task 2 finished

Note

You can rename the tasks so the output is more user-friendly. Use the rename() function as follows in taks.js :

// get the starting int 
var start = parseInt( argv(0)); 
// get the last 
var stop  = parseInt( argv(1));
rename('count_'+start+'_'+stop);

print('Counting from ' + start + ' to ' + stop );
...

The output is now :

(count_0_100:1)> Counting from 0 to 100
(count_0_100:1)> Sum from 0 to ... 100 is 4950
(boot:0)> Wait for end of task 1
(boot:0)> Task 1 finished
(count_100_200:2)> Counting from 100 to 200
(count_100_200:2)> Sum from 100 to ... 200 is 14950
(boot:0)> Task 2 finished

CPU Time

you can estimate the running time for each task by changing the way you call the SDRVM application. Add the -t flag :

sdrvm -t -f maintask.js

You will get timing informations :

(count_0_100:1)> Counting from 0 to 100
(count_0_100:1)> Sum from 0 to ... 100 is 4950
        Task count_0_100 [:1] was running for 1 ms.
(boot:0)> Wait for end of task 1
(boot:0)> Task 1 finished
(count_100_200:2)> Counting from 100 to 200
(count_100_200:2)> Sum from 100 to ... 200 is 14950
        Task count_100_200 [:2] was running for less than 1 ms.
(boot:0)> Task 2 finished
        Task boot [:0] was running for 4 ms.
Last update: February 13, 2022