BuiltinObjectKind.cpp (4836B)
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::Map: 21 return JSProto_Map; 22 case BuiltinObjectKind::Promise: 23 return JSProto_Promise; 24 case BuiltinObjectKind::RegExp: 25 return JSProto_RegExp; 26 case BuiltinObjectKind::Set: 27 return JSProto_Set; 28 case BuiltinObjectKind::Symbol: 29 return JSProto_Symbol; 30 31 case BuiltinObjectKind::FunctionPrototype: 32 return JSProto_Function; 33 case BuiltinObjectKind::IteratorPrototype: 34 return JSProto_Iterator; 35 36 case BuiltinObjectKind::DateTimeFormatPrototype: 37 return JSProto_DateTimeFormat; 38 case BuiltinObjectKind::NumberFormatPrototype: 39 return JSProto_NumberFormat; 40 41 case BuiltinObjectKind::None: 42 break; 43 } 44 MOZ_CRASH("Unexpected builtin object kind"); 45 } 46 47 static bool IsPrototype(BuiltinObjectKind kind) { 48 switch (kind) { 49 case BuiltinObjectKind::Array: 50 case BuiltinObjectKind::Map: 51 case BuiltinObjectKind::Promise: 52 case BuiltinObjectKind::RegExp: 53 case BuiltinObjectKind::Set: 54 case BuiltinObjectKind::Symbol: 55 return false; 56 57 case BuiltinObjectKind::FunctionPrototype: 58 case BuiltinObjectKind::IteratorPrototype: 59 return true; 60 61 case BuiltinObjectKind::DateTimeFormatPrototype: 62 case BuiltinObjectKind::NumberFormatPrototype: 63 return true; 64 65 case BuiltinObjectKind::None: 66 break; 67 } 68 MOZ_CRASH("Unexpected builtin object kind"); 69 } 70 71 BuiltinObjectKind js::BuiltinConstructorForName( 72 frontend::TaggedParserAtomIndex name) { 73 if (name == frontend::TaggedParserAtomIndex::WellKnown::Array()) { 74 return BuiltinObjectKind::Array; 75 } 76 if (name == frontend::TaggedParserAtomIndex::WellKnown::Map()) { 77 return BuiltinObjectKind::Map; 78 } 79 if (name == frontend::TaggedParserAtomIndex::WellKnown::Promise()) { 80 return BuiltinObjectKind::Promise; 81 } 82 if (name == frontend::TaggedParserAtomIndex::WellKnown::RegExp()) { 83 return BuiltinObjectKind::RegExp; 84 } 85 if (name == frontend::TaggedParserAtomIndex::WellKnown::Set()) { 86 return BuiltinObjectKind::Set; 87 } 88 if (name == frontend::TaggedParserAtomIndex::WellKnown::Symbol()) { 89 return BuiltinObjectKind::Symbol; 90 } 91 return BuiltinObjectKind::None; 92 } 93 94 BuiltinObjectKind js::BuiltinPrototypeForName( 95 frontend::TaggedParserAtomIndex name) { 96 if (name == frontend::TaggedParserAtomIndex::WellKnown::Function()) { 97 return BuiltinObjectKind::FunctionPrototype; 98 } 99 if (name == frontend::TaggedParserAtomIndex::WellKnown::Iterator()) { 100 return BuiltinObjectKind::IteratorPrototype; 101 } 102 if (name == frontend::TaggedParserAtomIndex::WellKnown::DateTimeFormat()) { 103 return BuiltinObjectKind::DateTimeFormatPrototype; 104 } 105 if (name == frontend::TaggedParserAtomIndex::WellKnown::NumberFormat()) { 106 return BuiltinObjectKind::NumberFormatPrototype; 107 } 108 return BuiltinObjectKind::None; 109 } 110 111 JSObject* js::MaybeGetBuiltinObject(GlobalObject* global, 112 BuiltinObjectKind kind) { 113 JSProtoKey key = ToProtoKey(kind); 114 if (IsPrototype(kind)) { 115 return global->maybeGetPrototype(key); 116 } 117 return global->maybeGetConstructor(key); 118 } 119 120 JSObject* js::GetOrCreateBuiltinObject(JSContext* cx, BuiltinObjectKind kind) { 121 JSProtoKey key = ToProtoKey(kind); 122 if (IsPrototype(kind)) { 123 return GlobalObject::getOrCreatePrototype(cx, key); 124 } 125 return GlobalObject::getOrCreateConstructor(cx, key); 126 } 127 128 const char* js::BuiltinObjectName(BuiltinObjectKind kind) { 129 switch (kind) { 130 case BuiltinObjectKind::Array: 131 return "Array"; 132 case BuiltinObjectKind::Map: 133 return "Map"; 134 case BuiltinObjectKind::Promise: 135 return "Promise"; 136 case BuiltinObjectKind::RegExp: 137 return "RegExp"; 138 case BuiltinObjectKind::Set: 139 return "Set"; 140 case BuiltinObjectKind::Symbol: 141 return "Symbol"; 142 143 case BuiltinObjectKind::FunctionPrototype: 144 return "Function.prototype"; 145 case BuiltinObjectKind::IteratorPrototype: 146 return "Iterator.prototype"; 147 148 case BuiltinObjectKind::DateTimeFormatPrototype: 149 return "DateTimeFormat.prototype"; 150 case BuiltinObjectKind::NumberFormatPrototype: 151 return "NumberFormat.prototype"; 152 153 case BuiltinObjectKind::None: 154 break; 155 } 156 MOZ_CRASH("Unexpected builtin object kind"); 157 }