diff --git a/sdk/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api/JSNumber.java b/sdk/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api/JSNumber.java index f4983570a4f1..1248f93235cb 100644 --- a/sdk/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api/JSNumber.java +++ b/sdk/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api/JSNumber.java @@ -119,4 +119,136 @@ public boolean equals(Object that) { public int hashCode() { return javaDouble().hashCode(); } + + @JS.Coerce + @JS(value = "return isFinite(number);") + public native static boolean isFinite(JSNumber number); + + @JS.Coerce + @JS(value = "return isFinite(number);") + public native static boolean isFinite(double number); + + @JS.Coerce + @JS(value = "return Number.isInteger(number);") + public native static boolean isInteger(JSNumber number); + + @JS.Coerce + @JS(value = "return Number.isInteger(number);") + public native static boolean isInteger(double number); + + @JS.Coerce + @JS(value = "return isNaN(number);") + public native static boolean isNaN(JSValue number); + + @JS.Coerce + @JS(value = "return isNaN(number);") + public native static boolean isNaN(double number); + + @JS.Coerce + @JS(value = "return Number.isSafeInteger(number);") + public native static boolean isSafeInteger(JSValue number); + + @JS.Coerce + @JS(value = "return Number.isSafeInteger(number);") + public native static boolean isSafeInteger(double number); + + @JS.Coerce + @JS(value = "return parseFloat(number);") + public native static float parseFloat(double number); + + @JS.Coerce + @JS(value = "return parseFloat(number);") + public native static float parseFloat(String number); + + @JS.Coerce + @JS(value = "return parseInt(number);") + public native static int parseInt(double number); + + @JS.Coerce + @JS(value = "return parseInt(number);") + public native static int parseInt(String number); + + @JS.Coerce + @JS(value = "return parseInt(number, radix);") + public native static int parseInt(String number, int radix); + + @JS.Coerce + @JS(value = "return Number.EPSILON;") + public native static double EPSILON(); + + @JS.Coerce + @JS(value = "return Number.MAX_SAFE_INTEGER;") + public native static double MAX_SAFE_INTEGER(); + + @JS.Coerce + @JS(value = "return Number.MAX_VALUE;") + public native static double MAX_VALUE(); + + @JS.Coerce + @JS(value = "return Number.MIN_SAFE_INTEGER;") + public native static double MIN_SAFE_INTEGER(); + + @JS.Coerce + @JS(value = "return Number.MIN_VALUE;") + public native static double MIN_VALUE(); + + @JS.Coerce + @JS(value = "return Number.NaN;") + public native static double NaN(); + + @JS.Coerce + @JS(value = "return Number.NEGATIVE_INFINITY;") + public native static double NEGATIVE_INFINITY(); + + @JS.Coerce + @JS(value = "return Number.POSITIVE_INFINITY;") + public native static double POSITIVE_INFINITY(); + + @JS.Coerce + @JS(value = "return this.toExponential();") + public native String toExponential(); + + @JS.Coerce + @JS(value = "return this.toExponential(fractionDigits);") + public native String toExponential(int fractionDigits); + + @JS.Coerce + @JS(value = "return this.toFixed();") + public native String toFixed(); + + @JS.Coerce + @JS(value = "return this.toFixed(digits);") + public native String toFixed(int digits); + + @JS.Coerce + @JS(value = "return this.toLocaleString();") + public native String toLocaleString(); + + @JS.Coerce + @JS(value = "return this.toLocaleString(locales);") + public native String toLocaleString(String locales); + + @JS.Coerce + @JS(value = "return this.toLocaleString(locales, options);") + public native String toLocaleString(String locales, JSObject options); + + @JS.Coerce + @JS(value = "return this.toPrecision();") + public native String toPrecision(); + + @JS.Coerce + @JS(value = "return this.toPrecision(precision);") + public native String toPrecision(int precision); + + @JS.Coerce + @JS(value = "return this.toString(radix);") + private native String toJSString(int radix); + + public String toString(int radix) { + return "JavaScript<" + typeof() + "; " + toJSString(radix) + ">"; + } + + @JS.Coerce + @JS(value = "return this.valueOf();") + public native double valueOf(); } diff --git a/sdk/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api/JSObject.java b/sdk/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api/JSObject.java index cbae872ab70e..ed64c5152d56 100644 --- a/sdk/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api/JSObject.java +++ b/sdk/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api/JSObject.java @@ -49,14 +49,14 @@ * wrapped into a JSObject instance. The JSObject allows the Java code to * access the fields of the underlying JavaScript object using the get and * set methods. - * + *

* The Java {@link JSObject} instance that corresponds to the JavaScript object is called a Java * mirror. Vice versa, the JavaScript instance is a JavaScript mirror for that * {@link JSObject} instance. * * *

Anonymous JavaScript objects

- * + *

* Here are a few examples of creating and modifying anonymous JavaScript objects: * *

@@ -68,7 +68,7 @@
  * System.out.println(pair.get("x"));
  * System.out.println(pair.get("y"));
  * 
- * + *

* In this example, using the {@code JSObject} methods, the user can access the x and * y fields. * @@ -76,32 +76,32 @@ * pair.set("x", 5.4); * System.out.println(pair.get("x")); * - * + *

* The code above sets a new value for the x field, and then prints the new value. * * *

Anonymous JavaScript functions

- * + *

* A {@code JSObject} can be a Java-side wrapper for a JavaScript {@code Function} object. The * JavaScript {@code Function} value can be returned by the JavaScript code of the method annotated * with the {@link JS} annotation. - * + *

* The Java program can then call the underlying function by calling the * {@link JSObject#call(Object, Object...)} method. If the underlying JavaScript object is not * callable, then calling {@code call} will result in an exception. - * + *

* The {@code call} method has the following signature: * *

  * public Object call(Object thisArgument, Object... arguments);
  * 
- * + *

* The {@code invoke} method has the following signature: * *

  * public Object invoke(Object... arguments);
  * 
- * + *

* The difference is that the method {@code call} takes an object for specifying {@code this} of the * JavaScript function, while {@code invoke} uses the underlying {@code JSObject} as the value of * {@code this}. @@ -130,13 +130,13 @@ * * *

Declaring JavaScript classes in Java

- * + *

* The second purpose of {@link JSObject} is to declare JavaScript classes as classes in Java code, * in a way that makes the Java objects look-and-feel like native JavaScript objects. Users should * subclass the {@link JSObject} class when they need to define a JavaScript class whose fields and * methods can be accessed conveniently from Java, without the {@link JSObject#get(Object)} and * {@link JSObject#set(Object, Object)} methods. - * + *

* Directly exposing a Java object to JavaScript code means that the JavaScript code is able to * manipulate the data within the object (e.g. mutate fields, add new fields, or redefine existing * fields), which is not allowed by default for regular Java classes. Extending {@link JSObject} @@ -144,7 +144,7 @@ * One of the use-cases for these functionalities are JavaScript frameworks that redefine properties * of JavaScript objects with custom getters and setters, with the goal of enabling data-binding or * reactive updates. - * + *

* In a subclass of {@link JSObject}, every JavaScript property directly corresponds to the Java * field of the same name. Consequently, all these properties point to native JavaScript values * rather than Java values, so bridge methods are generated that are called for each property access @@ -154,7 +154,7 @@ * corresponding Java field. For this reason, the bridge methods also generate check-casts on every * access: if the JavaScript property that corresponds to the Java field does not contain a * compatible value, a {@link ClassCastException} is thrown. - * + *

* There are several restrictions imposed on {@link JSObject} subclasses: *