Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LuaJ bindings #164

Merged
merged 12 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-natives.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ env:

jobs:
apple-natives:
runs-on: macos-latest
runs-on: macos-13
steps:
- uses: actions/checkout@v3
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ luajit/.tags
**/jni-headers/
/scripts/.ipynb_checkpoints/
/scripts/Untitled.ipynb

# Random files
bin/
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

================================================================================
[ LuaJava includes code from LuaJIT, which has this license statement: ]

Expand Down Expand Up @@ -142,6 +143,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

===============================================================================
[ LuaJava includes code (build-natives.yml) from LibGDX, which has this license statement: ]

Expand Down Expand Up @@ -223,3 +225,28 @@ limitations under the License.
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.

===============================================================================
[ LuaJava includes code from LuaJ, which has this license statement: ]

MIT License

Copyright (c) 2007 LuaJ. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

[![Build Status](https://github.com/gudzpoz/luajava/actions/workflows/build-natives.yml/badge.svg)](https://github.com/gudzpoz/luajava/actions/workflows/build-natives.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
![Codecov](https://img.shields.io/codecov/c/github/gudzpoz/luajava?label=Coverage)
![Java 8](https://img.shields.io/badge/Java-8-brown)
[![Codecov](https://img.shields.io/codecov/c/github/gudzpoz/luajava?label=Coverage)](https://app.codecov.io/gh/gudzpoz/luajava/)
[![Java 8](https://img.shields.io/badge/Java-8-brown)](https://www.oracle.com/java/technologies/java8.html)
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/party.iroiro.luajava/luajava?server=https%3A%2F%2Fs01.oss.sonatype.org&label=Nexus&color=pink)](https://s01.oss.sonatype.org/content/repositories/snapshots/party/iroiro/luajava/)
[![Maven Central](https://img.shields.io/maven-central/v/party.iroiro.luajava/luajava?color=blue&label=Maven%20Central)](https://mvnrepository.com/search?q=party.iroiro.luajava)

Expand Down Expand Up @@ -31,7 +31,7 @@ This is yet another fork of [the original LuaJava](https://github.com/jasonsanto

> LuaJava is a scripting tool for Java. The goal of this tool is to allow scripts written in Lua to manipulate components developed in Java. LuaJava allows Java components to be accessed from Lua using the same syntax that is used for accessing Lua's native objects, without any need for declarations or any kind of preprocessing.
>
> LuaJava also allows Java to implement an interface using Lua. This way any interface can be implemented in Lua and passed as parameter to any method, and when called, the equivalent function will be called in Lua, and it's result passed back to Java.
> LuaJava also allows any Java interface to get implemented in Lua and passed as parameter to any method, and when called, the equivalent function will be called in Lua, the result passed back to Java.
>
> LuaJava is available under the same license as Lua 5.1, that is, it can be used at no cost for both academic and commercial purposes.

Expand All @@ -43,24 +43,26 @@ since this library is more or less just a thin wrapper and requires some basic u

<div style="display:flex;justify-content:center">

| Lua 5.1 | Lua 5.2 | Lua 5.3 | Lua 5.4 | LuaJIT |
|:-------:|:-------:|:-------:|:-------:|:-----------:|
| 5.1.5 | 5.2.4 | 5.3.6 | 5.4.6 | [`0d313b2`] |
| Lua 5.1 | Lua 5.2 | Lua 5.3 | Lua 5.4 | LuaJIT | LuaJ |
|:-------:|:-------:|:-------:|:-------:|:-----------:|:--------:|
| 5.1.5 | 5.2.4 | 5.3.6 | 5.4.6 | [`0d313b2`] | [A fork] |

</div>


[`0d313b2`]: https://github.com/LuaJIT/LuaJIT/commits/0d313b243194a0b8d2399d8b549ca5a0ff234db5

Supported Lua versions: Lua 5.1, Lua 5.2, Lua 5.3, Lua 5.4 and LuaJIT.
[A fork]: https://github.com/wagyourtail/luaj

Supported Lua versions: Lua 5.1, Lua 5.2, Lua 5.3, Lua 5.4, LuaJ and LuaJIT.

Supported platforms: **Windows**, **Linux**, **MacOS**, **Android**, **iOS**. Compiled against both ARM and x32/x64. Binaries are not yet tested for iOS.
Supported platforms: **Windows**, **Linux**, **MacOS** and **Android**. Compiled against both ARM and x32/x64. Binaries are not yet tested for iOS.

Compiled natives are available for most common platforms. Check out [LuaJava Platforms](https://gudzpoz.github.io/luajava/#platforms) for a platform matrix.
Compiled natives are available for most common platforms. Check out [LuaJava Platforms](https://gudzpoz.github.io/luajava/#platforms) for a platform matrix. LuaJ bindings do not need native binaries and should run on all platforms theoretically.

### Artifacts

To include LuaJava into your project, you need to include two artifacts, one for the Java bindings, the other for the compiled native binaries.
(For LuaJ bindings, you don't need the latter one. However, you will need to add [JitPack](https://jitpack.io/) to your repositories.)

```groovy
// Example: LuaJIT with Desktop natives
Expand Down Expand Up @@ -127,6 +129,7 @@ You may also have a look at [our tests](./example/src/test/resources).
* [LibGDX](https://github.com/libgdx/libgdx)
* [jnigen](https://github.com/libgdx/gdx-jnigen)
* [Nonlua](https://github.com/deathbeam/jua)
* [LuaJ](https://github.com/wagyourtail/luaj)

## License ##

Expand Down
13 changes: 12 additions & 1 deletion android/android-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ repositories {
mavenLocal()
google()
mavenCentral()
maven {
url 'https://jitpack.io'
}
}

group = rootProject.group
Expand All @@ -15,6 +18,9 @@ ext {
luaVersions = ['lua51', 'lua52', 'lua53', 'lua54', 'luajit']
}

// Lint involves an outdated version of Kotlin, which cause weird errors.
lint.enabled = false

android {
namespace = project(':').group

Expand All @@ -35,7 +41,6 @@ android {
}
}


buildFeatures {
viewBinding true
}
Expand All @@ -48,6 +53,11 @@ android {
packagingOptions {
excludes += 'META-INF/LICENSE*'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand All @@ -57,6 +67,7 @@ dependencies {
androidTestImplementation project(':lua53')
androidTestImplementation project(':lua54')
androidTestImplementation project(':luajit')
androidTestImplementation project(':luaj')
androidTestImplementation project(':example:suite')
androidTestImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:core:1.5.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package party.iroiro.luajava;

import java.lang.reflect.Constructor;
import org.junit.Test;
import party.iroiro.luajava.lua51.Lua51;
import party.iroiro.luajava.lua52.Lua52;
Expand Down Expand Up @@ -42,4 +43,23 @@ public void luaJitTest() {
new LuaTestSuite<>(L, LuaJit::new).test();
}
}

@Test
public void luaJTest() {
org.junit.Assume.assumeTrue(android.os.Build.VERSION.SDK_INT >= 30);
try (AbstractLua L = getLuaJ()) {
new LuaTestSuite<>(L, AndroidLuaTest::getLuaJ).test();
}
}

public static AbstractLua getLuaJ() {
try {
Constructor<?> constructor = Class.forName("party.iroiro.luajava.luaj.LuaJ")
.getConstructor();
return (AbstractLua) constructor.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public void luaJitTest() {
}
}

@Test
public void luaJTest() {
org.junit.Assume.assumeTrue(android.os.Build.VERSION.SDK_INT >= 30);
try (AbstractLua L = AndroidLuaTest.getLuaJ()) {
new LuaScriptSuite<>(L, s -> Log.i("test", s)).test();
}
}

@Test
public void memoryTest() {
//noinspection resource
Expand All @@ -58,5 +66,10 @@ public void memoryTest() {
LuaScriptSuite.memoryTest(L);
L.close();
}
if (android.os.Build.VERSION.SDK_INT >= 30) {
Lua L = AndroidLuaTest.getLuaJ();
LuaScriptSuite.memoryTest(L);
L.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public void luaJitTest() {
new LuaValueSuite<>(L).test();
}
}

@Test
public void luaJTest() {
org.junit.Assume.assumeTrue(android.os.Build.VERSION.SDK_INT >= 30);
try (Lua L = AndroidLuaTest.getLuaJ()) {
new LuaValueSuite<>(L).test();
}
}

}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ task allJavadoc(type: Javadoc) {
'lua53',
'lua54',
'luajit',
'luaj',
'luajava',
]
source subprojects
Expand Down
5 changes: 3 additions & 2 deletions docs/.vuepress/components/Matrix.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<script setup>
import { ref } from 'vue'
const groupId = 'party.iroiro.luajava'
const version = '3.4.1'
const version = '3.5.0'
const notAvailable = {
luajit: {
Expand All @@ -25,6 +25,7 @@ const luaVersions = {
lua53: 'Lua 5.3',
lua54: 'Lua 5.4',
luajit: 'LuaJIT',
luaj: 'LuaJ',
}
const platform = ref('Desktop')
Expand Down Expand Up @@ -65,7 +66,7 @@ function lines(lua, platform, line) {
const native = natives[platform]
return `${line(groupId, 'luajava', version, null)}
${line(groupId, lua, version, null)}
${native.map(n => line(groupId, lua + '-platform', version, n, 'runtime')).join('\n')}`
${lua === 'luaj' ? '' : native.map(n => line(groupId, lua + '-platform', version, n, 'runtime')).join('\n')}`
}
}
</script>
Expand Down
14 changes: 11 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ For a `jclass` `clazz`:
4. Otherwise, it prepares for a method call. See `clazz:memberMethod(...)` below.
- `clazz.memberVar = value` assigns to the public static member. If exceptions occur, a Lua error is generated.
- `clazz:memberMethod(...)` calls the public static member method `memberMethod`. See [Proxied Method Calls](#proxied-method-calls) for more info.
- `class(...)`:
- `clazz(...)`:
- For an interface, this expects a table as the parameter and creates a proxy for it. See [`java.proxy`](#proxy-jclass-table-function).
- Otherwise, it calls the corresponding constructor. See [`java.new`](#new-jclass-function).
- `clazz.class` returns a `jobject`, wrapping an instance of `java.lang.Class<clazz>`.
Expand Down Expand Up @@ -98,7 +98,7 @@ Creates a Java array.

- **Parameters:**

- `jclass`: (***jclass*** | ***jobject***) The component type. One may pass a `jclass` or a `jobject` of `Class<?>`.
- `jclass`: (***jclass*** | ***jobject***) The component type. One may pass a `jclass` or a `jobject` of `Class<?>`.

- `dim1`: (***number***) The size of the first dimension.

Expand Down Expand Up @@ -465,7 +465,7 @@ assert(max(1.2, 2.3) == 2.3)
:::

If you would like to access an overridden default method from a proxy object,
you may also use
you may also use:

```lua
iter1 = java.proxy('java.util.Iterator', {})
Expand All @@ -486,3 +486,11 @@ iter = java.proxy('java.util.Iterator', iterImpl)
-- Calls the implemented `remove`, which then calls the default one
iter:remove()
```

::: warning

Calling default methods is not available with LuaJ bindings,
since the Java reflection does not provide a way to do so.
(We use JNI functions to achieve this within binary bindings.)

:::
3 changes: 1 addition & 2 deletions docs/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The jar bundles desktop natives with it, so you should be about to play around w

<ClientOnly><Asciinema :file="$withBase('/example.cast')" /></ClientOnly>

`Lua Version` can be any of `5.1`, `5.2`, `5.3`, `5.4` or `jit`.
`Lua Version` can be any of `5.1`, `5.2`, `5.3`, `5.4`, `luaj` or `jit`.

## Built-In Examples

Expand All @@ -33,4 +33,3 @@ Usage: <main class> [-t] [-l=<lua>] [-f=<file> | -e=<expression>]
-l, --lua=<lua> Specify the Lua version
-t, --test Run built-in tests
```

9 changes: 7 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ So you will need both to get LuaJava to work correctly. Basically, using Maven C

- The `groupId` is `party.iroiro.luajava`.
- The Java interface is `party.iroiro.luajava:luajava`.
- The Lua specific bridging artifacts are `lua5N` (`lua51` `lua52` ...) or `luajit`.
- The natives has `artifactId` like `lua5N-platform` (`lua51` `lua52` ...) or `luajit-platform`.
- The Lua specific bridging artifacts are `lua5N` (`lua51` `lua52` ...) or `luajit` or `luaj`.
- The natives has `artifactId` like `lua5N-platform` (`lua51` `lua52` ...) or `luajit-platform`. (No need for LuaJ.)
- LuaJ bindings need the [JitPack](https://jitpack.io/) repository.

However, there are different native artifacts for different platforms, each with a different `classifier`:

Expand Down Expand Up @@ -51,6 +52,10 @@ to get things work.
You can choose between the following two configurations. The former one uses a pre-bundled AAR archive while the latter
might give a little more flexibility.

For LuaJ bindings, you don't need a binary artifact.
However, the LuaJ library uses some Java 8 API unsupported by older Android API levels,
and you might need to configure [Android Java 8+ API Desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring) in your application.

### Using bundled AAR files

The AAR archive bundles native binaries for `armeabi-v7a` `arm64-v8a` `x86` and `x86_64`.
Expand Down
Loading
Loading