Support compiling to and running from binary files
This commit is contained in:
@@ -184,10 +184,40 @@ void Wassembler::EnableByteTranslationLogging()
|
||||
|
||||
bool Wassembler::CompileAndRun(std::string const & filePath)
|
||||
{
|
||||
std::vector<std::string> lines;
|
||||
std::vector<std::uint8_t> bytes;
|
||||
if (!CompileFile(filePath, bytes))
|
||||
if (filePath.size() > 4 && filePath.compare(filePath.size() - 4, 4, ".bin") == 0)
|
||||
{
|
||||
std::ifstream inputFile(filePath);
|
||||
if (!inputFile.is_open())
|
||||
{
|
||||
std::printf("Error: Cannot open file %s for reading", filePath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
std::size_t previousSize = 0;
|
||||
bytes.resize(100);
|
||||
while(inputFile.read(reinterpret_cast<char*>(&bytes[previousSize]), 100))
|
||||
{
|
||||
previousSize = bytes.size();
|
||||
bytes.resize(bytes.size() + 100);
|
||||
}
|
||||
|
||||
bytes.resize(bytes.size() - (100 - inputFile.gcount()));
|
||||
}
|
||||
else if (filePath.size() > 5 &&
|
||||
filePath.compare(filePath.size() - 5, 5, ".wasm") == 0)
|
||||
{
|
||||
if (!CompileFile(filePath, bytes))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::printf(
|
||||
"Error: unrecognized file extension on input file %s",
|
||||
filePath.c_str());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -221,9 +251,11 @@ bool Wassembler::CompileToFile(
|
||||
std::printf("Error: Cannot open file %s for writing", outputFilePath.c_str());
|
||||
return false;
|
||||
}
|
||||
for(std::size_t i = 0; i < bytes.size(); ++i)
|
||||
|
||||
if (!output.write(reinterpret_cast<char*>(bytes.data()), bytes.size()))
|
||||
{
|
||||
output << bytes[i];
|
||||
std::printf("Error: An error occurred whilst writing to %s", outputFilePath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user