Skip to content

Commit

Permalink
Patch funcfiftlib and emulator-emscripten builds (#905)
Browse files Browse the repository at this point in the history
Co-authored-by: SpyCheese <mikle98@yandex.ru>
  • Loading branch information
EmelyanenkoK and SpyCheese authored Feb 16, 2024
1 parent eb4831d commit a4d618b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
1 change: 1 addition & 0 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ if (USE_EMSCRIPTEN)
target_link_options(funcfiftlib PRIVATE -sALLOW_MEMORY_GROWTH=1)
target_link_options(funcfiftlib PRIVATE -sALLOW_TABLE_GROWTH=1)
target_link_options(funcfiftlib PRIVATE --embed-file ${CMAKE_CURRENT_SOURCE_DIR}/fift/lib@/fiftlib)
target_link_options(funcfiftlib PRIVATE --pre-js ${CMAKE_CURRENT_SOURCE_DIR}/funcfiftlib/funcfiftlib-prejs.js)
target_link_options(funcfiftlib PRIVATE -fexceptions)
target_compile_options(funcfiftlib PRIVATE -fexceptions -fno-stack-protector)
endif()
Expand Down
1 change: 1 addition & 0 deletions crypto/funcfiftlib/funcfiftlib-prejs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 1 addition & 24 deletions crypto/funcfiftlib/funcfiftlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,6 @@
#include <sstream>
#include <iomanip>

std::string escape_json(const std::string &s) {
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) {
case '"': o << "\\\""; break;
case '\\': o << "\\\\"; break;
case '\b': o << "\\b"; break;
case '\f': o << "\\f"; break;
case '\n': o << "\\n"; break;
case '\r': o << "\\r"; break;
case '\t': o << "\\t"; break;
default:
if ('\x00' <= *c && *c <= '\x1f') {
o << "\\u"
<< std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(*c);
} else {
o << *c;
}
}
}
return o.str();
}

td::Result<std::string> compile_internal(char *config_json) {
TRY_RESULT(input_json, td::json_decode(td::MutableSlice(config_json)))
auto &obj = input_json.get_object();
Expand Down Expand Up @@ -91,7 +68,7 @@ td::Result<std::string> compile_internal(char *config_json) {
auto result_obj = result_json.enter_object();
result_obj("status", "ok");
result_obj("codeBoc", td::base64_encode(boc));
result_obj("fiftCode", escape_json(outs.str()));
result_obj("fiftCode", outs.str());
result_obj("codeHashHex", code_cell->get_hash().to_hex());
result_obj.leave();

Expand Down
3 changes: 2 additions & 1 deletion emulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ if (USE_EMSCRIPTEN)
add_executable(emulator-emscripten ${EMULATOR_EMSCRIPTEN_SOURCE})
target_link_libraries(emulator-emscripten PUBLIC emulator)
target_link_options(emulator-emscripten PRIVATE -sEXPORTED_RUNTIME_METHODS=_malloc,free,UTF8ToString,stringToUTF8,allocate,ALLOC_NORMAL,lengthBytesUTF8)
target_link_options(emulator-emscripten PRIVATE -sEXPORTED_FUNCTIONS=_emulate,_free,_run_get_method)
target_link_options(emulator-emscripten PRIVATE -sEXPORTED_FUNCTIONS=_emulate,_free,_run_get_method,_create_emulator,_destroy_emulator,_emulate_with_emulator)
target_link_options(emulator-emscripten PRIVATE -sEXPORT_NAME=EmulatorModule)
target_link_options(emulator-emscripten PRIVATE -sERROR_ON_UNDEFINED_SYMBOLS=0)
target_link_options(emulator-emscripten PRIVATE -Oz)
Expand All @@ -57,6 +57,7 @@ if (USE_EMSCRIPTEN)
target_link_options(emulator-emscripten PRIVATE -sMODULARIZE=1)
target_link_options(emulator-emscripten PRIVATE -sENVIRONMENT=web)
target_link_options(emulator-emscripten PRIVATE -sFILESYSTEM=0)
target_link_options(emulator-emscripten PRIVATE -sALLOW_MEMORY_GROWTH=1)
target_link_options(emulator-emscripten PRIVATE -fexceptions)
if (USE_EMSCRIPTEN_NO_WASM)
target_link_options(emulator-emscripten PRIVATE -sWASM=0)
Expand Down
43 changes: 38 additions & 5 deletions emulator/emulator-emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,39 @@ td::Result<GetMethodParams> decode_get_method_params(const char* json) {
return params;
}

class NoopLog : public td::LogInterface {
public:
NoopLog() {
}

void append(td::CSlice new_slice, int log_level) override {
}

void rotate() override {
}
};

extern "C" {

const char *emulate(const char *config, const char* libs, int verbosity, const char* account, const char* message, const char* params) {
void* create_emulator(const char *config, int verbosity) {
NoopLog logger;

td::log_interface = &logger;

SET_VERBOSITY_LEVEL(verbosity_NEVER);
return transaction_emulator_create(config, verbosity);
}

void destroy_emulator(void* em) {
NoopLog logger;

td::log_interface = &logger;

SET_VERBOSITY_LEVEL(verbosity_NEVER);
transaction_emulator_destroy(em);
}

const char *emulate_with_emulator(void* em, const char* libs, const char* account, const char* message, const char* params) {
StringLog logger;

td::log_interface = &logger;
Expand All @@ -138,8 +168,6 @@ const char *emulate(const char *config, const char* libs, int verbosity, const c
}
auto decoded_params = decoded_params_res.move_as_ok();

auto em = transaction_emulator_create(config, verbosity);

bool rand_seed_set = true;
if (decoded_params.rand_seed_hex) {
rand_seed_set = transaction_emulator_set_rand_seed(em, decoded_params.rand_seed_hex.unwrap().c_str());
Expand All @@ -162,8 +190,6 @@ const char *emulate(const char *config, const char* libs, int verbosity, const c
result = transaction_emulator_emulate_transaction(em, account, message);
}

transaction_emulator_destroy(em);

const char* output = nullptr;
{
td::JsonBuilder jb;
Expand All @@ -178,6 +204,13 @@ const char *emulate(const char *config, const char* libs, int verbosity, const c
return output;
}

const char *emulate(const char *config, const char* libs, int verbosity, const char* account, const char* message, const char* params) {
auto em = transaction_emulator_create(config, verbosity);
auto result = emulate_with_emulator(em, libs, account, message, params);
transaction_emulator_destroy(em);
return result;
}

const char *run_get_method(const char *params, const char* stack, const char* config) {
StringLog logger;

Expand Down

0 comments on commit a4d618b

Please sign in to comment.