GraalJS is a high-performance JavaScript engine that supports running JavaScript code in a Java environment. When working with GraalJS, you can call JavaScript functions from Java and pass parameters, including float values. Below is a step-by-step guide to calling a JavaScript function with float parameters in GraalJS.
Prerequisites
Ensure you have the GraalVM SDK or the GraalJS library included in your Java project. If you're using Maven, add the following dependency to your pom.xml:
xml
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>23.0.1</version> <!-- Use the latest version -->
</dependency>
Make sure you have a JavaScript function defined that accepts float parameters.
Steps to Call a JavaScript Function with Float Parameters
1. Define the JavaScript Function
You need a JavaScript function that accepts float parameters. For example, you might have a function that adds two floating-point numbers:
javascript
function addFloats(a, b) {
return a + b;
}
You can either define this function in a JavaScript file or as a string in your Java code.
2. Set Up GraalJS Context
In your Java code, create a GraalJS Context to evaluate JavaScript code and execute functions. Use the Context class from the org.graalvm.polyglot package.
3. Pass Float Parameters
When calling the JavaScript function from Java, you can pass float values directly. GraalJS will handle the type conversion between Java float and JavaScript number.
4. Example Java Code
Here’s a complete example of how to call a JavaScript function with float parameters in GraalJS:
java
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
public class GraalJSExample {
public static void main(String[] args) {
// Create a GraalJS context
try (Context context = Context.newBuilder("js").build()) {
// Define the JavaScript function
String jsCode = "function addFloats(a, b) { return a + b; }";
context.eval("js", jsCode);
// Get the JavaScript function as a callable object
Value addFloatsFunction = context.getBindings("js").getMember("addFloats");
// Call the function with float parameters
float a = 3.14f;
float b = 2.718f;
Value result = addFloatsFunction.execute(a, b);
// Convert the result to a Java float
float sum = result.asFloat();
// Print the result
System.out.println("Sum of " + a + " and " + b + " is: " + sum);
}
}
}
5. Run the Code
Compile and run the Java code. The output should be:
code
Sum of 3.14 and 2.718 is: 5.858
Explanation of the Code
Context Creation:
Context.newBuilder("js").build() creates a GraalJS context for executing JavaScript code.
Evaluating JavaScript Code:
context.eval("js", jsCode) evaluates the JavaScript code, defining the addFloats function in the context.
Accessing the Function:
context.getBindings("js").getMember("addFloats") retrieves the JavaScript function as a Value object, which can be invoked.
Calling the Function:
addFloatsFunction.execute(a, b) calls the JavaScript function, passing the Java float values a and b. GraalJS automatically converts Java float to JavaScript number.
Handling the Result:
result.asFloat() converts the JavaScript result (a number) back to a Java float.
Notes
Type Conversion: GraalJS handles type conversion between Java float and JavaScript number seamlessly. JavaScript does not distinguish between float and double; all numbers are treated as double-precision floating-point numbers.
Error Handling: In a production environment, add error handling for cases where the JavaScript function is not defined, the result is not a number, etc.
Performance: If you plan to call the function multiple times, store the Value object representing the function (e.g., addFloatsFunction) to avoid repeated lookups.
Alternative: Loading JavaScript from a File
If your JavaScript code is in a file (e.g., script.js), you can load it into the context using a Source object:
java
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import java.io.File;
public class GraalJSExample {
public static void main(String[] args) throws Exception {
try (Context context = Context.newBuilder("js").build()) {
// Load JavaScript from a file
File jsFile = new File("script.js");
Source source = Source.newBuilder("js", jsFile).build();
context.eval(source);
// Get the JavaScript function
Value addFloatsFunction = context.getBindings("js").getMember("addFloats");
// Call the function with float parameters
float a = 3.14f;
float b = 2.718f;
Value result = addFloatsFunction.execute(a, b);
// Convert the result to a Java float
float sum = result.asFloat();
// Print the result
System.out.println("Sum of " + a + " and " + b + " is: " + sum);
}
}
}
In this case, script.js would contain:
javascript
function addFloats(a, b) {
return a + b;
}
Conclusion
Using GraalJS, you can easily call JavaScript functions with float parameters by leveraging the Context and Value APIs. The process involves defining the JavaScript function, retrieving it as a callable object, and passing Java float values, which are automatically converted to JavaScript number types.