Пример #1
0
        public static Exception GetCompilerExceptions(Error[] errors, CompileErrorHandling errorHandling)
        {
            if (errorHandling == CompileErrorHandling.ThrowOnError)
            {
                var exs = errors.Where(e => e.Severity == Severity.Error).Select(e => new CompilerException(e)).ToArray();
                if (exs.Length == 1)
                {
                    return(exs[0]);
                }
                else if (exs.Length > 1)
                {
                    return(new AggregateException($"{exs.Length} compiler errors", exs));
                }
            }
            if (errorHandling == CompileErrorHandling.ThrowOnWarning)
            {
                var exs = errors.Where(e => e.Severity == Severity.Warning).Select(e => new CompilerException(e)).ToArray();
                if (exs.Length == 1)
                {
                    return(exs[0]);
                }
                else if (exs.Length > 1)
                {
                    return(new AggregateException($"{exs.Length} compiler warnings", exs));
                }
            }

            return(null);
        }
Пример #2
0
        public OutputDescription Compile(string[] contractFilePaths,
                                         OutputType[] outputSelection,
                                         Optimizer optimizer = null,
                                         CompileErrorHandling errorHandling = CompileErrorHandling.ThrowOnError,
                                         Dictionary <string, string> soliditySourceFileContent = null)
        {
            var inputDesc = new InputDescription();

            inputDesc.Settings.OutputSelection["*"] = new Dictionary <string, OutputType[]>
            {
                ["*"] = outputSelection,
                [""]  = outputSelection
            };

            if (optimizer != null)
            {
                inputDesc.Settings.Optimizer = optimizer;
            }

            foreach (var filePath in contractFilePaths)
            {
                var normalizedPath = filePath.Replace('\\', '/');
                var source         = new Source {
                    Urls = new List <string> {
                        normalizedPath
                    }
                };
                inputDesc.Sources[normalizedPath] = source;
            }

            return(Compile(inputDesc, errorHandling, soliditySourceFileContent));
        }
Пример #3
0
 /// <param name="outputSelection">Defaults to all output types if not specified</param>
 public OutputDescription Compile(string contractFilePaths,
                                  OutputType?outputSelection         = null,
                                  CompileErrorHandling errorHandling = CompileErrorHandling.ThrowOnError,
                                  Dictionary <string, string> soliditySourceFileContent = null)
 {
     return(Compile(new[] { contractFilePaths }, outputSelection, errorHandling, soliditySourceFileContent));
 }
Пример #4
0
        public OutputDescription Compile(InputDescription input,
                                         CompileErrorHandling errorHandling = CompileErrorHandling.ThrowOnError,
                                         Dictionary <string, string> soliditySourceFileContent = null)
        {
            var jsonStr = input.ToJsonString();

            return(CompileInputDescriptionJson(jsonStr, errorHandling, soliditySourceFileContent));
        }
Пример #5
0
        /// <param name="outputSelection">Defaults to all output types if not specified</param>
        public OutputDescription Compile(string[] contractFilePaths,
                                         OutputType?outputSelection         = null,
                                         CompileErrorHandling errorHandling = CompileErrorHandling.ThrowOnError,
                                         Dictionary <string, string> soliditySourceFileContent = null)
        {
            var outputs = outputSelection == null ? OutputTypes.All : OutputTypes.GetItems(outputSelection.Value);

            return(Compile(contractFilePaths, outputs, errorHandling, soliditySourceFileContent));
        }
Пример #6
0
        private OutputDescription CompileInputDescriptionJson(string jsonInput,
                                                              CompileErrorHandling errorHandling = CompileErrorHandling.ThrowOnError,
                                                              Dictionary <string, string> soliditySourceFileContent = null)
        {
            // Wrap the resolver object in a using to avoid it from being garbage collected during the
            // execution of the native solc compile function.
            using (var sourceResolver = new SourceFileResolver(_solSourceRoot, soliditySourceFileContent))
            {
                var res    = _native.Compile(jsonInput, sourceResolver.ReadFileDelegate);
                var output = OutputDescription.FromJsonString(res);

                var compilerException = CompilerException.GetCompilerExceptions(output.Errors, errorHandling);
                if (compilerException != null)
                {
                    throw compilerException;
                }
                return(output);
            }
        }