diff --git a/etc/tupelo-wasm-sdk.api.md b/etc/tupelo-wasm-sdk.api.md index 3176b4d..e214c77 100644 --- a/etc/tupelo-wasm-sdk.api.md +++ b/etc/tupelo-wasm-sdk.api.md @@ -325,6 +325,8 @@ export namespace Tupelo { // (undocumented) export function newEmptyTree(store: IBlockService, publicKey: Uint8Array): Promise; // (undocumented) + export function newNamedTree(namespace: string, name: string, owners: string[]): Promise; + // (undocumented) export function passPhraseKey(phrase: Uint8Array, salt: Uint8Array): Promise; // (undocumented) export function playTransactions(tree: ChainTree, transactions: Transaction[]): Promise; diff --git a/src/js/go/index.js b/src/js/go/index.js index e74fe05..84b01fa 100644 --- a/src/js/go/index.js +++ b/src/js/go/index.js @@ -24,10 +24,6 @@ global.Go.setWasmPath = (path) => { global.Go.wasmPath = path; } -global.Go.readyPromise = new Promise((resolve) => { - global.Go.readyResolver = resolve; -}); - if (!global.Buffer) { global.Buffer = buffer.Buffer; } @@ -36,21 +32,39 @@ if (!global.process.title) { global.process.title = window.navigator.userAgent } +global._goWasm = {} + const runner = { - run: async () => { - log('outer go.run') + run: async(path) => { + if (!path) { + path = Go.wasmPath + } + log('outer go.run: ', path) const go = new Go(); + global._goWasm[path] = go + + go.argv = ["js", path] + + go.readyPromise = new Promise((resolve) => { + go.readyResolver = resolve; + }); + + console.log("setting ready") + go.ready = function() { + return go.readyPromise + } + go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env); let result if (isNodeJS) { - const wasmBits = global.fs.readFileSync(Go.wasmPath) + const wasmBits = global.fs.readFileSync(path) result = await WebAssembly.instantiate(wasmBits, go.importObject) process.on("exit", (code) => { // Node.js exits if no event handler is pending - Go.exit(); + go.terminate(); if (code === 0 && !go.exited) { // deadlock, make Go print error and stack traces go._pendingEvent = { id: 0 }; @@ -60,11 +74,11 @@ const runner = { } else { log("is not nodejs") if (typeof WebAssembly.instantiateStreaming == 'function') { - console.log("wasm path: ", Go.wasmPath) - result = await WebAssembly.instantiateStreaming(fetch(Go.wasmPath), go.importObject) + console.log("wasm path: ", path) + result = await WebAssembly.instantiateStreaming(fetch(path), go.importObject) } else { log('fetching wasm') - const wasmResp = await fetch(Go.wasmPath) + const wasmResp = await fetch(path) log('turning it into an array buffer') const wasm = await wasmResp.arrayBuffer() log('instantiating') @@ -72,15 +86,10 @@ const runner = { } } - log('inner go.run') - return go.run(result.instance); - }, - ready: async (path) => { - return global.Go.readyPromise; + log('inner go.run ', go) + go.exitPromise = go.run(result.instance) + return go; }, - populate: (obj, reqs) => { - return global.populateLibrary(obj, reqs); - } } -module.exports = runner; +module.exports = runner; \ No newline at end of file diff --git a/src/tupelo.ts b/src/tupelo.ts index 67223db..84aee83 100644 --- a/src/tupelo.ts +++ b/src/tupelo.ts @@ -1,6 +1,6 @@ import CID from 'cids'; -const go = require('./js/go') +const Go = require('./js/go') import { Transaction } from 'tupelo-messages' import { TokenPayload } from 'tupelo-messages/transactions/transactions_pb' import { IBlockService, IBlock } from './chaintree/dag/dag' @@ -105,9 +105,9 @@ namespace TupeloWasm { _tupelowasm = new Promise(async (resolve, reject) => { const wasm = new UnderlyingWasm; logger("go.run for first time"); - go.run("./main.wasm"); + const go = await Go.run(); await go.ready(); - go.populate(wasm, { + go.populateLibrary(wasm, { "cids": CID, "ipfs-block": require('ipfs-block'), }); diff --git a/tupelo b/tupelo index c525428..7c5381a 160000 --- a/tupelo +++ b/tupelo @@ -1 +1 @@ -Subproject commit c525428560a8b6db9b09688c71a4da1eab072940 +Subproject commit 7c5381a4dc650558e16e67c9ef50a724c8f5df97