Export StandardCompiler as compileStandard in soljson

This commit is contained in:
Alex Beregszaszi 2017-03-16 23:48:51 +00:00
parent 985db17e1e
commit 4f3c76364c

View File

@ -36,6 +36,7 @@
#include <libsolidity/analysis/NameAndTypeResolver.h> #include <libsolidity/analysis/NameAndTypeResolver.h>
#include <libsolidity/interface/Exceptions.h> #include <libsolidity/interface/Exceptions.h>
#include <libsolidity/interface/CompilerStack.h> #include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/StandardCompiler.h>
#include <libsolidity/interface/SourceReferenceFormatter.h> #include <libsolidity/interface/SourceReferenceFormatter.h>
#include <libsolidity/ast/ASTJsonConverter.h> #include <libsolidity/ast/ASTJsonConverter.h>
#include <libsolidity/interface/Version.h> #include <libsolidity/interface/Version.h>
@ -50,6 +51,41 @@ extern "C" {
typedef void (*CStyleReadFileCallback)(char const* _path, char** o_contents, char** o_error); typedef void (*CStyleReadFileCallback)(char const* _path, char** o_contents, char** o_error);
} }
ReadFile::Callback wrapReadCallback(CStyleReadFileCallback _readCallback = nullptr)
{
ReadFile::Callback readCallback;
if (_readCallback)
{
readCallback = [=](string const& _path)
{
char* contents_c = nullptr;
char* error_c = nullptr;
_readCallback(_path.c_str(), &contents_c, &error_c);
ReadFile::Result result;
result.success = true;
if (!contents_c && !error_c)
{
result.success = false;
result.contentsOrErrorMessage = "File not found.";
}
if (contents_c)
{
result.success = true;
result.contentsOrErrorMessage = string(contents_c);
free(contents_c);
}
if (error_c)
{
result.success = false;
result.contentsOrErrorMessage = string(error_c);
free(error_c);
}
return result;
};
}
return readCallback;
}
Json::Value functionHashes(ContractDefinition const& _contract) Json::Value functionHashes(ContractDefinition const& _contract)
{ {
Json::Value functionHashes(Json::objectValue); Json::Value functionHashes(Json::objectValue);
@ -103,37 +139,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback
{ {
Json::Value output(Json::objectValue); Json::Value output(Json::objectValue);
Json::Value errors(Json::arrayValue); Json::Value errors(Json::arrayValue);
ReadFile::Callback readCallback; CompilerStack compiler(wrapReadCallback(_readCallback));
if (_readCallback)
{
readCallback = [=](string const& _path)
{
char* contents_c = nullptr;
char* error_c = nullptr;
_readCallback(_path.c_str(), &contents_c, &error_c);
ReadFile::Result result;
result.success = true;
if (!contents_c && !error_c)
{
result.success = false;
result.contentsOrErrorMessage = "File not found.";
}
if (contents_c)
{
result.success = true;
result.contentsOrErrorMessage = string(contents_c);
free(contents_c);
}
if (error_c)
{
result.success = false;
result.contentsOrErrorMessage = string(error_c);
free(error_c);
}
return result;
};
}
CompilerStack compiler(readCallback);
auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return compiler.scanner(_sourceName); }; auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return compiler.scanner(_sourceName); };
bool success = false; bool success = false;
try try
@ -287,6 +293,13 @@ string compileSingle(string const& _input, bool _optimize)
return compile(sources, _optimize, nullptr); return compile(sources, _optimize, nullptr);
} }
string compileStandardInternal(string const& _input, CStyleReadFileCallback _readCallback = nullptr)
{
StandardCompiler compiler(wrapReadCallback(_readCallback));
return compiler.compile(_input);
}
static string s_outputBuffer; static string s_outputBuffer;
extern "C" extern "C"
@ -310,4 +323,9 @@ extern char const* compileJSONCallback(char const* _input, bool _optimize, CStyl
s_outputBuffer = compileMulti(_input, _optimize, _readCallback); s_outputBuffer = compileMulti(_input, _optimize, _readCallback);
return s_outputBuffer.c_str(); return s_outputBuffer.c_str();
} }
extern char const* compileStandard(char const* _input, CStyleReadFileCallback _readCallback)
{
s_outputBuffer = compileStandardInternal(_input, _readCallback);
return s_outputBuffer.c_str();
}
} }