Update configs. IGNORE BROKEN CHANGESETS CLOSED TREE NO BUG a=release ba=release
[gecko.git] / js / src / jsapi-tests / testGCCellPtr.cpp
blobef74808b52631e84ce55bd031944d7ac92c216a3
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "jsapi.h"
9 #include "jspubtd.h"
11 #include "js/CompilationAndEvaluation.h" // JS::Compile
12 #include "js/SourceText.h" // JS::Source{Ownership,Text}
13 #include "jsapi-tests/tests.h"
15 JS::GCCellPtr GivesAndTakesCells(JS::GCCellPtr cell) { return cell; }
17 BEGIN_TEST(testGCCellPtr) {
18 JS::RootedObject obj(cx, JS_NewPlainObject(cx));
19 CHECK(obj);
21 JS::RootedString str(cx, JS_NewStringCopyZ(cx, "probably foobar"));
22 CHECK(str);
24 const char* code = "function foo() { return 'bar'; }";
26 JS::CompileOptions opts(cx);
28 JS::SourceText<mozilla::Utf8Unit> srcBuf;
29 CHECK(srcBuf.init(cx, code, strlen(code), JS::SourceOwnership::Borrowed));
31 JS::RootedScript script(cx, JS::Compile(cx, opts, srcBuf));
32 CHECK(script);
34 CHECK(!JS::GCCellPtr(nullptr));
36 CHECK(JS::GCCellPtr(obj.get()));
37 CHECK(JS::GCCellPtr(obj.get()).kind() == JS::TraceKind::Object);
38 CHECK(JS::ObjectValue(*obj).toGCCellPtr().kind() == JS::TraceKind::Object);
40 CHECK(JS::GCCellPtr(str.get()));
41 CHECK(JS::GCCellPtr(str.get()).kind() == JS::TraceKind::String);
42 CHECK(JS::StringValue(str).toGCCellPtr().kind() == JS::TraceKind::String);
44 CHECK(JS::GCCellPtr(script.get()));
45 CHECK(!JS::GCCellPtr(nullptr));
46 CHECK(JS::GCCellPtr(script.get()).kind() == JS::TraceKind::Script);
48 JS::GCCellPtr objcell(obj.get());
49 JS::GCCellPtr scriptcell = JS::GCCellPtr(script.get());
50 CHECK(GivesAndTakesCells(objcell));
51 CHECK(GivesAndTakesCells(scriptcell));
53 JS::GCCellPtr copy = objcell;
54 CHECK(copy == objcell);
56 return true;
58 END_TEST(testGCCellPtr)