Skip to content

Commit a2f6166

Browse files
authored
feat-fix(dfg): fromJson includes environments (#1417)
1 parent a9c0796 commit a2f6166

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/dataflow/graph/graph.ts

+40-2
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ import type {
1212
import { VertexType } from './vertex';
1313
import { arrayEqual } from '../../util/arrays';
1414
import { EmptyArgument } from '../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
15-
import type { IdentifierDefinition, IdentifierReference } from '../environments/identifier';
15+
import type { Identifier, IdentifierDefinition, IdentifierReference } from '../environments/identifier';
1616
import type { NodeId } from '../../r-bridge/lang-4.x/ast/model/processing/node-id';
1717
import { normalizeIdToNumberIfPossible } from '../../r-bridge/lang-4.x/ast/model/processing/node-id';
18-
import type { REnvironmentInformation } from '../environments/environment';
18+
import type { EnvironmentMemory, IEnvironment, REnvironmentInformation } from '../environments/environment';
1919
import { initializeCleanEnvironments } from '../environments/environment';
2020
import type { AstIdMap } from '../../r-bridge/lang-4.x/ast/model/processing/decorate';
2121
import { cloneEnvironmentInformation } from '../environments/clone';
2222
import { jsonReplacer } from '../../util/json';
2323
import { BuiltIn } from '../environments/built-in';
2424
import { dataflowLogger } from '../logger';
2525
import type { LinkTo } from '../../queries/catalog/call-context-query/call-context-query-format';
26+
import type { Writable } from 'ts-essentials';
2627

2728
/**
2829
* Describes the information we store per function body.
@@ -461,6 +462,11 @@ export class DataflowGraph<
461462
const graph = new DataflowGraph(undefined);
462463
graph.rootVertices = new Set<NodeId>(data.rootVertices);
463464
graph.vertexInformation = new Map<NodeId, DataflowGraphVertexInfo>(data.vertexInformation);
465+
for(const [, vertex] of graph.vertexInformation) {
466+
if(vertex.environment) {
467+
(vertex.environment as Writable<REnvironmentInformation>) = renvFromJson(vertex.environment as unknown as REnvironmentInformationJson);
468+
}
469+
}
464470
graph.edgeInformation = new Map<NodeId, OutgoingEdges>(data.edgeInformation.map(([id, edges]) => [id, new Map<NodeId, DataflowGraphEdge>(edges)]));
465471
if(data.sourced) {
466472
graph._sourced = data.sourced;
@@ -495,3 +501,35 @@ function extractEdgeIds(from: NodeId | ReferenceForEdge, to: NodeId | ReferenceF
495501
const toId = typeof to === 'object' ? to.nodeId : to;
496502
return [fromId, toId];
497503
}
504+
505+
export interface IEnvironmentJson {
506+
readonly id: number;
507+
parent: IEnvironmentJson;
508+
memory: Record<Identifier, IdentifierDefinition[]>;
509+
}
510+
511+
interface REnvironmentInformationJson {
512+
readonly current: IEnvironmentJson;
513+
readonly level: number;
514+
}
515+
516+
function envFromJson(json: IEnvironmentJson): IEnvironment {
517+
const parent = json.parent ? envFromJson(json.parent) : undefined;
518+
const memory: EnvironmentMemory = new Map();
519+
for(const [key, value] of Object.entries(json.memory)) {
520+
memory.set(key as Identifier, value);
521+
}
522+
return {
523+
id: json.id,
524+
parent: parent as IEnvironment,
525+
memory
526+
};
527+
}
528+
529+
function renvFromJson(json: REnvironmentInformationJson): REnvironmentInformation {
530+
const current = envFromJson(json.current);
531+
return {
532+
current,
533+
level: json.level
534+
};
535+
}

0 commit comments

Comments
 (0)