Skip to content

Commit

Permalink
Add EOT to terminal (#204)
Browse files Browse the repository at this point in the history
* Add EOT to terminal

* Remove deprecated rmdir calls

* Small format changes
  • Loading branch information
dbaeumer authored Oct 3, 2024
1 parent 4152b63 commit 6af74e0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"includePath": [
"${workspaceFolder}/**",
// "/home/dirkb/bin/emsdk/upstream/emscripten/cache/sysroot/include"
"/home/dirkb/bin/wasi-sdk/share/wasi-sysroot/include"
"/home/dirkb/bin/wasi-sdk/share/wasi-sysroot/include/c++",
"/home/dirkb/bin/wasi-sdk/share/wasi-sysroot/include/wasm32-wasip1"
],
"defines": [],
//"compilerPath": "/home/dirkb/bin/emsdk/upstream/emscripten/emcc",
Expand Down
4 changes: 2 additions & 2 deletions sync-api-tests/src/desktop/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import path from 'path';
import fs from 'fs';
import os from 'os';
import path from 'path';
import * as uuid from 'uuid';
import find = require('find-process');

Expand All @@ -19,7 +19,7 @@ function rimraf(location: string) {
rimraf(path.join(location, dir));
}

fs.rmdirSync(location);
fs.rmSync(location, { recursive: true });
}
else {
fs.unlinkSync(location);
Expand Down
16 changes: 13 additions & 3 deletions testbeds/cpp/hello.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#include <iostream>
using namespace std;
// #include <iostream>
// using namespace std;

#include <stdio.h>

int main()
{
cout << "Hello World\n";
// cout << "Hello World\n";
// string input;
// cout << "Enter a string: ";
// cin >> input;
// cout << "You entered: " << input << endl;
int c;
while ((c = getchar()) != EOF) {
printf("%c", c);
}
return 0;
}
39 changes: 35 additions & 4 deletions wasm-wasi-core/src/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* ------------------------------------------------------------------------------------------ */
import { Event, EventEmitter } from 'vscode';

import { PseudoterminalState, Stdio, WasmPseudoterminal } from './api';
import RAL from './ral';
import { Stdio, WasmPseudoterminal, PseudoterminalState } from './api';

class LineBuffer {

Expand Down Expand Up @@ -252,6 +252,7 @@ export class WasmPseudoterminalImpl implements WasmPseudoterminal {
private writeBuffer: string[] | undefined;
private encoder: RAL.TextEncoder;
private decoder: RAL.TextDecoder;
private isStdInClosed: boolean;

constructor(options: Options = {}) {
this.options = options;
Expand Down Expand Up @@ -281,6 +282,7 @@ export class WasmPseudoterminalImpl implements WasmPseudoterminal {

this.encoder = RAL().TextEncoder.create();
this.decoder = RAL().TextDecoder.create();
this.isStdInClosed = false;

this.lines = [];
this.lineBuffer = new LineBuffer();
Expand All @@ -300,6 +302,11 @@ export class WasmPseudoterminalImpl implements WasmPseudoterminal {
const old = this.state;
this.state = state;
if (old !== state) {
if (state === PseudoterminalState.free || state === PseudoterminalState.idle) {
this.lineBuffer.clear();
this.lines = [];
this.isStdInClosed = false;
}
this._onDidChangeState.fire({ old, new: state });
}
}
Expand Down Expand Up @@ -335,6 +342,9 @@ export class WasmPseudoterminalImpl implements WasmPseudoterminal {
}

public async read(_maxBytesToRead: number): Promise<Uint8Array> {
if (this.isStdInClosed && this.lines.length === 0) {
return new Uint8Array(0);
}
const value = await this.readline();
return this.encoder.encode(value);
}
Expand All @@ -346,9 +356,13 @@ export class WasmPseudoterminalImpl implements WasmPseudoterminal {
if (this.lines.length > 0) {
return Promise.resolve(this.lines.shift()!);
}
return new Promise((resolve) => {
this.readlineCallback = resolve;
});
if (this.isStdInClosed) {
return Promise.resolve('');
} else {
return new Promise((resolve) => {
this.readlineCallback = resolve;
});
}
}

public write(content: string): Promise<void>;
Expand Down Expand Up @@ -389,6 +403,9 @@ export class WasmPseudoterminalImpl implements WasmPseudoterminal {
case '\x03': // ctrl+C
this.handleInterrupt();
break;
case '\x04': // ctrl+D (end of transmission)
this.handleEOT(data);
break;
case '\x06': // ctrl+f
case '\x1b[C': // right
this.adjustCursor(this.lineBuffer.moveCursorRelative(1), previousCursor, this.lineBuffer.getCursor());
Expand Down Expand Up @@ -487,6 +504,20 @@ export class WasmPseudoterminalImpl implements WasmPseudoterminal {
}
}

private handleEOT(_data: string): void {
this.isStdInClosed = true;
const line = this.lineBuffer.getLine();
this.lineBuffer.clear();
if (line.length > 0) {
this.lines.push(line);
}
if (this.readlineCallback !== undefined && this.lines.length > 0) {
const result = this.lines.shift()!;
this.readlineCallback(result);
this.readlineCallback = undefined;
}
}

private adjustCursor(success: boolean, oldCursor: number, newCursor: number): void {
if (!success) {
this.bell();
Expand Down
19 changes: 9 additions & 10 deletions wasm-wasi-core/src/desktop/nodeFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import { BigIntStats, Dir } from 'node:fs';
import fs from 'node:fs/promises';
import paths from 'node:path';

import { Dirent } from 'node:fs';
import { Uri } from 'vscode';
import { size, u64 } from '../common/baseTypes';
import { BigInts } from '../common/converter';
import { DeviceDriverKind, DeviceId, FileSystemDeviceDriver, NoSysDeviceDriver, ReaddirEntry, WritePermDeniedDeviceDriver } from '../common/deviceDriver';
import { BaseFileDescriptor, FdProvider, FileDescriptor } from '../common/fileDescriptor';
import RAL from '../common/ral';
import { u64, size } from '../common/baseTypes';
import {
fdstat, filestat, Rights, fd, rights, fdflags, Filetype, WasiError, Errno, filetype, Whence,
lookupflags, timestamp, fstflags, oflags, Oflags, filesize, Fdflags, inode, Lookupflags, Fstflags, errno
Errno, errno, fd, fdflags, Fdflags, fdstat, filesize, filestat, Filetype, filetype, fstflags, Fstflags, inode,
lookupflags, Lookupflags, oflags, Oflags, Rights, rights, timestamp, WasiError, Whence
} from '../common/wasi';
import { BigInts } from '../common/converter';
import { BaseFileDescriptor, FdProvider, FileDescriptor } from '../common/fileDescriptor';
import { NoSysDeviceDriver, ReaddirEntry, FileSystemDeviceDriver, DeviceId, DeviceDriverKind } from '../common/deviceDriver';
import { Dirent } from 'node:fs';
import { Uri } from 'vscode';
import { WritePermDeniedDeviceDriver } from '../common/deviceDriver';

const _DirectoryBaseRights: rights = Rights.fd_fdstat_set_flags | Rights.path_create_directory |
Rights.path_create_file | Rights.path_link_source | Rights.path_link_target | Rights.path_open |
Expand Down Expand Up @@ -589,7 +588,7 @@ export function create(deviceId: DeviceId, basePath: string, readOnly: boolean =
assertDirectoryDescriptor(fileDescriptor);
await assertDirectoryExists(fileDescriptor);
const fullpath = paths.join(fileDescriptor.dir.path, path);
await fs.rmdir(fullpath);
await fs.rm(fullpath, { recursive: true });
} catch (error) {
throw handleError(error, Errno.notempty);
}
Expand Down
8 changes: 4 additions & 4 deletions wasm-wasi-core/src/desktop/test/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'node:path';
import * as os from 'node:os';
import * as fs from 'node:fs/promises';
import * as os from 'node:os';
import * as path from 'node:path';

import * as uuid from 'uuid';
import fp from 'find-process';
import * as uuid from 'uuid';

import { runTests } from '@vscode/test-electron';

Expand Down Expand Up @@ -64,7 +64,7 @@ async function main() {
process.exit(1);
} finally {
if (testDir !== undefined) {
await fs.rmdir(testDir, { recursive: true });
await fs.rm(testDir, { recursive: true });
}
}
}
Expand Down

0 comments on commit 6af74e0

Please sign in to comment.