ttmg-wasm-tool 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/Release/wasm_tool_node.node +0 -0
- package/package.json +4 -7
- package/CMakeLists.txt +0 -126
- package/build/CMakeCache.txt +0 -430
- package/build/CMakeFiles/4.0.3/CMakeCCompiler.cmake +0 -84
- package/build/CMakeFiles/4.0.3/CMakeCXXCompiler.cmake +0 -104
- package/build/CMakeFiles/4.0.3/CMakeDetermineCompilerABI_C.bin +0 -0
- package/build/CMakeFiles/4.0.3/CMakeDetermineCompilerABI_CXX.bin +0 -0
- package/build/CMakeFiles/4.0.3/CMakeSystem.cmake +0 -15
- package/build/CMakeFiles/4.0.3/CompilerIdC/CMakeCCompilerId.c +0 -905
- package/build/CMakeFiles/4.0.3/CompilerIdC/a.out +0 -0
- package/build/CMakeFiles/4.0.3/CompilerIdC/apple-sdk.c +0 -1
- package/build/CMakeFiles/4.0.3/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -920
- package/build/CMakeFiles/4.0.3/CompilerIdCXX/a.out +0 -0
- package/build/CMakeFiles/4.0.3/CompilerIdCXX/apple-sdk.cpp +0 -1
- package/build/CMakeFiles/CMakeConfigureLog.yaml +0 -841
- package/build/CMakeFiles/CMakeDirectoryInformation.cmake +0 -16
- package/build/CMakeFiles/CheckTypeSize/SIZEOF_SIZE_T.bin +0 -0
- package/build/CMakeFiles/CheckTypeSize/SSIZE_T.bin +0 -0
- package/build/CMakeFiles/InstallScripts.json +0 -7
- package/build/CMakeFiles/Makefile.cmake +0 -152
- package/build/CMakeFiles/Makefile2 +0 -122
- package/build/CMakeFiles/TargetDirectories.txt +0 -3
- package/build/CMakeFiles/cmake.check_cache +0 -1
- package/build/CMakeFiles/progress.marks +0 -1
- package/build/CMakeFiles/wasm_tool_node.dir/DependInfo.cmake +0 -65
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/apply-names.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/apply-names.cc.o.d +0 -810
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary-reader-ir.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary-reader-ir.cc.o.d +0 -822
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary-reader-logging.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary-reader-logging.cc.o.d +0 -818
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary-reader.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary-reader.cc.o.d +0 -819
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary-writer.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary-writer.cc.o.d +0 -816
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binary.cc.o.d +0 -802
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binding-hash.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/binding-hash.cc.o.d +0 -808
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/color.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/color.cc.o.d +0 -808
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/common.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/common.cc.o.d +0 -804
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/config.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/config.cc.o.d +0 -106
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/error-formatter.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/error-formatter.cc.o.d +0 -807
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/expr-visitor.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/expr-visitor.cc.o.d +0 -810
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/feature.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/feature.cc.o.d +0 -804
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/filenames.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/filenames.cc.o.d +0 -802
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/generate-names.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/generate-names.cc.o.d +0 -811
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/hash-util.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/hash-util.cc.o.d +0 -779
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/ir-util.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/ir-util.cc.o.d +0 -822
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/ir.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/ir.cc.o.d +0 -824
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/leb128.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/leb128.cc.o.d +0 -803
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/lexer-source-line-finder.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/lexer-source-line-finder.cc.o.d +0 -804
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/lexer-source.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/lexer-source.cc.o.d +0 -803
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/literal.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/literal.cc.o.d +0 -808
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/opcode-code-table.c.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/opcode-code-table.c.o.d +0 -79
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/opcode.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/opcode.cc.o.d +0 -807
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/option-parser.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/option-parser.cc.o.d +0 -802
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/resolve-names.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/resolve-names.cc.o.d +0 -819
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/shared-validator.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/shared-validator.cc.o.d +0 -824
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/stream.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/stream.cc.o.d +0 -802
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/string-view.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/string-view.cc.o.d +0 -796
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/token.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/token.cc.o.d +0 -809
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tools/wasm-prepare.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tools/wasm-prepare.cc.o.d +0 -811
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tools/wasm-reader-writer.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tools/wasm-reader-writer.cc.o.d +0 -839
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tools/wasm-split.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tools/wasm-split.cc.o.d +0 -833
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tools/wat-writer.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tools/wat-writer.cc.o.d +0 -823
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tracing.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/tracing.cc.o.d +0 -802
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/type-checker.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/type-checker.cc.o.d +0 -814
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/utf8.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/utf8.cc.o.d +0 -87
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/validator.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/validator.cc.o.d +0 -827
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/wast-lexer.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/wast-lexer.cc.o.d +0 -821
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/wast-parser.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/wast-parser.cc.o.d +0 -835
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/wat-writer.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/Users/anrans/Desktop/workspace/ttop/ttmg-fe/packages/libs/wasm_tools/wasm-split/src/wat-writer.cc.o.d +0 -823
- package/build/CMakeFiles/wasm_tool_node.dir/build.make +0 -785
- package/build/CMakeFiles/wasm_tool_node.dir/cmake_clean.cmake +0 -95
- package/build/CMakeFiles/wasm_tool_node.dir/compiler_depend.make +0 -2
- package/build/CMakeFiles/wasm_tool_node.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/wasm_tool_node.dir/depend.make +0 -2
- package/build/CMakeFiles/wasm_tool_node.dir/flags.make +0 -21
- package/build/CMakeFiles/wasm_tool_node.dir/link.txt +0 -1
- package/build/CMakeFiles/wasm_tool_node.dir/progress.make +0 -45
- package/build/CMakeFiles/wasm_tool_node.dir/src/napi-binding.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/src/napi-binding.cc.o.d +0 -787
- package/build/CMakeFiles/wasm_tool_node.dir/src/wasm-buffer-ops.cc.o +0 -0
- package/build/CMakeFiles/wasm_tool_node.dir/src/wasm-buffer-ops.cc.o.d +0 -837
- package/build/Makefile +0 -1315
- package/build/cmake_install.cmake +0 -61
- package/build/compile_commands.json +0 -260
- package/build/config.h +0 -316
- package/src/napi-binding.cc +0 -139
- package/src/wasm-buffer-ops.cc +0 -867
- package/src/wasm-buffer-ops.h +0 -43
package/src/wasm-buffer-ops.cc
DELETED
|
@@ -1,867 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Buffer-based WASM operations implementation
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
#include "wasm-buffer-ops.h"
|
|
6
|
-
#include "src/binary-writer.h"
|
|
7
|
-
#include "src/apply-names.h"
|
|
8
|
-
#include "src/wast-lexer.h"
|
|
9
|
-
#include "src/stream.h"
|
|
10
|
-
#include "src/binary-reader-ir.h"
|
|
11
|
-
#include "src/common.h"
|
|
12
|
-
#include "src/validator.h"
|
|
13
|
-
#include "src/generate-names.h"
|
|
14
|
-
#include "src/wast-parser.h"
|
|
15
|
-
#include "src/ir.h"
|
|
16
|
-
#include "src/expr-visitor.h"
|
|
17
|
-
#include "src/tools/wat-writer.h"
|
|
18
|
-
|
|
19
|
-
#include <map>
|
|
20
|
-
#include <set>
|
|
21
|
-
|
|
22
|
-
using namespace wabt;
|
|
23
|
-
|
|
24
|
-
namespace wasm_tool {
|
|
25
|
-
|
|
26
|
-
// Helper to write WAT to a temporary buffer, then convert to WASM
|
|
27
|
-
static bool WatToWasmBuffer(const std::string& wat_content, std::vector<uint8_t>& output, std::string& error) {
|
|
28
|
-
std::unique_ptr<WastLexer> lexer = WastLexer::CreateBufferLexer(
|
|
29
|
-
"buffer.wat", wat_content.c_str(), wat_content.size());
|
|
30
|
-
|
|
31
|
-
Errors errors;
|
|
32
|
-
std::unique_ptr<Module> module;
|
|
33
|
-
static Features features;
|
|
34
|
-
WastParseOptions parse_wast_options(features);
|
|
35
|
-
|
|
36
|
-
Result result = ParseWatModule(lexer.get(), &module, &errors, &parse_wast_options);
|
|
37
|
-
if (!Succeeded(result) || !errors.empty()) {
|
|
38
|
-
error = "Failed to parse WAT";
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
ValidateOptions options(features);
|
|
43
|
-
result = ValidateModule(module.get(), &errors, options);
|
|
44
|
-
if (!Succeeded(result) || !errors.empty()) {
|
|
45
|
-
error = "Invalid WAT module";
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
MemoryStream stream;
|
|
50
|
-
WriteBinaryOptions binary_options;
|
|
51
|
-
result = WriteBinaryModule(&stream, module.get(), binary_options);
|
|
52
|
-
if (!Succeeded(result)) {
|
|
53
|
-
error = "Failed to write binary module";
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
auto& buffer = stream.output_buffer();
|
|
58
|
-
output.assign(buffer.data.begin(), buffer.data.end());
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Helper class for WASM module operations (for Prepare)
|
|
63
|
-
class WasmModule {
|
|
64
|
-
public:
|
|
65
|
-
WasmModule() : module_(MakeUnique<Module>()) {
|
|
66
|
-
InitStdio();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
bool ReadFromBuffer(const uint8_t* data, size_t size, bool generate_names = true) {
|
|
70
|
-
Errors errors;
|
|
71
|
-
ReadBinaryOptions options;
|
|
72
|
-
Result result = ReadBinaryIr("buffer", data, size, options, &errors, module_.get());
|
|
73
|
-
if (!Succeeded(result)) {
|
|
74
|
-
error_ = "Failed to read WASM binary";
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
result = ValidateModule(module_.get(), &errors, ValidateOptions());
|
|
79
|
-
if (!Succeeded(result)) {
|
|
80
|
-
error_ = "Invalid WASM module";
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (generate_names) {
|
|
85
|
-
result = GenerateNames(module_.get());
|
|
86
|
-
if (!Succeeded(result)) {
|
|
87
|
-
error_ = "Failed to generate names";
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
ApplyNames(module_.get());
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
parsed_ = true;
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
bool WriteToBuffer(std::vector<uint8_t>& output, bool write_debug_names = false) {
|
|
98
|
-
if (!parsed_) {
|
|
99
|
-
error_ = "Module not parsed";
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
MemoryStream stream;
|
|
104
|
-
WriteBinaryOptions binary_options;
|
|
105
|
-
binary_options.write_debug_names = write_debug_names;
|
|
106
|
-
Result result = WriteBinaryModule(&stream, module_.get(), binary_options);
|
|
107
|
-
if (!Succeeded(result)) {
|
|
108
|
-
error_ = "Failed to write binary module";
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
auto& buffer = stream.output_buffer();
|
|
113
|
-
output.assign(buffer.data.begin(), buffer.data.end());
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
Module* GetModule() { return module_.get(); }
|
|
118
|
-
uint64_t GetFuncCount() { return module_ ? module_->funcs.size() : 0; }
|
|
119
|
-
const std::string& GetError() const { return error_; }
|
|
120
|
-
bool IsParsed() const { return parsed_; }
|
|
121
|
-
|
|
122
|
-
private:
|
|
123
|
-
std::unique_ptr<Module> module_;
|
|
124
|
-
bool parsed_ = false;
|
|
125
|
-
std::string error_;
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
// Insert logCall instrumentation
|
|
129
|
-
void InsertLogCallInstrumentation(Module* module) {
|
|
130
|
-
std::string module_name = "scwebgl";
|
|
131
|
-
std::string field_name = "logCall";
|
|
132
|
-
std::string func_name = "$" + module_name + "." + field_name;
|
|
133
|
-
|
|
134
|
-
auto import = MakeUnique<FuncImport>();
|
|
135
|
-
import->func.decl.sig.param_types.push_back(Type(Type::Enum::I32));
|
|
136
|
-
auto index = module->GetFuncTypeIndex(import->func.decl.sig);
|
|
137
|
-
if (index == kInvalidIndex) {
|
|
138
|
-
auto typeField = MakeUnique<TypeModuleField>();
|
|
139
|
-
auto func_type = MakeUnique<FuncType>();
|
|
140
|
-
func_type->sig = import->func.decl.sig;
|
|
141
|
-
typeField->type = std::move(func_type);
|
|
142
|
-
index = module->types.size();
|
|
143
|
-
typeField->type->name = "$t" + std::to_string(index);
|
|
144
|
-
}
|
|
145
|
-
import->func.decl.has_func_type = true;
|
|
146
|
-
import->func.decl.type_var = Var("$t" + std::to_string(index));
|
|
147
|
-
import->func.name = func_name;
|
|
148
|
-
import->module_name = module_name;
|
|
149
|
-
import->field_name = field_name;
|
|
150
|
-
auto field = MakeUnique<ImportModuleField>(std::move(import));
|
|
151
|
-
module->fields.push_back(std::move(field));
|
|
152
|
-
|
|
153
|
-
Index func_index = 0;
|
|
154
|
-
for (auto iter = module->funcs.begin(); iter < module->funcs.end(); ++iter) {
|
|
155
|
-
(*iter)->exprs.insert((*iter)->exprs.begin(), MakeUnique<CallExpr>(Var(func_name)));
|
|
156
|
-
(*iter)->exprs.insert((*iter)->exprs.begin(), MakeUnique<ConstExpr>(Const::I32(func_index++)));
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
PrepareResult PrepareWasmFromBuffer(const uint8_t* data, size_t size) {
|
|
161
|
-
PrepareResult result;
|
|
162
|
-
result.success = false;
|
|
163
|
-
result.func_count = 0;
|
|
164
|
-
|
|
165
|
-
WasmModule module;
|
|
166
|
-
if (!module.ReadFromBuffer(data, size)) {
|
|
167
|
-
result.error = module.GetError();
|
|
168
|
-
return result;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
result.func_count = module.GetFuncCount();
|
|
172
|
-
|
|
173
|
-
// Insert instrumentation
|
|
174
|
-
InsertLogCallInstrumentation(module.GetModule());
|
|
175
|
-
|
|
176
|
-
// Write back to buffer
|
|
177
|
-
if (!module.WriteToBuffer(result.wasm_buffer)) {
|
|
178
|
-
result.error = module.GetError();
|
|
179
|
-
return result;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
result.success = true;
|
|
183
|
-
return result;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// ============================================================================
|
|
187
|
-
// WasmSplit implementation (buffer-based, adapted from wasm-split.cc)
|
|
188
|
-
// ============================================================================
|
|
189
|
-
|
|
190
|
-
static void SplitString(const std::string& s, const std::string& separator, std::vector<std::string>& result) {
|
|
191
|
-
std::string::size_type begin_pos, end_pos;
|
|
192
|
-
end_pos = s.find(separator);
|
|
193
|
-
begin_pos = 0;
|
|
194
|
-
while (std::string::npos != end_pos) {
|
|
195
|
-
result.push_back(s.substr(begin_pos, end_pos - begin_pos));
|
|
196
|
-
begin_pos = end_pos + separator.size();
|
|
197
|
-
end_pos = s.find(separator, begin_pos);
|
|
198
|
-
}
|
|
199
|
-
if (begin_pos != s.length())
|
|
200
|
-
result.push_back(s.substr(begin_pos));
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
class WasmSplitBuffer {
|
|
204
|
-
public:
|
|
205
|
-
WasmSplitBuffer() {
|
|
206
|
-
InitStdio();
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
bool ReadModule(const uint8_t* data, size_t size, const std::string& func_id_list = "") {
|
|
210
|
-
Errors errors;
|
|
211
|
-
ReadBinaryOptions options;
|
|
212
|
-
Result result = ReadBinaryIr("buffer", data, size, options, &errors, &module_);
|
|
213
|
-
if (!Succeeded(result)) {
|
|
214
|
-
error_ = "Failed to read WASM binary";
|
|
215
|
-
return false;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
result = ValidateModule(&module_, &errors, ValidateOptions());
|
|
219
|
-
if (!Succeeded(result)) {
|
|
220
|
-
error_ = "Invalid WASM module";
|
|
221
|
-
return false;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
result = GenerateNames(&module_);
|
|
225
|
-
if (!Succeeded(result)) {
|
|
226
|
-
error_ = "Failed to generate names";
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
ApplyNames(&module_);
|
|
230
|
-
|
|
231
|
-
ReadAllInputFuncIds(func_id_list);
|
|
232
|
-
ReadAllFuncs();
|
|
233
|
-
ReadAllImportFuncs();
|
|
234
|
-
ReadAllExportFuncs();
|
|
235
|
-
CheckHasImportTableAndMemory();
|
|
236
|
-
ReadSubWatFuncs();
|
|
237
|
-
ReadAllElemFuncs();
|
|
238
|
-
ReadAllCallExprNotInMainWatFuncs();
|
|
239
|
-
ReadAllCallExprNotInSubWatFuncs();
|
|
240
|
-
GetIndirectCallFuncsInMainWat();
|
|
241
|
-
GetAllFuncElems();
|
|
242
|
-
UpdateTableSizeIfNeed();
|
|
243
|
-
|
|
244
|
-
return true;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
bool WriteMainWasm(std::vector<uint8_t>& output) {
|
|
248
|
-
MemoryStream stream;
|
|
249
|
-
bytedance::WatWriter wat_writer(&stream, module_);
|
|
250
|
-
wat_writer.PrepareModule();
|
|
251
|
-
|
|
252
|
-
wat_writer.WriteModuleBegin();
|
|
253
|
-
wat_writer.WriteTypes();
|
|
254
|
-
WriteImportsForMainWat(wat_writer);
|
|
255
|
-
WriteAlternativeFuncsForMainWat(wat_writer);
|
|
256
|
-
WriteExtraFuncsForMainWat(wat_writer);
|
|
257
|
-
WriteIndirectCallFuncsForMainWat(wat_writer);
|
|
258
|
-
wat_writer.WriteTables();
|
|
259
|
-
wat_writer.WriteMemories();
|
|
260
|
-
WriteImmutableGlobalValues(wat_writer);
|
|
261
|
-
WriteOneGlobalValue(wat_writer);
|
|
262
|
-
WriteExportsForMainWat(wat_writer);
|
|
263
|
-
wat_writer.WriteStarts();
|
|
264
|
-
WriteElementsForMainWat(wat_writer);
|
|
265
|
-
wat_writer.WriteDataSegments();
|
|
266
|
-
wat_writer.WriteModuleEnd();
|
|
267
|
-
|
|
268
|
-
// Get WAT content and convert to WASM
|
|
269
|
-
const auto& buffer = stream.output_buffer();
|
|
270
|
-
std::string wat_content(buffer.data.begin(), buffer.data.end());
|
|
271
|
-
|
|
272
|
-
return WatToWasmBuffer(wat_content, output, error_);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
bool WriteSubWasm(std::vector<uint8_t>& output) {
|
|
276
|
-
MemoryStream stream;
|
|
277
|
-
bytedance::WatWriter wat_writer(&stream, module_);
|
|
278
|
-
wat_writer.PrepareModule();
|
|
279
|
-
|
|
280
|
-
wat_writer.WriteModuleBegin();
|
|
281
|
-
wat_writer.WriteTypes();
|
|
282
|
-
WriteImportsForSubWat(wat_writer);
|
|
283
|
-
WriteAlternativeFuncsForSubWat(wat_writer);
|
|
284
|
-
WriteIndirectCallFuncsForSubWat(wat_writer);
|
|
285
|
-
WriteImmutableGlobalValues(wat_writer);
|
|
286
|
-
WriteOneGlobalValue(wat_writer);
|
|
287
|
-
WriteElementsForSubWat(wat_writer);
|
|
288
|
-
wat_writer.WriteModuleEnd();
|
|
289
|
-
|
|
290
|
-
// Get WAT content and convert to WASM
|
|
291
|
-
const auto& buffer = stream.output_buffer();
|
|
292
|
-
std::string wat_content(buffer.data.begin(), buffer.data.end());
|
|
293
|
-
|
|
294
|
-
return WatToWasmBuffer(wat_content, output, error_);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
uint64_t GetTotalFuncCount() const { return all_funcs_.size(); }
|
|
298
|
-
uint64_t GetMainFuncCount() const { return main_wat_funcs_.size(); }
|
|
299
|
-
uint64_t GetSubFuncCount() const { return sub_wat_funcs_.size(); }
|
|
300
|
-
int64_t GetTableSize() const { return table_initial_size_; }
|
|
301
|
-
const std::string& GetGlobalVarList() const { return global_var_list_; }
|
|
302
|
-
const std::string& GetError() const { return error_; }
|
|
303
|
-
|
|
304
|
-
private:
|
|
305
|
-
// Expression visitor for finding call instructions
|
|
306
|
-
class WasmExprVisitor : public ExprVisitor::DelegateNop {
|
|
307
|
-
public:
|
|
308
|
-
explicit WasmExprVisitor(WasmSplitBuffer* splitter, bool is_main_wat)
|
|
309
|
-
: splitter_(splitter), is_main_wat_(is_main_wat) {}
|
|
310
|
-
|
|
311
|
-
Result OnCallExpr(CallExpr* expr) override {
|
|
312
|
-
const auto func_name = expr->var.name();
|
|
313
|
-
if (is_main_wat_) {
|
|
314
|
-
if (!funcs_set_.count(func_name)) {
|
|
315
|
-
splitter_->call_not_in_main_wat_funcs_.push_back(func_name);
|
|
316
|
-
funcs_set_.insert(func_name);
|
|
317
|
-
}
|
|
318
|
-
} else {
|
|
319
|
-
if (!splitter_->sub_wat_funcs_.count(func_name)) {
|
|
320
|
-
if (!funcs_set_.count(func_name)) {
|
|
321
|
-
splitter_->call_not_in_sub_wat_funcs_.push_back(func_name);
|
|
322
|
-
funcs_set_.insert(func_name);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
return Result::Ok;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
private:
|
|
330
|
-
WasmSplitBuffer* splitter_;
|
|
331
|
-
bool is_main_wat_;
|
|
332
|
-
std::set<std::string> funcs_set_;
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
void ReadAllInputFuncIds(const std::string& content) {
|
|
336
|
-
if (content.empty()) return;
|
|
337
|
-
std::vector<std::string> lines;
|
|
338
|
-
SplitString(content, "\n", lines);
|
|
339
|
-
auto total_funcs = module_.funcs.size();
|
|
340
|
-
for (size_t i = 0; i < lines.size(); ++i) {
|
|
341
|
-
const auto& line = lines[i];
|
|
342
|
-
if (line.empty()) continue;
|
|
343
|
-
try {
|
|
344
|
-
auto id = std::stoi(line);
|
|
345
|
-
if (id > 0 && static_cast<size_t>(id) < total_funcs) {
|
|
346
|
-
main_wat_funcs_.insert(module_.funcs[id]->name);
|
|
347
|
-
}
|
|
348
|
-
} catch (...) {}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
void ReadAllFuncs() {
|
|
353
|
-
int func_index = 0;
|
|
354
|
-
for (auto iter = module_.funcs.begin(); iter < module_.funcs.end(); ++iter) {
|
|
355
|
-
all_funcs_[(*iter)->name] = *iter;
|
|
356
|
-
all_func_indexes_[(*iter)->name] = func_index++;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
void CheckHasImportTableAndMemory() {
|
|
361
|
-
has_import_table_ = false;
|
|
362
|
-
has_import_memory_ = false;
|
|
363
|
-
for (auto p_import : module_.imports) {
|
|
364
|
-
if (p_import->kind() == ExternalKind::Table) {
|
|
365
|
-
has_import_table_ = true;
|
|
366
|
-
} else if (p_import->kind() == ExternalKind::Memory) {
|
|
367
|
-
has_import_memory_ = true;
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
void ReadAllImportFuncs() {
|
|
373
|
-
for (auto p_import : module_.imports) {
|
|
374
|
-
if (p_import->kind() == ExternalKind::Func) {
|
|
375
|
-
std::string func_name = "$" + p_import->module_name + "." + p_import->field_name;
|
|
376
|
-
import_funcs_.insert(func_name);
|
|
377
|
-
main_wat_funcs_.insert(func_name);
|
|
378
|
-
all_funcs_in_main_wat_.push_back(func_name);
|
|
379
|
-
all_funcs_in_sub_wat_.push_back(func_name);
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
void ReadAllExportFuncs() {
|
|
385
|
-
for (auto p_export : module_.exports) {
|
|
386
|
-
if (p_export->kind == ExternalKind::Func) {
|
|
387
|
-
export_funcs_.insert(p_export->var.name());
|
|
388
|
-
main_wat_funcs_.insert(p_export->var.name());
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
void ReadSubWatFuncs() {
|
|
394
|
-
for (auto func : module_.funcs) {
|
|
395
|
-
if (main_wat_funcs_.count(func->name) && !import_funcs_.count(func->name)) {
|
|
396
|
-
continue;
|
|
397
|
-
}
|
|
398
|
-
sub_wat_funcs_.insert(func->name);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
void ReadAllElemFuncs() {
|
|
403
|
-
std::set<std::string> export_funcs_set;
|
|
404
|
-
all_elem_funcs_after_split_.clear();
|
|
405
|
-
int offset = 0;
|
|
406
|
-
for (auto elem : module_.elem_segments) {
|
|
407
|
-
if (!elem->offset.empty()) {
|
|
408
|
-
auto iter = elem->offset.begin();
|
|
409
|
-
if (iter->type() == wabt::ExprType::Const) {
|
|
410
|
-
offset = dynamic_cast<ConstExpr*>(&(*iter))->const_.u32();
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
if (elem->elem_type == Type::Enum::FuncRef) {
|
|
414
|
-
for (const auto& elem_expr : elem->elem_exprs) {
|
|
415
|
-
if (elem_expr.kind == ElemExprKind::RefFunc) {
|
|
416
|
-
all_elem_funcs_after_split_.push_back(elem_expr.var.name());
|
|
417
|
-
if (!export_funcs_set.count(elem_expr.var.name())) {
|
|
418
|
-
original_elem_funcs_.push_back(elem_expr.var.name());
|
|
419
|
-
export_funcs_set.insert(elem_expr.var.name());
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
elem_offset_ = offset;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
void ReadAllCallExprNotInMainWatFuncs() {
|
|
429
|
-
WasmExprVisitor delegate(this, true);
|
|
430
|
-
ExprVisitor visitor(&delegate);
|
|
431
|
-
for (const auto& func_name : main_wat_funcs_) {
|
|
432
|
-
if (all_funcs_.count(func_name) && !import_funcs_.count(func_name)) {
|
|
433
|
-
const auto p_func = all_funcs_[func_name];
|
|
434
|
-
visitor.VisitExprList(const_cast<ExprList&>(p_func->exprs));
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
void ReadAllCallExprNotInSubWatFuncs() {
|
|
440
|
-
WasmExprVisitor delegate(this, false);
|
|
441
|
-
ExprVisitor visitor(&delegate);
|
|
442
|
-
for (const auto& func_name : sub_wat_funcs_) {
|
|
443
|
-
if (all_funcs_.find(func_name) != all_funcs_.end()) {
|
|
444
|
-
const auto p_func = all_funcs_[func_name];
|
|
445
|
-
visitor.VisitExprList(const_cast<ExprList&>(p_func->exprs));
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
extra_elem_count_in_main_wat_ = call_not_in_sub_wat_funcs_.size();
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
void GetIndirectCallFuncsInMainWat() {
|
|
452
|
-
std::set<std::string> uniq_funcs;
|
|
453
|
-
indirect_call_in_main_wat_funcs_.clear();
|
|
454
|
-
for (const auto& func_name : original_elem_funcs_) {
|
|
455
|
-
if (main_wat_funcs_.count(func_name) || uniq_funcs.count(func_name)) continue;
|
|
456
|
-
uniq_funcs.insert(func_name);
|
|
457
|
-
indirect_call_in_main_wat_funcs_.push_back(func_name);
|
|
458
|
-
}
|
|
459
|
-
for (const auto& func_name : call_not_in_main_wat_funcs_) {
|
|
460
|
-
if (main_wat_funcs_.count(func_name) || uniq_funcs.count(func_name)) continue;
|
|
461
|
-
uniq_funcs.insert(func_name);
|
|
462
|
-
indirect_call_in_main_wat_funcs_.push_back(func_name);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
void GetAllFuncElems() {
|
|
467
|
-
all_elem_funcs_after_split_.insert(
|
|
468
|
-
all_elem_funcs_after_split_.end(),
|
|
469
|
-
call_not_in_sub_wat_funcs_.begin(),
|
|
470
|
-
call_not_in_sub_wat_funcs_.end());
|
|
471
|
-
all_elem_funcs_after_split_.insert(
|
|
472
|
-
all_elem_funcs_after_split_.end(),
|
|
473
|
-
indirect_call_in_main_wat_funcs_.begin(),
|
|
474
|
-
indirect_call_in_main_wat_funcs_.end());
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
void UpdateTableSizeIfNeed() {
|
|
478
|
-
if (!module_.tables.empty()) {
|
|
479
|
-
auto& elem_limits = module_.tables[0]->elem_limits;
|
|
480
|
-
original_table_size_ = elem_limits.initial;
|
|
481
|
-
elem_limits.initial += indirect_call_in_main_wat_funcs_.size() + extra_elem_count_in_main_wat_;
|
|
482
|
-
table_initial_size_ = elem_limits.initial;
|
|
483
|
-
if (elem_limits.has_max) {
|
|
484
|
-
elem_limits.max += indirect_call_in_main_wat_funcs_.size() + extra_elem_count_in_main_wat_;
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
// Write functions for main WAT
|
|
490
|
-
void WriteAlternativeFuncsForMainWat(bytedance::WatWriter& writer) {
|
|
491
|
-
for (const auto& func_name : main_wat_funcs_) {
|
|
492
|
-
if (import_funcs_.count(func_name)) continue;
|
|
493
|
-
if (all_funcs_.count(func_name)) {
|
|
494
|
-
writer.WriteFunc(*all_funcs_[func_name]);
|
|
495
|
-
all_funcs_in_main_wat_.push_back(func_name);
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
Export CreateExport(const std::string& func_name, const std::string& var_name) {
|
|
501
|
-
Export exp;
|
|
502
|
-
exp.name = func_name;
|
|
503
|
-
exp.kind = ExternalKind::Func;
|
|
504
|
-
exp.var = Var(var_name);
|
|
505
|
-
return exp;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
void WriteExportsForMainWat(bytedance::WatWriter& writer) {
|
|
509
|
-
writer.WriteExports();
|
|
510
|
-
writer.WriteExport(CreateExport("setSubWasmLoaded", "$setSubWasmLoaded"));
|
|
511
|
-
writer.WriteExport(CreateExport("initGlobal", "$initGlobal"));
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
void WriteAlternativeFuncsForSubWat(bytedance::WatWriter& writer) {
|
|
515
|
-
for (const auto& func_name : sub_wat_funcs_) {
|
|
516
|
-
if (main_wat_funcs_.count(func_name)) continue;
|
|
517
|
-
if (all_funcs_.count(func_name)) {
|
|
518
|
-
auto p_func = all_funcs_[func_name];
|
|
519
|
-
if (func_name.substr(0, 2) == "$f") {
|
|
520
|
-
Index func_index = std::stoi(func_name.substr(2));
|
|
521
|
-
p_func->exprs.insert(p_func->exprs.begin(), MakeUnique<CallExpr>(Var("$scwebgl.logCall")));
|
|
522
|
-
p_func->exprs.insert(p_func->exprs.begin(), MakeUnique<ConstExpr>(Const::I32(func_index)));
|
|
523
|
-
}
|
|
524
|
-
writer.WriteFunc(*p_func);
|
|
525
|
-
all_funcs_in_sub_wat_.push_back(func_name);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
void WriteElementsForMainWat(bytedance::WatWriter& writer) {
|
|
531
|
-
writer.WriteElemSegments();
|
|
532
|
-
AppendElemsIfNeed(writer);
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
void WriteElementsForSubWat(bytedance::WatWriter& writer) {
|
|
536
|
-
if (indirect_call_in_main_wat_funcs_.empty()) return;
|
|
537
|
-
ElemSegment elem_segment("");
|
|
538
|
-
elem_segment.kind = SegmentKind::Active;
|
|
539
|
-
for (const auto& func_name : indirect_call_in_main_wat_funcs_) {
|
|
540
|
-
auto expr = ElemExpr();
|
|
541
|
-
expr.kind = ElemExprKind::RefFunc;
|
|
542
|
-
expr.var = Var(func_name);
|
|
543
|
-
elem_segment.elem_exprs.push_back(expr);
|
|
544
|
-
}
|
|
545
|
-
auto ptr = MakeUnique<ConstExpr>(Const::I32(original_table_size_ + extra_elem_count_in_main_wat_));
|
|
546
|
-
elem_segment.offset.push_back(std::move(ptr));
|
|
547
|
-
elem_segment.table_var = Var(0);
|
|
548
|
-
elem_segment.elem_type = Type::FuncRef;
|
|
549
|
-
writer.WriteElemSegment(elem_segment, 0);
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
void AppendElemsIfNeed(bytedance::WatWriter& writer) {
|
|
553
|
-
if (call_not_in_sub_wat_funcs_.empty()) return;
|
|
554
|
-
ElemSegment elem_segment("");
|
|
555
|
-
elem_segment.kind = SegmentKind::Active;
|
|
556
|
-
for (const auto& func_name : call_not_in_sub_wat_funcs_) {
|
|
557
|
-
auto expr = ElemExpr();
|
|
558
|
-
expr.kind = ElemExprKind::RefFunc;
|
|
559
|
-
expr.var = Var(func_name);
|
|
560
|
-
elem_segment.elem_exprs.push_back(expr);
|
|
561
|
-
}
|
|
562
|
-
auto ptr = MakeUnique<ConstExpr>(Const::I32(original_table_size_));
|
|
563
|
-
elem_segment.offset.push_back(std::move(ptr));
|
|
564
|
-
elem_segment.table_var = Var(0);
|
|
565
|
-
elem_segment.elem_type = Type::FuncRef;
|
|
566
|
-
writer.WriteElemSegment(elem_segment, module_.elem_segments.size());
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
static Global MakeGlobal(std::string name, bool mut = true, Type type = Type::I32, Const defaultVal = Const::I32(0)) {
|
|
570
|
-
Global global(name);
|
|
571
|
-
global.mutable_ = mut;
|
|
572
|
-
global.type = type;
|
|
573
|
-
auto const0Expr = MakeUnique<ConstExpr>(defaultVal);
|
|
574
|
-
global.init_expr.push_back(std::move(const0Expr));
|
|
575
|
-
return global;
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
void WriteImportMemory(bytedance::WatWriter& writer) {
|
|
579
|
-
if (!has_import_memory_ && !module_.memories.empty()) {
|
|
580
|
-
std::string moduleName = "scwebgl";
|
|
581
|
-
std::string fieldName = "memory";
|
|
582
|
-
MemoryImport import;
|
|
583
|
-
import.memory.name = module_.memories[0]->name;
|
|
584
|
-
import.module_name = moduleName;
|
|
585
|
-
import.field_name = fieldName;
|
|
586
|
-
auto& page_limits = module_.memories[0]->page_limits;
|
|
587
|
-
import.memory.page_limits.initial = page_limits.initial;
|
|
588
|
-
import.memory.page_limits.max = page_limits.max;
|
|
589
|
-
import.memory.page_limits.has_max = page_limits.has_max;
|
|
590
|
-
writer.WriteImport(import, 0);
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
void WriteImportTable(bytedance::WatWriter& writer) {
|
|
595
|
-
if (!has_import_table_ && !module_.tables.empty()) {
|
|
596
|
-
std::string moduleName = "scwebgl";
|
|
597
|
-
std::string fieldName = "table";
|
|
598
|
-
TableImport import;
|
|
599
|
-
import.table.name = module_.tables[0]->name;
|
|
600
|
-
import.module_name = moduleName;
|
|
601
|
-
import.field_name = fieldName;
|
|
602
|
-
auto elem_limits = module_.tables[0]->elem_limits;
|
|
603
|
-
import.table.elem_limits.initial = elem_limits.initial;
|
|
604
|
-
import.table.elem_limits.max = elem_limits.max;
|
|
605
|
-
import.table.elem_limits.has_max = elem_limits.has_max;
|
|
606
|
-
writer.WriteImport(import, 0);
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
void WriteWaitFunc(bytedance::WatWriter& writer) {
|
|
611
|
-
std::string moduleName = "scwebgl";
|
|
612
|
-
std::string fieldName = "wait";
|
|
613
|
-
std::string waitFuncName = "$" + moduleName + "." + fieldName;
|
|
614
|
-
FuncImport import;
|
|
615
|
-
import.func.decl.sig.param_types.push_back(Type(Type::Enum::I32));
|
|
616
|
-
auto index = module_.GetFuncTypeIndex(import.func.decl.sig);
|
|
617
|
-
if (index == kInvalidIndex) {
|
|
618
|
-
auto typeField = MakeUnique<TypeModuleField>();
|
|
619
|
-
auto func_type = MakeUnique<FuncType>();
|
|
620
|
-
func_type->sig = import.func.decl.sig;
|
|
621
|
-
typeField->type = std::move(func_type);
|
|
622
|
-
index = module_.types.size();
|
|
623
|
-
typeField->type->name = "$t" + std::to_string(index);
|
|
624
|
-
writer.WriteTypeEntry(*typeField->type, index);
|
|
625
|
-
}
|
|
626
|
-
import.func.decl.has_func_type = true;
|
|
627
|
-
import.func.decl.type_var = Var("$t" + std::to_string(index));
|
|
628
|
-
import.func.name = waitFuncName;
|
|
629
|
-
import.module_name = moduleName;
|
|
630
|
-
import.field_name = fieldName;
|
|
631
|
-
writer.WriteImport(import, -1);
|
|
632
|
-
all_funcs_in_main_wat_.push_back(import.func.name);
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
void WriteLogCallFunc(bytedance::WatWriter& writer) {
|
|
636
|
-
std::string moduleName = "scwebgl";
|
|
637
|
-
std::string fieldName = "logCall";
|
|
638
|
-
std::string waitFuncName = "$" + moduleName + "." + fieldName;
|
|
639
|
-
FuncImport import;
|
|
640
|
-
import.func.decl.sig.param_types.push_back(Type(Type::Enum::I32));
|
|
641
|
-
auto index = module_.GetFuncTypeIndex(import.func.decl.sig);
|
|
642
|
-
if (index == kInvalidIndex) {
|
|
643
|
-
auto typeField = MakeUnique<TypeModuleField>();
|
|
644
|
-
auto func_type = MakeUnique<FuncType>();
|
|
645
|
-
func_type->sig = import.func.decl.sig;
|
|
646
|
-
typeField->type = std::move(func_type);
|
|
647
|
-
index = module_.types.size();
|
|
648
|
-
typeField->type->name = "$t" + std::to_string(index);
|
|
649
|
-
writer.WriteTypeEntry(*typeField->type, index);
|
|
650
|
-
}
|
|
651
|
-
import.func.decl.has_func_type = true;
|
|
652
|
-
import.func.decl.type_var = Var("$t" + std::to_string(index));
|
|
653
|
-
import.func.name = waitFuncName;
|
|
654
|
-
import.module_name = moduleName;
|
|
655
|
-
import.field_name = fieldName;
|
|
656
|
-
writer.WriteImport(import, -1);
|
|
657
|
-
all_funcs_in_sub_wat_.push_back(import.func.name);
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
void WriteImportGlobals(bytedance::WatWriter& writer) {
|
|
661
|
-
std::string moduleName = "scwebgl";
|
|
662
|
-
const auto& globalFields = writer.GetGlobalFields();
|
|
663
|
-
auto globalImportsCount = globalFields.size();
|
|
664
|
-
std::vector<std::string> global_list;
|
|
665
|
-
for (size_t i = 0; i < globalImportsCount; ++i) {
|
|
666
|
-
const auto ptr = globalFields[i];
|
|
667
|
-
if (!ptr->global.mutable_) continue;
|
|
668
|
-
GlobalImport import;
|
|
669
|
-
import.global.name = ptr->global.name;
|
|
670
|
-
import.global.mutable_ = ptr->global.mutable_;
|
|
671
|
-
import.global.type = ptr->global.type;
|
|
672
|
-
import.module_name = moduleName;
|
|
673
|
-
import.field_name = "global" + std::to_string(i);
|
|
674
|
-
writer.WriteImport(import, -1);
|
|
675
|
-
std::string global_item = import.field_name + "," + ptr->global.type.GetName() + "," + std::to_string(int(import.global.mutable_));
|
|
676
|
-
global_list.push_back(global_item);
|
|
677
|
-
}
|
|
678
|
-
std::string global_str;
|
|
679
|
-
for (size_t i = 0; i < global_list.size(); ++i) {
|
|
680
|
-
global_str += global_list[i];
|
|
681
|
-
if (i < global_list.size() - 1) {
|
|
682
|
-
global_str += ";";
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
global_var_list_ = global_str;
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
void WriteImmutableGlobalValues(bytedance::WatWriter& writer) {
|
|
689
|
-
const auto& globalFields = writer.GetGlobalFields();
|
|
690
|
-
for (size_t i = 0; i < globalFields.size(); ++i) {
|
|
691
|
-
const auto ptr = globalFields[i];
|
|
692
|
-
if (!ptr->global.mutable_) {
|
|
693
|
-
writer.WriteGlobal(ptr->global, -1);
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
void WriteOneGlobalValue(bytedance::WatWriter& writer) {
|
|
699
|
-
std::string name = "$g" + std::to_string(module_.globals.size());
|
|
700
|
-
writer.WriteGlobal(MakeGlobal(name, true), -1);
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
void WriteImportsForMainWat(bytedance::WatWriter& writer) {
|
|
704
|
-
writer.WriteImports();
|
|
705
|
-
WriteWaitFunc(writer);
|
|
706
|
-
WriteImportGlobals(writer);
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
void WriteImportsForSubWat(bytedance::WatWriter& writer) {
|
|
710
|
-
WriteImportTable(writer);
|
|
711
|
-
WriteImportMemory(writer);
|
|
712
|
-
writer.WriteImports();
|
|
713
|
-
WriteLogCallFunc(writer);
|
|
714
|
-
WriteImportGlobals(writer);
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
void WriteExtraFuncsForMainWat(bytedance::WatWriter& writer) {
|
|
718
|
-
auto globalValue = "$g" + std::to_string(module_.globals.size());
|
|
719
|
-
auto ifExpr = MakeUnique<IfExpr>();
|
|
720
|
-
Func func1("$ensureSubWasm");
|
|
721
|
-
func1.exprs.push_back(std::move(MakeUnique<GlobalGetExpr>(Var(globalValue))));
|
|
722
|
-
func1.exprs.push_back(std::move(MakeUnique<ConvertExpr>(Opcode::I32Eqz)));
|
|
723
|
-
func1.decl.sig.param_types.push_back(Type(Type::Enum::I32));
|
|
724
|
-
ifExpr->true_.exprs.push_back(MakeUnique<LocalGetExpr>(Var(0)));
|
|
725
|
-
ifExpr->true_.exprs.push_back(MakeUnique<CallExpr>(Var("$scwebgl.wait")));
|
|
726
|
-
func1.exprs.push_back(std::move(ifExpr));
|
|
727
|
-
writer.WriteFunc(func1);
|
|
728
|
-
|
|
729
|
-
Func func2("$setSubWasmLoaded");
|
|
730
|
-
func2.exprs.push_back(std::move(MakeUnique<ConstExpr>(Const::I32(1))));
|
|
731
|
-
func2.exprs.push_back(std::move(MakeUnique<GlobalSetExpr>(Var(globalValue))));
|
|
732
|
-
func2.decl.sig.param_types.push_back(Type(Type::Enum::I32));
|
|
733
|
-
writer.WriteFunc(func2);
|
|
734
|
-
|
|
735
|
-
Func func3("$initGlobal");
|
|
736
|
-
const auto& globalFields = writer.GetGlobalFields();
|
|
737
|
-
for (size_t i = 0; i < globalFields.size(); ++i) {
|
|
738
|
-
const auto ptr = globalFields[i];
|
|
739
|
-
if (!ptr->global.mutable_) continue;
|
|
740
|
-
if (!ptr->global.init_expr.empty()) {
|
|
741
|
-
auto iter = ptr->global.init_expr.begin();
|
|
742
|
-
if (iter->type() == wabt::ExprType::Const) {
|
|
743
|
-
const auto& expr = dynamic_cast<const ConstExpr&>(*iter);
|
|
744
|
-
func3.exprs.push_back(std::move(MakeUnique<ConstExpr>(expr.const_)));
|
|
745
|
-
func3.exprs.push_back(std::move(MakeUnique<GlobalSetExpr>(Var(ptr->global.name))));
|
|
746
|
-
} else if (iter->type() == wabt::ExprType::GlobalGet) {
|
|
747
|
-
const auto& expr = dynamic_cast<const GlobalGetExpr&>(*iter);
|
|
748
|
-
func3.exprs.push_back(std::move(MakeUnique<GlobalGetExpr>(Var(expr.var.name()))));
|
|
749
|
-
func3.exprs.push_back(std::move(MakeUnique<GlobalSetExpr>(Var(ptr->global.name))));
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
writer.WriteFunc(func3);
|
|
754
|
-
|
|
755
|
-
all_funcs_in_main_wat_.push_back(func1.name);
|
|
756
|
-
all_funcs_in_main_wat_.push_back(func2.name);
|
|
757
|
-
all_funcs_in_main_wat_.push_back(func3.name);
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
void WriteIndirectCallFuncsForMainWat(bytedance::WatWriter& writer) {
|
|
761
|
-
Index index = original_table_size_ + extra_elem_count_in_main_wat_;
|
|
762
|
-
for (const auto& func_name : indirect_call_in_main_wat_funcs_) {
|
|
763
|
-
if (main_wat_funcs_.count(func_name)) continue;
|
|
764
|
-
if (all_funcs_.find(func_name) != all_funcs_.end()) {
|
|
765
|
-
Func func(func_name);
|
|
766
|
-
func.decl = all_funcs_[func_name]->decl;
|
|
767
|
-
if (func_name.substr(0, 2) == "$f") {
|
|
768
|
-
Index func_index = std::stoi(func_name.substr(2));
|
|
769
|
-
func.exprs.push_back(std::move(MakeUnique<ConstExpr>(Const::I32(func_index))));
|
|
770
|
-
func.exprs.push_back(std::move(MakeUnique<CallExpr>(Var("$ensureSubWasm"))));
|
|
771
|
-
for (size_t i = 0; i < func.decl.sig.param_types.size(); ++i) {
|
|
772
|
-
func.exprs.push_back(std::move(MakeUnique<LocalGetExpr>(Var(i))));
|
|
773
|
-
}
|
|
774
|
-
func.exprs.push_back(std::move(MakeUnique<ConstExpr>(Const::I32(index++))));
|
|
775
|
-
auto call_indirect_expr = MakeUnique<CallIndirectExpr>();
|
|
776
|
-
call_indirect_expr->decl.type_var = func.decl.type_var;
|
|
777
|
-
call_indirect_expr->table = Var(0);
|
|
778
|
-
func.exprs.push_back(std::move(call_indirect_expr));
|
|
779
|
-
writer.WriteFunc(func);
|
|
780
|
-
all_funcs_in_main_wat_.push_back(func_name);
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
void WriteIndirectCallFuncsForSubWat(bytedance::WatWriter& writer) {
|
|
787
|
-
Index index = original_table_size_;
|
|
788
|
-
for (const auto& func_name : call_not_in_sub_wat_funcs_) {
|
|
789
|
-
if (all_funcs_.find(func_name) != all_funcs_.end()) {
|
|
790
|
-
Func func(func_name);
|
|
791
|
-
func.decl = all_funcs_[func_name]->decl;
|
|
792
|
-
for (size_t i = 0; i < func.decl.sig.param_types.size(); ++i) {
|
|
793
|
-
func.exprs.push_back(std::move(MakeUnique<LocalGetExpr>(Var(i))));
|
|
794
|
-
}
|
|
795
|
-
func.exprs.push_back(std::move(MakeUnique<ConstExpr>(Const::I32(index++))));
|
|
796
|
-
auto call_indirect_expr = MakeUnique<CallIndirectExpr>();
|
|
797
|
-
call_indirect_expr->decl.type_var = func.decl.type_var;
|
|
798
|
-
call_indirect_expr->table = Var(0);
|
|
799
|
-
func.exprs.push_back(std::move(call_indirect_expr));
|
|
800
|
-
writer.WriteFunc(func);
|
|
801
|
-
all_funcs_in_sub_wat_.push_back(func_name);
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
Module module_;
|
|
807
|
-
std::map<std::string, Func*> all_funcs_;
|
|
808
|
-
std::map<std::string, int> all_func_indexes_;
|
|
809
|
-
std::set<std::string> import_funcs_;
|
|
810
|
-
std::set<std::string> export_funcs_;
|
|
811
|
-
std::vector<std::string> original_elem_funcs_;
|
|
812
|
-
std::vector<std::string> all_elem_funcs_after_split_;
|
|
813
|
-
std::vector<std::string> call_not_in_main_wat_funcs_;
|
|
814
|
-
std::vector<std::string> call_not_in_sub_wat_funcs_;
|
|
815
|
-
std::vector<std::string> all_funcs_in_main_wat_;
|
|
816
|
-
std::vector<std::string> all_funcs_in_sub_wat_;
|
|
817
|
-
std::vector<std::string> indirect_call_in_main_wat_funcs_;
|
|
818
|
-
std::set<std::string> main_wat_funcs_;
|
|
819
|
-
std::set<std::string> sub_wat_funcs_;
|
|
820
|
-
|
|
821
|
-
uint64_t original_table_size_ = 0;
|
|
822
|
-
uint64_t table_initial_size_ = 0;
|
|
823
|
-
uint64_t elem_offset_ = 0;
|
|
824
|
-
uint64_t extra_elem_count_in_main_wat_ = 0;
|
|
825
|
-
std::string global_var_list_;
|
|
826
|
-
bool has_import_table_ = false;
|
|
827
|
-
bool has_import_memory_ = false;
|
|
828
|
-
std::string error_;
|
|
829
|
-
|
|
830
|
-
friend class WasmExprVisitor;
|
|
831
|
-
};
|
|
832
|
-
|
|
833
|
-
SplitResult SplitWasmFromBuffer(const uint8_t* data, size_t size, const std::string& func_id_list) {
|
|
834
|
-
SplitResult result;
|
|
835
|
-
result.success = false;
|
|
836
|
-
result.total_func_count = 0;
|
|
837
|
-
result.main_func_count = 0;
|
|
838
|
-
result.sub_func_count = 0;
|
|
839
|
-
result.table_size = 0;
|
|
840
|
-
|
|
841
|
-
WasmSplitBuffer splitter;
|
|
842
|
-
if (!splitter.ReadModule(data, size, func_id_list)) {
|
|
843
|
-
result.error = splitter.GetError();
|
|
844
|
-
return result;
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
result.total_func_count = splitter.GetTotalFuncCount();
|
|
848
|
-
result.main_func_count = splitter.GetMainFuncCount();
|
|
849
|
-
result.sub_func_count = splitter.GetSubFuncCount();
|
|
850
|
-
result.table_size = splitter.GetTableSize();
|
|
851
|
-
result.global_var_list = splitter.GetGlobalVarList();
|
|
852
|
-
|
|
853
|
-
if (!splitter.WriteMainWasm(result.main_wasm_buffer)) {
|
|
854
|
-
result.error = splitter.GetError();
|
|
855
|
-
return result;
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
if (!splitter.WriteSubWasm(result.sub_wasm_buffer)) {
|
|
859
|
-
result.error = splitter.GetError();
|
|
860
|
-
return result;
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
result.success = true;
|
|
864
|
-
return result;
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
} // namespace wasm_tool
|