8000 add some new prototypes · dragoncoder047/pickle@71bb7c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71bb7c8

Browse files
add some new prototypes
1 parent 1de0513 commit 71bb7c8

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

pickle.js

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ class PickleObject {
417417
*/
418418
static typeName = "object";
419419
/**
420-
* Create a new Pickle object.
420+
* Create a new Pickle object with the specified prototypes.
421421
* @param {...PickleObject} prototypes
422422
*/
423423
constructor(...prototypes) {
@@ -442,7 +442,7 @@ class PickleObject {
442442
return this.constructor.typeName;
443443
}
444444
/**
445-
* Returns the method resolution order for this object's prototype chain.
445+
* Returns the method resolution order for this object's multiprototype tree.
446446
* @returns {PickleObject[]}
447447
*/
448448
getMRO() {
@@ -507,15 +507,23 @@ function toPickle(it) {
507507
default:
508508
if (it instanceof BigInt) return new PickleInteger(it);
509509
}
510-
throw "can't convert " + typeof it;
510+
throw new PickleError(`can't convert ${typeof it}`);
511511
}
512512

513513
/**
514514
* Create an object with the specified properties.
515-
* @param {{properties: string | number | BigInt | function, operators: [PickleOperator, string | number | BigInt | function][]}} data
516515
* @param {...PickleObject} prototypes
516+
* @param {{properties: string | number | BigInt | function, operators: [PickleOperator, string | number | BigInt | function][]}} data
517517
*/
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();
519527
var o = new PickleObject(...prototypes);
520528
for (var pname of Object.getOwnPropertyNames(data.properties)) {
521529
o.properties.set(pname, toPickle(data.properties[pname]));
@@ -526,7 +534,19 @@ function newPickleObject(data, ...prototypes) {
526534
return o;
527535
}
528536

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, {
530550
operators: [
531551
[new PickleOperator("$", "prefix", 100), function (args, scope) {
532552
var x;
@@ -537,7 +557,10 @@ const PickleSymbolPrototype = newPickleObject({
537557
}],
538558
[new PickleOperator("=", "left", -1), function (args, scope) {
539559
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+
}],
541564
]
542565
});
543566

@@ -564,6 +587,21 @@ class PickleSymbol extends PickleObject {
564587
} 57A6
565588
}
566589

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+
567605
class PickleString extends PickleObject {
568606
static typeName = "string";
569607
/**
@@ -574,7 +612,7 @@ class PickleString extends PickleObject {
574612
* @param {string} str The string content
575613
*/
576614
constructor(str) {
577-
super(FOO);
615+
super(PickleStringPrototype);
578616
if (PickleString._interned.has(str)) return PickleString._interned.get(str);
579617
/**
580618
* @type {string}
@@ -587,7 +625,9 @@ class PickleString extends PickleObject {
587625
}
588626
}
589627

590-
class PickleFloat extends PickleObject {
628+
class PickleScalar extends PickleObject {}
629+
630+
class PickleFloat extends PickleScalar {
591631
static typeName = "float";
592632
/**
593633
* @type {Map<number, PickleFloat>}
@@ -621,7 +661,7 @@ class PickleRational extends PickleObject {
621661
}
622662
}
623663

624-
class PickleInteger extends PickleObject {
664+
class PickleInteger extends PickleScalar {
625665
static typeName = "integer";
626666
/**
627667
* @type {Map<BigInt, PickleInteger>}
@@ -641,6 +681,26 @@ class PickleInteger extends PickleObject {
641681
}
642682
}
643683

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+
644704
class PickleErrorObject extends PickleObject {
645705
static typeName = "error";
646706
/**

0 commit comments

Comments
 (0)
0