cfprotected 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.mjs +26 -16
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -39,6 +39,8 @@ function bindDescriptor(desc, context) {
39
39
  * @returns {Object} The fully constructed inheritance object.
40
40
  */
41
41
  function share(inst, klass, members) {
42
+ let retval = {};
43
+
42
44
  if ((typeof(inst) == "function")
43
45
  && klass && (typeof(klass) == "object")
44
46
  && (members === void 0)) {
@@ -67,7 +69,7 @@ function share(inst, klass, members) {
67
69
  * }
68
70
  */
69
71
 
70
- //Find the nearest known registered ancestor
72
+ //Find the nearest known registered ancestor class
71
73
  let ancestor = Object.getPrototypeOf(klass);
72
74
  while (ancestor && !memos.has(ancestor)) {
73
75
  ancestor = Object.getPrototypeOf(ancestor);
@@ -76,25 +78,22 @@ function share(inst, klass, members) {
76
78
  //Get the memo from that ancestor
77
79
  let ancestorMemo = memos.get(ancestor) || new WeakMap();
78
80
 
79
- //Create a memo for the current class
81
+ //Create a memo map for the current class
80
82
  if (!memos.has(klass)) {
81
83
  memos.set(klass, new WeakMap());
82
84
  }
83
85
 
84
86
  //Get the protected data object.
85
87
  let ancestorKey = (inst === klass) ? ancestor : inst;
86
- let memo = ancestorMemo.get(ancestorKey) || {
87
- data: {},
88
- $uper: {},
89
- };
90
- let retval = {};
88
+ let memo = ancestorMemo.get(ancestorKey) || {data: {}, $uper: {}, inheritance: null};
89
+ let protData = memo.data;
91
90
 
92
91
  //Get the details of the protected properties.
93
92
  let mDesc = Object.getOwnPropertyDescriptors(members);
94
93
  let mKeys = getAllOwnKeys(members);
95
94
 
96
- //Change the prototype of protoData using the new members.
97
- let prototype = Object.getPrototypeOf(memo.data);
95
+ //Add the new members to the prototype chain of protData.
96
+ let prototype = Object.getPrototypeOf(protData);
98
97
  let proto = Object.create(prototype,
99
98
  Object.fromEntries(mKeys
100
99
  .map(k => {
@@ -108,11 +107,18 @@ function share(inst, klass, members) {
108
107
  bindDescriptor(mDesc[k], inst);
109
108
  return [k, mDesc[k]];
110
109
  })));
111
- Object.setPrototypeOf(retval, proto);
110
+ Object.setPrototypeOf(protData, proto);
112
111
 
112
+ //Build the accessors for this class.
113
+ mKeys.forEach(m => {
114
+ Object.defineProperty(retval, m, {
115
+ get() { return protData[m]; },
116
+ set(v) { protData[m] = v; }
117
+ });
118
+ });
119
+
113
120
  //Define the "$uper" accessors
114
- let $uper = {};
115
- Object.defineProperty(retval, "$uper", { value: $uper });
121
+ Object.defineProperty(retval, "$uper", { value: {} });
116
122
 
117
123
  //Build up the "$uper" object
118
124
  for (let key of mKeys) {
@@ -121,17 +127,21 @@ function share(inst, klass, members) {
121
127
  while (!obj.hasOwnProperty(key)) {
122
128
  obj = Object.getPrototypeOf(obj);
123
129
  }
124
- Object.defineProperty($uper, key, Object.getOwnPropertyDescriptor(obj, key));
130
+ Object.defineProperty(retval.$uper, key, Object.getOwnPropertyDescriptor(obj, key));
125
131
  }
126
132
  }
127
133
 
128
134
  //Attach the super inheritance
129
- Object.setPrototypeOf($uper, memo.$uper);
135
+ Object.setPrototypeOf(retval.$uper, memo.$uper);
136
+
137
+ //Inherit the inheritance
138
+ Object.setPrototypeOf(retval, memo.inheritance);
130
139
 
131
140
  //Save the inheritance & protected data
132
141
  memos.get(klass).set(inst, {
133
- data: retval,
134
- $uper: $uper
142
+ data: protData,
143
+ inheritance: retval,
144
+ $uper: retval.$uper
135
145
  });
136
146
 
137
147
  return retval;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cfprotected",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "An implementation of protected fields on top of class fields.",
5
5
  "main": "index.js",
6
6
  "scripts": {