The LanguageProvider provides factory methods for language data types, expressions,
statements and scripts used for testing language inter-operability. The LanguageProvider
implementations are loaded by the ServiceLoader and should be registered in the
'META-INF/services/org.graalvm.polyglot.tck.LanguageProvider'. See
Test
Compatibility Kit for details how to add a new language provider and execute tests.
Allows language providers to provide language options during the creation of the test
context. LanguageProviders are only allowed to set options of the
language they represent (Options starting with getId()). Attempts to set options
other than their own language will throw IllegalArgumentException on test context
creation.
Returns:
The (key, value) pairs of language option to add to the context.
Creates a Snippet for an identity function. The identity function just returns its
argument. This method allows an implementor to override the default identity function
verification. In most cases it's not needed to implement this method and it's enough to
implement createIdentityFunction(org.graalvm.polyglot.Context).
Creates a collection of functions creating language data types. For each language data type
create a function returning a value of given type and assign a correct TypeDescriptor
to it. The TypeDescriptor can be one of the predefined TypeDescriptors, an
array with component type, an executable with required parameter types or an intersection
type.
The JavaScript sample implementation creating a boolean type:
Parameters:
context - the context for a guest language code literal evaluation
Creates a collection of functions representing language expressions to test. For each
language operator create a function performing given operator and assign a correct
TypeDescriptors to its parameters and return type. The parameter types and return
type can be one of the predefined TypeDescriptors, an array with component type, an
executable with required parameter types or an union type.
The JavaScript sample implementation creating a plus operator:
@Override
public Collection<? extends Snippet> createExpressions(Context context) {
final Collection<Snippet> expressions = new ArrayList<>();
final TypeDescriptor numeric = TypeDescriptor.union(
TypeDescriptor.NUMBER,
TypeDescriptor.BOOLEAN);
final TypeDescriptor nonNumeric = TypeDescriptor.union(
TypeDescriptor.STRING,
TypeDescriptor.OBJECT,
TypeDescriptor.ARRAY,
TypeDescriptor.EXECUTABLE_ANY);
Snippet.Builder builder = Snippet.newBuilder(
"+",
context.eval("js",
"(function (a, b){ return a + b;})"),
TypeDescriptor.NUMBER).parameterTypes(numeric, numeric);
expressions.add(builder.build());
builder = Snippet.newBuilder(
"+",
context.eval("js",
"(function (a, b){ return a + b;})"),
TypeDescriptor.STRING).parameterTypes(nonNumeric, TypeDescriptor.ANY);
expressions.add(builder.build());
builder = Snippet.newBuilder(
"+",
context.eval("js",
"(function (a, b){ return a + b;})"),
TypeDescriptor.STRING).parameterTypes(TypeDescriptor.ANY, nonNumeric);
expressions.add(builder.build());
return expressions;
}
The R sample implementation creating a plus operator:
@Override
public Collection<? extends Snippet> createExpressions(Context context) {
final Collection<Snippet> expressions = new ArrayList<>();
final TypeDescriptor numOrBool = TypeDescriptor.union(
TypeDescriptor.NUMBER,
TypeDescriptor.BOOLEAN);
final TypeDescriptor arrNumOrBool = TypeDescriptor.array(
numOrBool);
final TypeDescriptor numeric = TypeDescriptor.union(
numOrBool,
arrNumOrBool);
Snippet.Builder builder = Snippet.newBuilder(
"+",
context.eval("R",
"function (a, b){ a + b}"),
numeric).parameterTypes(numeric, numeric);
expressions.add(builder.build());
return expressions;
}
Parameters:
context - the context for a guest language code literal evaluation
Creates a collection of functions representing language statements to test. For each control
flow statement create a function performing given statement and assign a correct
TypeDescriptors to its parameters and return type. The parameter types and return
type can be one of the predefined TypeDescriptors, an array with component type, an
executable with required parameter types or an union type.
The JavaScript sample implementation creating the if statement:
Creates a collection of simple scripts used for instrumentation testing. Each script is
represented as a function performing the script. The function must have no formal parameters
but may return a result which can be asserted by ResultVerifier.
The JavaScript sample implementation:
@Override
public Collection<? extends Snippet> createScripts(Context context) {
try {
final Collection<Snippet> scripts = new ArrayList<>();
Reader reader = new InputStreamReader(
getClass().getResourceAsStream("sample.js"),
"UTF-8");
Source source = Source.newBuilder(
"js",
reader,
"sample.js").build();
Snippet.Builder builder = Snippet.newBuilder(
source.getName(),
context.eval(source),
TypeDescriptor.NULL);
scripts.add(builder.build());
return scripts;
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
Parameters:
context - the context for a guest language code literal evaluation