Commit 8c0a4864 authored by Jeff Walden's avatar Jeff Walden
Browse files

Bug 1458011 - Remove the TypedObject.storage function and |new...

Bug 1458011 - Remove the TypedObject.storage function and |new TypedObject(buffer [, offset])| constructor overload because they're no longer planned to be part of standardization efforts in this area.  r=sfink

--HG--
extra : rebase_source : cbeb0e62928d03c2ef06d2465168ccde4840638f
parent 2690d233
Loading
Loading
Loading
Loading
+1 −69
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ const Class js::TypedObjectModuleObject::class_ = {

static const JSFunctionSpec TypedObjectMethods[] = {
    JS_SELF_HOSTED_FN("objectType", "TypeOfTypedObject", 1, 0),
    JS_SELF_HOSTED_FN("storage", "StorageOfTypedObject", 1, 0),
    JS_FS_END
};

@@ -2282,27 +2281,6 @@ LengthForType(TypeDescr& descr)
    MOZ_CRASH("Invalid kind");
}

static bool
CheckOffset(uint32_t offset, uint32_t size, uint32_t alignment, uint32_t bufferLength)
{
    // Offset (plus size) must be fully contained within the buffer.
    if (offset > bufferLength)
        return false;
    if (offset + size < offset)
        return false;
    if (offset + size > bufferLength)
        return false;

    // Offset must be aligned.
    if ((offset % alignment) != 0)
        return false;

    return true;
}

template<typename T, typename U, typename V, typename W>
inline bool CheckOffset(T, U, V, W) = delete;

/*static*/ bool
TypedObject::construct(JSContext* cx, unsigned int argc, Value* vp)
{
@@ -2311,11 +2289,9 @@ TypedObject::construct(JSContext* cx, unsigned int argc, Value* vp)
    MOZ_ASSERT(args.callee().is<TypeDescr>());
    Rooted<TypeDescr*> callee(cx, &args.callee().as<TypeDescr>());

    // Typed object constructors are overloaded in three ways, in order of
    // precedence:
    // Typed object constructors are overloaded in two ways:
    //
    //   new TypeObj()
    //   new TypeObj(buffer, [offset])
    //   new TypeObj(data)

    // Zero argument constructor:
@@ -2328,50 +2304,6 @@ TypedObject::construct(JSContext* cx, unsigned int argc, Value* vp)
        return true;
    }

    // Buffer constructor.
    if (args[0].isObject() && args[0].toObject().is<ArrayBufferObject>()) {
        Rooted<ArrayBufferObject*> buffer(cx);
        buffer = &args[0].toObject().as<ArrayBufferObject>();

        if (callee->opaque() || buffer->isDetached()) {
            JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_TYPEDOBJECT_BAD_ARGS);
            return false;
        }

        uint32_t offset;
        if (args.length() >= 2 && !args[1].isUndefined()) {
            if (!args[1].isInt32() || args[1].toInt32() < 0) {
                JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_TYPEDOBJECT_BAD_ARGS);
                return false;
            }

            offset = args[1].toInt32();
        } else {
            offset = 0;
        }

        if (args.length() >= 3 && !args[2].isUndefined()) {
            JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_TYPEDOBJECT_BAD_ARGS);
            return false;
        }

        if (!CheckOffset(offset, callee->size(), callee->alignment(),
                         buffer->byteLength()))
        {
            JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_TYPEDOBJECT_BAD_ARGS);
            return false;
        }

        Rooted<OutlineTypedObject*> obj(cx);
        obj = OutlineTypedObject::createUnattached(cx, callee, LengthForType(*callee));
        if (!obj)
            return false;

        obj->attach(cx, *buffer, offset);
        args.rval().setObject(*obj);
        return true;
    }

    // Data constructor.
    if (args[0].isObject()) {
        // Create the typed object.
+0 −27
Original line number Diff line number Diff line
@@ -906,33 +906,6 @@ function ArrayShorthand(...dims) {
  return accum;
}

// This is the `storage()` function defined in the spec.  When
// provided with a *transparent* typed object, it returns an object
// containing buffer, byteOffset, byteLength. When given an opaque
// typed object, it returns null. Otherwise it throws.
//
// Warning: user exposed!
function StorageOfTypedObject(obj) {
  if (IsObject(obj)) {
    if (ObjectIsOpaqueTypedObject(obj))
      return null;

    if (ObjectIsTransparentTypedObject(obj)) {
      if (!TypedObjectIsAttached(obj))
          ThrowTypeError(JSMSG_TYPEDOBJECT_HANDLE_UNATTACHED);

      var descr = TypedObjectTypeDescr(obj);
      var byteLength = DESCR_SIZE(descr);

      return { buffer: TypedObjectBuffer(obj),
               byteLength,
               byteOffset: TypedObjectByteOffset(obj) };
    }
  }

  ThrowTypeError(JSMSG_TYPEDOBJECT_BAD_ARGS);
}

// This is the `objectType()` function defined in the spec.
// It returns the type of its argument.
//
+6 −8
Original line number Diff line number Diff line
@@ -14,14 +14,12 @@ function main()
  var Point = new StructType({ x: uint32, y: uint32 });
  var Line = new StructType({ from: Point, to: Point });

  var buf = new ArrayBuffer(16);
  var line = new Line(buf);
  var line = new Line();

  assertThrowsInstanceOf(function()
  {
    line.to = { x: 22,
                get y() { detachArrayBuffer(buf); return 44; } };
  }, TypeError, "setting into a detached buffer is bad mojo");
  line.to = { x: 22, get y() { return 44; } };

  assertEq(line.to.x, 22);
  assertEq(line.to.y, 44);
}

main();
+0 −23
Original line number Diff line number Diff line
// Bug 976697. Check for various quirks when instantiating a typed
// object atop a detached buffer.

if (typeof TypedObject === "undefined")
  quit();

load(libdir + "asserts.js")

var {StructType, uint32, Object, Any, storage, objectType} = TypedObject;

function main() { // once a C programmer, always a C programmer.
  var Uints = uint32.array(0);
  var Unit = new StructType({});   // Empty struct type
  var buffer = new ArrayBuffer(0); // Empty buffer
  var p = new Unit(buffer);        // OK
  detachArrayBuffer(buffer);
  assertThrowsInstanceOf(() => new Unit(buffer), TypeError,
                         "Able to instantiate atop detached buffer");
  assertThrowsInstanceOf(() => new Uints(buffer), TypeError,
                         "Able to instantiate atop detached buffer");
}

main();
+0 −13
Original line number Diff line number Diff line
if (!this.hasOwnProperty("TypedObject"))
  quit();

var Uint32Array = TypedObject.float32.array(3);
const ONE_MINUS_EPSILON = 1 - Math.pow(2, -53);
const f = new Float64Array([0, 0]);
const u = new Uint32Array(f.buffer);
const diff = function(a, b) {
    f[1] = b;
    u[3 - ENDIAN];
};
ENDIAN = 1;
diff(1, ONE_MINUS_EPSILON)
Loading