#include "config.h"
#include "Symbol.h"
#include "JSCJSValueInlines.h"
#include "SymbolObject.h"
namespace JSC {
const ClassInfo Symbol::s_info = { "symbol", nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(Symbol) };
Symbol::Symbol(VM& vm)
: Base(vm, vm.symbolStructure.get())
, m_privateName()
{
}
Symbol::Symbol(VM& vm, const String& string)
: Base(vm, vm.symbolStructure.get())
, m_privateName(PrivateName::Description, string)
{
}
Symbol::Symbol(VM& vm, SymbolImpl& uid)
: Base(vm, vm.symbolStructure.get())
, m_privateName(uid)
{
}
void Symbol::finishCreation(VM& vm)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
vm.symbolImplToSymbolMap.set(&m_privateName.uid(), this);
}
inline SymbolObject* SymbolObject::create(VM& vm, JSGlobalObject* globalObject, Symbol* symbol)
{
SymbolObject* object = new (NotNull, allocateCell<SymbolObject>(vm.heap)) SymbolObject(vm, globalObject->symbolObjectStructure());
object->finishCreation(vm, symbol);
return object;
}
JSValue Symbol::toPrimitive(JSGlobalObject*, PreferredPrimitiveType) const
{
return const_cast<Symbol*>(this);
}
bool Symbol::getPrimitiveNumber(JSGlobalObject* globalObject, double& number, JSValue& result) const
{
result = this;
number = toNumber(globalObject);
return true;
}
JSObject* Symbol::toObject(JSGlobalObject* globalObject) const
{
return SymbolObject::create(globalObject->vm(), globalObject, const_cast<Symbol*>(this));
}
double Symbol::toNumber(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
throwTypeError(globalObject, scope, "Cannot convert a symbol to a number"_s);
return 0.0;
}
void Symbol::destroy(JSCell* cell)
{
static_cast<Symbol*>(cell)->Symbol::~Symbol();
}
String Symbol::descriptiveString() const
{
return makeString("Symbol(", String(m_privateName.uid()), ')');
}
String Symbol::description() const
{
auto& uid = m_privateName.uid();
return uid.isNullSymbol() ? String() : uid;
}
Symbol* Symbol::create(VM& vm)
{
Symbol* symbol = new (NotNull, allocateCell<Symbol>(vm.heap)) Symbol(vm);
symbol->finishCreation(vm);
return symbol;
}
Symbol* Symbol::createWithDescription(VM& vm, const String& description)
{
Symbol* symbol = new (NotNull, allocateCell<Symbol>(vm.heap)) Symbol(vm, description);
symbol->finishCreation(vm);
return symbol;
}
Symbol* Symbol::create(VM& vm, SymbolImpl& uid)
{
if (Symbol* symbol = vm.symbolImplToSymbolMap.get(&uid))
return symbol;
Symbol* symbol = new (NotNull, allocateCell<Symbol>(vm.heap)) Symbol(vm, uid);
symbol->finishCreation(vm);
return symbol;
}
}