@@ -4,12 +4,12 @@ import { hasOwn, isObject, isPlainObject, capitalize, hyphenate } from 'shared/u
44import { observe , observerState } from '../observer/index'
55import { warn } from './debug'
66
7- type PropOptions = {
8- type : Function | Array < Function > | null ,
9- default : any ,
10- required : ?boolean ,
11- validator : ?Function
12- }
7+ // type PropOptions = {
8+ // type: Function | Array<Function> | null,
9+ // default: any,
10+ // required: ?boolean,
11+ // validator: ?Function
12+ // }
1313
1414export function validateProp (
1515 key : string ,
@@ -23,7 +23,7 @@ export function validateProp (
2323 const absent = ! hasOwn ( propsData , key )
2424 let value = propsData [ key ]
2525 // handle boolean props
26- if ( prop . type === Boolean ) {
26+ if ( getType ( prop . type ) === ' Boolean' ) {
2727 if ( absent && ! hasOwn ( prop , 'default' ) ) {
2828 value = false
2929 } else if ( value === '' || value === hyphenate ( key ) ) {
@@ -131,31 +131,34 @@ function assertType (value: any, type: Function): {
131131 expectedType : string
132132} {
133133 let valid
134- let expectedType
135- if ( type === String ) {
136- expectedType = 'string '
137- valid = typeof value === expectedType
138- } else if ( type === Number ) {
139- expectedType = 'number '
140- valid = typeof value === expectedType
141- } else if ( type === Boolean ) {
142- expectedType = 'boolean '
143- valid = typeof value === expectedType
144- } else if ( type === Function ) {
145- expectedType = 'function '
146- valid = typeof value === expectedType
147- } else if ( type === Object ) {
148- expectedType = 'Object '
134+ let expectedType = getType ( type )
135+ if ( expectedType === 'String' ) {
136+ valid = typeof value === ( expectedType = 'string' )
137+ } else if ( expectedType === 'Number' ) {
138+ valid = typeof value === ( expectedType = 'number' )
139+ } else if ( expectedType === 'Boolean' ) {
140+ valid = typeof value === ( expectedType = 'boolean' )
141+ } else if ( expectedType === 'Function' ) {
142+ valid = typeof value === ( expectedType = 'function' )
143+ } else if ( expectedType === 'Object' ) {
149144 valid = isPlainObject ( value )
150- } else if ( type === Array ) {
151- expectedType = 'Array '
145+ } else if ( expectedType === 'Array' ) {
152146 valid = Array . isArray ( value )
153147 } else {
154- expectedType = type . name || type . toString ( )
155148 valid = value instanceof type
156149 }
157150 return {
158151 valid ,
159152 expectedType
160153 }
161154}
155+
156+ /**
157+ * Use function string name to check built-in types,
158+ * because a simple equality check will fail when running
159+ * across different vms / iframes.
160+ */
161+ function getType ( fn ) {
162+ const match = fn && fn . toString ( ) . match ( / ^ f u n c t i o n ( \w + ) / )
163+ return match && match [ 1 ]
164+ }
0 commit comments