Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

typed-array-constructor.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var global = require('../internals/global');
  4. var DESCRIPTORS = require('../internals/descriptors');
  5. var TYPED_ARRAYS_CONSTRUCTORS_REQUIRES_WRAPPERS = require('../internals/typed-array-constructors-require-wrappers');
  6. var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
  7. var ArrayBufferModule = require('../internals/array-buffer');
  8. var anInstance = require('../internals/an-instance');
  9. var createPropertyDescriptor = require('../internals/create-property-descriptor');
  10. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  11. var toLength = require('../internals/to-length');
  12. var toIndex = require('../internals/to-index');
  13. var toOffset = require('../internals/to-offset');
  14. var toPrimitive = require('../internals/to-primitive');
  15. var has = require('../internals/has');
  16. var classof = require('../internals/classof');
  17. var isObject = require('../internals/is-object');
  18. var create = require('../internals/object-create');
  19. var setPrototypeOf = require('../internals/object-set-prototype-of');
  20. var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
  21. var typedArrayFrom = require('../internals/typed-array-from');
  22. var forEach = require('../internals/array-iteration').forEach;
  23. var setSpecies = require('../internals/set-species');
  24. var definePropertyModule = require('../internals/object-define-property');
  25. var getOwnPropertyDescriptorModule = require('../internals/object-get-own-property-descriptor');
  26. var InternalStateModule = require('../internals/internal-state');
  27. var inheritIfRequired = require('../internals/inherit-if-required');
  28. var getInternalState = InternalStateModule.get;
  29. var setInternalState = InternalStateModule.set;
  30. var nativeDefineProperty = definePropertyModule.f;
  31. var nativeGetOwnPropertyDescriptor = getOwnPropertyDescriptorModule.f;
  32. var round = Math.round;
  33. var RangeError = global.RangeError;
  34. var ArrayBuffer = ArrayBufferModule.ArrayBuffer;
  35. var DataView = ArrayBufferModule.DataView;
  36. var NATIVE_ARRAY_BUFFER_VIEWS = ArrayBufferViewCore.NATIVE_ARRAY_BUFFER_VIEWS;
  37. var TYPED_ARRAY_TAG = ArrayBufferViewCore.TYPED_ARRAY_TAG;
  38. var TypedArray = ArrayBufferViewCore.TypedArray;
  39. var TypedArrayPrototype = ArrayBufferViewCore.TypedArrayPrototype;
  40. var aTypedArrayConstructor = ArrayBufferViewCore.aTypedArrayConstructor;
  41. var isTypedArray = ArrayBufferViewCore.isTypedArray;
  42. var BYTES_PER_ELEMENT = 'BYTES_PER_ELEMENT';
  43. var WRONG_LENGTH = 'Wrong length';
  44. var fromList = function (C, list) {
  45. var index = 0;
  46. var length = list.length;
  47. var result = new (aTypedArrayConstructor(C))(length);
  48. while (length > index) result[index] = list[index++];
  49. return result;
  50. };
  51. var addGetter = function (it, key) {
  52. nativeDefineProperty(it, key, { get: function () {
  53. return getInternalState(this)[key];
  54. } });
  55. };
  56. var isArrayBuffer = function (it) {
  57. var klass;
  58. return it instanceof ArrayBuffer || (klass = classof(it)) == 'ArrayBuffer' || klass == 'SharedArrayBuffer';
  59. };
  60. var isTypedArrayIndex = function (target, key) {
  61. return isTypedArray(target)
  62. && typeof key != 'symbol'
  63. && key in target
  64. && String(+key) == String(key);
  65. };
  66. var wrappedGetOwnPropertyDescriptor = function getOwnPropertyDescriptor(target, key) {
  67. return isTypedArrayIndex(target, key = toPrimitive(key, true))
  68. ? createPropertyDescriptor(2, target[key])
  69. : nativeGetOwnPropertyDescriptor(target, key);
  70. };
  71. var wrappedDefineProperty = function defineProperty(target, key, descriptor) {
  72. if (isTypedArrayIndex(target, key = toPrimitive(key, true))
  73. && isObject(descriptor)
  74. && has(descriptor, 'value')
  75. && !has(descriptor, 'get')
  76. && !has(descriptor, 'set')
  77. // TODO: add validation descriptor w/o calling accessors
  78. && !descriptor.configurable
  79. && (!has(descriptor, 'writable') || descriptor.writable)
  80. && (!has(descriptor, 'enumerable') || descriptor.enumerable)
  81. ) {
  82. target[key] = descriptor.value;
  83. return target;
  84. } return nativeDefineProperty(target, key, descriptor);
  85. };
  86. if (DESCRIPTORS) {
  87. if (!NATIVE_ARRAY_BUFFER_VIEWS) {
  88. getOwnPropertyDescriptorModule.f = wrappedGetOwnPropertyDescriptor;
  89. definePropertyModule.f = wrappedDefineProperty;
  90. addGetter(TypedArrayPrototype, 'buffer');
  91. addGetter(TypedArrayPrototype, 'byteOffset');
  92. addGetter(TypedArrayPrototype, 'byteLength');
  93. addGetter(TypedArrayPrototype, 'length');
  94. }
  95. $({ target: 'Object', stat: true, forced: !NATIVE_ARRAY_BUFFER_VIEWS }, {
  96. getOwnPropertyDescriptor: wrappedGetOwnPropertyDescriptor,
  97. defineProperty: wrappedDefineProperty
  98. });
  99. module.exports = function (TYPE, wrapper, CLAMPED) {
  100. var BYTES = TYPE.match(/\d+$/)[0] / 8;
  101. var CONSTRUCTOR_NAME = TYPE + (CLAMPED ? 'Clamped' : '') + 'Array';
  102. var GETTER = 'get' + TYPE;
  103. var SETTER = 'set' + TYPE;
  104. var NativeTypedArrayConstructor = global[CONSTRUCTOR_NAME];
  105. var TypedArrayConstructor = NativeTypedArrayConstructor;
  106. var TypedArrayConstructorPrototype = TypedArrayConstructor && TypedArrayConstructor.prototype;
  107. var exported = {};
  108. var getter = function (that, index) {
  109. var data = getInternalState(that);
  110. return data.view[GETTER](index * BYTES + data.byteOffset, true);
  111. };
  112. var setter = function (that, index, value) {
  113. var data = getInternalState(that);
  114. if (CLAMPED) value = (value = round(value)) < 0 ? 0 : value > 0xFF ? 0xFF : value & 0xFF;
  115. data.view[SETTER](index * BYTES + data.byteOffset, value, true);
  116. };
  117. var addElement = function (that, index) {
  118. nativeDefineProperty(that, index, {
  119. get: function () {
  120. return getter(this, index);
  121. },
  122. set: function (value) {
  123. return setter(this, index, value);
  124. },
  125. enumerable: true
  126. });
  127. };
  128. if (!NATIVE_ARRAY_BUFFER_VIEWS) {
  129. TypedArrayConstructor = wrapper(function (that, data, offset, $length) {
  130. anInstance(that, TypedArrayConstructor, CONSTRUCTOR_NAME);
  131. var index = 0;
  132. var byteOffset = 0;
  133. var buffer, byteLength, length;
  134. if (!isObject(data)) {
  135. length = toIndex(data);
  136. byteLength = length * BYTES;
  137. buffer = new ArrayBuffer(byteLength);
  138. } else if (isArrayBuffer(data)) {
  139. buffer = data;
  140. byteOffset = toOffset(offset, BYTES);
  141. var $len = data.byteLength;
  142. if ($length === undefined) {
  143. if ($len % BYTES) throw RangeError(WRONG_LENGTH);
  144. byteLength = $len - byteOffset;
  145. if (byteLength < 0) throw RangeError(WRONG_LENGTH);
  146. } else {
  147. byteLength = toLength($length) * BYTES;
  148. if (byteLength + byteOffset > $len) throw RangeError(WRONG_LENGTH);
  149. }
  150. length = byteLength / BYTES;
  151. } else if (isTypedArray(data)) {
  152. return fromList(TypedArrayConstructor, data);
  153. } else {
  154. return typedArrayFrom.call(TypedArrayConstructor, data);
  155. }
  156. setInternalState(that, {
  157. buffer: buffer,
  158. byteOffset: byteOffset,
  159. byteLength: byteLength,
  160. length: length,
  161. view: new DataView(buffer)
  162. });
  163. while (index < length) addElement(that, index++);
  164. });
  165. if (setPrototypeOf) setPrototypeOf(TypedArrayConstructor, TypedArray);
  166. TypedArrayConstructorPrototype = TypedArrayConstructor.prototype = create(TypedArrayPrototype);
  167. } else if (TYPED_ARRAYS_CONSTRUCTORS_REQUIRES_WRAPPERS) {
  168. TypedArrayConstructor = wrapper(function (dummy, data, typedArrayOffset, $length) {
  169. anInstance(dummy, TypedArrayConstructor, CONSTRUCTOR_NAME);
  170. return inheritIfRequired(function () {
  171. if (!isObject(data)) return new NativeTypedArrayConstructor(toIndex(data));
  172. if (isArrayBuffer(data)) return $length !== undefined
  173. ? new NativeTypedArrayConstructor(data, toOffset(typedArrayOffset, BYTES), $length)
  174. : typedArrayOffset !== undefined
  175. ? new NativeTypedArrayConstructor(data, toOffset(typedArrayOffset, BYTES))
  176. : new NativeTypedArrayConstructor(data);
  177. if (isTypedArray(data)) return fromList(TypedArrayConstructor, data);
  178. return typedArrayFrom.call(TypedArrayConstructor, data);
  179. }(), dummy, TypedArrayConstructor);
  180. });
  181. if (setPrototypeOf) setPrototypeOf(TypedArrayConstructor, TypedArray);
  182. forEach(getOwnPropertyNames(NativeTypedArrayConstructor), function (key) {
  183. if (!(key in TypedArrayConstructor)) {
  184. createNonEnumerableProperty(TypedArrayConstructor, key, NativeTypedArrayConstructor[key]);
  185. }
  186. });
  187. TypedArrayConstructor.prototype = TypedArrayConstructorPrototype;
  188. }
  189. if (TypedArrayConstructorPrototype.constructor !== TypedArrayConstructor) {
  190. createNonEnumerableProperty(TypedArrayConstructorPrototype, 'constructor', TypedArrayConstructor);
  191. }
  192. if (TYPED_ARRAY_TAG) {
  193. createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_TAG, CONSTRUCTOR_NAME);
  194. }
  195. exported[CONSTRUCTOR_NAME] = TypedArrayConstructor;
  196. $({
  197. global: true, forced: TypedArrayConstructor != NativeTypedArrayConstructor, sham: !NATIVE_ARRAY_BUFFER_VIEWS
  198. }, exported);
  199. if (!(BYTES_PER_ELEMENT in TypedArrayConstructor)) {
  200. createNonEnumerableProperty(TypedArrayConstructor, BYTES_PER_ELEMENT, BYTES);
  201. }
  202. if (!(BYTES_PER_ELEMENT in TypedArrayConstructorPrototype)) {
  203. createNonEnumerableProperty(TypedArrayConstructorPrototype, BYTES_PER_ELEMENT, BYTES);
  204. }
  205. setSpecies(CONSTRUCTOR_NAME);
  206. };
  207. } else module.exports = function () { /* empty */ };