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

0 commit comments

Comments
 (0)