Skip to content

Commit b5a4e36

Browse files
authored
[JS API] Add missing ids to getExpressionInfo and fix existing bugs (#7507)
This PR adds cases for GC expressions in `getExpressionInfo` and updates `CallIndirectId` (fixes #7490), `TryId`. In addition, it also adds tests for `getExpressionInfo` results, which were not checked before.
1 parent 93214fb commit b5a4e36

File tree

3 files changed

+1002
-20
lines changed

3 files changed

+1002
-20
lines changed

src/js/binaryen.js-post.js

Lines changed: 168 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,14 +3058,16 @@ Module['getExpressionType'] = function(expr) {
30583058
Module['getExpressionInfo'] = function(expr) {
30593059
const id = Module['_BinaryenExpressionGetId'](expr);
30603060
const type = Module['_BinaryenExpressionGetType'](expr);
3061-
switch (id) { // TODO: GC instructions
3062-
case Module['BlockId']:
3061+
switch (id) {
3062+
case Module['BlockId']: {
3063+
const name = Module['_BinaryenBlockGetName'](expr);
30633064
return {
30643065
'id': id,
30653066
'type': type,
3066-
'name': UTF8ToString(Module['_BinaryenBlockGetName'](expr)),
3067+
'name': name ? UTF8ToString(name) : null,
30673068
'children': getAllNested(expr, Module['_BinaryenBlockGetNumChildren'], Module['_BinaryenBlockGetChildAt'])
30683069
};
3070+
}
30693071
case Module['IfId']:
30703072
return {
30713073
'id': id,
@@ -3074,21 +3076,25 @@ Module['getExpressionInfo'] = function(expr) {
30743076
'ifTrue': Module['_BinaryenIfGetIfTrue'](expr),
30753077
'ifFalse': Module['_BinaryenIfGetIfFalse'](expr)
30763078
};
3077-
case Module['LoopId']:
3079+
case Module['LoopId']: {
3080+
const name = Module['_BinaryenLoopGetName'](expr);
30783081
return {
30793082
'id': id,
30803083
'type': type,
3081-
'name': UTF8ToString(Module['_BinaryenLoopGetName'](expr)),
3084+
'name': name ? UTF8ToString(name) : null,
30823085
'body': Module['_BinaryenLoopGetBody'](expr)
30833086
};
3084-
case Module['BreakId']:
3087+
}
3088+
case Module['BreakId']: {
3089+
const name = Module['_BinaryenBreakGetName'](expr);
30853090
return {
30863091
'id': id,
30873092
'type': type,
3088-
'name': UTF8ToString(Module['_BinaryenBreakGetName'](expr)),
3093+
'name': name ? UTF8ToString(name) : null,
30893094
'condition': Module['_BinaryenBreakGetCondition'](expr),
30903095
'value': Module['_BinaryenBreakGetValue'](expr)
30913096
};
3097+
}
30923098
case Module['SwitchId']:
30933099
return {
30943100
'id': id,
@@ -3113,8 +3119,10 @@ Module['getExpressionInfo'] = function(expr) {
31133119
'type': type,
31143120
'isReturn': Boolean(Module['_BinaryenCallIndirectIsReturn'](expr)),
31153121
'target': Module['_BinaryenCallIndirectGetTarget'](expr),
3116-
'table': Module['_BinaryenCallIndirectGetTable'](expr),
3117-
'operands': getAllNested(expr, Module['_BinaryenCallIndirectGetNumOperands'], Module['_BinaryenCallIndirectGetOperandAt'])
3122+
'table': UTF8ToString(Module['_BinaryenCallIndirectGetTable'](expr)),
3123+
'operands': getAllNested(expr, Module['_BinaryenCallIndirectGetNumOperands'], Module['_BinaryenCallIndirectGetOperandAt']),
3124+
'params': Module['_BinaryenCallIndirectGetParams'](expr),
3125+
'results': Module['_BinaryenCallIndirectGetResults'](expr)
31183126
};
31193127
case Module['LocalGetId']:
31203128
return {
@@ -3192,7 +3200,8 @@ Module['getExpressionInfo'] = function(expr) {
31923200
'bytes': Module['_BinaryenStoreGetBytes'](expr),
31933201
'align': Module['_BinaryenStoreGetAlign'](expr),
31943202
'ptr': Module['_BinaryenStoreGetPtr'](expr),
3195-
'value': Module['_BinaryenStoreGetValue'](expr)
3203+
'value': Module['_BinaryenStoreGetValue'](expr),
3204+
'valueType': Module['_BinaryenStoreGetValueType'](expr)
31963205
};
31973206
case Module['ConstId']: {
31983207
let value;
@@ -3386,11 +3395,13 @@ Module['getExpressionInfo'] = function(expr) {
33863395
'align': Module['_BinaryenSIMDLoadStoreLaneGetAlign'](expr),
33873396
'index': Module['_BinaryenSIMDLoadStoreLaneGetIndex'](expr),
33883397
'ptr': Module['_BinaryenSIMDLoadStoreLaneGetPtr'](expr),
3389-
'vec': Module['_BinaryenSIMDLoadStoreLaneGetVec'](expr)
3398+
'vec': Module['_BinaryenSIMDLoadStoreLaneGetVec'](expr),
3399+
'isStore': Boolean(Module['_BinaryenSIMDLoadStoreLaneIsStore'](expr))
33903400
};
33913401
case Module['MemoryInitId']:
33923402
return {
33933403
'id': id,
3404+
'type': type,
33943405
'segment': UTF8ToString(Module['_BinaryenMemoryInitGetSegment'](expr)),
33953406
'dest': Module['_BinaryenMemoryInitGetDest'](expr),
33963407
'offset': Module['_BinaryenMemoryInitGetOffset'](expr),
@@ -3399,18 +3410,21 @@ Module['getExpressionInfo'] = function(expr) {
33993410
case Module['DataDropId']:
34003411
return {
34013412
'id': id,
3413+
'type': type,
34023414
'segment': UTF8ToString(Module['_BinaryenDataDropGetSegment'](expr)),
34033415
};
34043416
case Module['MemoryCopyId']:
34053417
return {
34063418
'id': id,
3419+
'type': type,
34073420
'dest': Module['_BinaryenMemoryCopyGetDest'](expr),
34083421
'source': Module['_BinaryenMemoryCopyGetSource'](expr),
34093422
'size': Module['_BinaryenMemoryCopyGetSize'](expr)
34103423
};
34113424
case Module['MemoryFillId']:
34123425
return {
34133426
'id': id,
3427+
'type': type,
34143428
'dest': Module['_BinaryenMemoryFillGetDest'](expr),
34153429
'value': Module['_BinaryenMemoryFillGetValue'](expr),
34163430
'size': Module['_BinaryenMemoryFillGetSize'](expr)
@@ -3446,18 +3460,155 @@ Module['getExpressionInfo'] = function(expr) {
34463460
'left': Module['_BinaryenRefEqGetLeft'](expr),
34473461
'right': Module['_BinaryenRefEqGetRight'](expr)
34483462
};
3449-
case Module['TryId']:
3463+
case Module['RefTestId']:
3464+
return {
3465+
'id': id,
3466+
'type': type,
3467+
'ref': Module['_BinaryenRefTestGetRef'](expr),
3468+
'castType': Module['_BinaryenRefTestGetCastType'](expr)
3469+
};
3470+
case Module['RefCastId']:
3471+
return {
3472+
'id': id,
3473+
'type': type,
3474+
'ref': Module['_BinaryenRefCastGetRef'](expr)
3475+
};
3476+
case Module['BrOnId']:
3477+
return {
3478+
'id': id,
3479+
'type': type,
3480+
'op': Module['_BinaryenBrOnGetOp'](expr),
3481+
'name': UTF8ToString(Module['_BinaryenBrOnGetName'](expr)),
3482+
'ref': Module['_BinaryenBrOnGetRef'](expr),
3483+
'castType': Module['_BinaryenBrOnGetCastType'](expr)
3484+
};
3485+
case Module['StructNewId']:
3486+
return {
3487+
'id': id,
3488+
'type': type,
3489+
'operands': getAllNested(expr, Module['_BinaryenStructNewGetNumOperands'], Module['_BinaryenStructNewGetOperandAt']),
3490+
};
3491+
case Module['StructGetId']:
3492+
return {
3493+
'id': id,
3494+
'type': type,
3495+
'index': Module['_BinaryenStructGetGetIndex'](expr),
3496+
'ref': Module['_BinaryenStructGetGetRef'](expr),
3497+
'isSigned': Boolean(Module['_BinaryenStructGetIsSigned'](expr))
3498+
};
3499+
case Module['StructSetId']:
3500+
return {
3501+
'id': id,
3502+
'type': type,
3503+
'index': Module['_BinaryenStructSetGetIndex'](expr),
3504+
'ref': Module['_BinaryenStructSetGetRef'](expr),
3505+
'value': Module['_BinaryenStructSetGetValue'](expr)
3506+
};
3507+
case Module['ArrayNewId']:
3508+
return {
3509+
'id': id,
3510+
'type': type,
3511+
'init': Module['_BinaryenArrayNewGetInit'](expr),
3512+
'size': Module['_BinaryenArrayNewGetSize'](expr)
3513+
};
3514+
case Module['ArrayNewFixedId']:
3515+
return {
3516+
'id': id,
3517+
'type': type,
3518+
'values': getAllNested(expr, Module['_BinaryenArrayNewFixedGetNumValues'], Module['_BinaryenArrayNewFixedGetValueAt'])
3519+
};
3520+
case Module['ArrayNewDataId']:
3521+
return {
3522+
'id': id,
3523+
'type': type,
3524+
'segment': UTF8ToString(Module['_BinaryenArrayNewDataGetSegment'](expr)),
3525+
'offset': Module['_BinaryenArrayNewDataGetOffset'](expr),
3526+
'size': Module['_BinaryenArrayNewDataGetSize'](expr)
3527+
};
3528+
case Module['ArrayNewElemId']:
3529+
return {
3530+
'id': id,
3531+
'type': type,
3532+
'segment': UTF8ToString(Module['_BinaryenArrayNewElemGetSegment'](expr)),
3533+
'offset': Module['_BinaryenArrayNewElemGetOffset'](expr),
3534+
'size': Module['_BinaryenArrayNewElemGetSize'](expr)
3535+
};
3536+
case Module['ArrayGetId']:
3537+
return {
3538+
'id': id,
3539+
'type': type,
3540+
'ref': Module['_BinaryenArrayGetGetRef'](expr),
3541+
'index': Module['_BinaryenArrayGetGetIndex'](expr),
3542+
'isSigned': Boolean(Module['_BinaryenArrayGetIsSigned'](expr))
3543+
};
3544+
case Module['ArraySetId']:
3545+
return {
3546+
'id': id,
3547+
'type': type,
3548+
'ref': Module['_BinaryenArraySetGetRef'](expr),
3549+
'index': Module['_BinaryenArraySetGetIndex'](expr),
3550+
'value': Module['_BinaryenArraySetGetValue'](expr)
3551+
};
3552+
case Module['ArrayLenId']:
3553+
return {
3554+
'id': id,
3555+
'type': type,
3556+
'ref': Module['_BinaryenArrayLenGetRef'](expr)
3557+
};
3558+
case Module['ArrayFillId']:
3559+
return {
3560+
'id': id,
3561+
'type': type,
3562+
'ref': Module['_BinaryenArrayFillGetRef'](expr),
3563+
'index': Module['_BinaryenArrayFillGetIndex'](expr),
3564+
'value': Module['_BinaryenArrayFillGetValue'](expr),
3565+
'size': Module['_BinaryenArrayFillGetSize'](expr)
3566+
};
3567+
case Module['ArrayCopyId']:
3568+
return {
3569+
'id': id,
3570+
'type': type,
3571+
'destRef': Module['_BinaryenArrayCopyGetDestRef'](expr),
3572+
'destIndex': Module['_BinaryenArrayCopyGetDestIndex'](expr),
3573+
'srcRef': Module['_BinaryenArrayCopyGetSrcRef'](expr),
3574+
'srcIndex': Module['_BinaryenArrayCopyGetSrcIndex'](expr),
3575+
'length': Module['_BinaryenArrayCopyGetLength'](expr)
3576+
};
3577+
case Module['ArrayInitDataId']:
34503578
return {
34513579
'id': id,
34523580
'type': type,
3453-
'name': UTF8ToString(Module['_BinaryenTryGetName'](expr)),
3581+
'segment': UTF8ToString(Module['_BinaryenArrayInitDataGetSegment'](expr)),
3582+
'ref': Module['_BinaryenArrayInitDataGetRef'](expr),
3583+
'index': Module['_BinaryenArrayInitDataGetIndex'](expr),
3584+
'offset': Module['_BinaryenArrayInitDataGetOffset'](expr),
3585+
'size': Module['_BinaryenArrayInitDataGetSize'](expr)
3586+
};
3587+
case Module['ArrayInitElemId']:
3588+
return {
3589+
'id': id,
3590+
'type': type,
3591+
'segment': UTF8ToString(Module['_BinaryenArrayInitElemGetSegment'](expr)),
3592+
'ref': Module['_BinaryenArrayInitElemGetRef'](expr),
3593+
'index': Module['_BinaryenArrayInitElemGetIndex'](expr),
3594+
'offset': Module['_BinaryenArrayInitElemGetOffset'](expr),
3595+
'size': Module['_BinaryenArrayInitElemGetSize'](expr)
3596+
};
3597+
case Module['TryId']: {
3598+
const name = Module['_BinaryenTryGetName'](expr);
3599+
const delegateTarget = Module['_BinaryenTryGetDelegateTarget'](expr);
3600+
return {
3601+
'id': id,
3602+
'type': type,
3603+
'name': name ? UTF8ToString(name) : null,
34543604
'body': Module['_BinaryenTryGetBody'](expr),
3455-
'catchTags': getAllNested(expr, Module['_BinaryenTryGetNumCatchTags'], Module['_BinaryenTryGetCatchTagAt']),
3605+
'catchTags': getAllNested(expr, Module['_BinaryenTryGetNumCatchTags'], Module['_BinaryenTryGetCatchTagAt']).map(p => UTF8ToString(p)),
34563606
'catchBodies': getAllNested(expr, Module['_BinaryenTryGetNumCatchBodies'], Module['_BinaryenTryGetCatchBodyAt']),
3457-
'hasCatchAll': Module['_BinaryenTryHasCatchAll'](expr),
3458-
'delegateTarget': UTF8ToString(Module['_BinaryenTryGetDelegateTarget'](expr)),
3459-
'isDelegate': Module['_BinaryenTryIsDelegate'](expr)
3607+
'hasCatchAll': Boolean(Module['_BinaryenTryHasCatchAll'](expr)),
3608+
'delegateTarget': delegateTarget ? UTF8ToString(delegateTarget) : null,
3609+
'isDelegate': Boolean(Module['_BinaryenTryIsDelegate'](expr))
34603610
};
3611+
}
34613612
case Module['ThrowId']:
34623613
return {
34633614
'id': id,

test/binaryen.js/exception-handling.js.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@
3636

3737
getExpressionInfo(throw) = {"id":54,"type":1,"tag":"e"}
3838
getExpressionInfo(rethrow) = {"id":55,"type":1,"target":"l0"}
39-
getExpressionInfo(try_catch) = {"id":52,"type":1,"name":"l0","hasCatchAll":0,"delegateTarget":"","isDelegate":0}
40-
getExpressionInfo(try_delegate) = {"id":52,"type":0,"name":"try_outer","hasCatchAll":1,"delegateTarget":"","isDelegate":0}
39+
getExpressionInfo(try_catch) = {"id":52,"type":1,"name":"l0","hasCatchAll":false,"delegateTarget":null,"isDelegate":false}
40+
getExpressionInfo(try_delegate) = {"id":52,"type":0,"name":"try_outer","hasCatchAll":true,"delegateTarget":null,"isDelegate":false}

0 commit comments

Comments
 (0)