Skip to content

Commit

Permalink
improved reference, added a few missing functions, more formatting, s…
Browse files Browse the repository at this point in the history
…pelling checks
  • Loading branch information
GabrielaReyna committed Sep 27, 2023
1 parent bd9675c commit d71f8ac
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ http-server -p 8080

- CheerpJ loader is included from our cloud runtime as
`<script src="https://cjrtnc.leaningtech.com/2.3/loader.js"></script>`.
- CheerpJ runtime environment is initilized by `cheerpjInit()`.
- CheerpJ runtime environment is initialized by `cheerpjInit()`.
- `cheerpjCreateDisplay()` creates a graphical environment to contain all Java windows
- `cheerpjRunMain()` executes the `main` method of `ChangeThisToYourClassName`. The second parameter is a `:` separated list of `.jar` files where application classes can be found (the classpath).
- The `/app/` is a virtual file system mount point that reference the root of the web server this page is loaded from.
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/cheerpj2/04-guides/AOT-optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: AOT Optimization

CheerpJ compiles unmodified `.jar` files to JavaScript as an optimization step. CheerpJ AOT compiler is very easy to use, this page will guide you step by step into compiling an unmodified `.jar` file to a `.jar.js` file. We will also create a basic HTML file integrated with the CheerpJ loader to run the Java application in the browser.

This optimization is not required for running a Java application or applet in the browser but it enhaces performance. Before doing any compilation it is recommended to first run your application without the AOT optimization to ensure everything works.
This optimization is not required for running a Java application or applet in the browser but it enhances performance. Before doing any compilation it is recommended to first run your application without the AOT optimization to ensure everything works.

## CheerpJ AOT compiler installation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,21 @@ mkdir native/
cheerpjfy.py --stub-natives=native/ some.jar
```

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 with ready to be implemented.
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
```

## `CHEERPJ_COMPRESS(x)` macro

CheerpJ uses a compression scheme to encode mangled signatures. The `CHEERPJ_COMPRESS(x)` macro will encode the argument in parenthesis following such scheme. This macro is used automatically by the `cheerpjfy.py --stub-natives=` command, but can also be used manually.

## `CHEERPJ_SET_CLASS(x)` macro

Set the current internal class for resolving fields when using `CHEERPJ_FIELD` and `CHEERPJ_STATIC_FIELD` macros.

## `CHEERPJ_FIELD(x)` and `CHEERPJ_STATIC_FIELD(x)` macro

