1/* 2 * Copyright (C) 2013 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 "MapPrototype.h" 28 29#include "CachedCall.h" 30#include "Error.h" 31#include "ExceptionHelpers.h" 32#include "GetterSetter.h" 33#include "JSCJSValueInlines.h" 34#include "JSFunctionInlines.h" 35#include "JSMap.h" 36#include "JSMapIterator.h" 37#include "MapData.h" 38#include "StructureInlines.h" 39 40namespace JSC { 41 42const ClassInfo MapPrototype::s_info = { "Map", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(MapPrototype) }; 43 44static EncodedJSValue JSC_HOST_CALL mapProtoFuncClear(ExecState*); 45static EncodedJSValue JSC_HOST_CALL mapProtoFuncDelete(ExecState*); 46static EncodedJSValue JSC_HOST_CALL mapProtoFuncForEach(ExecState*); 47static EncodedJSValue JSC_HOST_CALL mapProtoFuncGet(ExecState*); 48static EncodedJSValue JSC_HOST_CALL mapProtoFuncHas(ExecState*); 49static EncodedJSValue JSC_HOST_CALL mapProtoFuncSet(ExecState*); 50static EncodedJSValue JSC_HOST_CALL mapProtoFuncKeys(ExecState*); 51static EncodedJSValue JSC_HOST_CALL mapProtoFuncValues(ExecState*); 52static EncodedJSValue JSC_HOST_CALL mapProtoFuncEntries(ExecState*); 53 54static EncodedJSValue JSC_HOST_CALL mapProtoFuncSize(ExecState*); 55 56void MapPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject) 57{ 58 Base::finishCreation(vm); 59 ASSERT(inherits(info())); 60 vm.prototypeMap.addPrototype(this); 61 62 JSC_NATIVE_FUNCTION(vm.propertyNames->clear, mapProtoFuncClear, DontEnum, 0); 63 JSC_NATIVE_FUNCTION(vm.propertyNames->deleteKeyword, mapProtoFuncDelete, DontEnum, 1); 64 JSC_NATIVE_FUNCTION(vm.propertyNames->forEach, mapProtoFuncForEach, DontEnum, 1); 65 JSC_NATIVE_FUNCTION(vm.propertyNames->get, mapProtoFuncGet, DontEnum, 1); 66 JSC_NATIVE_FUNCTION(vm.propertyNames->has, mapProtoFuncHas, DontEnum, 1); 67 JSC_NATIVE_FUNCTION(vm.propertyNames->set, mapProtoFuncSet, DontEnum, 2); 68 JSC_NATIVE_FUNCTION(vm.propertyNames->keys, mapProtoFuncKeys, DontEnum, 0); 69 JSC_NATIVE_FUNCTION(vm.propertyNames->values, mapProtoFuncValues, DontEnum, 0); 70 JSC_NATIVE_FUNCTION(vm.propertyNames->entries, mapProtoFuncEntries, DontEnum, 0); 71 JSC_NATIVE_FUNCTION(vm.propertyNames->iteratorPrivateName, mapProtoFuncEntries, DontEnum, 0); 72 73 GetterSetter* accessor = GetterSetter::create(vm); 74 JSFunction* function = JSFunction::create(vm, globalObject, 0, vm.propertyNames->size.string(), mapProtoFuncSize); 75 accessor->setGetter(vm, function); 76 putDirectNonIndexAccessor(vm, vm.propertyNames->size, accessor, DontEnum | Accessor); 77} 78 79ALWAYS_INLINE static MapData* getMapData(CallFrame* callFrame, JSValue thisValue) 80{ 81 if (!thisValue.isObject()) { 82 throwVMError(callFrame, createNotAnObjectError(callFrame, thisValue)); 83 return 0; 84 } 85 JSMap* map = jsDynamicCast<JSMap*>(thisValue); 86 if (!map) { 87 throwTypeError(callFrame, ASCIILiteral("Map operation called on non-Map object")); 88 return 0; 89 } 90 return map->mapData(); 91} 92 93EncodedJSValue JSC_HOST_CALL mapProtoFuncClear(CallFrame* callFrame) 94{ 95 MapData* data = getMapData(callFrame, callFrame->thisValue()); 96 if (!data) 97 return JSValue::encode(jsUndefined()); 98 data->clear(); 99 return JSValue::encode(jsUndefined()); 100} 101 102EncodedJSValue JSC_HOST_CALL mapProtoFuncDelete(CallFrame* callFrame) 103{ 104 MapData* data = getMapData(callFrame, callFrame->thisValue()); 105 if (!data) 106 return JSValue::encode(jsUndefined()); 107 return JSValue::encode(jsBoolean(data->remove(callFrame, callFrame->argument(0)))); 108} 109 110EncodedJSValue JSC_HOST_CALL mapProtoFuncForEach(CallFrame* callFrame) 111{ 112 MapData* data = getMapData(callFrame, callFrame->thisValue()); 113 if (!data) 114 return JSValue::encode(jsUndefined()); 115 JSValue callBack = callFrame->argument(0); 116 CallData callData; 117 CallType callType = getCallData(callBack, callData); 118 if (callType == CallTypeNone) 119 return JSValue::encode(throwTypeError(callFrame, WTF::ASCIILiteral("Map.prototype.forEach called without callback"))); 120 JSValue thisValue = callFrame->argument(1); 121 VM* vm = &callFrame->vm(); 122 if (callType == CallTypeJS) { 123 JSFunction* function = jsCast<JSFunction*>(callBack); 124 CachedCall cachedCall(callFrame, function, 2); 125 for (auto ptr = data->begin(), end = data->end(); ptr != end && !vm->exception(); ++ptr) { 126 cachedCall.setThis(thisValue); 127 cachedCall.setArgument(0, ptr.value()); 128 cachedCall.setArgument(1, ptr.key()); 129 cachedCall.call(); 130 } 131 } else { 132 for (auto ptr = data->begin(), end = data->end(); ptr != end && !vm->exception(); ++ptr) { 133 MarkedArgumentBuffer args; 134 args.append(ptr.value()); 135 args.append(ptr.key()); 136 JSC::call(callFrame, callBack, callType, callData, thisValue, args); 137 } 138 } 139 return JSValue::encode(jsUndefined()); 140} 141 142EncodedJSValue JSC_HOST_CALL mapProtoFuncGet(CallFrame* callFrame) 143{ 144 MapData* data = getMapData(callFrame, callFrame->thisValue()); 145 if (!data) 146 return JSValue::encode(jsUndefined()); 147 JSValue result = data->get(callFrame, callFrame->argument(0)); 148 if (!result) 149 result = jsUndefined(); 150 return JSValue::encode(result); 151} 152 153EncodedJSValue JSC_HOST_CALL mapProtoFuncHas(CallFrame* callFrame) 154{ 155 MapData* data = getMapData(callFrame, callFrame->thisValue()); 156 if (!data) 157 return JSValue::encode(jsUndefined()); 158 return JSValue::encode(jsBoolean(data->contains(callFrame, callFrame->argument(0)))); 159} 160 161EncodedJSValue JSC_HOST_CALL mapProtoFuncSet(CallFrame* callFrame) 162{ 163 MapData* data = getMapData(callFrame, callFrame->thisValue()); 164 if (!data) 165 return JSValue::encode(jsUndefined()); 166 data->set(callFrame, callFrame->argument(0), callFrame->argument(1)); 167 return JSValue::encode(callFrame->thisValue()); 168} 169 170EncodedJSValue JSC_HOST_CALL mapProtoFuncSize(CallFrame* callFrame) 171{ 172 MapData* data = getMapData(callFrame, callFrame->thisValue()); 173 if (!data) 174 return JSValue::encode(jsUndefined()); 175 return JSValue::encode(jsNumber(data->size(callFrame))); 176} 177 178EncodedJSValue JSC_HOST_CALL mapProtoFuncValues(CallFrame* callFrame) 179{ 180 JSMap* thisObj = jsDynamicCast<JSMap*>(callFrame->thisValue()); 181 if (!thisObj) 182 return JSValue::encode(throwTypeError(callFrame, ASCIILiteral("Cannot create a Map value iterator for a non-Map object."))); 183 return JSValue::encode(JSMapIterator::create(callFrame->vm(), callFrame->callee()->globalObject()->mapIteratorStructure(), thisObj, MapIterateValue)); 184} 185 186EncodedJSValue JSC_HOST_CALL mapProtoFuncEntries(CallFrame* callFrame) 187{ 188 JSMap* thisObj = jsDynamicCast<JSMap*>(callFrame->thisValue()); 189 if (!thisObj) 190 return JSValue::encode(throwTypeError(callFrame, ASCIILiteral("Cannot create a Map key iterator for a non-Map object."))); 191 return JSValue::encode(JSMapIterator::create(callFrame->vm(), callFrame->callee()->globalObject()->mapIteratorStructure(), thisObj, MapIterateKeyValue)); 192} 193 194EncodedJSValue JSC_HOST_CALL mapProtoFuncKeys(CallFrame* callFrame) 195{ 196 JSMap* thisObj = jsDynamicCast<JSMap*>(callFrame->thisValue()); 197 if (!thisObj) 198 return JSValue::encode(throwTypeError(callFrame, ASCIILiteral("Cannot create a Map entry iterator for a non-Map object."))); 199 return JSValue::encode(JSMapIterator::create(callFrame->vm(), callFrame->callee()->globalObject()->mapIteratorStructure(), thisObj, MapIterateKey)); 200} 201 202} 203