Skip to content

Commit 35b2ba1

Browse files
committed
refactor!: require nested struct types be struct constructors
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent cd45405 commit 35b2ba1

15 files changed

+83
-29
lines changed

lib/node_modules/@stdlib/dstructs/struct/examples/nested_struct.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var fields2 = [
5252
{
5353
'name': 'words',
5454
'description': 'high and low words',
55-
'type': new Struct1(),
55+
'type': Struct1,
5656
'enumerable': true,
5757
'writable': true,
5858
'castingMode': 'none'

lib/node_modules/@stdlib/dstructs/struct/examples/union_with_struct.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var fields2 = [
5757
{
5858
'name': 'words',
5959
'description': 'high and low words',
60-
'type': new Struct1(),
60+
'type': Struct1,
6161
'enumerable': true,
6262
'writable': true,
6363
'castingMode': 'none'

lib/node_modules/@stdlib/dstructs/struct/lib/byte_length.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
3535
function byteLength( obj ) {
3636
var nb;
3737
if ( obj.isStructType ) {
38-
nb = obj.type.constructor.byteLength;
38+
nb = obj.type.byteLength;
3939
} else {
4040
nb = bytesPerElement( obj.type );
4141
}

lib/node_modules/@stdlib/dstructs/struct/lib/format_layout.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ function layoutFormat( fields ) {
106106
}
107107
// If the current type is a struct, we need to serialize and then post-process...
108108
if ( o.isStructType ) {
109-
tmp = o.type.toString({
110-
'format': 'layout'
111-
});
112-
out.push( replace( tmp, re, replacer( o.byteOffset ) ) );
109+
out.push( replace( o.type.layout, re, replacer( o.byteOffset ) ) );
113110
continue;
114111
}
115112
// Format the field data:

lib/node_modules/@stdlib/dstructs/struct/lib/format_linear.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function linearFormat( Struct, fields ) {
6565
var N;
6666
var o;
6767
var f;
68+
var t;
6869
var i;
6970
var j;
7071
var k;
@@ -137,7 +138,12 @@ function linearFormat( Struct, fields ) {
137138
k = i + 1;
138139
while ( k < N && o.byteOffset === fields[ k ].byteOffset ) {
139140
f = fields[ k ];
140-
tmp.push( format( '%s<%s>[%u]', f.name, f.type, j%f.alignment ) );
141+
if ( f.isStructType ) {
142+
t = '<Struct>';
143+
} else {
144+
t = f.type;
145+
}
146+
tmp.push( format( '%s<%s>[%u]', f.name, t, j%f.alignment ) );
141147
k += 1;
142148
}
143149
ufmt = format( ufmt, tmp.join( ', ' ) );

lib/node_modules/@stdlib/dstructs/struct/lib/get_struct.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function getStruct( obj ) {
4545
*/
4646
function getter() {
4747
var view = this[ PRIVATE_BUFFER ];
48-
return new obj.type.constructor( view.buffer, view.byteOffset+obj.byteOffset, obj.byteLength ); // eslint-disable-line max-len
48+
return new obj.type( view.buffer, view.byteOffset+obj.byteOffset, obj.byteLength ); // eslint-disable-line max-len
4949
}
5050
}
5151

lib/node_modules/@stdlib/dstructs/struct/lib/get_struct_array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function getStructArray( obj ) {
5353
offset = view.byteOffset + obj.byteOffset;
5454
out = [];
5555
for ( i = 0; i < obj.length; i++ ) {
56-
out.push( new obj.type.constructor( view.buffer, offset, obj.byteLength ) ); // eslint-disable-line max-len
56+
out.push( new obj.type( view.buffer, offset, obj.byteLength ) );
5757
offset += obj.byteOffset;
5858
}
5959
return out;

lib/node_modules/@stdlib/dstructs/struct/lib/is_struct.js renamed to lib/node_modules/@stdlib/dstructs/struct/lib/is_struct_instance.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var PRIVATE_BUFFER = require( './private_buffer.js' );
3434
* @param {*} value - value to test
3535
* @returns {boolean} boolean indicating if a value is a `struct` instance
3636
*/
37-
function isStruct( value ) {
37+
function isStructInstance( value ) {
3838
// NOTE: the following is a relatively weak test, but we cannot use `instanceof` checks, etc, due to the factory nature of the implementation. Regardless, here, we are just trying to sniff out a `struct` type. If calling as a constructor later fails, we punt the responsibility off to the user to handle what should be an edge case. If, in the future, this check proves insufficient, we can add further "brand" checks...
3939
return (
4040
isObject( value ) &&
@@ -45,4 +45,4 @@ function isStruct( value ) {
4545

4646
// EXPORTS //
4747

48-
module.exports = isStruct;
48+
module.exports = isStructInstance;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
24+
var isFunction = require( '@stdlib/assert/is-function' );
25+
26+
27+
// MAIN //
28+
29+
/**
30+
* Returns a boolean indicating if a value is a `struct` instance.
31+
*
32+
* @private
33+
* @param {*} value - value to test
34+
* @returns {boolean} boolean indicating if a value is a `struct` instance
35+
*/
36+
function isStructType( value ) {
37+
return (
38+
isFunction( value ) &&
39+
isPositiveInteger( value.alignment ) &&
40+
isPositiveInteger( value.byteLength ) &&
41+
isFunction( value.byteLengthOf ) &&
42+
isFunction( value.byteOffsetOf ) &&
43+
isFunction( value.bufferOf ) &&
44+
isFunction( value.viewOf )
45+
);
46+
}
47+
48+
49+
// EXPORTS //
50+
51+
module.exports = isStructType;

lib/node_modules/@stdlib/dstructs/struct/lib/is_valid_type.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
var contains = require( '@stdlib/array/base/assert/contains' );
2424
var join = require( '@stdlib/array/base/join' );
2525
var format = require( '@stdlib/string/format' );
26-
var isStruct = require( './is_struct.js' );
26+
var isStructType = require( './is_struct_type.js' );
2727
var DTYPES = require( './dtypes.js' );
2828

2929

@@ -37,10 +37,10 @@ var DTYPES = require( './dtypes.js' );
3737
* @returns {(null|TypeError)} error object or null
3838
*/
3939
function isValidType( value ) {
40-
if ( contains( DTYPES, value ) || isStruct( value ) ) {
40+
if ( contains( DTYPES, value ) || isStructType( value ) ) {
4141
return null;
4242
}
43-
return new TypeError( format( 'invalid argument. `%s` field must be either a `struct` or one of the following: "%s". Value: `%s`.', 'type', join( DTYPES, ', ' ), value ) );
43+
return new TypeError( format( 'invalid argument. `%s` field must be either a struct type or one of the following: "%s". Value: `%s`.', 'type', join( DTYPES, ', ' ), value ) );
4444
}
4545

4646

lib/node_modules/@stdlib/dstructs/struct/lib/main.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var format = require( '@stdlib/string/format' );
3737
var PRIVATE_BUFFER = require( './private_buffer.js' );
3838
var CTOR_NAME = require( './ctor_name.js' );
3939
var createPrototypeAccessors = require( './create_prototype_accessors.js' );
40-
var isStruct = require( './is_struct.js' );
40+
var isStructInstance = require( './is_struct_instance.js' );
4141
var normalize = require( './normalize_field_list.js' );
4242
var fieldNames = require( './field_names.js' );
4343
var fieldIndex = require( './field_index.js' );
@@ -412,11 +412,11 @@ function factory( fields ) {
412412
* @readonly
413413
* @type {Function}
414414
* @param {Object} obj - struct instance
415-
* @throws {TypeError} must provide a `struct` instace
415+
* @throws {TypeError} must provide a `struct` instance
416416
* @returns {ArrayBuffer} underlying byte buffer
417417
*/
418418
setReadOnly( Struct, 'bufferOf', function bufferOf( obj ) {
419-
if ( !isStruct( obj ) ) {
419+
if ( !isStructInstance( obj ) ) {
420420
throw new TypeError( format( 'invalid argument. First argument must be a `struct` instance. Value: `%s`.', obj ) );
421421
}
422422
return obj[ PRIVATE_BUFFER ].buffer;
@@ -431,12 +431,12 @@ function factory( fields ) {
431431
* @readonly
432432
* @type {Function}
433433
* @param {Object} obj - struct instance
434-
* @throws {TypeError} must provide a `struct` instace
434+
* @throws {TypeError} must provide a `struct` instance
435435
* @returns {DataView} view of underlying byte buffer
436436
*/
437437
setReadOnly( Struct, 'viewOf', function viewOf( obj ) {
438438
var buf;
439-
if ( !isStruct( obj ) ) {
439+
if ( !isStructInstance( obj ) ) {
440440
throw new TypeError( format( 'invalid argument. First argument must be a `struct` instance. Value: `%s`.', obj ) );
441441
}
442442
buf = obj[ PRIVATE_BUFFER ];

lib/node_modules/@stdlib/dstructs/struct/lib/normalize_field.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var join = require( '@stdlib/array/base/join' );
2525
var constantFunction = require( '@stdlib/utils/constant-function' );
2626
var format = require( '@stdlib/string/format' );
2727
var hasProperties = require( './has_properties.js' );
28-
var isStruct = require( './is_struct.js' );
28+
var isStructType = require( './is_struct_type.js' );
2929
var isValidNonEmptyString = require( './is_valid_nonempty_string.js' );
3030
var isValidString = require( './is_valid_string.js' );
3131
var isValidPositiveInteger = require( './is_valid_positive_integer.js' );
@@ -89,10 +89,10 @@ function normalize( obj, keys ) {
8989
if ( !hasProperties( out, MANDATORY_FIELD_NAMES ) ) {
9090
return new TypeError( format( 'invalid argument. Field objects must have the following properties: "%s". Value: `%s`.', join( MANDATORY_FIELD_NAMES, ', ' ), JSON.stringify( obj ) ) );
9191
}
92-
out.isStructType = isStruct( out.type );
92+
out.isStructType = isStructType( out.type );
9393
out.byteLength = byteLength( out );
9494
if ( out.isStructType ) {
95-
out.alignment = out.type.constructor.alignment;
95+
out.alignment = out.type.alignment;
9696
} else {
9797
out.alignment = ALIGNMENTS[ out.type ];
9898
}

lib/node_modules/@stdlib/dstructs/struct/lib/set_struct.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var Uint8Array = require( '@stdlib/array/uint8' );
2626
var gcopy = require( '@stdlib/blas/base/gcopy' );
2727
var format = require( '@stdlib/string/format' );
2828
var PRIVATE_BUFFER = require( './private_buffer.js' );
29-
var isStruct = require( './is_struct.js' );
29+
var isStructInstance = require( './is_struct_instance.js' );
3030

3131

3232
// MAIN //
@@ -56,7 +56,7 @@ function setStruct( obj ) {
5656
var src;
5757
var buf;
5858
var nb;
59-
if ( !isStruct( value ) ) {
59+
if ( !isStructInstance( value ) ) {
6060
throw new TypeError( format( 'invalid assignment. `%s` must be a `struct` instance. Value: `%s`.', obj.name, value ) );
6161
}
6262
if ( obj.casting === 'none' && !( value instanceof obj.type ) ) {

lib/node_modules/@stdlib/dstructs/struct/lib/set_typedarray.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var gfill = require( '@stdlib/blas/ext/base/gfill' );
3636
var map = require( '@stdlib/array/base/map' );
3737
var format = require( '@stdlib/string/format' );
3838
var PRIVATE_BUFFER = require( './private_buffer.js' );
39-
var isStruct = require( './is_struct.js' );
39+
var isStructInstance = require( './is_struct_instance.js' );
4040
var number2boolean = require( './number2boolean.js' );
4141
var complex2boolean = require( './complex2boolean.js' );
4242
var complex2number = require( './complex2number.js' );
@@ -67,7 +67,7 @@ function setTypedArray( obj ) {
6767
var view;
6868
var buf;
6969
var dt;
70-
if ( !isCollection( value ) || isStruct( value ) ) {
70+
if ( !isCollection( value ) || isStructInstance( value ) ) {
7171
throw new TypeError( format( 'invalid assignment. `%s` must be an array-like object. Value: `%s`.', obj.name, value ) );
7272
}
7373
if ( value.length !== obj.length ) {

lib/node_modules/@stdlib/dstructs/struct/lib/to_json.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var isCollection = require( '@stdlib/assert/is-collection' );
2424
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
2525
var isFunction = require( '@stdlib/assert/is-function' );
2626
var typedarray2json = require( '@stdlib/array/to-json' );
27-
var isStruct = require( './is_struct.js' );
27+
var isStructInstance = require( './is_struct_instance.js' );
2828

2929

3030
// MAIN //
@@ -49,7 +49,7 @@ function toJSON( struct, fields ) {
4949
v = struct[ o.name ];
5050
if ( isCollection( v ) ) {
5151
v = typedarray2json( v );
52-
} else if ( isStruct( v ) ) {
52+
} else if ( isStructInstance( v ) ) {
5353
v = v.toJSON();
5454
} else if ( isComplexLike( v ) && isFunction( v.toJSON ) ) {
5555
v = v.toJSON();

0 commit comments

Comments
 (0)