@@ -76,9 +76,10 @@ const publicPropertiesMap: Record<
7676}
7777
7878const enum AccessTypes {
79+ SETUP ,
7980 DATA ,
80- CONTEXT ,
8181 PROPS ,
82+ CONTEXT ,
8283 OTHER
8384}
8485
@@ -91,6 +92,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
9192 get ( { _ : instance } : ComponentPublicProxyTarget , key : string ) {
9293 const {
9394 renderContext,
95+ setupState,
9496 data,
9597 props,
9698 accessCache,
@@ -109,6 +111,8 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
109111 const n = accessCache ! [ key ]
110112 if ( n !== undefined ) {
111113 switch ( n ) {
114+ case AccessTypes . SETUP :
115+ return setupState [ key ]
112116 case AccessTypes . DATA :
113117 return data [ key ]
114118 case AccessTypes . CONTEXT :
@@ -117,22 +121,25 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
117121 return props ! [ key ]
118122 // default: just fallthrough
119123 }
124+ } else if ( setupState !== EMPTY_OBJ && hasOwn ( setupState , key ) ) {
125+ accessCache ! [ key ] = AccessTypes . SETUP
126+ return setupState [ key ]
120127 } else if ( data !== EMPTY_OBJ && hasOwn ( data , key ) ) {
121128 accessCache ! [ key ] = AccessTypes . DATA
122129 return data [ key ]
130+ } else if (
131+ // only cache other properties when instance has declared (thus stable)
132+ // props
133+ type . props &&
134+ hasOwn ( normalizePropsOptions ( type . props ) [ 0 ] ! , key )
135+ ) {
136+ accessCache ! [ key ] = AccessTypes . PROPS
137+ return props ! [ key ]
123138 } else if ( renderContext !== EMPTY_OBJ && hasOwn ( renderContext , key ) ) {
124139 accessCache ! [ key ] = AccessTypes . CONTEXT
125140 return renderContext [ key ]
126- } else if ( type . props ) {
127- // only cache other properties when instance has declared (thus stable)
128- // props
129- if ( hasOwn ( normalizePropsOptions ( type . props ) [ 0 ] ! , key ) ) {
130- accessCache ! [ key ] = AccessTypes . PROPS
131- // return the value from propsProxy for ref unwrapping and readonly
132- return props ! [ key ]
133- } else {
134- accessCache ! [ key ] = AccessTypes . OTHER
135- }
141+ } else {
142+ accessCache ! [ key ] = AccessTypes . OTHER
136143 }
137144 }
138145
@@ -170,23 +177,25 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
170177 key : string ,
171178 value : any
172179 ) : boolean {
173- const { data, renderContext } = instance
174- if ( data !== EMPTY_OBJ && hasOwn ( data , key ) ) {
180+ const { data, setupState, renderContext } = instance
181+ if ( setupState !== EMPTY_OBJ && hasOwn ( setupState , key ) ) {
182+ setupState [ key ] = value
183+ } else if ( data !== EMPTY_OBJ && hasOwn ( data , key ) ) {
175184 data [ key ] = value
176- } else if ( hasOwn ( renderContext , key ) ) {
177- renderContext [ key ] = value
178- } else if ( key [ 0 ] === '$' && key . slice ( 1 ) in instance ) {
185+ } else if ( key in instance . props ) {
179186 __DEV__ &&
180187 warn (
181- `Attempting to mutate public property "${ key } ". ` +
182- `Properties starting with $ are reserved and readonly.` ,
188+ `Attempting to mutate prop "${ key } ". Props are readonly.` ,
183189 instance
184190 )
185191 return false
186- } else if ( key in instance . props ) {
192+ } else if ( hasOwn ( renderContext , key ) ) {
193+ renderContext [ key ] = value
194+ } else if ( key [ 0 ] === '$' && key . slice ( 1 ) in instance ) {
187195 __DEV__ &&
188196 warn (
189- `Attempting to mutate prop "${ key } ". Props are readonly.` ,
197+ `Attempting to mutate public property "${ key } ". ` +
198+ `Properties starting with $ are reserved and readonly.` ,
190199 instance
191200 )
192201 return false
@@ -206,15 +215,24 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
206215
207216 has (
208217 {
209- _ : { data, accessCache, renderContext, type, proxyTarget, appContext }
218+ _ : {
219+ data,
220+ setupState,
221+ accessCache,
222+ renderContext,
223+ type,
224+ proxyTarget,
225+ appContext
226+ }
210227 } : ComponentPublicProxyTarget ,
211228 key : string
212229 ) {
213230 return (
214231 accessCache ! [ key ] !== undefined ||
215232 ( data !== EMPTY_OBJ && hasOwn ( data , key ) ) ||
216- hasOwn ( renderContext , key ) ||
233+ ( setupState !== EMPTY_OBJ && hasOwn ( setupState , key ) ) ||
217234 ( type . props && hasOwn ( normalizePropsOptions ( type . props ) [ 0 ] ! , key ) ) ||
235+ hasOwn ( renderContext , key ) ||
218236 hasOwn ( publicPropertiesMap , key ) ||
219237 hasOwn ( proxyTarget , key ) ||
220238 hasOwn ( appContext . config . globalProperties , key )
@@ -306,15 +324,15 @@ export function exposePropsOnDevProxyTarget(
306324 }
307325}
308326
309- export function exposeRenderContextOnDevProxyTarget (
327+ export function exposeSetupStateOnDevProxyTarget (
310328 instance : ComponentInternalInstance
311329) {
312- const { proxyTarget, renderContext } = instance
313- Object . keys ( toRaw ( renderContext ) ) . forEach ( key => {
330+ const { proxyTarget, setupState } = instance
331+ Object . keys ( toRaw ( setupState ) ) . forEach ( key => {
314332 Object . defineProperty ( proxyTarget , key , {
315333 enumerable : true ,
316334 configurable : true ,
317- get : ( ) => renderContext [ key ] ,
335+ get : ( ) => setupState [ key ] ,
318336 set : NOOP
319337 } )
320338 } )
0 commit comments