|
| 1 | +package com.algorand.algosdk.logic; |
| 2 | + |
| 3 | +import java.lang.Integer; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | + |
| 7 | +/** |
| 8 | + * SourceMap class provides parser for source map from |
| 9 | + * algod compile endpoint |
| 10 | + */ |
| 11 | +public class SourceMap { |
| 12 | + |
| 13 | + public int version; |
| 14 | + public String file; |
| 15 | + public String[] sources; |
| 16 | + public String[] names; |
| 17 | + public String mappings; |
| 18 | + |
| 19 | + public HashMap<Integer, Integer> pcToLine; |
| 20 | + public HashMap<Integer, ArrayList<Integer>> lineToPc; |
| 21 | + |
| 22 | + public SourceMap(HashMap<String,Object> sourceMap) { |
| 23 | + int version = (int) sourceMap.get("version"); |
| 24 | + if(version != 3){ |
| 25 | + throw new IllegalArgumentException("Only source map version 3 is supported"); |
| 26 | + } |
| 27 | + this.version = version; |
| 28 | + |
| 29 | + this.file = (String) sourceMap.get("file"); |
| 30 | + this.mappings = (String) sourceMap.get("mappings"); |
| 31 | + |
| 32 | + this.lineToPc = new HashMap<>(); |
| 33 | + this.pcToLine = new HashMap<>(); |
| 34 | + |
| 35 | + Integer lastLine = 0; |
| 36 | + String[] vlqs = this.mappings.split(";"); |
| 37 | + for(int i=0; i<vlqs.length; i++){ |
| 38 | + ArrayList<Integer> vals = VLQDecoder.decodeSourceMapLine(vlqs[i]); |
| 39 | + |
| 40 | + // If the vals length >= 3 the lineDelta |
| 41 | + if(vals.size() >= 3){ |
| 42 | + lastLine = lastLine + vals.get(2); |
| 43 | + } |
| 44 | + |
| 45 | + if(!this.lineToPc.containsKey(lastLine)){ |
| 46 | + this.lineToPc.put(lastLine, new ArrayList<Integer>()); |
| 47 | + } |
| 48 | + |
| 49 | + ArrayList<Integer> currList = this.lineToPc.get(lastLine); |
| 50 | + currList.add(i); |
| 51 | + this.lineToPc.put(lastLine, currList); |
| 52 | + |
| 53 | + this.pcToLine.put(i, lastLine); |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns the Integer line number for the passed PC or null if not found |
| 60 | + * @param pc the pc (program counter) of the assembled file |
| 61 | + * @return the line number or null if not found |
| 62 | + */ |
| 63 | + public Integer getLineForPc(Integer pc) { |
| 64 | + return this.pcToLine.get(pc); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the List of PCs for the passed line number |
| 69 | + * @param line the line number of the source file |
| 70 | + * @return the list of PCs that line generated or empty array if not found |
| 71 | + */ |
| 72 | + public ArrayList<Integer> getPcsForLine(Integer line) { |
| 73 | + if(!this.pcToLine.containsKey(line)){ |
| 74 | + return new ArrayList<Integer>(); |
| 75 | + } |
| 76 | + return this.lineToPc.get(line); |
| 77 | + } |
| 78 | + |
| 79 | + private static class VLQDecoder { |
| 80 | + // VLQDecoder for decoding the VLQ values returned for source map |
| 81 | + // based on the decoder implementation here: https://github.com/algorand/go-algorand-sdk/blob/develop/logic/source_map.go |
| 82 | + |
| 83 | + private static final String b64table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 84 | + private static final int vlqShiftSize = 5; |
| 85 | + private static final int vlqFlag = 1 << vlqShiftSize; |
| 86 | + private static final int vlqMask = vlqFlag - 1; |
| 87 | + |
| 88 | + public static ArrayList<Integer> decodeSourceMapLine(String vlq) { |
| 89 | + |
| 90 | + ArrayList<Integer> results = new ArrayList<>(); |
| 91 | + int value = 0; |
| 92 | + int shift = 0; |
| 93 | + |
| 94 | + for(int i=0; i<vlq.length(); i++){ |
| 95 | + int digit = b64table.indexOf(vlq.charAt(i)); |
| 96 | + |
| 97 | + value |= (digit & vlqMask) << shift; |
| 98 | + |
| 99 | + if((digit & vlqFlag) > 0) { |
| 100 | + shift += vlqShiftSize; |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + if((value&1)>0){ |
| 105 | + value = (value >> 1) * -1; |
| 106 | + }else{ |
| 107 | + value = value >> 1; |
| 108 | + } |
| 109 | + |
| 110 | + results.add(value); |
| 111 | + |
| 112 | + // Reset |
| 113 | + value = 0; |
| 114 | + shift = 0; |
| 115 | + } |
| 116 | + |
| 117 | + return results; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments