Skip to content

Conversation

@FetBoba
Copy link

@FetBoba FetBoba commented Jan 1, 2026

No description provided.

Copy link
Contributor

@LPTK LPTK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! This looks well done to me. WDYT @Derppening?

@LPTK LPTK requested a review from Derppening January 1, 2026 16:40
Copy link

@Derppening Derppening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from some fields that appear to be better located in Ctx than WatBuilder, the rest looks good to me.

Please do add documentation where possible, including private members.

Copy link

@Derppening Derppening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the comments and also add some brief documentation for the private functions.


private def tupleArrayGet(
tupleExpr: Expr,
idxBuilder: Expr => Expr

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this needs to be a lambda and not just a simple Expr that evaluates to a single non-negative integer?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs to be lambda otherwise we end up with duplicating the index computation in both branches because we only get correct tupleRef inside each branch. And also negative indices require array.len(tupleRef)

Comment on lines 79 to 85
val tupleIsMutable = ref.test(tupleExpr, RefType(mutArrayType, nullable = true))
val mutableBranch =
val tupleRef = ref.cast(tupleExpr, RefType(mutArrayType, nullable = false))
array.get(mutArrayType, tupleRef, idxBuilder(tupleRef), elemType)
val immutableBranch =
val tupleRef = ref.cast(tupleExpr, RefType(immArrayType, nullable = false))
array.get(immArrayType, tupleRef, idxBuilder(tupleRef), elemType)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: If tupleExpr evaluates to an expression with possible side effects (e.g. a function call), would this repeated use of tupleExpr here cause the expression to be emitted 3 times and invoked twice during execution?

For example, in makePair(1, 2).[1], would makePair(1, 2) be evaluated twice?

If so, it may be worth it to generate a temporary local to store the result of tupleExpr, roughly like:

(if
  (local.tee $_tmp
    (ref.test (...))
  (then
    (array.get
      (ref.cast
        (local.get $_tmp)))

(local.tee sets the result of the child expression into the local, and returns the same value.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I added local.tee but it required extra handling for temp locals

Comment on lines 121 to 123
condition = i32.lt_s(idxI32, i32.const(0)),
ifTrue = i32.add(idxI32, array.len(tupleRef)),
ifFalse = S(idxI32),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idxI32 expression is emitted three times and will be executed twice during runtime, similar to this comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants