This commit is contained in:
crusader 2017-12-23 21:14:08 +09:00
parent 8fe2823519
commit 0db3f72fae
4 changed files with 84 additions and 10 deletions

View File

@ -5,10 +5,13 @@ import {
} from '@overflow/commons/core/type'; } from '@overflow/commons/core/type';
import {AccessibleObject} from './accessible_object'; import {AccessibleObject} from './accessible_object';
import {systemClassRegistry} from './class_registry';
import {Constructor} from './constructor'; import {Constructor} from './constructor';
import {Field} from './field'; import {Field} from './field';
import {Method} from './method'; import {Method} from './method';
export class Class extends AccessibleObject { export class Class extends AccessibleObject {
private _clazzType: ClassType; private _clazzType: ClassType;
private _constructor: Constructor; private _constructor: Constructor;
@ -19,17 +22,17 @@ export class Class extends AccessibleObject {
* forClass * forClass
*/ */
public static forClass(clazzType: ClassType): Class { public static forClass(clazzType: ClassType): Class {
return Metadata.get(LOAFER_CLASS, clazzType); return systemClassRegistry.get(clazzType);
} }
/** /**
* _defineClass * _defineClass
*/ */
public static _defineClass(clazzType: ClassType): Class { public static _defineClass(clazzType: ClassType): Class {
let clazz: Class = Metadata.get(LOAFER_CLASS, clazzType); let clazz: Class = Class.forClass(clazzType);
if (undefined === clazz) { if (undefined === clazz) {
clazz = new Class(clazzType); clazz = new Class(clazzType);
Metadata.set(LOAFER_CLASS, clazz, clazzType); systemClassRegistry.set(clazzType, clazz);
} }
if (null === clazz._constructor) { if (null === clazz._constructor) {
@ -126,10 +129,3 @@ export class Class extends AccessibleObject {
return null; return null;
} }
} }
/**
* Metadata key
* @private
* @type {string}
*/
const LOAFER_CLASS = Symbol('loafer:class');

View File

@ -0,0 +1,11 @@
import { Registry } from '@overflow/commons/core/util';
import { ClassType } from '@overflow/commons/core/type';
import { Class } from './class';
export class ClassRegistry extends Registry<ClassType, Class> {
public constructor(parent?: ClassRegistry) {
super(parent);
}
}
export const systemClassRegistry = new ClassRegistry();

View File

@ -0,0 +1 @@
export * from './registry';

View File

@ -0,0 +1,66 @@
export abstract class Registry<K, V> {
private _parent: Registry<K, V>;
private _map: Map<K, V>;
protected constructor(parent?: Registry<K, V>) {
this._parent = parent;
this._map = new Map<K, V>();
}
public get parent(): Registry<K, V> {
return this._parent;
}
public get size(): number {
return this._map.size;
}
public get(key: K): V | undefined {
let v = this._map.get(key);
if (undefined === v && undefined !== this._parent) {
v = this._parent.get(key);
}
return v;
}
public has(key: K): boolean {
let exist = this._map.has(key);
if (!exist && undefined !== this._parent) {
exist = this._parent.has(key);
}
return exist;
}
public set(key: K, value: V): void {
this._map.set(key, value);
}
public entries(): IterableIterator<[K, V]> {
return this._map.entries();
}
public keys(): IterableIterator<K> {
return this._map.keys();
}
public values(): IterableIterator<V> {
return this._map.values();
}
public clear(): void {
this._map.clear();
}
public delete(key: K): boolean {
let result = this._map.delete(key);
if (!result && undefined !== this._parent) {
result = this._parent.delete(key);
}
return result;
}
public forEach(callback: (vlaue: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
this._map.forEach(callback, thisArg);
}
}