From 1eadb691218a0e481602aa0088191530e6c4b432 Mon Sep 17 00:00:00 2001 From: Scott Tsai Date: Mon, 20 May 2019 23:19:28 +0800 Subject: [PATCH] remix-solidity: add compiler.setEvmVersion() Add a way to pass the `evmVersion` parameter to `solc` by: 1. Simplify the `Compiler.compileJSON()` interface from `compileJSON(source, optimize)` to `compileJSON(source)` Whether optimization is enabled is then always determined by the `Compiler.optimize` object field which simplifies data flow. 2. Add a `Compiler.evmVersion` field and a setter and make `compileJSON()` honor that. Specifying `evmVersion` is useful when targetting an older private chain with the latest `solc` or when using an EVM-compatible chain that's slightly behind Ethereum main-net in terms of features. The unused `userAgent` variable in `onInternalCompilerLoaded` was only removed to fix `npm run test` for `remix-solidity`'. --- remix-solidity/src/compiler/compiler-input.js | 8 ++++++-- remix-solidity/src/compiler/compiler.js | 20 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/remix-solidity/src/compiler/compiler-input.js b/remix-solidity/src/compiler/compiler-input.js index c5de89f735..6060e4a70e 100644 --- a/remix-solidity/src/compiler/compiler-input.js +++ b/remix-solidity/src/compiler/compiler-input.js @@ -1,7 +1,7 @@ 'use strict' module.exports = (sources, opts) => { - return JSON.stringify({ + const o = { language: 'Solidity', sources: sources, settings: { @@ -17,5 +17,9 @@ module.exports = (sources, opts) => { } } } - }) + } + if (opts.evmVersion) { + o.settings.evmVersion = opts.evmVersion + } + return JSON.stringify(o) } diff --git a/remix-solidity/src/compiler/compiler.js b/remix-solidity/src/compiler/compiler.js index 3766d8beea..f8ffc1e7f7 100644 --- a/remix-solidity/src/compiler/compiler.js +++ b/remix-solidity/src/compiler/compiler.js @@ -27,10 +27,16 @@ function Compiler (handleImportCall) { var optimize = false + var evmVersion = null + this.setOptimize = function (_optimize) { optimize = _optimize } + this.setEvmVersion = function (_evmVersion) { + evmVersion = _evmVersion + } + var compilationStartTime = null this.event.register('compilationFinished', (success, data, source) => { if (success && compilationStartTime) { @@ -49,7 +55,7 @@ function Compiler (handleImportCall) { self.lastCompilationResult = null self.event.trigger('compilationFinished', [false, {'error': { formattedMessage: error, severity: 'error' }}, files]) } else { - compileJSON(input, optimize ? 1 : 0) + compileJSON(input) } }) } @@ -73,14 +79,13 @@ function Compiler (handleImportCall) { function onInternalCompilerLoaded () { if (worker === null) { var compiler - var userAgent = (typeof (navigator) !== 'undefined') && navigator.userAgent ? navigator.userAgent.toLowerCase() : '-' if (typeof (window) === 'undefined') { compiler = require('solc') } else { compiler = solc(window.Module) } - compileJSON = function (source, optimize, cb) { + compileJSON = function (source) { var missingInputs = [] var missingInputsCallback = function (path) { missingInputs.push(path) @@ -89,7 +94,7 @@ function Compiler (handleImportCall) { var result try { - var input = compilerInput(source.sources, {optimize: optimize, target: source.target}) + var input = compilerInput(source.sources, {optimize: optimize, evmVersion: evmVersion, target: source.target}) result = compiler.compile(input, missingInputsCallback) result = JSON.parse(result) } catch (exception) { @@ -242,7 +247,7 @@ function Compiler (handleImportCall) { window.Module = undefined // Set a safe fallback until the new one is loaded - setCompileJSON(function (source, optimize) { + setCompileJSON(function (source) { compilationFinished({ error: { formattedMessage: 'Compiler not yet loaded.' } }) }) @@ -293,9 +298,10 @@ function Compiler (handleImportCall) { worker.addEventListener('error', function (msg) { compilationFinished({ error: 'Worker error: ' + msg.data }) }) - compileJSON = function (source, optimize) { + compileJSON = function (source) { jobs.push({sources: source}) - worker.postMessage({cmd: 'compile', job: jobs.length - 1, input: compilerInput(source.sources, {optimize: optimize, target: source.target})}) + worker.postMessage({cmd: 'compile', job: jobs.length - 1, input: compilerInput(source.sources, + {optimize: optimize, evmVersion: evmVersion, target: source.target})}) } worker.postMessage({cmd: 'loadVersion', data: url}) }