Skip to content

Commit 90a10a0

Browse files
committed
impl emulators
1 parent 24efef7 commit 90a10a0

File tree

4 files changed

+167
-147
lines changed

4 files changed

+167
-147
lines changed

masm-tasm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@
472472
"assert": "^2.0.0",
473473
"del": "^7.0.0",
474474
"download": "^8.0.0",
475-
"emulators": "./dev/emulators-v8.3.3-dosasm11.tgz",
475+
"emulators": "./dev/emulators-v8.3.3-dosasm2.0.tgz",
476476
"eslint": "^8.20.0",
477477
"glob": "^8.0.3",
478478
"http-proxy-agent": "^7.0.2",

masm-tasm/pnpm-lock.yaml

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

masm-tasm/src/emulators/jsdos-ci.ts

+141-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,153 @@
11
import * as vscode from 'vscode';
22
import * as conf from '../utils/configuration';
3+
import { CommandInterface, utils } from 'emulators';
4+
5+
class JsdosTerminal implements vscode.Pseudoterminal {
6+
onDidWrite: vscode.Event<string>;
7+
writeEmitter: vscode.EventEmitter<string>;
8+
stdout = "";
9+
shell: utils.Shell;
10+
constructor(private ci: CommandInterface) {
11+
this.shell = new utils.Shell(ci);
12+
this.writeEmitter = new vscode.EventEmitter<string>();
13+
this.onDidWrite = this.writeEmitter.event;
14+
let stdout = "";
15+
ci.events().onStdout(
16+
data => {
17+
stdout += data;
18+
this.stdout += data;
19+
if (["\n", ">", "-"].some(p => data.includes(p))) {
20+
this.writeEmitter.fire(stdout);
21+
stdout = "";
22+
}
23+
}
24+
);
25+
}
26+
onDidOverrideDimensions?: vscode.Event<vscode.TerminalDimensions | undefined> | undefined;
27+
onDidClose?: vscode.Event<number | void> | undefined;
28+
onDidChangeName?: vscode.Event<string> | undefined;
29+
open(initialDimensions: vscode.TerminalDimensions | undefined): void {
30+
this.writeEmitter.fire('\x1b[31mJSDos\x1b[0m\r\nhello');
31+
}
32+
close(): void {
33+
this.ci.exit();
34+
}
35+
input = "";
36+
handleInput?(data: string): void {
37+
if (data === "\r") {
38+
this.shell.exec(this.input)
39+
this.input = "";
40+
} else {
41+
this.writeEmitter.fire("\x1b[31m" + data + "\x1b[0m");
42+
this.input += data;
43+
}
44+
}
45+
}
346

4-
const bar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
547

6-
function showStatus() {
7-
bar.command = 'masmtasm.updateEmuASM';
8-
bar.text = `${conf.extConf.emulator} ${conf.extConf.asmType}`;
9-
bar.show();
48+
function getWebviewContent(webview: vscode.Webview, extensionUri: vscode.Uri) {
49+
const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(extensionUri, 'resources', 'webview', 'script.js'));
50+
return `
51+
<!DOCTYPE html>
52+
<html lang="en">
53+
<head>
54+
<meta charset="UTF-8">
55+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
56+
<title>时间延迟计算器</title>
57+
</head>
58+
<body>
59+
<div id="result"></div>
60+
<div><canvas id="layout"></canvas></div>
61+
<script src="${scriptUri}"></script>
62+
</body>
63+
</html>
64+
`;
1065
}
1166

