Skip to content

Commit 6244124

Browse files
committed
Auto-generated commit
1 parent 3a243e0 commit 6244124

File tree

8 files changed

+114
-5
lines changed

8 files changed

+114
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ var arr = ndarray2array( y );
225225

226226
## Notes
227227

228+
- An output [`ndarray`][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function throws an error.
228229
- A **slice argument** must be either a [`Slice`][@stdlib/slice/ctor], an integer, `null`, or `undefined`.
229230
- The number of slice dimensions must match the number of output array dimensions. Hence, if `y` is a zero-dimensional [`ndarray`][@stdlib/ndarray/ctor], then, if `s` is a [`MultiSlice`][@stdlib/slice/multi], `s` should be empty, and, if `s` is an array, `s` should not contain any slice arguments. Similarly, if `y` is a one-dimensional [`ndarray`][@stdlib/ndarray/ctor], then, if `s` is a [`MultiSlice`][@stdlib/slice/multi], `s` should have one slice dimension, and, if `s` is an array, `s` should contain a single slice argument. And so on and so forth.
230231
- The input [`ndarray`][@stdlib/ndarray/ctor] **must** be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output [`ndarray`][@stdlib/ndarray/ctor] view.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/repl.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
assigned to corresponding elements in a 'float32' output array).
3535

3636
y: ndarray
37-
Output array.
37+
Output array. The output array must be writable.
3838

3939
s: ...MultiSlice|Slice|null|undefined|integer|ArrayLike
4040
Slice arguments.

lib/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var isMultiSlice = require( '@stdlib/assert-is-multi-slice' );
2525
var isArrayLikeObject = require( '@stdlib/assert-is-array-like-object' );
2626
var isPlainObject = require( '@stdlib/assert-is-plain-object' );
2727
var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;
28+
var isReadOnly = require( '@stdlib/ndarray-base-assert-is-read-only' );
2829
var hasOwnProp = require( '@stdlib/assert-has-own-property' );
2930
var MultiSlice = require( '@stdlib/slice-multi' );
3031
var base = require( '@stdlib/ndarray-base-slice-assign' );
@@ -52,6 +53,7 @@ var format = require( '@stdlib/string-format' );
5253
* @throws {RangeError} slice exceeds array bounds
5354
* @throws {Error} input array must be broadcast compatible with an output array view
5455
* @throws {TypeError} input array cannot be safely cast to the output array data type
56+
* @throws {Error} cannot write to a read-only ndarray
5557
* @returns {ndarray} output array
5658
*
5759
* @example
@@ -116,6 +118,9 @@ function sliceAssign( x, y, s ) {
116118
if ( !isndarrayLike( y ) ) {
117119
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray. Value: `%s`.', y ) );
118120
}
121+
if ( isReadOnly( y ) ) {
122+
throw new Error( 'invalid argument. Cannot write to a read-only array.' );
123+
}
119124
if ( isPlainObject( arguments[ nargs-1 ] ) ) {
120125
nargs -= 1;
121126
options = arguments[ nargs ];

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@stdlib/assert-is-multi-slice": "^0.1.0",
4444
"@stdlib/assert-is-ndarray-like": "^0.1.0",
4545
"@stdlib/assert-is-plain-object": "^0.1.0",
46+
"@stdlib/ndarray-base-assert-is-read-only": "^0.1.0",
4647
"@stdlib/ndarray-base-slice-assign": "github:stdlib-js/ndarray-base-slice-assign#main",
4748
"@stdlib/slice-multi": "^0.1.0",
4849
"@stdlib/string-format": "^0.1.0",

test/dist/test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,57 @@ tape( 'the function throws an error if provided a second argument which is not a
248248
}
249249
});
250250

251+
tape( 'the function throws an error if provided a read-only output array (multislice)', function test( t ) {
252+
var x;
253+
var y;
254+
255+
x = scalar2ndarray( 2.0, 'float64', 'row-major' );
256+
y = zeros( [ 2, 2 ], {
257+
'readonly': true
258+
});
259+
260+
t.throws( badValue, Error, 'throws an error' );
261+
t.end();
262+
263+
function badValue() {
264+
sliceAssign( x, y, new MultiSlice( null, null ) );
265+
}
266+
});
267+
268+
tape( 'the function throws an error if provided a read-only output array (array)', function test( t ) {
269+
var x;
270+
var y;
271+
272+
x = scalar2ndarray( 2.0, 'float64', 'row-major' );
273+
y = zeros( [ 2, 2 ], {
274+
'readonly': true
275+
});
276+
277+
t.throws( badValue, Error, 'throws an error' );
278+
t.end();
279+
280+
function badValue() {
281+
sliceAssign( x, y, [ null, null ] );
282+
}
283+
});
284+
285+
tape( 'the function throws an error if provided a read-only output array (slice arguments)', function test( t ) {
286+
var x;
287+
var y;
288+
289+
x = scalar2ndarray( 2.0, 'float64', 'row-major' );
290+
y = zeros( [ 2, 2 ], {
291+
'readonly': true
292+
});
293+
294+
t.throws( badValue, Error, 'throws an error' );
295+
t.end();
296+
297+
function badValue() {
298+
sliceAssign( x, y, null, null );
299+
}
300+
});
301+
251302
tape( 'the function throws an error if provided an options argument which is not an object (multislice)', function test( t ) {
252303
var values;
253304
var x;

test/test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,57 @@ tape( 'the function throws an error if provided a second argument which is not a
248248
}
249249
});
250250

251+
tape( 'the function throws an error if provided a read-only output array (multislice)', function test( t ) {
252+
var x;
253+
var y;
254+
255+
x = scalar2ndarray( 2.0, 'float64', 'row-major' );
256+
y = zeros( [ 2, 2 ], {
257+
'readonly': true
258+
});
259+
260+
t.throws( badValue, Error, 'throws an error' );
261+
t.end();
262+
263+
function badValue() {
264+
sliceAssign( x, y, new MultiSlice( null, null ) );
265+
}
266+
});
267+
268+
tape( 'the function throws an error if provided a read-only output array (array)', function test( t ) {
269+
var x;
270+
var y;
271+
272+
x = scalar2ndarray( 2.0, 'float64', 'row-major' );
273+
y = zeros( [ 2, 2 ], {
274+
'readonly': true
275+
});
276+
277+
t.throws( badValue, Error, 'throws an error' );
278+
t.end();
279+
280+
function badValue() {
281+
sliceAssign( x, y, [ null, null ] );
282+
}
283+
});
284+
285+
tape( 'the function throws an error if provided a read-only output array (slice arguments)', function test( t ) {
286+
var x;
287+
var y;
288+
289+
x = scalar2ndarray( 2.0, 'float64', 'row-major' );
290+
y = zeros( [ 2, 2 ], {
291+
'readonly': true
292+
});
293+
294+
t.throws( badValue, Error, 'throws an error' );
295+
t.end();
296+
297+
function badValue() {
298+
sliceAssign( x, y, null, null );
299+
}
300+
});
301+
251302
tape( 'the function throws an error if provided an options argument which is not an object (multislice)', function test( t ) {
252303
var values;
253304
var x;

0 commit comments

Comments
 (0)