Verified Commit 14f6bdee authored by nagayama15's avatar nagayama15

feat(ordering): add export-ordering method

parent 7d57b57f
#ifndef INCLUDE_kyut_methods_ExportOrdering_hpp
#define INCLUDE_kyut_methods_ExportOrdering_hpp
#include "../Ordering.hpp"
#include "wasm.h"
namespace kyut {
class CircularBitStreamReader;
class BitStreamWriter;
} // namespace kyut
namespace kyut::methods::export_ordering {
std::size_t embed(CircularBitStreamReader& r, wasm::Module& module, std::size_t chunk_size) {
const auto size_bits = embed_by_ordering(
r,
chunk_size,
std::begin(module.exports),
std::end(module.exports),
[](const auto& a, const auto& b) {
return a->name < b->name;
});
return size_bits;
}
std::size_t extract(BitStreamWriter& w, wasm::Module& module, std::size_t chunk_size) {
const auto size_bits = extract_by_ordering(
w,
chunk_size,
std::begin(module.exports),
std::end(module.exports),
[](const auto& a, const auto& b) {
return a->name < b->name;
});
return size_bits;
}
} // namespace kyut::methods::export_ordering
#endif // INCLUDE_kyut_methods_ExportOrdering_hpp
...@@ -46,6 +46,7 @@ namespace kyut::methods::function_ordering { ...@@ -46,6 +46,7 @@ namespace kyut::methods::function_ordering {
[](const auto& a, const auto& b) { [](const auto& a, const auto& b) {
return a->name < b->name; return a->name < b->name;
}); });
return size_bits; return size_bits;
} }
} // namespace kyut::methods::function_ordering } // namespace kyut::methods::function_ordering
......
#include <fmt/printf.h> #include <fmt/printf.h>
#include "cmdline.h" #include "cmdline.h"
#include "kyut/methods/ExportOrdering.hpp"
#include "kyut/methods/FunctionOrdering.hpp" #include "kyut/methods/FunctionOrdering.hpp"
#include "wasm-io.h" #include "wasm-io.h"
...@@ -14,7 +15,7 @@ int main(int argc, char* argv[]) { ...@@ -14,7 +15,7 @@ int main(int argc, char* argv[]) {
options.add("help", 'h', "Print help message"); options.add("help", 'h', "Print help message");
options.add("version", 'v', "Print version"); options.add("version", 'v', "Print version");
options.add<std::string>("method", 'm', "Embedding method (function-ordering)", true, "", cmdline::oneof<std::string>("function-ordering")); options.add<std::string>("method", 'm', "Embedding method (function-ordering, export-ordering)", true, "", cmdline::oneof<std::string>("function-ordering", "export-ordering"));
options.add<std::size_t>("chunk-size", 'c', "Chunk size [2~20]", false, 20, cmdline::range<std::size_t>(2, 20)); options.add<std::size_t>("chunk-size", 'c', "Chunk size [2~20]", false, 20, cmdline::range<std::size_t>(2, 20));
options.add<std::string>("dump", 0, "Output format (ascii, hex)", false, "ascii", cmdline::oneof<std::string>("ascii", "hex")); options.add<std::string>("dump", 0, "Output format (ascii, hex)", false, "ascii", cmdline::oneof<std::string>("ascii", "hex"));
...@@ -52,6 +53,8 @@ int main(int argc, char* argv[]) { ...@@ -52,6 +53,8 @@ int main(int argc, char* argv[]) {
std::size_t size_bits; std::size_t size_bits;
if (method == "function-ordering") { if (method == "function-ordering") {
size_bits = kyut::methods::function_ordering::extract(w, module, chunk_size); size_bits = kyut::methods::function_ordering::extract(w, module, chunk_size);
} else if (method == "export-ordering") {
size_bits = kyut::methods::export_ordering::extract(w, module, chunk_size);
} else { } else {
WASM_UNREACHABLE(("unknown method: " + method).c_str()); WASM_UNREACHABLE(("unknown method: " + method).c_str());
} }
......
#include <fmt/printf.h> #include <fmt/printf.h>
#include "cmdline.h" #include "cmdline.h"
#include "kyut/methods/ExportOrdering.hpp"
#include "kyut/methods/FunctionOrdering.hpp" #include "kyut/methods/FunctionOrdering.hpp"
#include "support/colors.h" #include "support/colors.h"
#include "wasm-io.h" #include "wasm-io.h"
...@@ -16,7 +17,7 @@ int main(int argc, char* argv[]) { ...@@ -16,7 +17,7 @@ int main(int argc, char* argv[]) {
options.add("version", 'v', "Print version"); options.add("version", 'v', "Print version");
options.add<std::string>("output", 'o', "Output filename", true); options.add<std::string>("output", 'o', "Output filename", true);
options.add<std::string>("method", 'm', "Embedding method (function-ordering)", true, "", cmdline::oneof<std::string>("function-ordering")); options.add<std::string>("method", 'm', "Embedding method (function-ordering, export-ordering)", true, "", cmdline::oneof<std::string>("function-ordering", "export-ordering"));
options.add<std::string>("watermark", 'w', "Watermark to embed", true); options.add<std::string>("watermark", 'w', "Watermark to embed", true);
options.add<std::size_t>("chunk-size", 'c', "Chunk size [2~20]", false, 20, cmdline::range<std::size_t>(2, 20)); options.add<std::size_t>("chunk-size", 'c', "Chunk size [2~20]", false, 20, cmdline::range<std::size_t>(2, 20));
...@@ -69,12 +70,14 @@ int main(int argc, char* argv[]) { ...@@ -69,12 +70,14 @@ int main(int argc, char* argv[]) {
std::size_t size_bits; std::size_t size_bits;
if (method == "function-ordering") { if (method == "function-ordering") {
size_bits = kyut::methods::function_ordering::embed(r, module, chunk_size); size_bits = kyut::methods::function_ordering::embed(r, module, chunk_size);
} else if (method == "export-ordering") {
size_bits = kyut::methods::export_ordering::embed(r, module, chunk_size);
} else { } else {
WASM_UNREACHABLE(("unknown method: " + method).c_str()); WASM_UNREACHABLE(("unknown method: " + method).c_str());
} }
Colors::setEnabled(false); Colors::setEnabled(false);
wasm::ModuleWriter{}.writeText(module, output); wasm::ModuleWriter{}.writeBinary(module, output);
fmt::print("{} bits\n", size_bits); fmt::print("{} bits\n", size_bits);
} catch (const std::exception& e) { } catch (const std::exception& e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment