Bug 1220031 - Add JS::ubi::Node::scriptFilename; r=sfink

This commit is contained in:
Nick Fitzgerald 2015-11-04 05:49:33 -08:00
Родитель fb394133cc
Коммит 704858e786
5 изменённых файлов: 65 добавлений и 0 удалений

Просмотреть файл

@ -639,6 +639,12 @@ class Base {
return true; return true;
} }
// Methods for CoarseType::Script referents
// Return the script's source's filename if available. If unavailable,
// return nullptr.
virtual const char* scriptFilename() const { return nullptr; }
private: private:
Base(const Base& rhs) = delete; Base(const Base& rhs) = delete;
Base& operator=(const Base& rhs) = delete; Base& operator=(const Base& rhs) = delete;
@ -779,6 +785,8 @@ class Node {
return base()->jsObjectConstructorName(cx, outName); return base()->jsObjectConstructorName(cx, outName);
} }
const char* scriptFilename() const { return base()->scriptFilename(); }
using Size = Base::Size; using Size = Base::Size;
Size size(mozilla::MallocSizeOf mallocSizeof) const { Size size(mozilla::MallocSizeOf mallocSizeof) const {
return base()->size(mallocSizeof); return base()->size(mallocSizeof);
@ -1052,6 +1060,7 @@ struct Concrete<JS::Symbol> : TracerConcrete<JS::Symbol> {
template<> struct Concrete<JSScript> : TracerConcreteWithCompartment<JSScript> { template<> struct Concrete<JSScript> : TracerConcreteWithCompartment<JSScript> {
CoarseType coarseType() const final { return CoarseType::Script; } CoarseType coarseType() const final { return CoarseType::Script; }
Size size(mozilla::MallocSizeOf mallocSizeOf) const override; Size size(mozilla::MallocSizeOf mallocSizeOf) const override;
const char* scriptFilename() const final;
protected: protected:
explicit Concrete(JSScript *ptr) : TracerConcreteWithCompartment<JSScript>(ptr) { } explicit Concrete(JSScript *ptr) : TracerConcreteWithCompartment<JSScript>(ptr) { }

Просмотреть файл

@ -782,6 +782,7 @@ namespace ubi {
template<> template<>
struct Concrete<js::jit::JitCode> : TracerConcrete<js::jit::JitCode> { struct Concrete<js::jit::JitCode> : TracerConcrete<js::jit::JitCode> {
CoarseType coarseType() const final { return CoarseType::Script; } CoarseType coarseType() const final { return CoarseType::Script; }
Size size(mozilla::MallocSizeOf mallocSizeOf) const override { Size size(mozilla::MallocSizeOf mallocSizeOf) const override {
Size size = js::gc::Arena::thingSize(get().asTenured().getAllocKind()); Size size = js::gc::Arena::thingSize(get().asTenured().getAllocKind());
size += get().bufferSize(); size += get().bufferSize();

Просмотреть файл

@ -346,3 +346,37 @@ BEGIN_TEST(test_ubiPostOrder)
return true; return true;
} }
END_TEST(test_ubiPostOrder) END_TEST(test_ubiPostOrder)
BEGIN_TEST(test_JS_ubi_Node_scriptFilename)
{
JS::RootedValue val(cx);
CHECK(evaluate("(function one() { \n" // 1
" return (function two() { \n" // 2
" return (function three() { \n" // 3
" return function four() {}; \n" // 4
" }()); \n" // 5
" }()); \n" // 6
"}()); \n", // 7
"my-cool-filename.js",
1,
&val));
CHECK(val.isObject());
JS::RootedObject obj(cx, &val.toObject());
CHECK(obj->is<JSFunction>());
JS::RootedFunction func(cx, &obj->as<JSFunction>());
JS::RootedScript script(cx, func->getOrCreateScript(cx));
CHECK(script);
CHECK(script->filename());
JS::ubi::Node node(script);
const char* filename = node.scriptFilename();
CHECK(filename);
CHECK(strcmp(filename, script->filename()) == 0);
CHECK(strcmp(filename, "my-cool-filename.js") == 0);
return true;
}
END_TEST(test_JS_ubi_Node_scriptFilename)

Просмотреть файл

@ -4406,6 +4406,12 @@ JS::ubi::Concrete<JSScript>::size(mozilla::MallocSizeOf mallocSizeOf) const
return size; return size;
} }
const char*
JS::ubi::Concrete<JSScript>::scriptFilename() const
{
return get().filename();
}
JS::ubi::Node::Size JS::ubi::Node::Size
JS::ubi::Concrete<js::LazyScript>::size(mozilla::MallocSizeOf mallocSizeOf) const JS::ubi::Concrete<js::LazyScript>::size(mozilla::MallocSizeOf mallocSizeOf) const
{ {
@ -4413,3 +4419,17 @@ JS::ubi::Concrete<js::LazyScript>::size(mozilla::MallocSizeOf mallocSizeOf) cons
size += get().sizeOfExcludingThis(mallocSizeOf); size += get().sizeOfExcludingThis(mallocSizeOf);
return size; return size;
} }
const char*
JS::ubi::Concrete<js::LazyScript>::scriptFilename() const
{
auto sourceObject = get().sourceObject();
if (!sourceObject)
return nullptr;
auto source = sourceObject->source();
if (!source)
return nullptr;
return source->filename();
}

Просмотреть файл

@ -2487,6 +2487,7 @@ template<>
struct Concrete<js::LazyScript> : TracerConcrete<js::LazyScript> { struct Concrete<js::LazyScript> : TracerConcrete<js::LazyScript> {
CoarseType coarseType() const final { return CoarseType::Script; } CoarseType coarseType() const final { return CoarseType::Script; }
Size size(mozilla::MallocSizeOf mallocSizeOf) const override; Size size(mozilla::MallocSizeOf mallocSizeOf) const override;
const char* scriptFilename() const final;
protected: protected:
explicit Concrete(js::LazyScript *ptr) : TracerConcrete<js::LazyScript>(ptr) { } explicit Concrete(js::LazyScript *ptr) : TracerConcrete<js::LazyScript>(ptr) { }