Skip to content

Commit dabeec4

Browse files
committed
Rework JSObject docs
1 parent 4b52ca4 commit dabeec4

File tree

1 file changed

+21
-25
lines changed
  • web-image/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api

1 file changed

+21
-25
lines changed

web-image/src/org.graalvm.webimage.api/src/org/graalvm/webimage/api/JSObject.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* is that a JavaScript object is not normally an instance of any Java class, and it therefore
3131
* cannot be represented as a data-type in Java programs. When the JavaScript code (invoked by the
3232
* method annotated with the {@link JS} annotation) returns a JavaScript object, that object gets
33-
* wrapped into <code>JSObject</code> instance. The <code>JSObject</code> allows the Java code to
33+
* wrapped into a <code>JSObject</code> instance. The <code>JSObject</code> allows the Java code to
3434
* access the fields of the underlying JavaScript object using the <code>get</code> and
3535
* <code>set</code> methods.
3636
*
@@ -123,30 +123,27 @@
123123
*
124124
* Directly exposing a Java object to JavaScript code means that the JavaScript code is able to
125125
* manipulate the data within the object (e.g. mutate fields, add new fields, or redefine existing
126-
* fields), which is not allowed by default for regular Java classes translated by Web Image.
127-
* Extending {@link JSObject} furthermore allows the JavaScript code to instantiate objects of the
128-
* {@link JSObject} subclass. One of the use-cases for these functionalities are JavaScript
129-
* frameworks that redefine properties of JavaScript objects with custom getters and setters, with
130-
* the goal of enabling data-binding or reactive updates.
126+
* fields), which is not allowed by default for regular Java classes. Extending {@link JSObject}
127+
* furthermore allows the JavaScript code to instantiate objects of the {@link JSObject} subclass.
128+
* One of the use-cases for these functionalities are JavaScript frameworks that redefine properties
129+
* of JavaScript objects with custom getters and setters, with the goal of enabling data-binding or
130+
* reactive updates.
131131
*
132132
* In a subclass of {@link JSObject}, every JavaScript property directly corresponds to the Java
133133
* field of the same name. Consequently, all these properties point to native JavaScript values
134-
* rather than Java values, so Web Image generates bridge methods that are called instead of
135-
* property accesses and that convert native JavaScript values to their Java counterparts. The
136-
* conversion rules are the same as in a {@link JS}-annotated method. Furthermore, note that
137-
* JavaScript code can violate the Java type-safety by storing into some property a value that is
138-
* not compatible with the corresponding Java field. For this reason, the bridge methods also
139-
* generate check-casts on every access: if the JavaScript property that corresponds to the Java
140-
* field does not contain a compatible value, a {@link ClassCastException} is thrown.
141-
*
142-
* There are several restrictions that Web Image imposes on {@link JSObject} subclasses:
134+
* rather than Java values, so bridge methods are generated that are called for each property access
135+
* and that convert native JavaScript values to their Java counterparts. The conversion rules are
136+
* the same as in a {@link JS}-annotated method. Furthermore, note that JavaScript code can violate
137+
* the Java type-safety by storing into some property a value that is not compatible with the
138+
* corresponding Java field. For this reason, the bridge methods also generate check-casts on every
139+
* access: if the JavaScript property that corresponds to the Java field does not contain a
140+
* compatible value, a {@link ClassCastException} is thrown.
141+
*
142+
* There are several restrictions imposed on {@link JSObject} subclasses:
143143
* <ul>
144-
* <li>Only public and protected fields are exposed to JavaScript. This restriction ensures
145-
* encapsulation, and also ensures that users cannot introduce two private fields of the same name
146-
* within the same inheritance lineage.</li>
147-
* <li>Subclasses of this class are only allowed to have non-final fields. This restriction ensures
148-
* that JavaScript code cannot inadvertently change the property that corresponds to a final
149-
* field.</li>
144+
* <li>Only public and protected fields are allowed to ensure encapsulation.</li>
145+
* <li>Instance fields must not be {@code final}. This restriction ensures that JavaScript code
146+
* cannot inadvertently change the property that corresponds to a final field.</li>
150147
* </ul>
151148
*
152149
* <b>Example:</b> consider the following <code>JSObject</code> subclass:
@@ -172,8 +169,8 @@
172169
* <pre>
173170
* class Point {
174171
* constructor(x, y){
175-
* this.x=x;
176-
* this.y=y;
172+
* this.x=x;
173+
* this.y=y;
177174
* }
178175
*
179176
* absolute() {
@@ -225,8 +222,7 @@
225222
* </pre>
226223
*
227224
* A {@link Class} object that represents {@link JSObject} can also be passed to JavaScript code.
228-
* The {@link Class} object is converted to its JavaScript representation, which is the JavaScript
229-
* class constructor. The JavaScript class constructor can be used inside a {@code new} expression
225+
* The {@link Class} object is wrapped in a proxy, which can be used inside a {@code new} expression
230226
* to instantiate the object of the corresponding class from JavaScript.
231227
*
232228
* <b>Example:</b> the following code creates a {@code Point} object in JavaScript:

0 commit comments

Comments
 (0)