Mrmayman / rash

A JIT-compiled Scratch runtime written in rust

Repository from Github https://github.comMrmayman/rashRepository from Github https://github.comMrmayman/rash

rash

A Scratch JIT compiler written in rust using cranelift.

Hmm... What?

Scratch is a visual programming language aimed at children, and people push it to its limits (with projects like path tracers, 3d minecraft clones, neural networks and more), running into performance bottlenecks.

Rash aims to run scratch code with a JIT compiler, where the code is compiled to machine code and executed directly, much like Java.

What's the progress?

You can see the progress in implementing blocks in this Google Docs spreadsheet

Todo list:

(WIP means Work-In-Progress)

  • Implement loading from SB3 files (WIP)
  • Implement Control operations
    • If
    • If-Else
    • Repeat
    • Repeat Until
    • Forever
    • Wait
    • Wait Until
    • Stop this script
    • Stop all
    • Stop other scripts in sprite
  • Implement Math Operations
    • Arithmetic
    • And, Or, Not
    • Greater Than, Less Than
    • Equals
    • Join, Contains, Length
    • Mod, Round, Abs
    • Floor
    • Ceiling
    • Sqrt
    • Sin, Cos, Tan
    • ASin, ACos, ATan
    • Ln, Log
    • E^, 10^
  • Implement Custom Blocks
    • Implement Custom Block Arguments
    • Implement Without-Screen-Refresh Custom Blocks
    • Implement Screen-Refresh Custom Blocks
    • Implement Screen-Refresh Custom Blocks called from Without-Screen-Refresh Custom Blocks
  • Implement Variables
  • Implement Lists
  • Implement Broadcasts
  • Implement Clones
  • Add Graphics
    • Hide, Show blocks
    • Position blocks
    • Rotation blocks
    • Size block
    • Render sprites & stage
    • Render text
    • Render speech and thought bubbles
    • Sprite costumes and backdrops
    • Sprite graphical effects (Ghost, Fisheye, etc)
    • Pen canvas and clear operation
    • Pen stamps
    • Pen lines
    • Variable monitors
    • UI library
  • Add sound

Running

  • Install the Rust language if you haven't already.
  • Clone the repository: git clone https://github.com/Mrmayman/rash.git
  • Change directory: cd rash
  • Compile and run Rash: cargo run --release -- path/to/file.sb3
  • To run the test suite, do: cargo test

Contributing

Feel free to submit any changes you make as a pull request, I'll be happy to review it.

Benchmarks

Pi calculation:

  • Scratch: 2803 ms
  • Turbowarp: 30 ms
  • Rash: 6.3 ms

Quirks

Here are some weird Scratch quirks I've come across while creating this interpreter.

NaN Handling

In normal programming languages, any operation with a NaN value will return NaN. For example, 69.0 + NaN = NaN.

However, in Scratch, any operation with a NaN value will be treated as an operation with zero. For example, 69.0 + NaN will be treated as 69.0 + 0.0 = 69.0.

The check for this is basically (in pseudocode): if n == n { n } else { 0.0 }. This hurts performance noticeably but is essential for correctness.

String encoding

Scratch (appears to) encode strings in UTF-16, as opposed to Rust which encodes them in UTF-8. Basically normal text remains the same, but emojis are treated differently.

In Scratch, the skull emoji for example (πŸ’€) would be 2 characters, both invalid. letter(1) of "πŸ’€" and letter(2) of "πŸ’€" would return invalid characters, and length of "πŸ’€" would return 2.

However, in Rust, the skull emoji (πŸ’€) would be 4 characters, and "πŸ’€".len() == 4. To fix compatibility, str::encode_utf16() is used.

About

A JIT-compiled Scratch runtime written in rust

License:GNU General Public License v3.0


Languages

Language:Rust 99.0%Language:WGSL 0.9%Language:Shell 0.1%