Skip to content

Commit

Permalink
feat(daemon): add incarnateEval
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Feb 19, 2024
1 parent 073d46d commit 0bd689d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
28 changes: 28 additions & 0 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,33 @@ const makeDaemonCore = async (
);
};

/**
* @param {string} workerFormulaIdentifier
* @param {string} source
* @param {string[]} codeNames
* @param {string[]} endowmentFormulaIdentifiers
* @returns {Promise<{ formulaIdentifier: string, value: unknown }>}
*/
const incarnateEval = async (
workerFormulaIdentifier,
source,
codeNames,
endowmentFormulaIdentifiers,
) => {
const formulaNumber = await randomHex512();
/** @type {import('./types.js').EvalFormula} */
const formula = {
type: 'eval',
worker: workerFormulaIdentifier,
source,
names: codeNames,
values: endowmentFormulaIdentifiers,
};
return /** @type {Promise<{ formulaIdentifier: string, value: unknown }>} */ (
provideValueForNumberedFormula(formula.type, formulaNumber, formula)
);
};

/**
* @param {string} powersFormulaIdentifier
* @param {string} workerFormulaIdentifier
Expand Down Expand Up @@ -830,6 +857,7 @@ const makeDaemonCore = async (
provideControllerForFormulaIdentifier,
incarnateHost,
incarnateGuest,
incarnateEval,
incarnateHandle,
storeReaderRef,
randomHex512,
Expand Down
20 changes: 7 additions & 13 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const makeHostMaker = ({
provideControllerForFormulaIdentifier,
incarnateHost,
incarnateGuest,
incarnateEval,
incarnateHandle,
storeReaderRef,
makeSha512,
Expand Down Expand Up @@ -264,7 +265,7 @@ export const makeHostMaker = ({
throw new Error('Evaluator requires one pet name for each code name');
}

const formulaIdentifiers = await Promise.all(
const endowmentFormulaIdentifiers = await Promise.all(
petNamePaths.map(async (petNameOrPath, index) => {
if (typeof codeNames[index] !== 'string') {
throw new Error(`Invalid endowment name: ${q(codeNames[index])}`);
Expand All @@ -285,20 +286,13 @@ export const makeHostMaker = ({
}),
);

const evalFormula = {
/** @type {'eval'} */
type: 'eval',
worker: workerFormulaIdentifier,
source,
names: codeNames,
values: formulaIdentifiers,
};

// Behold, recursion:
// eslint-disable-next-line no-use-before-define
const { formulaIdentifier, value } = await provideValueForFormula(
evalFormula,
'eval',
const { formulaIdentifier, value } = await incarnateEval(
workerFormulaIdentifier,
source,
codeNames,
endowmentFormulaIdentifiers,
);
if (resultName !== undefined) {
await petStore.write(resultName, formulaIdentifier);
Expand Down

0 comments on commit 0bd689d

Please sign in to comment.