@@ -1440,17 +1440,37 @@ export class Compiler extends DiagnosticEmitter {
14401440 var nativeThisType = this . options . nativeSizeType ;
14411441 var nativeValueType = type . toNativeType ( ) ;
14421442 var module = this . module ;
1443- var valueExpr = module . local_get ( 1 , nativeValueType ) ;
1443+ var valueExpr : ExpressionRef ;
1444+ var varTypes : NativeType [ ] | null = null ;
14441445 if ( type . isManaged ) {
1445- valueExpr = this . makeReplace (
1446- module . load ( type . byteSize , false ,
1447- module . local_get ( 0 , nativeThisType ) ,
1448- nativeValueType , instance . memoryOffset
1446+ // Can't use makeReplace here since there's no corresponding flow, so
1447+ // 0: this, 1: value, 2: oldValue (temp)
1448+ valueExpr = module . block ( null , [
1449+ module . if (
1450+ module . binary ( nativeValueType == NativeType . I64 ? BinaryOp . NeI64 : BinaryOp . NeI32 ,
1451+ // value != (oldValue = this.field)
1452+ module . local_get ( 1 , nativeValueType ) ,
1453+ module . local_tee ( 2 ,
1454+ module . load ( type . byteSize , false ,
1455+ module . local_get ( 0 , nativeThisType ) ,
1456+ nativeValueType , instance . memoryOffset
1457+ )
1458+ )
1459+ ) ,
1460+ module . block ( null , [
1461+ module . drop (
1462+ this . makeRetain ( module . local_get ( 1 , nativeValueType ) )
1463+ ) ,
1464+ this . makeRelease ( module . local_get ( 2 , nativeValueType ) )
1465+ ] )
14491466 ) ,
1450- valueExpr
1451- ) ;
1467+ module . local_get ( 1 , nativeValueType )
1468+ ] , nativeValueType ) ;
1469+ varTypes = [ nativeValueType ] ;
1470+ } else {
1471+ valueExpr = module . local_get ( 1 , nativeValueType ) ;
14521472 }
1453- instance . setterRef = module . addFunction ( instance . internalSetterName , createType ( [ nativeThisType , nativeValueType ] ) , NativeType . None , null ,
1473+ instance . setterRef = module . addFunction ( instance . internalSetterName , createType ( [ nativeThisType , nativeValueType ] ) , NativeType . None , varTypes ,
14541474 module . store ( type . byteSize ,
14551475 module . local_get ( 0 , nativeThisType ) ,
14561476 valueExpr ,
@@ -6702,7 +6722,14 @@ export class Compiler extends DiagnosticEmitter {
67026722 }
67036723
67046724 /** Makes a replace, retaining the new expression's value and releasing the old expression's value, in this order. */
6705- makeReplace ( oldExpr : ExpressionRef , newExpr : ExpressionRef , alreadyRetained : bool = false ) : ExpressionRef {
6725+ makeReplace (
6726+ /** Old value being replaced. */
6727+ oldExpr : ExpressionRef ,
6728+ /** New value being assigned. */
6729+ newExpr : ExpressionRef ,
6730+ /** Whether the new value is already retained. */
6731+ alreadyRetained : bool = false ,
6732+ ) : ExpressionRef {
67066733 var module = this . module ;
67076734 var flow = this . currentFlow ;
67086735 var nativeSizeType = this . options . nativeSizeType ;
0 commit comments