@@ -417,7 +417,7 @@ class PickleObject {
417
417
*/
418
418
static typeName = "object" ;
419
419
/**
420
- * Create a new Pickle object.
420
+ * Create a new Pickle object with the specified prototypes .
421
421
* @param {...PickleObject } prototypes
422
422
*/
423
423
constructor ( ...prototypes ) {
@@ -442,7 +442,7 @@ class PickleObject {
442
442
return this . constructor . typeName ;
443
443
}
444
444
/**
445
- * Returns the method resolution order for this object's prototype chain .
445
+ * Returns the method resolution order for this object's multiprototype tree .
446
446
* @returns {PickleObject[] }
447
447
*/
448
448
getMRO ( ) {
@@ -507,15 +507,23 @@ function toPickle(it) {
507
507
default :
508
508
if ( it instanceof BigInt ) return new PickleInteger ( it ) ;
509
509
}
510
- throw " can't convert " + typeof it ;
510
+ throw new PickleError ( ` can't convert ${ typeof it } ` ) ;
511
511
}
512
512
513
513
/**
514
514
* Create an object with the specified properties.
515
- * @param {{properties: string | number | BigInt | function, operators: [PickleOperator, string | number | BigInt | function][]} } data
516
515
* @param {...PickleObject } prototypes
516
+ * @param {{properties: string | number | BigInt | function, operators: [PickleOperator, string | number | BigInt | function][]} } data
517
517
*/
518
- function newPickleObject ( data , ...prototypes ) {
518
+ function newPickleObject ( ) {
519
+ /**
520
+ * @type {PickleObject[] }
521
+ */
522
+ var prototypes = [ ] . slice . call ( arguments ) ;
523
+ /**
524
+ * @type {{properties: string | number | BigInt | function, operators: [PickleOperator, string | number | BigInt | function][]} }
525
+ */
526
+ var data = prototypes . pop ( ) ;
519
527
var o = new PickleObject ( ...prototypes ) ;
520
528
for ( var pname of Object . getOwnPropertyNames ( data . properties ) ) {
521
529
o . properties . set ( pname , toPickle ( data . properties [ pname ] ) ) ;
@@ -526,7 +534,19 @@ function newPickleObject(data, ...prototypes) {
526
534
return o ;
527
535
}
528
536
529
- const PickleSymbolPrototype = newPickleObject ( {
537
+ const PickleObjectPrototype = newPickleObject ( {
538
+ operators : [
539
+ [ new PickleOperator ( "." , "left" , 100 ) , function ( args , scope ) {
540
+ var x , prop = args . get ( 0 ) ;
541
+ for ( var p of this . getMRO ( ) )
542
+ if ( ( x = p . properties . find ( prop ) ) )
543
+ return new PickleBoundProperty ( this , x ) ;
544
+ throw new PickleError ( `no ${ p . sym } in ${ this . typeName } object` ) ;
545
+ } ]
546
+ ]
547
+ } ) ;
548
+
549
+ const PickleSymbolPrototype = newPickleObject ( PickleObjectPrototype , {
530
550
operators : [
531
551
[ new PickleOperator ( "$" , "prefix" , 100 ) , function ( args , scope ) {
532
552
var x ;
@@ -537,7 +557,10 @@ const PickleSymbolPrototype = newPickleObject({
537
557
} ] ,
538
558
[ new PickleOperator ( "=" , "left" , - 1 ) , function ( args , scope ) {
539
559
return new PicklePropertySetter ( this , args . get ( 0 ) ) ;
540
- } ]
560
+ } ] ,
561
+ [ new PickleOperator ( "==" , "left" , 2 ) , function ( args , scope ) {
562
+ return new PickleBoolean ( this === args . get ( 0 ) ) ;
563
+ } ] ,
541
564
]
542
565
} ) ;
543
566
@@ -564,6 +587,21 @@ class PickleSymbol extends PickleObject {
564
587
}
57A6
565
588
}
566
589
590
+ const PickleStringPrototype = newPickleObject ( PickleObjectPrototype , {
591
+ operators : [
592
+ [ new PickleOperator ( "+" , "left" , 100 ) , function ( args , scope ) {
593
+ var other = args . get ( 0 ) ;
594
+ if ( ! ( other instanceof PickleString ) ) throw new PickleError ( `can't add string to ${ other . typeName } ` ) ;
595
+ return new PickleString ( this . str + other . str ) ;
596
+ } ]
597
+ [ new PickleOperator ( "*" , "left" , 100 ) , function ( args , scope ) {
598
+ var other = args . get ( 0 ) ;
599
+ if ( ! ( other instanceof PickleScalar ) ) throw new PickleError ( `can't repeat string by ${ other . typeName } ` ) ;
600
+ return new PickleString ( this . str . repeat ( other . num ) ) ;
601
+ } ]
602
+ ]
603
+ } ) ;
604
+
567
605
class PickleString extends PickleObject {
568
606
static typeName = "string" ;
569
607
/**
@@ -574,7 +612,7 @@ class PickleString extends PickleObject {
574
612
* @param {string } str The string content
575
613
*/
576
614
constructor ( str ) {
577
- super ( FOO ) ;
615
+ super ( PickleStringPrototype ) ;
578
616
if ( PickleString . _interned . has ( str ) ) return PickleString . _interned . get ( str ) ;
579
617
/**
580
618
* @type {string }
@@ -587,7 +625,9 @@ class PickleString extends PickleObject {
587
625
}
588
626
}
589
627
590
- class PickleFloat extends PickleObject {
628
+ class PickleScalar extends PickleObject { }
629
+
630
+ class PickleFloat extends PickleScalar {
591
631
static typeName = "float" ;
592
632
/**
593
633
* @type {Map<number, PickleFloat> }
@@ -621,7 +661,7 @@ class PickleRational extends PickleObject {
621
661
}
622
662
}
623
663
624
- class PickleInteger extends PickleObject {
664
+ class PickleInteger extends PickleScalar {
625
665
static typeName = "integer" ;
626
666
/**
627
667
* @type {Map<BigInt, PickleInteger> }
@@ -641,6 +681,26 @@ class PickleInteger extends PickleObject {
641
681
}
642
682
}
643
683
684
+ class PickleBoolean extends PickleInteger {
685
+ static typeName = "boolean" ;
686
+ /**
687
+ * @type {Map<boolean, PickleBoolean> }
688
+ */
689
+ static _interned = new Map ( ) ;
690
+ /**
691
+ * @param {boolean } num The number
692
+ */
693
+ constructor ( b ) {
694
+ super ( FOO ) ;
695
+ if ( PickleBoolean . _interned . has ( num ) ) return PickleBoolean . _interned . get ( num ) ;
696
+ /**
697
+ * @type {boolean }
698
+ */
699
+ this . b = b ;
700
+ PickleBoolean . _interned . set ( b , this ) ;
701
+ }
702
+ }
703
+
644
704
class PickleErrorObject extends PickleObject {
645
705
static typeName = "error" ;
646
706
/**
0 commit comments