From 5881536661a0e5c29ea5b6f6ba61b094ce970a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EC=9C=A0=EC=84=B1?= Date: Fri, 28 Jun 2024 02:56:53 +0900 Subject: [PATCH 1/3] fix ES6 Map/Set checks for cross-window scripts --- packages/mobx/src/types/observablemap.ts | 2 +- packages/mobx/src/utils/utils.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/mobx/src/types/observablemap.ts b/packages/mobx/src/types/observablemap.ts index 99bd441750..afc6590aad 100644 --- a/packages/mobx/src/types/observablemap.ts +++ b/packages/mobx/src/types/observablemap.ts @@ -351,7 +351,7 @@ export class ObservableMap } else if (Array.isArray(other)) { other.forEach(([key, value]) => this.set(key, value)) } else if (isES6Map(other)) { - if (other.constructor !== Map) { + if (other.constructor.name !== "Map") { die(19, other) } other.forEach((value, key) => this.set(key, value)) diff --git a/packages/mobx/src/utils/utils.ts b/packages/mobx/src/utils/utils.ts index fc2760b04c..8f69f1b929 100644 --- a/packages/mobx/src/utils/utils.ts +++ b/packages/mobx/src/utils/utils.ts @@ -141,11 +141,11 @@ export function createInstanceofPredicate( } export function isES6Map(thing: any): thing is Map { - return thing instanceof Map + return thing != null && Object.prototype.toString.call(thing) === "[object Map]" } export function isES6Set(thing: any): thing is Set { - return thing instanceof Set + return thing != null && Object.prototype.toString.call(thing) === "[object Set]" } const hasGetOwnPropertySymbols = typeof Object.getOwnPropertySymbols !== "undefined" From 103d4b03163c4f2219bfd6056d68f2781c6c618b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EC=9C=A0=EC=84=B1?= Date: Fri, 28 Jun 2024 13:44:27 +0900 Subject: [PATCH 2/3] add isPlainES6Map() --- packages/mobx/src/types/observablemap.ts | 3 ++- packages/mobx/src/utils/utils.ts | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/mobx/src/types/observablemap.ts b/packages/mobx/src/types/observablemap.ts index afc6590aad..3cf868b255 100644 --- a/packages/mobx/src/types/observablemap.ts +++ b/packages/mobx/src/types/observablemap.ts @@ -16,6 +16,7 @@ import { hasListeners, interceptChange, isES6Map, + isPlainES6Map, isPlainObject, isSpyEnabled, makeIterable, @@ -351,7 +352,7 @@ export class ObservableMap } else if (Array.isArray(other)) { other.forEach(([key, value]) => this.set(key, value)) } else if (isES6Map(other)) { - if (other.constructor.name !== "Map") { + if (!isPlainES6Map(other)) { die(19, other) } other.forEach((value, key) => this.set(key, value)) diff --git a/packages/mobx/src/utils/utils.ts b/packages/mobx/src/utils/utils.ts index 8f69f1b929..1f1ca701d7 100644 --- a/packages/mobx/src/utils/utils.ts +++ b/packages/mobx/src/utils/utils.ts @@ -144,6 +144,13 @@ export function isES6Map(thing: any): thing is Map { return thing != null && Object.prototype.toString.call(thing) === "[object Map]" } +export function isPlainES6Map(thing: Map): boolean { + const mapProto = Object.getPrototypeOf(thing) + const objectProto = Object.getPrototypeOf(mapProto) + const nullProto = Object.getPrototypeOf(objectProto) + return nullProto === null +} + export function isES6Set(thing: any): thing is Set { return thing != null && Object.prototype.toString.call(thing) === "[object Set]" } From 8ddd0b4212246c117efb035473e2e05a03dc68ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EC=9C=A0=EC=84=B1?= Date: Sat, 29 Jun 2024 23:21:02 +0900 Subject: [PATCH 3/3] add changest --- .changeset/fresh-apricots-heal.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fresh-apricots-heal.md diff --git a/.changeset/fresh-apricots-heal.md b/.changeset/fresh-apricots-heal.md new file mode 100644 index 0000000000..b85e9a5327 --- /dev/null +++ b/.changeset/fresh-apricots-heal.md @@ -0,0 +1,5 @@ +--- +"mobx": patch +--- + +Fix ES6 Map/Set checks for cross-window scripts