Re-writing ethereum.js. Added future/promises support.

This commit is contained in:
obscuren 2014-09-19 22:42:55 +02:00
parent ae1de6593c
commit 8585e59718
8 changed files with 2488 additions and 385 deletions

View File

@ -19,36 +19,12 @@ ApplicationWindow {
property alias dataText: rawDataField.text property alias dataText: rawDataField.text
onClosing: { onClosing: {
//compileTimer.stop() dbg.Stop()
} }
MenuBar { menuBar: MenuBar {
Menu { Menu {
title: "Debugger" title: "Edit"
MenuItem {
text: "Run"
shortcut: "Ctrl+r"
onTriggered: debugCurrent()
}
MenuItem {
text: "Next"
shortcut: "Ctrl+n"
onTriggered: dbg.next()
}
MenuItem {
text: "Continue"
shortcut: "Ctrl+g"
onTriggered: dbg.continue()
}
MenuItem {
text: "Command"
shortcut: "Ctrl+l"
onTriggered: {
dbgCommand.focus = true
}
}
MenuItem { MenuItem {
text: "Focus code" text: "Focus code"
shortcut: "Ctrl+1" shortcut: "Ctrl+1"
@ -64,15 +40,41 @@ ApplicationWindow {
} }
} }
/*
MenuItem { MenuItem {
text: "Close window" text: "Command"
shortcut: "Ctrl+w" shortcut: "Ctrl+l"
onTriggered: { onTriggered: {
win.close() dbgCommand.focus = true
} }
} }
*/ }
Menu {
title: "Debugger"
MenuItem {
text: "Run"
shortcut: "Ctrl+r"
onTriggered: debugCurrent()
}
MenuItem {
text: "Stop"
onTriggered: dbp.stop()
}
MenuSeparator {}
MenuItem {
text: "Next"
shortcut: "Ctrl+n"
onTriggered: dbg.next()
}
MenuItem {
text: "Continue"
shortcut: "Ctrl+g"
onTriggered: dbg.continue()
}
} }
} }

View File

@ -0,0 +1,149 @@
// The magic return variable. The magic return variable will be set during the execution of the QML call.
(function(window) {
function message(type, data) {
document.title = JSON.stringify({type: type, data: data});
return window.____returnData;
}
function isPromise(o) {
return typeof o === "object" && o.then
}
window.eth = {
_callbacks: {},
_onCallbacks: {},
prototype: Object(),
coinbase: function() {
return new Promise(function(resolve, reject) {
postData({call: "getCoinBase"}, function(coinbase) {
resolve(coinbase);
});
});
},
block: function(numberOrHash) {
return new Promise(function(resolve, reject) {
var func;
if(typeof numberOrHash == "string") {
func = "getBlockByHash";
} else {
func = "getBlockByNumber";
}
postData({call: func, args: [numberOrHash]}, function(block) {
if(block)
resolve(block);
else
reject("not found");
});
});
},
transact: function(params) {
if(params === undefined) {
params = {};
}
if(params.endowment !== undefined)
params.value = params.endowment;
if(params.code !== undefined)
params.data = params.code;
var promises = []
if(isPromise(params.to)) {
promises.push(params.to.then(function(_to) { params.to = _to; }));
}
if(isPromise(params.from)) {
promises.push(params.from.then(function(_from) { params.from = _from; }));
}
if(isPromise(params.data)) {
promises.push(params.data.then(function(_code) { params.data = _code; }));
} else {
if(typeof params.data === "object") {
data = "";
for(var i = 0; i < params.data.length; i++) {
data += params.data[i]
}
} else {
data = params.data;
}
}
// Make sure everything is string
var fields = ["value", "gas", "gasPrice"];
for(var i = 0; i < fields.length; i++) {
if(params[fields[i]] === undefined) {
params[fields[i]] = "";
}
params[fields[i]] = params[fields[i]].toString();
}
// Load promises then call the last "transact".
return Q.all(promises).then(function() {
return new Promise(function(resolve, reject) {
postData({call: "transact", args: params}, function(data) {
if(data[1])
reject(data[0]);
else
resolve(data[0]);
});
});
})
},
compile: function(code) {
return new Promise(function(resolve, reject) {
postData({call: "compile", args: [code]}, function(data) {
if(data[1])
reject(data[0]);
else
resolve(data[0]);
});
});
},
key: function() {
return new Promise(function(resolve, reject) {
postData({call: "getKey"}, function(k) {
resolve(k);
});
});
}
};
function postData(data, cb) {
data._seed = Math.floor(Math.random() * 1000000)
if(cb) {
eth._callbacks[data._seed] = cb;
}
if(data.args === undefined) {
data.args = [];
}
navigator.qt.postMessage(JSON.stringify(data));
}
navigator.qt.onmessage = function(ev) {
var data = JSON.parse(ev.data)
if(data._event !== undefined) {
eth.trigger(data._event, data.data);
} else {
if(data._seed) {
var cb = eth._callbacks[data._seed];
if(cb) {
cb.call(this, data.data)
// Remove the "trigger" callback
delete eth._callbacks[ev._seed];
}
}
}
}
})(this);

