0.2.1 • Published 2 years ago

ecmassembly v0.2.1

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

ECMAssembly

Spec'd and/or common JavaScript APIs brought to AssemblyScript.

The name is a play on words: ECMAScript -> AssemblyScript -> ECMAssembly

Usage

Look at example/index.js to see how one would pass JavaScript-side requirements into the Wasm module via imports object, then look at example/assembly/index.ts to see how to import the APIs inside of AssemblyScript code.

First:

npm install ecmassembly

On the JavaScript side pass required glue code to the Wasm module via imports:

import {ECMAssembly} from 'ecmassembly/index.js'
import ASLoader from '@assemblyscript/loader'

const es = new ECMAssembly()

const imports = {
	...es.wasmImports,
	/*...All your own imports...*/
}

ASLoader.instantiateStreaming(fetch('path/to/module.wasm'), imports).then(wasmModule => {
	// After the Wasm module is created, you need to pass the exports back to the lib:
	es.wasmExports = wasmModule.exports

	// Then finally, run anything from the module that depends on setTimeout, Promise, etc:
	wasmModule.exports.runMyApp()
})

In your AssemblyScript code import what you need an use it:

import {Promise, setTimeout, PromiseActions} from '../node_modules/ecmassembly/assembly/index'

export function runMyApp() {
	const actions: PromiseActions | null = null

	const promise = new Promise<boolean>(_actions => {
		// Temporary hack while AS does not yet support closures (no closing over variable except those that are at the top-level of the module).
		actions = _actions

		// resolve after 1 second
		setTimeout(() => {
			actions.resolve(true)
		}, 1000)
	})

	promise.then(result => {
		// `result` will be `true` here, this runs one second later.
	})
}

or

import {requestAnimationFrame} from '../node_modules/ecmassembly/assembly/index'

// This is out here because there is no closure support for non-top-level variables yet.
let loop: (time: f32) => void = (t: f32) => {}

export function runMyApp() {
	// Make an infinite game loop.

	loop = (time: f32) => {
		// ... render something based on the current elapsed time ...

		requestAnimationFrame(loop)
	}

	requestAnimationFrame(loop)
}

APIs so far

  • requestAnimationFrame/cancelAnimationFrame
  • setTimeout/clearTimeout
  • setInterval/clearInterval
  • Promise (rudimentary initial implementation, still lacking things like promise chaining and static methods, etc)
0.2.1-beta.0

2 years ago

0.3.0-beta.0

2 years ago

0.2.1

2 years ago

0.2.0

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.2

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago