Bug 969578 -- Remove public facing Handle API r=till

This commit is contained in:
Nicholas D. Matsakis 2014-02-10 10:03:49 -05:00
Родитель d0e56d0b88
Коммит af83dc1a25
11 изменённых файлов: 68 добавлений и 580 удалений

Просмотреть файл

@ -138,7 +138,6 @@ class Float32x4Defn {
const JSFunctionSpec js::Float32x4Defn::TypeDescriptorMethods[] = {
JS_SELF_HOSTED_FN("toSource", "DescrToSourceMethod", 0, 0),
JS_SELF_HOSTED_FN("handle", "HandleCreate", 2, 0),
JS_SELF_HOSTED_FN("array", "ArrayShorthand", 1, 0),
JS_SELF_HOSTED_FN("equivalent", "TypeDescrEquivalent", 1, 0),
JS_FS_END
@ -160,7 +159,6 @@ const JSFunctionSpec js::Float32x4Defn::TypedDatumMethods[] = {
const JSFunctionSpec js::Int32x4Defn::TypeDescriptorMethods[] = {
JS_SELF_HOSTED_FN("toSource", "DescrToSourceMethod", 0, 0),
JS_SELF_HOSTED_FN("handle", "HandleCreate", 2, 0),
JS_SELF_HOSTED_FN("array", "ArrayShorthand", 1, 0),
JS_SELF_HOSTED_FN("equivalent", "TypeDescrEquivalent", 1, 0),
JS_FS_END,

Просмотреть файл

@ -216,7 +216,6 @@ const Class js::ScalarTypeDescr::class_ = {
const JSFunctionSpec js::ScalarTypeDescr::typeObjectMethods[] = {
JS_SELF_HOSTED_FN("toSource", "DescrToSourceMethod", 0, 0),
{"handle", {nullptr, nullptr}, 2, 0, "HandleCreate"},
{"array", {nullptr, nullptr}, 1, 0, "ArrayShorthand"},
{"equivalent", {nullptr, nullptr}, 1, 0, "TypeDescrEquivalent"},
JS_FS_END
@ -317,7 +316,6 @@ const Class js::ReferenceTypeDescr::class_ = {
const JSFunctionSpec js::ReferenceTypeDescr::typeObjectMethods[] = {
JS_SELF_HOSTED_FN("toSource", "DescrToSourceMethod", 0, 0),
{"handle", {nullptr, nullptr}, 2, 0, "HandleCreate"},
{"array", {nullptr, nullptr}, 1, 0, "ArrayShorthand"},
{"equivalent", {nullptr, nullptr}, 1, 0, "TypeDescrEquivalent"},
JS_FS_END
@ -467,7 +465,6 @@ const JSPropertySpec ArrayMetaTypeDescr::typeObjectProperties[] = {
};
const JSFunctionSpec ArrayMetaTypeDescr::typeObjectMethods[] = {
{"handle", {nullptr, nullptr}, 2, 0, "HandleCreate"},
{"array", {nullptr, nullptr}, 1, 0, "ArrayShorthand"},
JS_FN("dimension", UnsizedArrayTypeDescr::dimension, 1, 0),
JS_SELF_HOSTED_FN("toSource", "DescrToSourceMethod", 0, 0),
@ -756,7 +753,6 @@ const JSPropertySpec StructMetaTypeDescr::typeObjectProperties[] = {
};
const JSFunctionSpec StructMetaTypeDescr::typeObjectMethods[] = {
{"handle", {nullptr, nullptr}, 2, 0, "HandleCreate"},
{"array", {nullptr, nullptr}, 1, 0, "ArrayShorthand"},
JS_SELF_HOSTED_FN("toSource", "DescrToSourceMethod", 0, 0),
{"equivalent", {nullptr, nullptr}, 1, 0, "TypeDescrEquivalent"},
@ -1250,24 +1246,6 @@ GlobalObject::initTypedObjectModule(JSContext *cx, Handle<GlobalObject*> global)
JSPROP_READONLY | JSPROP_PERMANENT))
return nullptr;
// Handle
RootedObject handle(cx, NewBuiltinClassInstance(cx, &JSObject::class_));
if (!module)
return nullptr;
if (!JS_DefineFunctions(cx, handle, TypedHandle::handleStaticMethods))
return nullptr;
RootedValue handleValue(cx, ObjectValue(*handle));
if (!JSObject::defineProperty(cx, module, cx->names().Handle,
handleValue,
nullptr, nullptr,
JSPROP_READONLY | JSPROP_PERMANENT))
{
return nullptr;
}
// Everything is setup, install module on the global object:
RootedValue moduleValue(cx, ObjectValue(*module));
global->setConstructor(JSProto_TypedObject, moduleValue);

Просмотреть файл

@ -181,6 +181,12 @@ TypedObjectPointer.prototype.reset = function(inPtr) {
return this;
};
TypedObjectPointer.prototype.bump = function(size) {
assert(TO_INT32(this.offset) === this.offset, "current offset not int");
assert(TO_INT32(size) === size, "size not int");
this.offset += size;
}
TypedObjectPointer.prototype.kind = function() {
return DESCR_KIND(this.descr);
}
@ -312,8 +318,7 @@ TypedObjectPointer.prototype.moveToFieldIndex = function(index) {
// Reifies the value referenced by the pointer, meaning that it
// returns a new object pointing at the value. If the value is
// a scalar, it will return a JS number, but otherwise the reified
// result will be a typed object or handle, depending on the type
// of the ptr's datum.
// result will be a datum of the same class as the ptr's datum.
TypedObjectPointer.prototype.get = function() {
assert(ObjectIsAttached(this.datum), "get() called with unattached datum");
@ -328,10 +333,8 @@ TypedObjectPointer.prototype.get = function() {
return this.getX4();
case JS_TYPEREPR_SIZED_ARRAY_KIND:
return NewDerivedTypedDatum(this.descr, this.datum, this.offset);
case JS_TYPEREPR_STRUCT_KIND:
return NewDerivedTypedDatum(this.descr, this.datum, this.offset);
return this.getDerived();
case JS_TYPEREPR_UNSIZED_ARRAY_KIND:
assert(false, "Unhandled repr kind: " + this.kind());
@ -341,6 +344,12 @@ TypedObjectPointer.prototype.get = function() {
return undefined;
}
TypedObjectPointer.prototype.getDerived = function() {
assert(!TypeDescrIsSimpleType(this.descr),
"getDerived() used with simple type");
return NewDerivedTypedDatum(this.descr, this.datum, this.offset);
}
TypedObjectPointer.prototype.getScalar = function() {
var type = DESCR_TYPE(this.descr);
switch (type) {
@ -726,99 +735,6 @@ function TypedArrayRedimension(newArrayType) {
return NewDerivedTypedDatum(newArrayType, this, 0);
}
///////////////////////////////////////////////////////////////////////////
// Handles
//
// Note: these methods are directly invokable by users and so must be
// defensive.
// This is the `handle([obj, [...path]])` method on type objects.
// User exposed!
//
// FIXME bug 929656 -- label algorithms with steps from the spec
function HandleCreate(obj, ...path) {
if (!IsObject(this) || !ObjectIsTypeDescr(this))
ThrowError(JSMSG_INCOMPATIBLE_PROTO, "Type", "handle", "value");
switch (DESCR_KIND(this)) {
case JS_TYPEREPR_SCALAR_KIND:
case JS_TYPEREPR_REFERENCE_KIND:
case JS_TYPEREPR_X4_KIND:
case JS_TYPEREPR_SIZED_ARRAY_KIND:
case JS_TYPEREPR_STRUCT_KIND:
break;
case JS_TYPEREPR_UNSIZED_ARRAY_KIND:
ThrowError(JSMSG_TYPEDOBJECT_HANDLE_TO_UNSIZED);
}
var handle = NewTypedHandle(this);
if (obj !== undefined)
HandleMoveInternal(handle, obj, path);
return handle;
}
// Handle.move: user exposed!
// FIXME bug 929656 -- label algorithms with steps from the spec
function HandleMove(handle, obj, ...path) {
if (!IsObject(handle) || !ObjectIsTypedHandle(handle))
ThrowError(JSMSG_INCOMPATIBLE_PROTO, "Handle", "set", typeof value);
HandleMoveInternal(handle, obj, path);
}
function HandleMoveInternal(handle, obj, path) {
assert(IsObject(handle) && ObjectIsTypedHandle(handle),
"HandleMoveInternal: not typed handle");
if (!IsObject(obj) || !ObjectIsTypedDatum(obj))
ThrowError(JSMSG_INCOMPATIBLE_PROTO, "Handle", "set", "value");
var ptr = TypedObjectPointer.fromTypedDatum(obj);
for (var i = 0; i < path.length; i++)
ptr.moveTo(path[i]);
// Check that the new destination is equivalent to the handle type.
if (DESCR_TYPE_REPR(ptr.descr) !== DATUM_TYPE_REPR(handle))
ThrowError(JSMSG_TYPEDOBJECT_HANDLE_BAD_TYPE);
AttachHandle(handle, ptr.datum, ptr.offset)
}
// Handle.get: user exposed!
// FIXME bug 929656 -- label algorithms with steps from the spec
function HandleGet(handle) {
if (!IsObject(handle) || !ObjectIsTypedHandle(handle))
ThrowError(JSMSG_INCOMPATIBLE_PROTO, "Handle", "set", typeof value);
if (!ObjectIsAttached(handle))
ThrowError(JSMSG_TYPEDOBJECT_HANDLE_UNATTACHED);
var ptr = TypedObjectPointer.fromTypedDatum(handle);
return ptr.get();
}
// Handle.set: user exposed!
// FIXME bug 929656 -- label algorithms with steps from the spec
function HandleSet(handle, value) {
if (!IsObject(handle) || !ObjectIsTypedHandle(handle))
ThrowError(JSMSG_INCOMPATIBLE_PROTO, "Handle", "set", typeof value);
if (!ObjectIsAttached(handle))
ThrowError(JSMSG_TYPEDOBJECT_HANDLE_UNATTACHED);
var ptr = TypedObjectPointer.fromTypedDatum(handle);
ptr.set(value);
}
// Handle.isHandle: user exposed!
// FIXME bug 929656 -- label algorithms with steps from the spec
function HandleTest(obj) {
return IsObject(obj) && ObjectIsTypedHandle(obj);
}
///////////////////////////////////////////////////////////////////////////
// X4
@ -953,7 +869,6 @@ function TypedObjectArrayTypeFrom(a, b, c) {
// supporting an explicit depth of 1; while for typed input array,
// the expectation is explicit depth.
if (untypedInput) {
var explicitDepth = (b === 1);
if (explicitDepth && IsCallable(c))
@ -1168,25 +1083,25 @@ function BuildTypedSeqImpl(arrayType, len, depth, func) {
indices[i] = 0;
}
var handle = callFunction(HandleCreate, grainType);
var offset = 0;
var grainTypeIsSimple = TypeDescrIsSimpleType(grainType);
var size = DESCR_SIZE(grainType);
var outPointer = new TypedObjectPointer(grainType, result, 0);
for (i = 0; i < totalLength; i++) {
// Position handle to point at &result[...indices]
AttachHandle(handle, result, offset);
// Position out-pointer to point at &result[...indices], if appropriate.
var userOutPointer = (grainTypeIsSimple
? undefined
: outPointer.getDerived());
// Invoke func(...indices, out)
callFunction(std_Array_push, indices, handle);
var r = callFunction(std_Function_apply, func, void 0, indices);
// Invoke func(...indices, userOutPointer) and store the result
callFunction(std_Array_push, indices, userOutPointer);
var r = callFunction(std_Function_apply, func, undefined, indices);
callFunction(std_Array_pop, indices);
if (r !== undefined)
outPointer.set(r); // result[...indices] = r;
if (r !== undefined) {
// result[...indices] = r;
AttachHandle(handle, result, offset); // (func might have moved handle)
HandleSet(handle, r); // *handle = r
}
// Increment indices.
offset += DESCR_SIZE(grainType);
IncrementIterationSpace(indices, iterationSpace);
outPointer.bump(size);
}
return result;
@ -1261,35 +1176,32 @@ function MapUntypedSeqImpl(inArray, outputType, maybeFunc) {
// Create a zeroed instance with no data
var result = outputType.variable ? new outputType(inArray.length) : new outputType();
var outHandle = callFunction(HandleCreate, outGrainType);
var outUnitSize = DESCR_SIZE(outGrainType);
var outGrainTypeIsSimple = TypeDescrIsSimpleType(outGrainType);
var outPointer = new TypedObjectPointer(outGrainType, result, 0);
// Core of map computation starts here (comparable to
// DoMapTypedSeqDepth1 and DoMapTypedSeqDepthN below).
var offset = 0;
for (var i = 0; i < outLength; i++) {
// In this loop, since depth is 1, "indices" denotes singleton array [i].
// Adjust handle to point at &array[...indices] for result array.
AttachHandle(outHandle, result, offset);
if (i in inArray) { // Check for holes (only needed for untyped case).
// Extract element value (no input handles for untyped case).
// Extract element value.
var element = inArray[i];
// Invoke: var r = func(element, ...indices, collection, out);
var r = func(element, i, inArray, outHandle);
// Create out pointer to point at &array[...indices] for result array.
var out = (outGrainTypeIsSimple ? undefined : outPointer.getDerived());
if (r !== undefined) {
AttachHandle(outHandle, result, offset); // (func could move handle)
HandleSet(outHandle, r); // *handle = r; (i.e. result[i] = r).
}
// Invoke: var r = func(element, ...indices, collection, out);
var r = func(element, i, inArray, out);
if (r !== undefined)
outPointer.set(r); // result[i] = r
}
// Update offset and (implicitly) increment indices.
offset += outUnitSize;
outPointer.bump(outUnitSize);
}
return result;
@ -1320,40 +1232,33 @@ function MapTypedSeqImpl(inArray, depth, outputType, func) {
// Create a zeroed instance with no data
var result = outputType.variable ? new outputType(inArray.length) : new outputType();
var inHandle = callFunction(HandleCreate, inGrainType);
var outHandle = callFunction(HandleCreate, outGrainType);
var inGrainTypeIsSimple = TypeDescrIsSimpleType(inGrainType);
var outGrainTypeIsSimple = TypeDescrIsSimpleType(outGrainType);
var inPointer = new TypedObjectPointer(inGrainType, inArray, 0);
var outPointer = new TypedObjectPointer(outGrainType, result, 0);
var inUnitSize = DESCR_SIZE(inGrainType);
var outUnitSize = DESCR_SIZE(outGrainType);
var inGrainTypeIsSimple = TypeDescrIsSimpleType(inGrainType);
// Bug 956914: add additional variants for depth = 2, 3, etc.
function DoMapTypedSeqDepth1() {
var inOffset = 0;
var outOffset = 0;
for (var i = 0; i < totalLength; i++) {
// In this loop, since depth is 1, "indices" denotes singleton array [i].
// Adjust handles to point at &array[...indices] for in and out array.
AttachHandle(inHandle, inArray, inOffset);
AttachHandle(outHandle, result, outOffset);
// Extract element value if simple; if not, handle acts as array element.
var element = (inGrainTypeIsSimple ? HandleGet(inHandle) : inHandle);
// Prepare input element/handle and out pointer
var element = inPointer.get();
var out = (outGrainTypeIsSimple ? undefined : outPointer.getDerived());
// Invoke: var r = func(element, ...indices, collection, out);
var r = func(element, i, inArray, outHandle);
if (r !== undefined) {
AttachHandle(outHandle, result, outOffset); // (func could move handle)
HandleSet(outHandle, r); // *handle = r; (i.e. result[i] = r).
}
var r = func(element, i, inArray, out);
if (r !== undefined)
outPointer.set(r); // result[i] = r
// Update offsets and (implicitly) increment indices.
inOffset += inUnitSize;
outOffset += outUnitSize;
inPointer.bump(inUnitSize);
outPointer.bump(outUnitSize);
}
return result;
@ -1362,30 +1267,22 @@ function MapTypedSeqImpl(inArray, depth, outputType, func) {
function DoMapTypedSeqDepthN() {
var indices = new Uint32Array(depth);
var inOffset = 0;
var outOffset = 0;
for (var i = 0; i < totalLength; i++) {
// Adjust handles to point at &array[...indices] for in and out array.
AttachHandle(inHandle, inArray, inOffset);
AttachHandle(outHandle, result, outOffset);
// Extract element value if simple; if not, handle acts as array element.
var element = (inGrainTypeIsSimple ? HandleGet(inHandle) : inHandle);
// Prepare input element and out pointer
var element = inPointer.get();
var out = (outGrainTypeIsSimple ? undefined : outPointer.getDerived());
// Invoke: var r = func(element, ...indices, collection, out);
var args = [element];
callFunction(std_Function_apply, std_Array_push, args, indices);
callFunction(std_Array_push, args, inArray, outHandle);
callFunction(std_Array_push, args, inArray, out);
var r = callFunction(std_Function_apply, func, void 0, args);
if (r !== undefined) {
AttachHandle(outHandle, result, outOffset); // (func could move handle)
HandleSet(outHandle, r); // *handle = r
}
if (r !== undefined)
outPointer.set(r); // result[...indices] = r
// Update offsets and explicitly increment indices.
inOffset += inUnitSize;
outOffset += outUnitSize;
inPointer.bump(inUnitSize);
outPointer.bump(outUnitSize);
IncrementIterationSpace(indices, iterationSpace);
}
@ -1481,14 +1378,15 @@ function FilterTypedSeqImpl(array, func) {
var elementType = arrayType.elementType;
var flags = new Uint8Array(NUM_BYTES(array.length));
var handle = callFunction(HandleCreate, elementType);
var count = 0;
var size = DESCR_SIZE(elementType);
var inPointer = new TypedObjectPointer(elementType, array, 0);
for (var i = 0; i < array.length; i++) {
HandleMove(handle, array, i);
if (func(HandleGet(handle), i, array)) {
if (func(inPointer.get(), i, array)) {
SET_BIT(flags, i);
count++;
}
inPointer.bump(size);
}
var resultType = (arrayType.variable ? arrayType : arrayType.unsized);

Просмотреть файл

@ -1,13 +0,0 @@
// Test that we can trace a unattached handle.
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
if (!this.hasOwnProperty("TypedObject"))
quit();
var Object = TypedObject.Object;
var handle0 = Object.handle();
gc();

Просмотреть файл

@ -1,60 +0,0 @@
// |reftest| skip-if(!this.hasOwnProperty("TypedObject"))
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
var BUGNUMBER = 898342;
var summary = 'Handles';
var T = TypedObject;
function runTests() {
var Point = T.float32.array(3);
var Line = new T.StructType({from: Point, to: Point});
var Lines = Line.array(3);
var lines = new Lines([
{from: [1, 2, 3], to: [4, 5, 6]},
{from: [7, 8, 9], to: [10, 11, 12]},
{from: [13, 14, 15], to: [16, 17, 18]}
]);
var handle = Lines.handle(lines);
var handle0 = Line.handle(lines, 0);
var handle2 = Line.handle(lines, 2);
// Reads from handles should see the correct data:
assertEq(handle[0].from[0], 1);
assertEq(handle0.from[0], 1);
assertEq(handle2.from[0], 13);
// Writes to handles should modify the original:
handle2.from[0] = 22;
assertEq(lines[2].from[0], 22);
// Reads from handles should see the updated data:
assertEq(handle[0].from[0], 1);
assertEq(handle0.from[0], 1);
assertEq(handle2.from[0], 22);
// isHandle, when called on nonsense, returns false:
assertEq(T.Handle.isHandle(22), false);
assertEq(T.Handle.isHandle({}), false);
// Derived handles should remain handles:
assertEq(T.Handle.isHandle(lines), false);
assertEq(T.Handle.isHandle(lines[0]), false);
assertEq(T.Handle.isHandle(lines[0].from), false);
assertEq(T.Handle.isHandle(handle), true);
assertEq(T.Handle.isHandle(handle[0]), true);
assertEq(T.Handle.isHandle(handle[0].from), true);
reportCompare(true, true);
print("Tests complete");
}
runTests();

Просмотреть файл

@ -1,79 +0,0 @@
// |reftest| skip-if(!this.hasOwnProperty("TypedObject"))
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
var BUGNUMBER = 898342;
var summary = 'Handle Move';
var T = TypedObject;
var Point = T.float32.array(3);
var Line = new T.StructType({from: Point, to: Point});
var Lines = Line.array(3);
var Objects = T.Object.array(3);
function runTests() {
function testHandleGetSetWithScalarType() {
var lines = new Lines([
{from: [1, 2, 3], to: [4, 5, 6]},
{from: [7, 8, 9], to: [10, 11, 12]},
{from: [13, 14, 15], to: [16, 17, 18]}
]);
var handle = T.float32.handle(lines, 0, "to", 1);
assertEq(T.Handle.get(handle), 5);
T.Handle.set(handle, 22);
assertEq(T.Handle.get(handle), 22);
assertEq(lines[0].to[1], 22);
}
testHandleGetSetWithScalarType();
function testHandleGetSetWithObjectType() {
var one = {x: 1};
var two = {x: 2};
var three = {x: 3};
var objects = new Objects([one, two, three]);
var handle = T.Object.handle(objects, 0);
assertEq(T.Handle.get(handle), one);
T.Handle.set(handle, three);
assertEq(T.Handle.get(handle), three);
assertEq(objects[0], three);
T.Handle.move(handle, objects, 1);
assertEq(T.Handle.get(handle), two);
}
testHandleGetSetWithScalarType();
function testHandleGetSetWithComplexType() {
var lines = new Lines([
{from: [1, 2, 3], to: [4, 5, 6]},
{from: [7, 8, 9], to: [10, 11, 12]},
{from: [13, 14, 15], to: [16, 17, 18]}
]);
var handle = Point.handle(lines, 0, "to");
T.Handle.set(handle, [22, 23, 24]);
assertEq(handle[0], 22);
assertEq(handle[1], 23);
assertEq(handle[2], 24);
assertEq(T.Handle.get(handle)[0], 22);
assertEq(T.Handle.get(handle)[1], 23);
assertEq(T.Handle.get(handle)[2], 24);
}
testHandleGetSetWithComplexType();
reportCompare(true, true);
print("Tests complete");
}
runTests();

Просмотреть файл

@ -1,162 +0,0 @@
// |reftest| skip-if(!this.hasOwnProperty("TypedObject"))
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
var BUGNUMBER = 898342;
var summary = 'Handle Move';
var T = TypedObject;
var Point = T.float32.array(3);
var Line = new T.StructType({from: Point, to: Point});
var Lines = Line.array(3);
function runTests() {
function testHandleToPoint() {
var lines = new Lines([
{from: [1, 2, 3], to: [4, 5, 6]},
{from: [7, 8, 9], to: [10, 11, 12]},
{from: [13, 14, 15], to: [16, 17, 18]}
]);
function allPoints(lines, func) {
var handle = Point.handle();
for (var i = 0; i < lines.length; i++) {
T.Handle.move(handle, lines, i, "from");
func(handle);
T.Handle.move(handle, lines, i, "to");
func(handle);
}
}
// Iterate over all ponts and mutate them in place:
allPoints(lines, function(p) {
p[0] += 100;
p[1] += 200;
p[2] += 300;
});
// Spot check the results
assertEq(lines[0].from[0], 101);
assertEq(lines[1].to[1], 211);
assertEq(lines[2].to[2], 318);
}
testHandleToPoint();
function testHandleToFloat() {
var lines = new Lines([
{from: [1, 2, 3], to: [4, 5, 6]},
{from: [7, 8, 9], to: [10, 11, 12]},
{from: [13, 14, 15], to: [16, 17, 18]}
]);
function allPoints(lines, func) {
var handle = T.float32.handle();
for (var i = 0; i < lines.length; i++) {
T.Handle.move(handle, lines, i, "from", 0);
func(handle);
T.Handle.move(handle, lines, i, "from", 1);
func(handle);
T.Handle.move(handle, lines, i, "from", 2);
func(handle);
T.Handle.move(handle, lines, i, "to", 0);
func(handle);
T.Handle.move(handle, lines, i, "to", 1);
func(handle);
T.Handle.move(handle, lines, i, "to", 2);
func(handle);
}
}
// Iterate over all ponts and mutate them in place:
allPoints(lines, function(p) {
T.Handle.set(p, T.Handle.get(p) + 100);
});
// Spot check the results
assertEq(lines[0].from[0], 101);
assertEq(lines[1].to[1], 111);
assertEq(lines[2].to[2], 118);
}
testHandleToFloat();
function testHandleToEquivalentType() {
var Point2 = T.float32.array(3);
assertEq(Point.equivalent(Point2), true);
var lines = new Lines([
{from: [1, 2, 3], to: [4, 5, 6]},
{from: [7, 8, 9], to: [10, 11, 12]},
{from: [13, 14, 15], to: [16, 17, 18]}
]);
var handle = Point2.handle(lines, 0, "to");
assertEq(handle[0], 4);
assertEq(handle[1], 5);
assertEq(handle[2], 6);
}
testHandleToEquivalentType();
function testHandleMoveToIllegalType() {
var lines = new Lines([
{from: [1, 2, 3], to: [4, 5, 6]},
{from: [7, 8, 9], to: [10, 11, 12]},
{from: [13, 14, 15], to: [16, 17, 18]}
]);
// Moving a handle to a value of incorrect type should report an error:
assertThrowsInstanceOf(function() {
Line.handle(lines);
}, TypeError, "handle moved to destination of incorrect type");
assertThrowsInstanceOf(function() {
var h = Line.handle();
T.Handle.move(h, lines);
}, TypeError, "handle moved to destination of incorrect type");
assertThrowsInstanceOf(function() {
var h = T.float32.handle();
T.Handle.move(h, lines, 0);
}, TypeError, "handle moved to destination of incorrect type");
}
testHandleMoveToIllegalType();
function testHandleMoveToIllegalProperty() {
var lines = new Lines([
{from: [1, 2, 3], to: [4, 5, 6]},
{from: [7, 8, 9], to: [10, 11, 12]},
{from: [13, 14, 15], to: [16, 17, 18]}
]);
assertThrowsInstanceOf(function() {
var h = Point.handle();
T.Handle.move(h, lines, 0, "foo");
}, TypeError, "No such property: foo");
assertThrowsInstanceOf(function() {
var h = Point.handle();
T.Handle.move(h, lines, 22, "to");
}, TypeError, "No such property: 22");
assertThrowsInstanceOf(function() {
var h = Point.handle();
T.Handle.move(h, lines, -100, "to");
}, TypeError, "No such property: -100");
}
testHandleMoveToIllegalProperty();
reportCompare(true, true);
print("Tests complete");
}
runTests();

Просмотреть файл

@ -1,49 +0,0 @@
// |reftest| skip-if(!this.hasOwnProperty("TypedObject"))
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
var BUGNUMBER = 898342;
var summary = 'Unattached handles';
var T = TypedObject;
function runTests() {
var Line = new T.StructType({from: T.uint8, to: T.uint8});
var Lines = Line.array(3);
// Create unattached handle to array, struct:
var handle = Lines.handle();
var handle0 = Line.handle();
// Accessing length throws:
assertThrowsInstanceOf(function() handle.length, TypeError,
"handle.length did not yield error");
// Accessing properties throws:
assertThrowsInstanceOf(function() handle[0], TypeError,
"Unattached handle did not yield error");
assertThrowsInstanceOf(function() handle0.from, TypeError,
"Unattached handle did not yield error");
// Handle.get() throws:
assertThrowsInstanceOf(function() T.Handle.get(handle), TypeError,
"Unattached handle did not yield error");
assertThrowsInstanceOf(function() T.Handle.get(handle0), TypeError,
"Unattached handle did not yield error");
// Handle.set() throws:
assertThrowsInstanceOf(function() T.Handle.set(handle, [{},{},{}]), TypeError,
"Unattached handle did not yield error");
assertThrowsInstanceOf(function() T.Handle.set(handle0, {}), TypeError,
"Unattached handle did not yield error");
reportCompare(true, true);
print("Tests complete");
}
runTests();

Просмотреть файл

@ -19,15 +19,11 @@ var int32 = TypedObject.int32;
var float32 = TypedObject.float32;
var float64 = TypedObject.float64;
var Handle = TypedObject.Handle;
function oneDimensionalArrayOfUints() {
var grain = uint32;
var type = grain.array(4);
var r1 = type.build(x => x * 2);
var r2 = type.build((x, out) => Handle.set(out, x * 2));
assertTypedEqual(type, r1, new type([0, 2, 4, 6]));
assertTypedEqual(type, r1, r2);
}
function oneDimensionalArrayOfStructs() {
@ -87,10 +83,8 @@ function threeDimensionalArrayOfUintsWithDepth3() {
var grain = uint32;
var type = grain.array(2).array(2).array(2);
var r1 = type.build(3, (x,y,z) => x * 100 + y * 10 + z);
var r2 = type.build(3, (x,y,z, out) => Handle.set(out, x * 100 + y * 10 + z));
assertTypedEqual(type, r1, new type([[[ 0, 1], [ 10, 11]],
[[100, 101], [110, 111]]]));
assertTypedEqual(type, r1, r2);
}
function threeDimensionalArrayOfUintsWithDepth2() {

Просмотреть файл

@ -19,8 +19,6 @@ var int32 = TypedObject.int32;
var float32 = TypedObject.float32;
var float64 = TypedObject.float64;
var Handle = TypedObject.Handle;
// Test name format:
// from<N>DimArrayOf<G1>sTo<G2>s where <N> is a positive integer (or its
@ -58,9 +56,7 @@ function fromTwoDimArrayOfUint8ToUint32s() {
var r1 = type.from(i1, 2, x => x*2);
var r2 = type.from(i1, 1, a => rowtype.from(a, 1, x => x*2));
var r3 = type.from(i1, 1,
a => rowtype.from(a, 1, (x, j, c, out) => Handle.set(out, x*2)));
var r4 = type.from(i1, 1, (a, j, c, out) => { out[0] = a[0]*2;
var r3 = type.from(i1, 1, (a, j, c, out) => { out[0] = a[0]*2;
out[1] = a[1]*2;
out[2] = a[2]*2;
out[3] = a[3]*2; });
@ -70,7 +66,6 @@ function fromTwoDimArrayOfUint8ToUint32s() {
[80, 82, 84, 86]]));
assertTypedEqual(type, r1, r2);
assertTypedEqual(type, r1, r3);
assertTypedEqual(type, r1, r4);
}
function fromTwoDimArrayOfUint32ToUint8s() {
@ -84,9 +79,7 @@ function fromTwoDimArrayOfUint32ToUint8s() {
var r1 = type.from(i1, 2, x => x*2);
var r2 = type.from(i1, 1, a => rowtype.from(a, 1, x => x*2));
var r3 = type.from(i1, 1,
a => rowtype.from(a, 1, (x, j, c, out) => Handle.set(out, x*2)));
var r4 = type.from(i1, 1, (a, j, c, out) => { out[0] = a[0]*2;
var r3 = type.from(i1, 1, (a, j, c, out) => { out[0] = a[0]*2;
out[1] = a[1]*2;
out[2] = a[2]*2;
out[3] = a[3]*2; });
@ -96,7 +89,6 @@ function fromTwoDimArrayOfUint32ToUint8s() {
[80, 82, 84, 86]]));
assertTypedEqual(type, r1, r2);
assertTypedEqual(type, r1, r3);
assertTypedEqual(type, r1, r4);
}
function fromOneDimArrayOfArrayOfUint8ToUint32s() {
@ -110,13 +102,10 @@ function fromOneDimArrayOfArrayOfUint8ToUint32s() {
function combine(a,b,c,d) { return a << 24 | b << 16 | c << 8 | d; }
var r1 = type.from(i1, x => combine(x[0], x[1], x[2], x[3]));
var r2 = type.from(i1, 1,
(x, i, c, out) => Handle.set(out, combine(x[0], x[1], x[2], x[3])));
assertTypedEqual(type, r1, new type([0xddccbbaa,
0x09080706,
0x15141312,
0x23324150]));
assertTypedEqual(type, r1, r2);
}
function fromOneDimArrayOfUint32ToArrayOfUint8s() {

Просмотреть файл

@ -19,8 +19,6 @@ var int32 = TypedObject.int32;
var float32 = TypedObject.float32;
var float64 = TypedObject.float64;
var Handle = TypedObject.Handle;
// Test name format:
// map<N>DimArrayOf<G1>sTo<G2>s where <N> is a positive integer (or its
@ -54,8 +52,7 @@ function mapTwoDimArrayOfUint8() {
var r1 = i1.map(2, x => x*2);
var r2 = i1.map(1, a => a.map(1, x => x*2));
var r3 = i1.map(1, a => a.map(1, (x, j, c, out) => Handle.set(out, x*2)));
var r4 = i1.map(1, (a, j, c, out) => { out[0] = a[0]*2;
var r3 = i1.map(1, (a, j, c, out) => { out[0] = a[0]*2;
out[1] = a[1]*2;
out[2] = a[2]*2;
out[3] = a[3]*2; });
@ -65,7 +62,6 @@ function mapTwoDimArrayOfUint8() {
[80, 82, 84, 86]]));
assertTypedEqual(type, r1, r2);
assertTypedEqual(type, r1, r3);
assertTypedEqual(type, r1, r4);
}
function mapTwoDimArrayOfUint32() {
@ -77,8 +73,7 @@ function mapTwoDimArrayOfUint32() {
var r1 = i1.map(2, x => x*2);
var r2 = i1.map(1, a => a.map(1, x => x*2));
var r3 = i1.map(1, a => a.map(1, (x, j, c, out) => Handle.set(out, x*2)));
var r4 = i1.map(1, (a, j, c, out) => { out[0] = a[0]*2;
var r3 = i1.map(1, (a, j, c, out) => { out[0] = a[0]*2;
out[1] = a[1]*2;
out[2] = a[2]*2;
out[3] = a[3]*2; });
@ -88,7 +83,6 @@ function mapTwoDimArrayOfUint32() {
[80, 82, 84, 86]]));
assertTypedEqual(type, r1, r2);
assertTypedEqual(type, r1, r3);
assertTypedEqual(type, r1, r4);
}
var Grain = new StructType({f: uint32});