MCPcopy Create free account
hub / github.com/WebAssembly/wabt / WriteModule

Method WriteModule

src/binary-writer.cc:1435–1892  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1433}
1434
1435Result BinaryWriter::WriteModule() {
1436 stream_->WriteU32(WABT_BINARY_MAGIC, "WASM_BINARY_MAGIC");
1437 stream_->WriteU32(WABT_BINARY_VERSION, "WASM_BINARY_VERSION");
1438
1439 if (options_.relocatable) {
1440 CHECK_RESULT(symtab_.Populate(module_));
1441 }
1442
1443 if (module_->types.size()) {
1444 BeginKnownSection(BinarySection::Type);
1445 WriteU32Leb128(stream_, module_->types.size(), "num types");
1446 for (size_t i = 0; i < module_->types.size(); ++i) {
1447 const TypeEntry* type = module_->types[i];
1448 switch (type->kind()) {
1449 case TypeEntryKind::Func: {
1450 const FuncType* func_type = cast<FuncType>(type);
1451 const FuncSignature* sig = &func_type->sig;
1452 WriteHeader("func type", i);
1453 WriteType(stream_, Type::Func);
1454
1455 Index num_params = sig->param_types.size();
1456 Index num_results = sig->result_types.size();
1457 WriteU32Leb128(stream_, num_params, "num params");
1458 for (size_t j = 0; j < num_params; ++j) {
1459 WriteType(stream_, sig->param_types[j]);
1460 }
1461
1462 WriteU32Leb128(stream_, num_results, "num results");
1463 for (size_t j = 0; j < num_results; ++j) {
1464 WriteType(stream_, sig->result_types[j]);
1465 }
1466 break;
1467 }
1468
1469 case TypeEntryKind::Struct: {
1470 const StructType* struct_type = cast<StructType>(type);
1471 WriteHeader("struct type", i);
1472 WriteType(stream_, Type::Struct);
1473 Index num_fields = struct_type->fields.size();
1474 WriteU32Leb128(stream_, num_fields, "num fields");
1475 for (size_t j = 0; j < num_fields; ++j) {
1476 const Field& field = struct_type->fields[j];
1477 WriteType(stream_, field.type);
1478 stream_->WriteU8(field.mutable_, "field mutability");
1479 }
1480 break;
1481 }
1482
1483 case TypeEntryKind::Array: {
1484 const ArrayType* array_type = cast<ArrayType>(type);
1485 WriteHeader("array type", i);
1486 WriteType(stream_, Type::Array);
1487 WriteType(stream_, array_type->field.type);
1488 stream_->WriteU8(array_type->field.mutable_, "field mutability");
1489 break;
1490 }
1491 }
1492 }

Callers 1

WriteBinaryModuleFunction · 0.45

Calls 15

WriteTypeFunction · 0.85
WriteStrFunction · 0.85
wabt_snprintfFunction · 0.85
WriteDebugNameFunction · 0.85
WriteU32Method · 0.80
PopulateMethod · 0.80
WriteU8Method · 0.80
WriteU8EnumMethod · 0.80
GetFuncIndexMethod · 0.80
GetTableIndexMethod · 0.80
GetMemoryIndexMethod · 0.80

Tested by

no test coverage detected