clvm_tools
Version:
Javascript implementation of clvm_tools
97 lines (96 loc) • 3.96 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.initialize = exports.run_clvm = exports.wasm = void 0;
const imports = {};
let cachegetUint8Memory0 = null;
function getUint8Memory0() {
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== exports.wasm.memory.buffer) {
cachegetUint8Memory0 = new Uint8Array(exports.wasm.memory.buffer);
}
return cachegetUint8Memory0;
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1);
getUint8Memory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
let cachegetInt32Memory0 = null;
function getInt32Memory0() {
if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== exports.wasm.memory.buffer) {
cachegetInt32Memory0 = new Int32Array(exports.wasm.memory.buffer);
}
return cachegetInt32Memory0;
}
function getArrayU8FromWasm0(ptr, len) {
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
* @param {Uint8Array} program
* @param {Uint8Array} args
* @returns {Uint8Array}
*/
function run_clvm(program, args) {
try {
const retptr = exports.wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(program, exports.wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(args, exports.wasm.__wbindgen_malloc);
const len1 = WASM_VECTOR_LEN;
exports.wasm.run_clvm(retptr, ptr0, len0, ptr1, len1);
const r0 = getInt32Memory0()[retptr / 4 + 0];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const v2 = getArrayU8FromWasm0(r0, r1).slice();
exports.wasm.__wbindgen_free(r0, r1 * 1);
return v2;
}
finally {
exports.wasm.__wbindgen_add_to_stack_pointer(16);
}
}
exports.run_clvm = run_clvm;
const defaultClvmRsWasmPath = (() => {
if (typeof document !== "undefined" && document.currentScript) {
const scriptDir = document.currentScript.src.replace(/\/[^/]+$/, "");
return scriptDir + "/clvm_rs_bg.wasm";
}
else {
return "./clvm_rs_bg.wasm";
}
})();
function initialize(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_rs/clvm_rs_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);
exports.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);
exports.wasm = wasmInstance.instance.exports;
}
});
}
exports.initialize = initialize;