]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/SymbolConstructor.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / SymbolConstructor.cpp
1 /*
2 * Copyright (C) 2012 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, 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()->className);
70 putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, DontEnum | DontDelete | ReadOnly);
71 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
72
73 JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(INITIALIZE_WELL_KNOWN_SYMBOLS)
74 }
75
76 bool SymbolConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
77 {
78 return getStaticFunctionSlot<Base>(exec, symbolConstructorTable, jsCast<SymbolConstructor*>(object), propertyName, slot);
79 }
80
81 // ------------------------------ Functions ---------------------------
82
83 static EncodedJSValue JSC_HOST_CALL callSymbol(ExecState* exec)
84 {
85 JSValue description = exec->argument(0);
86 if (description.isUndefined())
87 return JSValue::encode(Symbol::create(exec->vm()));
88 return JSValue::encode(Symbol::create(exec, description.toString(exec)));
89 }
90
91 ConstructType SymbolConstructor::getConstructData(JSCell*, ConstructData&)
92 {
93 return ConstructTypeNone;
94 }
95
96 CallType SymbolConstructor::getCallData(JSCell*, CallData& callData)
97 {
98 callData.native.function = callSymbol;
99 return CallTypeHost;
100 }
101
102 EncodedJSValue JSC_HOST_CALL symbolConstructorFor(ExecState* exec)
103 {
104 JSString* stringKey = exec->argument(0).toString(exec);
105 if (exec->hadException())
106 return JSValue::encode(jsUndefined());
107 String string = stringKey->value(exec);
108 if (exec->hadException())
109 return JSValue::encode(jsUndefined());
110
111 return JSValue::encode(Symbol::create(exec->vm(), exec->vm().symbolRegistry().symbolForKey(string)));
112 }
113
114 EncodedJSValue JSC_HOST_CALL symbolConstructorKeyFor(ExecState* exec)
115 {
116 JSValue symbolValue = exec->argument(0);
117 if (!symbolValue.isSymbol())
118 return JSValue::encode(throwTypeError(exec));
119
120 SymbolImpl* uid = asSymbol(symbolValue)->privateName().uid();
121 if (!uid->symbolRegistry())
122 return JSValue::encode(jsUndefined());
123
124 ASSERT(uid->symbolRegistry() == &exec->vm().symbolRegistry());
125 return JSValue::encode(jsString(exec, exec->vm().symbolRegistry().keyForSymbol(*uid)));
126 }
127
128 } // namespace JSC