All files / kernel-test/src/vats importer-vat.ts

0% Statements 0/31
0% Branches 0/10
0% Functions 0/10
0% Lines 0/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                                                                                                                                                                                                                                                             
import { E } from '@endo/eventual-send';
import { makeDefaultExo } from '@metamask/kernel-utils/exo';
 
import { unwrapTestLogger } from '../test-powers.ts';
import type { TestPowers } from '../test-powers.ts';
 
/**
 * Build function for vats that will run various tests.
 *
 * @param vatPowers - Special powers granted to this vat.
 * @param vatPowers.logger - The logger for the vat.
 * @param parameters - Initialization parameters from the vat's config object.
 * @param parameters.name - The name of the vat.
 * @param _baggage - Root of vat's persistent state (not used here).
 * @returns The root object for the new vat.
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function buildRootObject(
  vatPowers: TestPowers,
  parameters: { name?: string } = {},
  _baggage: unknown = null,
) {
  const name = parameters?.name ?? 'anonymous';
  const logger = unwrapTestLogger(vatPowers, name);
  const tlog = (message: string, ...args: unknown[]): void =>
    logger(`${name}: ${message}`, ...args);
 
  let weakMap = new WeakMap<object, string>();
 
  const importedObjects = new Map<string, object>();
 
  /**
   * Print a message to the log.
   *
   * @param message - The message to print.
   */
  function log(message: string): void {
    // eslint-disable-next-line no-console
    console.log(`${name}: ${message}`);
  }
 
  return makeDefaultExo('root', {
    bootstrap() {
      log(`bootstrap`);
      return `bootstrap-${name}`;
    },
 
    /**
     * Store an imported object by ID
     * keeping a strong reference and a weak map entry.
     *
     * @param obj - The imported object to store.
     * @param id - The ID to store the object under.
     * @returns The string 'stored'.
     */
    storeImport(obj: object, id = 'default') {
      tlog(`Storing import ${id}`, obj);
      importedObjects.set(id, obj);
      weakMap.set(obj, id);
      return 'stored';
    },
 
    /**
     * Use the imported object by ID and call its method.
     *
     * @param id - The ID of the object to use.
     * @returns The result of calling the object's method.
     */
    useImport(id = 'default') {
      tlog(`useImport ${id}`);
      const obj = importedObjects.get(id);
      if (!obj) {
        throw new Error(`Object not found: ${id}`);
      }
      tlog(`Using import ${id}`);
      return E(obj).getValue();
    },
 
    /**
     * Make the reference to an imported object weak by
     * removing the strong reference, keeping only the weak one.
     *
     * @param id - The ID of the object to make weak.
     * @returns True if the object was successfully made weak, false if it doesn't exist.
     */
    makeWeak(id: string) {
      const obj = importedObjects.get(id);
      if (!obj) {
        tlog(`Cannot make weak reference to nonexistent object: ${id}`);
        return false;
      }
      tlog(`Making weak reference to ${id} (dropping strong ref)`);
      importedObjects.delete(id);
      return true;
    },
 
    /**
     * Completely forget about the imported object.
     * Once all vats forget it, retireImports() should trigger.
     *
     * @returns True if the object was successfully forgotten, false if it doesn't exist.
     */
    forgetImport() {
      weakMap = new WeakMap();
      return true;
    },
 
    /**
     * List all imported objects.
     *
     * @returns An array of all imported object IDs.
     */
    listImportedObjects() {
      return Array.from(importedObjects.keys());
    },
 
    /**
     * No-op method.
     *
     * @returns The string 'noop'.
     */
    noop() {
      return 'noop';
    },
  });
}