Browse Source

add GasMapWrapper

cl-refactor
yann300 10 years ago
parent
commit
16ce50dded
  1. 44
      mix/CodeModel.cpp
  2. 27
      mix/CodeModel.h

44
mix/CodeModel.cpp

@ -333,42 +333,36 @@ void CodeModel::runCompilationJob(int _jobId)
void CodeModel::gasEstimation(solidity::CompilerStack const& _cs) void CodeModel::gasEstimation(solidity::CompilerStack const& _cs)
{ {
m_gasCostsMaps.clear(); m_gasCostsMaps->deleteLater();
m_gasCostsMaps = new GasMapWrapper(this);
for (std::string n: _cs.getContractNames()) for (std::string n: _cs.getContractNames())
{ {
ContractDefinition const& contractDefinition = _cs.getContractDefinition(n); ContractDefinition const& contractDefinition = _cs.getContractDefinition(n);
QString sourceName = QString::fromStdString(*contractDefinition.getLocation().sourceName); QString sourceName = QString::fromStdString(*contractDefinition.getLocation().sourceName);
if (!m_gasCostsMaps.contains(sourceName)) if (!m_gasCostsMaps->contains(sourceName))
m_gasCostsMaps.insert(sourceName, QVariantList()); m_gasCostsMaps->insert(sourceName, QVariantList());
if (!contractDefinition.isFullyImplemented()) if (!contractDefinition.isFullyImplemented())
continue; continue;
dev::solidity::SourceUnit const& sourceUnit = _cs.getAST(*contractDefinition.getLocation().sourceName); dev::solidity::SourceUnit const& sourceUnit = _cs.getAST(*contractDefinition.getLocation().sourceName);
AssemblyItems const* items = _cs.getRuntimeAssemblyItems(n); AssemblyItems const* items = _cs.getRuntimeAssemblyItems(n);
StructuralGasEstimator estimator; StructuralGasEstimator estimator;
std::map<ASTNode const*, GasMeter::GasConsumption> gasCosts = estimator.breakToStatementLevel(estimator.performEstimation(*items, std::vector<ASTNode const*>({&sourceUnit})), {&sourceUnit}); std::map<ASTNode const*, GasMeter::GasConsumption> gasCosts = estimator.breakToStatementLevel(estimator.performEstimation(*items, std::vector<ASTNode const*>({&sourceUnit})), {&sourceUnit});
for (auto gasItem = gasCosts.begin(); gasItem != gasCosts.end(); ++gasItem) for (auto gasItem = gasCosts.begin(); gasItem != gasCosts.end(); ++gasItem)
{ {
SourceLocation const& location = gasItem->first->getLocation(); SourceLocation const& location = gasItem->first->getLocation();
GasMeter::GasConsumption cost = gasItem->second; GasMeter::GasConsumption cost = gasItem->second;
std::stringstream v; std::stringstream v;
v << cost.value; v << cost.value;
GasMap* gas = new GasMap(location.start, location.end, QString::fromStdString(v.str()), cost.isInfinite); m_gasCostsMaps->push(sourceName, location.start, location.end, QString::fromStdString(v.str()), cost.isInfinite);
m_gasCostsMaps.find(sourceName).value().push_back(QVariant::fromValue(gas));
} }
} }
} }
QVariantList CodeModel::gasCostByDocumentId(QString const& _documentId) const QVariantList CodeModel::gasCostByDocumentId(QString const& _documentId) const
{ {
auto gasIter = m_gasCostsMaps.find(_documentId); return m_gasCostsMaps->gasCostsByDocId(_documentId);
if (gasIter != m_gasCostsMaps.end())
return gasIter.value();
else
return QVariantList();
} }
void CodeModel::collectContracts(dev::solidity::CompilerStack const& _cs, std::vector<std::string> const& _sourceNames) void CodeModel::collectContracts(dev::solidity::CompilerStack const& _cs, std::vector<std::string> const& _sourceNames)
@ -560,3 +554,29 @@ QString CodeModel::resolveFunctionName(dev::SourceLocation const& _location)
} }
return QString(); return QString();
} }
void GasMapWrapper::push(QString _source, int _start, int _end, QString _value, bool _isInfinite)
{
GasMap* gas = new GasMap(_start, _end, _value, _isInfinite, this);
m_gasMaps.find(_source).value().push_back(QVariant::fromValue(gas));
}
bool GasMapWrapper::contains(QString _key)
{
return m_gasMaps.contains(_key);
}
void GasMapWrapper::insert(QString _source, QVariantList _variantList)
{
m_gasMaps.insert(_source, _variantList);
}
QVariantList GasMapWrapper::gasCostsByDocId(QString _source)
{
auto gasIter = m_gasMaps.find(_source);
if (gasIter != m_gasMaps.end())
return gasIter.value();
else
return QVariantList();
}

27
mix/CodeModel.h

@ -127,6 +127,26 @@ struct SourceMap
LocationMap functions; LocationMap functions;
}; };
using SourceMaps = QMap<QString, SourceMap>; //by source id
using GasCostsMaps = QMap<QString, QVariantList>; //gas cost by contract name
class GasMapWrapper: public QObject
{
Q_OBJECT
Q_PROPERTY(GasCostsMaps gasMaps MEMBER m_gasMaps CONSTANT)
public:
GasMapWrapper(QObject* _parent): QObject(_parent){}
void push(QString _source, int _start, int _end, QString _value, bool _isInfinite);
bool contains(QString _key);
void insert(QString _source, QVariantList _variantList);
QVariantList gasCostsByDocId(QString _source);
private:
GasCostsMaps m_gasMaps;
};
class GasMap: public QObject class GasMap: public QObject
{ {
Q_OBJECT Q_OBJECT
@ -137,7 +157,7 @@ class GasMap: public QObject
Q_PROPERTY(bool isInfinite MEMBER m_isInfinite CONSTANT) Q_PROPERTY(bool isInfinite MEMBER m_isInfinite CONSTANT)
public: public:
GasMap(int _start, int _end, QString _gas, bool _isInfinite): m_start(_start), m_end(_end), m_gas(_gas), m_isInfinite(_isInfinite) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); } GasMap(int _start, int _end, QString _gas, bool _isInfinite, QObject _parent): QObject(_parent), m_start(_start), m_end(_end), m_gas(_gas), m_isInfinite(_isInfinite) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); }
int m_start; int m_start;
int m_end; int m_end;
@ -145,9 +165,6 @@ public:
bool m_isInfinite; bool m_isInfinite;
}; };
using SourceMaps = QMap<QString, SourceMap>; //by source id
using GasCostsMaps = QMap<QString, QVariantList>; //gas cost by contract name
/// Code compilation model. Compiles contracts in background an provides compiled contract data /// Code compilation model. Compiles contracts in background an provides compiled contract data
class CodeModel: public QObject class CodeModel: public QObject
{ {
@ -227,7 +244,7 @@ private:
mutable dev::Mutex x_contractMap; mutable dev::Mutex x_contractMap;
ContractMap m_contractMap; ContractMap m_contractMap;
SourceMaps m_sourceMaps; SourceMaps m_sourceMaps;
GasCostsMaps m_gasCostsMaps; GasMapWrapper* m_gasCostsMaps;
std::unique_ptr<CodeHighlighterSettings> m_codeHighlighterSettings; std::unique_ptr<CodeHighlighterSettings> m_codeHighlighterSettings;
QThread m_backgroundThread; QThread m_backgroundThread;
BackgroundWorker m_backgroundWorker; BackgroundWorker m_backgroundWorker;

Loading…
Cancel
Save