View File

@ -35,3 +35,7 @@ navigator.qt.onmessage = function(ev) {
} }
} }
} }
if(typeof(Promise) === "undefined") {
window.Promise = Q.Promise;
}

1909
mist/assets/ext/q.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,13 @@
function HandleMessage(data) {
var message;
try { message = JSON.parse(data) } catch(e) {};
if(message) {
switch(message.type) {
case "coinbase":
return eth.coinBase();
case "block":
return eth.blockByNumber(0);
}
}
}

View File

@ -7,6 +7,8 @@ import QtQuick.Layouts 1.0;
import QtQuick.Window 2.1; import QtQuick.Window 2.1;
import Ethereum 1.0 import Ethereum 1.0
import "../ext/qml_messaging.js" as Messaging
//ApplicationWindow { //ApplicationWindow {
Rectangle { Rectangle {
id: window id: window
@ -14,10 +16,6 @@ Rectangle {
property var iconSource: "../browser.png" property var iconSource: "../browser.png"
property var menuItem property var menuItem
//width: 1000
//height: 800
//minimumHeight: 300
property alias url: webview.url property alias url: webview.url
property alias webView: webview property alias webView: webview
@ -97,7 +95,6 @@ Rectangle {
bottom: parent.bottom bottom: parent.bottom
top: navBar.bottom top: navBar.bottom
} }
onTitleChanged: { window.title = title }
property var cleanPath: false property var cleanPath: false
onNavigationRequested: { onNavigationRequested: {
@ -113,7 +110,7 @@ Rectangle {
uri.replace(reg, function(match, pre, domain, path) { uri.replace(reg, function(match, pre, domain, path) {
uri = pre; uri = pre;
var lookup = ui.lookupDomain(domain.substring(0, domain.length - 4)); var lookup = eth.lookupDomain(domain.substring(0, domain.length - 4));
var ip = []; var ip = [];
for(var i = 0, l = lookup.length; i < l; i++) { for(var i = 0, l = lookup.length; i < l; i++) {
ip.push(lookup.charCodeAt(i)) ip.push(lookup.charCodeAt(i))
@ -138,11 +135,22 @@ Rectangle {
} }
} }
function sendMessage(data) {
//this.experimental.evaluateJavaScript("window.____returnData="+JSON.stringify(data));
webview.experimental.postMessage(JSON.stringify(data))
}
onTitleChanged: {
var data = Messaging.HandleMessage(title);
if(data) {
sendMessage(data)
}
}
experimental.preferences.javascriptEnabled: true experimental.preferences.javascriptEnabled: true
experimental.preferences.navigatorQtObjectEnabled: true experimental.preferences.navigatorQtObjectEnabled: true
experimental.preferences.developerExtrasEnabled: true experimental.preferences.developerExtrasEnabled: true
experimental.userScripts: ["../ext/pre.js", "../ext/big.js", "../ext/string.js", "../ext/ethereum.js"] experimental.userScripts: ["../ext/q.js", "../ext/pre.js", "../ext/big.js", "../ext/string.js", "../ext/html_messaging.js"]
experimental.onMessageReceived: { experimental.onMessageReceived: {
console.log("[onMessageReceived]: ", message.data) console.log("[onMessageReceived]: ", message.data)
// TODO move to messaging.js // TODO move to messaging.js
@ -150,6 +158,10 @@ Rectangle {
try { try {
switch(data.call) { switch(data.call) {
case "compile":
postData(data._seed, eth.compile(data.args[0]))
break
case "getCoinBase": case "getCoinBase":
postData(data._seed, eth.coinBase()) postData(data._seed, eth.coinBase())
@ -191,7 +203,8 @@ Rectangle {
case "transact": case "transact":
require(5) require(5)
var tx = eth.transact(data.args[0], data.args[1], data.args[2],data.args[3],data.args[4],data.args[5]) var tx = eth.transact(data.args)
console.log("transactx", tx)
postData(data._seed, tx) postData(data._seed, tx)
break break

View File

@ -94,9 +94,7 @@ func (self *DebuggerWindow) ClearLog() {
} }
func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, dataStr string) { func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, dataStr string) {
if !self.Db.done { self.Stop()
self.Db.Q <- true
}
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
@ -186,6 +184,12 @@ func (self *DebuggerWindow) Continue() {
self.Next() self.Next()
} }
func (self *DebuggerWindow) Stop() {
if !self.Db.done {
self.Db.Q <- true
}
}
func (self *DebuggerWindow) ExecCommand(command string) { func (self *DebuggerWindow) ExecCommand(command string) {
if len(command) > 0 { if len(command) > 0 {
cmd := strings.Split(command, " ") cmd := strings.Split(command, " ")

View File

@ -248,3 +248,12 @@ func (self *UiLib) Transact(object map[string]interface{}) (*ethpipe.JSReceipt,
dataStr, dataStr,
) )
} }
func (self *UiLib) Compile(code string) (string, error) {
bcode, err := ethutil.Compile(code, false)
if err != nil {
return err.Error(), err
}
return ethutil.Bytes2Hex(bcode), err
}