Skip to content

Commit

Permalink
WIP stdio line buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Nov 23, 2018
1 parent d083f72 commit 7560787
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 7 additions & 1 deletion js/testing/testing.ts
Expand Up @@ -13,6 +13,7 @@
limitations under the License.
*/

import { stdout } from "deno";
export { assert, assertEqual, equal } from "./util";

export type TestFunction = () => void | Promise<void>;
Expand Down Expand Up @@ -70,6 +71,11 @@ function green_ok() {
return FG_GREEN + "ok" + RESET;
}

// Prints to stdout without newline.
async function print(s: string): Promise<void> {
await stdout.write(new TextEncoder().encode(s));
}

async function runTests() {
let passed = 0;
let failed = 0;
Expand All @@ -78,7 +84,7 @@ async function runTests() {
for (let i = 0; i < tests.length; i++) {
const { fn, name } = tests[i];
let result = green_ok();
console.log("test", name);
print(`test ${name}`);
try {
await fn();
passed++;
Expand Down
11 changes: 9 additions & 2 deletions src/resources.rs
Expand Up @@ -43,14 +43,21 @@ pub type ResourceId = u32; // Sometimes referred to RID.
// system ones.
type ResourceTable = HashMap<ResourceId, Repr>;


use std::os::unix::io::FromRawFd;

lazy_static! {
// Starts at 3 because stdio is [0-2].
static ref NEXT_RID: AtomicUsize = AtomicUsize::new(3);
static ref RESOURCE_TABLE: Mutex<ResourceTable> = Mutex::new({
let mut m = HashMap::new();
// TODO Load these lazily during lookup?
m.insert(0, Repr::Stdin(tokio::io::stdin()));
m.insert(1, Repr::Stdout(tokio::io::stdout()));

let stdout = unsafe {std::fs::File::from_raw_fd(1)};
m.insert(1, Repr::Stdout(
tokio::fs::File::from_std(stdout)));

m.insert(2, Repr::Stderr(tokio::io::stderr()));
m
});
Expand All @@ -59,7 +66,7 @@ lazy_static! {
// Internal representation of Resource.
enum Repr {
Stdin(tokio::io::Stdin),
Stdout(tokio::io::Stdout),
Stdout(tokio::fs::File),
Stderr(tokio::io::Stderr),
FsFile(tokio::fs::File),
TcpListener(tokio::net::TcpListener),
Expand Down

3 comments on commit 7560787

@hayd
Copy link

@hayd hayd commented on 7560787 Nov 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question is, if you're adding a print function, should console.log use this and remove the println func from libdeno. IIRC

@ry
Copy link
Owner Author

@ry ry commented on 7560787 Nov 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hayd Yah... maybe. One thing I'm worried about with this change is that it makes //js/testing not isomorphic. It would not be able to be used on the browser. Is there a console API for printing without new line?

@ry
Copy link
Owner Author

@ry ry commented on 7560787 Nov 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use console.group() to do this?

Please sign in to comment.