1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-21 23:30:58 +00:00

testing scripting platform

This commit is contained in:
Steven Hugg 2019-09-29 13:41:29 -05:00
parent a9736e18cf
commit 6753d9bf40
6 changed files with 131 additions and 11 deletions

View File

@ -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;
}

View File

@ -493,6 +493,7 @@ if (window.location.host.endsWith('8bitworkshop.com')) {
<script src="codemirror/mode/z80/z80.js"></script>
<script src="codemirror/mode/verilog/verilog.js"></script>
<script src="codemirror/mode/markdown/markdown.js"></script>
<script src="codemirror/mode/javascript/javascript.js"></script>
<script src="src/codemirror/6502.js"></script>
<script src="src/codemirror/bataribasic.js"></script>
<link rel="stylesheet" href="css/codemirror.css">

101
src/platform/script.ts Normal file
View File

@ -0,0 +1,101 @@
import { PLATFORMS } from "../emu";
import { Platform } from "../baseplatform";
// TODO: scripts run in global context
class NotebookItem {
obj: any;
el: JQuery<HTMLElement>;
wrap: JQuery<HTMLElement>;
setData(data: any) {
this.obj = data;
this.el = $("<div>");
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<string>(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 = $('<div class="markdown">');
$(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;

View File

@ -72,6 +72,7 @@ var TOOL_TO_SOURCE_STYLE = {
'zmac': 'z80',
'bataribasic': 'bataribasic',
'markdown': 'markdown',
'js': 'javascript',
'xasm6809': 'z80'
}

View File

@ -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);

View File

@ -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) {
}
}
}
//}();