1// Copyright 2014 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import {GNode, DEFAULT_NODE_BUBBLE_RADIUS} from "./node.js" 6 7export const MINIMUM_EDGE_SEPARATION = 20; 8 9export function isEdgeInitiallyVisible(target, index, source, type) { 10 return type == "control" && (target.cfg || source.cfg); 11} 12 13export class Edge { 14 target: GNode; 15 source: GNode; 16 index: number; 17 type: String; 18 backEdgeNumber: number; 19 visible: boolean; 20 21 constructor(target: GNode, index: number, source: GNode, type: string) { 22 this.target = target; 23 this.source = source; 24 this.index = index; 25 this.type = type; 26 this.backEdgeNumber = 0; 27 this.visible = isEdgeInitiallyVisible(target, index, source, type); 28 } 29 30 31 stringID() { 32 return this.source.id + "," + this.index + "," + this.target.id; 33 }; 34 35 isVisible() { 36 return this.visible && this.source.visible && this.target.visible; 37 }; 38 39 getInputHorizontalPosition(graph) { 40 if (this.backEdgeNumber > 0) { 41 return graph.maxGraphNodeX + this.backEdgeNumber * MINIMUM_EDGE_SEPARATION; 42 } 43 var source = this.source; 44 var target = this.target; 45 var index = this.index; 46 var input_x = target.x + target.getInputX(index); 47 var inputApproach = target.getInputApproach(this.index); 48 var outputApproach = source.getOutputApproach(graph); 49 if (inputApproach > outputApproach) { 50 return input_x; 51 } else { 52 var inputOffset = MINIMUM_EDGE_SEPARATION * (index + 1); 53 return (target.x < source.x) 54 ? (target.x + target.getTotalNodeWidth() + inputOffset) 55 : (target.x - inputOffset) 56 } 57 } 58 59 generatePath(graph) { 60 var target = this.target; 61 var source = this.source; 62 var input_x = target.x + target.getInputX(this.index); 63 var arrowheadHeight = 7; 64 var input_y = target.y - 2 * DEFAULT_NODE_BUBBLE_RADIUS - arrowheadHeight; 65 var output_x = source.x + source.getOutputX(); 66 var output_y = source.y + graph.getNodeHeight(source) + DEFAULT_NODE_BUBBLE_RADIUS; 67 var inputApproach = target.getInputApproach(this.index); 68 var outputApproach = source.getOutputApproach(graph); 69 var horizontalPos = this.getInputHorizontalPosition(graph); 70 71 var result = "M" + output_x + "," + output_y + 72 "L" + output_x + "," + outputApproach + 73 "L" + horizontalPos + "," + outputApproach; 74 75 if (horizontalPos != input_x) { 76 result += "L" + horizontalPos + "," + inputApproach; 77 } else { 78 if (inputApproach < outputApproach) { 79 inputApproach = outputApproach; 80 } 81 } 82 83 result += "L" + input_x + "," + inputApproach + 84 "L" + input_x + "," + input_y; 85 return result; 86 } 87 88 isBackEdge() { 89 return this.target.hasBackEdges() && (this.target.rank < this.source.rank); 90 } 91 92} 93 94export const edgeToStr = (e: Edge) => e.stringID(); 95