All Projects → maekawatoshiki → Rapidus

maekawatoshiki / Rapidus

Licence: mit
A toy JavaScript engine implemented in Rust from scratch

Programming Languages

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

Labels

Projects that are alternatives of or similar to Rapidus

Seahorn
SeaHorn Verification Framework
Stars: ✭ 270 (-30.77%)
Mutual labels:  llvm
Soll
SOLL is a new compiler for generate Ewasm from solidity and yul. See a demo here: https://asciinema.org/a/ezJqNLicn5fya02zwu4VXIo8a
Stars: ✭ 329 (-15.64%)
Mutual labels:  llvm
Easy Just In Time
LLVM Optimization to extract a function, embedded in its intermediate representation in the binary, and execute it using the LLVM Just-In-Time compiler.
Stars: ✭ 361 (-7.44%)
Mutual labels:  llvm
Go Ast Book
📚 《Go语法树入门——开启自制编程语言和编译器之旅》(开源免费图书/Go语言进阶/掌握抽象语法树/Go语言AST/凹语言)
Stars: ✭ 4,034 (+934.36%)
Mutual labels:  llvm
Swiftweekly.github.io
A community-driven weekly newsletter about Swift.org
Stars: ✭ 305 (-21.79%)
Mutual labels:  llvm
Staticscript
🎉🎉🎉 A new statically typed programming language, syntactically like TypeScript.
Stars: ✭ 337 (-13.59%)
Mutual labels:  llvm
Olifant
A simple programming language targeting LLVM
Stars: ✭ 58 (-85.13%)
Mutual labels:  llvm
Llvm Hs
Haskell bindings for LLVM
Stars: ✭ 370 (-5.13%)
Mutual labels:  llvm
Smack
SMACK Software Verifier and Verification Toolchain
Stars: ✭ 305 (-21.79%)
Mutual labels:  llvm
Decomp
Components of a decompilation pipeline.
Stars: ✭ 343 (-12.05%)
Mutual labels:  llvm
Speedy.js
Accelerate JavaScript Applications by Compiling to WebAssembly
Stars: ✭ 300 (-23.08%)
Mutual labels:  llvm
Mapping High Level Constructs To Llvm Ir
A guide that explains how high level programming language constructs are mapped to the LLVM intermediate language.
Stars: ✭ 305 (-21.79%)
Mutual labels:  llvm
Bfc
An industrial-grade brainfuck compiler
Stars: ✭ 340 (-12.82%)
Mutual labels:  llvm
Clang Power Tools
Bringing clang-tidy magic to Visual Studio C++ developers.
Stars: ✭ 285 (-26.92%)
Mutual labels:  llvm
Codegen
Experimental wrapper over LLVM for generating and compiling code at run-time.
Stars: ✭ 362 (-7.18%)
Mutual labels:  llvm
Deepc
vendor independent deep learning library, compiler and inference framework microcomputers and micro-controllers
Stars: ✭ 260 (-33.33%)
Mutual labels:  llvm
Clangkit
ClangKit provides an Objective-C frontend to LibClang. Source tokenization, diagnostics and fix-its are actually implemented.
Stars: ✭ 330 (-15.38%)
Mutual labels:  llvm
Nlvm
LLVM-based compiler for the Nim language
Stars: ✭ 380 (-2.56%)
Mutual labels:  llvm
Sericum
(Toy) Compiler Infrastructure influenced by LLVM written in Rust
Stars: ✭ 366 (-6.15%)
Mutual labels:  llvm
Wasi Sdk
WASI-enabled WebAssembly C/C++ toolchain
Stars: ✭ 342 (-12.31%)
Mutual labels:  llvm

Rapidus

CircleCI codecov

A toy JavaScript engine (now aiming for ES6).

What's this branch?

In this branch:

  • Develop the new generation of Rapidus (old gen in master branch)
  • Some information in this README may vary from the fact

Issues are always welcome

Rapidus on WASM

On this page, you can try rapidus compiled into WASM on your browser. How amazing, isn't it?

The compiled rapidus on the above page is some commits behind this branch.

Features

  • Small
  • Partly support for Tracing-JIT compiling
  • REPL

Building from Source

Building on Linux

  1. Install Rust

Run the command below and follow the onscreen instructions.

curl https://sh.rustup.rs -sSf | sh
  1. Use Rust Nightly
