@@ -2974,9 +2974,22 @@ ${hookUniformFields}}
29742974 _inferStructSchema ( firstElement ) {
29752975 const entries = Object . entries ( firstElement ) ;
29762976
2977- // TODO: if FES is enabled, check if all elements
2978- // share same schema, warn if not. Also check for
2979- // deeply nested objects or other unsupported fields
2977+ if ( ! p5 . disableFriendlyErrors ) {
2978+ for ( const [ name , value ] of entries ) {
2979+ if (
2980+ value !== null &&
2981+ typeof value === 'object' &&
2982+ ! Array . isArray ( value ) &&
2983+ ! value ?. isVector
2984+ ) {
2985+ p5 . _friendlyError (
2986+ `The "${ name } " property in your storage data contains a nested object. ` +
2987+ `Make sure you only use properties with numbers, arrays of numbers, or p5.Vector.` ,
2988+ 'createStorage'
2989+ ) ;
2990+ }
2991+ }
2992+ }
29802993
29812994 const fieldLines = entries . map ( ( [ name , value ] ) =>
29822995 ` ${ name } : ${ this . _jsValueToWgslType ( value ) } ,`
@@ -3031,6 +3044,38 @@ ${hookUniformFields}}
30313044 // Struct array: an array of plain objects
30323045 if ( Array . isArray ( dataOrCount ) && dataOrCount . length > 0 &&
30333046 typeof dataOrCount [ 0 ] === 'object' && ! Array . isArray ( dataOrCount [ 0 ] ) ) {
3047+ if ( ! p5 . disableFriendlyErrors && dataOrCount . length > 1 ) {
3048+ const firstKeys = Object . keys ( dataOrCount [ 0 ] ) ;
3049+ let warned = false ;
3050+ for ( let i = 1 ; i < dataOrCount . length ; i ++ ) {
3051+ const el = dataOrCount [ i ] ;
3052+ const elKeys = Object . keys ( el ) ;
3053+ const sameKeys = firstKeys . length === elKeys . length &&
3054+ firstKeys . every ( ( k , j ) => k === elKeys [ j ] ) ;
3055+ if ( ! sameKeys ) {
3056+ p5 . _friendlyError (
3057+ `Element ${ i } has different fields than element 0. ` +
3058+ `All elements should have the same properties.` ,
3059+ 'createStorage'
3060+ ) ;
3061+ break ;
3062+ }
3063+ for ( const key of firstKeys ) {
3064+ const firstType = this . _jsValueToWgslType ( dataOrCount [ 0 ] [ key ] ) ;
3065+ const elType = this . _jsValueToWgslType ( el [ key ] ) ;
3066+ if ( firstType !== elType ) {
3067+ p5 . _friendlyError (
3068+ `The "${ key } " property of element ${ i } has type ${ elType } ` +
3069+ `but element 0 has type ${ firstType } . Proporties should have the same type across all elements.` ,
3070+ 'createStorage'
3071+ ) ;
3072+ warned = true ;
3073+ break ;
3074+ }
3075+ }
3076+ if ( warned ) break ;
3077+ }
3078+ }
30343079 const schema = this . _inferStructSchema ( dataOrCount [ 0 ] ) ;
30353080 const packed = this . _packStructArray ( dataOrCount , schema ) ;
30363081 const size = packed . byteLength ;
0 commit comments