2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "SymbolConstructor.h"
31 #include "JSCInlines.h"
32 #include "JSGlobalObject.h"
34 #include "SymbolPrototype.h"
35 #include <wtf/text/SymbolRegistry.h>
39 static EncodedJSValue JSC_HOST_CALL
symbolConstructorFor(ExecState
*);
40 static EncodedJSValue JSC_HOST_CALL
symbolConstructorKeyFor(ExecState
*);
44 #include "SymbolConstructor.lut.h"
48 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(SymbolConstructor
);
50 const ClassInfo
SymbolConstructor::s_info
= { "Function", &Base::s_info
, &symbolConstructorTable
, CREATE_METHOD_TABLE(SymbolConstructor
) };
52 /* Source for SymbolConstructor.lut.h
53 @begin symbolConstructorTable
54 for symbolConstructorFor DontEnum|Function 1
55 keyFor symbolConstructorKeyFor DontEnum|Function 1
59 SymbolConstructor::SymbolConstructor(VM
& vm
, Structure
* structure
)
60 : InternalFunction(vm
, structure
)
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);
67 void SymbolConstructor::finishCreation(VM
& vm
, SymbolPrototype
* prototype
)
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
);
73 JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(INITIALIZE_WELL_KNOWN_SYMBOLS
)
76 bool SymbolConstructor::getOwnPropertySlot(JSObject
* object
, ExecState
* exec
, PropertyName propertyName
, PropertySlot
&slot
)
78 return getStaticFunctionSlot
<Base
>(exec
, symbolConstructorTable
, jsCast
<SymbolConstructor
*>(object
), propertyName
, slot
);
81 // ------------------------------ Functions ---------------------------
83 static EncodedJSValue JSC_HOST_CALL
callSymbol(ExecState
* exec
)
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
)));
91 ConstructType
SymbolConstructor::getConstructData(JSCell
*, ConstructData
&)
93 return ConstructTypeNone
;
96 CallType
SymbolConstructor::getCallData(JSCell
*, CallData
& callData
)
98 callData
.native
.function
= callSymbol
;
102 EncodedJSValue JSC_HOST_CALL
symbolConstructorFor(ExecState
* exec
)
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());
111 return JSValue::encode(Symbol::create(exec
->vm(), exec
->vm().symbolRegistry().symbolForKey(string
)));
114 EncodedJSValue JSC_HOST_CALL
symbolConstructorKeyFor(ExecState
* exec
)
116 JSValue symbolValue
= exec
->argument(0);
117 if (!symbolValue
.isSymbol())
118 return JSValue::encode(throwTypeError(exec
));
120 SymbolImpl
* uid
= asSymbol(symbolValue
)->privateName().uid();
121 if (!uid
->symbolRegistry())
122 return JSValue::encode(jsUndefined());
124 ASSERT(uid
->symbolRegistry() == &exec
->vm().symbolRegistry());
125 return JSValue::encode(jsString(exec
, exec
->vm().symbolRegistry().keyForSymbol(*uid
)));