feat(C & JS API)!: Add BinaryenHasMemorySegment#8286
feat(C & JS API)!: Add BinaryenHasMemorySegment#8286spotandjake wants to merge 2 commits intoWebAssembly:mainfrom
BinaryenHasMemorySegment#8286Conversation
b7f5068 to
cd887c1
Compare
|
The current |
I certainly could implement a The options I see going forward here would be:
I am happy todo any one of those, I just wanted to make sure the pr scope was clear before implementing. |
Makes sense, but yeah, those are quite different. "HasMemory" is back from the time before multimemory, when a memory existed or did not, and the other properties are quite special.
I think it makes sense to add that concept. |
This pr adds `BinaryenHasMemorySegment(module, name)` to the c api and `module.hasMemorySegment(name)` to the js api. chore: formatting fix: Ensure to call `preserveStack` before calling `strToStack`
c458717 to
2994ff6
Compare
|
I made those changes. This pr is now breaking. |
BinaryenHasMemorySegmentBinaryenHasMemorySegment
2994ff6 to
469d0d4
Compare
| 'passive': passive | ||
| }; | ||
| const segment = Module['_BinaryenGetMemorySegment'](module, strToStack(name)); | ||
| return segment === 0 ? null : segment; |
There was a problem hiding this comment.
Is this pattern needed? I don't see it elsewhere
There was a problem hiding this comment.
I figured the function should return null if the segment doesn't exist, I can remove it if it should be returning 0 instead though.
There was a problem hiding this comment.
Maybe it would be nicer as you suggest, but for consistency let's do it as other places do. We could consider changing the API in general separately.
This pr implements
BinaryenDataSegmentRefin the binaryen c api.The corresponding
BinaryenGetMemorySegmentandBinaryenGetMemorySegmentByIndexcan be used in the c-api to get a memory segment, andmodule.getMemorySegmentandmodule.getMemorySegmentByIndexcan be used in the js api.BinaryenGetMemorySegmentByteOffset,BinaryenGetMemorySegmentByteLength,BinaryenGetMemorySegmentPassiveandBinaryenCopyMemorySegmentDatato take aBinaryenDataSegmentRefwere converted to take a segmentRef as opposed to names, andgetMemorySegmentInfowas converted in the js api. AdditionallyBinaryenGetMemorySegmentNamewas implemented to retreive the segment name from a reference and the js api was modified to return this as part of the info.Motivation:
While adding a test to the grain compiler to check the size of a generated memory segment, I noticed that if the segment didn't exist, any operations on that segment would throw. As we are working from OCaml, we have no way to catch this fatal C++ exception related grain pr. As far as I am aware there is no other existing way to check if a segment exists, this change allows a user to use
getMemorySegmentwhich will returnNULLif the segment doesn't exist.getMemorySegmentallows a user to check for the existance of a segment before trying to perform operations on it.Related Binaryen.ml Issue
Related Grain issue