Commit 81644442 authored by Jan de Mooij's avatar Jan de Mooij
Browse files

Bug 1782487 part 3 - Add some tests. r=jonco

This also adds testing functions to allocate and check objects with many reserved slots.

Depends on D153437

Differential Revision: https://phabricator.services.mozilla.com/D153438
parent 1d6aa925
Loading
Loading
Loading
Loading
+59 −0
Original line number Diff line number Diff line
@@ -2964,6 +2964,54 @@ static bool NewObjectWithAddPropertyHook(JSContext* cx, unsigned argc,
  return true;
}

static constexpr JSClass ObjectWithManyReservedSlotsClass = {
    "ObjectWithManyReservedSlots", JSCLASS_HAS_RESERVED_SLOTS(40)};

static bool NewObjectWithManyReservedSlots(JSContext* cx, unsigned argc,
                                           Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

  static constexpr size_t NumReservedSlots =
      JSCLASS_RESERVED_SLOTS(&ObjectWithManyReservedSlotsClass);
  static_assert(NumReservedSlots > NativeObject::MAX_FIXED_SLOTS);

  RootedObject obj(cx, JS_NewObject(cx, &ObjectWithManyReservedSlotsClass));
  if (!obj) {
    return false;
  }

  for (size_t i = 0; i < NumReservedSlots; i++) {
    JS_SetReservedSlot(obj, i, Int32Value(i));
  }

  args.rval().setObject(*obj);
  return true;
}

static bool CheckObjectWithManyReservedSlots(JSContext* cx, unsigned argc,
                                             Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

  if (args.length() != 1 || !args[0].isObject() ||
      args[0].toObject().getClass() != &ObjectWithManyReservedSlotsClass) {
    JS_ReportErrorASCII(cx,
                        "Expected object from newObjectWithManyReservedSlots");
    return false;
  }

  JSObject* obj = &args[0].toObject();

  static constexpr size_t NumReservedSlots =
      JSCLASS_RESERVED_SLOTS(&ObjectWithManyReservedSlotsClass);

  for (size_t i = 0; i < NumReservedSlots; i++) {
    MOZ_RELEASE_ASSERT(JS::GetReservedSlot(obj, i).toInt32() == int32_t(i));
  }

  args.rval().setUndefined();
  return true;
}

static bool SetWatchtowerCallback(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

@@ -8028,6 +8076,17 @@ static const JSFunctionSpecWithHelp TestingFunctions[] = {
"  increments the value of the _propertiesAdded data property on the object\n"
"  when a new property is added."),

    JS_FN_HELP("newObjectWithManyReservedSlots", NewObjectWithManyReservedSlots, 0, 0,
"newObjectWithManyReservedSlots()",
"  Returns a new object with many reserved slots. The slots are initialized to int32\n"
"  values. checkObjectWithManyReservedSlots can be used to check the slots still\n"
"  hold these values."),

    JS_FN_HELP("checkObjectWithManyReservedSlots", CheckObjectWithManyReservedSlots, 1, 0,
"checkObjectWithManyReservedSlots(obj)",
"  Checks the reserved slots set by newObjectWithManyReservedSlots still hold the expected\n"
"  values."),

    JS_FN_HELP("setWatchtowerCallback", SetWatchtowerCallback, 1, 0,
"setWatchtowerCallback(function)",
"  Use the given function as callback for objects added to Watchtower by\n"
+15 −0
Original line number Diff line number Diff line
function f(o) {
    for (var i = 0; i < 100; i++) {
        o["a" + i] = i;
    }
    for (var i = 0; i < 100; i++) {
        delete o["a" + i];
    }
    o.x = 123;
    assertEq(Object.keys(o).length, 1);
}
f({});
f([]);
let obj = newObjectWithManyReservedSlots();
f(obj);
checkObjectWithManyReservedSlots(obj);
+15 −0
Original line number Diff line number Diff line
function f(o) {
    for (var i = 1; i <= 5; i += 4) {
        var start = i * 4000;
        var end = (start * 1.4)|0;
        for (var j = end; j > start; j--) {
            o[j] = j;
        }
    }
    assertEq(Object.keys(o).length, 9600);
}
f({});
f([]);
let obj = newObjectWithManyReservedSlots();
f(obj);
checkObjectWithManyReservedSlots(obj);