index.js
function hibernate (instance, symbol = '_') {
  const json = {
    globals: [],
    table: [],
    symbol
  }
  for (const key in instance.exports) {
    const val = instance.exports[key]
    if (key.startsWith(symbol)) {
      const keyElems = key.slice(symbol.length).split('_')
      // save the memory
      if (val instanceof WebAssembly.Memory) {
        json.memory = new Uint32Array(val.buffer)
      } else if (val instanceof WebAssembly.Table) {
        // mark the tables, (do something for external function?)
        // the external functions should have a callback
        for (let i = 0; i < val.length; i++) {
          const func = val.get(i)
          if (func === instance.exports[`${symbol}func_${func.name}`]) {
            json.table.push(func.name)
          }
        }
      } else if (keyElems[0] === 'global' && keyElems[1] === 'getter') {
        // save the globals
        const last = keyElems.pop()
        if (last === 'high') {
          json.globals.push([instance.exports[key]()])
        } else if (last === 'low') {
          json.globals[json.globals.length - 1].push(instance.exports[key]())
        } else {
          json.globals.push(instance.exports[key]())
        }
      }
    }
  }
  instance.__hibernated = true
  return json
}
function hibernate (instance, symbol = '_') {
  const json = {
    globals: [],
    table: [],
    symbol
  }
  for (const key in instance.exports) {
    const val = instance.exports[key]
    if (key.startsWith(symbol)) {
      const keyElems = key.slice(symbol.length).split('_')
      // save the memory
      if (val instanceof WebAssembly.Memory) {
        json.memory = new Uint32Array(val.buffer)
      } else if (val instanceof WebAssembly.Table) {
        // mark the tables, (do something for external function?)
        // the external functions should have a callback
        for (let i = 0; i < val.length; i++) {
          const func = val.get(i)
          if (func === instance.exports[`${symbol}func_${func.name}`]) {
            json.table.push(func.name)
          }
        }
      } else if (keyElems[0] === 'global' && keyElems[1] === 'getter') {
        // save the globals
        const last = keyElems.pop()
        if (last === 'high') {
          json.globals.push([instance.exports[key]()])
        } else if (last === 'low') {
          json.globals[json.globals.length - 1].push(instance.exports[key]())
        } else {
          json.globals.push(instance.exports[key]())
        }
      }
    }
  }
  instance.__hibernated = true
  return json
}
function hibernate ( instance , symbol = '_' ) { const json = { globals : [ ] , table : [ ] , symbol } for ( const key in instance . exports ) { const val = instance . exports [ key ] if ( key . startsWith ( symbol ) ) { const keyElems = key . slice ( symbol . length ) . split ( '_' ) // save the memory if ( val instanceof WebAssembly . Memory ) { json . memory = new Uint32Array ( val . buffer ) } else if ( val instanceof WebAssembly . Table ) { // mark the tables, (do something for external function?) // the external functions should have a callback for ( let i = 0 ; i < val . length ; i ++ ) { const func = val . get ( i ) if ( func === instance . exports [ ` ${ symbol } ${ func . name } ` ] ) { json . table . push ( func . name ) } } } else if ( keyElems [ 0 ] === 'global' && keyElems [ 1 ] === 'getter' ) { // save the globals const last = keyElems . pop ( ) if ( last === 'high' ) { json . globals . push ( [ instance . exports [ key ] ( ) ] ) } else if ( last === 'low' ) { json . globals [ json . globals . length - 1 ] . push ( instance . exports [ key ] ( ) ) } else { json . globals . push ( instance . exports [ key ] ( ) ) } } } } instance . __hibernated = true return json }
Given a Webassembly Instance this will produce an Object containing the current state of
the instance
@param {WebAssembly.Instance} instance
@param {String} symbol - the symbol that will be used to find the injected functions
@return {Object} the state of the wasm instance
Given a Webassembly Instance this will produce an Object containing the current state of the instance