22
33use std:: fmt:: Write ;
44
5- use crate :: intrinsics:: Intrinsic ;
5+ use crate :: intrinsics:: { Intrinsic , RenderIntrinsicsArgs } ;
66use crate :: source:: Source ;
77use crate :: uwriteln;
88
@@ -100,7 +100,7 @@ impl ConversionIntrinsic {
100100 }
101101
102102 /// Render an intrinsic to a string
103- pub fn render ( & self , output : & mut Source ) {
103+ pub fn render ( & self , output : & mut Source , render_args : & RenderIntrinsicsArgs ) {
104104 match self {
105105 Self :: I32ToF32 => output. push_str (
106106 "
@@ -125,40 +125,58 @@ impl ConversionIntrinsic {
125125 " ) ,
126126
127127 Self :: ToBigInt64 => {
128- let ensure_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
128+ let strict_checks = if render_args. transpile_opts . strict {
129+ let require_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
130+ format ! ( "{require_valid_numeric_primitive_fn}('s64', converted);" )
131+ } else {
132+ "" . into ( )
133+ } ;
134+
129135 uwriteln ! (
130136 output,
131137 r#"
132138 function toInt64(val) {{
133139 const converted = BigInt(val)
134- {ensure_valid_numeric_primitive_fn}('s64', converted);
140+ {strict_checks}
135141 return BigInt.asIntN(64, converted);
136142 }}
137143 "#
138144 )
139145 } ,
140146
141147 Self :: ToBigUint64 => {
142- let ensure_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
148+ let strict_checks = if render_args. transpile_opts . strict {
149+ let require_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
150+ format ! ( "{require_valid_numeric_primitive_fn}('u64', converted);" )
151+ } else {
152+ "" . into ( )
153+ } ;
154+
143155 uwriteln ! (
144156 output,
145157 r#"
146158 function toUint64(val) {{
147159 const converted = BigInt(val)
148- {ensure_valid_numeric_primitive_fn}('u64', converted);
160+ {strict_checks}
149161 return BigInt.asUintN(64, converted);
150162 }}
151163 "#
152164 ) ;
153165 } ,
154166
155167 Self :: ToInt16 => {
156- let ensure_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
168+ let strict_checks = if render_args. transpile_opts . strict {
169+ let require_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
170+ format ! ( "{require_valid_numeric_primitive_fn}('s16', val);" )
171+ } else {
172+ "" . into ( )
173+ } ;
174+
157175 uwriteln ! (
158176 output,
159177 r#"
160178 function toInt16(val) {{
161- {ensure_valid_numeric_primitive_fn}('s16', val);
179+ {strict_checks}
162180 val >>>= 0;
163181 val %= 2 ** 16;
164182 if (val >= 2 ** 15) {{
@@ -171,12 +189,18 @@ impl ConversionIntrinsic {
171189 } ,
172190
173191 Self :: ToUint16 => {
174- let ensure_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
192+ let strict_checks = if render_args. transpile_opts . strict {
193+ let require_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
194+ format ! ( "{require_valid_numeric_primitive_fn}('u16', val);" )
195+ } else {
196+ "" . into ( )
197+ } ;
198+
175199 uwriteln ! (
176200 output,
177201 r#"
178202 function toUint16(val) {{
179- {ensure_valid_numeric_primitive_fn}('u16', val);
203+ {strict_checks}
180204 val >>>= 0;
181205 val %= 2 ** 16;
182206 return val;
@@ -186,25 +210,37 @@ impl ConversionIntrinsic {
186210 } ,
187211
188212 Self :: ToInt32 => {
189- let ensure_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
213+ let strict_checks = if render_args. transpile_opts . strict {
214+ let require_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
215+ format ! ( "{require_valid_numeric_primitive_fn}('s32', val);" )
216+ } else {
217+ "" . into ( )
218+ } ;
219+
190220 uwriteln ! (
191221 output,
192222 r#"
193223 function toInt32(val) {{
194- {ensure_valid_numeric_primitive_fn}('s32', val);
224+ {strict_checks}
195225 return val >> 0;
196226 }}
197227 "#
198228 ) ;
199229 } ,
200230
201231 Self :: ToInt8 => {
202- let ensure_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
232+ let strict_checks = if render_args. transpile_opts . strict {
233+ let require_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
234+ format ! ( "{require_valid_numeric_primitive_fn}('s8', val);" )
235+ } else {
236+ "" . into ( )
237+ } ;
238+
203239 uwriteln ! (
204240 output,
205241 r#"
206242 function toInt8(val) {{
207- {ensure_valid_numeric_primitive_fn}('s8', val);
243+ {strict_checks}
208244 val >>>= 0;
209245 val %= 2 ** 8;
210246 if (val >= 2 ** 7) {{
@@ -238,25 +274,37 @@ impl ConversionIntrinsic {
238274
239275
240276 Self :: ToUint32 => {
241- let ensure_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
277+ let strict_checks = if render_args. transpile_opts . strict {
278+ let require_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
279+ format ! ( "{require_valid_numeric_primitive_fn}('u32', val);" )
280+ } else {
281+ "" . into ( )
282+ } ;
283+
242284 uwriteln ! (
243285 output,
244286 r#"
245287 function toUint32(val) {{
246- {ensure_valid_numeric_primitive_fn}('u32', val);
288+ {strict_checks}
247289 return val >>> 0;
248290 }}
249291 "#
250292 )
251293 } ,
252294
253295 Self :: ToUint8 => {
254- let ensure_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
296+ let strict_checks = if render_args. transpile_opts . strict {
297+ let require_valid_numeric_primitive_fn = Self :: RequireValidNumericPrimitive . name ( ) ;
298+ format ! ( "{require_valid_numeric_primitive_fn}('u8', val);" )
299+ } else {
300+ "" . into ( )
301+ } ;
302+
255303 uwriteln ! (
256304 output,
257305 r#"
258306 function toUint8(val) {{
259- {ensure_valid_numeric_primitive_fn}('u8', val);
307+ {strict_checks}
260308 val >>>= 0;
261309 val %= 2 ** 8;
262310 return val;
0 commit comments