Skip to content

Commit

Permalink
Add shared registry for use when multiple versions are loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Dec 13, 2023
1 parent 6227ddf commit 31dde5f
Show file tree
Hide file tree
Showing 10 changed files with 1,011 additions and 241 deletions.
354 changes: 285 additions & 69 deletions Reflect.ts

Large diffs are not rendered by default.

352 changes: 283 additions & 69 deletions ReflectLite.ts

Large diffs are not rendered by default.

403 changes: 311 additions & 92 deletions ReflectNoConflict.ts

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and limitations under the License.

interface SymbolConstructor {
(description?: string): symbol;
for(key: string): symbol;
readonly iterator: symbol;
readonly toPrimitive: symbol;
}
Expand Down Expand Up @@ -89,3 +90,20 @@ interface WeakMapConstructor {
declare var Map: MapConstructor;
declare var Set: SetConstructor;
declare var WeakMap: WeakMapConstructor;

// NOTE: These are not actually global, just shared between the Reflect*.ts variants

interface MetadataRegistry {
registerProvider(provider: MetadataProvider): void;
getProvider(O: object, P: string | symbol | undefined): MetadataProvider | undefined;
setProvider(O: object, P: string | symbol | undefined, provider: MetadataProvider): boolean;
}

interface MetadataProvider {
isProviderFor(O: object, P: string | symbol | undefined): boolean;
OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: object, P: string | symbol | undefined): void;
OrdinaryDeleteMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean;
OrdinaryHasOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean;
OrdinaryGetOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): any;
OrdinaryOwnMetadataKeys(O: object, P: string | symbol | undefined): any[];
}
53 changes: 43 additions & 10 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ const gls = require("gulp-live-server");

const debugProject = tsb.create("tsconfig.json");
const releaseProject = tsb.create("tsconfig-release.json");
const tests = tsb.create("test/tsconfig.json");
const tests = {
full: tsb.create("test/full/tsconfig.json"),
lite: tsb.create("test/lite/tsconfig.json"),
"no-conflict": tsb.create("test/no-conflict/tsconfig.json"),
registry: tsb.create("test/registry/tsconfig.json"),
};

let project = debugProject;

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

gulp.task("build:tests", ["build:reflect"], () => gulp
.src(["test/**/*.ts"])
.pipe(tests())
.pipe(gulp.dest("test")));
gulp.task("build:tests:full", ["build:reflect"], () => gulp
.src(["test/full/**/*.ts"])
.pipe(tests.full())
.pipe(gulp.dest("test/full")));

gulp.task("build:tests:lite", ["build:reflect"], () => gulp
.src(["test/lite/**/*.ts"])
.pipe(tests.lite())
.pipe(gulp.dest("test/lite")));

gulp.task("build:tests:no-conflict", ["build:reflect"], () => gulp
.src(["test/no-conflict/**/*.ts"])
.pipe(tests["no-conflict"]())
.pipe(gulp.dest("test/no-conflict")));

gulp.task("build:tests:registry", ["build:reflect"], () => gulp
.src(["test/registry/**/*.ts"])
.pipe(tests.registry())
.pipe(gulp.dest("test/registry")));

gulp.task("build:tests", [
"build:tests:full",
"build:tests:lite",
"build:tests:no-conflict",
"build:tests:registry"
]);

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

