Implement system.std{in,out,err}

See [issue 333][1] and pull request #192.

**Caveat**

`File::read` currently takes no parameters and is equivalent to a
"`readAll`".  This will be changed later to match [IO/A Spec's
`Stream#read`][2]; but, should still be noted.

[1]: http://code.google.com/p/phantomjs/issues/detail?id=333
[2]: http://wiki.commonjs.org/wiki/IO/A#Instance_Methods
1.9
execjosh 2012-12-15 20:01:36 +09:00 committed by Ariya Hidayat
parent e3517f108a
commit f6c87221a7
5 changed files with 121 additions and 0 deletions

View File

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

View File

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

View File

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

View File

@ -36,6 +36,7 @@
#include <QMap>
#include <QVariant>
#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<QString, QVariant> m_os;
virtual void initCompletions();
File *m_stdout;
File *m_stderr;
File *m_stdin;
};
#endif // SYSTEM_H

View File

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