JSTypedArray.h
1 /* 2 * Copyright (C) 2015 Dominic Szablewski (dominic@phoboslab.org) 3 * Copyright (C) 2015-2016 Apple Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef JSTypedArray_h 28 #define JSTypedArray_h 29 30 #include <JavaScriptCore/JSBase.h> 31 #include <JavaScriptCore/JSValueRef.h> 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 // ------------- Typed Array functions -------------- 38 39 /*! 40 @function 41 @abstract Creates a JavaScript Typed Array object with the given number of elements. 42 @param ctx The execution context to use. 43 @param arrayType A value identifying the type of array to create. If arrayType is kJSTypedArrayTypeNone or kJSTypedArrayTypeArrayBuffer then NULL will be returned. 44 @param length The number of elements to be in the new Typed Array. 45 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 46 @result A JSObjectRef that is a Typed Array with all elements set to zero or NULL if there was an error. 47 */ 48 JS_EXPORT JSObjectRef JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType arrayType, size_t length, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 49 50 /*! 51 @function 52 @abstract Creates a JavaScript Typed Array object from an existing pointer. 53 @param ctx The execution context to use. 54 @param arrayType A value identifying the type of array to create. If arrayType is kJSTypedArrayTypeNone or kJSTypedArrayTypeArrayBuffer then NULL will be returned. 55 @param bytes A pointer to the byte buffer to be used as the backing store of the Typed Array object. 56 @param byteLength The number of bytes pointed to by the parameter bytes. 57 @param bytesDeallocator The allocator to use to deallocate the external buffer when the JSTypedArrayData object is deallocated. 58 @param deallocatorContext A pointer to pass back to the deallocator. 59 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 60 @result A JSObjectRef Typed Array whose backing store is the same as the one pointed to by bytes or NULL if there was an error. 61 @discussion If an exception is thrown during this function the bytesDeallocator will always be called. 62 */ 63 JS_EXPORT JSObjectRef JSObjectMakeTypedArrayWithBytesNoCopy(JSContextRef ctx, JSTypedArrayType arrayType, void* bytes, size_t byteLength, JSTypedArrayBytesDeallocator bytesDeallocator, void* deallocatorContext, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 64 65 /*! 66 @function 67 @abstract Creates a JavaScript Typed Array object from an existing JavaScript Array Buffer object. 68 @param ctx The execution context to use. 69 @param arrayType A value identifying the type of array to create. If arrayType is kJSTypedArrayTypeNone or kJSTypedArrayTypeArrayBuffer then NULL will be returned. 70 @param buffer An Array Buffer object that should be used as the backing store for the created JavaScript Typed Array object. 71 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 72 @result A JSObjectRef that is a Typed Array or NULL if there was an error. The backing store of the Typed Array will be buffer. 73 */ 74 JS_EXPORT JSObjectRef JSObjectMakeTypedArrayWithArrayBuffer(JSContextRef ctx, JSTypedArrayType arrayType, JSObjectRef buffer, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 75 76 /*! 77 @function 78 @abstract Creates a JavaScript Typed Array object from an existing JavaScript Array Buffer object with the given offset and length. 79 @param ctx The execution context to use. 80 @param arrayType A value identifying the type of array to create. If arrayType is kJSTypedArrayTypeNone or kJSTypedArrayTypeArrayBuffer then NULL will be returned. 81 @param buffer An Array Buffer object that should be used as the backing store for the created JavaScript Typed Array object. 82 @param byteOffset The byte offset for the created Typed Array. byteOffset should aligned with the element size of arrayType. 83 @param length The number of elements to include in the Typed Array. 84 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 85 @result A JSObjectRef that is a Typed Array or NULL if there was an error. The backing store of the Typed Array will be buffer. 86 */ 87 JS_EXPORT JSObjectRef JSObjectMakeTypedArrayWithArrayBufferAndOffset(JSContextRef ctx, JSTypedArrayType arrayType, JSObjectRef buffer, size_t byteOffset, size_t length, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 88 89 /*! 90 @function 91 @abstract Returns a temporary pointer to the backing store of a JavaScript Typed Array object. 92 @param ctx The execution context to use. 93 @param object The Typed Array object whose backing store pointer to return. 94 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 95 @result A pointer to the raw data buffer that serves as object's backing store or NULL if object is not a Typed Array object. 96 @discussion The pointer returned by this function is temporary and is not guaranteed to remain valid across JavaScriptCore API calls. 97 */ 98 JS_EXPORT void* JSObjectGetTypedArrayBytesPtr(JSContextRef ctx, JSObjectRef object, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 99 100 /*! 101 @function 102 @abstract Returns the length of a JavaScript Typed Array object. 103 @param ctx The execution context to use. 104 @param object The Typed Array object whose length to return. 105 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 106 @result The length of the Typed Array object or 0 if the object is not a Typed Array object. 107 */ 108 JS_EXPORT size_t JSObjectGetTypedArrayLength(JSContextRef ctx, JSObjectRef object, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 109 110 /*! 111 @function 112 @abstract Returns the byte length of a JavaScript Typed Array object. 113 @param ctx The execution context to use. 114 @param object The Typed Array object whose byte length to return. 115 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 116 @result The byte length of the Typed Array object or 0 if the object is not a Typed Array object. 117 */ 118 JS_EXPORT size_t JSObjectGetTypedArrayByteLength(JSContextRef ctx, JSObjectRef object, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 119 120 /*! 121 @function 122 @abstract Returns the byte offset of a JavaScript Typed Array object. 123 @param ctx The execution context to use. 124 @param object The Typed Array object whose byte offset to return. 125 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 126 @result The byte offset of the Typed Array object or 0 if the object is not a Typed Array object. 127 */ 128 JS_EXPORT size_t JSObjectGetTypedArrayByteOffset(JSContextRef ctx, JSObjectRef object, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 129 130 /*! 131 @function 132 @abstract Returns the JavaScript Array Buffer object that is used as the backing of a JavaScript Typed Array object. 133 @param ctx The execution context to use. 134 @param object The JSObjectRef whose Typed Array type data pointer to obtain. 135 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 136 @result A JSObjectRef with a JSTypedArrayType of kJSTypedArrayTypeArrayBuffer or NULL if object is not a Typed Array. 137 */ 138 JS_EXPORT JSObjectRef JSObjectGetTypedArrayBuffer(JSContextRef ctx, JSObjectRef object, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 139 140 // ------------- Array Buffer functions ------------- 141 142 /*! 143 @function 144 @abstract Creates a JavaScript Array Buffer object from an existing pointer. 145 @param ctx The execution context to use. 146 @param bytes A pointer to the byte buffer to be used as the backing store of the Typed Array object. 147 @param byteLength The number of bytes pointed to by the parameter bytes. 148 @param bytesDeallocator The allocator to use to deallocate the external buffer when the Typed Array data object is deallocated. 149 @param deallocatorContext A pointer to pass back to the deallocator. 150 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 151 @result A JSObjectRef Array Buffer whose backing store is the same as the one pointed to by bytes or NULL if there was an error. 152 @discussion If an exception is thrown during this function the bytesDeallocator will always be called. 153 */ 154 JS_EXPORT JSObjectRef JSObjectMakeArrayBufferWithBytesNoCopy(JSContextRef ctx, void* bytes, size_t byteLength, JSTypedArrayBytesDeallocator bytesDeallocator, void* deallocatorContext, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 155 156 /*! 157 @function 158 @abstract Returns a pointer to the data buffer that serves as the backing store for a JavaScript Typed Array object. 159 @param object The Array Buffer object whose internal backing store pointer to return. 160 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 161 @result A pointer to the raw data buffer that serves as object's backing store or NULL if object is not an Array Buffer object. 162 @discussion The pointer returned by this function is temporary and is not guaranteed to remain valid across JavaScriptCore API calls. 163 */ 164 JS_EXPORT void* JSObjectGetArrayBufferBytesPtr(JSContextRef ctx, JSObjectRef object, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 165 166 /*! 167 @function 168 @abstract Returns the number of bytes in a JavaScript data object. 169 @param ctx The execution context to use. 170 @param object The JS Arary Buffer object whose length in bytes to return. 171 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 172 @result The number of bytes stored in the data object. 173 */ 174 JS_EXPORT size_t JSObjectGetArrayBufferByteLength(JSContextRef ctx, JSObjectRef object, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); 175 176 #ifdef __cplusplus 177 } 178 #endif 179 180 #endif /* JSTypedArray_h */