Merge pull request #7584 from ethereum/callback-context

Introduce callback context in libsolc
This commit is contained in:
Mathias L. Baumann 2019-11-26 15:29:03 +01:00 committed by GitHub
commit a45eed6dbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 12 deletions

View File

@ -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.

View File

@ -38,7 +38,7 @@ using namespace solidity;
namespace
{
ReadCallback::Callback wrapReadCallback(CStyleReadFileCallback _readCallback = nullptr)
ReadCallback::Callback wrapReadCallback(CStyleReadFileCallback _readCallback, void* _readContext)
{
ReadCallback::Callback readCallback;
if (_readCallback)
@ -47,7 +47,7 @@ ReadCallback::Callback wrapReadCallback(CStyleReadFileCallback _readCallback = n
{
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 = n
return readCallback;
}
string compile(string _input, CStyleReadFileCallback _readCallback = nullptr)
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

View File

@ -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.
///

View File

@ -35,7 +35,7 @@ function solcjs_test
SOLCJS_INPUT_DIR="$TEST_DIR"/test/externalTests/solc-js
# set up solc-js on the branch specified
setup master_060
setup callback-context
printLog "Updating index.js file..."
echo "require('./determinism.js');" >> test/index.js

View File

@ -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")

View File

@ -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;