-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
109d75d
commit 58ee818
Showing
13 changed files
with
633 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<!-- | ||
@license Apache-2.0 | ||
Copyright (c) 2024 The Stdlib Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
# isWebAssemblyMemory | ||
|
||
> Test if a value is a WebAssembly [memory][@stdlib/wasm/memory] instance. | ||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var isWebAssemblyMemory = require( '@stdlib/assert/is-wasm-memory' ); | ||
``` | ||
|
||
#### isWebAssemblyMemory( value ) | ||
|
||
Tests if a value is a WebAssembly [memory][@stdlib/wasm/memory] instance. | ||
|
||
```javascript | ||
var Memory = require( '@stdlib/wasm/memory' ); | ||
|
||
var mem = new Memory({ | ||
'initial': 0 | ||
}); | ||
var bool = isWebAssemblyMemory( mem ); | ||
// returns true | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var Uint8Array = require( '@stdlib/array/uint8' ); | ||
var ArrayBuffer = require( '@stdlib/array/buffer' ); | ||
var Memory = require( '@stdlib/wasm/memory' ); | ||
var isWebAssemblyMemory = require( '@stdlib/assert/is-wasm-memory' ); | ||
|
||
var mem = new Memory({ | ||
'initial': 0 | ||
}); | ||
var bool = isWebAssemblyMemory( mem ); | ||
// returns true | ||
|
||
bool = isWebAssemblyMemory( new Uint8Array( 10 ) ); | ||
// returns false | ||
|
||
bool = isWebAssemblyMemory( new ArrayBuffer( 10 ) ); | ||
// returns false | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[@stdlib/wasm/memory]: https://github.com/stdlib-js/wasm-memory | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var Uint8Array = require( '@stdlib/array/uint8' ); | ||
var ArrayBuffer = require( '@stdlib/array/buffer' ); | ||
var Memory = require( '@stdlib/wasm/memory' ); | ||
var hasWebAssemblySupport = require( './../../has-wasm-support' ); | ||
var isBoolean = require( './../../is-boolean' ).isPrimitive; | ||
var pkg = require( './../package.json' ).name; | ||
var isWebAssemblyMemory = require( './../lib' ); | ||
|
||
|
||
// VARIABLES // | ||
|
||
var opts = { | ||
'skip': !hasWebAssemblySupport() | ||
}; | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg+'::true', opts, function benchmark( b ) { | ||
var values; | ||
var bool; | ||
var o; | ||
var i; | ||
|
||
o = { | ||
'initial': 0 | ||
}; | ||
|
||
values = [ | ||
new Memory( o ), | ||
new Memory( o ) | ||
]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
bool = isWebAssemblyMemory( values[ i%values.length ] ); | ||
if ( typeof bool !== 'boolean' ) { | ||
b.fail( 'should return a boolean' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isBoolean( bool ) ) { | ||
b.fail( 'should return a boolean' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); | ||
|
||
bench( pkg+'::false', function benchmark( b ) { | ||
var values; | ||
var bool; | ||
var i; | ||
|
||
values = [ | ||
new Uint8Array( 10 ), | ||
new ArrayBuffer( 10 ) | ||
]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
bool = isWebAssemblyMemory( values[ i%values.length ] ); | ||
if ( typeof bool !== 'boolean' ) { | ||
b.fail( 'should return a boolean' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isBoolean( bool ) ) { | ||
b.fail( 'should return a boolean' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
{{alias}}( value ) | ||
Tests if a value is a WebAssembly memory instance. | ||
|
||
Parameters | ||
---------- | ||
value: any | ||
Value to test. | ||
|
||
Returns | ||
------- | ||
bool: boolean | ||
Boolean indicating whether value is a WebAssembly memory instance. | ||
|
||
Examples | ||
-------- | ||
> var bool = {{alias}}( {} ) | ||
false | ||
|
||
See Also | ||
-------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/** | ||
* Tests if a value is a WebAssembly memory instance. | ||
* | ||
* @param value - value to test | ||
* @returns boolean indicating whether value is a WebAssembly memory instance | ||
* | ||
* @example | ||
* var Memory = require( '@stdlib/wasm/memory' ); | ||
* | ||
* var mem = new Memory({ | ||
* 'initial': 0 | ||
* }); | ||
* var bool = isWebAssemblyMemory( mem ); | ||
* // returns true | ||
* | ||
* @example | ||
* var bool = isWebAssemblyMemory( [] ); | ||
* // returns false | ||
*/ | ||
declare function isWebAssemblyMemory( value: any ): boolean; // Note: WebAssembly.Memory is not available in older JavaScript environments | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = isWebAssemblyMemory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import isWebAssemblyMemory = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a boolean... | ||
{ | ||
isWebAssemblyMemory( [] ); // $ExpectType boolean | ||
} | ||
|
||
// The compiler throws an error if the function is provided an unsupported number of arguments... | ||
{ | ||
isWebAssemblyMemory(); // $ExpectError | ||
isWebAssemblyMemory( [], 123 ); // $ExpectError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var hasWebAssemblySupport = require( './../../has-wasm-support' ); | ||
var Uint8Array = require( '@stdlib/array/uint8' ); | ||
var ArrayBuffer = require( '@stdlib/array/buffer' ); | ||
var Memory = require( '@stdlib/wasm/memory' ); | ||
var isWebAssemblyMemory = require( './../lib' ); | ||
|
||
var bool = isWebAssemblyMemory( new Uint8Array( 10 ) ); | ||
console.error( bool ); | ||
// => false | ||
|
||
bool = isWebAssemblyMemory( new ArrayBuffer( 10 ) ); | ||
console.error( bool ); | ||
// => false | ||
|
||
if ( hasWebAssemblySupport() ) { | ||
bool = isWebAssemblyMemory( new Memory({ | ||
'initial': 0 | ||
})); | ||
console.log( bool ); | ||
// => true | ||
} |
Oops, something went wrong.