1/*
2 * Copyright (C) 2012, 2016 Apple Inc. All rights reserved.
3 * Copyright (C) 2015-2016 Yusuke Suzuki <[email protected]>.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "Symbol.h"
29
30#include "Error.h"
31#include "JSCInlines.h"
32#include "SymbolObject.h"
33
34namespace JSC {
35
36const ClassInfo Symbol::s_info = { "symbol", nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(Symbol) };
37
38Symbol::Symbol(VM& vm)
39 : Base(vm, vm.symbolStructure.get())
40 , m_privateName()
41{
42}
43
44Symbol::Symbol(VM& vm, const String& string)
45 : Base(vm, vm.symbolStructure.get())
46 , m_privateName(PrivateName::Description, string)
47{
48}
49
50Symbol::Symbol(VM& vm, SymbolImpl& uid)
51 : Base(vm, vm.symbolStructure.get())
52 , m_privateName(uid)
53{
54}
55
56void Symbol::finishCreation(VM& vm)
57{
58 Base::finishCreation(vm);
59 ASSERT(inherits(vm, info()));
60
61 vm.symbolImplToSymbolMap.set(&m_privateName.uid(), this);
62}
63
64inline SymbolObject* SymbolObject::create(VM& vm, JSGlobalObject* globalObject, Symbol* symbol)
65{
66 SymbolObject* object = new (NotNull, allocateCell<SymbolObject>(vm.heap)) SymbolObject(vm, globalObject->symbolObjectStructure());
67 object->finishCreation(vm, symbol);
68 return object;
69}
70
71JSValue Symbol::toPrimitive(ExecState*, PreferredPrimitiveType) const
72{
73 return const_cast<Symbol*>(this);
74}
75
76bool Symbol::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
77{
78 result = this;
79 number = toNumber(exec);
80 return true;
81}
82
83JSObject* Symbol::toObject(ExecState* exec, JSGlobalObject* globalObject) const
84{
85 return SymbolObject::create(exec->vm(), globalObject, const_cast<Symbol*>(this));
86}
87
88double Symbol::toNumber(ExecState* exec) const
89{
90 VM& vm = exec->vm();
91 auto scope = DECLARE_THROW_SCOPE(vm);
92 throwTypeError(exec, scope, "Cannot convert a symbol to a number"_s);
93 return 0.0;
94}
95
96void Symbol::destroy(JSCell* cell)
97{
98 static_cast<Symbol*>(cell)->Symbol::~Symbol();
99}
100
101String Symbol::descriptiveString() const
102{
103 return makeString("Symbol(", String(m_privateName.uid()), ')');
104}
105
106String Symbol::description() const
107{
108 auto& uid = m_privateName.uid();
109 return uid.isNullSymbol() ? String() : uid;
110}
111
112Symbol* Symbol::create(VM& vm)
113{
114 Symbol* symbol = new (NotNull, allocateCell<Symbol>(vm.heap)) Symbol(vm);
115 symbol->finishCreation(vm);
116 return symbol;
117}
118
119Symbol* Symbol::createWithDescription(VM& vm, const String& description)
120{
121 Symbol* symbol = new (NotNull, allocateCell<Symbol>(vm.heap)) Symbol(vm, description);
122 symbol->finishCreation(vm);
123 return symbol;
124}
125
126Symbol* Symbol::create(VM& vm, SymbolImpl& uid)
127{
128 if (Symbol* symbol = vm.symbolImplToSymbolMap.get(&uid))
129 return symbol;
130
131 Symbol* symbol = new (NotNull, allocateCell<Symbol>(vm.heap)) Symbol(vm, uid);
132 symbol->finishCreation(vm);
133 return symbol;
134}
135
136} // namespace JSC
137