Browse Source

Javascript console and env.load(), env.note, ...

cl-refactor
Gav Wood 11 years ago
parent
commit
4d3f8e849b
  1. 274
      alethzero/Main.ui
  2. 72
      alethzero/MainWin.cpp
  3. 10
      alethzero/MainWin.h
  4. 2
      libethereum/Client.cpp
  5. 1
      libethereum/Client.h
  6. 2
      liblll/Parser.cpp
  7. 9
      libqethereum/QEthereum.cpp

274
alethzero/Main.ui

@ -96,9 +96,6 @@
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="jsInput"/>
</item>
</layout>
</widget>
</widget>
@ -1014,6 +1011,277 @@
</layout>
</widget>
</widget>
<widget class="QDockWidget" name="dockWidget_11">
<property name="features">
<set>QDockWidget::DockWidgetFeatureMask</set>
</property>
<property name="windowTitle">
<string>Javascript Console</string>
</property>
<attribute name="dockWidgetArea">
<number>8</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents_11">
<layout class="QVBoxLayout" name="verticalLayout_6">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWebView" name="jsConsole">
<property name="url">
<url>
<string>about:blank</string>
</url>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_9">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_7">
<property name="minimumSize">
<size>
<width>14</width>
<height>0</height>
</size>
</property>
<property name="palette">
<palette>
<active>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>51</red>
<green>85</green>
<blue>221</blue>
</color>
</brush>
</colorrole>
<colorrole role="Button">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Text">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>51</red>
<green>85</green>
<blue>221</blue>
</color>
</brush>
</colorrole>
<colorrole role="ButtonText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>51</red>
<green>85</green>
<blue>221</blue>
</color>
</brush>
</colorrole>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>51</red>
<green>85</green>
<blue>221</blue>
</color>
</brush>
</colorrole>
<colorrole role="Button">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Text">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>51</red>
<green>85</green>
<blue>221</blue>
</color>
</brush>
</colorrole>
<colorrole role="ButtonText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>51</red>
<green>85</green>
<blue>221</blue>
</color>
</brush>
</colorrole>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>51</red>
<green>85</green>
<blue>221</blue>
</color>
</brush>
</colorrole>
<colorrole role="Button">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Text">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>51</red>
<green>85</green>
<blue>221</blue>
</color>
</brush>
</colorrole>
<colorrole role="ButtonText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>51</red>
<green>85</green>
<blue>221</blue>
</color>
</brush>
</colorrole>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="styleSheet">
<string notr="true">background-color: white;
color: #35d;
font-weight: bold;
font-family: Monospace, Ubuntu Mono, Lucida Console, Courier New;
font-size: 14pt</string>
</property>
<property name="text">
<string>&gt;</string>
</property>
<property name="margin">
<number>0</number>
</property>
<property name="buddy">
<cstring>jsInput</cstring>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="jsInput">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">background: white;
border: 0;
font-family: Monospace, Ubuntu Mono, Lucida Console, Courier New</string>
</property>
<property name="inputMask">
<string/>
</property>
<property name="text">
<string/>
</property>
<property name="frame">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<action name="quit">
<property name="text">
<string>&amp;Quit</string>

72
alethzero/MainWin.cpp

