diff --git a/examples/stdin-stdout-stderr.coffee b/examples/stdin-stdout-stderr.coffee new file mode 100644 index 00000000..6bee88d8 --- /dev/null +++ b/examples/stdin-stdout-stderr.coffee @@ -0,0 +1,18 @@ +system = require 'system' + +system.stdout.write 'Hello, system.stdout.write!' +system.stdout.writeLine '\nHello, system.stdout.writeLine!' + +system.stderr.write 'Hello, system.stderr.write!' +system.stderr.writeLine '\nHello, system.stderr.writeLine!' + +system.stdout.writeLine 'system.stdin.readLine(): ' +line = system.stdin.readLine() +system.stdout.writeLine JSON.stringify line + +# This is essentially a `readAll` +system.stdout.writeLine 'system.stdin.read(): (ctrl+D to end)' +input = system.stdin.read() +system.stdout.writeLine JSON.stringify input + +phantom.exit 0 diff --git a/examples/stdin-stdout-stderr.js b/examples/stdin-stdout-stderr.js new file mode 100644 index 00000000..03734bed --- /dev/null +++ b/examples/stdin-stdout-stderr.js @@ -0,0 +1,18 @@ +var system = require('system'); + +system.stdout.write('Hello, system.stdout.write!'); +system.stdout.writeLine('\nHello, system.stdout.writeLine!'); + +system.stderr.write('Hello, system.stderr.write!'); +system.stderr.writeLine('\nHello, system.stderr.writeLine!'); + +system.stdout.writeLine('system.stdin.readLine(): '); +var line = system.stdin.readLine(); +system.stdout.writeLine(JSON.stringify(line)); + +// This is essentially a `readAll` +system.stdout.writeLine('system.stdin.read(): (ctrl+D to end)'); +var input = system.stdin.read(); +system.stdout.writeLine(JSON.stringify(input)); + +phantom.exit(0); diff --git a/src/system.cpp b/src/system.cpp index 96164edf..263d8e6d 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -39,6 +39,9 @@ System::System(QObject *parent) : REPLCompletable(parent) + , m_stdout((File *)NULL) + , m_stderr((File *)NULL) + , m_stdin((File *)NULL) { // Populate "env" m_env = Env::instance()->asVariantMap(); @@ -122,6 +125,23 @@ System::System(QObject *parent) : #endif } +System::~System() +{ + // Clean-up standard streams + if ((File *)NULL != m_stdout) { + delete m_stdout; + m_stdout = (File *)NULL; + } + if ((File *)NULL != m_stderr) { + delete m_stderr; + m_stderr = (File *)NULL; + } + if ((File *)NULL != m_stdin) { + delete m_stdin; + m_stdin = (File *)NULL; + } +} + qint64 System::pid() const { return QApplication::applicationPid(); @@ -152,6 +172,36 @@ bool System::isSSLSupported() const return QSslSocket::supportsSsl(); } +QObject *System::_stdout() { + if ((File *)NULL == m_stdout) { + QFile *f = new QFile(); + f->open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered); + m_stdout = new File(f, (QTextCodec *)NULL, this); + } + + return m_stdout; +} + +QObject *System::_stderr() { + if ((File *)NULL == m_stderr) { + QFile *f = new QFile(); + f->open(stderr, QIODevice::WriteOnly | QIODevice::Unbuffered); + m_stderr = new File(f, (QTextCodec *)NULL, this); + } + + return m_stderr; +} + +QObject *System::_stdin() { + if ((File *)NULL == m_stdin) { + QFile *f = new QFile(); + f->open(stdin, QIODevice::ReadOnly | QIODevice::Unbuffered); + m_stdin = new File(f, (QTextCodec *)NULL, this); + } + + return m_stdin; +} + void System::initCompletions() { addCompletion("pid"); @@ -160,4 +210,7 @@ void System::initCompletions() addCompletion("platform"); addCompletion("os"); addCompletion("isSSLSupported"); + addCompletion("stdin"); + addCompletion("stdout"); + addCompletion("stderr"); } diff --git a/src/system.h b/src/system.h index 5e435cd1..46f78d83 100644 --- a/src/system.h +++ b/src/system.h @@ -36,6 +36,7 @@ #include #include +#include "filesystem.h" #include "replcompletable.h" // This class implements the CommonJS System/1.0 spec. @@ -48,9 +49,13 @@ class System : public REPLCompletable Q_PROPERTY(QVariant env READ env) Q_PROPERTY(QVariant os READ os) Q_PROPERTY(bool isSSLSupported READ isSSLSupported) + Q_PROPERTY(QObject *stdout READ _stdout) + Q_PROPERTY(QObject *stderr READ _stderr) + Q_PROPERTY(QObject *stdin READ _stdin) public: explicit System(QObject *parent = 0); + virtual ~System(); qint64 pid() const; @@ -63,11 +68,23 @@ public: bool isSSLSupported() const; + // system.stdout + QObject *_stdout(); + + // system.stderr + QObject *_stderr(); + + // system.stdin + QObject *_stdin(); + private: QStringList m_args; QVariant m_env; QMap m_os; virtual void initCompletions(); + File *m_stdout; + File *m_stderr; + File *m_stdin; }; #endif // SYSTEM_H diff --git a/test/system-spec.js b/test/system-spec.js index 046c199d..b94a52ec 100644 --- a/test/system-spec.js +++ b/test/system-spec.js @@ -54,4 +54,19 @@ describe("System object", function() { expect(typeof system.isSSLSupported).toEqual('boolean'); }); + it("should have stdout as object", function() { + expect(typeof system.stdout).toEqual('object'); + expect(null == system.stdout).toBeFalsy(); + }); + + it("should have stderr as object", function() { + expect(typeof system.stderr).toEqual('object'); + expect(null == system.stderr).toBeFalsy(); + }); + + it("should have stdin as object", function() { + expect(typeof system.stdin).toEqual('object'); + expect(null == system.stdin).toBeFalsy(); + }); + });