diff --git a/mix/CodeModel.cpp b/mix/CodeModel.cpp index 137188e40..e622d7973 100644 --- a/mix/CodeModel.cpp +++ b/mix/CodeModel.cpp @@ -333,42 +333,36 @@ void CodeModel::runCompilationJob(int _jobId) void CodeModel::gasEstimation(solidity::CompilerStack const& _cs) { - m_gasCostsMaps.clear(); + m_gasCostsMaps->deleteLater(); + m_gasCostsMaps = new GasMapWrapper(this); for (std::string n: _cs.getContractNames()) { ContractDefinition const& contractDefinition = _cs.getContractDefinition(n); QString sourceName = QString::fromStdString(*contractDefinition.getLocation().sourceName); - if (!m_gasCostsMaps.contains(sourceName)) - m_gasCostsMaps.insert(sourceName, QVariantList()); + if (!m_gasCostsMaps->contains(sourceName)) + m_gasCostsMaps->insert(sourceName, QVariantList()); if (!contractDefinition.isFullyImplemented()) continue; dev::solidity::SourceUnit const& sourceUnit = _cs.getAST(*contractDefinition.getLocation().sourceName); AssemblyItems const* items = _cs.getRuntimeAssemblyItems(n); - StructuralGasEstimator estimator; std::map gasCosts = estimator.breakToStatementLevel(estimator.performEstimation(*items, std::vector({&sourceUnit})), {&sourceUnit}); - for (auto gasItem = gasCosts.begin(); gasItem != gasCosts.end(); ++gasItem) { SourceLocation const& location = gasItem->first->getLocation(); GasMeter::GasConsumption cost = gasItem->second; std::stringstream v; v << cost.value; - GasMap* gas = new GasMap(location.start, location.end, QString::fromStdString(v.str()), cost.isInfinite); - m_gasCostsMaps.find(sourceName).value().push_back(QVariant::fromValue(gas)); + m_gasCostsMaps->push(sourceName, location.start, location.end, QString::fromStdString(v.str()), cost.isInfinite); } } } QVariantList CodeModel::gasCostByDocumentId(QString const& _documentId) const { - auto gasIter = m_gasCostsMaps.find(_documentId); - if (gasIter != m_gasCostsMaps.end()) - return gasIter.value(); - else - return QVariantList(); + return m_gasCostsMaps->gasCostsByDocId(_documentId); } void CodeModel::collectContracts(dev::solidity::CompilerStack const& _cs, std::vector const& _sourceNames) @@ -560,3 +554,29 @@ QString CodeModel::resolveFunctionName(dev::SourceLocation const& _location) } 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(); +} + diff --git a/mix/CodeModel.h b/mix/CodeModel.h index 7cc9d3477..ca9658786 100644 --- a/mix/CodeModel.h +++ b/mix/CodeModel.h @@ -127,6 +127,26 @@ struct SourceMap LocationMap functions; }; +using SourceMaps = QMap; //by source id +using GasCostsMaps = QMap; //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 { Q_OBJECT @@ -137,7 +157,7 @@ class GasMap: public QObject Q_PROPERTY(bool isInfinite MEMBER m_isInfinite CONSTANT) 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_end; @@ -145,9 +165,6 @@ public: bool m_isInfinite; }; -using SourceMaps = QMap; //by source id -using GasCostsMaps = QMap; //gas cost by contract name - /// Code compilation model. Compiles contracts in background an provides compiled contract data class CodeModel: public QObject { @@ -227,7 +244,7 @@ private: mutable dev::Mutex x_contractMap; ContractMap m_contractMap; SourceMaps m_sourceMaps; - GasCostsMaps m_gasCostsMaps; + GasMapWrapper* m_gasCostsMaps; std::unique_ptr m_codeHighlighterSettings; QThread m_backgroundThread; BackgroundWorker m_backgroundWorker;