The compiler replaces this macro with the encoded field name, it assumes the current class has been set by `CHEERPJ_SET_CLASS`.
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](/cheerpj2/reference/Command-Line-Options#--stub-nativesnativespath).
20 changes: 19 additions & 1 deletion src/content/docs/cheerpj2/04-guides/Startup-time-optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@ When initialized with this option CheerpJ will keep track of the classes used at

`cheerpjfy.py` supports a command line option (`--pack-jar`) to generate a minimised JAR for deployment.

This smaller JAR is stripped of all original Java bytecode and can no longer be used to run the application on the JVM. The JAR is however necessary for CheerpJ to support Java reflection.
This smaller JAR is stripped of all original Java bytecode and can no longer be used to run the application on the JVM. The JAR is however necessary for CheerpJ to support Java reflection. This optimisation can be used with our without AOT compiling.

Usage example:

```shell

/cheerpj_2.3/cheerpjfy.py yourInput.jar --pack-jar yourOutput.jar

```

> It is important to use the same name on your input and output `.jar` files for your application to work. This action will overwrite your original `.jar` file with the packed one. We recommend backing up your original files somewhere else to keep their state in case you need to use them again.
To use this command you will require to have java installed on your machine.

More details are available [here](/cheerpj2/reference/Command-Line-Options#pack-jarpackjar)

Expand All @@ -68,6 +80,12 @@ The result will look like this:
["/lts/file1", "/lt/file2"];
```

If the output is not visible fully, you can use:

```js
document.write(cjGetRuntimeResources());
```

The JavaScript console may enclose the string between quotes (`"`), which you should ignore. See [here](/cheerpj2/reference/Runtime-API#cjgetruntimeresources) for more information.

2. Modify the CheerpJ integration to enable preloading. You will only need to change the `cheerpjInit` call, to pass the `preloadResources` option. For example:
Expand Down
14 changes: 7 additions & 7 deletions src/content/docs/cheerpj2/04-guides/Using-web-workers.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
title: Using web workers
title: Using Web Workers
---

## Using the JavaScript web workers API
## Using the JavaScript Web Workers API

CheerpJ supports running Java code in the background using Web Workers. To use this functionality you need to include the `loader.js` script as usual (e.g. `https://cjrtnc.leaningtech.com/latest/loader.js`). The script exposes the APIs described in [JavaScript web worker API](/cheerpj2/reference/WebWorker-API#javascript-web-worker-api). You can use CheerpJ in the main thread at the same time.
CheerpJ supports running Java code in the background using Web Workers. To use this functionality you need to include the `loader.js` script as usual (e.g. `https://cjrtnc.leaningtech.com/latest/loader.js`). The script exposes the APIs described in [JavaScript Web Worker API](/cheerpj2/reference/WebWorker-API#javascript-web-worker-api). You can use CheerpJ in the main thread at the same time.

All code in a Worker runs in parallel and asynchronously with the main thread. All the methods below return standard JavaScript `Promise`s, and you can use `.then(...)`, `.catch(...)` and `async/await` with them.

Expand All @@ -23,7 +23,7 @@ This starts the WebWorker and initializes CheerpJ in that context. All workers n

### Parameters and return values

Web workers do not share any memory with the main threads, and all interactions happen through messages. This imposes limitations on the type of data that can be passed around.
Web Workers do not share any memory with the main threads, and all interactions happen through messages. This imposes limitations on the type of data that can be passed around.

| Data type | Limitations |
| -------------------------------------------- | ------------------------------------------- |
Expand All @@ -38,13 +38,13 @@ It is possible to move Java arrays from the main thread and others `CheerpJWorke

Java Strings, being Java objects, cannot be passed or returned. But JavaScript strings can be used as parameters and will be converted to Java Strings directly in the WebWorker context.

## Using the Java API for web workers
## Using the Java API for Web Workers

CheerpJ exposes a custom API to access this feature directly from Java code. The API is equivalent in terms of capabilities. This API is blocking, so to actually take advantage of concurrency between the main thread and Web Workers it is necessary to use this API from a Java thread.

The Java version of the API is also extended to support `long`s in parameters and returned values. Currently they are converted to native JS values when passed to Workers, so their range is limited to +/-2^52.

See the reference for [Java web worker API](/cheerpj2/reference/WebWorker-API#java-web-worker-api)
See the reference for [Java Web Worker API](/cheerpj2/reference/WebWorker-API#java-web-worker-api)

Example usage:

Expand All @@ -69,4 +69,4 @@ javac -cp cheerpj_install_dir/cheerpj-public.jar WW.java

## Further reading

- [Web worker APIs (reference)](/cheerpj2/reference/WebWorker-API)
- [Web Worker APIs (reference)](/cheerpj2/reference/WebWorker-API)
36 changes: 33 additions & 3 deletions src/content/docs/cheerpj2/05-reference/02-Command-Line-Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@ Do not automatically add the runtime to the class path.
### `--natives=NATIVESPATH`

Root of the native JS implementations for classes in this JAR file.
Assuming the `/natives` directory exists:

### --deps=DEPSPATHS
```shell
cheerpjfy.py --natives=native/ some.jar
```

### `--deps=DEPSPATHS`

List of `:` separated JARs that this JAR depends on. Please note that all the listed JAR paths should be either relative to the target JAR or absolute.

```shell
cheerpjfy.py --deps dependency1.jar:dependency2.jar my_application_archive.jar
```

### `--work-dir=WORKDIRPATH`

A directory where all the JARs are unpacked. This is useful to speed up multiple compilations of the same JARs and to select a different disk when not enough free space is available in the temporary directory. Keep in mind that the directory passed to the option must be manually created _before_ the command is run.
Expand All @@ -49,10 +58,31 @@ Generates a stripped version of the input JAR with all code replaced by nops for
### `--pack-jar=PACKJAR`

Generate a packed version of the input JAR using the pack200 utility. Debug information and code are removed.
To use this command you will require to have java installed on your machine.

```shell
cheerpjfy.py yourInput.jar --pack-jar yourOutput.jar
```

> This action will overwrite your original .jar file with the packed one when using the same filename (necessary for your app to work). We recommend backing up your original files somewhere else to keep their state in case you need to use them again.
### `--stub-natives=NATIVESPATH`

Generates stubs for all native methods from classes in this JAR. The parameter must be an existing directory, it will be populated with new JavaScript files for each class having native methods.

```shell
mkdir native/
cheerpjfy.py --stub-natives=native/ some.jar

```

### --stub-natives=NATIVESPATH
> **Note**: Existing files in the passed directory will be overwritten.
Generates stubs for all native methods from classes in this JAR. The parameter must be an existing directory, it will be populated with new JavaScript files for each class having native methods. **Note**: Existing files in the passed directory will be overwritten.
| macro | Description |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `CHEERPJ_COMPRESS` | Encode the argument in parenthesis following such scheme. |
| `CHEERPJ_SET_CLASS` | Set the current internal class for resolving fields when using `CHEERPJ_FIELD` and `CHEERPJ_STATIC_FIELD` macros. |
| `CHEERPJ_FIELD` | The compiler replaces this macro with the encoded field name, it assumes the current class has been set by `CHEERPJ_SET_CLASS`. |

### `--pack-classes-list=PACKCLASSESLIST`

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/cheerpj2/05-reference/Java-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Converts a JSString to a Java String.
String mystring = Global.JavaString("Hello!");
```

## Global.jsCall / jsCallI /j sCallD / jsCallS
## Global.jsCall / jsCallI / jsCallD / jsCallS

Calls an arbitrary JavaScript function.
| Method | Parameters | Output |
Expand Down
Loading

0 comments on commit d71f8ac

Please sign in to comment.