@ -233,6 +233,7 @@ Main::Main(QWidget *parent) :
connect(f, &QWebFrame::javaScriptWindowObjectCleared, [=](){
auto qe = new QEthereum(this, m_client.get(), owned());
qe->setup(f);
f->addToJavaScriptWindowObject("env", this, QWebFrame::QtOwnership);
});
readSettings();
@ -254,10 +255,74 @@ Main::~Main()
writeSettings();
}
void Main::load(QString _s)
{
QFile fin(_s);
if (!fin.open(QFile::ReadOnly))
return;
QString line;
while (!fin.atEnd())
{
QString l = QString::fromUtf8(fin.readLine());
line.append(l);
if (line.count('"') % 2)
{
line.chop(1);
}
else if (line.endsWith("\\\n"))
line.chop(2);
else
{
ui->webView->page()->currentFrame()->evaluateJavaScript(line);
//eval(line);
line.clear();
}
}
}
// env.load("/home/gav/eth/init.eth")
void Main::note(QString _s)
{
cnote << _s.toStdString();
}
void Main::debug(QString _s)
{
cdebug << _s.toStdString();
}
void Main::warn(QString _s)
{
cwarn << _s.toStdString();
}
void Main::on_jsInput_returnPressed()
{
ui->jsInput->setText(ui->webView->page()->currentFrame()->evaluateJavaScript(ui->jsInput->text()).toString());
ui->jsInput->setSelection(0, ui->jsInput->text().size());
eval(ui->jsInput->text());
ui->jsInput->setText("");
}
void Main::eval(QString const& _js)
{
if (_js.trimmed().isEmpty())
return;
QVariant ev = ui->webView->page()->currentFrame()->evaluateJavaScript(_js);
QString s;
if (ev.isNull())
s = "<span style=\"color: #888\">null</span>";
else if (ev.type() == QVariant::String)
s = "<span style=\"color: #444\">\"</span><span style=\"color: #f44\">" + ev.toString().toHtmlEscaped() + "</span><span style=\"color: #444\">\"</span>";
else if (ev.type() == QVariant::Int || ev.type() == QVariant::Double)
s = "<span style=\"color: #44f\">" + ev.toString().toHtmlEscaped() + "</span>";
else
s = "<span style=\"color: #888\">unknown type</span>";
m_consoleHistory.push_back(qMakePair(_js, s));
s = "<html><body style=\"font-family: Monospace, Ubuntu Mono, Lucida Console, Courier New; margin: 0; font-size: 12pt\"><div style=\"position: absolute; bottom: 0; border: 0px; margin: 0px; width: 100%\">";
for (auto const& i: m_consoleHistory)
s += "<div style=\"border-bottom: 1 solid #eee; width: 100%\"><span style=\"float: left; width: 1em; color: #888; font-weight: bold\">&gt;</span><span style=\"color: #35d\">" + i.first.toHtmlEscaped() + "</span></div>"
"<div style=\"border-bottom: 1 solid #eee; width: 100%\"><span style=\"float: left; width: 1em\">&nbsp;</span><span>" + i.second + "</span></div>";
s += "</div></body></html>";
ui->jsConsole->setHtml(s);
}
QString Main::pretty(eth::Address _a) const
@ -489,12 +554,13 @@ void Main::refresh(bool _override)
auto acs = st.addresses();
ui->accounts->clear();
ui->contracts->clear();
for (auto n = 0; n < (ui->showAllAccounts->isChecked() ? 2 : 1); ++n)
for (auto n = 0; n < 2; ++n)
for (auto i: acs)
{
auto r = render(i.first);
if (r.contains('(') == !n)
{
if (n == 0 || ui->showAllAccounts->isChecked())
(new QListWidgetItem(QString("%2: %1 [%3]").arg(formatBalance(i.second).c_str()).arg(r).arg((unsigned)state().transactionsFrom(i.first)), ui->accounts))
->setData(Qt::UserRole, QByteArray((char const*)i.first.data(), Address::size));
if (st.addressHasCode(i.first))

10
alethzero/MainWin.h

@ -63,7 +63,15 @@ public:
QList<eth::KeyPair> const& owned() const { return m_myKeys; }
public slots:
void load(QString _file);
void note(QString _entry);
void debug(QString _entry);
void warn(QString _entry);
private slots:
void eval(QString const& _js);
void on_connect_triggered();
void on_mine_triggered();
void on_send_clicked();
@ -155,5 +163,7 @@ private:
QNetworkAccessManager m_webCtrl;
QList<QPair<QString, QString>> m_consoleHistory;
QEthereum* m_ethereum;
};

2
libethereum/Client.cpp

@ -154,7 +154,6 @@ void Client::transact(Secret _secret, u256 _value, Address _dest, bytes const& _
t.sign(_secret);
cnote << "New transaction " << t;
m_tq.attemptImport(t.rlp());
m_changed = true;
}
Address Client::transact(Secret _secret, u256 _endowment, bytes const& _init, u256 _gas, u256 _gasPrice)
@ -170,7 +169,6 @@ Address Client::transact(Secret _secret, u256 _endowment, bytes const& _init, u2
t.sign(_secret);
cnote << "New transaction " << t;
m_tq.attemptImport(t.rlp());
m_changed = true;
return right160(sha3(rlpList(t.sender(), t.nonce)));
}

1
libethereum/Client.h

@ -122,6 +122,7 @@ public:
/// Determines whether at least one of the state/blockChain/transactionQueue has changed since the last call to changed().
bool changed() const { auto ret = m_changed; m_changed = false; return ret; }
bool peekChanged() const { return m_changed; }
/// Get the object representing the current state of Ethereum.
State const& state() const { return m_preMine; }

2
liblll/Parser.cpp

@ -75,7 +75,7 @@ void eth::parseTreeLLL(string const& _s, sp::utree& o_out)
qi::rule<it, qi::ascii::space_type, sp::utree()> element;
qi::rule<it, string()> str = '"' > qi::lexeme[+(~qi::char_(std::string("\"") + '\0'))] > '"';
qi::rule<it, string()> strsh = '\'' > qi::lexeme[+(~qi::char_(std::string(" ;())") + '\0'))];
qi::rule<it, string()> strsh = '\'' > qi::lexeme[+(~qi::char_(std::string(" ;@()[]{}:") + '\0'))];
qi::rule<it, symbol_type()> symbol = qi::lexeme[+(~qi::char_(std::string(" @[]{}:();\"\x01-\x1f\x7f") + '\0'))];
qi::rule<it, string()> intstr = qi::lexeme[ qi::no_case["0x"][qi::_val = "0x"] >> *qi::char_("0-9a-fA-F")[qi::_val += qi::_1]] | qi::lexeme[+qi::char_("0-9")[qi::_val += qi::_1]];
qi::rule<it, bigint()> integer = intstr;

9
libqethereum/QEthereum.cpp

@ -394,12 +394,19 @@ unsigned QEthereum::peerCount() const
QString QEthereum::doCreate(QString _secret, QString _amount, QString _init, QString _gas, QString _gasPrice)
{
return toQJS(client()->transact(toSecret(_secret), toU256(_amount), toBytes(_init), toU256(_gas), toU256(_gasPrice)));
client()->changed();
auto ret = toQJS(client()->transact(toSecret(_secret), toU256(_amount), toBytes(_init), toU256(_gas), toU256(_gasPrice)));
while (!client()->peekChanged())
usleep(10000);
return ret;
}
void QEthereum::doTransact(QString _secret, QString _amount, QString _dest, QString _data, QString _gas, QString _gasPrice)
{
client()->changed();
client()->transact(toSecret(_secret), toU256(_amount), toAddress(_dest), toBytes(_data), toU256(_gas), toU256(_gasPrice));
while (!client()->peekChanged())
usleep(10000);
}
// extra bits needed to link on VS

Loading…
Cancel
Save