Skip to content

Commit 31dde5f

Browse files
committedDec 13, 2023
Add shared registry for use when multiple versions are loaded
1 parent 6227ddf commit 31dde5f

10 files changed

+1011
-241
lines changed
 

‎Reflect.ts

+285-69
Large diffs are not rendered by default.

‎ReflectLite.ts

+283-69
Large diffs are not rendered by default.

‎ReflectNoConflict.ts

+311-92
Large diffs are not rendered by default.

‎globals.d.ts

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and limitations under the License.
1515

1616
interface SymbolConstructor {
1717
(description?: string): symbol;
18+
for(key: string): symbol;
1819
readonly iterator: symbol;
1920
readonly toPrimitive: symbol;
2021
}
@@ -89,3 +90,20 @@ interface WeakMapConstructor {
8990
declare var Map: MapConstructor;
9091
declare var Set: SetConstructor;
9192
declare var WeakMap: WeakMapConstructor;
93+
94+
// NOTE: These are not actually global, just shared between the Reflect*.ts variants
95+
96+
interface MetadataRegistry {
97+
registerProvider(provider: MetadataProvider): void;
98+
getProvider(O: object, P: string | symbol | undefined): MetadataProvider | undefined;
99+
setProvider(O: object, P: string | symbol | undefined, provider: MetadataProvider): boolean;
100+
}
101+
102+
interface MetadataProvider {
103+
isProviderFor(O: object, P: string | symbol | undefined): boolean;
104+
OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: object, P: string | symbol | undefined): void;
105+
OrdinaryDeleteMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean;
106+
OrdinaryHasOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean;
107+
OrdinaryGetOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): any;
108+
OrdinaryOwnMetadataKeys(O: object, P: string | symbol | undefined): any[];
109+
}

‎gulpfile.js

+43-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const gls = require("gulp-live-server");
99

1010
const debugProject = tsb.create("tsconfig.json");
1111
const releaseProject = tsb.create("tsconfig-release.json");
12-
const tests = tsb.create("test/tsconfig.json");
12+
const tests = {
13+
full: tsb.create("test/full/tsconfig.json"),
14+
lite: tsb.create("test/lite/tsconfig.json"),
15+
"no-conflict": tsb.create("test/no-conflict/tsconfig.json"),
16+
registry: tsb.create("test/registry/tsconfig.json"),
17+
};
1318

1419
let project = debugProject;
1520

@@ -30,10 +35,32 @@ gulp.task("build:reflect", () => gulp
3035
.pipe(project())
3136
.pipe(gulp.dest(".")));
3237

33-
gulp.task("build:tests", ["build:reflect"], () => gulp
34-
.src(["test/**/*.ts"])
35-
.pipe(tests())
36-
.pipe(gulp.dest("test")));
38+
gulp.task("build:tests:full", ["build:reflect"], () => gulp
39+
.src(["test/full/**/*.ts"])
40+
.pipe(tests.full())
41+
.pipe(gulp.dest("test/full")));
42+
43+
gulp.task("build:tests:lite", ["build:reflect"], () => gulp
44+
.src(["test/lite/**/*.ts"])
45+
.pipe(tests.lite())
46+
.pipe(gulp.dest("test/lite")));
47+
48+
gulp.task("build:tests:no-conflict", ["build:reflect"], () => gulp
49+
.src(["test/no-conflict/**/*.ts"])
50+
.pipe(tests["no-conflict"]())
51+
.pipe(gulp.dest("test/no-conflict")));
52+
53+
gulp.task("build:tests:registry", ["build:reflect"], () => gulp
54+
.src(["test/registry/**/*.ts"])
55+
.pipe(tests.registry())
56+
.pipe(gulp.dest("test/registry")));
57+
58+
gulp.task("build:tests", [
59+
"build:tests:full",
60+
"build:tests:lite",
61+
"build:tests:no-conflict",
62+
"build:tests:registry"
63+
]);
3764

