| 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 "jsapi.h" |
| 9 |
#include "jspubtd.h" |
| 10 |
|
| 11 |
#include "js/CompilationAndEvaluation.h" // JS::Compile |
| 12 |
#include "js/SourceText.h" // JS::Source{Ownership,Text} |
| 13 |
#include "jsapi-tests/tests.h" |
| 14 |
|
| 15 |
JS::GCCellPtr GivesAndTakesCells(JS::GCCellPtr cell) { return cell; } |
| 16 |
|
| 17 |
BEGIN_TEST(testGCCellPtr) { |
| 18 |
JS::RootedObject obj(cx, JS_NewPlainObject(cx)); |
| 19 |
CHECK(obj); |
| 20 |
|
| 21 |
JS::RootedString str(cx, JS_NewStringCopyZ(cx, "probably foobar")); |
| 22 |
CHECK(str); |
| 23 |
|
| 24 |
const char* code = "function foo() { return 'bar'; }"; |
| 25 |
|
| 26 |
JS::CompileOptions opts(cx); |
| 27 |
|
| 28 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 29 |
CHECK(srcBuf.init(cx, code, strlen(code), JS::SourceOwnership::Borrowed)); |
| 30 |
|
| 31 |
JS::RootedScript script(cx, JS::Compile(cx, opts, srcBuf)); |
| 32 |
CHECK(script); |
| 33 |
|
| 34 |
CHECK(!JS::GCCellPtr(nullptr)); |
| 35 |
|
| 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); |
| 39 |
|
| 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); |
| 43 |
|
| 44 |
CHECK(JS::GCCellPtr(script.get())); |
| 45 |
CHECK(!JS::GCCellPtr(nullptr)); |
| 46 |
CHECK(JS::GCCellPtr(script.get()).kind() == JS::TraceKind::Script); |
| 47 |
|
| 48 |
JS::GCCellPtr objcell(obj.get()); |
| 49 |
JS::GCCellPtr scriptcell = JS::GCCellPtr(script.get()); |
| 50 |
CHECK(GivesAndTakesCells(objcell)); |
| 51 |
CHECK(GivesAndTakesCells(scriptcell)); |
| 52 |
|
| 53 |
JS::GCCellPtr copy = objcell; |
| 54 |
CHECK(copy == objcell); |
| 55 |
|
| 56 |
return true; |
| 57 |
} |
| 58 |
END_TEST(testGCCellPtr) |