@Override public boolean hasBytes(Object impl) { com.oracle.truffle.api.source.Source source = (com.oracle.truffle.api.source.Source) impl; return source.hasBytes(); }
@Override public boolean hasBytes() { return key.base.hasBytes(); }
final TextMap getTextMap() { if (hasBytes()) { throw new UnsupportedOperationException("Operation is only enabled for character based sources."); } TextMap res = textMap; if (res == null) { res = textMap = createTextMap(); } assert res != null; return res; }
Source resolve(Source source) { if (source.hasCharacters() || source.hasBytes()) { return source; } Source resolved; synchronized (resolvedMap) { resolved = resolvedMap.getOrDefault(source, source); if (resolved == source) { // not resolved resolved = doResolve(source); resolvedMap.put(source, resolved); } } return resolved; }
/** * Creates a {@linkplain Source Source instance} that represents the contents of a sub-range of * an <code>this</code> {@link Source}. * * @param baseCharIndex 0-based index of the first character of the sub-range * @param length the number of characters in the sub-range * @return a new instance representing a sub-range of another Source * @throws IllegalArgumentException if the specified sub-range is not contained in the base * @throws UnsupportedOperationException if this source cannot contain {@link #hasCharacters() * characters}. * @since 0.15 */ public Source subSource(int baseCharIndex, int length) { if (hasBytes()) { throw new UnsupportedOperationException("Operation is only enabled for character based sources."); } CompilerAsserts.neverPartOfCompilation(NO_FASTPATH_SUBSOURCE_CREATION_MESSAGE); return SubSourceImpl.create(this, baseCharIndex, length); }
if (hasBytes()) { throw new UnsupportedOperationException("Operation is only enabled for character based sources.");
/** * Gets the number of characters or bytes of the source. * * @throws UnsupportedOperationException if this source does not contain {@link #hasCharacters() * characters}, nor {@link #hasBytes() bytes}. * @since 0.8 */ public final int getLength() { if (hasCharacters()) { return getTextMap().length(); } else if (hasBytes()) { return getBytes().length(); } else { throw new UnsupportedOperationException("Operation is only enabled for sources with character or byte content."); } }
/** * Get URI of the source. Every source has an associated {@link URI}, which can be used as a * persistent identification of the source. For example one can * {@link com.oracle.truffle.api.debug.DebuggerSession#install(com.oracle.truffle.api.debug.Breakpoint) * register a breakpoint using a URI} to a source that isn't loaded yet and it will be activated * when the source is evaluated. The {@link URI} returned by this method should be as unique as * possible, yet it can happen that different {@link Source sources} return the same * {@link #getURI} - for example when content of a {@link Source#newBuilder(String, TruffleFile) * file on a disk} changes and is re-loaded. * * @return a URI, it's never <code>null</code> * @since 0.14 */ public final URI getURI() { URI uri = getOriginalURI(); if (uri == null) { uri = computedURI; if (uri == null) { byte[] bytes = hasBytes() ? getBytes().toByteArray() : getCharacters().toString().getBytes(); uri = computedURI = getNamedURI(getName(), bytes); } } return uri; }
if (hasBytes() || !hasCharacters()) { throw new UnsupportedOperationException("Operation is only enabled for character based sources.");
if (hasBytes()) { throw new UnsupportedOperationException("Operation is only enabled for character based sources.");
private static void validateSource(PolyglotLanguageContext context, Source source) { if (!source.hasBytes() && !source.hasCharacters()) { throw new PolyglotIllegalArgumentException(String.format("Error evaluating the source. The source does not specify characters nor bytes.")); if (source.hasBytes()) { throw new PolyglotIllegalArgumentException( String.format("Error evaluating the source. MIME type '%s' is character based for language '%s' but the source contents are byte based.", mimeType,
/** * Uses configuration of this builder to create new {@link Source} object. The method throws * an {@link IOException} if an error loading the source occured. * * @return the source object * @throws IOException if an error reading the content occurred * @throws SecurityException if this {@link SourceBuilder} was created for a * {@link TruffleFile} and the used {@link org.graalvm.polyglot.io.FileSystem * filesystem} denied its reading * @since 1.0 */ public Source build() throws IOException { assert this.language != null; Source source = buildSource(this.language, this.origin, this.name, this.mimeType, this.content, this.uri, this.internal, this.interactive, this.cached, false); // make sure origin is not consumed again if builder is used twice if (source.hasBytes()) { this.content = source.getBytes(); } else if (source.hasCharacters()) { this.content = source.getCharacters(); } assert source.getName() != null; assert !source.hasCharacters() || source.getCharacters() != null; assert !source.hasBytes() || source.getBytes() != null; assert source.getLanguage() != null; return source; }
if (hasBytes()) { throw new UnsupportedOperationException("Operation is only enabled for character based sources.");
/** * {@inheritDoc} * * @since 1.0 */ @Override public String toString() { return "Source [language=" + getLanguage() + ", name=" + getName() + ", path=" + getPath() + ", internal=" + isInternal() + ", cached=" + isCached() + ", interactive=" + isInteractive() + ", hasBytes=" + hasBytes() + ", hasCharacters=" + hasCharacters() + ", URL=" + getURL() + ", URI=" + getURI() + ", mimeType=" + getMimeType() + "]"; }