js/src/jsapi-tests/testConservativeGC.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jsapi-tests/testConservativeGC.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#if !defined(JSGC_USE_EXACT_ROOTING)
     1.9 +
    1.10 +#include "jsobj.h"
    1.11 +
    1.12 +#include "jsapi-tests/tests.h"
    1.13 +#include "vm/String.h"
    1.14 +
    1.15 +BEGIN_TEST(testConservativeGC)
    1.16 +{
    1.17 +    JS::RootedValue v2(cx);
    1.18 +    EVAL("({foo: 'bar'});", &v2);
    1.19 +    CHECK(v2.isObject());
    1.20 +    char objCopy[sizeof(JSObject)];
    1.21 +    js_memcpy(&objCopy, JSVAL_TO_OBJECT(v2), sizeof(JSObject));
    1.22 +
    1.23 +    JS::RootedValue v3(cx);
    1.24 +    EVAL("String(Math.PI);", &v3);
    1.25 +    CHECK(JSVAL_IS_STRING(v3));
    1.26 +    char strCopy[sizeof(JSString)];
    1.27 +    js_memcpy(&strCopy, JSVAL_TO_STRING(v3), sizeof(JSString));
    1.28 +
    1.29 +    JS::RootedValue tmp(cx);
    1.30 +    EVAL("({foo2: 'bar2'});", &tmp);
    1.31 +    CHECK(tmp.isObject());
    1.32 +    JS::RootedObject obj2(cx, JSVAL_TO_OBJECT(tmp));
    1.33 +    char obj2Copy[sizeof(JSObject)];
    1.34 +    js_memcpy(&obj2Copy, obj2, sizeof(JSObject));
    1.35 +
    1.36 +    EVAL("String(Math.sqrt(3));", &tmp);
    1.37 +    CHECK(JSVAL_IS_STRING(tmp));
    1.38 +    JS::RootedString str2(cx, JSVAL_TO_STRING(tmp));
    1.39 +    char str2Copy[sizeof(JSString)];
    1.40 +    js_memcpy(&str2Copy, str2, sizeof(JSString));
    1.41 +
    1.42 +    tmp = JSVAL_NULL;
    1.43 +
    1.44 +    JS_GC(rt);
    1.45 +
    1.46 +    EVAL("var a = [];\n"
    1.47 +         "for (var i = 0; i != 10000; ++i) {\n"
    1.48 +         "a.push(i + 0.1, [1, 2], String(Math.sqrt(i)), {a: i});\n"
    1.49 +         "}", &tmp);
    1.50 +
    1.51 +    JS_GC(rt);
    1.52 +
    1.53 +    checkObjectFields((JSObject *)objCopy, JSVAL_TO_OBJECT(v2));
    1.54 +    CHECK(!memcmp(strCopy, JSVAL_TO_STRING(v3), sizeof(strCopy)));
    1.55 +
    1.56 +    checkObjectFields((JSObject *)obj2Copy, obj2);
    1.57 +    CHECK(!memcmp(str2Copy, str2, sizeof(str2Copy)));
    1.58 +
    1.59 +    return true;
    1.60 +}
    1.61 +
    1.62 +bool checkObjectFields(JSObject *savedCopy, JSObject *obj)
    1.63 +{
    1.64 +    /* Ignore fields which are unstable across GCs. */
    1.65 +    CHECK(savedCopy->lastProperty() == obj->lastProperty());
    1.66 +    return true;
    1.67 +}
    1.68 +
    1.69 +END_TEST(testConservativeGC)
    1.70 +
    1.71 +BEGIN_TEST(testDerivedValues)
    1.72 +{
    1.73 +  JSString *str = JS_NewStringCopyZ(cx, "once upon a midnight dreary");
    1.74 +  JS::Anchor<JSString *> str_anchor(str);
    1.75 +  static const jschar expected[] = { 'o', 'n', 'c', 'e' };
    1.76 +  const jschar *ch = JS_GetStringCharsZ(cx, str);
    1.77 +  str = nullptr;
    1.78 +
    1.79 +  /* Do a lot of allocation and collection. */
    1.80 +  for (int i = 0; i < 3; i++) {
    1.81 +    for (int j = 0; j < 1000; j++)
    1.82 +      JS_NewStringCopyZ(cx, "as I pondered weak and weary");
    1.83 +    JS_GC(rt);
    1.84 +  }
    1.85 +
    1.86 +  CHECK(!memcmp(ch, expected, sizeof(expected)));
    1.87 +  return true;
    1.88 +}
    1.89 +END_TEST(testDerivedValues)
    1.90 +
    1.91 +#endif /* !defined(JSGC_USE_EXACT_ROOTING) */

mercurial