From 949f9df012747e5de653a7556e374850e292c877 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 22 Jan 2015 20:11:27 +0100 Subject: [PATCH] evaluate natspec expressions --- alethzero/OurWebThreeStubServer.cpp | 14 +++++++------- libjsqrc/natspec.js | 25 ++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/alethzero/OurWebThreeStubServer.cpp b/alethzero/OurWebThreeStubServer.cpp index 28705653f..3e13cd8ff 100644 --- a/alethzero/OurWebThreeStubServer.cpp +++ b/alethzero/OurWebThreeStubServer.cpp @@ -68,10 +68,9 @@ bool OurWebThreeStubServer::authenticate(dev::TransactionSkeleton const& _t) std::string userNotice = m_main->lookupNatSpecUserNotice(contractCodeHash, _t.data); - // TODO: uncomment this -// if (userNotice.empty()) -// return showAuthenticationPopup("Unverified Pending Transaction", -// "An undocumented transaction is about to be executed."); + if (userNotice.empty()) + return showAuthenticationPopup("Unverified Pending Transaction", + "An undocumented transaction is about to be executed."); QNatspecExpressionEvaluator evaluator(this, m_main); userNotice = evaluator.evalExpression(QString::fromStdString(userNotice)).toStdString(); @@ -93,9 +92,10 @@ QString QNatspecExpressionEvaluator::evalExpression(QString const& _expression) // evaluate the natspec m_main->evalRaw(contentsOfQResource(":/js/natspec.js")); - (void)_expression; - auto result = m_main->evalRaw(QString::fromStdString((string)"evaluateExpression('" + "multiply(4)" + "')")); -// auto result = m_main->evalRaw(_expression); + // _expression should be in the format like this + // auto toEval = QString::fromStdString("the result of calling multiply(4) is `multiply(4)`"); + auto toEval = _expression; + auto result = m_main->evalRaw("evaluateExpression('" + toEval + "')"); return result.toString(); } diff --git a/libjsqrc/natspec.js b/libjsqrc/natspec.js index 684dd58cc..1f4ba5871 100644 --- a/libjsqrc/natspec.js +++ b/libjsqrc/natspec.js @@ -19,7 +19,7 @@ var copyToContext = function (obj, context) { /// Function called to get all contract's storage values /// @returns hashmap with contract properties which are used -var getContractProperties = function (expression, abi) { +var getContractProperties = function (address, abi) { return {}; }; @@ -38,13 +38,32 @@ var evaluateExpression = function (expression) { var abi = web3._currentContractAbi; var address = web3._currentContractAddress; - var storage = getContractProperties(expression, abi); + var storage = getContractProperties(address, abi); var methods = getContractMethods(address, abi); copyToContext(storage, self); copyToContext(methods, self); // 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; };