Skip to content

Commit b15fd5c

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

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();
@@ -1798,6 +1810,34 @@ BinaryenExpressionRef BinaryenRethrow(BinaryenModuleRef module,
17981810
Builder(*(Module*)module).makeRethrow(target));
17991811
}
18001812

1813+
BinaryenExpressionRef BinaryenTryTable(BinaryenModuleRef module,
1814+
BinaryenExpressionRef body,
1815+
const char** catchTags,
1816+
const char** catchDests,
1817+
const bool* catchRefs,
1818+
BinaryenIndex numCatches) {
1819+
std::vector<Name> tags;
1820+
std::vector<Name> dests;
1821+
std::vector<bool> refs;
1822+
tags.reserve(numCatches);
1823+
dests.reserve(numCatches);
1824+
refs.reserve(numCatches);
1825+
for (BinaryenIndex i = 0; i < numCatches; i++) {
1826+
tags.push_back(catchTags[i] ? Name(catchTags[i]) : Name());
1827+
dests.push_back(catchDests[i]);
1828+
refs.push_back(catchRefs[i]);
1829+
}
1830+
return static_cast<Expression*>(
1831+
Builder(*(Module*)module)
1832+
.makeTryTable((Expression*)body, tags, dests, refs));
1833+
}
1834+
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*>(
@@ -4154,6 +4194,125 @@ void BinaryenRethrowSetTarget(BinaryenExpressionRef expr, const char* target) {
41544194
assert(expression->is<Rethrow>());
41554195
static_cast<Rethrow*>(expression)->target = target;
41564196
}
4197+
// TryTable
4198+
BinaryenExpressionRef BinaryenTryTableGetBody(BinaryenExpressionRef expr) {
4199+
auto* expression = (Expression*)expr;
4200+
assert(expression->is<TryTable>());
4201+
return static_cast<TryTable*>(expression)->body;
4202+
}
4203+
void BinaryenTryTableSetBody(BinaryenExpressionRef expr,
4204+
BinaryenExpressionRef bodyExpr) {
4205+
auto* expression = (Expression*)expr;
4206+
assert(expression->is<TryTable>());
4207+
assert(bodyExpr);
4208+
static_cast<TryTable*>(expression)->body = (Expression*)bodyExpr;
4209+
}
4210+
BinaryenIndex BinaryenTryTableGetNumCatches(BinaryenExpressionRef expr) {
4211+
auto* expression = (Expression*)expr;
4212+
assert(expression->is<TryTable>());
4213+
return static_cast<TryTable*>(expression)->catchTags.size();
4214+
}
4215+
const char* BinaryenTryTableGetCatchTagAt(BinaryenExpressionRef expr,
4216+
BinaryenIndex index) {
4217+
auto* expression = (Expression*)expr;
4218+
assert(expression->is<TryTable>());
4219+
assert(index < static_cast<TryTable*>(expression)->catchTags.size());
4220+
auto name = static_cast<TryTable*>(expression)->catchTags[index];
4221+
return name.is() ? name.str.data() : nullptr;
4222+
}
4223+
void BinaryenTryTableSetCatchTagAt(BinaryenExpressionRef expr,
4224+
BinaryenIndex index,
4225+
const char* catchTag) {
4226+
auto* expression = (Expression*)expr;
4227+
assert(expression->is<TryTable>());
4228+
assert(index < static_cast<TryTable*>(expression)->catchTags.size());
4229+
static_cast<TryTable*>(expression)->catchTags[index] =
4230+
catchTag ? Name(catchTag) : Name();
4231+
}
4232+
const char* BinaryenTryTableGetCatchDestAt(BinaryenExpressionRef expr,
4233+
BinaryenIndex index) {
4234+
auto* expression = (Expression*)expr;
4235+
assert(expression->is<TryTable>());
4236+
assert(index < static_cast<TryTable*>(expression)->catchDests.size());
4237+
return static_cast<TryTable*>(expression)->catchDests[index].str.data();
4238+
}
4239+
void BinaryenTryTableSetCatchDestAt(BinaryenExpressionRef expr,
4240+
BinaryenIndex index,
4241+
const char* catchDest) {
4242+
auto* expression = (Expression*)expr;
4243+
assert(expression->is<TryTable>());
4244+
assert(index < static_cast<TryTable*>(expression)->catchDests.size());
4245+
static_cast<TryTable*>(expression)->catchDests[index] = catchDest;
4246+
}
4247+
bool BinaryenTryTableIsCatchRefAt(BinaryenExpressionRef expr,
4248+
BinaryenIndex index) {
4249+
auto* expression = (Expression*)expr;
4250+
assert(expression->is<TryTable>());
4251+
assert(index < static_cast<TryTable*>(expression)->catchRefs.size());
4252+
return static_cast<TryTable*>(expression)->catchRefs[index];
4253+
}
4254+
void BinaryenTryTableSetCatchRefAt(BinaryenExpressionRef expr,
4255+
BinaryenIndex index,
4256+
bool catchRef) {
4257+
auto* expression = (Expression*)expr;
4258+
assert(expression->is<TryTable>());
4259+
assert(index < static_cast<TryTable*>(expression)->catchRefs.size());
4260+
static_cast<TryTable*>(expression)->catchRefs[index] = catchRef;
4261+
}
4262+
BinaryenIndex BinaryenTryTableAppendCatch(BinaryenExpressionRef expr,
4263+
const char* catchTag,
4264+
const char* catchDest,
4265+
bool catchRef) {
4266+
auto* expression = (Expression*)expr;
4267+
assert(expression->is<TryTable>());
4268+
assert(catchDest);
4269+
auto* tryTable = static_cast<TryTable*>(expression);
4270+
auto index = tryTable->catchTags.size();
4271+
tryTable->catchTags.push_back(catchTag ? Name(catchTag) : Name());
4272+
tryTable->catchDests.push_back(Name(catchDest));
4273+
tryTable->catchRefs.push_back(catchRef);
4274+
return index;
4275+
}
4276+
void BinaryenTryTableInsertCatchAt(BinaryenExpressionRef expr,
4277+
BinaryenIndex index,
4278+
const char* catchTag,
4279+
const char* catchDest,
4280+
bool catchRef) {
4281+
auto* expression = (Expression*)expr;
4282+
assert(expression->is<TryTable>());
4283+
assert(catchDest);
4284+
auto* tryTable = static_cast<TryTable*>(expression);
4285+
tryTable->catchTags.insertAt(index, catchTag ? Name(catchTag) : Name());
4286+
tryTable->catchDests.insertAt(index, Name(catchDest));
4287+
tryTable->catchRefs.insertAt(index, catchRef);
4288+
}
4289+
const char* BinaryenTryTableRemoveCatchAt(BinaryenExpressionRef expr,
4290+
BinaryenIndex index) {
4291+
auto* expression = (Expression*)expr;
4292+
assert(expression->is<TryTable>());
4293+
auto* tryTable = static_cast<TryTable*>(expression);
4294+
tryTable->catchTags.removeAt(index);
4295+
tryTable->catchRefs.removeAt(index);
4296+
return tryTable->catchDests.removeAt(index).str.data();
4297+
}
4298+
bool BinaryenTryTableHasCatchAll(BinaryenExpressionRef expr) {
4299+
auto* expression = (Expression*)expr;
4300+
assert(expression->is<TryTable>());
4301+
return static_cast<TryTable*>(expression)->hasCatchAll();
4302+
}
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);
@@ -1044,6 +1048,16 @@ BinaryenThrow(BinaryenModuleRef module,
10441048
BinaryenIndex numOperands);
10451049
BINARYEN_API BinaryenExpressionRef BinaryenRethrow(BinaryenModuleRef module,
10461050
const char* target);
1051+
// TryTable: catch tag names may be NULL to denote catch_all or catch_all_ref.
1052+
// catchRefs[i] is true if the i-th catch is catch_ref or catch_all_ref.
1053+
BINARYEN_API BinaryenExpressionRef BinaryenTryTable(BinaryenModuleRef module,
1054+
BinaryenExpressionRef body,
1055+
const char** catchTags,
1056+
const char** catchDests,
1057+
const bool* catchRefs,
1058+
BinaryenIndex numCatches);
1059+
BINARYEN_API BinaryenExpressionRef
1060+
BinaryenThrowRef(BinaryenModuleRef module, BinaryenExpressionRef exnref);
10471061
BINARYEN_API BinaryenExpressionRef
10481062
BinaryenTupleMake(BinaryenModuleRef module,
10491063
BinaryenExpressionRef* operands,
@@ -2404,6 +2418,76 @@ BINARYEN_API const char* BinaryenRethrowGetTarget(BinaryenExpressionRef expr);
24042418
BINARYEN_API void BinaryenRethrowSetTarget(BinaryenExpressionRef expr,
24052419
const char* target);
24062420

2421+
// TryTable
2422+
2423+
// Gets the body expression of a `try_table` expression.
2424+
BINARYEN_API BinaryenExpressionRef
2425+
BinaryenTryTableGetBody(BinaryenExpressionRef expr);
2426+
// Sets the body expression of a `try_table` expression.
2427+
BINARYEN_API void BinaryenTryTableSetBody(BinaryenExpressionRef expr,
2428+
BinaryenExpressionRef bodyExpr);
2429+
// Gets the number of catch clauses of a `try_table` expression.
2430+
BINARYEN_API BinaryenIndex
2431+
BinaryenTryTableGetNumCatches(BinaryenExpressionRef expr);
2432+
// Gets the catch tag at the specified index of a `try_table` expression. Empty
2433+
// (NULL) for catch_all and catch_all_ref clauses.
2434+
BINARYEN_API const char*
2435+
BinaryenTryTableGetCatchTagAt(BinaryenExpressionRef expr, BinaryenIndex index);
2436+
// Sets the catch tag at the specified index of a `try_table` expression. Pass
2437+
// NULL for catch_all/catch_all_ref clauses.
2438+
BINARYEN_API void BinaryenTryTableSetCatchTagAt(BinaryenExpressionRef expr,
2439+
BinaryenIndex index,
2440+
const char* catchTag);
2441+
// Gets the catch destination label at the specified index of a `try_table`
2442+
// expression.
2443+
BINARYEN_API const char*
2444+
BinaryenTryTableGetCatchDestAt(BinaryenExpressionRef expr, BinaryenIndex index);
2445+
// Sets the catch destination label at the specified index of a `try_table`
2446+
// expression.
2447+
BINARYEN_API void BinaryenTryTableSetCatchDestAt(BinaryenExpressionRef expr,
2448+
BinaryenIndex index,
2449+
const char* catchDest);
2450+
// Gets whether the catch clause at the specified index of a `try_table`
2451+
// expression is a `catch_ref` or `catch_all_ref` clause (passes the exnref).
2452+
BINARYEN_API bool BinaryenTryTableIsCatchRefAt(BinaryenExpressionRef expr,
2453+
BinaryenIndex index);
2454+
// Sets whether the catch clause at the specified index of a `try_table`
2455+
// expression is a `catch_ref`/`catch_all_ref` clause.
2456+
BINARYEN_API void BinaryenTryTableSetCatchRefAt(BinaryenExpressionRef expr,
2457+
BinaryenIndex index,
2458+
bool catchRef);
2459+
// Appends a catch clause to a `try_table` expression, returning its insertion
2460+
// index. Pass NULL for `catchTag` for catch_all/catch_all_ref.
2461+
BINARYEN_API BinaryenIndex
2462+
BinaryenTryTableAppendCatch(BinaryenExpressionRef expr,
2463+
const char* catchTag,
2464+
const char* catchDest,
2465+
bool catchRef);
2466+
// Inserts a catch clause at the specified index of a `try_table` expression,
2467+
// moving existing clauses including the one previously at that index one
2468+
// index up.
2469+
BINARYEN_API void BinaryenTryTableInsertCatchAt(BinaryenExpressionRef expr,
2470+
BinaryenIndex index,
2471+
const char* catchTag,
2472+
const char* catchDest,
2473+
bool catchRef);
2474+
// Removes the catch clause at the specified index of a `try_table` expression,
2475+
// moving all subsequent clauses one index down. Returns the removed clause's
2476+
// destination label.
2477+
BINARYEN_API const char*
2478+
BinaryenTryTableRemoveCatchAt(BinaryenExpressionRef expr, BinaryenIndex index);
2479+
// Gets whether a `try_table` expression has a catch_all/catch_all_ref clause.
2480+
BINARYEN_API bool BinaryenTryTableHasCatchAll(BinaryenExpressionRef expr);
2481+
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)