All Projects → richardanaya → wasm-script

richardanaya / wasm-script

Licence: MIT license
Compile WebAssembly in your HTML

Programming Languages

javascript
184084 projects - #8 most used programming language
rust
11053 projects
Makefile
30231 projects

Projects that are alternatives of or similar to wasm-script

Jlang
JLang: Ahead-of-time compilation of Java programs to LLVM
Stars: ✭ 186 (+564.29%)
Mutual labels:  compilers
BOHM1.1
Bologna Optimal Higher-Order Machine, Version 1.1
Stars: ✭ 45 (+60.71%)
Mutual labels:  compilers
AwesomeCompiler
The Big list of the github, open-source compilers.
Stars: ✭ 27 (-3.57%)
Mutual labels:  compilers
Writing A Compiler In Ruby
Code from my series on writing a Ruby compiler in Ruby
Stars: ✭ 229 (+717.86%)
Mutual labels:  compilers
go-recipes
🦩 Tools for Go projects
Stars: ✭ 2,490 (+8792.86%)
Mutual labels:  compilers
qcor
C++ compiler for heterogeneous quantum-classical computing built on Clang and XACC
Stars: ✭ 78 (+178.57%)
Mutual labels:  compilers
Pytket
Python module for interfacing with the CQC t|ket> library of quantum software
Stars: ✭ 162 (+478.57%)
Mutual labels:  compilers
compiler-course-unipi
Lab of the course Languages, Compilers and Interpreters (Cod. 653AA) @ UNIPI
Stars: ✭ 18 (-35.71%)
Mutual labels:  compilers
suicide
LLVM pass that detects one undefined behavior, and emits code to delete your hard drive
Stars: ✭ 33 (+17.86%)
Mutual labels:  compilers
UniversalQCompiler
Synthesizing arbitrary quantum computations
Stars: ✭ 53 (+89.29%)
Mutual labels:  compilers
Cs6120
advanced compilers
Stars: ✭ 232 (+728.57%)
Mutual labels:  compilers
Never
Never: statically typed, embeddable functional programming language.
Stars: ✭ 248 (+785.71%)
Mutual labels:  compilers
jet
A Fast C and Python like Programming Language that puts the Developer first. WIP
Stars: ✭ 41 (+46.43%)
Mutual labels:  compilers
Bolt
Bolt is a language with in-built data-race freedom!
Stars: ✭ 215 (+667.86%)
Mutual labels:  compilers
types-and-programming-languages
C++ Implementations of programming languages and type systems studied in "Types and Programming Languages" by Benjamin C. Pierce..
Stars: ✭ 32 (+14.29%)
Mutual labels:  compilers
Waforth
A bootstrapping dynamic Forth Interpreter/Compiler for WebAssembly
Stars: ✭ 176 (+528.57%)
Mutual labels:  compilers
Decaf-Compiler
Compiler for Decaf Programming Language
Stars: ✭ 36 (+28.57%)
Mutual labels:  compilers
build-anywhere
Scripts for building compilers that run anywhere, which build things that run anywhere
Stars: ✭ 72 (+157.14%)
Mutual labels:  compilers
Fortran-Tools
Fortran compilers, preprocessors, static analyzers, transpilers, IDEs, build systems, etc.
Stars: ✭ 31 (+10.71%)
Mutual labels:  compilers
LL-Script
Simple script
Stars: ✭ 38 (+35.71%)
Mutual labels:  compilers

wasm-script

This is a library for bringing your WebAssembly compiler to the web.

Vision

<script src="wasm-script.js"></script>
<wasm-script id="math" lang="c" compiler="http://yourcompiler.com/c.wasm">
     
     extern int add(int a, int b){
          return a + b;
     }  

</wasm-script>
<script type="module">
     // Compile as a module for accelerating javascript
     const mathModule = await document.getElementById("math").compile();
     console.log(mathModule.add(2,2));
</script>

Or run stand alone

<script src="wasm-script.min.js"></script>
<wasm-script lang="python" src="tetris.python" execute></wasm-script>
<!-- use default compilers from the community for certain languages -->

Features

  • script for loading a WebAssembly module from a compiler
  • ask to load dependent files by url
  • cacheing
  • show compile errors in console

CDN

https://cdn.jsdelivr.net/gh/richardanaya/wasm-script@latest/wasm-script.js

Reference Implementation

A reference implementation compiler is in progress in Rust for the wasp programming language ( a simple lisp-like syntax language ).

<script src="wasm-script.js"></script>
<wasm-script id="math" lang="wasp" compiler="compilers/waspc/compiler.wasm">
     
    pub fn secret(){
        42
    }

</wasm-script>
<script>
     // top-level await doesn't exist yet, so we have to do it the lame way
    (async function(){
        const mathModule = await document.getElementById("math").compile();
        document.body.innerHTML = mathModule.secret(1);
    })();
</script>

See the demo here

What the compiler is doing is fairly simple:

use wasm_compiler::{process,log};
use wasp_core::{compiler, parser};

#[no_mangle]
pub fn compile(code_ptr: usize) -> usize {
    process(code_ptr, |code| {
        log("compiling the code below!!");
        log(&code);
        let app = parser::parse(code)?;
        Ok(compiler::compile(app)?)
    })
}

Exposing Browser Functionality

Until we have DOM exposure in WebAssembly, we still are in a position we need to provide our own exposures to the browser's functionality. This library has some helper functions for common memory structures.

<script src="../wasm-script.js"></script>
<wasm-script id="math" lang="wasp" compiler="../compilers/waspc/compiler.wasm">
     
    extern console_log(message)
    pub fn main(){
        console_log("hello world!")
    }

</wasm-script>
<script>
     // top-level await doesn't exist yet, so we have to do it the lame way
    (async function(){
        const mathModule = await document.getElementById("math").compile({
            console_log: function(message_start) {
                const message = WasmScript.getCString(mathModule.memory,message_start)
                document.body.innerHTML += message;
              }
        });
        mathModule.main();
    })();
</script>

See the demo here

Memory Helper API

  • WasmScript.getCString(mem,start) - Returns a utf-8 string from position in memory that ends with a zero character.
  • WasmScript.getUint8ArrayFromMemory(mem,start) - Returns a Uint8Array from position in memory, starting with a uint32 count followed by the elements.

Building Your Own Compiler

Right now the current contract for building a compiler is very simple and can be implemented in any language that compiles to WebAssembly:

// external function to print a long takes a zero-character ending C string
extern void compiler_log(uint32 start_cstr)
// external function to print a error log and throw an exception takes a zero-character ending
// C string ( this function does not return )
extern void compiler_error(uint32 start_cstr)

// allocate some bytes of size
uint32 malloc(uint32 size)
// compile a code by passing in the start of the code in memory (created using malloc above). 
// and get back a list of bytes (the length as u32, followed by the data)
uint32 compile(uint32 code_str)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].