Skip to content

Commit

Permalink
Cleanup and define GeneratedRange
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Jun 24, 2024
1 parent b16650e commit 86b883c
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 52 deletions.
177 changes: 137 additions & 40 deletions src/scopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,73 @@ import {
encodeInteger,
hasMoreVlq,
indexOf,
maybeWrite,
maybeFlush,
posOut,
semicolon,
write,
td,
resetPos,
} from './vlq';

type Line = number;
type Column = number;
type Kind = number;
type Name = number;
type Var = number;

type OriginalScope =
| [Line, Column, Line, Column, Kind, Name]
| [Line, Column, Line, Column, Kind, Name, Var]
| [Line, Column, Line, Column, Kind, Name, Var, Var]
| [Line, Column, Line, Column, Kind, Name, Var, Var, Var]
| [Line, Column, Line, Column, Kind, Name, Var, Var, Var, Var]
| [Line, Column, Line, Column, Kind, Name, Var, Var, Var, Var, Var];
type SourcesIndex = number;
type ScopesIndex = number;

export type OriginalScope =
| {
0: Line;
1: Column;
2: Line;
3: Column;
4: Kind;
length: 5;
}
| {
0: Line;
1: Column;
2: Line;
3: Column;
4: Kind;
5: Name;
length: 6;
}
| {
0: Line;
1: Column;
2: Line;
3: Column;
4: Kind;
vars: Var[];
length: 5;
}
| {
0: Line;
1: Column;
2: Line;
3: Column;
4: Kind;
5: Name;
vars: Var[];
length: 6;
};

const NO_NAME = -1;
// const NO_SCOPE = -1;

