diff --git a/Changelog.md b/Changelog.md index eea0f017f..2c46f09aa 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,7 @@ Breaking changes: * ABI: remove the deprecated ``constant`` and ``payable`` fields. * ABI: the ``type`` field is now required and no longer specified to default to ``function``. * C API (``libsolc``): the provided callback now takes two parameters, kind and data. The callback can then be used for multiple purposes, such has file imports and SMT queries. + * C API (``libsolc``): Introduce context parameter. * Commandline interface: remove the text-based ast printer (``--ast``). * Command line interface: Switch to the new error reporter by default. ``--old-reporter`` falls back to the deprecated old error reporter. * Command line interface: Add option to disable or choose hash method between IPFS and Swarm for the bytecode metadata. diff --git a/libsolc/libsolc.cpp b/libsolc/libsolc.cpp index 966c83d3f..ffe05eaef 100644 --- a/libsolc/libsolc.cpp +++ b/libsolc/libsolc.cpp @@ -38,7 +38,7 @@ using namespace solidity; namespace { -ReadCallback::Callback wrapReadCallback(CStyleReadFileCallback _readCallback) +ReadCallback::Callback wrapReadCallback(CStyleReadFileCallback _readCallback, void* _readContext) { ReadCallback::Callback readCallback; if (_readCallback) @@ -47,7 +47,7 @@ ReadCallback::Callback wrapReadCallback(CStyleReadFileCallback _readCallback) { char* contents_c = nullptr; char* error_c = nullptr; - _readCallback(_kind.c_str(), _data.c_str(), &contents_c, &error_c); + _readCallback(_readContext, _kind.c_str(), _data.c_str(), &contents_c, &error_c); ReadCallback::Result result; result.success = true; if (!contents_c && !error_c) @@ -73,9 +73,9 @@ ReadCallback::Callback wrapReadCallback(CStyleReadFileCallback _readCallback) return readCallback; } -string compile(string _input, CStyleReadFileCallback _readCallback) +string compile(string _input, CStyleReadFileCallback _readCallback, void* _readContext) { - StandardCompiler compiler(wrapReadCallback(_readCallback)); + StandardCompiler compiler(wrapReadCallback(_readCallback, _readContext)); return compiler.compile(std::move(_input)); } @@ -94,9 +94,9 @@ extern char const* solidity_version() noexcept { return VersionString.c_str(); } -extern char const* solidity_compile(char const* _input, CStyleReadFileCallback _readCallback) noexcept +extern char const* solidity_compile(char const* _input, CStyleReadFileCallback _readCallback, void* _readContext) noexcept { - s_outputBuffer = compile(_input, _readCallback); + s_outputBuffer = compile(_input, _readCallback, _readContext); return s_outputBuffer.c_str(); } extern void solidity_free() noexcept diff --git a/libsolc/libsolc.h b/libsolc/libsolc.h index ca7ded32b..22d43cba8 100644 --- a/libsolc/libsolc.h +++ b/libsolc/libsolc.h @@ -36,6 +36,7 @@ extern "C" { /// Callback used to retrieve additional source files or data. /// +/// @param _context The readContext passed to solidity_compile. Can be NULL. /// @param _kind The kind of callback (a string). /// @param _data The data for the callback. /// @param o_contents A pointer to the contents of the file, if found. @@ -44,7 +45,7 @@ extern "C" { /// If the callback is not supported, o_contents and o_error should be set to NULL. /// /// The two pointers (o_contents and o_error) should be heap-allocated and are free'd by the caller. -typedef void (*CStyleReadFileCallback)(char const* _kind, char const* _data, char** o_contents, char** o_error); +typedef void (*CStyleReadFileCallback)(void* _context, char const* _kind, char const* _data, char** o_contents, char** o_error); /// Returns the complete license document. /// @@ -61,9 +62,10 @@ char const* solidity_version() SOLC_NOEXCEPT; /// /// @param _input The input JSON to process. /// @param _readCallback The optional callback pointer. Can be NULL. +/// @param _readContext An optional context pointer passed to _readCallback. Can be NULL. /// /// @returns A pointer to the result. The pointer returned must not be freed by the caller. -char const* solidity_compile(char const* _input, CStyleReadFileCallback _readCallback) SOLC_NOEXCEPT; +char const* solidity_compile(char const* _input, CStyleReadFileCallback _readCallback, void* _readContext) SOLC_NOEXCEPT; /// Frees up any allocated memory. /// diff --git a/test/libsolidity/LibSolc.cpp b/test/libsolidity/LibSolc.cpp index 7beebf3ea..e2a208273 100644 --- a/test/libsolidity/LibSolc.cpp +++ b/test/libsolidity/LibSolc.cpp @@ -59,7 +59,7 @@ bool containsError(Json::Value const& _compilerResult, string const& _type, stri Json::Value compile(string const& _input, CStyleReadFileCallback _callback = nullptr) { - string output(solidity_compile(_input.c_str(), _callback)); + string output(solidity_compile(_input.c_str(), _callback, nullptr)); Json::Value ret; BOOST_REQUIRE(jsonParseStrict(output, ret)); solidity_free(); @@ -138,8 +138,10 @@ BOOST_AUTO_TEST_CASE(with_callback) )"; CStyleReadFileCallback callback{ - [](char const* _kind, char const* _path, char** o_contents, char** o_error) + [](void* _context, char const* _kind, char const* _path, char** o_contents, char** o_error) { + // Passed in a nullptr in the compile() helper above. + BOOST_CHECK(_context == nullptr); // Caller frees the pointers. BOOST_CHECK(string(_kind) == ReadCallback::kindString(ReadCallback::Kind::ReadFile)); if (string(_path) == "found.sol") diff --git a/test/tools/fuzzer_common.cpp b/test/tools/fuzzer_common.cpp index 5cd64a6c7..ba97545bc 100644 --- a/test/tools/fuzzer_common.cpp +++ b/test/tools/fuzzer_common.cpp @@ -42,7 +42,7 @@ void FuzzerUtil::runCompiler(string const& _input, bool _quiet) { if (!_quiet) cout << "Input JSON: " << _input << endl; - string outputString(solidity_compile(_input.c_str(), nullptr)); + string outputString(solidity_compile(_input.c_str(), nullptr, nullptr)); if (!_quiet) cout << "Output JSON: " << outputString << endl;