Skip to content

Commit 01151ce

Browse files
HerringtonDarkholmeyyx990803
authored andcommitted
fix #4872, use context agnostic Function constructor check (#4928)
* fix #4872, use context agnostic Function constructor check * use getType to check Function Constructor * fix negation
1 parent dfaf126 commit 01151ce

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/core/util/props.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ function getPropDefaultValue (vm: ?Component, prop: PropOptions, key: string): a
7070
return vm._props[key]
7171
}
7272
// call factory function for non-Function types
73-
return typeof def === 'function' && prop.type !== Function
73+
// a value is Function if its prototype is function even across different execution context
74+
return typeof def === 'function' && getType(prop.type) !== 'Function'
7475
? def.call(vm)
7576
: def
7677
}

test/ssr/ssr-string.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from '../../dist/vue.runtime.common.js'
2+
import VM from 'vm'
23
import { createRenderer } from '../../packages/vue-server-renderer'
34
const { renderToString } = createRenderer()
45

@@ -699,6 +700,23 @@ describe('SSR: renderToString', () => {
699700
done()
700701
}, context)
701702
})
703+
704+
it('default value Foreign Function', () => {
705+
const FunctionConstructor = VM.runInNewContext('Function')
706+
const func = () => 123
707+
const vm = new Vue({
708+
props: {
709+
a: {
710+
type: FunctionConstructor,
711+
default: func
712+
}
713+
},
714+
propsData: {
715+
a: undefined
716+
}
717+
})
718+
expect(vm.a).toBe(func)
719+
})
702720
})
703721

704722
function renderVmWithOptions (options, cb) {

test/unit/features/options/props.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ describe('Options props', () => {
9898
}).then(done)
9999
})
100100

101+
it('default value Function', () => {
102+
const func = () => 132
103+
const vm = new Vue({
104+
props: {
105+
a: {
106+
type: Function,
107+
default: func
108+
}
109+
},
110+
propsData: {
111+
a: undefined
112+
}
113+
})
114+
expect(vm.a).toBe(func)
115+
})
116+
101117
it('warn object/array default values', () => {
102118
new Vue({
103119
props: {

0 commit comments

Comments
 (0)