Skip to content

Commit

Permalink
Merge pull request #161 from rbuckton/fix-fallback
Browse files Browse the repository at this point in the history
Fix fallback
  • Loading branch information
rbuckton authored Mar 29, 2024
2 parents 6dd6417 + a0a0553 commit 49e57c0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1750,8 +1750,8 @@ namespace Reflect {
const provider: MetadataProvider = {
isProviderFor(O, P) {
let metadataPropertySet = metadataOwner.get(O);
if (!IsUndefined(metadataPropertySet)) {
return metadataPropertySet.has(P);
if (!IsUndefined(metadataPropertySet) && metadataPropertySet.has(P)) {
return true;
}
if (getOwnMetadataKeys(O, P!).length) {
if (IsUndefined(metadataPropertySet)) {
Expand Down
4 changes: 2 additions & 2 deletions ReflectLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1710,8 +1710,8 @@ namespace Reflect {
const provider: MetadataProvider = {
isProviderFor(O, P) {
let metadataPropertySet = metadataOwner.get(O);
if (!IsUndefined(metadataPropertySet)) {
return metadataPropertySet.has(P);
if (!IsUndefined(metadataPropertySet) && metadataPropertySet.has(P)) {
return true;
}
if (getOwnMetadataKeys(O, P!).length) {
if (IsUndefined(metadataPropertySet)) {
Expand Down
4 changes: 2 additions & 2 deletions ReflectNoConflict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1587,8 +1587,8 @@ function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider {
const provider: MetadataProvider = {
isProviderFor(O, P) {
let metadataPropertySet = metadataOwner.get(O);
if (!IsUndefined(metadataPropertySet)) {
return metadataPropertySet.has(P);
if (!IsUndefined(metadataPropertySet) && metadataPropertySet.has(P)) {
return true;
}
if (getOwnMetadataKeys(O, P!).length) {
if (IsUndefined(metadataPropertySet)) {
Expand Down
1 change: 0 additions & 1 deletion test/reflect-definemetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ for (const { name, header, context } of suites) {
describe(name, () => {
describe("Reflect.defineMetadata", () => {
it("InvalidTarget", () => {
debugger;
const { Reflect, TypeError } = script(context)`
${header}
exports.Reflect = Reflect;
Expand Down
60 changes: 60 additions & 0 deletions test/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,64 @@ for (const { name, header, context } of suites.filter(s => s.global && !s.polyfi
});
});
});
}

for (const { name, header, context } of suites) {
describe(`fallback + ${name}`, () => {
it("MetadataRegistry", () => {
const { provider1, provider2 } = script({ ESMap: Map, ...context })`
{
const Map = ESMap;
const map = new Map();
Reflect.defineMetadata = function(key, value, target, prop) {
let props = map.get(target);
if (!props) map.set(target, props = new Map());
let meta = props.get(prop);
if (!meta) props.set(prop, meta = new Map());
meta.set(key, value);
};
Reflect.hasOwnMetadata = function(key, target, prop) {
let props = map.get(target);
let meta = props && props.get(prop);
return meta && meta.has(key) || false;
};
Reflect.getOwnMetadata = function(key, target, prop) {
let props = map.get(target);
let meta = props && props.get(prop);
return meta && meta.get(key);
};
Reflect.getOwnMetadataKeys = function(target, prop) {
let props = map.get(target);
let meta = props && props.get(prop);
const keys = meta && meta.keys() || [];
return [...keys];
};
Reflect.deleteMetadata = function(key, target, prop) {
let props = map.get(target);
let meta = props && props.get(prop);
return meta && meta.delete(key) || false;
};
}
const obj = {};
Reflect.defineMetadata("a", 1, obj);
Reflect.defineMetadata("b", 2, obj, "c");
{
${header}
}
const registrySymbol = Symbol.for("@reflect-metadata:registry");
const registry = Reflect[registrySymbol];
const provider1 = registry.getProvider(obj, undefined);
const provider2 = registry.getProvider(obj, "c");
exports.provider1 = provider1;
exports.provider2 = provider2;
`;
assert.isDefined(provider1);
assert.isDefined(provider2);
assert.strictEqual(provider1, provider2);
});
});
}

0 comments on commit 49e57c0

Please sign in to comment.