wasm-elementary-cellular-automaton
Version:
A simple elementary cellular automaton implemented in Rust and compiled to WebAssembly.
130 lines (111 loc) • 3.8 kB
JavaScript
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
let cachedUint8ArrayMemory0 = null;
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8ArrayMemory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
function getArrayU8FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
}
const CellularAutomatonFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_cellularautomaton_free(ptr >>> 0, 1));
export class CellularAutomaton {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(CellularAutomaton.prototype);
obj.__wbg_ptr = ptr;
CellularAutomatonFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
CellularAutomatonFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_cellularautomaton_free(ptr, 0);
}
/**
* @param {number} rule
* @param {Uint8Array} initial_state
* @param {boolean} periodic_boundary
* @returns {CellularAutomaton}
*/
static new(rule, initial_state, periodic_boundary) {
const ptr0 = passArray8ToWasm0(initial_state, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.cellularautomaton_new(rule, ptr0, len0, periodic_boundary);
return CellularAutomaton.__wrap(ret);
}
step() {
wasm.cellularautomaton_step(this.__wbg_ptr);
}
/**
* @returns {Uint8Array}
*/
get_state() {
const ret = wasm.cellularautomaton_get_state(this.__wbg_ptr);
var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
return v1;
}
/**
* @param {number} rule
*/
set_rule(rule) {
wasm.cellularautomaton_set_rule(this.__wbg_ptr, rule);
}
/**
* @param {Uint8Array} initial_state
*/
set_initial_state(initial_state) {
const ptr0 = passArray8ToWasm0(initial_state, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.cellularautomaton_set_initial_state(this.__wbg_ptr, ptr0, len0);
}
/**
* @param {boolean} periodic_boundary
*/
set_periodic_boundary(periodic_boundary) {
wasm.cellularautomaton_set_periodic_boundary(this.__wbg_ptr, periodic_boundary);
}
reset() {
wasm.cellularautomaton_reset(this.__wbg_ptr);
}
}
export function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);
table.set(offset + 0, undefined);
table.set(offset + 1, null);
table.set(offset + 2, true);
table.set(offset + 3, false);
;
};