Skip to content
Merged
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
18 changes: 13 additions & 5 deletions src/main/compiler/CompilerScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ export class CompilerScope {
return this.emitParamNode(node, resSym);
case '@system/Input':
return this.emitInputNode(node, resSym);
case '@system/Scope':
return this.emitScopeNode(node, resSym);
case '@system/Result':
case '@system/Output':
return this.emitOutputNode(node, resSym);
Expand Down Expand Up @@ -265,7 +267,13 @@ export class CompilerScope {
}

private emitInputNode(node: NodeView, resSym: string) {
this.code.line(`${resSym} = params;`);
this.code.line(`${resSym} = { ...params, ...ctx.getScopeData() };`);
// TODO phase out the workaround after subgraphs are migrated to Scope + Param nodes
// this.code.line(`${resSym} = params;`);
}

private emitScopeNode(node: NodeView, resSym: string) {
this.code.line(`${resSym} = ctx.getScopeData();`);
}

private emitOutputNode(node: NodeView, resSym: string) {
Expand Down Expand Up @@ -367,14 +375,14 @@ export class CompilerScope {
const sym = this.symbols.getNodeSym(rootNode.nodeUid);
if (this.options.introspect) {
return [
`(params, ctx) => {`,
`ctx.scopeCaptured.emit({ nodeUid: ${JSON.stringify(node.nodeUid)}, params });`,
`(scopeData, ctx) => {`,
`ctx.scopeCaptured.emit({ nodeUid: ${JSON.stringify(node.nodeUid)}, params: scopeData });`,
`ctx.checkPendingNode(${JSON.stringify(node.nodeUid)});`,
`return ${sym}(params, ctx);`,
`return ${sym}(params, ctx.setScopeData(scopeData));`,
`}`,
].join('');
}
return sym;
return `(scopeData, ctx) => ${sym}(params, ctx.setScopeData(scopeData))`;
}

private emitNodeProps(node: NodeView) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/runtime/GraphEvalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class GraphEvalContext implements t.GraphEvalContext {
// Locals are stored per-context. Lookups delegate up the hierarchy.
locals = new Map<string, any>();

scopeData: any = undefined;

constructor(
readonly parent: GraphEvalContext | null = null,
) {
Expand Down Expand Up @@ -68,6 +70,15 @@ export class GraphEvalContext implements t.GraphEvalContext {
return new GraphEvalContext(this);
}

getScopeData() {
return this.scopeData;
}

setScopeData(data: any) {
this.scopeData = data;
return this;
}

convertType(value: unknown, schema: SchemaSpec) {
return new Schema(schema as any).decode(value);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/runtime/ModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export abstract class GenericModuleLoader implements ModuleLoader {
this.addModule('@system/Output', systemModules.Output);
this.addModule('@system/Param', systemModules.Param);
this.addModule('@system/Result', systemModules.Output);
this.addModule('@system/Scope', systemModules.Scope);
}

abstract resolveComputeUrl(ref: string): string;
Expand Down
17 changes: 17 additions & 0 deletions src/main/system/Scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ModuleSpecSchema } from '../schema/ModuleSpec.js';

export const Scope = ModuleSpecSchema.create({
moduleName: 'Scope',
version: '0.0.0',
params: {
},
result: {
schema: { type: 'any' },
},
cacheMode: 'never',
attributes: {
hidden: true,
hideEvalControls: true,
forceColor: 'cyan',
}
});
1 change: 1 addition & 0 deletions src/main/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './Frame.js';
export * from './Input.js';
export * from './Output.js';
export * from './Param.js';
export * from './Scope.js';
2 changes: 2 additions & 0 deletions src/main/types/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface GraphEvalContext {
clear(): void;
finalize(): Promise<void>;
newScope(): GraphEvalContext;
getScopeData(): any;
setScopeData(data: any): this;
checkPendingNode(nodeUid: string): void;

getLocal<T>(key: string, defaultValue?: T): T | undefined;
Expand Down
Loading