-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[experimental] play with metadata reflection api, #152
- Loading branch information
Showing
28 changed files
with
404 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var Map = require('./es6.map') | ||
, $export = require('./_export') | ||
, shared = require('./_shared')('metadata') | ||
, store = shared.store || (shared.store = new (require('./es6.weak-map'))); | ||
|
||
var getOrCreateMetadataMap = function(target, targetKey, create){ | ||
var targetMetadata = store.get(target); | ||
if(!targetMetadata){ | ||
if(!create)return undefined; | ||
targetMetadata = new Map(); | ||
store.set(target, targetMetadata); | ||
} | ||
var keyMetadata = targetMetadata.get(targetKey); | ||
if(!keyMetadata){ | ||
if(!create)return undefined; | ||
keyMetadata = new Map(); | ||
targetMetadata.set(targetKey, keyMetadata); | ||
} return keyMetadata; | ||
}; | ||
var ordinaryHasOwnMetadata = function(MetadataKey, O, P){ | ||
var metadataMap = getOrCreateMetadataMap(O, P, false); | ||
return metadataMap === undefined ? false : metadataMap.has(MetadataKey); | ||
}; | ||
var ordinaryGetOwnMetadata = function(MetadataKey, O, P){ | ||
var metadataMap = getOrCreateMetadataMap(O, P, false); | ||
return metadataMap === undefined ? undefined : metadataMap.get(MetadataKey); | ||
}; | ||
var ordinaryDefineOwnMetadata = function(MetadataKey, MetadataValue, O, P){ | ||
getOrCreateMetadataMap(O, P, true).set(MetadataKey, MetadataValue); | ||
}; | ||
var ordinaryOwnMetadataKeys = function(target, targetKey) { | ||
var metadataMap = getOrCreateMetadataMap(target, targetKey, false); | ||
var keys = []; | ||
if(metadataMap)metadataMap.forEach(function(_, key){ keys.push(key); }); | ||
return keys; | ||
}; | ||
var toMetaKey = function(it) { | ||
return it === undefined || typeof it == 'symbol' ? it : String(it); | ||
}; | ||
var exp = function(O){ | ||
$export($export.S, 'Reflect', O); | ||
}; | ||
|
||
module.exports = { | ||
store: store, | ||
map: getOrCreateMetadataMap, | ||
has: ordinaryHasOwnMetadata, | ||
get: ordinaryGetOwnMetadata, | ||
set: ordinaryDefineOwnMetadata, | ||
keys: ordinaryOwnMetadataKeys, | ||
key: toMetaKey, | ||
exp: exp | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var metadata = require('./_metadata') | ||
, anObject = require('./_an-object') | ||
, toMetaKey = metadata.key | ||
, ordinaryDefineOwnMetadata = metadata.set; | ||
|
||
metadata.exp({defineMetadata: function defineMetadata(metadataKey, metadataValue, target, targetKey){ | ||
return ordinaryDefineOwnMetadata(metadataKey, metadataValue, anObject(target), toMetaKey(targetKey)); | ||
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var metadata = require('./_metadata') | ||
, anObject = require('./_an-object') | ||
, toPropertyKey = metadata.key | ||
, getOrCreateMetadataMap = metadata.map | ||
, store = metadata.store; | ||
|
||
metadata.exp({deleteMetadata: function deleteMetadata(metadataKey, target, targetKey){ | ||
anObject(target); | ||
if(targetKey !== undefined)targetKey = toPropertyKey(targetKey); | ||
var metadataMap = getOrCreateMetadataMap(target, targetKey, false); | ||
if(metadataMap === undefined || !metadataMap['delete'](metadataKey))return false; | ||
if(metadataMap.size)return true; | ||
var targetMetadata = store.get(target); | ||
targetMetadata['delete'](targetKey); | ||
if(targetMetadata.size)return true; | ||
store['delete'](target); | ||
return true; | ||
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var Set = require('./es6.set') | ||
, metadata = require('./_metadata') | ||
, anObject = require('./_an-object') | ||
, getPrototypeOf = require('./_').getProto | ||
, ordinaryOwnMetadataKeys = metadata.keys | ||
, toMetaKey = metadata.key; | ||
|
||
var ordinaryMetadataKeys = function(O, P){ | ||
var ownKeys = ordinaryOwnMetadataKeys(O, P); | ||
var parent = getPrototypeOf(O); | ||
if(parent === null)return ownKeys; | ||
var parentKeys = ordinaryMetadataKeys(parent, P); | ||
if(parentKeys.length <= 0)return ownKeys; | ||
if(ownKeys.length <= 0)return parentKeys; | ||
var set = new Set(); | ||
var keys = []; | ||
for(var i = 0; i < ownKeys.length; i++){ | ||
var key = ownKeys[i]; | ||
var hasKey = set.has(key); | ||
if(!hasKey){ | ||
set.add(key); | ||
keys.push(key); | ||
} | ||
} | ||
for(var j = 0; j < parentKeys.length; j++){ | ||
var key = parentKeys[j]; | ||
var hasKey = set.has(key); | ||
if(!hasKey){ | ||
set.add(key); | ||
keys.push(key); | ||
} | ||
} return keys; | ||
}; | ||
|
||
metadata.exp({getMetadataKeys: function getMetadataKeys(target, targetKey){ | ||
return ordinaryMetadataKeys(anObject(target), toMetaKey(targetKey)); | ||
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var metadata = require('./_metadata') | ||
, anObject = require('./_an-object') | ||
, getPrototypeOf = require('./_').getProto | ||
, ordinaryHasOwnMetadata = metadata.has | ||
, ordinaryGetOwnMetadata = metadata.get | ||
, toMetaKey = metadata.key; | ||
|
||
var ordinaryGetMetadata = function(MetadataKey, O, P){ | ||
var hasOwn = ordinaryHasOwnMetadata(MetadataKey, O, P); | ||
if(hasOwn)return ordinaryGetOwnMetadata(MetadataKey, O, P); | ||
var parent = getPrototypeOf(O); | ||
return parent !== null ? ordinaryGetMetadata(MetadataKey, parent, P) : undefined; | ||
}; | ||
|
||
metadata.exp({getMetadata: function getMetadata(metadataKey, target, targetKey){ | ||
return ordinaryGetMetadata(metadataKey, anObject(target), toMetaKey(targetKey)); | ||
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var metadata = require('./_metadata') | ||
, anObject = require('./_an-object') | ||
, ordinaryGetOwnMetadata = metadata.get | ||
, toMetaKey = metadata.key; | ||
|
||
metadata.exp({getOwnMetadata: function getOwnMetadata(metadataKey, target, targetKey){ | ||
return ordinaryGetOwnMetadata(metadataKey, anObject(target), toMetaKey(targetKey)); | ||
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var metadata = require('./_metadata') | ||
, anObject = require('./_an-object') | ||
, ordinaryOwnMetadataKeys = metadata.keys | ||
, toMetaKey = metadata.key; | ||
|
||
metadata.exp({getOwnMetadataKeys: function getOwnMetadataKeys(target, targetKey){ | ||
return ordinaryOwnMetadataKeys(anObject(target), toMetaKey(targetKey)); | ||
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var metadata = require('./_metadata') | ||
, anObject = require('./_an-object') | ||
, getPrototypeOf = require('./_').getProto | ||
, ordinaryHasOwnMetadata = metadata.has | ||
, toMetaKey = metadata.key; | ||
|
||
var ordinaryHasMetadata = function(MetadataKey, O, P){ | ||
var hasOwn = ordinaryHasOwnMetadata(MetadataKey, O, P); | ||
if(hasOwn)return true; | ||
var parent = getPrototypeOf(O); | ||
return parent !== null ? ordinaryHasMetadata(MetadataKey, parent, P) : false; | ||
}; | ||
|
||
metadata.exp({hasMetadata: function hasMetadata(metadataKey, target, targetKey){ | ||
return ordinaryHasMetadata(metadataKey, anObject(target), toMetaKey(targetKey)); | ||
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var metadata = require('./_metadata') | ||
, anObject = require('./_an-object') | ||
, ordinaryHasOwnMetadata = metadata.has | ||
, toMetaKey = metadata.key; | ||
|
||
metadata.exp({hasOwnMetadata: function hasOwnMetadata(metadataKey, target, targetKey){ | ||
return ordinaryHasOwnMetadata(metadataKey, anObject(target), toMetaKey(targetKey)); | ||
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var metadata = require('./_metadata') | ||
, anObject = require('./_an-object') | ||
, aFunction = require('./_a-function') | ||
, toMetaKey = metadata.key | ||
, ordinaryDefineOwnMetadata = metadata.set; | ||
|
||
metadata.exp({metadata: function metadata(metadataKey, metadataValue) { | ||
return function decorator(target, targetKey){ | ||
ordinaryDefineOwnMetadata( | ||
metadataKey, metadataValue, | ||
(targetKey !== undefined ? anObject : aFunction)(target), | ||
toMetaKey(targetKey) | ||
); | ||
}; | ||
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var Map = require('./es6.map') | ||
, $export = require('./_export') | ||
, shared = require('./_shared')('metadata') | ||
, store = shared.store || (shared.store = new (require('./es6.weak-map'))); | ||
|
||
var getOrCreateMetadataMap = function(target, targetKey, create){ | ||
var targetMetadata = store.get(target); | ||
if(!targetMetadata){ | ||
if(!create)return undefined; | ||
targetMetadata = new Map(); | ||
store.set(target, targetMetadata); | ||
} | ||
var keyMetadata = targetMetadata.get(targetKey); | ||
if(!keyMetadata){ | ||
if(!create)return undefined; | ||
keyMetadata = new Map(); | ||
targetMetadata.set(targetKey, keyMetadata); | ||
} return keyMetadata; | ||
}; | ||
var ordinaryHasOwnMetadata = function(MetadataKey, O, P){ | ||
var metadataMap = getOrCreateMetadataMap(O, P, false); | ||
return metadataMap === undefined ? false : metadataMap.has(MetadataKey); | ||
}; | ||
var ordinaryGetOwnMetadata = function(MetadataKey, O, P){ | ||
var metadataMap = getOrCreateMetadataMap(O, P, false); | ||
return metadataMap === undefined ? undefined : metadataMap.get(MetadataKey); | ||
}; | ||
var ordinaryDefineOwnMetadata = function(MetadataKey, MetadataValue, O, P){ | ||
getOrCreateMetadataMap(O, P, true).set(MetadataKey, MetadataValue); | ||
}; | ||
var ordinaryOwnMetadataKeys = function(target, targetKey) { | ||
var metadataMap = getOrCreateMetadataMap(target, targetKey, false); | ||
var keys = []; | ||
if(metadataMap)metadataMap.forEach(function(_, key){ keys.push(key); }); | ||
return keys; | ||
}; | ||
var toMetaKey = function(it) { | ||
return it === undefined || typeof it == 'symbol' ? it : String(it); | ||
}; | ||
var exp = function(O){ | ||
$export($export.S, 'Reflect', O); | ||
}; | ||
|
||
module.exports = { | ||
store: store, | ||
map: getOrCreateMetadataMap, | ||
has: ordinaryHasOwnMetadata, | ||
get: ordinaryGetOwnMetadata, | ||
set: ordinaryDefineOwnMetadata, | ||
keys: ordinaryOwnMetadataKeys, | ||
key: toMetaKey, | ||
exp: exp | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.