README.md (6821B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # SharedArrayBuffer 22 23 > [Constructor][mdn-sharedarraybuffer] returning an object used to represent a generic, fixed-length raw binary data buffer which can be used to create views of shared memory. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var SharedArrayBuffer = require( '@stdlib/array/shared-buffer' ); 41 ``` 42 43 #### SharedArrayBuffer( size ) 44 45 Returns a [`SharedArrayBuffer`][mdn-sharedarraybuffer] having a specified number of bytes. 46 47 <!-- eslint-disable stdlib/require-globals, no-unused-vars, no-inner-declarations --> 48 49 ```javascript 50 try { 51 var buf = new SharedArrayBuffer( 5 ); 52 // returns <SharedArrayBuffer> 53 } catch ( err ) { 54 console.log( 'Environment does not support SharedArrayBuffers.' ); 55 } 56 ``` 57 58 * * * 59 60 ### Properties 61 62 #### SharedArrayBuffer.length 63 64 Number of input arguments the constructor accepts. 65 66 <!-- eslint-disable stdlib/require-globals --> 67 68 ```javascript 69 var len = SharedArrayBuffer.length; 70 // returns 1 71 ``` 72 73 #### SharedArrayBuffer.prototype.byteLength 74 75 **Read-only** property which returns the length (in bytes) of the [`SharedArrayBuffer`][mdn-sharedarraybuffer]. 76 77 <!-- eslint-disable stdlib/require-globals, no-unused-vars, no-inner-declarations --> 78 79 ```javascript 80 try { 81 var buf = new SharedArrayBuffer( 5 ); 82 var byteLength = buf.byteLength; 83 // returns 5 84 } catch ( err ) { 85 console.log( 'Environment does not support SharedArrayBuffers.' ); 86 } 87 ``` 88 89 * * * 90 91 ### Methods 92 93 #### SharedArrayBuffer.prototype.slice( \[start\[, end]] ) 94 95 Copies the bytes of a [`SharedArrayBuffer`][mdn-sharedarraybuffer] to a new [`SharedArrayBuffer`][mdn-sharedarraybuffer]. 96 97 <!-- eslint-disable stdlib/require-globals, no-unused-vars, no-inner-declarations --> 98 99 ```javascript 100 try { 101 var b1 = new SharedArrayBuffer( 10 ); 102 103 var b2 = b1.slice(); 104 // returns <SharedArrayBuffer> 105 106 var bool = ( b2 === b1 ); 107 // returns false 108 } catch ( err ) { 109 console.log( 'Environment does not support SharedArrayBuffers.' ); 110 } 111 ``` 112 113 By default, the method copies from the beginning of the [`SharedArrayBuffer`][mdn-sharedarraybuffer]. To beginning copying from a different byte index, provide a `start` argument, specifying the starting byte index (inclusive). 114 115 <!-- eslint-disable stdlib/require-globals, no-unused-vars, no-inner-declarations --> 116 117 ```javascript 118 try { 119 var b1 = new SharedArrayBuffer( 10 ); 120 var b2 = b1.slice( 2 ); 121 122 var nbytes = b2.byteLength; 123 // returns 8 124 } catch ( err ) { 125 console.log( 'Environment does not support SharedArrayBuffers.' ); 126 } 127 ``` 128 129 If `start < 0`, the index is relative to the end of the [`SharedArrayBuffer`][mdn-sharedarraybuffer]. 130 131 <!-- eslint-disable stdlib/require-globals, no-unused-vars, no-inner-declarations --> 132 133 ```javascript 134 try { 135 var b1 = new SharedArrayBuffer( 10 ); 136 var b2 = b1.slice( -2 ); 137 138 var nbytes = b2.byteLength; 139 // returns 2 140 } catch ( err ) { 141 console.log( 'Environment does not support SharedArrayBuffers.' ); 142 } 143 ``` 144 145 By default, the method copies to the end of the [`SharedArrayBuffer`][mdn-sharedarraybuffer]. To copy until a particular byte index, provide an `end` index, specifying the ending byte index (exclusive). 146 147 <!-- eslint-disable stdlib/require-globals, no-unused-vars, no-inner-declarations --> 148 149 ```javascript 150 try { 151 var b1 = new SharedArrayBuffer( 10 ); 152 var b2 = b1.slice( 2, 6 ); 153 154 var nbytes = b2.byteLength; 155 // returns 4 156 } catch ( err ) { 157 console.log( 'Environment does not support SharedArrayBuffers.' ); 158 } 159 ``` 160 161 If `end < 0`, the index is relative to the end of the [`SharedArrayBuffer`][mdn-sharedarraybuffer]. 162 163 <!-- eslint-disable stdlib/require-globals, no-unused-vars, no-inner-declarations --> 164 165 ```javascript 166 try { 167 var b1 = new SharedArrayBuffer( 10 ); 168 var b2 = b1.slice( 2, -2 ); 169 170 var nbytes = b2.byteLength; 171 // returns 6 172 } catch ( err ) { 173 console.log( 'Environment does not support SharedArrayBuffers.' ); 174 } 175 ``` 176 177 </section> 178 179 <!-- /.usage --> 180 181 * * * 182 183 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 184 185 <section class="notes"> 186 187 </section> 188 189 <!-- /.notes --> 190 191 <!-- Package usage examples. --> 192 193 <section class="examples"> 194 195 ## Examples 196 197 <!-- TODO: update example to show explicit threading use case --> 198 199 <!-- eslint no-undef: "error" --> 200 201 <!-- eslint-disable no-unused-vars --> 202 203 ```javascript 204 var randu = require( '@stdlib/random/base/randu' ); 205 var Float64Array = require( '@stdlib/array/float64' ); 206 var Uint8Array = require( '@stdlib/array/uint8' ); 207 var toBinaryString = require( '@stdlib/number/uint8/base/to-binary-string' ); 208 var SharedArrayBuffer = require( '@stdlib/array/shared-buffer' ); 209 210 function main() { 211 var bytes; 212 var buf; 213 var arr; 214 var i; 215 216 // Create a new SharedArrayBuffer: 217 buf = new SharedArrayBuffer( 64 ); 218 219 // Create a Float64 array buffer view: 220 arr = new Float64Array( buf.byteLength/8 ); 221 for ( i = 0; i < arr.length; i++ ) { 222 arr[ i ] = randu() * 100.0; 223 } 224 225 // Create a "bytes" view of the array buffer: 226 bytes = new Uint8Array( arr.buffer ); 227 228 // Print the bytes: 229 for ( i = 0; i < bytes.length; i++ ) { 230 console.log( 'byte %d: %s', i, toBinaryString( bytes[ i ] ) ); 231 } 232 } 233 234 try { 235 main(); 236 } catch ( err ) { 237 console.error( 'Environment does not provide SharedArrayBuffer support.' ); 238 } 239 ``` 240 241 </section> 242 243 <!-- /.examples --> 244 245 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 246 247 <section class="references"> 248 249 </section> 250 251 <!-- /.references --> 252 253 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 254 255 <section class="links"> 256 257 [mdn-sharedarraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer 258 259 </section> 260 261 <!-- /.links -->