Skip to content

Commit

Permalink
feat: Enforce order of the statements
Browse files Browse the repository at this point in the history
Slangroom statement will be exectuted in the order they are written
such that it is possible to use data from a previous computation.

All slangroom statement should be written before (for given) or
after (for then) the zenroom statements. This is because, at the
moment, we cannot start executing zenroom and suspend its execution
before each slangroom statement (maybe it will be possible with
improvement on exportable heap in zenroom).
  • Loading branch information
albertolerda committed Sep 18, 2023
1 parent 1f385b5 commit d729101
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
4 changes: 3 additions & 1 deletion pkg/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export class BeforePlugin {
* named [execute], which takes in the necessary parameters from [AfterParams].
*/
export class AfterPlugin {
constructor(readonly execute: (params: AfterParams) => Promise<void> | void) { }
constructor(readonly execute: (params: AfterParams
) => Promise<void> | void | Promise<JsonableObject> | JsonableObject
) { }
}

/**
Expand Down
36 changes: 13 additions & 23 deletions pkg/core/src/slangroom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,41 +56,31 @@ export class Slangroom {
*/
async execute(contract: string, params?: ZenroomParams): Promise<ZenroomOutput> {
const ignoreds = await getIgnoredStatements(contract, params);
params = params || {data: {}, keys: {}}

// TODO: remove the statements when they match (decide how)
const befores: ReturnType<BeforePlugin['execute']>[] = [];
for (const b of this._beforeExecution) {
for (const ignored of ignoreds) {
befores.push(
b.execute({
statement: ignored,
params: params,
})
);
const res = await b.execute({
statement: ignored,
params: params,
})
params.data = Object.assign(params.data || {}, res?.data)
}
}

const generatedParams = await Promise.all(befores);
const mergedParams = generatedParams.reduce((acc: ZenroomParams, x) => {
acc.data = Object.assign(acc.data || {}, x?.data);
acc.keys = Object.assign(acc.keys || {}, x?.keys);
return acc;
}, params || {});
const zout = await zencodeExec(contract, mergedParams);
const zout = await zencodeExec(contract, params);

const afters: ReturnType<AfterPlugin['execute']>[] = [];
for (const a of this._afterExecution) {
for (const ignored of ignoreds) {
afters.push(
a.execute({
statement: ignored,
result: zout.result,
params: params,
})
);
const res = await a.execute({
statement: ignored,
result: zout.result,
params: params,
})
zout.result = Object.assign(zout.result || {}, res)
}
}
await Promise.all(afters);

return zout;
}
Expand Down

0 comments on commit d729101

Please sign in to comment.