diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt index 0871d250..827c903b 100644 --- a/crypto/CMakeLists.txt +++ b/crypto/CMakeLists.txt @@ -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() diff --git a/crypto/funcfiftlib/funcfiftlib-prejs.js b/crypto/funcfiftlib/funcfiftlib-prejs.js new file mode 100644 index 00000000..38326c38 --- /dev/null +++ b/crypto/funcfiftlib/funcfiftlib-prejs.js @@ -0,0 +1 @@ +var crypto = { getRandomValues: function(array) { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } }; \ No newline at end of file diff --git a/crypto/funcfiftlib/funcfiftlib.cpp b/crypto/funcfiftlib/funcfiftlib.cpp index c8bf4fc5..a041c25d 100644 --- a/crypto/funcfiftlib/funcfiftlib.cpp +++ b/crypto/funcfiftlib/funcfiftlib.cpp @@ -34,29 +34,6 @@ #include #include -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(*c); - } else { - o << *c; - } - } - } - return o.str(); -} - td::Result compile_internal(char *config_json) { TRY_RESULT(input_json, td::json_decode(td::MutableSlice(config_json))) auto &obj = input_json.get_object(); @@ -91,7 +68,7 @@ td::Result 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(); diff --git a/emulator/CMakeLists.txt b/emulator/CMakeLists.txt index 969f9a88..7a4b7676 100644 --- a/emulator/CMakeLists.txt +++ b/emulator/CMakeLists.txt @@ -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) @@ -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) diff --git a/emulator/emulator-emscripten.cpp b/emulator/emulator-emscripten.cpp index dbd1c0d5..e76607c6 100644 --- a/emulator/emulator-emscripten.cpp +++ b/emulator/emulator-emscripten.cpp @@ -124,9 +124,39 @@ td::Result 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; @@ -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()); @@ -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; @@ -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;