Skip to content

Commit fe99e34

Browse files
authored
Add event section (#2151)
This adds support for the event and the event section, as specified in https://github.com/WebAssembly/exception-handling/blob/master/proposals/Exceptions.md#changes-to-the-binary-model. Wasm events are features that suspend the current execution and transfer the control flow to a corresponding handler. Currently the only supported event kind is exceptions. For events, this includes support for - Binary file reading/writing - Wast file reading/writing - Binaryen.js API - Fuzzer - Validation - Metadce - Passes: metrics, minify-imports-and-exports, remove-unused-module-elements
1 parent 7306f60 commit fe99e34

60 files changed

Lines changed: 2011 additions & 720 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build-js.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ export_function "_BinaryenExternalFunction"
230230
export_function "_BinaryenExternalTable"
231231
export_function "_BinaryenExternalMemory"
232232
export_function "_BinaryenExternalGlobal"
233+
export_function "_BinaryenExternalEvent"
233234

234235
# Features
235236
export_function "_BinaryenFeatureMVP"
@@ -759,14 +760,19 @@ export_function "_BinaryenRemoveFunction"
759760
export_function "_BinaryenAddGlobal"
760761
export_function "_BinaryenGetGlobal"
761762
export_function "_BinaryenRemoveGlobal"
763+
export_function "_BinaryenAddEvent"
764+
export_function "_BinaryenGetEvent"
765+
export_function "_BinaryenRemoveEvent"
762766
export_function "_BinaryenAddFunctionImport"
763767
export_function "_BinaryenAddTableImport"
764768
export_function "_BinaryenAddMemoryImport"
765769
export_function "_BinaryenAddGlobalImport"
770+
export_function "_BinaryenAddEventImport"
766771
export_function "_BinaryenAddFunctionExport"
767772
export_function "_BinaryenAddTableExport"
768773
export_function "_BinaryenAddMemoryExport"
769774
export_function "_BinaryenAddGlobalExport"
775+
export_function "_BinaryenAddEventExport"
770776
export_function "_BinaryenRemoveExport"
771777
export_function "_BinaryenSetFunctionTable"
772778
export_function "_BinaryenSetMemory"
@@ -817,11 +823,20 @@ export_function "_BinaryenGlobalGetType"
817823
export_function "_BinaryenGlobalIsMutable"
818824
export_function "_BinaryenGlobalGetInitExpr"
819825

826+
# 'Event' operations
827+
export_function "_BinaryenEventGetName"
828+
export_function "_BinaryenEventGetType"
829+
export_function "_BinaryenEventGetNumParams"
830+
export_function "_BinaryenEventGetParam"
831+
export_function "_BinaryenEventGetAttribute"
832+
820833
# 'Import' operations
821834
export_function "_BinaryenGlobalImportGetModule"
822835
export_function "_BinaryenGlobalImportGetBase"
823836
export_function "_BinaryenFunctionImportGetModule"
824837
export_function "_BinaryenFunctionImportGetBase"
838+
export_function "_BinaryenEventImportGetModule"
839+
export_function "_BinaryenEventImportGetBase"
825840

826841
# 'Export' operations
827842
export_function "_BinaryenExportGetKind"

src/binaryen-c.cpp

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ std::map<BinaryenFunctionTypeRef, size_t> functionTypes;
125125
std::map<BinaryenExpressionRef, size_t> expressions;
126126
std::map<BinaryenFunctionRef, size_t> functions;
127127
std::map<BinaryenGlobalRef, size_t> globals;
128+
std::map<BinaryenEventRef, size_t> events;
128129
std::map<BinaryenExportRef, size_t> exports;
129130
std::map<RelooperBlockRef, size_t> relooperBlocks;
130131

@@ -368,6 +369,9 @@ BinaryenExternalKind BinaryenExternalMemory(void) {
368369
BinaryenExternalKind BinaryenExternalGlobal(void) {
369370
return static_cast<BinaryenExternalKind>(ExternalKind::Global);
370371
}
372+
BinaryenExternalKind BinaryenExternalEvent(void) {
373+
return static_cast<BinaryenExternalKind>(ExternalKind::Event);
374+
}
371375

372376
// Features
373377

@@ -417,12 +421,14 @@ void BinaryenModuleDispose(BinaryenModuleRef module) {
417421
std::cout << " expressions.clear();\n";
418422
std::cout << " functions.clear();\n";
419423
std::cout << " globals.clear();\n";
424+
std::cout << " events.clear();\n";
420425
std::cout << " exports.clear();\n";
421426
std::cout << " relooperBlocks.clear();\n";
422427
functionTypes.clear();
423428
expressions.clear();
424429
functions.clear();
425430
globals.clear();
431+
events.clear();
426432
exports.clear();
427433
relooperBlocks.clear();
428434
}
@@ -2775,6 +2781,45 @@ void BinaryenRemoveGlobal(BinaryenModuleRef module, const char* name) {
27752781
wasm->removeGlobal(name);
27762782
}
27772783

2784+
// Events
2785+
2786+
BinaryenEventRef BinaryenAddEvent(BinaryenModuleRef module,
2787+
const char* name,
2788+
uint32_t attribute,
2789+
BinaryenFunctionTypeRef type) {
2790+
if (tracing) {
2791+
std::cout << " BinaryenAddEvent(the_module, \"" << name << "\", "
2792+
<< attribute << ", functionTypes[" << functionTypes[type]
2793+
<< "]);\n";
2794+
}
2795+
2796+
auto* wasm = (Module*)module;
2797+
auto* ret = new Event();
2798+
ret->name = name;
2799+
ret->attribute = attribute;
2800+
ret->type = ((FunctionType*)type)->name;
2801+
ret->params = ((FunctionType*)type)->params;
2802+
wasm->addEvent(ret);
2803+
return ret;
2804+
}
2805+
2806+
BinaryenEventRef BinaryenGetEvent(BinaryenModuleRef module, const char* name) {
2807+
if (tracing) {
2808+
std::cout << " BinaryenGetEvent(the_module, \"" << name << "\");\n";
2809+
}
2810+
2811+
auto* wasm = (Module*)module;
2812+
return wasm->getEvent(name);
2813+
}
2814+
void BinaryenRemoveEvent(BinaryenModuleRef module, const char* name) {
2815+
if (tracing) {
2816+
std::cout << " BinaryenRemoveEvent(the_module, \"" << name << "\");\n";
2817+
}
2818+
2819+
auto* wasm = (Module*)module;
2820+
wasm->removeEvent(name);
2821+
}
2822+
27782823
// Imports
27792824

27802825
void BinaryenAddFunctionImport(BinaryenModuleRef module,
@@ -2850,6 +2895,29 @@ void BinaryenAddGlobalImport(BinaryenModuleRef module,
28502895
ret->type = Type(globalType);
28512896
wasm->addGlobal(ret);
28522897
}
2898+
void BinaryenAddEventImport(BinaryenModuleRef module,
2899+
const char* internalName,
2900+
const char* externalModuleName,
2901+
const char* externalBaseName,
2902+
uint32_t attribute,
2903+
BinaryenFunctionTypeRef eventType) {
2904+
auto* wasm = (Module*)module;
2905+
auto* ret = new Event();
2906+
2907+
if (tracing) {
2908+
std::cout << " BinaryenAddEventImport(the_module, \"" << internalName
2909+
<< "\", \"" << externalModuleName << "\", \"" << externalBaseName
2910+
<< "\", " << attribute << ", functionTypes["
2911+
<< functionTypes[eventType] << "]);\n";
2912+
}
2913+
2914+
ret->name = internalName;
2915+
ret->module = externalModuleName;
2916+
ret->base = externalBaseName;
2917+
ret->type = ((FunctionType*)eventType)->name;
2918+
ret->params = ((FunctionType*)eventType)->params;
2919+
wasm->addEvent(ret);
2920+
}
28532921

28542922
// Exports
28552923

@@ -2938,6 +3006,26 @@ BinaryenExportRef BinaryenAddGlobalExport(BinaryenModuleRef module,
29383006
wasm->addExport(ret);
29393007
return ret;
29403008
}
3009+
BinaryenExportRef BinaryenAddEventExport(BinaryenModuleRef module,
3010+
const char* internalName,
3011+
const char* externalName) {
3012+
auto* wasm = (Module*)module;
3013+
auto* ret = new Export();
3014+
3015+
if (tracing) {
3016+
auto id = exports.size();
3017+
exports[ret] = id;
3018+
std::cout << " exports[" << id
3019+
<< "] = BinaryenAddEventExport(the_module, \"" << internalName
3020+
<< "\", \"" << externalName << "\");\n";
3021+
}
3022+
3023+
ret->value = internalName;
3024+
ret->name = externalName;
3025+
ret->kind = ExternalKind::Event;
3026+
wasm->addExport(ret);
3027+
return ret;
3028+
}
29413029
void BinaryenRemoveExport(BinaryenModuleRef module, const char* externalName) {
29423030
if (tracing) {
29433031
std::cout << " BinaryenRemoveExport(the_module, \"" << externalName
@@ -3654,6 +3742,52 @@ BinaryenExpressionRef BinaryenGlobalGetInitExpr(BinaryenGlobalRef global) {
36543742
return ((Global*)global)->init;
36553743
}
36563744

3745+
//
3746+
// =========== Event operations ===========
3747+
//
3748+
3749+
const char* BinaryenEventGetName(BinaryenEventRef event) {
3750+
if (tracing) {
3751+
std::cout << " BinaryenEventGetName(events[" << events[event] << "]);\n";
3752+
}
3753+
3754+
return ((Event*)event)->name.c_str();
3755+
}
3756+
int BinaryenEventGetAttribute(BinaryenEventRef event) {
3757+
if (tracing) {
3758+
std::cout << " BinaryenEventGetAttribute(events[" << events[event]
3759+
<< "]);\n";
3760+
}
3761+
3762+
return ((Event*)event)->attribute;
3763+
}
3764+
const char* BinaryenEventGetType(BinaryenEventRef event) {
3765+
if (tracing) {
3766+
std::cout << " BinaryenEventGetType(events[" << events[event] << "]);\n";
3767+
}
3768+
3769+
return ((Event*)event)->type.c_str();
3770+
}
3771+
BinaryenIndex BinaryenEventGetNumParams(BinaryenEventRef event) {
3772+
if (tracing) {
3773+
std::cout << " BinaryenEventGetNumParams(events[" << events[event]
3774+
<< "]);\n";
3775+
}
3776+
3777+
return ((Event*)event)->params.size();
3778+
}
3779+
BinaryenType BinaryenEventGetParam(BinaryenEventRef event,
3780+
BinaryenIndex index) {
3781+
if (tracing) {
3782+
std::cout << " BinaryenEventGetParam(events[" << events[event] << "], "
3783+
<< index << ");\n";
3784+
}
3785+
3786+
auto* fn = (Event*)event;
3787+
assert(index < fn->params.size());
3788+
return fn->params[index];
3789+
}
3790+
36573791
//
36583792
// =========== Import operations ===========
36593793
//
@@ -3684,6 +3818,19 @@ const char* BinaryenGlobalImportGetModule(BinaryenGlobalRef import) {
36843818
return "";
36853819
}
36863820
}
3821+
const char* BinaryenEventImportGetModule(BinaryenEventRef import) {
3822+
if (tracing) {
3823+
std::cout << " BinaryenEventImportGetModule(events[" << events[import]
3824+
<< "]);\n";
3825+
}
3826+
3827+
auto* event = (Event*)import;
3828+
if (event->imported()) {
3829+
return event->module.c_str();
3830+
} else {
3831+
return "";
3832+
}
3833+
}
36873834
const char* BinaryenFunctionImportGetBase(BinaryenFunctionRef import) {
36883835
if (tracing) {
36893836
std::cout << " BinaryenFunctionImportGetBase(functions["
@@ -3710,6 +3857,19 @@ const char* BinaryenGlobalImportGetBase(BinaryenGlobalRef import) {
37103857
return "";
37113858
}
37123859
}
3860+
const char* BinaryenEventImportGetBase(BinaryenEventRef import) {
3861+
if (tracing) {
3862+
std::cout << " BinaryenEventImportGetBase(events[" << events[import]
3863+
<< "]);\n";
3864+
}
3865+
3866+
auto* event = (Event*)import;
3867+
if (event->imported()) {
3868+
return event->base.c_str();
3869+
} else {
3870+
return "";
3871+
}
3872+
}
37133873

37143874
//
37153875
// =========== Export operations ===========
@@ -3875,6 +4035,7 @@ void BinaryenSetAPITracing(int on) {
38754035
" std::map<size_t, BinaryenExpressionRef> expressions;\n"
38764036
" std::map<size_t, BinaryenFunctionRef> functions;\n"
38774037
" std::map<size_t, BinaryenGlobalRef> globals;\n"
4038+
" std::map<size_t, BinaryenEventRef> events;\n"
38784039
" std::map<size_t, BinaryenExportRef> exports;\n"
38794040
" std::map<size_t, RelooperBlockRef> relooperBlocks;\n"
38804041
" BinaryenModuleRef the_module = NULL;\n"

src/binaryen-c.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ BinaryenExternalKind BinaryenExternalFunction(void);
136136
BinaryenExternalKind BinaryenExternalTable(void);
137137
BinaryenExternalKind BinaryenExternalMemory(void);
138138
BinaryenExternalKind BinaryenExternalGlobal(void);
139+
BinaryenExternalKind BinaryenExternalEvent(void);
139140

140141
// Features. Call to get the value of each; you can cache them. Use bitwise
141142
// operators to combine and test particular features.
@@ -882,6 +883,12 @@ void BinaryenAddGlobalImport(BinaryenModuleRef module,
882883
const char* externalModuleName,
883884
const char* externalBaseName,
884885
BinaryenType globalType);
886+
void BinaryenAddEventImport(BinaryenModuleRef module,
887+
const char* internalName,
888+
const char* externalModuleName,
889+
const char* externalBaseName,
890+
uint32_t attribute,
891+
BinaryenFunctionTypeRef eventType);
885892

886893
// Exports
887894

@@ -902,6 +909,9 @@ BinaryenExportRef BinaryenAddMemoryExport(BinaryenModuleRef module,
902909
BinaryenExportRef BinaryenAddGlobalExport(BinaryenModuleRef module,
903910
const char* internalName,
904911
const char* externalName);
912+
BinaryenExportRef BinaryenAddEventExport(BinaryenModuleRef module,
913+
const char* internalName,
914+
const char* externalName);
905915
void BinaryenRemoveExport(BinaryenModuleRef module, const char* externalName);
906916

907917
// Globals
@@ -917,6 +927,17 @@ BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module,
917927
BinaryenGlobalRef BinaryenGetGlobal(BinaryenModuleRef module, const char* name);
918928
void BinaryenRemoveGlobal(BinaryenModuleRef module, const char* name);
919929

930+
// Events
931+
932+
typedef void* BinaryenEventRef;
933+
934+
BinaryenEventRef BinaryenAddEvent(BinaryenModuleRef module,
935+
const char* name,
936+
uint32_t attribute,
937+
BinaryenFunctionTypeRef type);
938+
BinaryenEventRef BinaryenGetEvent(BinaryenModuleRef module, const char* name);
939+
void BinaryenEventEvent(BinaryenModuleRef module, const char* name);
940+
920941
// Function table. One per module
921942

922943
void BinaryenSetFunctionTable(BinaryenModuleRef module,
@@ -1153,16 +1174,33 @@ int BinaryenGlobalIsMutable(BinaryenGlobalRef global);
11531174
BinaryenExpressionRef BinaryenGlobalGetInitExpr(BinaryenGlobalRef global);
11541175

11551176
//
1177+
// ========== Event Operations ==========
1178+
//
1179+
1180+
// Gets the name of the specified `Event`.
1181+
const char* BinaryenEventGetName(BinaryenEventRef event);
1182+
// Gets the attribute of the specified `Event`.
1183+
int BinaryenEventGetAttribute(BinaryenEventRef event);
1184+
// Gets the name of the `FunctionType` associated with the specified `Event`.
1185+
const char* BinaryenEventGetType(BinaryenEventRef event);
1186+
// Gets the number of parameters of the specified `Event`.
1187+
BinaryenIndex BinaryenEventGetNumParams(BinaryenEventRef event);
1188+
// Gets the type of the parameter at the specified index of the specified
1189+
// `Event`.
1190+
BinaryenType BinaryenEventGetParam(BinaryenEventRef event, BinaryenIndex index);
1191+
11561192
//
11571193
// ========== Import Operations ==========
11581194
//
11591195

11601196
// Gets the external module name of the specified import.
11611197
const char* BinaryenFunctionImportGetModule(BinaryenFunctionRef import);
11621198
const char* BinaryenGlobalImportGetModule(BinaryenGlobalRef import);
1199+
const char* BinaryenEventImportGetModule(BinaryenEventRef import);
11631200
// Gets the external base name of the specified import.
11641201
const char* BinaryenFunctionImportGetBase(BinaryenFunctionRef import);
11651202
const char* BinaryenGlobalImportGetBase(BinaryenGlobalRef import);
1203+
const char* BinaryenEventImportGetBase(BinaryenEventRef import);
11661204

11671205
//
11681206
// ========== Export Operations ==========

src/ir/ReFinalize.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ void ReFinalize::visitExport(Export* curr) { WASM_UNREACHABLE(); }
172172
void ReFinalize::visitGlobal(Global* curr) { WASM_UNREACHABLE(); }
173173
void ReFinalize::visitTable(Table* curr) { WASM_UNREACHABLE(); }
174174
void ReFinalize::visitMemory(Memory* curr) { WASM_UNREACHABLE(); }
175+
void ReFinalize::visitEvent(Event* curr) { WASM_UNREACHABLE(); }
175176
void ReFinalize::visitModule(Module* curr) { WASM_UNREACHABLE(); }
176177

177178
void ReFinalize::updateBreakValueType(Name name, Type type) {

0 commit comments

Comments
 (0)