Skip to content

Commit

Permalink
Merge pull request #26 from wentout/proto
Browse files Browse the repository at this point in the history
updates
  • Loading branch information
wentout committed Mar 21, 2023
2 parents 98f1059 + de6d13a commit c608a7a
Show file tree
Hide file tree
Showing 12 changed files with 649 additions and 564 deletions.
3 changes: 2 additions & 1 deletion lib/fields.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export declare const SymbolInitialValue: unique symbol;
declare const SymbolInitialValue: unique symbol;
interface FieldDefinition {
[SymbolInitialValue]: unknown;
}
Expand All @@ -7,5 +7,6 @@ export declare class FieldConstructor implements FieldDefinition {
get get(): (this: FieldDefinition) => unknown;
get set(): () => never;
constructor(value: unknown);
static get SymbolInitialValue(): symbol;
}
export {};
15 changes: 9 additions & 6 deletions lib/fields.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldConstructor = exports.SymbolInitialValue = void 0;
exports.SymbolInitialValue = Symbol('Initial Value');
exports.FieldConstructor = void 0;
const errors_1 = require("./errors");
const SymbolInitialValue = Symbol('Initial Value');
class FieldConstructor {
constructor(value) {
this[exports.SymbolInitialValue] = value;
}
get get() {
const self = this;
return function () {
return self[exports.SymbolInitialValue];
return self[SymbolInitialValue];
};
}
get set() {
return function () {
throw new TypeError(errors_1.ErrorsNames.FORBIDDEN_RE);
};
}
constructor(value) {
this[SymbolInitialValue] = value;
}
static get SymbolInitialValue() {
return SymbolInitialValue;
}
}
exports.FieldConstructor = FieldConstructor;
Object.freeze(FieldConstructor.prototype);
Expand Down
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare const BaseConstructor: ObjectConstructor;
export declare class BaseClass extends BaseConstructor {
}
export declare type IDEF<T, P = {}, R = {}> = {
export type IDEF<T, P = {}, R = {}> = {
new (...args: unknown[]): T;
(this: T, ...args: unknown[]): R;
prototype: P;
Expand Down
21 changes: 9 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ const resolver = Object.entries({
if (invocationThis !== receiver) {
throw new ReferenceError(errors_1.ErrorsNames.ACCESS_DENIED);
}
return handler.get();
const result = handler.get();
return result;
},
set(replacementValue) {
const invocationThis = this;
if (invocationThis !== receiver) {
throw new ReferenceError(errors_1.ErrorsNames.ACCESS_DENIED);
}
return handler.set(replacementValue);
const result = handler.set(replacementValue);
return result;
}
};
};
Expand All @@ -42,13 +44,8 @@ const createProperty = (propName, initialValue, receiver) => {
const descriptor = (isObject && (value instanceof fields_1.FieldConstructor)) ?
value
: Object.assign({ enumerable: true }, resolver[type](value, receiver));
try {
const result = Reflect.defineProperty(receiver, propName, descriptor);
return result;
}
catch (error) {
throw error;
}
const result = Reflect.defineProperty(receiver, propName, descriptor);
return result;
};
const props2skip = new Set([Symbol.toStringTag, Symbol.iterator]);
const util = require('util');
Expand Down Expand Up @@ -81,11 +78,11 @@ const handlers = {
const BaseTarget = Object.create(null);
const BaseConstructor = function (InstanceTarget = BaseTarget) {
if (!new.target) {
const constructor = BaseConstructor.bind(this, InstanceTarget);
constructor.prototype = {
const self = BaseConstructor.bind(this, InstanceTarget);
self.prototype = {
constructor: BaseConstructor
};
return constructor;
return self;
}
const InstancePrototype = new Proxy(InstanceTarget, handlers);
let protoPointer = this;
Expand Down
3 changes: 2 additions & 1 deletion lib/types/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const primitives = (initialValue) => {
if (value[prop] instanceof Function) {
return value[prop].bind(value);
}
return value[prop];
const answer = value[prop];
return answer;
}
});
return proxyAsValue;
Expand Down

0 comments on commit c608a7a

Please sign in to comment.