Skip to content

Commit e6e94ae

Browse files
committed
Implement the basics of component model
1 parent 595a459 commit e6e94ae

23 files changed

Lines changed: 7268 additions & 1688 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ set(WABT_LIBRARY_CC
300300
src/binding-hash.cc
301301
src/color.cc
302302
src/common.cc
303+
src/component.cc
303304
src/config.cc
304305
src/config.h.in
305306
src/decompiler.cc

include/wabt/binary-reader-ir.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace wabt {
2424

25+
class Component;
2526
struct Module;
2627
struct ReadBinaryOptions;
2728

@@ -32,6 +33,13 @@ Result ReadBinaryIr(const char* filename,
3233
Errors*,
3334
Module* out_module);
3435

36+
Result ReadBinaryComponentIr(const char* filename,
37+
const void* data,
38+
size_t size,
39+
const ReadBinaryOptions& options,
40+
Errors*,
41+
Component* out_component);
42+
3543
} // namespace wabt
3644

3745
#endif /* WABT_BINARY_READER_IR_H_ */

include/wabt/binary-reader.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "wabt/binary.h"
2525
#include "wabt/common.h"
26+
#include "wabt/component.h"
2627
#include "wabt/error.h"
2728
#include "wabt/feature.h"
2829
#include "wabt/opcode.h"
@@ -558,11 +559,112 @@ class BinaryReaderDelegate {
558559
const State* state = nullptr;
559560
};
560561

562+
class ComponentBinaryReaderDelegate {
563+
public:
564+
virtual ~ComponentBinaryReaderDelegate() {}
565+
566+
virtual bool OnError(const Error&) = 0;
567+
virtual void OnSetState(const BinaryReaderDelegate::State* s) { state = s; }
568+
569+
virtual Result OnCoreModule(const void* data,
570+
size_t size,
571+
const ReadBinaryOptions& options) = 0;
572+
virtual Result BeginComponent(uint32_t version, size_t depth) = 0;
573+
virtual Result EndComponent() = 0;
574+
575+
virtual Result BeginCoreInstanceSection(uint32_t count) = 0;
576+
virtual Result EndCoreInstanceSection() = 0;
577+
virtual Result OnCoreInstance(uint32_t module_index,
578+
uint32_t argument_count,
579+
ComponentNamedSort* arguments) = 0;
580+
virtual Result OnInlineCoreInstance(uint32_t argument_count,
581+
ComponentNamedSort* arguments) = 0;
582+
583+
virtual Result BeginInstanceSection(uint32_t count) = 0;
584+
virtual Result EndInstanceSection() = 0;
585+
virtual Result OnInstance(uint32_t module_index,
586+
uint32_t argument_count,
587+
ComponentNamedSort* arguments) = 0;
588+
virtual Result OnInlineInstance(uint32_t argument_count,
589+
ComponentNamedExportInfo* arguments) = 0;
590+
591+
virtual Result BeginAliasSection(uint32_t count) = 0;
592+
virtual Result EndAliasSection() = 0;
593+
virtual Result OnAliasExport(ComponentSort sort,
594+
uint32_t instance_index,
595+
std::string_view name) = 0;
596+
virtual Result OnAliasCoreExport(ComponentSort sort,
597+
uint32_t core_instance_index,
598+
std::string_view name) = 0;
599+
virtual Result OnAliasOuter(ComponentSort sort,
600+
uint32_t counter,
601+
uint32_t index) = 0;
602+
603+
virtual Result BeginTypeSection(uint32_t count) = 0;
604+
virtual Result EndTypeSection() = 0;
605+
virtual Result OnPrimitiveType(ComponentType type) = 0;
606+
virtual Result OnRecordType(uint32_t field_count,
607+
ComponentNamedType* fields) = 0;
608+
virtual Result OnVariantType(uint32_t case_count,
609+
ComponentNamedType* cases) = 0;
610+
virtual Result OnListType(ComponentType type) = 0;
611+
virtual Result OnListFixedType(ComponentType type, uint32_t size) = 0;
612+
virtual Result OnTupleType(uint32_t type_count, ComponentType* types) = 0;
613+
virtual Result OnFlagsType(uint32_t flag_count, std::string_view* flags) = 0;
614+
virtual Result OnEnumType(uint32_t enum_count, std::string_view* enums) = 0;
615+
virtual Result OnOptionType(ComponentType type) = 0;
616+
virtual Result OnResultType(ComponentType result, ComponentType error) = 0;
617+
virtual Result OnOwnType(Index index) = 0;
618+
virtual Result OnBorrowType(Index index) = 0;
619+
virtual Result OnStreamType(ComponentType type) = 0;
620+
virtual Result OnFutureType(ComponentType type) = 0;
621+
virtual Result OnFuncType(ComponentTypeDef type,
622+
uint32_t param_count,
623+
ComponentNamedType* params,
624+
ComponentType result) = 0;
625+
virtual Result BeginInstanceType(uint32_t count) = 0;
626+
virtual Result EndInstanceType() = 0;
627+
virtual Result BeginComponentType(uint32_t count) = 0;
628+
virtual Result EndComponentType() = 0;
629+
630+
virtual Result BeginCanonSection(uint32_t count) = 0;
631+
virtual Result EndCanonSection() = 0;
632+
virtual Result OnCanonLift(Index core_func_index,
633+
uint32_t option_count,
634+
ComponentCanonOption* options,
635+
Index type_index) = 0;
636+
virtual Result OnCanonLower(Index func_index,
637+
uint32_t option_count,
638+
ComponentCanonOption* options) = 0;
639+
virtual Result OnCanonType(ComponentBinaryCanon canon, Index type_index) = 0;
640+
641+
virtual Result BeginImportSection(uint32_t count) = 0;
642+
virtual Result EndImportSection() = 0;
643+
virtual Result OnImport(std::string_view external_name,
644+
std::string_view* version_suffix,
645+
ComponentExternalInfo* external_info) = 0;
646+
647+
virtual Result BeginExportSection(uint32_t count) = 0;
648+
virtual Result EndExportSection() = 0;
649+
virtual Result OnExport(std::string_view external_name,
650+
std::string_view* version_suffix,
651+
ComponentExternalInfo* external_info,
652+
ComponentExportInfo* export_info) = 0;
653+
const BinaryReaderDelegate::State* state = nullptr;
654+
};
655+
561656
Result ReadBinary(const void* data,
562657
size_t size,
563658
BinaryReaderDelegate* reader,
564659
const ReadBinaryOptions& options);
565660

661+
Result ReadBinaryComponent(const void* data,
662+
size_t size,
663+
ComponentBinaryReaderDelegate* component_delegate,
664+
const ReadBinaryOptions& options);
665+
666+
bool ReadBinaryIsComponent(const void* data, size_t size);
667+
566668
size_t ReadU32Leb128(const uint8_t* ptr,
567669
const uint8_t* end,
568670
uint32_t* out_value);

include/wabt/binary-writer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace wabt {
2626

27+
class Component;
2728
struct Module;
2829
struct Script;
2930

@@ -45,6 +46,7 @@ struct WriteBinaryOptions {
4546
};
4647

4748
Result WriteBinaryModule(Stream*, const Module*, const WriteBinaryOptions&);
49+
Result WriteBinaryComponent(Stream*, const Component*, const WriteBinaryOptions&);
4850

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

include/wabt/binary.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#define WABT_BINARY_MAGIC 0x6d736100
2323
#define WABT_BINARY_VERSION 1
24+
#define WABT_BINARY_COMPONENT_VERSION 0xd
2425
#define WABT_BINARY_LAYER_MODULE 0
2526
#define WABT_BINARY_LAYER_COMPONENT 1
2627
#define WABT_BINARY_LIMITS_HAS_MAX_FLAG 0x1
@@ -104,6 +105,59 @@ enum class NameSectionSubsection {
104105
};
105106
const char* GetNameSectionSubsectionName(NameSectionSubsection subsec);
106107

108+
enum class ComponentBinarySort : uint8_t {
109+
Core = 0x00,
110+
Func = 0x01,
111+
Value = 0x02,
112+
Type = 0x03,
113+
Component = 0x04,
114+
Instance = 0x05,
115+
};
116+
117+
enum class ComponentBinaryCoreSort : uint8_t {
118+
Func = 0x00,
119+
Table = 0x01,
120+
Memory = 0x02,
121+
Global = 0x03,
122+
Type = 0x10,
123+
Module = 0x11,
124+
Instance = 0x12,
125+
NonCore = 0xff,
126+
};
127+
128+
enum class ComponentBinaryInstance : uint8_t {
129+
Reference = 0x00,
130+
Inline = 0x01,
131+
};
132+
133+
enum class ComponentBinaryAlias : uint8_t {
134+
Export = 0x00,
135+
CoreExport = 0x01,
136+
Outer = 0x02,
137+
};
138+
139+
enum class ComponentBinaryType : uint8_t {
140+
None = 0,
141+
Some = 1,
142+
143+
ResultSome = 0,
144+
ResultNone = 1,
145+
};
146+
147+
enum class ComponentBinaryInterface : uint8_t {
148+
CoreType = 0,
149+
Type = 1,
150+
Alias = 2,
151+
Import = 3,
152+
Export = 4,
153+
};
154+
155+
enum class ComponentBinaryCanon : uint8_t {
156+
Lift = 0x00,
157+
Lower = 0x01,
158+
ResourceDrop = 0x03,
159+
};
160+
107161
} // namespace wabt
108162

109163
#endif /* WABT_BINARY_H_ */

include/wabt/component.h

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright 2016 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef WABT_COMPONENT_H_
18+
#define WABT_COMPONENT_H_
19+
20+
#include "wabt/common.h"
21+
22+
namespace wabt {
23+
24+
// Contains data structures used by the component model.
25+
26+
enum class ComponentSection : uint8_t {
27+
Custom = 0,
28+
CoreModule = 1,
29+
CoreInstance = 2,
30+
CoreType = 3,
31+
Component = 4,
32+
Instance = 5,
33+
Alias = 6,
34+
Type = 7,
35+
Canon = 8,
36+
Import = 10,
37+
Export = 11,
38+
};
39+
40+
class ComponentSort {
41+
public:
42+
enum Enum : uint8_t {
43+
CoreFunc,
44+
CoreTable,
45+
CoreMemory,
46+
CoreGlobal,
47+
CoreType,
48+
CoreModule,
49+
CoreInstance,
50+
Func,
51+
Value,
52+
Type,
53+
Component,
54+
Instance,
55+
Invalid,
56+
};
57+
58+
ComponentSort(Enum sort)
59+
: sort_(sort) {}
60+
61+
ComponentSort()
62+
: sort_(Invalid) {}
63+
64+
constexpr operator Enum() const { return sort_; }
65+
66+
const char* GetName() const;
67+
const char* GetCoreName() const;
68+
69+
private:
70+
Enum sort_;
71+
};
72+
73+
// Composite type definitions.
74+
enum class ComponentTypeDef : uint8_t {
75+
ValueType = 0,
76+
77+
// Types
78+
Record = 0x72,
79+
Variant = 0x71,
80+
List = 0x70,
81+
Tuple = 0x6f,
82+
Flags = 0x6e,
83+
Enum = 0x6d,
84+
Option = 0x6b,
85+
Result = 0x6a,
86+
Own = 0x69,
87+
Borrow = 0x68,
88+
Stream = 0x66,
89+
Future = 0x65,
90+
ListFixed = 0x67,
91+
AsyncFunc = 0x43,
92+
Instance = 0x42,
93+
Component = 0x41,
94+
Func = 0x40,
95+
};
96+
97+
enum class ComponentExternalDesc : uint8_t {
98+
ValueEq = 0x00,
99+
ValueType = 0x01,
100+
TypeEq = 0x00,
101+
TypeSubRes = 0x01,
102+
Unused = 0xff,
103+
};
104+
105+
struct ComponentNamedType {
106+
std::string_view name;
107+
ComponentType type;
108+
};
109+
110+
struct ComponentNamedSort {
111+
std::string_view name;
112+
ComponentSort sort;
113+
Index index;
114+
};
115+
116+
struct ComponentCanonOption {
117+
enum Option : uint8_t {
118+
StrEncUtf8 = 0x00,
119+
StrEncUtf16 = 0x01,
120+
StrEncLatin1Utf16 = 0x02,
121+
Memory = 0x03,
122+
Realloc = 0x04,
123+
PostReturn = 0x05,
124+
Async = 0x06,
125+
Callback = 0x07,
126+
};
127+
128+
Option option;
129+
Index index;
130+
};
131+
132+
struct ComponentExternalInfo {
133+
ComponentSort sort;
134+
ComponentExternalDesc external;
135+
Index index;
136+
};
137+
138+
struct ComponentExportInfo {
139+
ComponentSort sort;
140+
Index index;
141+
};
142+
143+
struct ComponentNamedExportInfo {
144+
std::string_view name;
145+
std::string_view version_suffix;
146+
ComponentSort sort;
147+
bool has_version_suffix;
148+
Index index;
149+
};
150+
151+
} // namespace wabt
152+
153+
#endif // WABT_DECOMPILER_AST_H_

0 commit comments

Comments
 (0)