Skip to content

Commit 718ba36

Browse files
committed
feat: add assert/is-wasm-memory
1 parent 190c7b4 commit 718ba36

File tree

10 files changed

+596
-0
lines changed

10 files changed

+596
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isWebAssemblyMemory
22+
23+
> Test if a value is a WebAssembly [memory][@stdlib/wasm/memory] instance.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isWebAssemblyMemory = require( '@stdlib/assert/is-wasm-memory' );
31+
```
32+
33+
#### isWebAssemblyMemory( value )
34+
35+
Tests if a value is a WebAssembly [memory][@stdlib/wasm/memory] instance.
36+
37+
```javascript
38+
var Memory = require( '@stdlib/wasm/memory' );
39+
40+
var mem = new Memory({
41+
'initial': 0
42+
});
43+
var bool = isWebAssemblyMemory( mem );
44+
// returns true
45+
```
46+
47+
</section>
48+
49+
<!-- /.usage -->
50+
51+
<section class="examples">
52+
53+
## Examples
54+
55+
<!-- eslint no-undef: "error" -->
56+
57+
```javascript
58+
var Uint8Array = require( '@stdlib/array/uint8' );
59+
var ArrayBuffer = require( '@stdlib/array/buffer' );
60+
var Memory = require( '@stdlib/wasm/memory' );
61+
var isWebAssemblyMemory = require( '@stdlib/assert/is-wasm-memory' );
62+
63+
var mem = new Memory({
64+
'initial': 0
65+
});
66+
var bool = isWebAssemblyMemory( mem );
67+
// returns true
68+
69+
bool = isWebAssemblyMemory( new Uint8Array( 10 ) );
70+
// returns false
71+
72+
bool = isWebAssemblyMemory( new ArrayBuffer( 10 ) );
73+
// returns false
74+
```
75+
76+
</section>
77+
78+
<!-- /.examples -->
79+
80+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
81+
82+
<section class="related">
83+
84+
</section>
85+
86+
<!-- /.related -->
87+
88+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
89+
90+
<section class="links">
91+
92+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
93+
94+
</section>
95+
96+
<!-- /.links -->
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var Uint8Array = require( '@stdlib/array/uint8' );
25+
var ArrayBuffer = require( '@stdlib/array/buffer' );
26+
var Memory = require( '@stdlib/wasm/memory' );
27+
var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
28+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
29+
var pkg = require( './../package.json' ).name;
30+
var isWebAssemblyMemory = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var opts = {
36+
'skip': !hasWebAssemblySupport()
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::true', opts, function benchmark( b ) {
43+
var values;
44+
var bool;
45+
var o;
46+
var i;
47+
48+
o = {
49+
'initial': 0
50+
};
51+
52+
values = [
53+
new Memory( o ),
54+
new Memory( o )
55+
];
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
bool = isWebAssemblyMemory( values[ i%values.length ] );
60+
if ( typeof bool !== 'boolean' ) {
61+
b.fail( 'should return a boolean' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isBoolean( bool ) ) {
66+
b.fail( 'should return a boolean' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
71+
72+
bench( pkg+'::false', function benchmark( b ) {
73+
var values;
74+
var bool;
75+
var i;
76+
77+
values = [
78+
new Uint8Array( 10 ),
79+
new ArrayBuffer( 10 )
80+
];
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
bool = isWebAssemblyMemory( values[ i%values.length ] );
85+
if ( typeof bool !== 'boolean' ) {
86+
b.fail( 'should return a boolean' );
87+
}
88+
}
89+
b.toc();
90+
if ( !isBoolean( bool ) ) {
91+
b.fail( 'should return a boolean' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{{alias}}( value )
3+
Tests if a value is a WebAssembly memory instance.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating whether value is a WebAssembly memory instance.
14+
15+
Examples
16+
--------
17+
> var bool = {{alias}}( {} )
18+
false
19+
20+
See Also
21+
--------
22+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Tests if a value is a WebAssembly memory instance.
23+
*
24+
* @param value - value to test
25+
* @returns boolean indicating whether value is a WebAssembly memory instance
26+
*
27+
* @example
28+
* var Memory = require( '@stdlib/wasm/memory' );
29+
*
30+
* var mem = new Memory({
31+
* 'initial': 0
32+
* });
33+
* var bool = isWebAssemblyMemory( mem );
34+
* // returns true
35+
*
36+
* @example
37+
* var bool = isWebAssemblyMemory( [] );
38+
* // returns false
39+
*/
40+
declare function isWebAssemblyMemory( value: any ): boolean; // Note: WebAssembly.Memory is not available in older JavaScript environments
41+
42+
43+
// EXPORTS //
44+
45+
export = isWebAssemblyMemory;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
import isWebAssemblyMemory = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isWebAssemblyMemory( [] ); // $ExpectType boolean
27+
}
28+
29+
// The compiler throws an error if the function is provided an unsupported number of arguments...
30+
{
31+
isWebAssemblyMemory(); // $ExpectError
32+
isWebAssemblyMemory( [], 123 ); // $ExpectError
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
22+
var Uint8Array = require( '@stdlib/array/uint8' );
23+
var ArrayBuffer = require( '@stdlib/array/buffer' );
24+
var Memory = require( '@stdlib/wasm/memory' );
25+
var isWebAssemblyMemory = require( './../lib' );
26+
27+
var bool = isWebAssemblyMemory( new Uint8Array( 10 ) );
28+
console.error( bool );
29+
// => false
30+
31+
bool = isWebAssemblyMemory( new ArrayBuffer( 10 ) );
32+
console.error( bool );
33+
// => false
34+
35+
if ( hasWebAssemblySupport() ) {
36+
bool = isWebAssemblyMemory( new Memory({
37+
'initial': 0
38+
}));
39+
console.log( bool );
40+
// => true
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
/**
22+
* Test if a value is a WebAssembly memory instance.
23+
*
24+
* @module @stdlib/assert/is-wasm-memory
25+
*
26+
* @example
27+
* var Memory = require( '@stdlib/wasm/memory' );
28+
* var isWebAssemblyMemory = require( '@stdlib/assert/is-wasm-memory' );
29+
*
30+
* var mem = new Memory({
31+
* 'initial': 0
32+
* });
33+
* var bool = isWebAssemblyMemory( mem );
34+
* // returns true
35+
*
36+
* bool = isWebAssemblyMemory( [] );
37+
* // returns false
38+
*/
39+
40+
// MODULES //
41+
42+
var isWebAssemblyMemory = require( './main.js' );
43+
44+
45+
// EXPORTS //
46+
47+
module.exports = isWebAssemblyMemory;

0 commit comments

Comments
 (0)