2024-10-31 06:51:07 +00:00
|
|
|
/*
|
|
|
|
This file is part of TON Blockchain source code.
|
|
|
|
|
|
|
|
TON Blockchain is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
TON Blockchain is distributed in the hope that it will be useful,
|
|
|
|
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 TON Blockchain. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
You must obey the GNU General Public License in all respects for all
|
|
|
|
of the code used other than OpenSSL. If you modify file(s) with this
|
|
|
|
exception, you may extend this exception to your version of the file(s),
|
|
|
|
but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
exception statement from your version. If you delete this exception statement
|
|
|
|
from all source files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
#include "tolk.h"
|
2024-10-31 07:11:41 +00:00
|
|
|
#include "tolk-version.h"
|
2024-10-31 07:02:01 +00:00
|
|
|
#include "compiler-state.h"
|
2024-10-31 06:51:07 +00:00
|
|
|
#include "git.h"
|
|
|
|
#include "td/utils/JsonBuilder.h"
|
|
|
|
#include "fift/utils.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include <sstream>
|
2024-10-31 07:02:01 +00:00
|
|
|
|
|
|
|
using namespace tolk;
|
2024-10-31 06:51:07 +00:00
|
|
|
|
2024-10-31 07:16:19 +00:00
|
|
|
static td::Result<std::string> compile_internal(char *config_json) {
|
2024-10-31 06:51:07 +00:00
|
|
|
TRY_RESULT(input_json, td::json_decode(td::MutableSlice(config_json)))
|
2024-10-31 06:59:23 +00:00
|
|
|
td::JsonObject& config = input_json.get_object();
|
2024-10-31 06:51:07 +00:00
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
TRY_RESULT(opt_level, td::get_json_object_int_field(config, "optimizationLevel", true, 2));
|
|
|
|
TRY_RESULT(stack_comments, td::get_json_object_bool_field(config, "withStackComments", true, false));
|
2024-10-31 07:04:58 +00:00
|
|
|
TRY_RESULT(entrypoint_filename, td::get_json_object_string_field(config, "entrypointFileName", false));
|
2024-10-31 07:11:41 +00:00
|
|
|
TRY_RESULT(experimental_options, td::get_json_object_string_field(config, "experimentalOptions", true));
|
2024-10-31 06:51:07 +00:00
|
|
|
|
2024-10-31 07:02:01 +00:00
|
|
|
G.settings.verbosity = 0;
|
|
|
|
G.settings.optimization_level = std::max(0, opt_level);
|
|
|
|
G.settings.stack_layout_comments = stack_comments;
|
2024-10-31 07:11:41 +00:00
|
|
|
if (!experimental_options.empty()) {
|
|
|
|
G.settings.parse_experimental_options_cmd_arg(experimental_options.c_str());
|
|
|
|
}
|
2024-10-31 06:51:07 +00:00
|
|
|
|
|
|
|
std::ostringstream outs, errs;
|
2024-10-31 07:02:01 +00:00
|
|
|
std::cout.rdbuf(outs.rdbuf());
|
|
|
|
std::cerr.rdbuf(errs.rdbuf());
|
2024-10-31 07:16:19 +00:00
|
|
|
int exit_code = tolk_proceed(entrypoint_filename);
|
|
|
|
if (exit_code != 0) {
|
2024-10-31 06:59:23 +00:00
|
|
|
return td::Status::Error("Tolk compilation error: " + errs.str());
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
TRY_RESULT(fift_res, fift::compile_asm_program(outs.str(), "/fiftlib/"));
|
2024-10-31 06:51:07 +00:00
|
|
|
|
|
|
|
td::JsonBuilder result_json;
|
2024-10-31 06:59:23 +00:00
|
|
|
auto obj = result_json.enter_object();
|
|
|
|
obj("status", "ok");
|
|
|
|
obj("fiftCode", fift_res.fiftCode);
|
|
|
|
obj("codeBoc64", fift_res.codeBoc64);
|
|
|
|
obj("codeHashHex", fift_res.codeHashHex);
|
2024-10-31 07:02:01 +00:00
|
|
|
obj("stderr", errs.str().c_str());
|
2024-10-31 06:59:23 +00:00
|
|
|
obj.leave();
|
2024-10-31 06:51:07 +00:00
|
|
|
|
|
|
|
return result_json.string_builder().as_cslice().str();
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
/// Callback used to retrieve file contents from a "not file system". See tolk-js for implementation.
|
|
|
|
/// The callback must fill either destContents or destError.
|
|
|
|
/// The implementor must use malloc() for them and use free() after tolk_compile returns.
|
2024-10-31 07:16:19 +00:00
|
|
|
typedef void (*WasmFsReadCallback)(int kind, char const* data, char** destContents, char** destError);
|
|
|
|
|
|
|
|
static CompilerSettings::FsReadCallback wrap_wasm_read_callback(WasmFsReadCallback _readCallback) {
|
|
|
|
return [_readCallback](CompilerSettings::FsReadCallbackKind kind, char const* data) -> td::Result<std::string> {
|
|
|
|
char* destContents = nullptr;
|
|
|
|
char* destError = nullptr;
|
|
|
|
if (_readCallback) {
|
2024-10-31 06:59:23 +00:00
|
|
|
_readCallback(static_cast<int>(kind), data, &destContents, &destError);
|
2024-10-31 07:16:19 +00:00
|
|
|
}
|
|
|
|
if (destContents) {
|
|
|
|
return destContents;
|
|
|
|
}
|
|
|
|
if (destError) {
|
2024-10-31 06:59:23 +00:00
|
|
|
return td::Status::Error(std::string(destError));
|
2024-10-31 07:16:19 +00:00
|
|
|
}
|
|
|
|
return td::Status::Error("Invalid callback from wasm");
|
|
|
|
};
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
const char* version() {
|
2024-10-31 07:16:19 +00:00
|
|
|
td::JsonBuilder version_json = td::JsonBuilder();
|
2024-10-31 06:51:07 +00:00
|
|
|
auto obj = version_json.enter_object();
|
2024-10-31 07:11:41 +00:00
|
|
|
obj("tolkVersion", TOLK_VERSION);
|
2024-10-31 06:51:07 +00:00
|
|
|
obj("tolkFiftLibCommitHash", GitMetadata::CommitSHA1());
|
|
|
|
obj("tolkFiftLibCommitDate", GitMetadata::CommitDate());
|
|
|
|
obj.leave();
|
|
|
|
return strdup(version_json.string_builder().as_cslice().c_str());
|
|
|
|
}
|
|
|
|
|
2024-10-31 07:16:19 +00:00
|
|
|
const char *tolk_compile(char *config_json, WasmFsReadCallback callback) {
|
|
|
|
G.settings.read_callback = wrap_wasm_read_callback(callback);
|
2024-10-31 06:51:07 +00:00
|
|
|
|
2024-10-31 07:02:01 +00:00
|
|
|
td::Result<std::string> res = compile_internal(config_json);
|
2024-10-31 06:51:07 +00:00
|
|
|
|
|
|
|
if (res.is_error()) {
|
2024-10-31 07:16:19 +00:00
|
|
|
td::JsonBuilder error_res = td::JsonBuilder();
|
|
|
|
auto obj = error_res.enter_object();
|
|
|
|
obj("status", "error");
|
|
|
|
obj("message", res.move_as_error().message().str());
|
|
|
|
obj.leave();
|
2024-10-31 06:51:07 +00:00
|
|
|
return strdup(error_res.string_builder().as_cslice().c_str());
|
|
|
|
}
|
|
|
|
|
2024-10-31 07:16:19 +00:00
|
|
|
std::string res_string = res.move_as_ok();
|
2024-10-31 06:51:07 +00:00
|
|
|
return strdup(res_string.c_str());
|
|
|
|
}
|
2024-10-31 07:16:19 +00:00
|
|
|
|
|
|
|
} // extern "C"
|