js/src/vm/TypedArrayObject.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/vm/TypedArrayObject.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,380 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef vm_TypedArrayObject_h
    1.11 +#define vm_TypedArrayObject_h
    1.12 +
    1.13 +#include "jsobj.h"
    1.14 +
    1.15 +#include "builtin/TypedObject.h"
    1.16 +#include "gc/Barrier.h"
    1.17 +#include "js/Class.h"
    1.18 +#include "vm/ArrayBufferObject.h"
    1.19 +
    1.20 +typedef struct JSProperty JSProperty;
    1.21 +
    1.22 +namespace js {
    1.23 +
    1.24 +/*
    1.25 + * TypedArrayObject
    1.26 + *
    1.27 + * The non-templated base class for the specific typed implementations.
    1.28 + * This class holds all the member variables that are used by
    1.29 + * the subclasses.
    1.30 + */
    1.31 +
    1.32 +class TypedArrayObject : public ArrayBufferViewObject
    1.33 +{
    1.34 +  protected:
    1.35 +    // Typed array properties stored in slots, beyond those shared by all
    1.36 +    // ArrayBufferViews.
    1.37 +    static const size_t LENGTH_SLOT    = JS_TYPEDOBJ_SLOT_LENGTH;
    1.38 +    static const size_t TYPE_SLOT      = JS_TYPEDOBJ_SLOT_TYPE_DESCR;
    1.39 +    static const size_t RESERVED_SLOTS = JS_TYPEDOBJ_SLOTS;
    1.40 +    static const size_t DATA_SLOT      = JS_TYPEDOBJ_SLOT_DATA;
    1.41 +
    1.42 +    static_assert(js::detail::TypedArrayLengthSlot == LENGTH_SLOT,
    1.43 +                  "bad inlined constant in jsfriendapi.h");
    1.44 +
    1.45 +  public:
    1.46 +    static const Class classes[ScalarTypeDescr::TYPE_MAX];
    1.47 +    static const Class protoClasses[ScalarTypeDescr::TYPE_MAX];
    1.48 +
    1.49 +    static const size_t FIXED_DATA_START = DATA_SLOT + 1;
    1.50 +
    1.51 +    // For typed arrays which can store their data inline, the array buffer
    1.52 +    // object is created lazily.
    1.53 +    static const uint32_t INLINE_BUFFER_LIMIT =
    1.54 +        (JSObject::MAX_FIXED_SLOTS - FIXED_DATA_START) * sizeof(Value);
    1.55 +
    1.56 +    static gc::AllocKind
    1.57 +    AllocKindForLazyBuffer(size_t nbytes)
    1.58 +    {
    1.59 +        JS_ASSERT(nbytes <= INLINE_BUFFER_LIMIT);
    1.60 +        /* For GGC we need at least one slot in which to store a forwarding pointer. */
    1.61 +        size_t dataSlots = Max(size_t(1), AlignBytes(nbytes, sizeof(Value)) / sizeof(Value));
    1.62 +        JS_ASSERT(nbytes <= dataSlots * sizeof(Value));
    1.63 +        return gc::GetGCObjectKind(FIXED_DATA_START + dataSlots);
    1.64 +    }
    1.65 +
    1.66 +    static Value bufferValue(TypedArrayObject *tarr) {
    1.67 +        return tarr->getFixedSlot(BUFFER_SLOT);
    1.68 +    }
    1.69 +    static Value byteOffsetValue(TypedArrayObject *tarr) {
    1.70 +        return tarr->getFixedSlot(BYTEOFFSET_SLOT);
    1.71 +    }
    1.72 +    static Value byteLengthValue(TypedArrayObject *tarr) {
    1.73 +        return tarr->getFixedSlot(BYTELENGTH_SLOT);
    1.74 +    }
    1.75 +    static Value lengthValue(TypedArrayObject *tarr) {
    1.76 +        return tarr->getFixedSlot(LENGTH_SLOT);
    1.77 +    }
    1.78 +
    1.79 +    static bool
    1.80 +    ensureHasBuffer(JSContext *cx, Handle<TypedArrayObject *> tarray);
    1.81 +
    1.82 +    ArrayBufferObject *sharedBuffer() const;
    1.83 +    ArrayBufferObject *buffer() const {
    1.84 +        JSObject *obj = bufferValue(const_cast<TypedArrayObject*>(this)).toObjectOrNull();
    1.85 +        if (!obj)
    1.86 +            return nullptr;
    1.87 +        if (obj->is<ArrayBufferObject>())
    1.88 +            return &obj->as<ArrayBufferObject>();
    1.89 +        return sharedBuffer();
    1.90 +    }
    1.91 +    uint32_t byteOffset() const {
    1.92 +        return byteOffsetValue(const_cast<TypedArrayObject*>(this)).toInt32();
    1.93 +    }
    1.94 +    uint32_t byteLength() const {
    1.95 +        return byteLengthValue(const_cast<TypedArrayObject*>(this)).toInt32();
    1.96 +    }
    1.97 +    uint32_t length() const {
    1.98 +        return lengthValue(const_cast<TypedArrayObject*>(this)).toInt32();
    1.99 +    }
   1.100 +
   1.101 +    uint32_t type() const {
   1.102 +        return getFixedSlot(TYPE_SLOT).toInt32();
   1.103 +    }
   1.104 +    void *viewData() const {
   1.105 +        // Keep synced with js::Get<Type>ArrayLengthAndData in jsfriendapi.h!
   1.106 +        return static_cast<void*>(getPrivate(DATA_SLOT));
   1.107 +    }
   1.108 +
   1.109 +    Value getElement(uint32_t index);
   1.110 +    static void setElement(TypedArrayObject &obj, uint32_t index, double d);
   1.111 +
   1.112 +    void neuter(void *newData);
   1.113 +
   1.114 +    static uint32_t slotWidth(int atype) {
   1.115 +        switch (atype) {
   1.116 +          case ScalarTypeDescr::TYPE_INT8:
   1.117 +          case ScalarTypeDescr::TYPE_UINT8:
   1.118 +          case ScalarTypeDescr::TYPE_UINT8_CLAMPED:
   1.119 +            return 1;
   1.120 +          case ScalarTypeDescr::TYPE_INT16:
   1.121 +          case ScalarTypeDescr::TYPE_UINT16:
   1.122 +            return 2;
   1.123 +          case ScalarTypeDescr::TYPE_INT32:
   1.124 +          case ScalarTypeDescr::TYPE_UINT32:
   1.125 +          case ScalarTypeDescr::TYPE_FLOAT32:
   1.126 +            return 4;
   1.127 +          case ScalarTypeDescr::TYPE_FLOAT64:
   1.128 +            return 8;
   1.129 +          default:
   1.130 +            MOZ_ASSUME_UNREACHABLE("invalid typed array type");
   1.131 +        }
   1.132 +    }
   1.133 +
   1.134 +    int slotWidth() {
   1.135 +        return slotWidth(type());
   1.136 +    }
   1.137 +
   1.138 +    /*
   1.139 +     * Byte length above which created typed arrays and data views will have
   1.140 +     * singleton types regardless of the context in which they are created.
   1.141 +     */
   1.142 +    static const uint32_t SINGLETON_TYPE_BYTE_LENGTH = 1024 * 1024 * 10;
   1.143 +
   1.144 +    static int lengthOffset();
   1.145 +    static int dataOffset();
   1.146 +};
   1.147 +
   1.148 +inline bool
   1.149 +IsTypedArrayClass(const Class *clasp)
   1.150 +{
   1.151 +    return &TypedArrayObject::classes[0] <= clasp &&
   1.152 +           clasp < &TypedArrayObject::classes[ScalarTypeDescr::TYPE_MAX];
   1.153 +}
   1.154 +
   1.155 +inline bool
   1.156 +IsTypedArrayProtoClass(const Class *clasp)
   1.157 +{
   1.158 +    return &TypedArrayObject::protoClasses[0] <= clasp &&
   1.159 +           clasp < &TypedArrayObject::protoClasses[ScalarTypeDescr::TYPE_MAX];
   1.160 +}
   1.161 +
   1.162 +bool
   1.163 +IsTypedArrayConstructor(HandleValue v, uint32_t type);
   1.164 +
   1.165 +bool
   1.166 +IsTypedArrayBuffer(HandleValue v);
   1.167 +
   1.168 +ArrayBufferObject &
   1.169 +AsTypedArrayBuffer(HandleValue v);
   1.170 +
   1.171 +// Return value is whether the string is some integer. If the string is an
   1.172 +// integer which is not representable as a uint64_t, the return value is true
   1.173 +// and the resulting index is UINT64_MAX.
   1.174 +bool
   1.175 +StringIsTypedArrayIndex(JSLinearString *str, uint64_t *indexp);
   1.176 +
   1.177 +inline bool
   1.178 +IsTypedArrayIndex(jsid id, uint64_t *indexp)
   1.179 +{
   1.180 +    if (JSID_IS_INT(id)) {
   1.181 +        int32_t i = JSID_TO_INT(id);
   1.182 +        JS_ASSERT(i >= 0);
   1.183 +        *indexp = (double)i;
   1.184 +        return true;
   1.185 +    }
   1.186 +
   1.187 +    if (MOZ_UNLIKELY(!JSID_IS_STRING(id)))
   1.188 +        return false;
   1.189 +
   1.190 +    JSAtom *atom = JSID_TO_ATOM(id);
   1.191 +
   1.192 +    jschar c = atom->chars()[0];
   1.193 +    if (!JS7_ISDEC(c) && c != '-')
   1.194 +        return false;
   1.195 +
   1.196 +    return StringIsTypedArrayIndex(atom, indexp);
   1.197 +}
   1.198 +
   1.199 +static inline unsigned
   1.200 +TypedArrayShift(ArrayBufferView::ViewType viewType)
   1.201 +{
   1.202 +    switch (viewType) {
   1.203 +      case ArrayBufferView::TYPE_INT8:
   1.204 +      case ArrayBufferView::TYPE_UINT8:
   1.205 +      case ArrayBufferView::TYPE_UINT8_CLAMPED:
   1.206 +        return 0;
   1.207 +      case ArrayBufferView::TYPE_INT16:
   1.208 +      case ArrayBufferView::TYPE_UINT16:
   1.209 +        return 1;
   1.210 +      case ArrayBufferView::TYPE_INT32:
   1.211 +      case ArrayBufferView::TYPE_UINT32:
   1.212 +      case ArrayBufferView::TYPE_FLOAT32:
   1.213 +        return 2;
   1.214 +      case ArrayBufferView::TYPE_FLOAT64:
   1.215 +        return 3;
   1.216 +      default:;
   1.217 +    }
   1.218 +    MOZ_ASSUME_UNREACHABLE("Unexpected array type");
   1.219 +}
   1.220 +
   1.221 +class DataViewObject : public ArrayBufferViewObject
   1.222 +{
   1.223 +    static const size_t RESERVED_SLOTS = JS_DATAVIEW_SLOTS;
   1.224 +    static const size_t DATA_SLOT      = JS_TYPEDOBJ_SLOT_DATA;
   1.225 +
   1.226 +  private:
   1.227 +    static const Class protoClass;
   1.228 +
   1.229 +    static bool is(HandleValue v) {
   1.230 +        return v.isObject() && v.toObject().hasClass(&class_);
   1.231 +    }
   1.232 +
   1.233 +    template <typename NativeType>
   1.234 +    static uint8_t *
   1.235 +    getDataPointer(JSContext *cx, Handle<DataViewObject*> obj, uint32_t offset);
   1.236 +
   1.237 +    template<Value ValueGetter(DataViewObject *view)>
   1.238 +    static bool
   1.239 +    getterImpl(JSContext *cx, CallArgs args);
   1.240 +
   1.241 +    template<Value ValueGetter(DataViewObject *view)>
   1.242 +    static bool
   1.243 +    getter(JSContext *cx, unsigned argc, Value *vp);
   1.244 +
   1.245 +    template<Value ValueGetter(DataViewObject *view)>
   1.246 +    static bool
   1.247 +    defineGetter(JSContext *cx, PropertyName *name, HandleObject proto);
   1.248 +
   1.249 +  public:
   1.250 +    static const Class class_;
   1.251 +
   1.252 +    static Value byteOffsetValue(DataViewObject *view) {
   1.253 +        Value v = view->getReservedSlot(BYTEOFFSET_SLOT);
   1.254 +        JS_ASSERT(v.toInt32() >= 0);
   1.255 +        return v;
   1.256 +    }
   1.257 +
   1.258 +    static Value byteLengthValue(DataViewObject *view) {
   1.259 +        Value v = view->getReservedSlot(BYTELENGTH_SLOT);
   1.260 +        JS_ASSERT(v.toInt32() >= 0);
   1.261 +        return v;
   1.262 +    }
   1.263 +
   1.264 +    static Value bufferValue(DataViewObject *view) {
   1.265 +        return view->getReservedSlot(BUFFER_SLOT);
   1.266 +    }
   1.267 +
   1.268 +    uint32_t byteOffset() const {
   1.269 +        return byteOffsetValue(const_cast<DataViewObject*>(this)).toInt32();
   1.270 +    }
   1.271 +
   1.272 +    uint32_t byteLength() const {
   1.273 +        return byteLengthValue(const_cast<DataViewObject*>(this)).toInt32();
   1.274 +    }
   1.275 +
   1.276 +    ArrayBufferObject &arrayBuffer() const {
   1.277 +        return bufferValue(const_cast<DataViewObject*>(this)).toObject().as<ArrayBufferObject>();
   1.278 +    }
   1.279 +
   1.280 +    void *dataPointer() const {
   1.281 +        return getPrivate();
   1.282 +    }
   1.283 +
   1.284 +    static bool class_constructor(JSContext *cx, unsigned argc, Value *vp);
   1.285 +    static bool constructWithProto(JSContext *cx, unsigned argc, Value *vp);
   1.286 +    static bool construct(JSContext *cx, JSObject *bufobj, const CallArgs &args,
   1.287 +                          HandleObject proto);
   1.288 +
   1.289 +    static inline DataViewObject *
   1.290 +    create(JSContext *cx, uint32_t byteOffset, uint32_t byteLength,
   1.291 +           Handle<ArrayBufferObject*> arrayBuffer, JSObject *proto);
   1.292 +
   1.293 +    static bool getInt8Impl(JSContext *cx, CallArgs args);
   1.294 +    static bool fun_getInt8(JSContext *cx, unsigned argc, Value *vp);
   1.295 +
   1.296 +    static bool getUint8Impl(JSContext *cx, CallArgs args);
   1.297 +    static bool fun_getUint8(JSContext *cx, unsigned argc, Value *vp);
   1.298 +
   1.299 +    static bool getInt16Impl(JSContext *cx, CallArgs args);
   1.300 +    static bool fun_getInt16(JSContext *cx, unsigned argc, Value *vp);
   1.301 +
   1.302 +    static bool getUint16Impl(JSContext *cx, CallArgs args);
   1.303 +    static bool fun_getUint16(JSContext *cx, unsigned argc, Value *vp);
   1.304 +
   1.305 +    static bool getInt32Impl(JSContext *cx, CallArgs args);
   1.306 +    static bool fun_getInt32(JSContext *cx, unsigned argc, Value *vp);
   1.307 +
   1.308 +    static bool getUint32Impl(JSContext *cx, CallArgs args);
   1.309 +    static bool fun_getUint32(JSContext *cx, unsigned argc, Value *vp);
   1.310 +
   1.311 +    static bool getFloat32Impl(JSContext *cx, CallArgs args);
   1.312 +    static bool fun_getFloat32(JSContext *cx, unsigned argc, Value *vp);
   1.313 +
   1.314 +    static bool getFloat64Impl(JSContext *cx, CallArgs args);
   1.315 +    static bool fun_getFloat64(JSContext *cx, unsigned argc, Value *vp);
   1.316 +
   1.317 +    static bool setInt8Impl(JSContext *cx, CallArgs args);
   1.318 +    static bool fun_setInt8(JSContext *cx, unsigned argc, Value *vp);
   1.319 +
   1.320 +    static bool setUint8Impl(JSContext *cx, CallArgs args);
   1.321 +    static bool fun_setUint8(JSContext *cx, unsigned argc, Value *vp);
   1.322 +
   1.323 +    static bool setInt16Impl(JSContext *cx, CallArgs args);
   1.324 +    static bool fun_setInt16(JSContext *cx, unsigned argc, Value *vp);
   1.325 +
   1.326 +    static bool setUint16Impl(JSContext *cx, CallArgs args);
   1.327 +    static bool fun_setUint16(JSContext *cx, unsigned argc, Value *vp);
   1.328 +
   1.329 +    static bool setInt32Impl(JSContext *cx, CallArgs args);
   1.330 +    static bool fun_setInt32(JSContext *cx, unsigned argc, Value *vp);
   1.331 +
   1.332 +    static bool setUint32Impl(JSContext *cx, CallArgs args);
   1.333 +    static bool fun_setUint32(JSContext *cx, unsigned argc, Value *vp);
   1.334 +
   1.335 +    static bool setFloat32Impl(JSContext *cx, CallArgs args);
   1.336 +    static bool fun_setFloat32(JSContext *cx, unsigned argc, Value *vp);
   1.337 +
   1.338 +    static bool setFloat64Impl(JSContext *cx, CallArgs args);
   1.339 +    static bool fun_setFloat64(JSContext *cx, unsigned argc, Value *vp);
   1.340 +
   1.341 +    static bool initClass(JSContext *cx);
   1.342 +    static void neuter(JSObject *view);
   1.343 +    template<typename NativeType>
   1.344 +    static bool read(JSContext *cx, Handle<DataViewObject*> obj,
   1.345 +                     CallArgs &args, NativeType *val, const char *method);
   1.346 +    template<typename NativeType>
   1.347 +    static bool write(JSContext *cx, Handle<DataViewObject*> obj,
   1.348 +                      CallArgs &args, const char *method);
   1.349 +
   1.350 +    void neuter(void *newData);
   1.351 +
   1.352 +  private:
   1.353 +    static const JSFunctionSpec jsfuncs[];
   1.354 +};
   1.355 +
   1.356 +static inline int32_t
   1.357 +ClampIntForUint8Array(int32_t x)
   1.358 +{
   1.359 +    if (x < 0)
   1.360 +        return 0;
   1.361 +    if (x > 255)
   1.362 +        return 255;
   1.363 +    return x;
   1.364 +}
   1.365 +
   1.366 +} // namespace js
   1.367 +
   1.368 +template <>
   1.369 +inline bool
   1.370 +JSObject::is<js::TypedArrayObject>() const
   1.371 +{
   1.372 +    return js::IsTypedArrayClass(getClass());
   1.373 +}
   1.374 +
   1.375 +template <>
   1.376 +inline bool
   1.377 +JSObject::is<js::ArrayBufferViewObject>() const
   1.378 +{
   1.379 +    return is<js::DataViewObject>() || is<js::TypedArrayObject>() ||
   1.380 +           IsTypedObjectClass(getClass());
   1.381 +}
   1.382 +
   1.383 +#endif /* vm_TypedArrayObject_h */

mercurial