Skip to content

Commit

Permalink
Backwards compatbility
Browse files Browse the repository at this point in the history
  • Loading branch information
phschaad committed Jan 31, 2024
1 parent 4fa299e commit ff420dc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spcl/sdfv",
"version": "1.1.5",
"version": "1.1.6",
"description": "A standalone viewer for SDFGs",
"homepage": "https://github.com/spcl/dace-webclient",
"main": "out/index.js",
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Point2D,
SDFVTooltipFunc,
SimpleRect,
checkCompatSave,
stringify_sdfg
} from '../index';
import { SMLayouter } from '../layouter/state_machine/sm_layouter';
Expand Down Expand Up @@ -1365,8 +1366,10 @@ export class SDFGRenderer extends EventEmitter {

public save_sdfg(): void {
const name = this.sdfg.attributes.name;
const contents = 'data:text/json;charset=utf-8,' +
encodeURIComponent(stringify_sdfg(this.sdfg));
const sdfgString = stringify_sdfg(checkCompatSave(this.sdfg));
const contents = 'data:text/json;charset=utf-8,' + encodeURIComponent(
sdfgString
);
this.save(name + '.sdfg', contents);
}

Expand Down
6 changes: 3 additions & 3 deletions src/sdfv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
State
} from './renderer/renderer_elements';
import { htmlSanitize } from './utils/sanitization';
import { parse_sdfg, stringify_sdfg } from './utils/sdfg/json_serializer';
import { checkCompatLoad, parse_sdfg, stringify_sdfg } from './utils/sdfg/json_serializer';
import { SDFVSettings } from './utils/sdfv_settings';

declare const vscode: any;
Expand Down Expand Up @@ -503,7 +503,7 @@ function file_read_complete(sdfv: SDFV): void {
const result_string = fr.result;
const container = document.getElementById('contents');
if (result_string && container) {
const sdfg = parse_sdfg(result_string);
const sdfg = checkCompatLoad(parse_sdfg(result_string));
sdfv.get_renderer()?.destroy();
sdfv.set_renderer(new SDFGRenderer(sdfv, sdfg, container, mouse_event));
sdfv.close_menu();
Expand Down Expand Up @@ -632,7 +632,7 @@ function load_sdfg_from_url(sdfv: SDFV, url: string): void {
request.responseType = 'text'; // Will be parsed as JSON by parse_sdfg
request.onload = () => {
if (request.status === 200) {
const sdfg = parse_sdfg(request.response);
const sdfg = checkCompatLoad(parse_sdfg(request.response));
sdfv.get_renderer()?.destroy();
init_sdfv(sdfg, null, false, null);
} else {
Expand Down
66 changes: 65 additions & 1 deletion src/utils/sdfg/json_serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,75 @@ import { Edge, JsonSDFG } from '../../index';
import { gunzipSync } from 'zlib';
import { Buffer } from 'buffer';

const propertyReplacements_0_16_0: { [key: string]: {
replaceWith: string,
recursive: boolean,
}} = {
'start_state': {
replaceWith: 'start_block',
recursive: false,
},
'sdfg_list_id': {
replaceWith: 'cfg_list_id',
recursive: true,
},
};

function propertyReplace(obj: any, fromName: string, toName: string): void {
if (Object.hasOwn(obj, fromName)) {
const prop = Object.getOwnPropertyDescriptor(obj, fromName)!;
Object.defineProperty(obj, toName, prop);
delete obj[fromName];
}
}

function makeCompat(sdfg: any, direction: 'in' | 'out'): void {
if (sdfg.dace_version && sdfg.dace_version < '0.16.0') {
for (const k in propertyReplacements_0_16_0) {
const v = propertyReplacements_0_16_0[k];
if (v.recursive) {
const recurse = (el: {
nodes?: any[],
edges?: any[],
attributes?: { sdfg?: any },
}) => {
if (direction === 'in')
propertyReplace(el, k, v.replaceWith);
else
propertyReplace(el, v.replaceWith, k);
el.nodes?.forEach(recurse);
el.edges?.forEach(recurse);
if (el.attributes?.sdfg)
recurse(el.attributes.sdfg);
};
recurse(sdfg);
} else {
if (direction === 'in')
propertyReplace(sdfg, k, v.replaceWith);
else
propertyReplace(sdfg, v.replaceWith, k);
}
}
}
}

export function checkCompatLoad(sdfg: JsonSDFG): JsonSDFG {
makeCompat(sdfg, 'in');
return sdfg;
}

export function checkCompatSave(sdfg: JsonSDFG): JsonSDFG {
makeCompat(sdfg, 'out');
return sdfg;
}

export function read_or_decompress(
json: string | ArrayBuffer
): [string, boolean] {
try {
return [new TextDecoder().decode(gunzipSync(Buffer.from(json as Uint8Array))), true];
return [new TextDecoder().decode(
gunzipSync(Buffer.from(json as Uint8Array))
), true];
} catch {
if (typeof json !== 'string') {
const enc = new TextDecoder('utf-8');
Expand Down

0 comments on commit ff420dc

Please sign in to comment.