Debian Codegraph @

js/src/jsapi-tests/testPrivateGCThingValue.cpp

raw
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/. */
7
8 #include "mozilla/Utf8.h" // mozilla::Utf8Unit
9
10 #include "jsapi.h"
11
12 #include "js/CompilationAndEvaluation.h" // JS::Compile
13 #include "js/HeapAPI.h"
14 #include "js/SourceText.h" // JS::Source{Ownership,Text}
15 #include "jsapi-tests/tests.h"
16 #include "util/Text.h"
17
18 class TestTracer final : public JS::CallbackTracer {
19 void onChild(JS::GCCellPtr thing, const char* name) override {
20 if (thing.asCell() == expectedCell && thing.kind() == expectedKind) {
21 found = true;
22 }
23 }
24
25 public:
26 js::gc::Cell* expectedCell;
27 JS::TraceKind expectedKind;
28 bool found;
29
30 explicit TestTracer(JSContext* cx)
31 : JS::CallbackTracer(cx),
32 expectedCell(nullptr),
33 expectedKind(static_cast<JS::TraceKind>(0)),
34 found(false) {}
35 };
36
37 static const JSClass TestClass = {
38 "TestClass",
39 JSCLASS_HAS_RESERVED_SLOTS(1),
40 };
41
42 BEGIN_TEST(testPrivateGCThingValue) {
43 JS::RootedObject obj(cx, JS_NewObject(cx, &TestClass));
44 CHECK(obj);
45
46 // Make a JSScript to stick into a PrivateGCThingValue.
47 static const char code[] = "'objet petit a'";
48
49 JS::CompileOptions options(cx);
50 options.setFileAndLine(__FILE__, __LINE__);
51
52 JS::SourceText<mozilla::Utf8Unit> srcBuf;
53 CHECK(srcBuf.init(cx, code, js_strlen(code), JS::SourceOwnership::Borrowed));
54
55 JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf));
56 CHECK(script);
57 JS_SetReservedSlot(obj, 0, PrivateGCThingValue(script));
58
59 TestTracer trc(cx);
60 trc.expectedCell = script;
61 trc.expectedKind = JS::TraceKind::Script;
62 JS::TraceChildren(&trc, JS::GCCellPtr(obj, JS::TraceKind::Object));
63 CHECK(trc.found);
64
65 return true;
66 }
67 END_TEST(testPrivateGCThingValue)