clvm
Version:
Javascript implementation of chia lisp
444 lines (443 loc) • 15 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeClvmWasm = exports.__wbindgen_throw = exports.__wbg_set_502d29070ea18557 = exports.__wbg_newwithlength_3ec098a360da1909 = exports.__wbindgen_bigint_from_u64 = exports.__wbindgen_string_new = exports.__wbg_lazynode_new = exports.LazyNode = exports.Flag = exports.run_chia_program = exports.run_clvm = exports.node_from_bytes = exports.serialized_length = void 0;
/**
* How to generate this file.
* 1. Generate js interface files
* 1.1. git clone https://github.com/Chia-Network/clvm_rs
* 1.2. cd clvm_rs/wasm
* 1.3. cargo install wasm-pack
* 1.4. wasm-pack build --release --target=bundler
* 2. Preserve loader code embedded below.
* 3. Check contents of ./pkg/clvm_wasm_bg.wasm.d.ts and compose `ClvmWasmExports` type from it.
* 3. Copy contents of ./pkg/clvm_wasm_bg.js and paste here
* 4. Annotate typings, fix lint issues
* 5. Paste loader code preserved in the previous procedure
* 6. Add `__wb*` functions to the `imports` object.
* 7. Add `toJSON()` method to `LazyNode`.
*/
const Word32Array_1 = require("jscrypto/Word32Array");
const imports = {};
let wasm;
const lTextDecoder = typeof TextDecoder === "undefined" ? (0, module.require)("util").TextDecoder : TextDecoder;
const cachedTextDecoder = new lTextDecoder("utf-8", { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
let cachedUint8Memory0 = null;
function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
const heap = new Array(128).fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
function addHeapObject(obj) {
if (heap_next === heap.length)
heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8Memory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
let cachedBigInt64Memory0 = null;
function getBigInt64Memory0() {
if (cachedBigInt64Memory0 === null || cachedBigInt64Memory0.byteLength === 0) {
cachedBigInt64Memory0 = new BigInt64Array(wasm.memory.buffer);
}
return cachedBigInt64Memory0;
}
let cachedInt32Memory0 = null;
function getInt32Memory0() {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}
function getObject(idx) {
return heap[idx];
}
function dropObject(idx) {
if (idx < 132)
return;
heap[idx] = heap_next;
heap_next = idx;
}
function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
/**
* @param {Uint8Array} program
* @returns {bigint}
*/
function serialized_length(program) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(program, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.serialized_length(retptr, ptr0, len0);
const r0 = getBigInt64Memory0()[retptr / 8 + 0];
const r2 = getInt32Memory0()[retptr / 4 + 2];
const r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
return BigInt.asUintN(64, r0);
}
finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
exports.serialized_length = serialized_length;
/**
* @param {Uint8Array} b
* @param {number} flag
* @returns {LazyNode}
*/
function node_from_bytes(b, flag) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(b, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.node_from_bytes(retptr, ptr0, len0, flag);
const r0 = getInt32Memory0()[retptr / 4 + 0];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return LazyNode.__wrap(r0);
}
finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
exports.node_from_bytes = node_from_bytes;
function getArrayU8FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
* @param {Uint8Array} program
* @param {Uint8Array} args
* @param {number} flag
* @returns {Uint8Array}
*/
function run_clvm(program, args, flag) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(program, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(args, wasm.__wbindgen_malloc);
const len1 = WASM_VECTOR_LEN;
wasm.run_clvm(retptr, ptr0, len0, ptr1, len1, flag);
const r0 = getInt32Memory0()[retptr / 4 + 0];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const v3 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v3;
}
finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
exports.run_clvm = run_clvm;
/**
* @param {Uint8Array} program
* @param {Uint8Array} args
* @param {bigint} max_cost
* @param {number} flag
* @returns {[bigint, any]}
*/
function run_chia_program(program, args, max_cost, flag) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(program, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(args, wasm.__wbindgen_malloc);
const len1 = WASM_VECTOR_LEN;
wasm.run_chia_program(retptr, ptr0, len0, ptr1, len1, max_cost, flag);
const r0 = getInt32Memory0()[retptr / 4 + 0];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return takeObject(r0);
}
finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
exports.run_chia_program = run_chia_program;
/**
*/
class Flag {
constructor() {
this.__wbg_ptr = 0;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_flag_free(ptr);
}
/**
* @returns {number}
*/
static no_unknown_ops() {
const ret = wasm.flag_no_unknown_ops();
return ret >>> 0;
}
/**
* @returns {number}
*/
static allow_backrefs() {
const ret = wasm.flag_allow_backrefs();
return ret >>> 0;
}
}
exports.Flag = Flag;
/**
*/
class LazyNode {
constructor() {
this.__wbg_ptr = 0;
}
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(LazyNode.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_lazynode_free(ptr);
}
/**
* @returns {Array<any> | undefined}
*/
get pair() {
const ret = wasm.lazynode_pair(this.__wbg_ptr);
return takeObject(ret);
}
/**
* @returns {Uint8Array | undefined}
*/
get atom() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.lazynode_atom(retptr, this.__wbg_ptr);
const r0 = getInt32Memory0()[retptr / 4 + 0];
const r1 = getInt32Memory0()[retptr / 4 + 1];
let v1;
if (r0 !== 0) {
v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
}
return v1;
}
finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {Uint8Array}
*/
to_bytes_with_backref() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.lazynode_to_bytes_with_backref(retptr, this.__wbg_ptr);
const r0 = getInt32Memory0()[retptr / 4 + 0];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const r2 = getInt32Memory0()[retptr / 4 + 2];
const r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
const v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v1;
}
finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @param {number} limit
* @returns {Uint8Array}
*/
to_bytes(limit) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.lazynode_to_bytes(retptr, this.__wbg_ptr, limit);
const r0 = getInt32Memory0()[retptr / 4 + 0];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const r2 = getInt32Memory0()[retptr / 4 + 2];
const r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
const v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v1;
}
finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @param {Uint8Array} b
* @returns {LazyNode}
*/
static from_bytes_with_backref(b) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(b, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.lazynode_from_bytes_with_backref(retptr, ptr0, len0);
const r0 = getInt32Memory0()[retptr / 4 + 0];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return LazyNode.__wrap(r0);
}
finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @param {Uint8Array} b
* @returns {LazyNode}
*/
static from_bytes(b) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(b, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.lazynode_from_bytes(retptr, ptr0, len0);
const r0 = getInt32Memory0()[retptr / 4 + 0];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return LazyNode.__wrap(r0);
}
finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
toJSON() {
if (this.pair) {
return this.pair;
}
if (this.atom) {
return (new Word32Array_1.Word32Array(this.atom)).toString();
}
throw new Error("Invalid object");
}
}
exports.LazyNode = LazyNode;
function __wbg_lazynode_new(arg0) {
const ret = LazyNode.__wrap(arg0);
return addHeapObject(ret);
}
exports.__wbg_lazynode_new = __wbg_lazynode_new;
function __wbindgen_string_new(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
}
exports.__wbindgen_string_new = __wbindgen_string_new;
function __wbindgen_bigint_from_u64(arg0) {
const ret = BigInt.asUintN(64, arg0);
return addHeapObject(ret);
}
exports.__wbindgen_bigint_from_u64 = __wbindgen_bigint_from_u64;
function __wbg_newwithlength_3ec098a360da1909(arg0) {
const ret = new Array(arg0 >>> 0);
return addHeapObject(ret);
}
exports.__wbg_newwithlength_3ec098a360da1909 = __wbg_newwithlength_3ec098a360da1909;
function __wbg_set_502d29070ea18557(arg0, arg1, arg2) {
getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
}
exports.__wbg_set_502d29070ea18557 = __wbg_set_502d29070ea18557;
function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
}
exports.__wbindgen_throw = __wbindgen_throw;
// Loader part
imports["__wbindgen_placeholder__"] = {
__wbg_lazynode_new,
__wbindgen_string_new,
__wbindgen_bigint_from_u64,
__wbg_newwithlength_3ec098a360da1909,
__wbg_set_502d29070ea18557,
__wbindgen_throw,
};
const defaultClvmRsWasmPath = (() => {
if (typeof document !== "undefined" && document.currentScript) {
const scriptDir = document.currentScript.src.replace(/\/[^/]+$/, "");
return scriptDir + "/clvm_wasm_bg.wasm";
}
return "./clvm_wasm_bg.wasm";
})();
function initializeClvmWasm(option) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof window === "undefined") {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require.resolve("clvm_wasm/clvm_wasm_bg.wasm");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const bytes = require("fs").readFileSync(path);
const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
}
else {
let url;
if (option && option.pathToWasm) {
url = option.pathToWasm;
}
else {
url = defaultClvmRsWasmPath;
}
const fetcher = fetch(url, option && option.fetchOption);
const wasmInstance = yield WebAssembly.instantiateStreaming(fetcher, imports);
wasm = wasmInstance.instance.exports;
}
});
}
exports.initializeClvmWasm = initializeClvmWasm;