3865
gulp.task("build:spec", () => gulp
3966
.src(["spec.html"])
@@ -55,31 +82,37 @@ gulp.task("use-polyfill", () => {
5582
process.env["REFLECT_METADATA_USE_MAP_POLYFILL"] = "true";
5683
});
5784

58-
gulp.task("test:full", ["build:tests", "no-polyfill"], () => {
85+
gulp.task("test:full", ["build:tests:full", "no-polyfill"], () => {
5986
console.log("Running tests w/o polyfill...");
6087
return gulp
6188
.src(["test/full/**/*.js"], { read: false })
6289
.pipe(mocha({ reporter: "dot" }));
6390
});
64-
gulp.task("test:lite", ["build:tests", "no-polyfill"], () => {
91+
gulp.task("test:lite", ["build:tests:lite", "no-polyfill"], () => {
6592
console.log("Running lite-mode tests w/o polyfill...");
6693
return gulp
6794
.src(["test/lite/**/*.js"], { read: false })
6895
.pipe(mocha({ reporter: "dot" }));
6996
});
70-
gulp.task("test:no-conflict", ["build:tests", "no-polyfill"], () => {
97+
gulp.task("test:no-conflict", ["build:tests:no-conflict", "no-polyfill"], () => {
7198
console.log("Running no-conflict-mode tests w/o polyfill...");
7299
return gulp
73100
.src(["test/no-conflict/**/*.js"], { read: false })
74101
.pipe(mocha({ reporter: "dot" }));
75102
});
76-
gulp.task("test:use-polyfill", ["build:tests", "use-polyfill"], () => {
103+
gulp.task("test:registry", ["build:tests:registry", "no-polyfill"], () => {
104+
console.log("Running registry...");
105+
return gulp
106+
.src(["test/registry/**/*.js"], { read: false })
107+
.pipe(mocha({ reporter: "dot" }));
108+
});
109+
gulp.task("test:use-polyfill", ["build:tests:full", "use-polyfill"], () => {
77110
console.log("Running tests w/ polyfill...");
78111
return gulp
79112
.src(["test/full/**/*.js"], { read: false })
80113
.pipe(mocha({ reporter: "dot" }));
81114
});
82-
gulp.task("test", sequence("test:full", "test:lite", "test:no-conflict", "test:use-polyfill"));
115+
gulp.task("test", sequence("test:full", "test:lite", "test:no-conflict", "test:registry", "test:use-polyfill"));
83116

84117

85118
gulp.task("watch:reflect", () => gulp.watch([

‎test/tsconfig.json ‎test/full/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"sourceMap": true,
66
"module": "commonjs",
77
"types": ["node", "mocha"],
8-
"typeRoots": ["../node_modules/@types"],
8+
"typeRoots": ["../../node_modules/@types"],
99
},
1010
"include": [
1111
"**/*.ts"

‎test/lite/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"noImplicitAny": true,
5+
"sourceMap": true,
6+
"module": "commonjs",
7+
"types": ["node", "mocha"],
8+
"typeRoots": ["../../node_modules/@types"],
9+
},
10+
"include": [
11+
"**/*.ts"
12+
]
13+
}

‎test/no-conflict/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"noImplicitAny": true,
5+
"sourceMap": true,
6+
"module": "commonjs",
7+
"types": ["node", "mocha"],
8+
"typeRoots": ["../../node_modules/@types"],
9+
},
10+
"include": [
11+
"**/*.ts"
12+
]
13+
}

‎test/registry/registry.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="../../index.d.ts" />
2+
/// <reference path="../../globals.d.ts" />
3+
const ReflectNoConflict = require("../../ReflectNoConflict");
4+
require("../../Reflect");
5+
import { assert } from "chai";
6+
7+
describe("MetadataRegistry", () => {
8+
it("defines registry", () => {
9+
const registrySymbol = Symbol.for("@reflect-metadata:registry");
10+
const registry = (Reflect as any)[registrySymbol] as MetadataRegistry;
11+
assert.isDefined(registry);
12+
});
13+
it("two registries", () => {
14+
const registrySymbol = Symbol.for("@reflect-metadata:registry");
15+
const registry = (Reflect as any)[registrySymbol] as MetadataRegistry;
16+
const obj1 = {};
17+
ReflectNoConflict.defineMetadata("key", "value", obj1);
18+
const obj2 = {};
19+
Reflect.defineMetadata("key", "value", obj2);
20+
const provider1 = registry.getProvider(obj1, undefined);
21+
const provider2 = registry.getProvider(obj2, undefined);
22+
assert.isDefined(provider1);
23+
assert.isDefined(provider2);
24+
assert.notStrictEqual(provider1, provider2);
25+
});
26+
it("registries are shared", () => {
27+
const obj = {};
28+
ReflectNoConflict.defineMetadata("key", "value", obj);
29+
assert.isTrue(Reflect.hasOwnMetadata("key", obj));
30+
});
31+
});

‎test/registry/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"noImplicitAny": true,
5+
"sourceMap": true,
6+
"module": "commonjs",
7+
"types": ["node", "mocha"],
8+
"typeRoots": ["../../node_modules/@types"],
9+
},
10+
"include": [
11+
"**/*.ts"
12+
]
13+
}

0 commit comments

Comments
 (0)
Please sign in to comment.