ecmassembly

0.2.1 • Public • Published

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)
}

Finally, make sure when you compile your AS code you pass --exportTable --exportRuntime to the asc CLI. For example:

asc assembly/index.ts --target release --exportTable --exportRuntime

APIs so far

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

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i ecmassembly

    Weekly Downloads

    1

    Version

    0.2.1

    License

    none

    Unpacked Size

    14.8 kB

    Total Files

    11

    Last publish

    Collaborators

    • trusktr