From 82f01b3fcdc9af50bf3e5f50258cbf1071be1baf Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Sun, 8 Jul 2018 09:07:19 -0500 Subject: [PATCH] moved some types to workertypes.ts --- index.html | 1 + src/project.ts | 80 +-------------------------------------- src/ui.ts | 39 +++++++++---------- src/views.ts | 3 +- src/windows.ts | 3 +- src/workertypes.ts | 86 ++++++++++++++++++++++++++++++++++++++++++ test/cli/teststore.js | 1 + test/cli/testworker.js | 1 + tsconfig.json | 3 +- 9 files changed, 116 insertions(+), 101 deletions(-) create mode 100644 src/workertypes.ts diff --git a/index.html b/index.html index 95be7d6e..00c45067 100644 --- a/index.html +++ b/index.html @@ -248,6 +248,7 @@ function require(modname) { + diff --git a/src/project.ts b/src/project.ts index 07d50f71..d33c996b 100644 --- a/src/project.ts +++ b/src/project.ts @@ -1,84 +1,6 @@ "use strict"; -type FileData = string | Uint8Array; - -export interface SourceLine { - offset:number; - line:number; - insns?:string; - iscode?:boolean; -} - -export interface Dependency { - path:string, - filename:string, - data:FileData // TODO: or binary? -} - -export interface WorkerFileUpdate { path:string, data:FileData }; -export interface WorkerBuildStep { path:string, platform:string, tool:string, mainfile?:boolean }; - -export interface WorkerMessage { - updates:WorkerFileUpdate[], - buildsteps:WorkerBuildStep[] -} - -export interface WorkerError { - line:number, - msg:string, - path?:string - //TODO -} - -export class SourceFile { - lines: SourceLine[]; - text: string; - offset2line: {[offset:number]:number}; - line2offset: {[line:number]:number}; - - constructor(lines:SourceLine[], text?:string) { - lines = lines || []; - this.lines = lines; - this.text = text; - this.offset2line = {}; - this.line2offset = {}; - for (var info of lines) { - if (info.offset >= 0) { - this.offset2line[info.offset] = info.line; - this.line2offset[info.line] = info.offset; - } - } - } - findLineForOffset(PC:number):number { - if (this.offset2line) { - for (var i=0; i<16; i++) { - var line = this.offset2line[PC]; - if (line >= 0) { - return line; - } - PC--; - } - } - return null; - } - lineCount():number { return this.lines.length; } -} - -interface CodeListing { - lines:SourceLine[], - asmlines:SourceLine[], - text:string, - sourcefile:SourceFile, - assemblyfile:SourceFile -} - -type CodeListingMap = {[path:string]:CodeListing}; - -interface WorkerResult { - output:Uint8Array, //TODO - errors:WorkerError[], - listings:CodeListingMap, -} +import { FileData, Dependency, SourceLine, SourceFile, CodeListing, CodeListingMap, WorkerError, WorkerResult } from "./workertypes"; type BuildResultCallback = (result:WorkerResult) => void; type BuildStatusCallback = (busy:boolean) => void; diff --git a/src/ui.ts b/src/ui.ts index ed6f6226..0a10e9bb 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -4,7 +4,8 @@ import $ = require("jquery"); import * as bootstrap from "bootstrap"; -import { SourceFile, CodeProject } from "./project"; +import { CodeProject } from "./project"; +import { WorkerResult, SourceFile } from "./workertypes"; import { ProjectWindows } from "./windows"; import { Platform, Preset } from "./baseplatform"; import * as Views from "./views"; @@ -43,7 +44,7 @@ var TOOL_TO_SOURCE_STYLE = { 'jsasm': 'z80' } -function newWorker() { +function newWorker() : Worker { return new Worker("./src/worker/workermain.js"); } @@ -60,21 +61,21 @@ var store; // persistent store var lastDebugInfo; // last debug info (CPU text) var lastDebugState; // last debug state (object) -function inspectVariable(ed, name) { +function inspectVariable(ed, name) { // TODO: ed? var val; if (platform.inspect) { platform.inspect(name); } } -function getCurrentPresetTitle() { +function getCurrentPresetTitle() : string { if (!current_preset_entry) return "ROM"; else return current_preset_entry.title || current_preset_entry.name || "ROM"; } -function setLastPreset(id) { +function setLastPreset(id:string) { if (platform_id != 'base_z80') { // TODO localStorage.setItem("__lastplatform", platform_id); localStorage.setItem("__lastid_"+platform_id, id); @@ -125,7 +126,7 @@ function refreshWindowList() { } } - function loadEditor(path) { + function loadEditor(path:string) { var tool = platform.getToolForFilename(path); var mode = tool && TOOL_TO_SOURCE_STYLE[tool]; return new Views.SourceEditor(path, mode); @@ -170,7 +171,7 @@ function refreshWindowList() { } // can pass integer or string id -function loadProject(preset_id) { +function loadProject(preset_id:string) { var index = parseInt(preset_id+""); // might fail -1 for (var i=0; i 0) { projectWindows.setErrors(data.errors); @@ -387,7 +388,7 @@ function setCompileOutput(data) { compparams = data.params; // load ROM var rom = data.output; - if (rom) { + if (rom instanceof Uint8Array) { try { //console.log("Loading ROM length", rom.length); platform.loadROM(getCurrentPresetTitle(), rom); @@ -427,7 +428,7 @@ function showMemory(state?) { } } -function setDebugButtonState(btnid, btnstate) { +function setDebugButtonState(btnid:string, btnstate:string) { $("#debug_bar").find("button").removeClass("btn_active").removeClass("btn_stopped"); $("#dbg_"+btnid).addClass("btn_"+btnstate); } @@ -483,7 +484,7 @@ function singleFrameStep() { platform.runToVsync(); } -function getEditorPC() { +function getEditorPC() : number { var wnd = projectWindows.getActive(); return wnd && wnd.getCursorPC && wnd.getCursorPC(); } @@ -545,7 +546,7 @@ function _breakExpression() { } } -function getSymbolAtAddress(a) { +function getSymbolAtAddress(a : number) { if (addr2symbol[a]) return addr2symbol[a]; var i=0; while (--a >= 0) { @@ -607,7 +608,7 @@ function _recordVideo() { f(); } -function setFrameRateUI(fps) { +function setFrameRateUI(fps:number) { platform.setFrameRate(fps); if (fps > 0.01) $("#fps_label").text(fps.toFixed(2)); @@ -859,7 +860,7 @@ function startPlatform() { } } -function loadSharedFile(sharekey) { +function loadSharedFile(sharekey : string) { var github = new Octokat(); var gist = github.gists(sharekey); gist.fetch().done(function(val) { @@ -881,7 +882,7 @@ function loadSharedFile(sharekey) { } // start -function startUI(loadplatform) { +function startUI(loadplatform : boolean) { installErrorHandler(); // add default platform? platform_id = qs['platform'] || localStorage.getItem("__lastplatform"); diff --git a/src/views.ts b/src/views.ts index 55cbb737..48d9ec68 100644 --- a/src/views.ts +++ b/src/views.ts @@ -1,7 +1,8 @@ "use strict"; import $ = require("jquery"); -import { SourceFile, WorkerError, CodeProject } from "./project"; +import { CodeProject } from "./project"; +import { SourceFile, WorkerError } from "./workertypes"; import { Platform } from "./baseplatform"; export interface ProjectView { diff --git a/src/windows.ts b/src/windows.ts index ad683290..eafd21ed 100644 --- a/src/windows.ts +++ b/src/windows.ts @@ -1,7 +1,8 @@ "use strict"; import $ = require("jquery"); -import { WorkerError, CodeProject } from "./project"; +import { CodeProject } from "./project"; +import { WorkerError } from "./workertypes"; import { ProjectView } from "./views"; type WindowCreateFunction = (id:string) => ProjectView; diff --git a/src/workertypes.ts b/src/workertypes.ts new file mode 100644 index 00000000..cf5dfb82 --- /dev/null +++ b/src/workertypes.ts @@ -0,0 +1,86 @@ + +export type FileData = string | Uint8Array; + +export interface SourceLine { + offset:number; + line:number; + insns?:string; + iscode?:boolean; +} + +export class SourceFile { + lines: SourceLine[]; + text: string; + offset2line: {[offset:number]:number}; + line2offset: {[line:number]:number}; + + constructor(lines:SourceLine[], text?:string) { + lines = lines || []; + this.lines = lines; + this.text = text; + this.offset2line = {}; + this.line2offset = {}; + for (var info of lines) { + if (info.offset >= 0) { + this.offset2line[info.offset] = info.line; + this.line2offset[info.line] = info.offset; + } + } + } + findLineForOffset(PC:number):number { + if (this.offset2line) { + for (var i=0; i<16; i++) { + var line = this.offset2line[PC]; + if (line >= 0) { + return line; + } + PC--; + } + } + return null; + } + lineCount():number { return this.lines.length; } +} + +export interface Dependency { + path:string, + filename:string, + data:FileData // TODO: or binary? +} + +export interface WorkerFileUpdate { path:string, data:FileData }; +export interface WorkerBuildStep { path:string, platform:string, tool:string, mainfile?:boolean }; + +export interface WorkerMessage { + updates:WorkerFileUpdate[], + buildsteps:WorkerBuildStep[] +} + +export interface WorkerError { + line:number, + msg:string, + path?:string + //TODO +} + +export interface CodeListing { + lines:SourceLine[], + asmlines:SourceLine[], + text:string, + sourcefile?:SourceFile, + assemblyfile?:SourceFile +} + +export type CodeListingMap = {[path:string]:CodeListing}; + +// TODO +export type WorkerOutput = Uint8Array | + {program_rom_variable:string, program_rom:Uint8Array, code:{}, name:string, ports:any[], signals:any[]}; + +export interface WorkerResult { + output:WorkerOutput, + errors:WorkerError[], + listings:CodeListingMap, + symbolmap:{[sym:string]:number}, + params:{}, +} diff --git a/test/cli/teststore.js b/test/cli/teststore.js index c1dbf891..8039af4f 100644 --- a/test/cli/teststore.js +++ b/test/cli/teststore.js @@ -48,6 +48,7 @@ includeInThisContext("localForage/dist/localforage.js"); includeInThisContext("gen/util.js"); includeInThisContext("src/store.js"); var prj = require("../../gen/project.js"); +var prj = require("../../gen/workertypes.js"); var test_platform_id = "_TEST"; diff --git a/test/cli/testworker.js b/test/cli/testworker.js index 073d2291..f34b5771 100644 --- a/test/cli/testworker.js +++ b/test/cli/testworker.js @@ -36,6 +36,7 @@ function doBuild(msgs, callback, outlen, nlines, nerrors) { } else { assert.equal(nerrors||0, 0, "errors"); assert.equal(msg.output.code?msg.output.code.length:msg.output.length, outlen, "output binary"); + assert.ok(msg.output.code || msg.output instanceof Uint8Array); if (nlines) { if (typeof nlines === 'number') nlines = [nlines]; diff --git a/tsconfig.json b/tsconfig.json index d60bea2e..bb804d99 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,8 @@ "dom" ], "noImplicitThis": false, - "noImplicitAny": false + "noImplicitAny": false, + "alwaysStrict": true }, "include": [ "./src/*.ts",