#lincoln #web #assembly #interactive #shall #demostrate

lincoln_wasm

an interactive shall using web assembly to demostrate Lincoln

2 releases

0.1.1 Apr 5, 2019
0.1.0 Apr 4, 2019

#824 in WebAssembly

31 downloads per month

MIT license

205KB
2.5K SLoC

Rust 2K SLoC // 0.0% comments JavaScript 374 SLoC // 0.1% comments

What is this for?

This is a library supposed to be use as a WebAssembly module. An example was given in html folder.

Prerequsits

cargo install http-server

For the last piece (http server), you can also use the following:

Build

cd html
cd ..; wasm-pack build --target web; copy pkg\Lincoln_wasm.js html; copy pkg\Lincoln_wasm_bg.wasm html; cd html; http-server -p 8080

Try

image

Open http://127.0.0.1:8080 ,

Copy and paste the factorial program to the text area. Enter a number like 10 in the input box, hit "run". You will get the result and how many steps the intepretor need to run the program.

image

If you open the Javascript console, you can see the above piece, which is a representation of the compiled program.

Under the hook

Within index.html, some simple functions are defined to work as the "external" entry points, plus an async function run that loads the WebAssembly intepretor, and define an event handler for the "Run" button. It then uses the intepretor API to run the program given in the text area.

Easy Integration

The following Javascript code is all you need to use this in your web site:

 <script type="module">
    import { LincolnIntepretor, default as init } from './lincoln_wasm.js';
    async function run() {
        await init('./lincoln_wasm_bg.wasm');
        // work with the API
    }
    run();
</script>

LincolnIntepretor.new() creates an instance of the intepretor.

LincolnIntepretor.prototype.set_program allows to pass in a Javascript object as the running program.

intepretor.set_program(JSON.parse(program))

LincolnIntepretor.prototype.compile compiles the program. You will need to pass a set of functions and values. Functions can be just it; but other values have to have a name so you will write {name: "one", value: 1} instead.

intepretor.compile([copy_int,drop_int,try_minus,mul,{name: "one", value: 1}]);

LincolnIntepretor.prototype.run runs the program. You need to specify an entry point (by name), a variation (because a single entry point can have variations), and an array contains the initial values. The last value is a boolean to indicate whether you want to run it in step mode.

intepretor.run("fact", 0, [input], true);

LincolnIntepretor.prototype.step requires the intepretor to be in step mode. It does not take any arguments, and returns true if it remains in step mode (execution didn't terminate), or false if that is the end of execution.

Once at the end of execution, LincolnIntepretor.prototype.get_context gives you the final result.

intepretor.get_context();

Dependencies

~9–13MB
~243K SLoC