Downloading... Download Failed Please Login Download Completed

The Smojo Virtual Machine

Official

This project aims to help anyone run Smojo programs on their own laptops, phones or servers. We have virtual machines in Java (main), JavaScript and C++.

What is this project about?

This project enables you to:

  • write and publish programs your Smojo programs,
  • run that program anywhere using the appropriate Smojo Virtual Machine (SmojoVM).

So, it's a 2-step process: You write and publish the program first, then run it using the right SmojoVM. There are 2 ways to run your Smojo program:

  1. Use the SmojoVM/Java version to run it on any computer (with Java v11+).
  2. Run that code on a web browser using SmojoVM/Javascript.

There is also an experimental SmojoVM/C++ only for POSIX-like OSes (MacOSX,Linux,BSD).

Can I use SmojoVM without a Smojo account?

Yes you can! First, follow the instructions below to download and compile the relevant VM. Second, you need to know the username/program of any published Smojo program you wish to run. Then simply use the relevant SmojoVM run option to run this program. It's that easy!

Supported Platforms & Languages

SmojoVM is implemented in 3 languages: Java, JavaScript and C++. The Java and JavaScript versions should run on all major platforms, but the C++ one has only been tested on MacOSX, Linux, FreeBSD and NetBSD.

  • The Java version is the most feature complete. Use it for most applications. It requires Java 11+.
  • The JavaScript version is designed for use on browsers. Usage on node.js is possible but not tested.
  • The C++ version is experimental, only for POSIX-compliant systems.

How to Publish your Smojo Application

The first step is to write your Smojo application then publish it.

The publishing mechanism uses a main.m file with a publish word:

copy code
\ === main.m === 
smojo/publish 
arnold/vm 
\ .strict 
.permissive 
 
vm{ 
    include hello.m 
}vm 
 
: debug .debug ; 
 
: publish 
    json: json 
    publish: hello.json 
    title: Hello World 
    description: Hello World programme 
; 

You should copy this code as a template into your own main.m. The orange code needs editing of course. The sample above is to publish a file called hello.m. Note that the publish: word needs a file extension .json

You should also have the usual ui-words.json file also:

copy code
[ 
  { "label" : "Debug"       , "wordname" : "debug"    }, 
  { "label" : "Publish"       , "wordname" : "publish" } 
]

Saved to the same folder as main.m

Key points to note:

  • In the example main.m above, the included file is hello.m, but you should use your own.
  • Your program files should be included between vm{ and }vm.
  • Your publish: name must end with .json
  • You should also edit the title and description of your program.

Using the Java Version of SmojoVM on MacOSX/Linux/BSD

The Java version

  1. Runs on all platforms supporting Java™ Runtime.
  2. Download script available for MacOSX / Linux / BSD.
  3. You must have Curl installed to perform the download.
  4. You should have Java 11+ installed.

Step 1: In the first step (MacOSX™/Linux/BSD), you should download the installation bash script:

copy code
curl -k https://app.smojo.org/arnold/smojo.sh > smojo.sh 
chmod +x smojo.sh 
./smojo.sh -c

These commands download the installation script for Java into a file called smojo.sh and makes it executable. The -c option downloads and compiles the latest version of SmojoVM/Java. This results in a .jar file that is saved in your system's temporary in-memory folder.

Step 2: After running the compilation, you can now run published Smojo programs using the same installer script, but using the command -r:

copy code
./smojo.sh -r username/program

For example:

copy code
./smojo.sh -r arnold/hello

SmojoVM/Java on Windows and other OSes

Instead of using the bash script method (as described in the preceding section) you may also download, compile and run SmojoVM/Java manually.

Ensure that you have the Java Development Kit for your platform (ver 11+) installed on your computer.

Step 1: Download the latest copy of SmojoVM/Java using the following link:

copy code
https://app.smojo.org/arnold/vm.java

Step 2: Rename the downloaded file as SmojoVM.java. The name is case sensitive.

Step 3: Create a folder called smojo in the same place you have SmojoVM.java

Step 4: Run this command in the parent folder of smojo:

copy code
javac -d ./smojo SmojoVM.java 
jar cfe smojo.jar SmojoVM -C ./smojo .

Step 5: These steps should yield a jar file called smojo.jar To run a published app (eg, arnold/hello) on this VM, you should use the command:

copy code
java -jar smojo.jar  https://app.smojo.org/arnold/hello.json

To run other applications, simply replace with username/app of the published app.

Using the JavaScript version of SmojoVM

The JavaScript version doesn't have to be compiled. You only have to import it in your webpage's <head> section:

copy code
<script src="https://app.smojo.org/arnold/vm.js"></script>

This code exposes a JavaScript class called SmojoVM. The VM will need a way to input a published program and a way to output the result.

  • You set the output by using setPrinter
  • You run a published smojo program by calling smojoMain

Here's an example script, assuming an output div called output, running the program arnold/snake:

copy code
<script> 
    const smojo = new SmojoVM(); 
 
    smojo.setPrinter(function(s){ 
        var x = document.getElementById("output"); 
            x.innerText = x.innerText + s; 
    }); 
 
    function runApp(url){ 
        document.getElementById("output").innerText = ""; 
        smojo.smojoMain(url); 
    } 
 
    runApp("https://app.smojo.org/arnold/snake.json"); 
 
</script>

There is a sample test page for SmojoVM/JavaScript here:

copy code
https://app.smojo.org/arnold/test-vm.html

Clicking on the blue links will run the associated program. The source code for these programs are in the test folder in the download.