1/*
2 * Copyright (C) 2013, 2016 Apple, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WeakMapPrototype.h"
28
29#include "JSCInlines.h"
30#include "JSWeakMap.h"
31
32namespace JSC {
33
34const ClassInfo WeakMapPrototype::s_info = { "WeakMap", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(WeakMapPrototype) };
35
36static EncodedJSValue JSC_HOST_CALL protoFuncWeakMapDelete(ExecState*);
37static EncodedJSValue JSC_HOST_CALL protoFuncWeakMapGet(ExecState*);
38static EncodedJSValue JSC_HOST_CALL protoFuncWeakMapHas(ExecState*);
39static EncodedJSValue JSC_HOST_CALL protoFuncWeakMapSet(ExecState*);
40
41void WeakMapPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
42{
43 Base::finishCreation(vm);
44 ASSERT(inherits(vm, info()));
45 didBecomePrototype();
46
47 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->deleteKeyword, protoFuncWeakMapDelete, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
48 JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->get, protoFuncWeakMapGet, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, JSWeakMapGetIntrinsic);
49 JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->has, protoFuncWeakMapHas, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, JSWeakMapHasIntrinsic);
50 JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->set, protoFuncWeakMapSet, static_cast<unsigned>(PropertyAttribute::DontEnum), 2, JSWeakMapSetIntrinsic);
51
52 putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "WeakMap"), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
53}
54
55ALWAYS_INLINE static JSWeakMap* getWeakMap(CallFrame* callFrame, JSValue value)
56{
57 VM& vm = callFrame->vm();
58 auto scope = DECLARE_THROW_SCOPE(vm);
59
60 if (UNLIKELY(!value.isObject())) {
61 throwTypeError(callFrame, scope, "Called WeakMap function on non-object"_s);
62 return nullptr;
63 }
64
65 auto* map = jsDynamicCast<JSWeakMap*>(vm, asObject(value));
66 if (LIKELY(map))
67 return map;
68
69 throwTypeError(callFrame, scope, "Called WeakMap function on a non-WeakMap object"_s);
70 return nullptr;
71}
72
73EncodedJSValue JSC_HOST_CALL protoFuncWeakMapDelete(CallFrame* callFrame)
74{
75 auto* map = getWeakMap(callFrame, callFrame->thisValue());
76 if (!map)
77 return JSValue::encode(jsUndefined());
78 JSValue key = callFrame->argument(0);
79 return JSValue::encode(jsBoolean(key.isObject() && map->remove(asObject(key))));
80}
81
82EncodedJSValue JSC_HOST_CALL protoFuncWeakMapGet(CallFrame* callFrame)
83{
84 auto* map = getWeakMap(callFrame, callFrame->thisValue());
85 if (!map)
86 return JSValue::encode(jsUndefined());
87 JSValue key = callFrame->argument(0);
88 if (!key.isObject())
89 return JSValue::encode(jsUndefined());
90 return JSValue::encode(map->get(asObject(key)));
91}
92
93EncodedJSValue JSC_HOST_CALL protoFuncWeakMapHas(CallFrame* callFrame)
94{
95 auto* map = getWeakMap(callFrame, callFrame->thisValue());
96 if (!map)
97 return JSValue::encode(jsUndefined());
98 JSValue key = callFrame->argument(0);
99 return JSValue::encode(jsBoolean(key.isObject() && map->has(asObject(key))));
100}
101
102EncodedJSValue JSC_HOST_CALL protoFuncWeakMapSet(CallFrame* callFrame)
103{
104 VM& vm = callFrame->vm();
105 auto scope = DECLARE_THROW_SCOPE(vm);
106
107 auto* map = getWeakMap(callFrame, callFrame->thisValue());
108 EXCEPTION_ASSERT(!!scope.exception() == !map);
109 if (!map)
110 return JSValue::encode(jsUndefined());
111 JSValue key = callFrame->argument(0);
112 if (!key.isObject())
113 return JSValue::encode(throwTypeError(callFrame, scope, "Attempted to set a non-object key in a WeakMap"_s));
114 map->set(vm, asObject(key), callFrame->argument(1));
115 return JSValue::encode(callFrame->thisValue());
116}
117
118}
119