-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove mention of cheerpjify.py, update JNI guide, use type=module
- Loading branch information
Showing
8 changed files
with
44 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 18 additions & 43 deletions
61
...ntent/docs/cheerpj3/04-guides/Implementing-Java-native-methods-in-JavaScript.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,32 @@ | ||
--- | ||
title: Implementing native methods | ||
subtitle: Java Native Interface (JNI) with CheerpJ | ||
--- | ||
|
||
With CheerpJ, it is possible to implement Java 'native' methods (that would normally be implemented in C/C++ or other AOT-compiled language) in JavaScript, similarly to what would be done in regular Java using the Java Native Interface (JNI). | ||
|
||
## Java Native Methods | ||
As an example, consider the following Java class: | ||
|
||
Take as an example the following Java class | ||
|
||
```java title="SomeClass.java" | ||
public class SomeClass { | ||
public static void someStaticMethod() { | ||
... | ||
} | ||
public float someInstanceMethod() { | ||
... | ||
} | ||
public native int someNativeMethod(); | ||
```java title="TestClass.java" | ||
public class TestClass { | ||
public native void alert(String str); | ||
} | ||
``` | ||
|
||
Java will search for the implementation of `someNativeMethod` using the JNI. | ||
|
||
When compiling this class with CheerpJ, a JavaScript implementation of this method will need to be provided. Implementing native Java methods in JavaScript can be useful to use browser functionalities that are not currently exposed at the Java level. | ||
|
||
## Java Native Methods in CheerpJ | ||
|
||
Implementing native methods is simply a matter of adding a JavaScript function in the global scope with a correctly mangled signature. | ||
To provide an implementation of `someNativeMethod` in JavaScript, pass it to the `cheerpjInit` function as a property of the `natives` object: | ||
|
||
Since this is a rather involved process, the `cheerpjfy.py` script provides functionality to simplify the process by using the `--stub-natives=destinationDir` command line option. | ||
|
||
Assume the previous class has been compiled and packaged in `some.jar`, to generate a directory tree for JS native code you can do: | ||
|
||
```shell | ||
mkdir native/ | ||
cheerpjfy.py --stub-natives=native/ some.jar | ||
```js | ||
cheerpjInit({ | ||
natives: { | ||
Java_TestClass_alert(lib, str) { | ||
alert(str); | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
This will generate a tree of directories under the `native` folder, which will replicate the Java package structure. Each class with at least one native method will generate a `ClassName_native.js` stub file ready to be implemented. | ||
|
||
```js title= "Someclass_native.js" | ||
function _CHEERPJ_COMPRESS(ZN9Someclass16someNativeMethodEVEI)(a0,p) | ||
{ | ||
/*instance*/ | ||
debugger | ||
} | ||
|
||
``` | ||
|
||
Once all have been implemented, native methods can be packaged with the compiled code using the following command: | ||
|
||
```shell | ||
cheerpjfy.py --natives=native/ some.jar | ||
``` | ||
> [!todo] TODO | ||
> Explanation of lib parameter, also consider calling it env to match native JNI | ||
Check failure on line 29 in src/content/docs/cheerpj3/04-guides/Implementing-Java-native-methods-in-JavaScript.md GitHub Actions / vale[vale] src/content/docs/cheerpj3/04-guides/Implementing-Java-native-methods-in-JavaScript.md#L29
Raw output
|
||
CheerpJ uses a compression scheme to encode mangled signatures. The `CHEERPJ_COMPRESS` macro is used automatically by the `cheerpjfy.py --stub-natives=` command, but can also be used manually. For more information about macros visit [this page](/cheerpj3/reference/Command-Line-Options#--stub-nativesnativespath). | ||
> [!todo] TODO | ||
> Explanation of how method names are resolved ([it's not the same as native JNI](https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#resolving_native_method_names)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 0 additions & 103 deletions
103
src/content/docs/cheerpj3/05-reference/02-Command-Line-Options.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters