Skip to content

Commit b0d708b

Browse files
committed
Expose try_table and throw_ref in C and JS API
1 parent 0f38ce7 commit b0d708b

11 files changed

Lines changed: 533 additions & 4 deletions

src/binaryen-c.cpp

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ BinaryenType BinaryenTypeNullExternref(void) {
226226
BinaryenType BinaryenTypeNullFuncref(void) {
227227
return Type(HeapType::nofunc, Nullable).getID();
228228
}
229+
BinaryenType BinaryenTypeExnref(void) {
230+
return Type(HeapType::exn, Nullable).getID();
231+
}
232+
BinaryenType BinaryenTypeNullExnref(void) {
233+
return Type(HeapType::noexn, Nullable).getID();
234+
}
229235
BinaryenType BinaryenTypeUnreachable(void) { return Type::unreachable; }
230236
BinaryenType BinaryenTypeAuto(void) { return uintptr_t(-1); }
231237

@@ -302,6 +308,12 @@ BinaryenHeapType BinaryenHeapTypeNoext() {
302308
BinaryenHeapType BinaryenHeapTypeNofunc() {
303309
return static_cast<BinaryenHeapType>(HeapType::BasicHeapType::nofunc);
304310
}
311+
BinaryenHeapType BinaryenHeapTypeExn() {
312+
return static_cast<BinaryenHeapType>(HeapType::BasicHeapType::exn);
313+
}
314+
BinaryenHeapType BinaryenHeapTypeNoexn() {
315+
return static_cast<BinaryenHeapType>(HeapType::BasicHeapType::noexn);
316+
}
305317

306318
bool BinaryenHeapTypeIsBasic(BinaryenHeapType heapType) {
307319
return HeapType(heapType).isBasic();
@@ -1780,6 +1792,28 @@ BinaryenExpressionRef BinaryenTry(BinaryenModuleRef module,
17801792
return static_cast<Expression*>(ret);
17811793
}
17821794

1795+
BinaryenExpressionRef BinaryenTryTable(BinaryenModuleRef module,
1796+
BinaryenExpressionRef body,
1797+
const char** catchTags,
1798+
const char** catchDests,
1799+
const bool* catchRefs,
1800+
BinaryenIndex numCatches) {
1801+
std::vector<Name> tags;
1802+
std::vector<Name> dests;
1803+
std::vector<bool> refs;
1804+
tags.reserve(numCatches);
1805+
dests.reserve(numCatches);
1806+
refs.reserve(numCatches);
1807+
for (BinaryenIndex i = 0; i < numCatches; i++) {
1808+
tags.push_back(catchTags[i] ? Name(catchTags[i]) : Name());
1809+
dests.push_back(catchDests[i]);
1810+
refs.push_back(catchRefs[i]);
1811+
}
1812+
return static_cast<Expression*>(
1813+
Builder(*(Module*)module)
1814+
.makeTryTable((Expression*)body, tags, dests, refs));
1815+
}
1816+
17831817
BinaryenExpressionRef BinaryenThrow(BinaryenModuleRef module,
17841818
const char* tag,
17851819
BinaryenExpressionRef* operands,
@@ -1798,6 +1832,12 @@ BinaryenExpressionRef BinaryenRethrow(BinaryenModuleRef module,
17981832
Builder(*(Module*)module).makeRethrow(target));
17991833
}
18001834

1835+
BinaryenExpressionRef BinaryenThrowRef(BinaryenModuleRef module,
1836+
BinaryenExpressionRef exnref) {
1837+
return static_cast<Expression*>(
1838+
Builder(*(Module*)module).makeThrowRef((Expression*)exnref));
1839+
}
1840+
18011841
BinaryenExpressionRef BinaryenRefI31(BinaryenModuleRef module,
18021842
BinaryenExpressionRef value) {
18031843
return static_cast<Expression*>(
@@ -4086,6 +4126,112 @@ bool BinaryenTryIsDelegate(BinaryenExpressionRef expr) {
40864126
assert(expression->is<Try>());
40874127
return static_cast<Try*>(expression)->isDelegate();
40884128
}
4129+
// TryTable
4130+
BinaryenExpressionRef BinaryenTryTableGetBody(BinaryenExpressionRef expr) {
4131+
auto* expression = (Expression*)expr;
4132+
assert(expression->is<TryTable>());
4133+
return static_cast<TryTable*>(expression)->body;
4134+
}
4135+
void BinaryenTryTableSetBody(BinaryenExpressionRef expr,
4136+
BinaryenExpressionRef bodyExpr) {
4137+
auto* expression = (Expression*)expr;
4138+
assert(expression->is<TryTable>());
4139+
assert(bodyExpr);
4140+
static_cast<TryTable*>(expression)->body = (Expression*)bodyExpr;
4141+
}
4142+
BinaryenIndex BinaryenTryTableGetNumCatches(BinaryenExpressionRef expr) {
4143+
auto* expression = (Expression*)expr;
4144+
assert(expression->is<TryTable>());
4145+
return static_cast<TryTable*>(expression)->catchTags.size();
4146+
}
4147+
const char* BinaryenTryTableGetCatchTagAt(BinaryenExpressionRef expr,
4148+
BinaryenIndex index) {
4149+
auto* expression = (Expression*)expr;
4150+
assert(expression->is<TryTable>());
4151+
assert(index < static_cast<TryTable*>(expression)->catchTags.size());
4152+
auto name = static_cast<TryTable*>(expression)->catchTags[index];
4153+
return name.is() ? name.str.data() : nullptr;
4154+
}
4155+
void BinaryenTryTableSetCatchTagAt(BinaryenExpressionRef expr,
4156+
BinaryenIndex index,
4157+
const char* catchTag) {
4158+
auto* expression = (Expression*)expr;
4159+
assert(expression->is<TryTable>());
4160+
assert(index < static_cast<TryTable*>(expression)->catchTags.size());
4161+
static_cast<TryTable*>(expression)->catchTags[index] =
4162+
catchTag ? Name(catchTag) : Name();
4163+
}
4164+
const char* BinaryenTryTableGetCatchDestAt(BinaryenExpressionRef expr,
4165+
BinaryenIndex index) {
4166+
auto* expression = (Expression*)expr;
4167+
assert(expression->is<TryTable>());
4168+
assert(index < static_cast<TryTable*>(expression)->catchDests.size());
4169+
return static_cast<TryTable*>(expression)->catchDests[index].str.data();
4170+
}
4171+
void BinaryenTryTableSetCatchDestAt(BinaryenExpressionRef expr,
4172+
BinaryenIndex index,
4173+
const char* catchDest) {
4174+
auto* expression = (Expression*)expr;
4175+
assert(expression->is<TryTable>());
4176+
assert(index < static_cast<TryTable*>(expression)->catchDests.size());
4177+
static_cast<TryTable*>(expression)->catchDests[index] = catchDest;
4178+
}
4179+
bool BinaryenTryTableIsCatchRefAt(BinaryenExpressionRef expr,
4180+
BinaryenIndex index) {
4181+
auto* expression = (Expression*)expr;
4182+
assert(expression->is<TryTable>());
4183+
assert(index < static_cast<TryTable*>(expression)->catchRefs.size());
4184+
return static_cast<TryTable*>(expression)->catchRefs[index];
4185+
}
4186+
void BinaryenTryTableSetCatchRefAt(BinaryenExpressionRef expr,
4187+
BinaryenIndex index,
4188+
bool catchRef) {
4189+
auto* expression = (Expression*)expr;
4190+
assert(expression->is<TryTable>());
4191+
assert(index < static_cast<TryTable*>(expression)->catchRefs.size());
4192+
static_cast<TryTable*>(expression)->catchRefs[index] = catchRef;
4193+
}
4194+
BinaryenIndex BinaryenTryTableAppendCatch(BinaryenExpressionRef expr,
4195+
const char* catchTag,
4196+
const char* catchDest,
4197+
bool catchRef) {
4198+
auto* expression = (Expression*)expr;
4199+
assert(expression->is<TryTable>());
4200+
assert(catchDest);
4201+
auto* tryTable = static_cast<TryTable*>(expression);
4202+
auto index = tryTable->catchTags.size();
4203+
tryTable->catchTags.push_back(catchTag ? Name(catchTag) : Name());
4204+
tryTable->catchDests.push_back(Name(catchDest));
4205+
tryTable->catchRefs.push_back(catchRef);
4206+
return index;
4207+
}
4208+
void BinaryenTryTableInsertCatchAt(BinaryenExpressionRef expr,
4209+
BinaryenIndex index,
4210+
const char* catchTag,
4211+
const char* catchDest,
4212+
bool catchRef) {
4213+
auto* expression = (Expression*)expr;
4214+
assert(expression->is<TryTable>());
4215+
assert(catchDest);
4216+
auto* tryTable = static_cast<TryTable*>(expression);
4217+
tryTable->catchTags.insertAt(index, catchTag ? Name(catchTag) : Name());
4218+
tryTable->catchDests.insertAt(index, Name(catchDest));
4219+
tryTable->catchRefs.insertAt(index, catchRef);
4220+
}
4221+
const char* BinaryenTryTableRemoveCatchAt(BinaryenExpressionRef expr,
4222+
BinaryenIndex index) {
4223+
auto* expression = (Expression*)expr;
4224+
assert(expression->is<TryTable>());
4225+
auto* tryTable = static_cast<TryTable*>(expression);
4226+
tryTable->catchTags.removeAt(index);
4227+
tryTable->catchRefs.removeAt(index);
4228+
return tryTable->catchDests.removeAt(index).str.data();
4229+
}
4230+
bool BinaryenTryTableHasCatchAll(BinaryenExpressionRef expr) {
4231+
auto* expression = (Expression*)expr;
4232+
assert(expression->is<TryTable>());
4233+
return static_cast<TryTable*>(expression)->hasCatchAll();
4234+
}
40894235
// Throw
40904236
const char* BinaryenThrowGetTag(BinaryenExpressionRef expr) {
40914237
auto* expression = (Expression*)expr;
@@ -4154,6 +4300,19 @@ void BinaryenRethrowSetTarget(BinaryenExpressionRef expr, const char* target) {
41544300
assert(expression->is<Rethrow>());
41554301
static_cast<Rethrow*>(expression)->target = target;
41564302
}
4303+
// ThrowRef
4304+
BinaryenExpressionRef BinaryenThrowRefGetExnref(BinaryenExpressionRef expr) {
4305+
auto* expression = (Expression*)expr;
4306+
assert(expression->is<ThrowRef>());
4307+
return static_cast<ThrowRef*>(expression)->exnref;
4308+
}
4309+
void BinaryenThrowRefSetExnref(BinaryenExpressionRef expr,
4310+
BinaryenExpressionRef exnrefExpr) {
4311+
auto* expression = (Expression*)expr;
4312+
assert(expression->is<ThrowRef>());
4313+
assert(exnrefExpr);
4314+
static_cast<ThrowRef*>(expression)->exnref = (Expression*)exnrefExpr;
4315+
}
41574316
// TupleMake
41584317
BinaryenIndex BinaryenTupleMakeGetNumOperands(BinaryenExpressionRef expr) {
41594318
auto* expression = (Expression*)expr;

src/binaryen-c.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ BINARYEN_API BinaryenType BinaryenTypeStringref(void);
112112
BINARYEN_API BinaryenType BinaryenTypeNullref(void);
113113
BINARYEN_API BinaryenType BinaryenTypeNullExternref(void);
114114
BINARYEN_API BinaryenType BinaryenTypeNullFuncref(void);
115+
BINARYEN_API BinaryenType BinaryenTypeExnref(void);
116+
BINARYEN_API BinaryenType BinaryenTypeNullExnref(void);
115117
BINARYEN_API BinaryenType BinaryenTypeUnreachable(void);
116118
// Not a real type. Used as the last parameter to BinaryenBlock to let
117119
// the API figure out the type instead of providing one.
@@ -151,6 +153,8 @@ BINARYEN_API BinaryenHeapType BinaryenHeapTypeString(void);
151153
BINARYEN_API BinaryenHeapType BinaryenHeapTypeNone(void);
152154
BINARYEN_API BinaryenHeapType BinaryenHeapTypeNoext(void);
153155
BINARYEN_API BinaryenHeapType BinaryenHeapTypeNofunc(void);
156+
BINARYEN_API BinaryenHeapType BinaryenHeapTypeExn(void);
157+
BINARYEN_API BinaryenHeapType BinaryenHeapTypeNoexn(void);
154158

155159
BINARYEN_API bool BinaryenHeapTypeIsBasic(BinaryenHeapType heapType);
156160
BINARYEN_API bool BinaryenHeapTypeIsSignature(BinaryenHeapType heapType);
@@ -1037,6 +1041,14 @@ BinaryenTry(BinaryenModuleRef module,
10371041
BinaryenExpressionRef* catchBodies,
10381042
BinaryenIndex numCatchBodies,
10391043
const char* delegateTarget);
1044+
// TryTable: catch tag names may be NULL to denote catch_all or catch_all_ref.
1045+
// catchRefs[i] is true if the i-th catch is catch_ref or catch_all_ref.
1046+
BINARYEN_API BinaryenExpressionRef BinaryenTryTable(BinaryenModuleRef module,
1047+
BinaryenExpressionRef body,
1048+
const char** catchTags,
1049+
const char** catchDests,
1050+
const bool* catchRefs,
1051+
BinaryenIndex numCatches);
10401052
BINARYEN_API BinaryenExpressionRef
10411053
BinaryenThrow(BinaryenModuleRef module,
10421054
const char* tag,
@@ -1045,6 +1057,8 @@ BinaryenThrow(BinaryenModuleRef module,
10451057
BINARYEN_API BinaryenExpressionRef BinaryenRethrow(BinaryenModuleRef module,
10461058
const char* target);
10471059
BINARYEN_API BinaryenExpressionRef
1060+
BinaryenThrowRef(BinaryenModuleRef module, BinaryenExpressionRef exnref);
1061+
BINARYEN_API BinaryenExpressionRef
10481062
BinaryenTupleMake(BinaryenModuleRef module,
10491063
BinaryenExpressionRef* operands,
10501064
BinaryenIndex numOperands);
@@ -2362,6 +2376,67 @@ BINARYEN_API void BinaryenTrySetDelegateTarget(BinaryenExpressionRef expr,
23622376
// Gets whether a `try` expression is a try-delegate.
23632377
BINARYEN_API bool BinaryenTryIsDelegate(BinaryenExpressionRef expr);
23642378

2379+
// TryTable
2380+
2381+
// Gets the body expression of a `try_table` expression.
2382+
BINARYEN_API BinaryenExpressionRef
2383+
BinaryenTryTableGetBody(BinaryenExpressionRef expr);
2384+
// Sets the body expression of a `try_table` expression.
2385+
BINARYEN_API void BinaryenTryTableSetBody(BinaryenExpressionRef expr,
2386+
BinaryenExpressionRef bodyExpr);
2387+
// Gets the number of catch clauses of a `try_table` expression.
2388+
BINARYEN_API BinaryenIndex
2389+
BinaryenTryTableGetNumCatches(BinaryenExpressionRef expr);
2390+
// Gets the catch tag at the specified index of a `try_table` expression. Empty
2391+
// (NULL) for catch_all and catch_all_ref clauses.
2392+
BINARYEN_API const char*
2393+
BinaryenTryTableGetCatchTagAt(BinaryenExpressionRef expr, BinaryenIndex index);
2394+
// Sets the catch tag at the specified index of a `try_table` expression. Pass
2395+
// NULL for catch_all/catch_all_ref clauses.
2396+
BINARYEN_API void BinaryenTryTableSetCatchTagAt(BinaryenExpressionRef expr,
2397+
BinaryenIndex index,
2398+
const char* catchTag);
2399+
// Gets the catch destination label at the specified index of a `try_table`
2400+
// expression.
2401+
BINARYEN_API const char*
2402+
BinaryenTryTableGetCatchDestAt(BinaryenExpressionRef expr, BinaryenIndex index);
2403+
// Sets the catch destination label at the specified index of a `try_table`
2404+
// expression.
2405+
BINARYEN_API void BinaryenTryTableSetCatchDestAt(BinaryenExpressionRef expr,
2406+
BinaryenIndex index,
2407+
const char* catchDest);
2408+
// Gets whether the catch clause at the specified index of a `try_table`
2409+
// expression is a `catch_ref` or `catch_all_ref` clause (passes the exnref).
2410+
BINARYEN_API bool BinaryenTryTableIsCatchRefAt(BinaryenExpressionRef expr,
2411+
BinaryenIndex index);
2412+
// Sets whether the catch clause at the specified index of a `try_table`
2413+
// expression is a `catch_ref`/`catch_all_ref` clause.
2414+
BINARYEN_API void BinaryenTryTableSetCatchRefAt(BinaryenExpressionRef expr,
2415+
BinaryenIndex index,
2416+
bool catchRef);
2417+
// Appends a catch clause to a `try_table` expression, returning its insertion
2418+
// index. Pass NULL for `catchTag` for catch_all/catch_all_ref.
2419+
BINARYEN_API BinaryenIndex
2420+
BinaryenTryTableAppendCatch(BinaryenExpressionRef expr,
2421+
const char* catchTag,
2422+
const char* catchDest,
2423+
bool catchRef);
2424+
// Inserts a catch clause at the specified index of a `try_table` expression,
2425+
// moving existing clauses including the one previously at that index one
2426+
// index up.
2427+
BINARYEN_API void BinaryenTryTableInsertCatchAt(BinaryenExpressionRef expr,
2428+
BinaryenIndex index,
2429+
const char* catchTag,
2430+
const char* catchDest,
2431+
bool catchRef);
2432+
// Removes the catch clause at the specified index of a `try_table` expression,
2433+
// moving all subsequent clauses one index down. Returns the removed clause's
2434+
// destination label.
2435+
BINARYEN_API const char*
2436+
BinaryenTryTableRemoveCatchAt(BinaryenExpressionRef expr, BinaryenIndex index);
2437+
// Gets whether a `try_table` expression has a catch_all/catch_all_ref clause.
2438+
BINARYEN_API bool BinaryenTryTableHasCatchAll(BinaryenExpressionRef expr);
2439+
23652440
// Throw
23662441

23672442
// Gets the name of the tag being thrown by a `throw` expression.
@@ -2404,6 +2479,15 @@ BINARYEN_API const char* BinaryenRethrowGetTarget(BinaryenExpressionRef expr);
24042479
BINARYEN_API void BinaryenRethrowSetTarget(BinaryenExpressionRef expr,
24052480
const char* target);
24062481

2482+
// ThrowRef
2483+
2484+
// Gets the exnref operand of a `throw_ref` expression.
2485+
BINARYEN_API BinaryenExpressionRef
2486+
BinaryenThrowRefGetExnref(BinaryenExpressionRef expr);
2487+
// Sets the exnref operand of a `throw_ref` expression.
2488+
BINARYEN_API void BinaryenThrowRefSetExnref(BinaryenExpressionRef expr,
2489+
BinaryenExpressionRef exnrefExpr);
2490+
24072491
// TupleMake
24082492

24092493
// Gets the number of operands of a `tuple.make` expression.

0 commit comments

Comments
 (0)