Convert Figma logo to code with AI

AssemblyScript logoassemblyscript

A TypeScript-like language for WebAssembly.

16,845
656
16,845
177

Top Related Projects

Optimizer and compiler/toolchain library for WebAssembly

Emscripten: An LLVM-to-WebAssembly Compiler

Facilitating high-level interactions between Wasm modules and JavaScript

15,263

A fast and secure runtime for WebAssembly

18,623

🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten

WebAssembly Micro Runtime (WAMR)

Quick Overview

AssemblyScript is a TypeScript-like language that compiles to WebAssembly. It provides a familiar syntax for developers who are used to TypeScript or JavaScript, while allowing them to write high-performance code that can run in browsers, Node.js, or other WebAssembly environments.

Pros

  • Familiar syntax for TypeScript/JavaScript developers
  • Compiles to efficient WebAssembly code
  • Strong typing and compile-time checks
  • Seamless integration with existing JavaScript projects

Cons

  • Limited ecosystem compared to more established languages
  • Not all TypeScript features are supported
  • Performance may not match hand-optimized C or Rust code
  • Learning curve for developers new to low-level programming concepts

Code Examples

  1. Basic function:
export function add(a: i32, b: i32): i32 {
  return a + b;
}

This example shows a simple function that adds two 32-bit integers.

  1. Working with arrays:
export function sumArray(arr: Int32Array): i32 {
  let sum: i32 = 0;
  for (let i = 0; i < arr.length; i++) {
    sum += arr[i];
  }
  return sum;
}

This function demonstrates how to work with typed arrays in AssemblyScript.

  1. Using classes:
class Vec2 {
  constructor(public x: f32, public y: f32) {}

  add(other: Vec2): Vec2 {
    return new Vec2(this.x + other.x, this.y + other.y);
  }
}

export function addVectors(v1: Vec2, v2: Vec2): Vec2 {
  return v1.add(v2);
}

This example shows how to define and use classes in AssemblyScript.

Getting Started

  1. Install the AssemblyScript compiler:
npm install -g assemblyscript
  1. Create a new project:
npx asinit my-project
cd my-project
  1. Edit the assembly/index.ts file to add your AssemblyScript code.

  2. Compile your code:

npm run asbuild
  1. Use the compiled WebAssembly module in your JavaScript project or run it with Node.js:
const fs = require('fs');
const loader = require('@assemblyscript/loader');
const imports = { /* imports go here */ };
const wasmModule = loader.instantiateSync(fs.readFileSync('build/optimized.wasm'), imports);
console.log(wasmModule.exports.add(1, 2));

This quick start guide demonstrates how to set up an AssemblyScript project, compile it, and use the resulting WebAssembly module in a Node.js environment.

Competitor Comparisons

Optimizer and compiler/toolchain library for WebAssembly

Pros of Binaryen

  • More mature and widely adopted in the WebAssembly ecosystem
  • Supports multiple input languages and formats (not limited to AssemblyScript)
  • Offers advanced optimization techniques for WebAssembly modules

Cons of Binaryen

  • Steeper learning curve for developers new to WebAssembly
  • Requires more manual configuration and setup compared to AssemblyScript
  • Less intuitive for developers coming from a TypeScript background

Code Comparison

AssemblyScript:

export function add(a: i32, b: i32): i32 {
  return a + b;
}

Binaryen (using C++ API):

auto module = make_unique<Module>();
auto func = module->addFunction("add", 
  FunctionType(vector<Type>{i32, i32}, i32),
  vector<Type>{},
  module->block("", {
    module->return_(
      module->binary(BinaryOp::AddInt32,
        module->local.get(0, i32),
        module->local.get(1, i32)
      )
    )
  })
);

The AssemblyScript code is more concise and familiar to TypeScript developers, while the Binaryen code offers more low-level control but requires more verbose setup.

Emscripten: An LLVM-to-WebAssembly Compiler

Pros of Emscripten

  • Mature and widely adopted toolchain with extensive documentation
  • Supports compilation of existing C/C++ codebases to WebAssembly
  • Provides a more complete runtime environment, including libc implementation

Cons of Emscripten

  • Larger output size due to included runtime and libraries
  • Steeper learning curve, especially for developers not familiar with C/C++
  • Slower compilation times compared to AssemblyScript

Code Comparison

AssemblyScript:

export function add(a: i32, b: i32): i32 {
  return a + b;
}

Emscripten (C++):

#include <emscripten.h>

EMSCRIPTEN_KEEPALIVE
extern "C" int add(int a, int b) {
  return a + b;
}

AssemblyScript offers a TypeScript-like syntax, making it more accessible for web developers. Emscripten, on the other hand, allows direct compilation of C/C++ code, which can be beneficial for porting existing projects or leveraging C/C++ libraries.

While AssemblyScript provides a simpler development experience and potentially smaller output sizes, Emscripten offers broader compatibility with existing codebases and a more comprehensive runtime environment. The choice between the two depends on project requirements, existing codebase, and developer expertise.

Facilitating high-level interactions between Wasm modules and JavaScript

Pros of wasm-bindgen

  • Seamless integration with Rust's ecosystem and toolchain
  • More mature and feature-rich, with extensive documentation
  • Supports complex data types and async functions

