1/*
2 * Copyright (C) 2012, 2016 Apple Inc. All rights reserved.
3 * Copyright (C) 2015 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 "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
37namespace JSC {
38
39static EncodedJSValue JSC_HOST_CALL symbolConstructorFor(ExecState*);
40static EncodedJSValue JSC_HOST_CALL symbolConstructorKeyFor(ExecState*);
41
42}
43
44#include "SymbolConstructor.lut.h"
45
46namespace JSC {
47
48STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(SymbolConstructor);
49
50const 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
59static EncodedJSValue JSC_HOST_CALL callSymbol(ExecState*);
60
61SymbolConstructor::SymbolConstructor(VM& vm, Structure* structure)
62 : InternalFunction(vm, structure, callSymbol, nullptr)
63{
64}
65
66#define INITIALIZE_WELL_KNOWN_SYMBOLS(name) \
67putDirectWithoutTransition(vm, Identifier::fromString(&vm, #name), Symbol::create(vm, static_cast<SymbolImpl&>(*vm.propertyNames->name##Symbol.impl())), PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
68
69void SymbolConstructor::finishCreation(VM& vm, SymbolPrototype* prototype)
70{
71 Base::finishCreation(vm, vm.propertyNames->Symbol.string(), NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition);
72 putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
73 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
74
75 JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(INITIALIZE_WELL_KNOWN_SYMBOLS)
76}
77
78// ------------------------------ Functions ---------------------------
79
80static EncodedJSValue JSC_HOST_CALL callSymbol(ExecState* exec)
81{
82 VM& vm = exec->vm();
83 auto scope = DECLARE_THROW_SCOPE(vm);
84
85 JSValue description = exec->argument(0);
86 if (description.isUndefined())
87 return JSValue::encode(Symbol::create(vm));
88
89 String string = description.toWTFString(exec);
90 RETURN_IF_EXCEPTION(scope, { });
91 return JSValue::encode(Symbol::createWithDescription(vm, string));
92}
93
94EncodedJSValue JSC_HOST_CALL symbolConstructorFor(ExecState* exec)
95{
96 VM& vm = exec->vm();
97 auto scope = DECLARE_THROW_SCOPE(vm);
98
99 JSString* stringKey = exec->argument(0).toString(exec);
100 RETURN_IF_EXCEPTION(scope, encodedJSValue());
101 String string = stringKey->value(exec);
102 RETURN_IF_EXCEPTION(scope, encodedJSValue());
103
104 return JSValue::encode(Symbol::create(exec->vm(), exec->vm().symbolRegistry().symbolForKey(string)));
105}
106
107const ASCIILiteral SymbolKeyForTypeError { "Symbol.keyFor requires that the first argument be a symbol"_s };
108
109EncodedJSValue JSC_HOST_CALL symbolConstructorKeyFor(ExecState* exec)
110{
111 VM& vm = exec->vm();
112 auto scope = DECLARE_THROW_SCOPE(vm);
113
114 JSValue symbolValue = exec->argument(0);
115 if (!symbolValue.isSymbol())
116 return JSValue::encode(throwTypeError(exec, scope, SymbolKeyForTypeError));
117
118 PrivateName privateName = asSymbol(symbolValue)->privateName();
119 SymbolImpl& uid = privateName.uid();
120 if (!uid.symbolRegistry())
121 return JSValue::encode(jsUndefined());
122
123 ASSERT(uid.symbolRegistry() == &vm.symbolRegistry());
124 return JSValue::encode(jsString(exec, &uid));
125}
126
127} // namespace JSC
128