| 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 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "vm/BuiltinObjectKind.h" |
| 8 |
|
| 9 |
#include "jspubtd.h" |
| 10 |
|
| 11 |
#include "frontend/ParserAtom.h" |
| 12 |
#include "vm/GlobalObject.h" |
| 13 |
|
| 14 |
using namespace js; |
| 15 |
|
| 16 |
static JSProtoKey ToProtoKey(BuiltinObjectKind kind) { |
| 17 |
switch (kind) { |
| 18 |
case BuiltinObjectKind::Array: |
| 19 |
return JSProto_Array; |
| 20 |
case BuiltinObjectKind::ListFormat: |
| 21 |
return JSProto_ListFormat; |
| 22 |
case BuiltinObjectKind::Map: |
| 23 |
return JSProto_Map; |
| 24 |
case BuiltinObjectKind::Promise: |
| 25 |
return JSProto_Promise; |
| 26 |
case BuiltinObjectKind::RegExp: |
| 27 |
return JSProto_RegExp; |
| 28 |
case BuiltinObjectKind::Set: |
| 29 |
return JSProto_Set; |
| 30 |
case BuiltinObjectKind::Symbol: |
| 31 |
return JSProto_Symbol; |
| 32 |
|
| 33 |
case BuiltinObjectKind::FunctionPrototype: |
| 34 |
return JSProto_Function; |
| 35 |
case BuiltinObjectKind::IteratorPrototype: |
| 36 |
return JSProto_Iterator; |
| 37 |
|
| 38 |
case BuiltinObjectKind::DateTimeFormatPrototype: |
| 39 |
return JSProto_DateTimeFormat; |
| 40 |
case BuiltinObjectKind::NumberFormatPrototype: |
| 41 |
return JSProto_NumberFormat; |
| 42 |
|
| 43 |
case BuiltinObjectKind::None: |
| 44 |
break; |
| 45 |
} |
| 46 |
MOZ_CRASH("Unexpected builtin object kind"); |
| 47 |
} |
| 48 |
|
| 49 |
static bool IsPrototype(BuiltinObjectKind kind) { |
| 50 |
switch (kind) { |
| 51 |
case BuiltinObjectKind::Array: |
| 52 |
case BuiltinObjectKind::ListFormat: |
| 53 |
case BuiltinObjectKind::Map: |
| 54 |
case BuiltinObjectKind::Promise: |
| 55 |
case BuiltinObjectKind::RegExp: |
| 56 |
case BuiltinObjectKind::Set: |
| 57 |
case BuiltinObjectKind::Symbol: |
| 58 |
return false; |
| 59 |
|
| 60 |
case BuiltinObjectKind::FunctionPrototype: |
| 61 |
case BuiltinObjectKind::IteratorPrototype: |
| 62 |
return true; |
| 63 |
|
| 64 |
case BuiltinObjectKind::DateTimeFormatPrototype: |
| 65 |
case BuiltinObjectKind::NumberFormatPrototype: |
| 66 |
return true; |
| 67 |
|
| 68 |
case BuiltinObjectKind::None: |
| 69 |
break; |
| 70 |
} |
| 71 |
MOZ_CRASH("Unexpected builtin object kind"); |
| 72 |
} |
| 73 |
|
| 74 |
BuiltinObjectKind js::BuiltinConstructorForName( |
| 75 |
frontend::TaggedParserAtomIndex name) { |
| 76 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::Array()) { |
| 77 |
return BuiltinObjectKind::Array; |
| 78 |
} |
| 79 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::ListFormat()) { |
| 80 |
return BuiltinObjectKind::ListFormat; |
| 81 |
} |
| 82 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::Map()) { |
| 83 |
return BuiltinObjectKind::Map; |
| 84 |
} |
| 85 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::Promise()) { |
| 86 |
return BuiltinObjectKind::Promise; |
| 87 |
} |
| 88 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::RegExp()) { |
| 89 |
return BuiltinObjectKind::RegExp; |
| 90 |
} |
| 91 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::Set()) { |
| 92 |
return BuiltinObjectKind::Set; |
| 93 |
} |
| 94 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::Symbol()) { |
| 95 |
return BuiltinObjectKind::Symbol; |
| 96 |
} |
| 97 |
return BuiltinObjectKind::None; |
| 98 |
} |
| 99 |
|
| 100 |
BuiltinObjectKind js::BuiltinPrototypeForName( |
| 101 |
frontend::TaggedParserAtomIndex name) { |
| 102 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::Function()) { |
| 103 |
return BuiltinObjectKind::FunctionPrototype; |
| 104 |
} |
| 105 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::Iterator()) { |
| 106 |
return BuiltinObjectKind::IteratorPrototype; |
| 107 |
} |
| 108 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::DateTimeFormat()) { |
| 109 |
return BuiltinObjectKind::DateTimeFormatPrototype; |
| 110 |
} |
| 111 |
if (name == frontend::TaggedParserAtomIndex::WellKnown::NumberFormat()) { |
| 112 |
return BuiltinObjectKind::NumberFormatPrototype; |
| 113 |
} |
| 114 |
return BuiltinObjectKind::None; |
| 115 |
} |
| 116 |
|
| 117 |
JSObject* js::MaybeGetBuiltinObject(GlobalObject* global, |
| 118 |
BuiltinObjectKind kind) { |
| 119 |
JSProtoKey key = ToProtoKey(kind); |
| 120 |
if (IsPrototype(kind)) { |
| 121 |
return global->maybeGetPrototype(key); |
| 122 |
} |
| 123 |
return global->maybeGetConstructor(key); |
| 124 |
} |
| 125 |
|
| 126 |
JSObject* js::GetOrCreateBuiltinObject(JSContext* cx, BuiltinObjectKind kind) { |
| 127 |
JSProtoKey key = ToProtoKey(kind); |
| 128 |
if (IsPrototype(kind)) { |
| 129 |
return GlobalObject::getOrCreatePrototype(cx, key); |
| 130 |
} |
| 131 |
return GlobalObject::getOrCreateConstructor(cx, key); |
| 132 |
} |
| 133 |
|
| 134 |
const char* js::BuiltinObjectName(BuiltinObjectKind kind) { |
| 135 |
switch (kind) { |
| 136 |
case BuiltinObjectKind::Array: |
| 137 |
return "Array"; |
| 138 |
case BuiltinObjectKind::ListFormat: |
| 139 |
return "ListFormat"; |
| 140 |
case BuiltinObjectKind::Map: |
| 141 |
return "Map"; |
| 142 |
case BuiltinObjectKind::Promise: |
| 143 |
return "Promise"; |
| 144 |
case BuiltinObjectKind::RegExp: |
| 145 |
return "RegExp"; |
| 146 |
case BuiltinObjectKind::Set: |
| 147 |
return "Set"; |
| 148 |
case BuiltinObjectKind::Symbol: |
| 149 |
return "Symbol"; |
| 150 |
|
| 151 |
case BuiltinObjectKind::FunctionPrototype: |
| 152 |
return "Function.prototype"; |
| 153 |
case BuiltinObjectKind::IteratorPrototype: |
| 154 |
return "Iterator.prototype"; |
| 155 |
|
| 156 |
case BuiltinObjectKind::DateTimeFormatPrototype: |
| 157 |
return "DateTimeFormat.prototype"; |
| 158 |
case BuiltinObjectKind::NumberFormatPrototype: |
| 159 |
return "NumberFormat.prototype"; |
| 160 |
|
| 161 |
case BuiltinObjectKind::None: |
| 162 |
break; |
| 163 |
} |
| 164 |
MOZ_CRASH("Unexpected builtin object kind"); |
| 165 |
} |