Skip to content

Commit

Permalink
+impl pack opcodeRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
guzibei committed Jan 27, 2025
1 parent b3ad677 commit 5fcaded
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/wabt/binary-writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ struct WriteBinaryOptions {

Result WriteBinaryModule(Stream*, const Module*, const WriteBinaryOptions&);

class OpcodeRawExpr;
Result PackOpcodeRawExpr(OpcodeRawExpr& expr,
Module& module,
const WriteBinaryOptions& options);

void WriteType(Stream* stream, Type type, const char* desc = nullptr);

void WriteStr(Stream* stream,
Expand Down
2 changes: 1 addition & 1 deletion src/binary-reader-ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1911,4 +1911,4 @@ Result ExtractOpcodeRawExpr(OpcodeRawExpr &expr, Module &module,
return result;
}

} // namespace wabt
} // namespace wabt
29 changes: 29 additions & 0 deletions src/binary-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ class BinaryWriter {
WABT_DISALLOW_COPY_AND_ASSIGN(BinaryWriter);

public:
friend Result wabt::PackOpcodeRawExpr(OpcodeRawExpr& expr,
Module& module,
const WriteBinaryOptions& options);
BinaryWriter(Stream*,
const WriteBinaryOptions& options,
const Module* module);
Expand Down Expand Up @@ -1865,4 +1868,30 @@ Result WriteBinaryModule(Stream* stream,
return binary_writer.WriteModule();
}

Result PackOpcodeRawExpr(OpcodeRawExpr& expr,
Module& module,
const WriteBinaryOptions& options) {
if (!expr.is_extracted) {
return Result::Ok;
}

MemoryStream stream;
BinaryWriter binary_writer(&stream, options, &module);

binary_writer.WriteExprList(nullptr, expr.extracted_exprs);
WriteOpcode(&stream, Opcode::End);
auto& ob = stream.output_buffer();

if (ob.data.size()) {
expr.opcode_buffer = std::move(ob.data);
stream.ReleaseOutputBuffer();
expr.is_extracted = false;
expr.extracted_exprs.clear();
return Result::Ok;
} else {
stream.ReleaseOutputBuffer();
return Result::Error;
}
}

} // namespace wabt

0 comments on commit 5fcaded

Please sign in to comment.