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, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.polyglot;
import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;
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.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropException;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
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.library.CachedLibrary;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.polyglot.PolyglotLanguageContext.ToGuestValueNode;
import com.oracle.truffle.polyglot.PolyglotMapFactory.CacheFactory.ContainsKeyNodeGen;
import com.oracle.truffle.polyglot.PolyglotMapFactory.CacheFactory.EntrySetNodeGen;
import com.oracle.truffle.polyglot.PolyglotMapFactory.CacheFactory.PutNodeGen;
import com.oracle.truffle.polyglot.PolyglotMapFactory.CacheFactory.RemoveBooleanNodeGen;
import com.oracle.truffle.polyglot.PolyglotMapFactory.CacheFactory.RemoveNodeGen;
class PolyglotMap extends AbstractMap implements HostWrapper {
final PolyglotLanguageContext languageContext;
final Object guestObject;
final Cache cache;
PolyglotMap(PolyglotLanguageContext languageContext, Object 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, Object 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 PolyglotLanguageContext getLanguageContext() {
return languageContext;
}
@Override
public Object getGuestObject() {
return guestObject;
}
@Override
public PolyglotContextImpl getContext() {
return languageContext.context;
}
@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) {
V prev = get(key);
cache.put.call(languageContext, guestObject, key, value);
return prev;
}
@SuppressWarnings("unchecked")
@Override
public V remove(Object key) {
V prev = get(key);
cache.remove.call(languageContext, guestObject, key);
return prev;
}
@Override
public String toString() {
return HostWrapper.toString(this);
}
@Override
public int hashCode() {
return HostWrapper.hashCode(languageContext, guestObject);
}
@Override
public boolean equals(Object o) {
if (o instanceof PolyglotMap) {
return HostWrapper.equals(languageContext, guestObject, ((PolyglotMap, ?>) o).guestObject);
} else {
return false;
}
}
@TruffleBoundary
private static int intValue(Object key) {
return ((Number) key).intValue();
}
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