export function decodeOriginalScopes(input: string): OriginalScope[] {
let line = 0;
let column = 0;
let namesIndex = 0;
let varsIndex = 0;
const scopes: OriginalScope[] = [];
const stack: OriginalScope[] = [];

for (let i = 0; i < input.length; i = posOut + 1) {
line = decodeInteger(input, i, line);
column = decodeInteger(input, posOut, column);
const column = decodeInteger(input, posOut, 0);

if (!hasMoreVlq(input, posOut, length)) {
if (!hasMoreVlq(input, posOut, input.length)) {
const last = stack.pop()!;
last[2] = line;
last[3] = column;
Expand All @@ -48,13 +80,19 @@ export function decodeOriginalScopes(input: string): OriginalScope[] {
const kind = decodeInteger(input, posOut, 0);
const fields = decodeInteger(input, posOut, 0);
const name = fields & 0b1 ? decodeInteger(input, posOut, namesIndex) : NO_NAME;
const scope: OriginalScope = [line, column, 0, 0, kind, name];
const scope: OriginalScope =
name === NO_NAME ? [line, column, 0, 0, kind] : [line, column, 0, 0, kind, name];
scopes.push(scope);
stack.push(scope);

const index = indexOf(input, ',', posOut);
while (posOut < index) {
varsIndex = decodeInteger(input, posOut, varsIndex);
scope.push(varsIndex);
if (posOut < index) {
const vars: Var[] = [];
(scope as any).vars = vars;
while (posOut < index) {
const varsIndex = decodeInteger(input, posOut, 0);
vars.push(varsIndex);
}
}
}

Expand All @@ -69,49 +107,108 @@ export function encodeOriginalScopes(scopes: OriginalScope[]): string {
const subLength = bufLength - (7 * 6 + 1);
const buf = new Uint8Array(bufLength);
const sub = buf.subarray(0, subLength);
let pos = 0;
resetPos();

const endStack: number[] = [0, 0];
let { 2: lastEndLine, 3: lastEndColumn } = scopes[scopes.length - 1];
const endStack: number[] = [];
let lastEndLine = scopes[0][2] + 1;
let lastEndColumn = scopes[0][3];
let line = 0;
let column = 0;
let namesIndex = 0;
let varsIndex = 0;

for (let i = 0; i < scopes.length; i++, pos = posOut) {
for (let i = 0; i < scopes.length; i++) {
const scope = scopes[i];
const { 0: startLine, 1: startColumn, 2: endLine, 3: endColumn, 4: name } = scope;
const { 0: startLine, 1: startColumn, 2: endLine, 3: endColumn, 4: kind } = scope;
const name = scope.length === 6 ? scope[5] : NO_NAME;
const vars = 'vars' in scope ? scope.vars! : [];

out = maybeWrite(out, buf, pos, buf, bufLength);
pos = posOut;
if (i > 0) buf[pos++] = semicolon;
out = maybeFlush(out, buf, posOut, buf, bufLength);
if (i > 0) write(buf, posOut, comma);

while (startLine > lastEndLine || (startLine === lastEndLine && startColumn >= lastEndColumn)) {
out = maybeWrite(out, sub, pos, buf, subLength);
out = maybeFlush(out, sub, posOut, buf, subLength);

line = encodeInteger(buf, posOut, lastEndLine, line);
column = encodeInteger(buf, posOut, lastEndColumn, column);
pos = posOut;
buf[pos++] = comma;
encodeInteger(buf, posOut, lastEndColumn, 0);
write(buf, posOut, comma);

lastEndColumn = endStack.pop()!;
lastEndLine = endStack.pop()!;
}

line = encodeInteger(buf, pos, startLine, line);
column = encodeInteger(buf, posOut, startColumn, column);
endStack.push(endLine);
endStack.push(endColumn);
line = encodeInteger(buf, posOut, startLine, line);
encodeInteger(buf, posOut, startColumn, 0);
endStack.push(lastEndLine);
endStack.push(lastEndColumn);
lastEndLine = endLine;
lastEndColumn = endColumn;

encodeInteger(buf, posOut, kind, 0);
encodeInteger(buf, posOut, name === NO_NAME ? 0 : 1, 0);
if (name !== NO_NAME) namesIndex = encodeInteger(buf, posOut, name, namesIndex);

for (let j = 5; j < scope.length; j++) {
out = maybeWrite(out, sub, pos, buf, subLength);
varsIndex = encodeInteger(buf, posOut, scope[j], varsIndex);
for (const v of vars) {
out = maybeFlush(out, sub, posOut, buf, subLength);
encodeInteger(buf, posOut, v, 0);
}
}
while (endStack.length > 0) {}
while (endStack.length > 0) {
out = maybeFlush(out, sub, posOut, buf, subLength);

write(buf, posOut, comma);
line = encodeInteger(buf, posOut, lastEndLine, line);
encodeInteger(buf, posOut, lastEndColumn, 0);

lastEndColumn = endStack.pop()!;
lastEndLine = endStack.pop()!;
}

return out + td.decode(buf.subarray(0, posOut));
}

export type GeneratedRange =
| {
0: Line;
1: Column;
2: Line;
3: Column;
4: SourcesIndex;
5: ScopesIndex;
}
| {
0: Line;
1: Column;
2: Line;
3: Column;
4: SourcesIndex;
5: ScopesIndex;
callsite: CallSite;
}
| {
0: Line;
1: Column;
2: Line;
3: Column;
4: SourcesIndex;
5: ScopesIndex;
bindings: ExpressionBinding[];
}
| {
0: Line;
1: Column;
2: Line;
3: Column;
4: SourcesIndex;
5: ScopesIndex;
callsite: CallSite;
bindings: ExpressionBinding[];
};
export type CallSite = [SourcesIndex, Line, Column];
export type ExpressionBinding = [Name] | [Name, Line, Column];

export function decodeGeneratedRanges(input: string): GeneratedRange[] {
return [];
}

export function encodeGeneratedRanges(ranges: GeneratedRange[]): string {
return '';
}
22 changes: 11 additions & 11 deletions src/sourcemap-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
posOut,
indexOf,
td,
maybeWrite,
maybeFlush,
write,
resetPos,
} from './vlq';
// export { decodeOriginalScopes } from './scopes';

Expand Down Expand Up @@ -84,7 +86,7 @@ export function encode(decoded: Readonly<SourceMapMappings>): string {
const subLength = bufLength - (7 * 5 + 1);
const buf = new Uint8Array(bufLength);
const sub = buf.subarray(0, subLength);
let pos = 0;
resetPos();
let out = '';
let genColumn = 0;
let sourcesIndex = 0;
Expand All @@ -94,21 +96,19 @@ export function encode(decoded: Readonly<SourceMapMappings>): string {

for (let i = 0; i < decoded.length; i++) {
const line = decoded[i];
out = maybeWrite(out, buf, pos, buf, bufLength);
pos = posOut;
if (i > 0) buf[pos++] = semicolon;
out = maybeFlush(out, buf, posOut, buf, bufLength);
if (i > 0) write(buf, posOut, semicolon);

if (line.length === 0) continue;

genColumn = 0;

for (let j = 0; j < line.length; j++, pos = posOut) {
for (let j = 0; j < line.length; j++) {
const segment = line[j];
out = maybeWrite(out, sub, pos, buf, subLength);
pos = posOut;
if (j > 0) buf[pos++] = comma;
out = maybeFlush(out, sub, posOut, buf, subLength);
if (j > 0) write(buf, posOut, comma);

genColumn = encodeInteger(buf, pos, segment[0], genColumn);
genColumn = encodeInteger(buf, posOut, segment[0], genColumn);

if (segment.length === 1) continue;
sourcesIndex = encodeInteger(buf, posOut, segment[1], sourcesIndex);
Expand All @@ -120,5 +120,5 @@ export function encode(decoded: Readonly<SourceMapMappings>): string {
}
}

return out + td.decode(buf.subarray(0, pos));
return out + td.decode(buf.subarray(0, posOut));
}
11 changes: 10 additions & 1 deletion src/vlq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export function indexOf(mappings: string, char: string, index: number): number {
}

export let posOut = 0;
export function resetPos() {
posOut = 0;
}

export function decodeInteger(mappings: string, pos: number, relative: number): number {
let value = 0;
let shift = 0;
Expand Down Expand Up @@ -60,7 +64,7 @@ export function encodeInteger(buf: Uint8Array, pos: number, num: number, relativ
return num;
}

export function maybeWrite(
export function maybeFlush(
build: string,
buf: Uint8Array,
pos: number,
Expand All @@ -77,6 +81,11 @@ export function maybeWrite(
return build + out;
}

export function write(buf: Uint8Array, pos: number, value: number) {
buf[pos] = value;
posOut = pos + 1;
}

// Provide a fallback for older environments.
export const td =
typeof TextDecoder !== 'undefined'
Expand Down

0 comments on commit 86b883c

Please sign in to comment.