@@ -78,28 +78,69 @@ class p5 {
7878 this . _updateWindowSize ( ) ;
7979
8080 const bindGlobal = property => {
81- Object . defineProperty ( window , property , {
82- configurable : true ,
83- enumerable : true ,
84- get : ( ) => {
85- if ( typeof this [ property ] === 'function' ) {
86- return this [ property ] . bind ( this ) ;
87- } else {
88- return this [ property ] ;
81+ if ( property === 'constructor' ) return ;
82+
83+ // Check if this property has a getter on the instance or prototype
84+ const instanceDescriptor = Object . getOwnPropertyDescriptor ( this , property ) ;
85+ const prototypeDescriptor = Object . getOwnPropertyDescriptor ( p5 . prototype , property ) ;
86+ const hasGetter = ( instanceDescriptor && instanceDescriptor . get ) ||
87+ ( prototypeDescriptor && prototypeDescriptor . get ) ;
88+
89+ // Only check if it's a function if it doesn't have a getter
90+ // to avoid actually evaluating getters before things like the
91+ // renderer are fully constructed
92+ let isPrototypeFunction = false ;
93+ if ( ! hasGetter ) {
94+ const prototypeValue = p5 . prototype [ property ] ;
95+ isPrototypeFunction = typeof prototypeValue === 'function' ;
96+ }
97+
98+ if ( isPrototypeFunction ) {
99+ // For regular functions, cache the bound function
100+ const boundFunction = p5 . prototype [ property ] . bind ( this ) ;
101+ Object . defineProperty ( window , property , {
102+ configurable : true ,
103+ enumerable : true ,
104+ get ( ) {
105+ return boundFunction ;
106+ } ,
107+ set ( newValue ) {
108+ Object . defineProperty ( window , property , {
109+ configurable : true ,
110+ enumerable : true ,
111+ value : newValue ,
112+ writable : true
113+ } ) ;
114+ if ( ! p5 . disableFriendlyErrors ) {
115+ console . log ( `You just changed the value of "${ property } ", which was a p5 function. This could cause problems later if you're not careful.` ) ;
116+ }
89117 }
90- } ,
91- set : newValue => {
92- Object . defineProperty ( window , property , {
93- configurable : true ,
94- enumerable : true ,
95- value : newValue ,
96- writable : true
97- } ) ;
98- if ( ! p5 . disableFriendlyErrors ) {
99- console . log ( `You just changed the value of "${ property } ", which was a p5 global value. This could cause problems later if you're not careful.` ) ;
118+ } ) ;
119+ } else {
120+ // For properties with getters or non-function properties, use dynamic access
121+ Object . defineProperty ( window , property , {
122+ configurable : true ,
123+ enumerable : true ,
124+ get : ( ) => {
125+ if ( typeof this [ property ] === 'function' ) {
126+ return this [ property ] . bind ( this ) ;
127+ } else {
128+ return this [ property ] ;
129+ }
130+ } ,
131+ set : newValue => {
132+ Object . defineProperty ( window , property , {
133+ configurable : true ,
134+ enumerable : true ,
135+ value : newValue ,
136+ writable : true
137+ } ) ;
138+ if ( ! p5 . disableFriendlyErrors ) {
139+ console . log ( `You just changed the value of "${ property } ", which was a p5 global value. This could cause problems later if you're not careful.` ) ;
140+ }
100141 }
101- }
102- } ) ;
142+ } ) ;
143+ }
103144 } ;
104145 // If the user has created a global setup or draw function,
105146 // assume "global" mode and make everything global (i.e. on the window)
0 commit comments