Git Product home page Git Product logo

examples's Introduction

AssemblyScript Examples

Actions Status

Instructions

This repository contains one example per directory. All examples can be obtained by cloning the repository:

$> git clone https://github.com/AssemblyScript/examples.git
$> cd examples

Afterwards, select the example you'd like to run and follow the instructions in its README file.

Low-level perspective

The following examples make use of AssemblyScript's low-level capabilities, i.e. to essentially write WebAssembly with a nicer syntax.

  • Conway's Game Of Life (demo)
    An implementation of the game of life with slight modifications. Updates an image buffer in memory, that is then presented on a canvas.

  • Mandelbrot Set (demo)
    Computes 2048 offsets of a color gradient in memory, line by line, and presents the set using the gradient's actual colors, as computed on the JavaScript side, on a canvas.

  • Interference effect (demo)
    Colin Eberhardt's and Ben Smith's WebAssembly interference effect, if it was written in AssemblyScript.

High-level perspective

These examples cover slightly higher level aspects, like working with managed objects or interfacing with them.

  • N-body system (demo)
    This is actually a benchmark - visualizing it just so happened.

  • Loader
    Utilizes the loader to perform various common tasks on the WebAssembly/JavaScript boundary, like passing along strings and arrays between both worlds.

Libraries

Various WebAssembly or AssemblyScript features as a library.

  • i64
    Exposes WebAssembly's i64 operations to JavaScript using 32-bit integers (low and high bits).

  • libm
    Exposes AssemblyScript's math routines for double and single precision as a library.

Features

General examples showing how to utilize specific AssemblyScript features.

  • Browser SDK (demo)
    Shows how to use the browser SDK to run the AssemblyScript compiler in the browser.

  • Compiler transforms
    An example of using compiler transforms to hook into the compilation process.

Additional resources

  • Wasm By Example
    A concise, hands-on introduction to WebAssembly using code snippets and annotated example programs. If you "learn best by doing", or just need a good starting point for a concept, this is the place for you.

  • Built with AssemblyScript
    A list of more sophisticated open source projects using AssemblyScript.

examples's People

Contributors

dcodeio avatar herrcai0907 avatar maxgraey avatar pvorona avatar rottencandy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

examples's Issues

Transform usage example

It would be awesome to have a usage example for transforms that showed how to transform some code. I want to try implementing a custom decorator as a preprocessor macro (as hinted by the docs you can do) but I'm not sure where to start.

Confused about performance

Hi there.
I'm new to WebAssembly and AssemblyScript ... I was looking to speed up JavaScript code and started to play around with it.

So I had a question:
I was playing with the Mandelbrot demo.
What I did is, I ported the code from the ts file, directly to the js file ... so I can calculate the mandelbot set either by the code in the compiled webassembly - or directly within the javascript code. I expected the webassmbly calculation to be (significantly?) quicker.

But actually these are my results:

Using Chrome: JavaScript Code ~ 530ms ... WebAssembly ~ 1000ms
Using Firefox: JavaScript Code ~ 580ms ... WebAssembly ~ 570ms
("limit" set to do 800 iterations. My PC is running windows, AMD Ryzen 9 3900X)

So it seems that in Chrome the WebAssembly version is half as fast as the normal JavaScript version ... in Firefox both version are at least equal in performance.

Is this expected? Or am I missing something?

Loader example does not work

Running the loader example results in


> @assemblyscript/[email protected] test
> node tests

Example 1:
/tmp/examples/loader/node_modules/@assemblyscript/loader/umd/index.js:174
      if (id !== STRING_ID) throw Error(`not a string: ${ptr}`);
                            ^

Error: not a string: 1056
    at __getString (/tmp/examples/loader/node_modules/@assemblyscript/loader/umd/index.js:174:35)
    at Object.<anonymous> (/tmp/examples/loader/tests/index.js:22:22)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.15.0

Examples per feature

Can we create examples per feature instead of examples per whatever? Because I need examples with simd and reference types (in future) but I can’t find it.

Also seems examples repo little bit not popular, can we make a monorepo with AS and examples for AS? It will be a self documentation for a project and also e2e tests or integration test, whatever you call it.

"Hello, World!"

Perhaps one of the more simpler requests of an example, which has a complicated solution. If WASI is implemented, this would be a call to fd_write. If they aren't, then this would be one hell of a "Hello, World!".

The build flags are incorrect (-o instead of -b, -b instead of -d)

The build flags specified in the examples are incorrect for asc version 0.20.6.

Currently the build scripts use "-b output.wasm" and "-d build/optimized.d.ts"

The correct flags are "-o output.wasm" and "-b esm"

Here is a diff:

diff --git a/mandelbrot/package.json b/mandelbrot/package.json
index 5b6c6b9..5165d15 100644
--- a/mandelbrot/package.json
+++ b/mandelbrot/package.json
@@ -4,8 +4,8 @@
   "license": "Apache-2.0",
   "private": true,
   "scripts": {
-    "asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime stub --importMemory --sourceMap --debug --measure",
-    "asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -d build/optimized.d.ts --use Math=JSMath --runtime stub -O3 --importMemory --sourceMap --measure",
+    "asbuild:untouched": "asc assembly/index.ts -o build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime stub --importMemory --sourceMap --debug --measure",
+    "asbuild:optimized": "asc assembly/index.ts -o build/optimized.wasm -t build/optimized.wat -b esm --use Math=JSMath --runtime stub -O3 --importMemory --sourceMap --measure",
     "asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
     "start": "npx serve"
   },

Game of life example not building

I still can't build game of life example. When running : npm run asbuild

I get the error :

> @assemblyscript/[email protected] asbuild
> npm run asbuild:debug && npm run asbuild:release


> @assemblyscript/[email protected] asbuild:debug
> asc assembly/index.ts --target debug

Fatal: Module::addMemory: 0 already exists

▌ Whoops, the AssemblyScript compiler has crashed during compile :-(
▌ 
▌ There is no stack trace. Perhaps a Binaryen exception above / in console?
▌ 
▌ > undefined
▌ 
▌ If you see where the error is, feel free to send us a pull request. If not,
▌ please let us know: https://github.com/AssemblyScript/assemblyscript/issues
▌ 
▌ Thank you!

Provide e2e browser sdk example

It is not obvious how to use @assemblyscript/loader with the browser sdk example, I had to download the loader separately but I see there are some instantiate bits in the dist sources but they're not exported or I can't figure out how to do it.

It would be helpful if we had a full(but simple) e2e example where the compiled binary gets instantiated and used using the loader and maybe also show some passing data back and forth to give the user a head start.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.