rustup override set nightly
  1. Install dependencies
  • LLVM 6.0
  • (Other packages as necessary...)
# e.g. Ubuntu or Debian
apt-get install llvm-6.0
  1. Test
cargo test
  • If the compilation failed because of LLVM-related errors, the following command may help.
ln -sf /usr/bin/llvm-config-6.0 /usr/bin/llvm-config
  1. Build
cargo run --release
  1. Run
cargo run --release examples/XXX.js
  1. multilined-aware REPL
$ cargo run
> function fact(n) {
... if (n < 2) {     <- recognize multilined input
... return n
... } else {
... return n * fact(n-1)
... }
... }                <- recognize the end of input
undefined
> fact(10)
3628800
  1. Debug mode (tracing bytecode execution)

    use --trace option.

$ cargo run -- --trace
> function fibo(x) { if (x<2) return 1; return fibo(x-1)+fibo(x-2)}
00020m 00000 Return                    <empty>
undefined
> fibo(3)
00009m 00000 PushInt8 3                <empty>
00015m 00002 GetValue 'fibo'           3.0
00066m 00007 Call 1                    Function
--> call function
  module_id:0 func_id:0
00007m 00000 GetValue 'x'              <empty>
00001m 00005 PushInt8 2                3.0
00013m 00007 Lt                        2.0

...

00001m 00007 Lt                        2.0
00000m 00008 JmpIfFalse 00016          true
00000m 00013 PushInt8 1                <empty>
00167m 00015 Return                    1.0
<-- return value(1.0)
  module_id:0 func_id:2
00001m 00052 Add                       1.0
00044m 00053 Return                    3.0
<-- return value(3.0)
  module_id:0 func_id:0
00000m 00012 Return                    3.0
3
> 
   |     |     |                        | 
   |     |     |                        \- value at the top of exec stack
   |     |     \-------------------------- instruction
   |     \-------------------------------- program counter
   \-------------------------------------- execution time per inst. (in microsecs)

Building on other platforms

I don't know.

  • tips: If you are using macOS, you cannot use llvm installed with brew. You should use macports or docker instead. Now it works!

Use DLLs written in Rust

THIS FEATURE IS EXPERIMENTAL

  1. Make a cargo project in the directory rapidus' directory is located
$ cargo new hello --lib
$ ls
rapidus hello
  1. Edit Cargo.toml
$ cd hello
$ <YOUR EDITOR> Cargo.toml
# Add the followings to Cargo.toml

[dependencies]
rapidus = { path = "../rapidus" }
# other dependencies if you want...

[lib]
name = "hello"
crate_type = ["cdylib"] # try 'dylib' if it doesn't work.
  1. Edit src/lib.rs
$ <YOUR EDITOR> src/lib.rs
// src/lib.rs

#[macro_use]
extern crate rapidus;
use rapidus::{
   gc,
   vm::{callobj::CallObject, error::RuntimeError, value::*, vm::VM},
};

#[no_mangle]
fn initialize(vm: &mut VM, _: &Vec<Value>, _: CallObjectRef) -> Result<(), RuntimeError> {
    // make_object!() is useful
    let module_exports = make_object!(
        greet:   Value::default_builtin_function(greet),
        message: Value::String("hello".to_string())
    );

    vm.set_return_value(module_exports); // We have to return module.exports

    Ok(())
}

#[no_mangle]
fn greet(vm: &mut VM, _: &Vec<Value>, _: CallObjectRef) -> Result<(), RuntimeError> {
    println!("Hello World from Rust DLL!");

    vm.set_return_value(Value::Undefined); // Remember to return a value you want

    Ok(())
}
  1. Let's build
$ cargo build # --release as necessary
  1. Copy the generated DLL to rapidus' directory
$ cp ./target/debug/libhello.(so|dll|dylib) ../rapidus
$ cd ../rapidus
$ ls
libhello.(so|dll|dylib) etc...
  1. You're ready to use it from rapidus. Let's try from REPL.
$ cargo run
> var mod = require('hello')
> mod.greet()
Hello World from Rust DLL!
> mod.message
'hello'
  1. Now everything can be possible from Rust!

Use Dockerfile

  • Docker image: uint256/rapidus
$ docker build -t rapidus:0.1.1 .
$ docker run -it rapidus:0.1.1
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].