Skip to content
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

fix(runtime-core): avoid the proxy object polluting the slots of the internal instance #3698

Merged
merged 2 commits into from
May 24, 2021
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 packages/runtime-core/src/componentRenderContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export function withCtx(
// mark this as a compiled slot function.
// this is used in vnode.ts -> normalizeChildren() to set the slot
// rendering flag.
renderFnWithContext._c = true
// also used to cache the normalized results to avoid repeated normalization
renderFnWithContext._c = renderFnWithContext
if (__COMPAT__ && isNonScopedSlot) {
renderFnWithContext._nonScoped = true
}
Expand Down
10 changes: 7 additions & 3 deletions packages/runtime-core/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { isKeepAlive } from './components/KeepAlive'
import { withCtx } from './componentRenderContext'
import { isHmrUpdating } from './hmr'
import { DeprecationTypes, isCompatEnabled } from './compat/compatConfig'
import { toRaw } from '@vue/reactivity'

export type Slot = (...args: any[]) => VNode[]

Expand Down Expand Up @@ -62,7 +63,8 @@ const normalizeSlot = (
rawSlot: Function,
ctx: ComponentInternalInstance | null | undefined
): Slot =>
withCtx((props: any) => {
(rawSlot as any)._c ||
(withCtx((props: any) => {
if (__DEV__ && currentInstance) {
warn(
`Slot "${key}" invoked outside of the render function: ` +
Expand All @@ -71,7 +73,7 @@ const normalizeSlot = (
)
}
return normalizeSlotValue(rawSlot(props))
}, ctx) as Slot
}, ctx) as Slot)

const normalizeObjectSlots = (
rawSlots: RawSlots,
Expand Down Expand Up @@ -128,7 +130,9 @@ export const initSlots = (
if (instance.vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
const type = (children as RawSlots)._
if (type) {
instance.slots = children as InternalSlots
// users can get the shallow readonly version of the slots object through `this.$slots`,
// we should avoid the proxy object polluting the slots of the internal instance
instance.slots = toRaw(children as InternalSlots)
// make compiler marker non-enumerable
def(children as InternalSlots, '_', type)
} else {
Expand Down