From 7c1a9c78cf84ef88764b39ea260bb209e9be8a06 Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Thu, 28 Oct 2021 14:13:35 +0200 Subject: [PATCH 1/3] fix(compat): don't bind function from globalProperties to the current istance if it has additional properties close #4403 --- packages/runtime-core/src/componentPublicInstance.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index dd385a815c9..42fe3c73e06 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -356,7 +356,9 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { return desc.get.call(instance.proxy) } else { const val = globalProperties[key] - return isFunction(val) ? val.bind(instance.proxy) : val + return isFunction(val) && Object.keys(val).length === 0 + ? val.bind(instance.proxy) + : val } } else { return globalProperties[key] From 7d28b47f96d63f3ec90af81d2bb8bf9fc0f1eb32 Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Thu, 25 Nov 2021 18:20:09 +0100 Subject: [PATCH 2/3] fix: ensure custom properties are copied onto bound function --- packages/runtime-core/src/componentPublicInstance.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 42fe3c73e06..25f24f93878 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -356,8 +356,8 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { return desc.get.call(instance.proxy) } else { const val = globalProperties[key] - return isFunction(val) && Object.keys(val).length === 0 - ? val.bind(instance.proxy) + return isFunction(val) + ? Object.assign(val.bind(instance.proxy), val) : val } } else { From 0ff055c81e38867423399361a216961694350507 Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Fri, 26 Nov 2021 08:52:16 +0100 Subject: [PATCH 3/3] test: Add test case --- packages/vue-compat/__tests__/global.spec.ts | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/vue-compat/__tests__/global.spec.ts b/packages/vue-compat/__tests__/global.spec.ts index 86bb4391092..06109a67f64 100644 --- a/packages/vue-compat/__tests__/global.spec.ts +++ b/packages/vue-compat/__tests__/global.spec.ts @@ -285,6 +285,28 @@ describe('GLOBAL_PROTOTYPE', () => { delete Vue.prototype.$test }) + test.only('functions keeps additional properties', () => { + function test(this: any) { + return this.msg + } + test.additionalFn = () => { + return 'additional fn' + } + + Vue.prototype.$test = test + const vm = new Vue({ + data() { + return { + msg: 'test' + } + } + }) as any + expect(typeof vm.$test).toBe('function') + expect(typeof vm.$test.additionalFn).toBe('function') + expect(vm.$test.additionalFn()).toBe('additional fn') + delete Vue.prototype.$test + }) + test('extended prototype', async () => { const Foo = Vue.extend() Foo.prototype.$test = 1