12-
async function statusBarCommand() {
13-
const _conf = vscode.workspace.getConfiguration('masmtasm.ASM');
14-
const items=["show jsdos view","show terminal"]
15-
16-
const placeHolder = 'manipulate emulator';
17-
const Selected = await vscode.window.showQuickPick(items, { placeHolder });
18-
if (Selected) {
19-
const [emu1, asm1] = Selected?.split('\t');
20-
const target = vscode.ConfigurationTarget.Global;
21-
await _conf.update('emulator', emu1, target);
22-
await _conf.update('assembler', asm1, target);
23-
showStatus();
67+
68+
class Manager {
69+
ci: CommandInterface | undefined = undefined;
70+
terminal:vscode.Terminal|undefined=undefined;
71+
updateci(ci: CommandInterface) {
72+
const bar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
73+
bar.command = 'masmtasm.emulatorStatus';
74+
bar.text = `jsdos`;
75+
bar.show();
76+
77+
this.ci = ci;
78+
const pty = new JsdosTerminal(this.ci);
79+
this.terminal=vscode.window.createTerminal({ name: "jsdos", pty, });
80+
}
81+
webview(context:vscode.ExtensionContext) {
82+
if (this.ci) {
83+
const panel = vscode.window.createWebviewPanel(
84+
"jsdos",
85+
"jsdos panel",
86+
{ viewColumn: vscode.ViewColumn.Beside },
87+
{
88+
enableScripts: true
89+
}
90+
)
91+
const currentTime = new Date().getTime();
92+
93+
panel.webview.html = getWebviewContent(panel.webview, context.extensionUri);
94+
panel.webview.postMessage({ command: 'setTime', time: currentTime });
95+
96+
panel.webview.postMessage({
97+
command: "ci",
98+
width: this.ci?.width(),
99+
height: this.ci?.height()
100+
});
101+
this.ci?.events().onFrame((rgb, rgba) => {
102+
panel.webview.postMessage({
103+
command: 'rgb',
104+
time: new Date().getTime(),
105+
data: rgb
106+
});
107+
});
108+
panel.webview.onDidReceiveMessage(
109+
message => {
110+
console.log(message);
111+
switch (message.command) {
112+
case 'alert':
113+
vscode.window.showInformationMessage(message.text);
114+
return;
115+
case 'keyup':
116+
const up = utils.htmlKey2jsdos(message.code);
117+
if (up && this.ci)
118+
this.ci.sendKeyEvent(up, false);
119+
return;
120+
case 'keydown':
121+
const down = utils.htmlKey2jsdos(message.code);
122+
if (down && this.ci)
123+
this.ci.sendKeyEvent(down, true);
124+
return;
125+
}
126+
},
127+
undefined,
128+
context.subscriptions
129+
);
130+
}
24131
}
25132
}
26133

134+
export const manager=new Manager();
135+
136+
137+
27138
export function activate(context: vscode.ExtensionContext): void {
28-
const disposable = vscode.commands.registerCommand('masmtasm.updateEmuASM', statusBarCommand);
139+
async function statusBarCommand() {
140+
const items = ["show jsdos view", "show terminal"];
141+
142+
const placeHolder = 'manipulate emulator';
143+
const seleted = await vscode.window.showQuickPick(items, { placeHolder });
144+
if (seleted===items[0]) {
145+
manager.webview(context);
146+
}
147+
if(seleted===items[1]){
148+
manager.terminal?.show();
149+
}
150+
}
151+
const disposable = vscode.commands.registerCommand('masmtasm.emulatorStatus', statusBarCommand);
29152
context.subscriptions.push(disposable);
30-
showStatus();
31153
}

masm-tasm/src/emulators/jsdos.ts

+19-121
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { ExtensionContext, ExtensionMode, Uri } from "vscode";
2-
import * as vscode from "vscode";
32
import { ActionContext, AsmResult, ExecAction } from "../ASM/manager";
43
import { DosEmulatorType } from "../utils/configuration";
5-
import { CommandInterface, getEmulators,utils } from "emulators";
6-
4+
import { CommandInterface, getEmulators,platform } from "emulators";
5+
import { manager } from "./jsdos-ci";
6+
platform.current.node_require=function(url:string){
7+
return __non_webpack_require__(url);
8+
};
79

810
const TEST_STRING="XDRGS";
911
const config={
@@ -15,145 +17,41 @@ echo ${TEST_STRING}
1517
},
1618
};
1719

18-
class JsdosTerminal implements vscode.Pseudoterminal{
19-
onDidWrite: vscode.Event<string>;
20-
writeEmitter:vscode.EventEmitter<string>
21-
stdout=""
22-
shell:utils.Shell
23-
constructor(private ci:CommandInterface){
24-
this.shell=new utils.Shell(ci)
25-
this.writeEmitter = new vscode.EventEmitter<string>();
26-
this.onDidWrite=this.writeEmitter.event;
27-
let stdout=""
28-
ci.events().onStdout(
29-
data=>{
30-
stdout+=data
31-
this.stdout+=data
32-
if(["\n",">","-"].some(p=>data.includes(p))){
33-
this.writeEmitter.fire(stdout)
34-
stdout=""
35-
}
36-
}
37-
)
38-
}
39-
onDidOverrideDimensions?: vscode.Event<vscode.TerminalDimensions | undefined> | undefined;
40-
onDidClose?: vscode.Event<number | void> | undefined;
41-
onDidChangeName?: vscode.Event<string> | undefined;
42-
open(initialDimensions: vscode.TerminalDimensions | undefined): void {
43-
this.writeEmitter.fire('\x1b[31mJSDos\x1b[0m\r\nhello');
44-
}
45-
close(): void {
46-
this.ci.exit()
47-
}
48-
input=""
49-
handleInput?(data: string): void {
50-
if(data==="\r"){
51-
this.shell.exec(this.input)
52-
this.input=""
53-
}else{
54-
this.writeEmitter.fire("\x1b[31m"+data+"\x1b[0m")
55-
this.input+=data
56-
}
57-
}
58-
}
20+
5921

6022
class JsdosRuntime{
61-
emulators
62-
ci:CommandInterface|undefined
23+
emulators;
24+
ci:CommandInterface|undefined;
6325
constructor(pathprefix:string){
6426
this.emulators=getEmulators(pathprefix);
65-
const a=__non_webpack_require__(pathprefix+"wdosbox.js")
66-
console.log(a)
27+
const a=__non_webpack_require__(pathprefix+"wdosbox.js");
28+
console.log(a);
6729
}
6830
async run(){
69-
this.ci=await this.emulators.dosboxDirect(config,)
70-
const pty=new JsdosTerminal(this.ci)
71-
vscode.window.createTerminal({name:"jsdos",pty,})
31+
this.ci=await this.emulators.dosboxDirect(config,);
7232
}
7333
}
7434

75-
function getWebviewContent(webview: vscode.Webview, extensionUri: vscode.Uri) {
76-
const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(extensionUri, 'resources', 'webview', 'script.js'));
77-
return `
78-
<!DOCTYPE html>
79-
<html lang="en">
80-
<head>
81-
<meta charset="UTF-8">
82-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
83-
<title>时间延迟计算器</title>
84-
</head>
85-
<body>
86-
<div id="result"></div>
87-
<div><canvas id="layout"></canvas></div>
88-
<script src="${scriptUri}"></script>
89-
</body>
90-
</html>
91-
`;
92-
}
93-
94-
let runtime:JsdosRuntime|undefined=undefined
35+
let runtime:JsdosRuntime|undefined=undefined;
9536

