Package com.oracle.truffle.api.source
Class Source.LiteralBuilder
java.lang.Object
com.oracle.truffle.api.source.Source.SourceBuilder
com.oracle.truffle.api.source.Source.LiteralBuilder
- Enclosing class:
Source
- Since:
- 19.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Uses configuration of this builder to create newSource
object.cached
(boolean cached) Enables or disables code caching for this source.canonicalizePath
(boolean canonicalize) Whether theSource.getPath()
(from theTruffleFile
) should be canonicalized.content
(CharSequence characters) Specifies character based content ofto-be-built
Source
.content
(ByteSequence bytes) Specifies byte based content ofto-be-built
Source
.Explicitly assigns an encoding used to read the file content.interactive
(boolean enabled) Marks the source as interactive.internal
(boolean enabled) Marks the source as internal.Specifies a name to theto-be-built
Source
.
-
Method Details
-
content
Specifies character based content ofto-be-built
Source
. Using this method one can ignore the real content of a file or URL and use already read one, or completely different one. UseSource.CONTENT_NONE
to set no content,Source.hasCharacters()
will befalse
then. The given characters must not mutate after they were accessed for the first time. Example:URL resource = relativeClass.getResource("sample.js"); Source source = Source.newBuilder("js", resource) .content("{}") .build(); assert resource.toExternalForm().equals(source.getPath()); assert "sample.js".equals(source.getName()); assert resource.toExternalForm().equals(source.getURI().toString()); assert "{}".equals(source.getCharacters());
- Overrides:
content
in classSource.SourceBuilder
- Parameters:
characters
- the code to be available viaSource.getCharacters()
, orSource.CONTENT_NONE
- Returns:
- instance of this builder - which's
Source.SourceBuilder.build()
method no longer throws anIOException
- Since:
- 19.3
-
content
Specifies byte based content ofto-be-built
Source
. Using this method one can ignore the real content of a file or URL and use already read one, or completely different one. The given bytes must not mutate after they were accessed for the first time. Example:URL resource = relativeClass.getResource("sample.js"); Source source = Source.newBuilder("js", resource) .content("{}") .build(); assert resource.toExternalForm().equals(source.getPath()); assert "sample.js".equals(source.getName()); assert resource.toExternalForm().equals(source.getURI().toString()); assert "{}".equals(source.getCharacters());
- Overrides:
content
in classSource.SourceBuilder
- Parameters:
bytes
- the code to be available viaSource.getBytes()
- Returns:
- instance of this builder - which's
Source.SourceBuilder.build()
method no longer throws anIOException
- Since:
- 19.3
-
name
Specifies a name to theto-be-built
Source
.- Overrides:
name
in classSource.SourceBuilder
- Parameters:
newName
- name that replaces the previously given one. If set tonull
then"Unnamed"
will be used.- Returns:
- instance of
this
builder - Since:
- 19.0
-
mimeType
Explicitly assigns aMIME type
to theto-be-built
Source
. If the MIME type isnull
then thedefault MIME type
of the language will be used to interpret the source. If set explicitly then the language needs tosupport
the MIME type in order toparse
a source. If notnull
then the MIME type will be verified containing no spaces and a '/' between group and sub-group. An example for a valid MIME type is:text/javascript
.The MIME type can be guessed by the system based on
files
orurls
.- Overrides:
mimeType
in classSource.SourceBuilder
- Parameters:
newMimeType
- the new mime type to be assigned, ornull
if default MIME type should be used.- Returns:
- instance of
this
builder ready tocreate new source
- Since:
- 19.0
- See Also:
-
cached
Enables or disables code caching for this source. By default code caching is enabled. Iftrue
then the source does not require parsing every time this source is evaluated. Iffalse
then the source requires parsing every time the source is evaluated but does not remember any code. Disabling caching may be useful if the source is known to only be evaluated once.If a source instance is no longer referenced by the client then all code caches will be freed automatically. Also, if the underlying context or engine is no longer referenced then cached code for evaluated sources will be freed automatically.
- Overrides:
cached
in classSource.SourceBuilder
- Returns:
- instance of
this
builder ready tocreate new source
- Since:
- 19.0
-
internal
Marks the source as internal. Internal sources are those that aren't created by user, but rather inherently present by the language system. Calling this method influences result of createSource.isInternal()
- Overrides:
internal
in classSource.SourceBuilder
- Returns:
- the instance of this builder
- Since:
- 19.0
-
interactive
Marks the source as interactive. Evaluation of interactive sources by aninteractive language
can use theenvironment
streams to print the result and read an input. However, non-interactive languages are expected to ignore the interactive property of sources and not use the environment streams. Any desired printing of the evaluated result provided by a non-interactive language needs to be handled by the caller. Calling of this method influences the result ofSource.isInteractive()
.- Overrides:
interactive
in classSource.SourceBuilder
- Returns:
- the instance of this builder
- Since:
- 19.0
-
uri
Assigns newURI
to theto-be-created
Source
. Each source providesSource.getURI()
as a persistent identification of its location. A default value for the method is deduced from the location or content, but one can change it by using this method- Overrides:
uri
in classSource.SourceBuilder
- Parameters:
ownUri
- the URL to use instead of default one, cannot benull
- Returns:
- the instance of this builder
- Since:
- 19.0
-
canonicalizePath
Whether theSource.getPath()
(from theTruffleFile
) should be canonicalized. By default the path is canonicalized to improve Source caching. If set tofalse
, thenSource.getPath()
will be the same as the passed TruffleFileTruffleFile.getPath()
.- Overrides:
canonicalizePath
in classSource.SourceBuilder
- Parameters:
canonicalize
- whether to canonicalize the path from the the TruffleFile- Returns:
- the instance of this builder
- Since:
- 20.2
-
encoding
Explicitly assigns an encoding used to read the file content. If the encoding isnull
then the file contained encoding information is used. If the file doesn't provide an encoding information the defaultUTF-8
encoding is used.- Overrides:
encoding
in classSource.SourceBuilder
- Parameters:
encoding
- the new file encoding to be used for reading the content- Returns:
- instance of
this
builder ready tocreate new source
- Since:
- 19.0
-
build
Uses configuration of this builder to create newSource
object.- Overrides:
build
in classSource.SourceBuilder
- Returns:
- the source object
- Throws:
SecurityException
- if the usedfilesystem
denied file reading- Since:
- 19.0
-