solidity/libsolc/libsolc.cpp

110 lines
2.7 KiB
C++
Raw Normal View History

2015-01-28 07:50:53 +00:00
/*
This file is part of solidity.
2015-01-28 07:50:53 +00:00
solidity is free software: you can redistribute it and/or modify
2015-01-28 07:50:53 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
2015-01-28 07:50:53 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
2015-01-28 07:50:53 +00:00
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Public compiler API.
2015-01-28 07:50:53 +00:00
*/
2017-09-16 02:50:16 +00:00
#include <libsolc/libsolc.h>
#include <libsolidity/interface/StandardCompiler.h>
2015-10-20 22:21:52 +00:00
#include <libsolidity/interface/Version.h>
#include <libyul/YulString.h>
#include <libdevcore/Common.h>
#include <libdevcore/JSON.h>
2015-01-28 07:50:53 +00:00
2017-08-28 23:34:39 +00:00
#include <string>
#include "license.h"
2015-01-28 07:50:53 +00:00
using namespace std;
using namespace dev;
using namespace solidity;
2017-08-09 11:44:53 +00:00
namespace
{
2017-07-13 19:06:04 +00:00
ReadCallback::Callback wrapReadCallback(CStyleReadFileCallback _readCallback = nullptr)
{
2017-07-13 19:06:04 +00:00
ReadCallback::Callback readCallback;
if (_readCallback)
{
readCallback = [=](string const& _path)
{
char* contents_c = nullptr;
char* error_c = nullptr;
_readCallback(_path.c_str(), &contents_c, &error_c);
2017-07-13 19:06:04 +00:00
ReadCallback::Result result;
result.success = true;
if (!contents_c && !error_c)
{
result.success = false;
result.responseOrErrorMessage = "Callback not supported.";
}
if (contents_c)
{
result.success = true;
2017-07-13 19:06:04 +00:00
result.responseOrErrorMessage = string(contents_c);
free(contents_c);
}
if (error_c)
{
result.success = false;
2017-07-13 19:06:04 +00:00
result.responseOrErrorMessage = string(error_c);
free(error_c);
}
return result;
};
}
return readCallback;
}
2019-04-06 23:48:37 +00:00
string compile(string _input, CStyleReadFileCallback _readCallback = nullptr)
{
StandardCompiler compiler(wrapReadCallback(_readCallback));
2019-04-06 23:48:37 +00:00
return compiler.compile(std::move(_input));
}
2017-08-09 11:44:53 +00:00
}
2015-10-15 16:29:47 +00:00
static string s_outputBuffer;
2015-01-28 07:50:53 +00:00
extern "C"
{
extern char const* solidity_license() noexcept
{
2017-06-30 14:59:53 +00:00
static string fullLicenseText = otherLicenses + licenseText;
return fullLicenseText.c_str();
}
extern char const* solidity_version() noexcept
{
return VersionString.c_str();
}
extern char const* solidity_compile(char const* _input, CStyleReadFileCallback _readCallback) noexcept
{
s_outputBuffer = compile(_input, _readCallback);
return s_outputBuffer.c_str();
}
2019-01-22 11:41:21 +00:00
extern void solidity_free() noexcept
{
// This is called right before each compilation, but not at the end, so additional memory
// can be freed here.
yul::YulStringRepository::reset();
2019-01-22 11:41:21 +00:00
s_outputBuffer.clear();
}
2015-01-28 07:50:53 +00:00
}