Remove excessive headers from JavaScriptCore
[WebKit-https.git] / Source / JavaScriptCore / runtime / SymbolConstructor.cpp
1 /*
2  * Copyright (C) 2012, 2016 Apple Inc. All rights reserved.
3  * Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>.
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 "SymbolConstructor.h"
29
30 #include "Error.h"
31 #include "JSCInlines.h"
32 #include "JSGlobalObject.h"
33 #include "Symbol.h"
34 #include "SymbolPrototype.h"
35 #include <wtf/text/SymbolRegistry.h>
36
37 namespace JSC {
38
39 static EncodedJSValue JSC_HOST_CALL symbolConstructorFor(ExecState*);
40 static EncodedJSValue JSC_HOST_CALL symbolConstructorKeyFor(ExecState*);
41
42 }
43
44 #include "SymbolConstructor.lut.h"
45
46 namespace JSC {
47
48 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(SymbolConstructor);
49
50 const ClassInfo SymbolConstructor::s_info = { "Function", &Base::s_info, &symbolConstructorTable, nullptr, CREATE_METHOD_TABLE(SymbolConstructor) };
51
52 /* Source for SymbolConstructor.lut.h
53 @begin symbolConstructorTable
54   for       symbolConstructorFor       DontEnum|Function 1
55   keyFor    symbolConstructorKeyFor    DontEnum|Function 1
56 @end
57 */
58
59 SymbolConstructor::SymbolConstructor(VM& vm, Structure* structure)
60     : InternalFunction(vm, structure)
61 {
62 }
63
64 #define INITIALIZE_WELL_KNOWN_SYMBOLS(name) \
65     putDirectWithoutTransition(vm, Identifier::fromString(&vm, #name), Symbol::create(vm, static_cast<SymbolImpl&>(*vm.propertyNames->name##Symbol.impl())), DontEnum | DontDelete | ReadOnly);
66
67 void SymbolConstructor::finishCreation(VM& vm, SymbolPrototype* prototype)
68 {
69     Base::finishCreation(vm, prototype->classInfo(vm)->className);
70     putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, DontEnum | DontDelete | ReadOnly);
71     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), ReadOnly | DontEnum);
72
73     JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(INITIALIZE_WELL_KNOWN_SYMBOLS)
74 }
75
76 // ------------------------------ Functions ---------------------------
77
78 static EncodedJSValue JSC_HOST_CALL callSymbol(ExecState* exec)
79 {
80     JSValue description = exec->argument(0);
81     if (description.isUndefined())
82         return JSValue::encode(Symbol::create(exec->vm()));
83     return JSValue::encode(Symbol::create(exec, description.toString(exec)));
84 }
85
86 ConstructType SymbolConstructor::getConstructData(JSCell*, ConstructData&)
87 {
88     return ConstructType::None;
89 }
90
91 CallType SymbolConstructor::getCallData(JSCell*, CallData& callData)
92 {
93     callData.native.function = callSymbol;
94     return CallType::Host;
95 }
96
97 EncodedJSValue JSC_HOST_CALL symbolConstructorFor(ExecState* exec)
98 {
99     VM& vm = exec->vm();
100     auto scope = DECLARE_THROW_SCOPE(vm);
101
102     JSString* stringKey = exec->argument(0).toString(exec);
103     RETURN_IF_EXCEPTION(scope, encodedJSValue());
104     String string = stringKey->value(exec);
105     RETURN_IF_EXCEPTION(scope, encodedJSValue());
106
107     return JSValue::encode(Symbol::create(exec->vm(), exec->vm().symbolRegistry().symbolForKey(string)));
108 }
109
110 const char* const SymbolKeyForTypeError = "Symbol.keyFor requires that the first argument be a symbol";
111
112 EncodedJSValue JSC_HOST_CALL symbolConstructorKeyFor(ExecState* exec)
113 {
114     VM& vm = exec->vm();
115     auto scope = DECLARE_THROW_SCOPE(vm);
116
117     JSValue symbolValue = exec->argument(0);
118     if (!symbolValue.isSymbol())
119         return JSValue::encode(throwTypeError(exec, scope, ASCIILiteral(SymbolKeyForTypeError)));
120
121     SymbolImpl& uid = asSymbol(symbolValue)->privateName().uid();
122     if (!uid.symbolRegistry())
123         return JSValue::encode(jsUndefined());
124
125     ASSERT(uid.symbolRegistry() == &vm.symbolRegistry());
126     return JSValue::encode(jsString(exec, vm.symbolRegistry().keyForSymbol(*uid.asRegisteredSymbolImpl())));
127 }
128
129 } // namespace JSC