Skip to content

Commit c61e309

Browse files
committed
Updated example and formatted code
1 parent e9797b4 commit c61e309

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

sites/cheerpj/src/content/docs/11-guides/implementing-native-methods.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ In the JavaScript implementation of `nativeMethodName`, you can use the `lib` pa
7575
```js
7676
// Example placeholders — replace ClassName/javaMethodName with your own
7777
async function Java_ClassName_nativeMethodName(lib) {
78-
const ClassName = await lib.ClassName; // Access your Java class
79-
await ClassName.javaMethodName(); // Call a Java static method
78+
const ClassName = await lib.ClassName; // Access your Java class
79+
await ClassName.javaMethodName(); // Call a Java static method
8080
}
8181
```
8282

@@ -86,7 +86,7 @@ This functionality is useful when you need to call back into the Java class in r
8686

8787
To use the native method in CheerpJ, pass the function to the [`cheerpjInit`] function as a property of the [`natives`] option. There are two ways in which you can do this.
8888

89-
1. **In the function definition directly**:
89+
1. **In the function definition directly**
9090

9191
```js
9292
await cheerpjInit({
@@ -98,7 +98,7 @@ await cheerpjInit({
9898
});
9999
```
100100

101-
2. **Or just the function name if it was defined earlier**:
101+
2. **Or just the function name if it was defined earlier**
102102

103103
```js
104104
async function Java_Example_nativeMethodName(lib, str) {
@@ -121,22 +121,22 @@ Here’s a full example that demonstrates the native method setup in Java and it
121121
```java title="Example.java"
122122
public class Example {
123123
public static void main(String[] args) {
124-
nativeAlert("Hello, world!");
124+
Alert("Hello, world!");
125125
}
126126

127-
public static native void nativeAlert(String message);
127+
public static native void Alert(String message);
128128
}
129129
```
130130

131131
2. Implement the native method by creating an `async` function in JavaScript that follows the naming convention `Java_<fully-qualified-class-name>_<method-name>`.
132132

133133
```js title="index.html"
134-
async function Java_Example_nativeAlert(lib, str) {
134+
async function Java_Example_Alert(lib, str) {
135135
window.alert(str);
136136
}
137137
```
138138

139-
Here, we provide an implementation for the `nativeAlert` method in the `Example` class, by creating a function named `Java_Example_nativeAlert`. The function displays an alert dialog with the message using `window.alert`.
139+
Here, we provide an implementation for the `Alert` method in the `Example` class, by creating a function named `Java_Example_Alert`. The function displays an alert dialog with the message using `window.alert`.
140140

141141
3. Initialize CheerpJ with the `natives` option and pass the native method implementation to [`cheerpjInit`]:
142142

@@ -148,13 +148,15 @@ Here, we provide an implementation for the `nativeAlert` method in the `Example`
148148
<script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script>
149149
</head>
150150
<body>
151-
<script>
152-
async function Java_Example_nativeAlert(lib, str) {
153-
window.alert(str);
151+
<script type="module">
152+
async function Java_Example_Alert(lib, str) {
153+
window.alert(str);
154154
}
155-
156-
await cheerpjInit({ natives: { Java_Example_nativeAlert } });
157-
await cheerpjRunMain("Example", "/app/");
155+
// Init CheerpJ and register natives, then run your main
156+
await cheerpjInit({
157+
natives: { Java_Example_Alert },
158+
});
159+
await cheerpjRunMain("Example", "/app");
158160
</script>
159161
</body>
160162
</html>

0 commit comments

Comments
 (0)