/ README.adoc
README.adoc
  1  = AffineScript Playground
  2  :toc: macro
  3  :icons: font
  4  
  5  // SPDX-License-Identifier: AGPL-3.0-or-later
  6  
  7  image:https://img.shields.io/badge/Language-AffineScript-cyan[AffineScript]
  8  image:https://img.shields.io/badge/Types-Affine-purple[Affine Types]
  9  image:https://img.shields.io/badge/Target-WebAssembly-black[WASM]
 10  image:https://img.shields.io/badge/License-AGPL--3.0-blue[License]
 11  
 12  *Playground for AffineScript - Affine Types for WebAssembly*
 13  
 14  toc::[]
 15  
 16  == Overview
 17  
 18  AffineScript brings affine type semantics to WebAssembly, ensuring resources are used at most once while maintaining high performance for web applications.
 19  
 20  === Key Features
 21  
 22  * **Affine Types**: Use resources at most once
 23  * **WebAssembly Target**: Compiles to efficient WASM
 24  * **JavaScript Interop**: Seamless JS/TS integration
 25  * **Zero Runtime Overhead**: Types erased at compile time
 26  
 27  == Affine vs Linear Types
 28  
 29  [cols="1,2,2"]
 30  |===
 31  | Type System | Resource Usage | Behavior
 32  
 33  | **Affine** (AffineScript)
 34  | At most once
 35  | Can drop without use
 36  
 37  | **Linear** (Ephapax)
 38  | Exactly once
 39  | Must use, cannot drop
 40  |===
 41  
 42  == Language Design
 43  
 44  === Affine Resources
 45  
 46  [source,affinescript]
 47  ----
 48  -- Affine type: can be used 0 or 1 time
 49  type Handle = affine {
 50      ptr: WasmPtr,
 51  }
 52  
 53  fn example():
 54      let h = acquire_handle()
 55      -- Can use h once:
 56      use_handle(h)
 57      -- Or drop without use (affine allows this):
 58      -- (h goes out of scope, auto-dropped)
 59  ----
 60  
 61  === WASM Compilation
 62  
 63  [source,affinescript]
 64  ----
 65  -- Compile to WebAssembly
 66  @wasm_export
 67  fn add(a: i32, b: i32) -> i32:
 68      a + b
 69  
 70  -- Import from JavaScript
 71  @wasm_import("console", "log")
 72  fn js_log(msg: String) -> ()
 73  ----
 74  
 75  === JavaScript Integration
 76  
 77  [source,javascript]
 78  ----
 79  // Using AffineScript from JavaScript
 80  import { init } from './app.wasm';
 81  
 82  const wasm = await init();
 83  const result = wasm.add(1, 2);
 84  console.log(result); // 3
 85  ----
 86  
 87  == Getting Started
 88  
 89  [source,bash]
 90  ----
 91  # Install
 92  guix install affinescript
 93  
 94  # Compile to WASM
 95  affinescript build --target=wasm32 app.afs
 96  
 97  # Run in browser or Node.js
 98  ----
 99  
100  == Examples
101  
102  * `wasm/` - WebAssembly examples
103  * `interop/` - JS interoperability
104  * `resources/` - Affine resource management
105  
106  == Related
107  
108  * **Ephapax** - Sibling language with linear types
109  
110  == License
111  
112  SPDX-License-Identifier: AGPL-3.0-or-later