@@ -17,7 +17,7 @@ class IntType {
1717 customId = "jwInt"
1818
1919 constructor ( x = 0n ) {
20- this . number = x instanceof BigInt ? x : BigInt ( x ) ;
20+ this . number = x ;
2121 }
2222
2323 static toInt ( x ) {
@@ -107,7 +107,7 @@ class Extension {
107107 vm . runtime . registerSerializer (
108108 "jwInt" ,
109109 v => v . number . toString ( ) ,
110- v => new IntType ( v )
110+ v => new IntType ( BigInt ( v ) )
111111 ) ;
112112 }
113113
@@ -273,20 +273,43 @@ class Extension {
273273 } ,
274274 ...jwInt . Block
275275 } ,
276+ {
277+ opcode : "not" ,
278+ text : "~ [INT]" ,
279+ arguments : {
280+ INT : jwInt . Argument
281+ } ,
282+ ...jwInt . Block
283+ } ,
276284 "---" ,
277285 {
278286 opcode : "mathop" ,
279287 text : "[MATHOP] of [INPUT]" ,
280288 arguments : {
281289 MATHOP : {
282- type : ArgumentType . MENU ,
283290 defaultValue : "abs" ,
284291 menu : "mathop"
285292 } ,
286293 INPUT : jwInt . Argument
287294 } ,
288295 ...jwInt . Block
289296 } ,
297+ {
298+ opcode : "truncate" ,
299+ text : "truncate [INT] to [BITS] bits [SIGN]" ,
300+ arguments : {
301+ INT : jwInt . Argument ,
302+ BITS : {
303+ type : ArgumentType . NUMBER ,
304+ defaultValue : 64
305+ } ,
306+ SIGN : {
307+ defaultValue : "signed" ,
308+ menu : "sign"
309+ }
310+ } ,
311+ ...jwInt . Block
312+ } ,
290313 "---" ,
291314 {
292315 opcode : "stringify" ,
@@ -295,7 +318,6 @@ class Extension {
295318 arguments : {
296319 INPUT : jwInt . Argument ,
297320 FORMAT : {
298- type : ArgumentType . MENU ,
299321 defaultValue : "default" ,
300322 menu : "stringifyFormat"
301323 }
@@ -311,6 +333,13 @@ class Extension {
311333 "sign"
312334 ]
313335 } ,
336+ sign : {
337+ acceptReporters : false ,
338+ items : [
339+ "signed" ,
340+ "unsigned"
341+ ]
342+ } ,
314343 stringifyFormat : {
315344 acceptReporters : false ,
316345 items : [
@@ -432,23 +461,46 @@ class Extension {
432461 return new jwInt . Type ( A . number >> B . number )
433462 }
434463
464+ not ( { INT } ) {
465+ INT = jwInt . Type . toInt ( INT ) ;
466+ return new jwInt . Type ( ~ INT . number ) ;
467+ }
468+
435469 mathop ( { MATHOP , INPUT } ) {
436470 INPUT = jwInt . Type . toInt ( INPUT ) ;
437471 switch ( MATHOP ) {
438472 case "abs" :
439473 return new jwInt . Type ( INPUT . number > 0n ? INPUT . number : - INPUT . number ) ;
440474 case 'log' :
441- if ( INPUT . number < 1n ) return new jwInt . Type ( - 1 ) ;
475+ if ( INPUT . number < 1n ) return new jwInt . Type ( - 1n ) ;
442476 else return new jwInt . Type ( INPUT . number . toString ( ) . length - 1 ) ;
443477 case "sign" :
444- if ( INPUT . number === 0n ) return new jwInt . Type ( 0 ) ;
445- else if ( INPUT . number > 0n ) return new jwInt . Type ( 1 ) ;
446- else return new jwInt . Type ( - 1 ) ;
478+ if ( INPUT . number === 0n ) return new jwInt . Type ( ) ;
479+ else if ( INPUT . number > 0n ) return new jwInt . Type ( 1n ) ;
480+ else return new jwInt . Type ( - 1n ) ;
447481 default :
448482 return INPUT ;
449483 }
450484 }
451485
486+ truncate ( { INT , BITS , SIGN } ) {
487+ INT = jwInt . Type . toInt ( INT ) ;
488+ BITS = Cast . toNumber ( BITS ) ;
489+
490+ if ( BITS < 0 ) return new jwInt . Type ( ) ;
491+
492+ try {
493+ switch ( SIGN ) {
494+ case "signed" :
495+ return new jwInt . Type ( BigInt . asIntN ( BITS , INT . number ) ) ;
496+ case "unsigned" :
497+ return new jwInt . Type ( BigInt . asUintN ( BITS , INT . number ) ) ;
498+ }
499+ } catch ( e ) { }
500+
501+ return INT ;
502+ }
503+
452504 stringify ( { INPUT , FORMAT } ) {
453505 INPUT = jwInt . Type . toInt ( INPUT )
454506 switch ( FORMAT ) {
0 commit comments