1
1
/*
2
- * Copyright (c) 2019, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2019, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* The Universal Permissive License (UPL), Version 1.0
40
40
*/
41
41
package org .graalvm .wasm ;
42
42
43
- import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
43
+ import java .util .Locale ;
44
+
44
45
import org .graalvm .wasm .exception .Failure ;
45
46
import org .graalvm .wasm .exception .WasmException ;
46
47
48
+ import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
49
+
47
50
public class Assert {
48
51
49
52
public static void assertByteEqual (byte b1 , byte b2 , Failure failure ) throws WasmException {
50
53
if (b1 != b2 ) {
51
- fail (failure , format ( "%s: 0x%02X should = 0x%02X" , failure .name , b1 , b2 ) );
54
+ fail (failure , "%s: 0x%02X should = 0x%02X" , failure .name , b1 , b2 );
52
55
}
53
56
}
54
57
55
58
public static void assertByteEqual (byte b1 , byte b2 , String message , Failure failure ) throws WasmException {
56
59
if (b1 != b2 ) {
57
- fail (failure , format ( "%s: 0x%02X should = 0x%02X" , message , b1 , b2 ) );
60
+ fail (failure , "%s: 0x%02X should = 0x%02X" , message , b1 , b2 );
58
61
}
59
62
}
60
63
61
64
public static void assertIntEqual (int actual , int expected , Failure failure ) throws WasmException {
62
- assertIntEqual (actual , expected , failure . name , failure );
65
+ assertIntEqual (actual , expected , failure , failure . name );
63
66
}
64
67
65
- public static void assertIntEqual (int actual , int expected , String message , Failure failure ) throws WasmException {
68
+ public static void assertIntEqual (int actual , int expected , Failure failure , String message ) throws WasmException {
66
69
if (actual != expected ) {
67
- fail (failure , format ("%s: %d should = %d" , message , actual , expected ));
70
+ fail (failure , "%s: %d should = %d" , message , actual , expected );
71
+ }
72
+ }
73
+
74
+ public static void assertIntEqual (int actual , int expected , Failure failure , String format , int arg ) throws WasmException {
75
+ if (actual != expected ) {
76
+ fail (failure , "%s: %d should = %d" , format (format , arg ), actual , expected );
68
77
}
69
78
}
70
79
71
80
public static void assertIntGreaterOrEqual (int n1 , int n2 , Failure failure ) throws WasmException {
72
81
if (n1 < n2 ) {
73
- fail (failure , format ( "%s: %d should be >= %d" , failure .name , n1 , n2 ) );
82
+ fail (failure , "%s: %d should be >= %d" , failure .name , n1 , n2 );
74
83
}
75
84
}
76
85
77
86
public static void assertIntGreater (int n1 , int n2 , String message , Failure failure ) throws WasmException {
78
87
if (n1 <= n2 ) {
79
- fail (failure , format ( "%s: %d should be > %d" , message , n1 , n2 ) );
88
+ fail (failure , "%s: %d should be > %d" , message , n1 , n2 );
80
89
}
81
90
}
82
91
@@ -86,7 +95,7 @@ public static void assertIntLessOrEqual(int n1, int n2, Failure failure) throws
86
95
87
96
public static void assertIntLess (int n1 , int n2 , Failure failure ) throws WasmException {
88
97
if (n1 >= n2 ) {
89
- fail (failure , format ( "%s: %d should be < %d" , failure .name , n1 , n2 ) );
98
+ fail (failure , "%s: %d should be < %d" , failure .name , n1 , n2 );
90
99
}
91
100
}
92
101
@@ -96,13 +105,25 @@ public static void assertUnsignedIntLess(int n1, int n2, Failure failure) throws
96
105
97
106
public static void assertUnsignedIntLess (int n1 , int n2 , Failure failure , String message ) throws WasmException {
98
107
if (Integer .compareUnsigned (n1 , n2 ) >= 0 ) {
99
- fail (failure , format ("%s: %s should be < %s" , message , Integer .toUnsignedString (n1 ), Integer .toUnsignedString (n2 )));
108
+ fail (failure , "%s: %s should be < %s" , message , Integer .toUnsignedString (n1 ), Integer .toUnsignedString (n2 ));
109
+ }
110
+ }
111
+
112
+ public static void assertUnsignedIntLess (int n1 , int n2 , Failure failure , String format , Object arg ) throws WasmException {
113
+ if (Integer .compareUnsigned (n1 , n2 ) >= 0 ) {
114
+ fail (failure , "%s: %s should be < %s" , format (format , arg ), Integer .toUnsignedString (n1 ), Integer .toUnsignedString (n2 ));
115
+ }
116
+ }
117
+
118
+ public static void assertUnsignedIntLess (int n1 , int n2 , Failure failure , String format , Object arg1 , Object arg2 ) throws WasmException {
119
+ if (Integer .compareUnsigned (n1 , n2 ) >= 0 ) {
120
+ fail (failure , "%s: %s should be < %s" , format (format , arg1 , arg2 ), Integer .toUnsignedString (n1 ), Integer .toUnsignedString (n2 ));
100
121
}
101
122
}
102
123
103
124
public static void assertIntLessOrEqual (int n1 , int n2 , String message , Failure failure ) throws WasmException {
104
125
if (n1 > n2 ) {
105
- fail (failure , format ( "%s: %d should be <= %d" , message , n1 , n2 ) );
126
+ fail (failure , "%s: %d should be <= %d" , message , n1 , n2 );
106
127
}
107
128
}
108
129
@@ -112,7 +133,7 @@ public static void assertUnsignedIntLessOrEqual(int n1, int n2, Failure failure)
112
133
113
134
public static void assertUnsignedIntLessOrEqual (int n1 , int n2 , Failure failure , String message ) throws WasmException {
114
135
if (Integer .compareUnsigned (n1 , n2 ) > 0 ) {
115
- fail (failure , format ( "%s: %s should be <= %s" , message , Integer .toUnsignedString (n1 ), Integer .toUnsignedString (n2 ) ));
136
+ fail (failure , "%s: %s should be <= %s" , message , Integer .toUnsignedString (n1 ), Integer .toUnsignedString (n2 ));
116
137
}
117
138
}
118
139
@@ -122,7 +143,7 @@ public static void assertUnsignedLongLess(long n1, long n2, Failure failure) thr
122
143
123
144
public static void assertUnsignedLongLess (long n1 , long n2 , Failure failure , String message ) throws WasmException {
124
145
if (Long .compareUnsigned (n1 , n2 ) >= 0 ) {
125
- fail (failure , format ( "%s: %s should be < %s" , message , Long .toUnsignedString (n1 ), Long .toUnsignedString (n2 ) ));
146
+ fail (failure , "%s: %s should be < %s" , message , Long .toUnsignedString (n1 ), Long .toUnsignedString (n2 ));
126
147
}
127
148
}
128
149
@@ -132,7 +153,7 @@ public static void assertUnsignedLongLessOrEqual(long n1, long n2, Failure failu
132
153
133
154
public static void assertUnsignedLongLessOrEqual (long n1 , long n2 , Failure failure , String message ) throws WasmException {
134
155
if (Long .compareUnsigned (n1 , n2 ) > 0 ) {
135
- fail (failure , format ( "%s: %s should be <= %s" , message , Long .toUnsignedString (n1 ), Long .toUnsignedString (n2 ) ));
156
+ fail (failure , "%s: %s should be <= %s" , message , Long .toUnsignedString (n1 ), Long .toUnsignedString (n2 ));
136
157
}
137
158
}
138
159
@@ -142,7 +163,7 @@ public static void assertUnsignedIntGreaterOrEqual(int n1, int n2, Failure failu
142
163
143
164
public static void assertUnsignedIntGreaterOrEqual (int n1 , int n2 , Failure failure , String message ) throws WasmException {
144
165
if (Integer .compareUnsigned (n1 , n2 ) < 0 ) {
145
- fail (failure , format ( "%s: %s should be >= %s" , message , Integer .toUnsignedString (n1 ), Integer .toUnsignedString (n2 ) ));
166
+ fail (failure , "%s: %s should be >= %s" , message , Integer .toUnsignedString (n1 ), Integer .toUnsignedString (n2 ));
146
167
}
147
168
}
148
169
@@ -152,19 +173,19 @@ public static void assertUnsignedLongGreaterOrEqual(long n1, long n2, Failure fa
152
173
153
174
public static void assertUnsignedLongGreaterOrEqual (long n1 , long n2 , Failure failure , String message ) throws WasmException {
154
175
if (Long .compareUnsigned (n1 , n2 ) < 0 ) {
155
- fail (failure , format ( "%s: %s should be >= %s" , message , Long .toUnsignedString (n1 ), Long .toUnsignedString (n2 ) ));
176
+ fail (failure , "%s: %s should be >= %s" , message , Long .toUnsignedString (n1 ), Long .toUnsignedString (n2 ));
156
177
}
157
178
}
158
179
159
180
public static void assertLongLessOrEqual (long n1 , long n2 , Failure failure ) throws WasmException {
160
181
if (n1 > n2 ) {
161
- fail (failure , format ( "%s: %d should be <= %d" , failure .name , n1 , n2 ) );
182
+ fail (failure , "%s: %d should be <= %d" , failure .name , n1 , n2 );
162
183
}
163
184
}
164
185
165
186
public static void assertNotNull (Object object , String message , Failure failure ) throws WasmException {
166
187
if (object == null ) {
167
- fail (failure , format ( "%s: expected a non-null value" , message ) );
188
+ fail (failure , "%s: expected a non-null value" , message );
168
189
}
169
190
}
170
191
@@ -179,13 +200,28 @@ public static void assertTrue(boolean condition, String message, Failure failure
179
200
}
180
201
181
202
@ TruffleBoundary
182
- public static RuntimeException fail (Failure failure , String message , Object ... args ) throws WasmException {
183
- throw WasmException .format (failure , message , args );
203
+ public static RuntimeException fail (Failure failure , String format , Object ... args ) throws WasmException {
204
+ throw WasmException .format (failure , format , args );
205
+ }
206
+
207
+ @ TruffleBoundary
208
+ public static RuntimeException fail (Failure failure , String format , Object arg ) throws WasmException {
209
+ throw WasmException .format (failure , format , arg );
210
+ }
211
+
212
+ @ TruffleBoundary
213
+ public static RuntimeException fail (Failure failure , String format , int arg ) throws WasmException {
214
+ throw WasmException .format (failure , format , arg );
215
+ }
216
+
217
+ @ TruffleBoundary
218
+ public static RuntimeException fail (Failure failure , String message ) throws WasmException {
219
+ throw WasmException .create (failure , message );
184
220
}
185
221
186
222
@ TruffleBoundary
187
223
private static String format (String format , Object ... args ) {
188
- return String .format (format , args );
224
+ return String .format (Locale . ROOT , format , args );
189
225
}
190
226
191
227
}
0 commit comments