@@ -129,6 +129,71 @@ describe("User-defined Functions Tests", () => {
129129 db . close ( ) ;
130130 } ) ;
131131
132+ test ( "user-defined functions - DataView return values" , ( ) => {
133+ const db = new DatabaseSync ( ":memory:" ) ;
134+
135+ // Register a function that returns a DataView
136+ db . function ( "dataview_func" , ( ) => {
137+ const buffer = new ArrayBuffer ( 4 ) ;
138+ const view = new DataView ( buffer ) ;
139+ view . setUint8 ( 0 , 0xde ) ;
140+ view . setUint8 ( 1 , 0xad ) ;
141+ view . setUint8 ( 2 , 0xbe ) ;
142+ view . setUint8 ( 3 , 0xef ) ;
143+ return view ;
144+ } ) ;
145+
146+ const stmt = db . prepare ( "SELECT dataview_func() as result" ) ;
147+ const result = stmt . get ( ) ;
148+
149+ expect ( Buffer . isBuffer ( result . result ) ) . toBe ( true ) ;
150+ expect ( result . result ) . toEqual ( Buffer . from ( [ 0xde , 0xad , 0xbe , 0xef ] ) ) ;
151+
152+ db . close ( ) ;
153+ } ) ;
154+
155+ test ( "user-defined functions - TypedArray return values" , ( ) => {
156+ const db = new DatabaseSync ( ":memory:" ) ;
157+
158+ // Register a function that returns a Uint8Array
159+ db . function ( "uint8array_func" , ( ) => {
160+ return new Uint8Array ( [ 0x01 , 0x02 , 0x03 , 0x04 ] ) ;
161+ } ) ;
162+
163+ const stmt = db . prepare ( "SELECT uint8array_func() as result" ) ;
164+ const result = stmt . get ( ) ;
165+
166+ expect ( Buffer . isBuffer ( result . result ) ) . toBe ( true ) ;
167+ expect ( result . result ) . toEqual ( Buffer . from ( [ 0x01 , 0x02 , 0x03 , 0x04 ] ) ) ;
168+
169+ db . close ( ) ;
170+ } ) ;
171+
172+ test ( "user-defined functions - DataView with offset and length" , ( ) => {
173+ const db = new DatabaseSync ( ":memory:" ) ;
174+
175+ // Register a function that returns a DataView with non-zero offset
176+ db . function ( "dataview_offset_func" , ( ) => {
177+ const buffer = new ArrayBuffer ( 8 ) ;
178+ const fullView = new DataView ( buffer ) ;
179+ // Fill the whole buffer
180+ for ( let i = 0 ; i < 8 ; i ++ ) {
181+ fullView . setUint8 ( i , i + 1 ) ;
182+ }
183+ // Return a view that starts at offset 2 and has length 4
184+ return new DataView ( buffer , 2 , 4 ) ;
185+ } ) ;
186+
187+ const stmt = db . prepare ( "SELECT dataview_offset_func() as result" ) ;
188+ const result = stmt . get ( ) ;
189+
190+ expect ( Buffer . isBuffer ( result . result ) ) . toBe ( true ) ;
191+ // Should contain bytes at positions 2,3,4,5 which are 3,4,5,6
192+ expect ( result . result ) . toEqual ( Buffer . from ( [ 0x03 , 0x04 , 0x05 , 0x06 ] ) ) ;
193+
194+ db . close ( ) ;
195+ } ) ;
196+
132197 test ( "user-defined functions - empty string handling" , ( ) => {
133198 const db = new DatabaseSync ( ":memory:" ) ;
134199
0 commit comments