Browse Source

evaluate natspec expressions

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
949f9df012
  1. 14
      alethzero/OurWebThreeStubServer.cpp
  2. 25
      libjsqrc/natspec.js

14
alethzero/OurWebThreeStubServer.cpp

@ -68,10 +68,9 @@ bool OurWebThreeStubServer::authenticate(dev::TransactionSkeleton const& _t)
std::string userNotice = m_main->lookupNatSpecUserNotice(contractCodeHash, _t.data); std::string userNotice = m_main->lookupNatSpecUserNotice(contractCodeHash, _t.data);
// TODO: uncomment this if (userNotice.empty())
// if (userNotice.empty()) return showAuthenticationPopup("Unverified Pending Transaction",
// return showAuthenticationPopup("Unverified Pending Transaction", "An undocumented transaction is about to be executed.");
// "An undocumented transaction is about to be executed.");
QNatspecExpressionEvaluator evaluator(this, m_main); QNatspecExpressionEvaluator evaluator(this, m_main);
userNotice = evaluator.evalExpression(QString::fromStdString(userNotice)).toStdString(); userNotice = evaluator.evalExpression(QString::fromStdString(userNotice)).toStdString();
@ -93,9 +92,10 @@ QString QNatspecExpressionEvaluator::evalExpression(QString const& _expression)
// evaluate the natspec // evaluate the natspec
m_main->evalRaw(contentsOfQResource(":/js/natspec.js")); m_main->evalRaw(contentsOfQResource(":/js/natspec.js"));
(void)_expression; // _expression should be in the format like this
auto result = m_main->evalRaw(QString::fromStdString((string)"evaluateExpression('" + "multiply(4)" + "')")); // auto toEval = QString::fromStdString("the result of calling multiply(4) is `multiply(4)`");
// auto result = m_main->evalRaw(_expression); auto toEval = _expression;
auto result = m_main->evalRaw("evaluateExpression('" + toEval + "')");
return result.toString(); return result.toString();
} }

25
libjsqrc/natspec.js

@ -19,7 +19,7 @@ var copyToContext = function (obj, context) {
/// Function called to get all contract's storage values /// Function called to get all contract's storage values
/// @returns hashmap with contract properties which are used /// @returns hashmap with contract properties which are used
var getContractProperties = function (expression, abi) { var getContractProperties = function (address, abi) {
return {}; return {};
}; };
@ -38,13 +38,32 @@ var evaluateExpression = function (expression) {
var abi = web3._currentContractAbi; var abi = web3._currentContractAbi;
var address = web3._currentContractAddress; var address = web3._currentContractAddress;
var storage = getContractProperties(expression, abi); var storage = getContractProperties(address, abi);
var methods = getContractMethods(address, abi); var methods = getContractMethods(address, abi);
copyToContext(storage, self); copyToContext(storage, self);
copyToContext(methods, self); copyToContext(methods, self);
// TODO: test if it is safe // TODO: test if it is safe
return eval(expression).toString(); var evaluatedExpression = "";
// match everything in `` quotes
var pattern = /\`(?:\\.|[^`\\])*\`/gim
var match;
var lastIndex = 0;
while ((match = pattern.exec(expression)) !== null) {
var startIndex = pattern.lastIndex - match[0].length;
var toEval = match[0].slice(1, match[0].length - 1);
evaluatedExpression += expression.slice(lastIndex, startIndex);
evaluatedExpression += eval(toEval).toString();
lastIndex = pattern.lastIndex;
}
evaluatedExpression += expression.slice(lastIndex);
return evaluatedExpression;
}; };

Loading…
Cancel
Save