From 6753d9bf4041f3db4de37141bddb56e36a45010d Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Sun, 29 Sep 2019 13:41:29 -0500 Subject: [PATCH] testing scripting platform --- css/ui.css | 9 ++-- index.html | 1 + src/platform/script.ts | 101 +++++++++++++++++++++++++++++++++++++++ src/ui.ts | 1 + src/windows.ts | 2 +- src/worker/workermain.ts | 28 +++++++++-- 6 files changed, 131 insertions(+), 11 deletions(-) create mode 100644 src/platform/script.ts diff --git a/css/ui.css b/css/ui.css index dab533f4..d8d1ddd7 100644 --- a/css/ui.css +++ b/css/ui.css @@ -376,6 +376,7 @@ div.markdown { width:94%; margin:3%; padding:1em; + user-select: auto; } div.markdown table { margin:1em; @@ -415,13 +416,11 @@ div.markdown th { color:rgba(0,0,0,0); } .disable-select { - -webkit-touch-callout: none; - -webkit-user-select: none; - -khtml-user-select: none; - -moz-user-select: none; - -ms-user-select: none; user-select: none; } +.enable-select { + user-select: auto; +} .alert { z-index:2; } diff --git a/index.html b/index.html index c54ff666..e23a9b66 100644 --- a/index.html +++ b/index.html @@ -493,6 +493,7 @@ if (window.location.host.endsWith('8bitworkshop.com')) { + diff --git a/src/platform/script.ts b/src/platform/script.ts new file mode 100644 index 00000000..7d675068 --- /dev/null +++ b/src/platform/script.ts @@ -0,0 +1,101 @@ + +import { PLATFORMS } from "../emu"; +import { Platform } from "../baseplatform"; + +// TODO: scripts run in global context + +class NotebookItem { + obj: any; + el: JQuery; + wrap: JQuery; + + setData(data: any) { + this.obj = data; + this.el = $("
"); + this.el.text(JSON.stringify(data, null, ' ')); + return true; + } + delete() { + this.wrap && this.wrap.remove(); + this.wrap = null; + } +} + +class Notebook { + top: HTMLElement; + items: {[id:string]:NotebookItem}; + + constructor(top: HTMLElement) { + this.items = {}; + this.top = top; + $(top).css('overflowY', 'auto'); + } + update(obj: any) { + var last = null; + var unused = new Set(Object.keys(this.items)); + for (var entry of Object.entries(obj)) { + var id = entry[0]; + var data = entry[1]; + var item = this.items[id]; + //console.log(id,data,item); + if (!item) { + this.items[id] = item = new NotebookItem(); + } + if (item.setData(data)) { + if (!item.wrap) { + item.wrap = $('
'); + $(this.top).append(item.wrap); + } + item.wrap.empty().append(item.el); + } + unused.delete(id); + } + unused.forEach((id) => { this.items[id].delete(); }); + } +} + +class ScriptPlatform implements Platform { + mainElement; + htmlDiv; + notebook: Notebook; + + constructor(mainElement:HTMLElement) { + this.mainElement = mainElement; + this.notebook = new Notebook(mainElement); + } + start() { + } + reset() { + } + pause() { + } + resume() { + } + loadROM(title, data) { + this.notebook.update(data); + } + isRunning() { + return false; + } + isDebugging() : boolean { + return false; + } + getToolForFilename(fn : string) : string { + return "js"; + } + getDefaultExtension() : string { + return ".js"; + } + getPresets() { + return [ + {id:'hello.js', name:'Hello'}, + ]; + } + /* + showHelp() { + window.open("https://github.com/showdownjs/showdown/wiki/Showdown's-Script-syntax", "_help"); + } + */ +} + +PLATFORMS['script'] = ScriptPlatform; diff --git a/src/ui.ts b/src/ui.ts index b2c6e885..7849a43b 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -72,6 +72,7 @@ var TOOL_TO_SOURCE_STYLE = { 'zmac': 'z80', 'bataribasic': 'bataribasic', 'markdown': 'markdown', + 'js': 'javascript', 'xasm6809': 'z80' } diff --git a/src/windows.ts b/src/windows.ts index 49c446bd..fa6d0c20 100644 --- a/src/windows.ts +++ b/src/windows.ts @@ -60,7 +60,7 @@ export class ProjectWindows { this.activediv = div; this.activewnd = wnd; $(div).show(); - this.refresh(moveCursor); + this.refresh(true); // needed to tell asset editor 1st time running, but that's bad this.refreshErrors(); wnd.setVisible && wnd.setVisible(true); this.id2showfn[id] && this.id2showfn[id](id, wnd); diff --git a/src/worker/workermain.ts b/src/worker/workermain.ts index 29704375..9d9cdfba 100644 --- a/src/worker/workermain.ts +++ b/src/worker/workermain.ts @@ -2,13 +2,16 @@ import { WorkerResult, WorkerFileUpdate, WorkerBuildStep, WorkerMessage, WorkerError, Dependency, SourceLine } from "../workertypes"; -const emglobal : any = this['window'] || this['global'] || this; declare var WebAssembly; declare function importScripts(path:string); declare function postMessage(msg); -var ENVIRONMENT_IS_WEB = typeof window === 'object'; -var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function'; +const emglobal : any = this['window'] || this['global'] || this; +const ENVIRONMENT_IS_WEB = typeof window === 'object'; +const ENVIRONMENT_IS_WORKER = typeof importScripts === 'function'; + +//const self = emglobal.self; +//const __WORKER__ = function() { // WebAssembly module cache // TODO: leaks memory even when disabled... @@ -1328,7 +1331,6 @@ function linkSDLDZ80(step:BuildStep) } } -var sdcc; function compileSDCC(step:BuildStep) { gatherFiles(step, { @@ -1339,7 +1341,7 @@ function compileSDCC(step:BuildStep) { var errors = []; var params = step.params; loadNative('sdcc'); - var SDCC = sdcc({ + var SDCC = emglobal.sdcc({ instantiateWasm: moduleInstFn('sdcc'), noInitialRun:true, noFSInit:true, @@ -1871,6 +1873,19 @@ function translateShowdown(step:BuildStep) { }; } +function runJavascript(step:BuildStep) { + var code = getWorkFileAsString(step.path); + try { + //eval(code); + var fn = new Function("'use strict';\n" + code); + var obj = {}; + var out = fn.call(obj); + return { output:out||obj }; + } catch (e) { + return { errors:[{line:(e.lineNumber-2)|0, msg:e.message||"Error"}] }; + } +} + // http://datapipe-blackbeltsystems.com/windows/flex/asm09.html function assembleXASM6809(step:BuildStep) { load("xasm6809"); @@ -2054,6 +2069,7 @@ var TOOLS = { 'nesasm': assembleNESASM, 'bataribasic': compileBatariBasic, 'markdown': translateShowdown, + 'js': runJavascript, } var TOOL_PRELOADFS = { @@ -2188,3 +2204,5 @@ if (ENVIRONMENT_IS_WORKER) { } } } + +//}();