Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.polyglot;
import static com.oracle.truffle.api.interop.ForeignAccess.sendGetSize;
import static com.oracle.truffle.api.interop.ForeignAccess.sendHasKeys;
import static com.oracle.truffle.api.interop.ForeignAccess.sendHasSize;
import static com.oracle.truffle.api.interop.ForeignAccess.sendKeyInfo;
import static com.oracle.truffle.api.interop.ForeignAccess.sendKeys;
import static com.oracle.truffle.api.interop.ForeignAccess.sendRead;
import static com.oracle.truffle.api.interop.ForeignAccess.sendRemove;
import static com.oracle.truffle.api.interop.ForeignAccess.sendWrite;
import java.lang.reflect.Type;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.interop.KeyInfo;
import com.oracle.truffle.api.interop.Message;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.polyglot.PolyglotLanguageContext.ToGuestValueNode;
class PolyglotMap extends AbstractMap {
final PolyglotLanguageContext languageContext;
final TruffleObject guestObject;
final Cache cache;
PolyglotMap(PolyglotLanguageContext languageContext, TruffleObject obj, Class keyClass, Class valueClass, Type valueType) {
this.guestObject = obj;
this.languageContext = languageContext;
this.cache = Cache.lookup(languageContext, obj.getClass(), keyClass, valueClass, valueType);
}
static Map create(PolyglotLanguageContext languageContext, TruffleObject foreignObject, boolean implementsFunction, Class keyClass, Class valueClass, Type valueType) {
if (implementsFunction) {
return new PolyglotMapAndFunction<>(languageContext, foreignObject, keyClass, valueClass, valueType);
} else {
return new PolyglotMap<>(languageContext, foreignObject, keyClass, valueClass, valueType);
}
}
@Override
public boolean containsKey(Object key) {
return (boolean) cache.containsKey.call(languageContext, guestObject, key);
}
@SuppressWarnings("unchecked")
@Override
public Set> entrySet() {
return (Set>) cache.entrySet.call(languageContext, guestObject, this);
}
@SuppressWarnings("unchecked")
@Override
public V get(Object key) {
return (V) cache.get.call(languageContext, guestObject, key);
}
@SuppressWarnings("unchecked")
@Override
public V put(K key, V value) {
return (V) cache.put.call(languageContext, guestObject, key, value);
}
@SuppressWarnings("unchecked")
@Override
public V remove(Object key) {
return (V) cache.remove.call(languageContext, guestObject, key);
}
@Override
public String toString() {
try {
return languageContext.asValue(guestObject).toString();
} catch (UnsupportedOperationException e) {
return super.toString();
}
}
@Override
public int hashCode() {
return guestObject.hashCode();
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (o instanceof PolyglotMap) {
return languageContext.context == ((PolyglotMap, ?>) o).languageContext.context && guestObject.equals(((PolyglotMap, ?>) o).guestObject);
} else {
return false;
}
}
private final class LazyEntries extends AbstractSet> {
private final List> props;
private final int keysSize;
private final int elemSize;
LazyEntries(List> keys, int keysSize, int elemSize) {
assert keys != null || keysSize == 0;
this.props = keys;
this.keysSize = keysSize;
this.elemSize = elemSize;
}
@Override
public Iterator> iterator() {
if (keysSize > 0 && elemSize > 0) {
return new CombinedIterator();
} else if (keysSize > 0) {
return new LazyKeysIterator();
} else {
return new ElementsIterator();
}
}
@Override
public int size() {
return ((props != null) ? props.size() : keysSize) + elemSize;
}
@Override
public boolean contains(Object o) {
return containsKey(o);
}
@Override
@SuppressWarnings("unchecked")
public boolean remove(Object o) {
if (o instanceof Entry) {
Entry