1- import type {
2- Infer ,
3- InferPartial ,
4- IsValidVertexSchema ,
5- } from '../shared/repr.ts' ;
61import { $internal } from '../shared/symbols.ts' ;
7- import type {
8- $invalidSchemaReason ,
9- $repr ,
10- $reprPartial ,
11- $validVertexSchema ,
12- } from '../shared/symbols.ts' ;
132import type { AnyData , Disarray } from './dataTypes.ts' ;
3+ import { schemaCallWrapper } from './utils.ts' ;
144
155// ----------
166// Public API
@@ -31,42 +21,49 @@ import type { AnyData, Disarray } from './dataTypes.ts';
3121 * const disarray = d.disarrayOf(d.align(16, d.vec3f), 3);
3222 *
3323 * @param elementType The type of elements in the array.
34- * @param count The number of elements in the array.
24+ * @param elementCount The number of elements in the array.
3525 */
3626export function disarrayOf < TElement extends AnyData > (
3727 elementType : TElement ,
38- count : number ,
28+ elementCount : number ,
3929) : Disarray < TElement > {
40- return new DisarrayImpl ( elementType , count ) ;
30+ // In the schema call, create and return a deep copy
31+ // by wrapping all the values in `elementType` schema calls.
32+ const disarraySchema = ( elements ?: TElement [ ] ) => {
33+ if ( elements && elements . length !== elementCount ) {
34+ throw new Error (
35+ `Disarray schema of ${ elementCount } elements of type ${ elementType . type } called with ${ elements . length } argument(s).` ,
36+ ) ;
37+ }
38+
39+ return Array . from (
40+ { length : elementCount } ,
41+ ( _ , i ) => schemaCallWrapper ( elementType , elements ?. [ i ] ) ,
42+ ) ;
43+ } ;
44+ Object . setPrototypeOf ( disarraySchema , DisarrayImpl ) ;
45+
46+ disarraySchema . elementType = elementType ;
47+
48+ if ( ! Number . isInteger ( elementCount ) || elementCount < 0 ) {
49+ throw new Error (
50+ `Cannot create disarray schema with invalid element count: ${ elementCount } .` ,
51+ ) ;
52+ }
53+ disarraySchema . elementCount = elementCount ;
54+
55+ return disarraySchema as unknown as Disarray < TElement > ;
4156}
4257
4358// --------------
4459// Implementation
4560// --------------
4661
47- class DisarrayImpl < TElement extends AnyData > implements Disarray < TElement > {
48- public readonly [ $internal ] = true ;
49- public readonly type = 'disarray' ;
50-
51- // Type-tokens, not available at runtime
52- declare readonly [ $repr ] : Infer < TElement > [ ] ;
53- declare readonly [ $reprPartial ] : {
54- idx : number ;
55- value : InferPartial < TElement > ;
56- } [ ] ;
57- declare readonly [ $validVertexSchema ] : IsValidVertexSchema < TElement > ;
58- declare readonly [ $invalidSchemaReason ] :
59- Disarray [ typeof $invalidSchemaReason ] ;
60- // ---
62+ const DisarrayImpl = {
63+ [ $internal ] : true ,
64+ type : 'disarray' ,
6165
62- constructor (
63- public readonly elementType : TElement ,
64- public readonly elementCount : number ,
65- ) {
66- if ( ! Number . isInteger ( elementCount ) || elementCount < 0 ) {
67- throw new Error (
68- `Cannot create disarray schema with invalid element count: ${ elementCount } .` ,
69- ) ;
70- }
71- }
72- }
66+ toString ( this : Disarray ) : string {
67+ return `disarrayOf(${ this . elementType } , ${ this . elementCount } )` ;
68+ } ,
69+ } ;
0 commit comments