Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple wasm files to peacefully co-exist #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions etc/tupelo-wasm-sdk.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ export namespace Tupelo {
// (undocumented)
export function newEmptyTree(store: IBlockService, publicKey: Uint8Array): Promise<CID_2>;
// (undocumented)
export function newNamedTree(namespace: string, name: string, owners: string[]): Promise<CID_2>;
// (undocumented)
export function passPhraseKey(phrase: Uint8Array, salt: Uint8Array): Promise<Uint8Array[]>;
// (undocumented)
export function playTransactions(tree: ChainTree, transactions: Transaction[]): Promise<Proof>;
Expand Down
49 changes: 29 additions & 20 deletions src/js/go/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 };
Expand All @@ -60,27 +74,22 @@ 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')
result = await WebAssembly.instantiate(wasm, go.importObject)
}

}
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;
6 changes: 3 additions & 3 deletions src/tupelo.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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'),
});
Expand Down
2 changes: 1 addition & 1 deletion tupelo
Submodule tupelo updated 1 files
+8 −3 sdk/wasm/main.go