Cons of wasm-bindgen

  • Steeper learning curve, especially for developers new to Rust
  • Longer compilation times compared to AssemblyScript
  • Larger bundle sizes for small projects

Code Comparison

AssemblyScript:

export function add(a: i32, b: i32): i32 {
  return a + b;
}

wasm-bindgen:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

Summary

AssemblyScript offers a TypeScript-like syntax for WebAssembly development, making it more accessible for JavaScript developers. It has a gentler learning curve and faster compilation times, but may lack some advanced features and ecosystem support compared to wasm-bindgen.

wasm-bindgen, on the other hand, leverages Rust's powerful type system and performance optimizations. It provides more robust tooling and better integration with existing Rust projects. However, it may require more effort to learn and use effectively, especially for developers without prior Rust experience.

Choose AssemblyScript for simpler projects or if you're more comfortable with TypeScript. Opt for wasm-bindgen when working on complex applications, need advanced features, or want to leverage the Rust ecosystem.

15,263

A fast and secure runtime for WebAssembly

Pros of Wasmtime

  • Broader WebAssembly support: Wasmtime is a general-purpose WebAssembly runtime, supporting multiple languages and use cases
  • Performance: Optimized for speed and efficiency in executing WebAssembly modules
  • Active development: Backed by the Bytecode Alliance, with frequent updates and improvements

Cons of Wasmtime

  • Steeper learning curve: Requires more in-depth knowledge of WebAssembly concepts
  • Less focused on specific language compilation: Unlike AssemblyScript, which targets TypeScript-like syntax

Code Comparison

AssemblyScript (compiling TypeScript-like code to WebAssembly):

export function add(a: i32, b: i32): i32 {
  return a + b;
}

Wasmtime (executing WebAssembly modules in Rust):

let engine = Engine::default();
let module = Module::from_file(&engine, "add.wasm")?;
let instance = Instance::new(&mut store, &module, &[])?;
let add = instance.get_func(&mut store, "add")?;
let result = add.call(&mut store, &[Val::I32(5), Val::I32(37)])?;

This comparison highlights the different focus areas of AssemblyScript and Wasmtime. AssemblyScript provides a familiar TypeScript-like syntax for writing WebAssembly modules, while Wasmtime offers a runtime environment for executing and managing WebAssembly modules across various languages and platforms.

18,623

🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten

Pros of Wasmer

  • Broader language support: Wasmer can run WebAssembly modules compiled from various languages, not limited to AssemblyScript
  • More comprehensive runtime environment: Offers a full WebAssembly runtime with additional features like WASI support
  • Cross-platform compatibility: Runs on multiple operating systems and architectures

Cons of Wasmer

  • Higher complexity: Requires more setup and configuration compared to AssemblyScript's simpler approach
  • Larger footprint: Generally has a larger runtime size and resource usage
  • Steeper learning curve: May be more challenging for developers new to WebAssembly concepts

Code Comparison

AssemblyScript:

export function add(a: i32, b: i32): i32 {
  return a + b;
}

Wasmer (using Rust):

#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
    a + b
}

While AssemblyScript provides a TypeScript-like syntax for writing WebAssembly modules, Wasmer focuses on running and managing WebAssembly modules compiled from various languages. AssemblyScript is more specialized for TypeScript developers, while Wasmer offers a more versatile runtime environment for WebAssembly modules from different sources.

WebAssembly Micro Runtime (WAMR)

Pros of wasm-micro-runtime

  • Supports multiple programming languages, not limited to a single language like AssemblyScript
  • Provides a complete runtime environment for WebAssembly, including memory management and garbage collection
  • Offers a smaller footprint, making it suitable for embedded and IoT devices

Cons of wasm-micro-runtime

  • Steeper learning curve compared to AssemblyScript's TypeScript-like syntax
  • Less mature ecosystem and tooling support
  • May require more manual optimization for performance-critical applications

Code Comparison

AssemblyScript:

export function add(a: i32, b: i32): i32 {
  return a + b;
}

wasm-micro-runtime (C):

#include <wasm_export.h>

int32_t add(wasm_exec_env_t exec_env, int32_t a, int32_t b) {
  return a + b;
}

The AssemblyScript code is more concise and familiar to TypeScript developers, while the wasm-micro-runtime example requires more boilerplate and C knowledge. However, wasm-micro-runtime's approach allows for greater flexibility in language choice and runtime control.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

AssemblyScript logo

Test status Publish status npm compiler version Discord online #StandWithUkraine

AssemblyScript compiles a variant of TypeScript (basically JavaScript with types) to WebAssembly using Binaryen. It generates lean and mean WebAssembly modules while being just an npm install away.

About  Â·  Getting started  Â·  Examples  Â·  Built with AssemblyScript


Contributors

Contributor logos

Thanks to our sponsors!

Most of the maintainers and contributors do this open source work in their free time. If you use AssemblyScript for a serious task or plan to do so, and you'd like us to invest more time on it, please donate to our OpenCollective. By sponsoring this project, your logo will show up below. Thank you so much for your support!

Sponsor logos

Development instructions

A development environment can be set up by cloning the repository:

git clone https://github.com/AssemblyScript/assemblyscript.git
cd assemblyscript
npm install
npm link

The link step is optional and makes the development instance available globally. The full process is documented as part of the repository:

NPM DownloadsLast 30 Days