9637
export class JSDosHost implements ExecAction{
9738
name: DosEmulatorType | DosEmulatorType[]=DosEmulatorType.jsdos;
9839
async run(context: ExtensionContext, ctx: ActionContext): Promise<AsmResult> {
99-
const panel=vscode.window.createWebviewPanel(
100-
"jsdos",
101-
"jsdos panel",
102-
{viewColumn:vscode.ViewColumn.Beside},
103-
{
104-
enableScripts: true
105-
}
106-
)
107-
const currentTime = new Date().getTime();
108-
109-
panel.webview.html = getWebviewContent(panel.webview, context.extensionUri);
110-
panel.webview.postMessage({ command: 'setTime', time: currentTime });
11140

11241
if(runtime===undefined||runtime.ci===undefined){
11342
if(context.extensionMode==ExtensionMode.Development){
114-
runtime=new JsdosRuntime(Uri.joinPath(context.extensionUri,"node_modules/emulators/dist/").fsPath)
43+
runtime=new JsdosRuntime(Uri.joinPath(context.extensionUri,"node_modules/emulators/dist/").fsPath);
11544
}else{
116-
throw new Error("not implemented")
45+
throw new Error("not implemented");
46+
}
47+
await runtime.run();
48+
if(runtime.ci){
49+
manager.updateci(runtime.ci);
11750
}
118-
await runtime.run()
119-
panel.webview.postMessage({
120-
command:"ci",
121-
width:runtime.ci?.width(),
122-
height:runtime.ci?.height()
123-
})
124-
runtime.ci?.events().onFrame((rgb,rgba)=>{
125-
panel.webview.postMessage({
126-
command: 'rgb',
127-
time:new Date().getTime(),
128-
data:rgb
129-
})
130-
})
131-
panel.webview.onDidReceiveMessage(
132-
message => {
133-
console.log(message)
134-
switch (message.command) {
135-
case 'alert':
136-
vscode.window.showInformationMessage(message.text);
137-
return;
138-
case 'keyup':
139-
const up=utils.htmlKey2jsdos(message.code)
140-
if(up && runtime&& runtime.ci)
141-
runtime.ci.sendKeyEvent(up, false);
142-
return;
143-
case 'keydown':
144-
const down=utils.htmlKey2jsdos(message.code)
145-
if(down && runtime&& runtime.ci)
146-
runtime.ci.sendKeyEvent(down, true);
147-
return;
148-
}
149-
},
150-
undefined,
151-
context.subscriptions
152-
);
15351
}
15452

15553
return {
15654
message:"hello"
157-
}
55+
};
15856
}
15957
}

0 commit comments

Comments
 (0)