Skip to content

fix #4872, use context agnostic Function constructor check #4928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ function getPropDefaultValue (vm: ?Component, prop: PropOptions, key: string): a
return vm[key]
}
// call factory function for non-Function types
return typeof def === 'function' && prop.type !== Function
// a value is Function if its prototype is function even across different execution context
return typeof def === 'function' && getType(prop.type) !== 'Function'
? def.call(vm)
: def
}
Expand Down
18 changes: 18 additions & 0 deletions test/ssr/ssr-string.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from '../../dist/vue.runtime.common.js'
import VM from 'vm'
import { createRenderer } from '../../packages/vue-server-renderer'
const { renderToString } = createRenderer()

Expand Down Expand Up @@ -699,6 +700,23 @@ describe('SSR: renderToString', () => {
done()
}, context)
})

it('default value Foreign Function', () => {
const FunctionConstructor = VM.runInNewContext('Function')
const func = () => 123
const vm = new Vue({
props: {
a: {
type: FunctionConstructor,
default: func
}
},
propsData: {
a: undefined
}
})
expect(vm.a).toBe(func)
})
})

function renderVmWithOptions (options, cb) {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ describe('Options props', () => {
}).then(done)
})

it('default value Function', () => {
const func = () => 132
const vm = new Vue({
props: {
a: {
type: Function,
default: func
}
},
propsData: {
a: undefined
}
})
expect(vm.a).toBe(func)
})

it('warn object/array default values', () => {
new Vue({
props: {
Expand Down