@@ -147,6 +147,28 @@ Object.keys(descs).forEach((key) => {
147147 }
148148} ) ;
149149
150+ export function warpObject ( thisContext : Object , ...context : Object [ ] ) {
151+ // 处理Object上的方法
152+ thisContext . hasOwnProperty = ( name : PropertyKey ) => {
153+ return (
154+ Object . hasOwnProperty . call ( thisContext , name ) ||
155+ context . some ( ( val ) => Object . hasOwnProperty . call ( val , name ) )
156+ ) ;
157+ } ;
158+ thisContext . isPrototypeOf = ( name : Object ) => {
159+ return (
160+ Object . isPrototypeOf . call ( thisContext , name ) ||
161+ context . some ( ( val ) => Object . isPrototypeOf . call ( val , name ) )
162+ ) ;
163+ } ;
164+ thisContext . propertyIsEnumerable = ( name : PropertyKey ) => {
165+ return (
166+ Object . propertyIsEnumerable . call ( thisContext , name ) ||
167+ context . some ( ( val ) => Object . propertyIsEnumerable . call ( val , name ) )
168+ ) ;
169+ } ;
170+ }
171+
150172// 拦截上下文
151173export function proxyContext (
152174 global : any ,
@@ -161,6 +183,7 @@ export function proxyContext(
161183 }
162184 thisContext . eval = global . eval ;
163185 thisContext . define = undefined ;
186+ warpObject ( thisContext , special , global , context ) ;
164187 // keyword是与createContext时同步的,避免访问到context的内部变量
165188 const contextKeyword : { [ key : string ] : any } = {
166189 message : 1 ,
@@ -203,13 +226,13 @@ export function proxyContext(
203226 return thisContext [ name ] ;
204227 }
205228 if ( typeof name === "string" ) {
206- if ( context [ name ] ) {
207- if ( contextKeyword [ name ] ) {
229+ if ( has ( context , name ) ) {
230+ if ( has ( contextKeyword , name ) ) {
208231 return undefined ;
209232 }
210233 return context [ name ] ;
211234 }
212- if ( special [ name ] !== undefined ) {
235+ if ( has ( special , name ) ) {
213236 if (
214237 typeof special [ name ] === "function" &&
215238 ! ( < { prototype : any } > special [ name ] ) . prototype
@@ -250,22 +273,22 @@ export function proxyContext(
250273 }
251274 if ( name !== "undefined" ) {
252275 if ( typeof name === "string" ) {
253- if ( unscopables [ name ] ) {
276+ if ( has ( unscopables , name ) ) {
254277 return false ;
255278 }
256279 if ( has ( thisContext , name ) ) {
257280 return true ;
258281 }
259- if ( context [ name ] ) {
260- if ( contextKeyword [ name ] ) {
282+ if ( has ( context , name ) ) {
283+ if ( has ( contextKeyword , name ) ) {
261284 return false ;
262285 }
263286 return true ;
264287 }
265- if ( special [ name ] !== undefined ) {
288+ if ( has ( special , name ) ) {
266289 return true ;
267290 }
268- if ( global [ name ] !== undefined ) {
291+ if ( has ( global [ name ] , name ) ) {
269292 return true ;
270293 }
271294 } else if ( typeof name === "symbol" ) {
@@ -279,12 +302,11 @@ export function proxyContext(
279302 case "window" :
280303 case "self" :
281304 case "globalThis" :
282- special . global = val ;
283- return true ;
305+ return false ;
284306 default :
285307 }
286- if ( special [ name ] ) {
287- special [ name ] = val ;
308+ if ( has ( special , name ) ) {
309+ thisContext [ name ] = val ;
288310 return true ;
289311 }
290312 if ( init . has ( name ) ) {
@@ -293,7 +315,7 @@ export function proxyContext(
293315 if ( des && des . get && ! des . set && des . configurable ) {
294316 return true ;
295317 }
296- global [ name ] = val ;
318+ thisContext [ name ] = val ;
297319 return true ;
298320 }
299321 // @ts -ignore
0 commit comments