gulp.task("test:full", ["build:tests", "no-polyfill"], () => {
gulp.task("test:full", ["build:tests:full", "no-polyfill"], () => {
console.log("Running tests w/o polyfill...");
return gulp
.src(["test/full/**/*.js"], { read: false })
.pipe(mocha({ reporter: "dot" }));
});
gulp.task("test:lite", ["build:tests", "no-polyfill"], () => {
gulp.task("test:lite", ["build:tests:lite", "no-polyfill"], () => {
console.log("Running lite-mode tests w/o polyfill...");
return gulp
.src(["test/lite/**/*.js"], { read: false })
.pipe(mocha({ reporter: "dot" }));
});
gulp.task("test:no-conflict", ["build:tests", "no-polyfill"], () => {
gulp.task("test:no-conflict", ["build:tests:no-conflict", "no-polyfill"], () => {
console.log("Running no-conflict-mode tests w/o polyfill...");
return gulp
.src(["test/no-conflict/**/*.js"], { read: false })
.pipe(mocha({ reporter: "dot" }));
});
gulp.task("test:use-polyfill", ["build:tests", "use-polyfill"], () => {
gulp.task("test:registry", ["build:tests:registry", "no-polyfill"], () => {
console.log("Running registry...");
return gulp
.src(["test/registry/**/*.js"], { read: false })
.pipe(mocha({ reporter: "dot" }));
});
gulp.task("test:use-polyfill", ["build:tests:full", "use-polyfill"], () => {
console.log("Running tests w/ polyfill...");
return gulp
.src(["test/full/**/*.js"], { read: false })
.pipe(mocha({ reporter: "dot" }));
});
gulp.task("test", sequence("test:full", "test:lite", "test:no-conflict", "test:use-polyfill"));
gulp.task("test", sequence("test:full", "test:lite", "test:no-conflict", "test:registry", "test:use-polyfill"));


gulp.task("watch:reflect", () => gulp.watch([
Expand Down
2 changes: 1 addition & 1 deletion test/tsconfig.json → test/full/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"sourceMap": true,
"module": "commonjs",
"types": ["node", "mocha"],
"typeRoots": ["../node_modules/@types"],
"typeRoots": ["../../node_modules/@types"],
},
"include": [
"**/*.ts"
Expand Down
13 changes: 13 additions & 0 deletions test/lite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es5",
"noImplicitAny": true,
"sourceMap": true,
"module": "commonjs",
"types": ["node", "mocha"],
"typeRoots": ["../../node_modules/@types"],
},
"include": [
"**/*.ts"
]
}
13 changes: 13 additions & 0 deletions test/no-conflict/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es5",
"noImplicitAny": true,
"sourceMap": true,
"module": "commonjs",
"types": ["node", "mocha"],
"typeRoots": ["../../node_modules/@types"],
},
"include": [
"**/*.ts"
]
}
31 changes: 31 additions & 0 deletions test/registry/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// <reference path="../../index.d.ts" />
/// <reference path="../../globals.d.ts" />
const ReflectNoConflict = require("../../ReflectNoConflict");
require("../../Reflect");
import { assert } from "chai";

describe("MetadataRegistry", () => {
it("defines registry", () => {
const registrySymbol = Symbol.for("@reflect-metadata:registry");
const registry = (Reflect as any)[registrySymbol] as MetadataRegistry;
assert.isDefined(registry);
});
it("two registries", () => {
const registrySymbol = Symbol.for("@reflect-metadata:registry");
const registry = (Reflect as any)[registrySymbol] as MetadataRegistry;
const obj1 = {};
ReflectNoConflict.defineMetadata("key", "value", obj1);
const obj2 = {};
Reflect.defineMetadata("key", "value", obj2);
const provider1 = registry.getProvider(obj1, undefined);
const provider2 = registry.getProvider(obj2, undefined);
assert.isDefined(provider1);
assert.isDefined(provider2);
assert.notStrictEqual(provider1, provider2);
});
it("registries are shared", () => {
const obj = {};
ReflectNoConflict.defineMetadata("key", "value", obj);
assert.isTrue(Reflect.hasOwnMetadata("key", obj));
});
});
13 changes: 13 additions & 0 deletions test/registry/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es5",
"noImplicitAny": true,
"sourceMap": true,
"module": "commonjs",
"types": ["node", "mocha"],
"typeRoots": ["../../node_modules/@types"],
},
"include": [
"**/*.ts"
]
}

0 comments on commit 31dde5f

Please sign in to comment.