This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fetch('memory-access.gc.wasm') | |
| .then(r => r.arrayBuffer()) | |
| .then(arrayBuff => WebAssembly.instantiate(arrBuff, {})) | |
| .then(rustWasm => console.log(rustWasm)); | |
| // Object { module: WebAssembly.Module, instance: WebAssembly.Instance } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const typedArr = new Uint32Array(arrayBuffer, 0, 4); | |
| const arr = [...typedArray]; | |
| // [100, 200, 300, 400] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[no_mangle] | |
| pub fn simple_arr() -> *mut i32 { | |
| let vec = Box::new([100, 200, 300, 400]); | |
| Box::into_raw(vec) as *mut i32 | |
| } | |
| fn main() { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rustup default nightly | |
| rustup target add wasm32-unknown-unknown --toolchain nightly | |
| cargo install wasm-gc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function distil(imgPath, nrOfPalettes = 2) { | |
| const wasmModule = await compile('distil.wasm'); | |
| const { imgBufferLocation, length } = await readImgIntoMemory(imgPath); | |
| const paletteBuffer = wasmModule.exports.distil(imgBufferLocation, length); | |
| console.log(bufferToHex(paletteBuffer, nrOfPalettes)); | |
| } | |
| // -> ['#ff1122', '#jjkkll'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import eventEmiter, { EVENTS } from './core/bus'; | |
| function authenticate(user, password) { | |
| return new Promise((resolve, reject) => { | |
| eventEmitter.once(EVENTS.AUTH.SUCCESS, resolve); | |
| eventEmitter.once(EVENTS.AUTH.FAILED, reject); | |
| eventEmitter.trigger(EVENTS.AUTH.ATTEMPT, { user, pass }); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import eventEmiter, { EVENTS } from './core/bus'; | |
| function authenticate(user, password) { | |
| return new Promise((resolve, reject) => { | |
| eventEmitter.on(EVENTS.AUTH.SUCCESS, resolve); | |
| eventEmitter.on(EVENTS.AUTH.FAILED, reject); | |
| eventEmitter.trigger(EVENTS.AUTH.ATTEMPT, { user, pass }); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import EventEmitter2 from 'eventemitter2'; | |
| const appBus = new eventEmitter(); | |
| const EVENTS = { | |
| AUTH: { | |
| SUCCESS: 'AUTH.SUCCESS', | |
| FAILED: 'AUTH.FAILED', | |
| ATTEMPT: 'AUTH.ATTEMPT', | |
| }; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import eventEmiter, { EVENTS } from './core/bus'; | |
| eventEmitter.on(EVENTS.AUTH.SUCCESS, () => {}); | |
| eventEmitter.on(EVENTS.AUTH.FAILED, () => {}); | |
| eventEmitter.trigger(EVENTS.AUTH.ATTEMPT, { user, pass }); |