├── settings.gradle.kts
├── .gitignore
├── luajavax
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── bennyhuo
│ │ │ └── luajavax
│ │ │ ├── LuaFactory.kt
│ │ │ ├── utils
│ │ │ └── IO.kt
│ │ │ ├── core
│ │ │ ├── ILua.kt
│ │ │ ├── DummyLua.kt
│ │ │ └── Lua.kt
│ │ │ ├── annotations
│ │ │ └── LuaApi.java
│ │ │ └── log
│ │ │ └── AndroidLog.kt
│ └── test
│ │ └── java
│ │ └── com
│ │ └── bennyhuo
│ │ └── luajavax
│ │ └── ExampleUnitTest.java
├── build.gradle.kts
└── proguard-rules.pro
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── app
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ └── layout
│ │ │ ├── activity_jnitest.xml
│ │ │ └── activity_main.xml
│ │ ├── assets
│ │ ├── lua
│ │ │ ├── setup.lua
│ │ │ └── TestErrorMessageForJavaMethodCall.lua
│ │ └── logback.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── bennyhuo
│ │ └── luajavax
│ │ └── sample
│ │ ├── MainActivity.kt
│ │ └── cases
│ │ └── LuaTests.kt
├── proguard-rules.pro
└── build.gradle.kts
├── luajava
├── src
│ ├── main
│ │ ├── AndroidManifest.xml
│ │ ├── jni
│ │ │ ├── lua
│ │ │ │ ├── lua.hpp
│ │ │ │ ├── lapi.h
│ │ │ │ ├── lundump.h
│ │ │ │ ├── lprefix.h
│ │ │ │ ├── lualib.h
│ │ │ │ ├── ldebug.h
│ │ │ │ ├── lstring.h
│ │ │ │ ├── lzio.c
│ │ │ │ ├── lzio.h
│ │ │ │ ├── lfunc.h
│ │ │ │ ├── linit.c
│ │ │ │ ├── ltm.h
│ │ │ │ ├── ltable.h
│ │ │ │ ├── lctype.h
│ │ │ │ ├── ldo.h
│ │ │ │ ├── lctype.c
│ │ │ │ ├── llex.h
│ │ │ │ ├── lmem.h
│ │ │ │ ├── lmem.c
│ │ │ │ ├── lcode.h
│ │ │ │ ├── lopcodes.c
│ │ │ │ ├── lvm.h
│ │ │ │ ├── lfunc.c
│ │ │ │ ├── lcorolib.c
│ │ │ │ ├── lparser.h
│ │ │ │ ├── lgc.h
│ │ │ │ ├── ltm.c
│ │ │ │ ├── ldump.c
│ │ │ │ ├── lbitlib.c
│ │ │ │ ├── lundump.c
│ │ │ │ ├── Makefile
│ │ │ │ ├── lstring.c
│ │ │ │ ├── lutf8lib.c
│ │ │ │ ├── lstate.h
│ │ │ │ ├── llimits.h
│ │ │ │ ├── lauxlib.h
│ │ │ │ └── lopcodes.h
│ │ │ ├── CMakeLists.txt
│ │ │ └── luajava
│ │ │ │ └── log.h
│ │ └── java
│ │ │ └── org
│ │ │ └── keplerproject
│ │ │ └── luajava
│ │ │ ├── LuaException.java
│ │ │ ├── CPtr.java
│ │ │ ├── LuaInvocationHandler.java
│ │ │ ├── Console.java
│ │ │ ├── JavaFunction.java
│ │ │ └── LuaStateFactory.java
│ └── test
│ │ └── java
│ │ └── org
│ │ └── keplerproject
│ │ └── luajava
│ │ └── ExampleUnitTest.java
├── build.gradle.kts
└── proguard-rules.pro
├── LICENSE
├── gradle.properties
├── gradlew.bat
├── README.md
└── gradlew
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | include(":app",":luajava", ":luajavax")
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | build
2 | .gradle
3 | .idea
4 | local.properties
5 | captures
6 | .cxx
7 | .externalNativeBuild
8 | out
9 |
--------------------------------------------------------------------------------
/luajavax/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | luax
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bennyhuo/Android-LuaJavax/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bennyhuo/Android-LuaJavax/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/luajavax/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/app/src/main/assets/lua/setup.lua:
--------------------------------------------------------------------------------
1 | print = function(...)
2 | logger:debug(tostring(...))
3 | end
4 |
5 | function redirect_output(path)
6 | io.output(path)
7 | end
--------------------------------------------------------------------------------
/luajava/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/assets/lua/TestErrorMessageForJavaMethodCall.lua:
--------------------------------------------------------------------------------
1 | --comment 1
2 | --comment 2
3 |
4 | print("Hello World")
5 |
6 | logger.debug("This is an invalid Java method call.")
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Android-LuaJava
3 |
4 | Hello world!
5 | Settings
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lua.hpp:
--------------------------------------------------------------------------------
1 | // lua.hpp
2 | // Lua header files for C++
3 | // <> not supplied automatically because Lua also compiles as C++
4 |
5 | extern "C" {
6 | #include "lua.h"
7 | #include "lualib.h"
8 | #include "lauxlib.h"
9 | }
10 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Feb 17 15:03:15 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
7 |
--------------------------------------------------------------------------------
/luajavax/src/main/java/com/bennyhuo/luajavax/LuaFactory.kt:
--------------------------------------------------------------------------------
1 | package com.bennyhuo.luajavax
2 |
3 | import android.content.Context
4 | import com.bennyhuo.luajavax.core.ILua
5 | import com.bennyhuo.luajavax.core.Lua
6 |
7 | object LuaFactory {
8 |
9 | @JvmStatic
10 | fun createLua(context: Context): ILua = Lua(context)
11 |
12 | }
--------------------------------------------------------------------------------
/luajavax/src/main/java/com/bennyhuo/luajavax/utils/IO.kt:
--------------------------------------------------------------------------------
1 | package com.bennyhuo.luajavax.utils
2 |
3 | import java.io.InputStream
4 | import java.io.OutputStream
5 |
6 | fun InputStream.copyToAndClose(out: OutputStream, bufferSize: Int = DEFAULT_BUFFER_SIZE): Long {
7 | return copyTo(out, bufferSize).also {
8 | this.close()
9 | out.close()
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_jnitest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
9 |
--------------------------------------------------------------------------------
/app/src/main/assets/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | [%thread](%file:%line\)%M: %msg%n
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/luajavax/src/test/java/com/bennyhuo/luajavax/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.bennyhuo.luajavax;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/luajava/src/test/java/org/keplerproject/luajava/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package org.keplerproject.luajava;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lapi.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 roberto Exp $
3 | ** Auxiliary functions from Lua API
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lapi_h
8 | #define lapi_h
9 |
10 |
11 | #include "llimits.h"
12 | #include "lstate.h"
13 |
14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
15 | "stack overflow");}
16 |
17 | #define adjustresults(L,nres) \
18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
19 |
20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
21 | "not enough elements in the stack")
22 |
23 |
24 | #endif
25 |
--------------------------------------------------------------------------------
/luajavax/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | id("com.android.library")
3 | kotlin("android")
4 | }
5 |
6 | android {
7 | compileSdkVersion(30)
8 |
9 | defaultConfig {
10 | minSdkVersion(16)
11 | targetSdkVersion(30)
12 | versionCode = 1
13 | versionName = "1.0"
14 |
15 | consumerProguardFiles("proguard-rules.pro")
16 | }
17 |
18 | buildTypes {
19 | val release by getting {
20 | isMinifyEnabled = false
21 | proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation(fileTree("dir" to "libs", "include" to arrayOf("*.jar")))
29 | api(project(":luajava"))
30 |
31 | implementation("org.slf4j:slf4j-api:1.7.21")
32 |
33 | testImplementation("junit:junit:4.12")
34 | }
35 |
--------------------------------------------------------------------------------
/luajava/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | id("com.android.library")
3 | }
4 |
5 | android {
6 | compileSdkVersion(30)
7 |
8 | defaultConfig {
9 | minSdkVersion(16)
10 | targetSdkVersion(30)
11 | versionCode = 1
12 | versionName = "1.0"
13 |
14 | consumerProguardFiles("proguard-rules.pro")
15 | }
16 |
17 | buildTypes {
18 | val release by getting {
19 | isMinifyEnabled = false
20 | proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
21 | }
22 | }
23 |
24 | externalNativeBuild {
25 | cmake {
26 | path = file("src/main/jni/CMakeLists.txt")
27 | }
28 | }
29 |
30 | }
31 |
32 | dependencies {
33 | implementation(fileTree("dir" to "libs", "include" to arrayOf("*.jar")))
34 | testImplementation("junit:junit:4.12")
35 | }
36 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\studio_sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # luajava
20 |
21 | -keep class kotlin.** {
22 | *;
23 | }
24 |
25 | -keep class kotlinx.** {
26 | *;
27 | }
28 |
29 | -keep class ch.qos.logback.** {
30 | *;
31 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
12 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/luajavax/src/main/java/com/bennyhuo/luajavax/core/ILua.kt:
--------------------------------------------------------------------------------
1 | package com.bennyhuo.luajavax.core
2 |
3 | import java.io.Closeable
4 | import java.io.File
5 | import java.io.InputStream
6 |
7 | interface ILua : Closeable {
8 |
9 | val isClosed: Boolean
10 |
11 | fun redirectStdoutToFile(outputFile: File): Boolean
12 | fun redirectStdioToLogcat(): Boolean
13 |
14 | operator fun set(name: String, value: Any?): Boolean
15 | operator fun get(name: String): T?
16 | fun getInt(name: String): Int?
17 | fun getDouble(name: String): Double?
18 | fun getString(name: String): String?
19 | fun getBoolean(name: String): Boolean?
20 |
21 | fun runText(luaScriptText: String): Boolean
22 | fun runFile(luaScriptFile: File): Boolean
23 | fun runStream(luaScriptStream: InputStream): Boolean
24 | fun runScriptInAssets(scriptPath: String): Boolean
25 |
26 | override fun close()
27 | fun finalize()
28 | }
29 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lundump.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lundump.h,v 1.45 2015/09/08 15:41:05 roberto Exp $
3 | ** load precompiled Lua chunks
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lundump_h
8 | #define lundump_h
9 |
10 | #include "llimits.h"
11 | #include "lobject.h"
12 | #include "lzio.h"
13 |
14 |
15 | /* data to catch conversion errors */
16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n"
17 |
18 | #define LUAC_INT 0x5678
19 | #define LUAC_NUM cast_num(370.5)
20 |
21 | #define MYINT(s) (s[0]-'0')
22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR))
23 | #define LUAC_FORMAT 0 /* this is the official format */
24 |
25 | /* load one chunk; from lundump.c */
26 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name);
27 |
28 | /* dump one chunk; from ldump.c */
29 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
30 | void* data, int strip);
31 |
32 | #endif
33 |
--------------------------------------------------------------------------------
/luajavax/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
23 | -dontwarn ch.qos.logback.**
24 |
25 | -keep class com.bennyhuo.luajavax.annotations.*
26 | -keep @com.bennyhuo.luajavax.annotations.LuaApi class **.* {
27 | *;
28 | }
--------------------------------------------------------------------------------
/luajavax/src/main/java/com/bennyhuo/luajavax/annotations/LuaApi.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Bennyhuo.
3 | *
4 | * Licensed under the MIT License (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://opensource.org/licenses/MIT
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | *
17 | */
18 |
19 | package com.bennyhuo.luajavax.annotations;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | @Target(ElementType.TYPE)
27 | @Retention(RetentionPolicy.CLASS)
28 | public @interface LuaApi {
29 | }
30 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lprefix.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $
3 | ** Definitions for Lua code that must come before any other header file
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lprefix_h
8 | #define lprefix_h
9 |
10 |
11 | /*
12 | ** Allows POSIX/XSI stuff
13 | */
14 | #if !defined(LUA_USE_C89) /* { */
15 |
16 | #if !defined(_XOPEN_SOURCE)
17 | #define _XOPEN_SOURCE 600
18 | #elif _XOPEN_SOURCE == 0
19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */
20 | #endif
21 |
22 | /*
23 | ** Allows manipulation of large files in gcc and some other compilers
24 | */
25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS)
26 | #define _LARGEFILE_SOURCE 1
27 | #define _FILE_OFFSET_BITS 64
28 | #endif
29 |
30 | #endif /* } */
31 |
32 |
33 | /*
34 | ** Windows stuff
35 | */
36 | #if defined(_WIN32) /* { */
37 |
38 | #if !defined(_CRT_SECURE_NO_WARNINGS)
39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
40 | #endif
41 |
42 | #endif /* } */
43 |
44 | #endif
45 |
46 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bennyhuo/luajavax/sample/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.bennyhuo.luajavax.sample
2 |
3 | import android.app.Activity
4 | import android.os.Bundle
5 | import com.bennyhuo.luajavax.sample.cases.*
6 | import kotlinx.android.synthetic.main.activity_main.*
7 | import org.slf4j.LoggerFactory
8 |
9 | val logger = LoggerFactory.getLogger("LuaJava")
10 |
11 | class MainActivity : Activity() {
12 |
13 | val sharedPreferences by lazy {
14 | getSharedPreferences("Android-LuaJava", 0)
15 | }
16 |
17 | override fun onCreate(savedInstanceState: Bundle?) {
18 | super.onCreate(savedInstanceState)
19 | setContentView(R.layout.activity_main)
20 |
21 | runLua.setOnClickListener {
22 | // basic()
23 | // testErrorMessageForJavaMethodCall()
24 | // testNameConflictForFieldAndMethod()
25 | // testNestedJavaMethodCall()
26 | // testBindClass()
27 | // testJavaNew()
28 | // testJavaMethodNotFound()
29 | // testLuaStdio()
30 | testValues()
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/luajavax/src/main/java/com/bennyhuo/luajavax/log/AndroidLog.kt:
--------------------------------------------------------------------------------
1 | package com.bennyhuo.luajavax.log
2 |
3 | import android.util.Log
4 |
5 | object AndroidLog {
6 | private val androidLogConstructor by lazy {
7 | try {
8 | Class.forName("com.android.internal.os.AndroidPrintStream")
9 | .getConstructor(Int::class.javaPrimitiveType, String::class.java)
10 | ?.also {
11 | it.isAccessible = true
12 | }
13 | } catch (e: Exception) {
14 | e.printStackTrace()
15 | null
16 | }
17 | }
18 |
19 | val stdout by lazy {
20 | try {
21 | androidLogConstructor?.newInstance(Log.INFO, "luajavax")
22 | } catch (e: Exception) {
23 | e.printStackTrace()
24 | null
25 | }
26 | }
27 |
28 | val stderr by lazy {
29 | try {
30 | androidLogConstructor?.newInstance(Log.WARN, "luajavax")
31 | } catch (e: Exception) {
32 | e.printStackTrace()
33 | null
34 | }
35 | }
36 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Bennyhuo
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/luajava/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\studio_sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
27 | -keep class org.keplerproject.**.*{
28 | *;
29 | }
30 | # For native methods
31 | -keepclasseswithmembernames class * {
32 | native ;
33 | }
34 |
--------------------------------------------------------------------------------
/app/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | id("com.android.application")
3 | kotlin("android")
4 | kotlin("android.extensions")
5 | }
6 |
7 | android {
8 | compileSdkVersion(30)
9 |
10 | defaultConfig {
11 | minSdkVersion(16)
12 | targetSdkVersion(30)
13 | versionCode = 1
14 | versionName = "1.0"
15 | }
16 | buildTypes {
17 | val release by getting {
18 | isMinifyEnabled = false
19 | signingConfig = signingConfigs.getByName("debug")
20 | proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
21 | }
22 | }
23 |
24 | lintOptions {
25 | isCheckReleaseBuilds = false
26 | // Or, if you prefer, you can continue to check for errors in release builds,
27 | // but continue the build even when errors are found:
28 | isAbortOnError = false
29 | }
30 | }
31 |
32 | tasks.withType {
33 | options.encoding = "UTF-8"
34 | }
35 |
36 | dependencies {
37 | implementation(project(":luajavax"))
38 | api("org.jetbrains.kotlin:kotlin-stdlib:1.4.30")
39 |
40 | api("org.slf4j:slf4j-api:1.7.21")
41 | api("com.github.tony19:logback-android-core:1.1.1-6")
42 | api("com.github.tony19:logback-android-classic:1.1.1-6") {
43 | // workaround issue #73
44 | exclude(group = "com.google.android", module = "android")
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lualib.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 roberto Exp $
3 | ** Lua standard libraries
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #ifndef lualib_h
9 | #define lualib_h
10 |
11 | #include "lua.h"
12 |
13 |
14 |
15 | LUAMOD_API int (luaopen_base) (lua_State *L);
16 |
17 | #define LUA_COLIBNAME "coroutine"
18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L);
19 |
20 | #define LUA_TABLIBNAME "table"
21 | LUAMOD_API int (luaopen_table) (lua_State *L);
22 |
23 | #define LUA_IOLIBNAME "io"
24 | LUAMOD_API int (luaopen_io) (lua_State *L);
25 |
26 | #define LUA_OSLIBNAME "os"
27 | LUAMOD_API int (luaopen_os) (lua_State *L);
28 |
29 | #define LUA_STRLIBNAME "string"
30 | LUAMOD_API int (luaopen_string) (lua_State *L);
31 |
32 | #define LUA_UTF8LIBNAME "utf8"
33 | LUAMOD_API int (luaopen_utf8) (lua_State *L);
34 |
35 | #define LUA_BITLIBNAME "bit32"
36 | LUAMOD_API int (luaopen_bit32) (lua_State *L);
37 |
38 | #define LUA_MATHLIBNAME "math"
39 | LUAMOD_API int (luaopen_math) (lua_State *L);
40 |
41 | #define LUA_DBLIBNAME "debug"
42 | LUAMOD_API int (luaopen_debug) (lua_State *L);
43 |
44 | #define LUA_LOADLIBNAME "package"
45 | LUAMOD_API int (luaopen_package) (lua_State *L);
46 |
47 |
48 | /* open all previous libraries */
49 | LUALIB_API void (luaL_openlibs) (lua_State *L);
50 |
51 |
52 |
53 | #if !defined(lua_assert)
54 | #define lua_assert(x) ((void)0)
55 | #endif
56 |
57 |
58 | #endif
59 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/ldebug.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp $
3 | ** Auxiliary functions from Debug Interface module
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef ldebug_h
8 | #define ldebug_h
9 |
10 |
11 | #include "lstate.h"
12 |
13 |
14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
15 |
16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1)
17 |
18 | #define resethookcount(L) (L->hookcount = L->basehookcount)
19 |
20 |
21 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
22 | const char *opname);
23 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
24 | const TValue *p2);
25 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
26 | const TValue *p2,
27 | const char *msg);
28 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
29 | const TValue *p2);
30 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
31 | const TValue *p2);
32 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
33 | LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
34 | TString *src, int line);
35 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
36 | LUAI_FUNC void luaG_traceexec (lua_State *L);
37 |
38 |
39 | #endif
40 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
19 |
20 | # Android configuration
21 |
22 | GROUP=com.bennyhuo
23 | VERSION_NAME=1.0
24 |
25 | POM_NAME=Android-Luajavax
26 |
27 | POM_URL=https://github.com/bennyhuo/Android-LuaJavax
28 | POM_DESCRIPTION=Run Lua scripts in Android APPs
29 |
30 | POM_SCM_URL=https://github.com/bennyhuo/Android-LuaJavax
31 | POM_SCM_CONNECTION=scm:git:git://github.com/bennyhuo/Android-LuaJavax.git
32 | POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com:bennyhuo/Android-LuaJavax.git
33 |
34 | POM_LICENCE_NAME=MIT License
35 | POM_LICENCE_URL=https://github.com/bennyhuo/Android-LuaJavax/blob/master/LICENSE
36 | POM_LICENCE_DIST=repo
37 |
38 | POM_DEVELOPER_ID=bennyhuo
39 | POM_DEVELOPER_NAME=Benny Huo
40 | POM_DEVELOPER_URL=https://github.com/bennyhuo/
41 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lstring.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp $
3 | ** String table (keep all strings handled by Lua)
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lstring_h
8 | #define lstring_h
9 |
10 | #include "lgc.h"
11 | #include "lobject.h"
12 | #include "lstate.h"
13 |
14 |
15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
16 |
17 | #define sizeludata(l) (sizeof(union UUdata) + (l))
18 | #define sizeudata(u) sizeludata((u)->len)
19 |
20 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
21 | (sizeof(s)/sizeof(char))-1))
22 |
23 |
24 | /*
25 | ** test whether a string is a reserved word
26 | */
27 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0)
28 |
29 |
30 | /*
31 | ** equality for short strings, which are always internalized
32 | */
33 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b))
34 |
35 |
36 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
37 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts);
38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
39 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
40 | LUAI_FUNC void luaS_clearcache (global_State *g);
41 | LUAI_FUNC void luaS_init (lua_State *L);
42 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
43 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s);
44 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
45 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
46 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
47 |
48 |
49 | #endif
50 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lzio.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lzio.c,v 1.37 2015/09/08 15:41:05 roberto Exp $
3 | ** Buffered streams
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lzio_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "llimits.h"
18 | #include "lmem.h"
19 | #include "lstate.h"
20 | #include "lzio.h"
21 |
22 |
23 | int luaZ_fill (ZIO *z) {
24 | size_t size;
25 | lua_State *L = z->L;
26 | const char *buff;
27 | lua_unlock(L);
28 | buff = z->reader(L, z->data, &size);
29 | lua_lock(L);
30 | if (buff == NULL || size == 0)
31 | return EOZ;
32 | z->n = size - 1; /* discount char being returned */
33 | z->p = buff;
34 | return cast_uchar(*(z->p++));
35 | }
36 |
37 |
38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
39 | z->L = L;
40 | z->reader = reader;
41 | z->data = data;
42 | z->n = 0;
43 | z->p = NULL;
44 | }
45 |
46 |
47 | /* --------------------------------------------------------------- read --- */
48 | size_t luaZ_read (ZIO *z, void *b, size_t n) {
49 | while (n) {
50 | size_t m;
51 | if (z->n == 0) { /* no bytes in buffer? */
52 | if (luaZ_fill(z) == EOZ) /* try to read more */
53 | return n; /* no more input; return number of missing bytes */
54 | else {
55 | z->n++; /* luaZ_fill consumed first byte; put it back */
56 | z->p--;
57 | }
58 | }
59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
60 | memcpy(b, z->p, m);
61 | z->n -= m;
62 | z->p += m;
63 | b = (char *)b + m;
64 | n -= m;
65 | }
66 | return 0;
67 | }
68 |
69 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # For more information about using CMake with Android Studio, read the
2 | # documentation: https://d.android.com/studio/projects/add-native-code.html
3 |
4 | # Sets the minimum version of CMake required to build the native library.
5 |
6 | cmake_minimum_required(VERSION 3.10.2)
7 |
8 | # Declares and names the project.
9 |
10 | project(luajava C)
11 | set(CMAKE_C_STANDARD 99)
12 |
13 | # Creates and names a library, sets it as either STATIC
14 | # or SHARED, and provides the relative paths to its source code.
15 | # You can define multiple libraries, and CMake builds them for you.
16 | # Gradle automatically packages shared libraries with your APK.
17 |
18 | include_directories(lua)
19 | include_directories(luajava)
20 |
21 | set(CMAKE_C_FLAGS "-DLUA_DL_DLOPEN -DLUA_USE_C89 -DLUA_COMPAT_5_1 -DLUA_COMPAT_5_2 -DLUA_USE_LINUX -fPIE")
22 |
23 | file(GLOB lua_files "${CMAKE_CURRENT_SOURCE_DIR}/lua/*.c")
24 | list(FILTER lua_files EXCLUDE REGEX "${CMAKE_CURRENT_SOURCE_DIR}/lua/lua.*" )
25 | add_library(luajava SHARED luajava/luajava.c ${lua_files})
26 |
27 | # Searches for a specified prebuilt library and stores the path as a
28 | # variable. Because CMake includes system libraries in the search path by
29 | # default, you only need to specify the name of the public NDK library
30 | # you want to add. CMake verifies that the library exists before
31 | # completing its build.
32 |
33 | find_library(log-lib log)
34 |
35 | # Specifies libraries CMake should link to your target library. You
36 | # can link multiple libraries, such as libraries you define in this
37 | # build script, prebuilt third-party libraries, or system libraries.
38 |
39 | target_link_libraries(luajava ${log-lib})
--------------------------------------------------------------------------------
/luajavax/src/main/java/com/bennyhuo/luajavax/core/DummyLua.kt:
--------------------------------------------------------------------------------
1 | package com.bennyhuo.luajavax.core
2 |
3 | import java.io.File
4 | import java.io.InputStream
5 |
6 | /**
7 | * Created by benny on 21/11/2018.
8 | */
9 | class DummyLua : ILua {
10 |
11 | private var outputFile: File? = null
12 |
13 | private fun printTips(): Boolean {
14 | outputFile?.writeText("Lua is disabled.\n")
15 | return false
16 | }
17 |
18 | override val isClosed: Boolean = true
19 |
20 | override fun redirectStdoutToFile(outputFile: File): Boolean {
21 | this.outputFile = outputFile
22 | return true
23 | }
24 |
25 | override fun redirectStdioToLogcat(): Boolean {
26 | return true
27 | }
28 |
29 | override fun set(name: String, value: Any?): Boolean = printTips()
30 | override fun get(name: String): T? {
31 | return null
32 | }
33 |
34 | override fun getInt(name: String): Int? {
35 | return null
36 | }
37 |
38 | override fun getDouble(name: String): Double? {
39 | return null
40 | }
41 |
42 | override fun getString(name: String): String? {
43 | return null
44 | }
45 |
46 | override fun getBoolean(name: String): Boolean? {
47 | return null
48 | }
49 |
50 | override fun runText(luaScriptText: String): Boolean = printTips()
51 |
52 | override fun runFile(luaScriptFile: File): Boolean = printTips()
53 |
54 | override fun runStream(luaScriptStream: InputStream): Boolean = printTips()
55 |
56 | override fun runScriptInAssets(scriptPath: String): Boolean = printTips()
57 |
58 | override fun close() {
59 | }
60 |
61 | override fun finalize() {
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lzio.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lzio.h,v 1.31 2015/09/08 15:41:05 roberto Exp $
3 | ** Buffered streams
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #ifndef lzio_h
9 | #define lzio_h
10 |
11 | #include "lua.h"
12 |
13 | #include "lmem.h"
14 |
15 |
16 | #define EOZ (-1) /* end of stream */
17 |
18 | typedef struct Zio ZIO;
19 |
20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z))
21 |
22 |
23 | typedef struct Mbuffer {
24 | char *buffer;
25 | size_t n;
26 | size_t buffsize;
27 | } Mbuffer;
28 |
29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
30 |
31 | #define luaZ_buffer(buff) ((buff)->buffer)
32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize)
33 | #define luaZ_bufflen(buff) ((buff)->n)
34 |
35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i))
36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0)
37 |
38 |
39 | #define luaZ_resizebuffer(L, buff, size) \
40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \
41 | (buff)->buffsize, size), \
42 | (buff)->buffsize = size)
43 |
44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
45 |
46 |
47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
48 | void *data);
49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */
50 |
51 |
52 |
53 | /* --------- Private Part ------------------ */
54 |
55 | struct Zio {
56 | size_t n; /* bytes still unread */
57 | const char *p; /* current position in buffer */
58 | lua_Reader reader; /* reader function */
59 | void *data; /* additional data */
60 | lua_State *L; /* Lua state (for reader) */
61 | };
62 |
63 |
64 | LUAI_FUNC int luaZ_fill (ZIO *z);
65 |
66 | #endif
67 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lfunc.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp $
3 | ** Auxiliary functions to manipulate prototypes and closures
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lfunc_h
8 | #define lfunc_h
9 |
10 |
11 | #include "lobject.h"
12 |
13 |
14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
15 | cast(int, sizeof(TValue)*((n)-1)))
16 |
17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
18 | cast(int, sizeof(TValue *)*((n)-1)))
19 |
20 |
21 | /* test whether thread is in 'twups' list */
22 | #define isintwups(L) (L->twups != L)
23 |
24 |
25 | /*
26 | ** maximum number of upvalues in a closure (both C and Lua). (Value
27 | ** must fit in a VM register.)
28 | */
29 | #define MAXUPVAL 255
30 |
31 |
32 | /*
33 | ** Upvalues for Lua closures
34 | */
35 | struct UpVal {
36 | TValue *v; /* points to stack or to its own value */
37 | lu_mem refcount; /* reference counter */
38 | union {
39 | struct { /* (when open) */
40 | UpVal *next; /* linked list */
41 | int touched; /* mark to avoid cycles with dead threads */
42 | } open;
43 | TValue value; /* the value (when closed) */
44 | } u;
45 | };
46 |
47 | #define upisopen(up) ((up)->v != &(up)->u.value)
48 |
49 |
50 | LUAI_FUNC Proto *luaF_newproto (lua_State *L);
51 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems);
52 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems);
53 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
54 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
55 | LUAI_FUNC void luaF_close (lua_State *L, StkId level);
56 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
57 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
58 | int pc);
59 |
60 |
61 | #endif
62 |
--------------------------------------------------------------------------------
/luajava/src/main/java/org/keplerproject/luajava/LuaException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: LuaException.java,v 1.6 2006/12/22 14:06:40 thiago Exp $
3 | * Copyright (C) 2003-2007 Kepler Project.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package org.keplerproject.luajava;
26 |
27 | /**
28 | * LuaJava exception
29 | *
30 | * @author Thiago Ponte
31 | */
32 | public class LuaException extends Exception {
33 | /**
34 | *
35 | */
36 | private static final long serialVersionUID = 1L;
37 |
38 | public LuaException(String str) {
39 | super(str);
40 | }
41 |
42 | /**
43 | * Will work only on Java 1.4 or later.
44 | * To work with Java 1.3, comment the first line and uncomment the second one.
45 | */
46 | public LuaException(Exception e) {
47 | super((e.getCause() != null) ? e.getCause() : e);
48 | //super(e.getMessage());
49 | }
50 | }
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/linit.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: linit.c,v 1.38 2015/01/05 13:48:33 roberto Exp $
3 | ** Initialization of libraries for lua.c and other clients
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #define linit_c
9 | #define LUA_LIB
10 |
11 | /*
12 | ** If you embed Lua in your program and need to open the standard
13 | ** libraries, call luaL_openlibs in your program. If you need a
14 | ** different set of libraries, copy this file to your project and edit
15 | ** it to suit your needs.
16 | **
17 | ** You can also *preload* libraries, so that a later 'require' can
18 | ** open the library, which is already linked to the application.
19 | ** For that, do the following code:
20 | **
21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
22 | ** lua_pushcfunction(L, luaopen_modname);
23 | ** lua_setfield(L, -2, modname);
24 | ** lua_pop(L, 1); // remove _PRELOAD table
25 | */
26 |
27 | #include "lprefix.h"
28 |
29 |
30 | #include
31 |
32 | #include "lua.h"
33 |
34 | #include "lualib.h"
35 | #include "lauxlib.h"
36 |
37 |
38 | /*
39 | ** these libs are loaded by lua.c and are readily available to any Lua
40 | ** program
41 | */
42 | static const luaL_Reg loadedlibs[] = {
43 | {"_G", luaopen_base},
44 | {LUA_LOADLIBNAME, luaopen_package},
45 | {LUA_COLIBNAME, luaopen_coroutine},
46 | {LUA_TABLIBNAME, luaopen_table},
47 | {LUA_IOLIBNAME, luaopen_io},
48 | {LUA_OSLIBNAME, luaopen_os},
49 | {LUA_STRLIBNAME, luaopen_string},
50 | {LUA_MATHLIBNAME, luaopen_math},
51 | {LUA_UTF8LIBNAME, luaopen_utf8},
52 | {LUA_DBLIBNAME, luaopen_debug},
53 | #if defined(LUA_COMPAT_BITLIB)
54 | {LUA_BITLIBNAME, luaopen_bit32},
55 | #endif
56 | {NULL, NULL}
57 | };
58 |
59 |
60 | LUALIB_API void luaL_openlibs (lua_State *L) {
61 | const luaL_Reg *lib;
62 | /* "require" functions from 'loadedlibs' and set results to global table */
63 | for (lib = loadedlibs; lib->func; lib++) {
64 | luaL_requiref(L, lib->name, lib->func, 1);
65 | lua_pop(L, 1); /* remove lib */
66 | }
67 | }
68 |
69 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/ltm.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ltm.h,v 2.22 2016/02/26 19:20:15 roberto Exp $
3 | ** Tag methods
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef ltm_h
8 | #define ltm_h
9 |
10 |
11 | #include "lobject.h"
12 |
13 |
14 | /*
15 | * WARNING: if you change the order of this enumeration,
16 | * grep "ORDER TM" and "ORDER OP"
17 | */
18 | typedef enum {
19 | TM_INDEX,
20 | TM_NEWINDEX,
21 | TM_GC,
22 | TM_MODE,
23 | TM_LEN,
24 | TM_EQ, /* last tag method with fast access */
25 | TM_ADD,
26 | TM_SUB,
27 | TM_MUL,
28 | TM_MOD,
29 | TM_POW,
30 | TM_DIV,
31 | TM_IDIV,
32 | TM_BAND,
33 | TM_BOR,
34 | TM_BXOR,
35 | TM_SHL,
36 | TM_SHR,
37 | TM_UNM,
38 | TM_BNOT,
39 | TM_LT,
40 | TM_LE,
41 | TM_CONCAT,
42 | TM_CALL,
43 | TM_N /* number of elements in the enum */
44 | } TMS;
45 |
46 |
47 |
48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \
49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
50 |
51 | #define fasttm(l,et,e) gfasttm(G(l), et, e)
52 |
53 | #define ttypename(x) luaT_typenames_[(x) + 1]
54 |
55 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS];
56 |
57 |
58 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
59 |
60 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
61 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
62 | TMS event);
63 | LUAI_FUNC void luaT_init (lua_State *L);
64 |
65 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
66 | const TValue *p2, TValue *p3, int hasres);
67 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
68 | StkId res, TMS event);
69 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
70 | StkId res, TMS event);
71 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
72 | const TValue *p2, TMS event);
73 |
74 |
75 |
76 | #endif
77 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/ltable.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ltable.h,v 2.21 2015/11/03 15:47:30 roberto Exp $
3 | ** Lua tables (hash)
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef ltable_h
8 | #define ltable_h
9 |
10 | #include "lobject.h"
11 |
12 |
13 | #define gnode(t,i) (&(t)->node[i])
14 | #define gval(n) (&(n)->i_val)
15 | #define gnext(n) ((n)->i_key.nk.next)
16 |
17 |
18 | /* 'const' to avoid wrong writings that can mess up field 'next' */
19 | #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk))
20 |
21 | /*
22 | ** writable version of 'gkey'; allows updates to individual fields,
23 | ** but not to the whole (which has incompatible type)
24 | */
25 | #define wgkey(n) (&(n)->i_key.nk)
26 |
27 | #define invalidateTMcache(t) ((t)->flags = 0)
28 |
29 |
30 | /* returns the key, given the value of a table entry */
31 | #define keyfromval(v) \
32 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))
33 |
34 |
35 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
36 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
37 | TValue *value);
38 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key);
39 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
40 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
41 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key);
42 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
43 | LUAI_FUNC Table *luaH_new (lua_State *L);
44 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
45 | unsigned int nhsize);
46 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize);
47 | LUAI_FUNC void luaH_free (lua_State *L, Table *t);
48 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
49 | LUAI_FUNC int luaH_getn (Table *t);
50 |
51 |
52 | #if defined(LUA_DEBUG)
53 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
54 | LUAI_FUNC int luaH_isdummy (Node *n);
55 | #endif
56 |
57 |
58 | #endif
59 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lctype.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $
3 | ** 'ctype' functions for Lua
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lctype_h
8 | #define lctype_h
9 |
10 | #include "lua.h"
11 |
12 |
13 | /*
14 | ** WARNING: the functions defined here do not necessarily correspond
15 | ** to the similar functions in the standard C ctype.h. They are
16 | ** optimized for the specific needs of Lua
17 | */
18 |
19 | #if !defined(LUA_USE_CTYPE)
20 |
21 | #if 'A' == 65 && '0' == 48
22 | /* ASCII case: can use its own tables; faster and fixed */
23 | #define LUA_USE_CTYPE 0
24 | #else
25 | /* must use standard C ctype */
26 | #define LUA_USE_CTYPE 1
27 | #endif
28 |
29 | #endif
30 |
31 |
32 | #if !LUA_USE_CTYPE /* { */
33 |
34 | #include
35 |
36 | #include "llimits.h"
37 |
38 |
39 | #define ALPHABIT 0
40 | #define DIGITBIT 1
41 | #define PRINTBIT 2
42 | #define SPACEBIT 3
43 | #define XDIGITBIT 4
44 |
45 |
46 | #define MASK(B) (1 << (B))
47 |
48 |
49 | /*
50 | ** add 1 to char to allow index -1 (EOZ)
51 | */
52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p))
53 |
54 | /*
55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_'
56 | */
57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT))
58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT))
60 | #define lisspace(c) testprop(c, MASK(SPACEBIT))
61 | #define lisprint(c) testprop(c, MASK(PRINTBIT))
62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
63 |
64 | /*
65 | ** this 'ltolower' only works for alphabetic characters
66 | */
67 | #define ltolower(c) ((c) | ('A' ^ 'a'))
68 |
69 |
70 | /* two more entries for 0 and -1 (EOZ) */
71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];
72 |
73 |
74 | #else /* }{ */
75 |
76 | /*
77 | ** use standard C ctypes
78 | */
79 |
80 | #include
81 |
82 |
83 | #define lislalpha(c) (isalpha(c) || (c) == '_')
84 | #define lislalnum(c) (isalnum(c) || (c) == '_')
85 | #define lisdigit(c) (isdigit(c))
86 | #define lisspace(c) (isspace(c))
87 | #define lisprint(c) (isprint(c))
88 | #define lisxdigit(c) (isxdigit(c))
89 |
90 | #define ltolower(c) (tolower(c))
91 |
92 | #endif /* } */
93 |
94 | #endif
95 |
96 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/ldo.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ldo.h,v 2.29 2015/12/21 13:02:14 roberto Exp $
3 | ** Stack and Call structure of Lua
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef ldo_h
8 | #define ldo_h
9 |
10 |
11 | #include "lobject.h"
12 | #include "lstate.h"
13 | #include "lzio.h"
14 |
15 |
16 | /*
17 | ** Macro to check stack size and grow stack if needed. Parameters
18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the
19 | ** stack across reallocations, doing the work only when needed.
20 | ** 'condmovestack' is used in heavy tests to force a stack reallocation
21 | ** at every check.
22 | */
23 | #define luaD_checkstackaux(L,n,pre,pos) \
24 | if (L->stack_last - L->top <= (n)) \
25 | { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); }
26 |
27 | /* In general, 'pre'/'pos' are empty (nothing to save) */
28 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0)
29 |
30 |
31 |
32 | #define savestack(L,p) ((char *)(p) - (char *)L->stack)
33 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
34 |
35 |
36 | /* type of protected functions, to be ran by 'runprotected' */
37 | typedef void (*Pfunc) (lua_State *L, void *ud);
38 |
39 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
40 | const char *mode);
41 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line);
42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
44 | LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
45 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
46 | ptrdiff_t oldtop, ptrdiff_t ef);
47 | LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult,
48 | int nres);
49 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
50 | LUAI_FUNC void luaD_growstack (lua_State *L, int n);
51 | LUAI_FUNC void luaD_shrinkstack (lua_State *L);
52 | LUAI_FUNC void luaD_inctop (lua_State *L);
53 |
54 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
55 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
56 |
57 | #endif
58 |
59 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/luajava/log.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by leon on 16/3/10.
3 | //
4 |
5 | #ifndef LUA_LOG_H
6 | #define LUA_LOG_H
7 |
8 | #include
9 |
10 | #define LOG_TAG "luajava"
11 |
12 | #define ANDROID_LOG(level, format, ...) \
13 | do { \
14 | __android_log_print(level, LOG_TAG,"(%s:%d) %s: " format "\n", strrchr(__FILE__, '/') + 1, __LINE__, __FUNCTION__ , ##__VA_ARGS__); \
15 | } while(0)
16 |
17 | #define LOG_INFO(format, ...) ANDROID_LOG(ANDROID_LOG_INFO, format, ##__VA_ARGS__)
18 | #define LOG_ERROR(format, ...) ANDROID_LOG(ANDROID_LOG_ERROR, format, ##__VA_ARGS__)
19 | #define LOG_WARN(format, ...) ANDROID_LOG(ANDROID_LOG_WARN, format, ##__VA_ARGS__)
20 |
21 | #define PRINTLNF(format, ...) ANDROID_LOG(ANDROID_LOG_DEBUG, format, ##__VA_ARGS__)
22 |
23 | #define PRINT_CHAR(char_value) PRINTLNF(#char_value": %c", char_value)
24 | #define PRINT_WCHAR(char_value) PRINTLNF(#char_value": %lc", char_value)
25 | #define PRINT_INT(int_value) PRINTLNF(#int_value": %d", int_value)
26 | #define PRINT_LONG(long_value) PRINTLNF(#long_value": %ld", long_value)
27 | #define PRINT_LLONG(long_value) PRINTLNF(#long_value": %lld", long_value)
28 | #define PRINT_HEX(int_value) PRINTLNF(#int_value": %#x", int_value)
29 | #define PRINT_BOOL(bool_value) PRINTLNF(#bool_value": %s", bool_value ? "true" : "false")
30 | #define PRINT_DOUBLE(double_value) PRINTLNF(#double_value": %g", double_value)
31 | #define PRINT_STRING(string_value) PRINTLNF(#string_value": %s", string_value)
32 |
33 | #define PRINT_ARRAY(format, array, length) \
34 | { int array_index; \
35 | for (array_index = 0; array_index < length; ++array_index) { \
36 | printf(format, array[array_index]); \
37 | };\
38 | printf("\n"); }
39 |
40 | #define PRINT_INT_ARRAY_LN(array, length) \
41 | { int i; \
42 | for (i = 0; i < length; ++i) { \
43 | PRINTLNF(#array"[%d]: %d", i, array[i]); \
44 | }}
45 |
46 | #define PRINT_INT_ARRAY(array, length) PRINT_ARRAY("%d, ", array, length)
47 | #define PRINT_CHAR_ARRAY(array, length) PRINT_ARRAY("%c, ", array, length)
48 | #define PRINT_DOUBLE_ARRAY(array, length) PRINT_ARRAY("%g, ", array, length)
49 |
50 | #define PRINT_IF_ERROR(format, ...) \
51 | if (errno != 0) { \
52 | fprintf(stderr, format, ##__VA_ARGS__); \
53 | fprintf(stderr, ": %s\n", strerror(errno)); \
54 | }
55 |
56 | #endif //LUA_LOG_H
57 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lctype.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $
3 | ** 'ctype' functions for Lua
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lctype_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include "lctype.h"
14 |
15 | #if !LUA_USE_CTYPE /* { */
16 |
17 | #include
18 |
19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
20 | 0x00, /* EOZ */
21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */
24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */
26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */
28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */
30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */
32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05,
33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */
34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */
38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */
40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */
42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */
44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */
46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */
48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */
50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */
52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 | };
54 |
55 | #endif /* } */
56 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/llex.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp $
3 | ** Lexical Analyzer
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef llex_h
8 | #define llex_h
9 |
10 | #include "lobject.h"
11 | #include "lzio.h"
12 |
13 |
14 | #define FIRST_RESERVED 257
15 |
16 |
17 | #if !defined(LUA_ENV)
18 | #define LUA_ENV "_ENV"
19 | #endif
20 |
21 |
22 | /*
23 | * WARNING: if you change the order of this enumeration,
24 | * grep "ORDER RESERVED"
25 | */
26 | enum RESERVED {
27 | /* terminal symbols denoted by reserved words */
28 | TK_AND = FIRST_RESERVED, TK_BREAK,
29 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
30 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
31 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
32 | /* other terminal symbols */
33 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
34 | TK_SHL, TK_SHR,
35 | TK_DBCOLON, TK_EOS,
36 | TK_FLT, TK_INT, TK_NAME, TK_STRING
37 | };
38 |
39 | /* number of reserved words */
40 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
41 |
42 |
43 | typedef union {
44 | lua_Number r;
45 | lua_Integer i;
46 | TString *ts;
47 | } SemInfo; /* semantics information */
48 |
49 |
50 | typedef struct Token {
51 | int token;
52 | SemInfo seminfo;
53 | } Token;
54 |
55 |
56 | /* state of the lexer plus state of the parser when shared by all
57 | functions */
58 | typedef struct LexState {
59 | int current; /* current character (charint) */
60 | int linenumber; /* input line counter */
61 | int lastline; /* line of last token 'consumed' */
62 | Token t; /* current token */
63 | Token lookahead; /* look ahead token */
64 | struct FuncState *fs; /* current function (parser) */
65 | struct lua_State *L;
66 | ZIO *z; /* input stream */
67 | Mbuffer *buff; /* buffer for tokens */
68 | Table *h; /* to avoid collection/reuse strings */
69 | struct Dyndata *dyd; /* dynamic structures used by the parser */
70 | TString *source; /* current source name */
71 | TString *envn; /* environment variable name */
72 | } LexState;
73 |
74 |
75 | LUAI_FUNC void luaX_init (lua_State *L);
76 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
77 | TString *source, int firstchar);
78 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
79 | LUAI_FUNC void luaX_next (LexState *ls);
80 | LUAI_FUNC int luaX_lookahead (LexState *ls);
81 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s);
82 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);
83 |
84 |
85 | #endif
86 |
--------------------------------------------------------------------------------
/luajava/src/main/java/org/keplerproject/luajava/CPtr.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: CPtr.java,v 1.4 2006/12/22 14:06:40 thiago Exp $
3 | * Copyright (C) 2003-2007 Kepler Project.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package org.keplerproject.luajava;
26 |
27 | /**
28 | * An abstraction for a C pointer data type. A CPtr instance represents, on
29 | * the Java side, a C pointer. The C pointer could be any type of C
30 | * pointer.
31 | */
32 | public class CPtr {
33 |
34 | /**
35 | * Compares this CPtr to the specified object.
36 | *
37 | * @param other a CPtr
38 | * @return true if the class of this CPtr object and the
39 | * class of other are exactly equal, and the C
40 | * pointers being pointed to by these objects are also
41 | * equal. Returns false otherwise.
42 | */
43 | public boolean equals(Object other) {
44 | if (other == null)
45 | return false;
46 | if (other == this)
47 | return true;
48 | if (CPtr.class != other.getClass())
49 | return false;
50 | return peer == ((CPtr) other).peer;
51 | }
52 |
53 |
54 | /* Pointer value of the real C pointer. Use long to be 64-bit safe. */
55 | private long peer;
56 |
57 | /**
58 | * Gets the value of the C pointer abstraction
59 | *
60 | * @return long
61 | */
62 | protected long getPeer() {
63 | return peer;
64 | }
65 |
66 | /* No-args constructor. */
67 | CPtr() {
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lmem.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $
3 | ** Interface to Memory Manager
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lmem_h
8 | #define lmem_h
9 |
10 |
11 | #include
12 |
13 | #include "llimits.h"
14 | #include "lua.h"
15 |
16 |
17 | /*
18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where
19 | ** each element has size 'e'. In case of arithmetic overflow of the
20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because
21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e).
22 | **
23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof'
24 | ** comparison avoids a runtime comparison when overflow cannot occur.
25 | ** The compiler should be able to optimize the real test by itself, but
26 | ** when it does it, it may give a warning about "comparison is always
27 | ** false due to limited range of data type"; the +1 tricks the compiler,
28 | ** avoiding this warning but also this optimization.)
29 | */
30 | #define luaM_reallocv(L,b,on,n,e) \
31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \
32 | ? luaM_toobig(L) : cast_void(0)) , \
33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
34 |
35 | /*
36 | ** Arrays of chars do not need any test
37 | */
38 | #define luaM_reallocvchar(L,b,on,n) \
39 | cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
40 |
41 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
42 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
43 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0)
44 |
45 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s))
46 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
47 | #define luaM_newvector(L,n,t) \
48 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
49 |
50 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s))
51 |
52 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \
53 | if ((nelems)+1 > (size)) \
54 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
55 |
56 | #define luaM_reallocvector(L, v,oldn,n,t) \
57 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
58 |
59 | LUAI_FUNC l_noret luaM_toobig (lua_State *L);
60 |
61 | /* not to be called directly */
62 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
63 | size_t size);
64 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
65 | size_t size_elem, int limit,
66 | const char *what);
67 |
68 | #endif
69 |
70 |
--------------------------------------------------------------------------------
/luajava/src/main/java/org/keplerproject/luajava/LuaInvocationHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: LuaInvocationHandler.java,v 1.4 2006/12/22 14:06:40 thiago Exp $
3 | * Copyright (C) 2003-2007 Kepler Project.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package org.keplerproject.luajava;
26 |
27 | import java.lang.reflect.InvocationHandler;
28 | import java.lang.reflect.Method;
29 |
30 | /**
31 | * Class that implements the InvocationHandler interface.
32 | * This class is used in the LuaJava's proxy system.
33 | * When a proxy object is accessed, the method invoked is
34 | * called from Lua
35 | *
36 | * @author Rizzato
37 | * @author Thiago Ponte
38 | */
39 | public class LuaInvocationHandler implements InvocationHandler {
40 | private LuaObject obj;
41 |
42 |
43 | public LuaInvocationHandler(LuaObject obj) {
44 | this.obj = obj;
45 | }
46 |
47 | /**
48 | * Function called when a proxy object function is invoked.
49 | */
50 | public Object invoke(Object proxy, Method method, Object[] args) throws LuaException {
51 | synchronized (obj.L) {
52 | String methodName = method.getName();
53 | LuaObject func = obj.getField(methodName);
54 |
55 | if (func.isNil()) {
56 | return null;
57 | }
58 |
59 | Class retType = method.getReturnType();
60 | Object ret;
61 |
62 | // Checks if returned type is void. if it is returns null.
63 | if (retType.equals(Void.class) || retType.equals(void.class)) {
64 | func.call(args, 0);
65 | ret = null;
66 | } else {
67 | ret = func.call(args, 1)[0];
68 | if (ret != null && ret instanceof Double) {
69 | ret = LuaState.convertLuaNumber((Double) ret, retType);
70 | }
71 | }
72 |
73 | return ret;
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lmem.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $
3 | ** Interface to Memory Manager
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lmem_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "ldebug.h"
18 | #include "ldo.h"
19 | #include "lgc.h"
20 | #include "lmem.h"
21 | #include "lobject.h"
22 | #include "lstate.h"
23 |
24 |
25 |
26 | /*
27 | ** About the realloc function:
28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
29 | ** ('osize' is the old size, 'nsize' is the new size)
30 | **
31 | ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
32 | ** matter 'x').
33 | **
34 | ** * frealloc(ud, p, x, 0) frees the block 'p'
35 | ** (in this specific case, frealloc must return NULL);
36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing
37 | ** (which is equivalent to free(NULL) in ISO C)
38 | **
39 | ** frealloc returns NULL if it cannot create or reallocate the area
40 | ** (any reallocation to an equal or smaller size cannot fail!)
41 | */
42 |
43 |
44 |
45 | #define MINSIZEARRAY 4
46 |
47 |
48 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
49 | int limit, const char *what) {
50 | void *newblock;
51 | int newsize;
52 | if (*size >= limit/2) { /* cannot double it? */
53 | if (*size >= limit) /* cannot grow even a little? */
54 | luaG_runerror(L, "too many %s (limit is %d)", what, limit);
55 | newsize = limit; /* still have at least one free place */
56 | }
57 | else {
58 | newsize = (*size)*2;
59 | if (newsize < MINSIZEARRAY)
60 | newsize = MINSIZEARRAY; /* minimum size */
61 | }
62 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
63 | *size = newsize; /* update only when everything else is OK */
64 | return newblock;
65 | }
66 |
67 |
68 | l_noret luaM_toobig (lua_State *L) {
69 | luaG_runerror(L, "memory allocation error: block too big");
70 | }
71 |
72 |
73 |
74 | /*
75 | ** generic allocation routine.
76 | */
77 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
78 | void *newblock;
79 | global_State *g = G(L);
80 | size_t realosize = (block) ? osize : 0;
81 | lua_assert((realosize == 0) == (block == NULL));
82 | #if defined(HARDMEMTESTS)
83 | if (nsize > realosize && g->gcrunning)
84 | luaC_fullgc(L, 1); /* force a GC whenever possible */
85 | #endif
86 | newblock = (*g->frealloc)(g->ud, block, osize, nsize);
87 | if (newblock == NULL && nsize > 0) {
88 | lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
89 | if (g->version) { /* is state fully built? */
90 | luaC_fullgc(L, 1); /* try to free some memory... */
91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
92 | }
93 | if (newblock == NULL)
94 | luaD_throw(L, LUA_ERRMEM);
95 | }
96 | lua_assert((nsize == 0) == (newblock == NULL));
97 | g->GCdebt = (g->GCdebt + nsize) - realosize;
98 | return newblock;
99 | }
100 |
101 |
--------------------------------------------------------------------------------
/luajava/src/main/java/org/keplerproject/luajava/Console.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: Console.java,v 1.7 2006/12/22 14:06:40 thiago Exp $
3 | * Copyright (C) 2003-2007 Kepler Project.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package org.keplerproject.luajava;
26 |
27 | import java.io.BufferedReader;
28 | import java.io.InputStreamReader;
29 |
30 | /**
31 | * Simple LuaJava console.
32 | * This is also an example on how to use the Java side of LuaJava and how to startup
33 | * a LuaJava application.
34 | *
35 | * @author Thiago Ponte
36 | */
37 | public class Console {
38 |
39 | /**
40 | * Creates a console for user interaction.
41 | *
42 | * @param args names of the lua files to be executed
43 | */
44 | public static void main(String[] args) {
45 | try {
46 | LuaState L = LuaStateFactory.newLuaState();
47 | L.openLibs();
48 |
49 | if (args.length > 0) {
50 | for (int i = 0; i < args.length; i++) {
51 | int res = L.LloadFile(args[i]);
52 | if (res == 0) {
53 | res = L.pcall(0, 0, 0);
54 | }
55 | if (res != 0) {
56 | throw new LuaException("Error on file: " + args[i] + ". " + L.toString(-1));
57 | }
58 | }
59 |
60 | return;
61 | }
62 |
63 | System.out.println("API Lua Java - console mode.");
64 |
65 | BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
66 |
67 | String line;
68 |
69 | System.out.print("> ");
70 | while ((line = inp.readLine()) != null && !line.equals("exit")) {
71 | int ret = L.LloadBuffer(line.getBytes(), "from console");
72 | if (ret == 0) {
73 | ret = L.pcall(0, 0, 0);
74 | }
75 | if (ret != 0) {
76 | System.err.println("Error on line: " + line);
77 | System.err.println(L.toString(-1));
78 | }
79 | System.out.print("> ");
80 | }
81 |
82 | L.close();
83 | } catch (Exception e) {
84 | e.printStackTrace();
85 | }
86 |
87 | }
88 | }
--------------------------------------------------------------------------------
/luajava/src/main/java/org/keplerproject/luajava/JavaFunction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: JavaFunction.java,v 1.6 2006/12/22 14:06:40 thiago Exp $
3 | * Copyright (C) 2003-2007 Kepler Project.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package org.keplerproject.luajava;
26 |
27 | /**
28 | * JavaFunction is a class that can be used to implement a Lua function in Java.
29 | * JavaFunction is an abstract class, so in order to use it you must extend this
30 | * class and implement the execute method. This execute
31 | * method is the method that will be called when you call the function from Lua.
32 | * To register the JavaFunction in Lua use the method register(String name).
33 | */
34 | public abstract class JavaFunction {
35 |
36 | /**
37 | * This is the state in which this function will exist.
38 | */
39 | protected LuaState L;
40 |
41 | /**
42 | * This method is called from Lua. Any parameters can be taken with
43 | * getParam. A reference to the JavaFunctionWrapper itself is
44 | * always the first parameter received. Values passed back as results
45 | * of the function must be pushed onto the stack.
46 | *
47 | * @return The number of values pushed onto the stack.
48 | */
49 | public abstract int execute() throws LuaException;
50 |
51 | /**
52 | * Constructor that receives a LuaState.
53 | *
54 | * @param L LuaState object associated with this JavaFunction object
55 | */
56 | public JavaFunction(LuaState L) {
57 | this.L = L;
58 | }
59 |
60 | /**
61 | * Returns a parameter received from Lua. Parameters are numbered from 1.
62 | * A reference to the JavaFunction itself is always the first parameter
63 | * received (the same as this).
64 | *
65 | * @param idx Index of the parameter.
66 | * @return Reference to parameter.
67 | * @see LuaObject
68 | */
69 | public LuaObject getParam(int idx) {
70 | return L.getLuaObject(idx);
71 | }
72 |
73 | /**
74 | * Register a JavaFunction with a given name. This method registers in a
75 | * global variable the JavaFunction specified.
76 | *
77 | * @param name name of the function.
78 | */
79 | public void register(String name) throws LuaException {
80 | synchronized (L) {
81 | L.pushJavaFunction(this);
82 | L.setGlobal(name);
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lcode.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp $
3 | ** Code generator for Lua
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lcode_h
8 | #define lcode_h
9 |
10 | #include "llex.h"
11 | #include "lobject.h"
12 | #include "lopcodes.h"
13 | #include "lparser.h"
14 |
15 |
16 | /*
17 | ** Marks the end of a patch list. It is an invalid value both as an absolute
18 | ** address, and as a list link (would link an element to itself).
19 | */
20 | #define NO_JUMP (-1)
21 |
22 |
23 | /*
24 | ** grep "ORDER OPR" if you change these enums (ORDER OP)
25 | */
26 | typedef enum BinOpr {
27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
28 | OPR_DIV,
29 | OPR_IDIV,
30 | OPR_BAND, OPR_BOR, OPR_BXOR,
31 | OPR_SHL, OPR_SHR,
32 | OPR_CONCAT,
33 | OPR_EQ, OPR_LT, OPR_LE,
34 | OPR_NE, OPR_GT, OPR_GE,
35 | OPR_AND, OPR_OR,
36 | OPR_NOBINOPR
37 | } BinOpr;
38 |
39 |
40 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
41 |
42 |
43 | /* get (pointer to) instruction of given 'expdesc' */
44 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info])
45 |
46 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
47 |
48 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET)
49 |
50 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
51 |
52 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
53 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
54 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k);
55 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
56 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
57 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
58 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
59 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s);
60 | LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n);
61 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
62 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
63 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e);
64 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
65 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
66 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
67 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
68 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
69 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
70 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e);
71 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
72 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
73 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
74 | LUAI_FUNC int luaK_jump (FuncState *fs);
75 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
76 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
77 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
78 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level);
79 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
80 | LUAI_FUNC int luaK_getlabel (FuncState *fs);
81 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
82 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
83 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
84 | expdesc *v2, int line);
85 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
86 |
87 |
88 | #endif
89 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lopcodes.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp $
3 | ** Opcodes for Lua virtual machine
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lopcodes_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lopcodes.h"
16 |
17 |
18 | /* ORDER OP */
19 |
20 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
21 | "MOVE",
22 | "LOADK",
23 | "LOADKX",
24 | "LOADBOOL",
25 | "LOADNIL",
26 | "GETUPVAL",
27 | "GETTABUP",
28 | "GETTABLE",
29 | "SETTABUP",
30 | "SETUPVAL",
31 | "SETTABLE",
32 | "NEWTABLE",
33 | "SELF",
34 | "ADD",
35 | "SUB",
36 | "MUL",
37 | "MOD",
38 | "POW",
39 | "DIV",
40 | "IDIV",
41 | "BAND",
42 | "BOR",
43 | "BXOR",
44 | "SHL",
45 | "SHR",
46 | "UNM",
47 | "BNOT",
48 | "NOT",
49 | "LEN",
50 | "CONCAT",
51 | "JMP",
52 | "EQ",
53 | "LT",
54 | "LE",
55 | "TEST",
56 | "TESTSET",
57 | "CALL",
58 | "TAILCALL",
59 | "RETURN",
60 | "FORLOOP",
61 | "FORPREP",
62 | "TFORCALL",
63 | "TFORLOOP",
64 | "SETLIST",
65 | "CLOSURE",
66 | "VARARG",
67 | "EXTRAARG",
68 | NULL
69 | };
70 |
71 |
72 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m))
73 |
74 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
75 | /* T A B C mode opcode */
76 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */
77 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */
78 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */
79 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
80 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */
81 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
82 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */
83 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
84 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */
85 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */
86 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */
87 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */
88 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */
89 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */
90 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */
91 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */
92 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */
93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */
94 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */
95 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */
96 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */
97 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */
98 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */
99 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */
100 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */
101 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
102 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */
103 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */
104 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */
105 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */
106 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */
107 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
108 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */
109 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */
110 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */
111 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */
112 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */
113 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */
114 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */
115 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */
116 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */
117 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */
118 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */
119 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */
120 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */
121 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */
122 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */
123 | };
124 |
125 |
--------------------------------------------------------------------------------
/luajava/src/main/jni/lua/lvm.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lvm.h,v 2.40 2016/01/05 16:07:21 roberto Exp $
3 | ** Lua virtual machine
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lvm_h
8 | #define lvm_h
9 |
10 |
11 | #include "ldo.h"
12 | #include "lobject.h"
13 | #include "ltm.h"
14 |
15 |
16 | #if !defined(LUA_NOCVTN2S)
17 | #define cvt2str(o) ttisnumber(o)
18 | #else
19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */
20 | #endif
21 |
22 |
23 | #if !defined(LUA_NOCVTS2N)
24 | #define cvt2num(o) ttisstring(o)
25 | #else
26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */
27 | #endif
28 |
29 |
30 | /*
31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers
32 | ** by flooring them (instead of raising an error if they are not
33 | ** integral values)
34 | */
35 | #if !defined(LUA_FLOORN2I)
36 | #define LUA_FLOORN2I 0
37 | #endif
38 |
39 |
40 | #define tonumber(o,n) \
41 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
42 |
43 | #define tointeger(o,i) \
44 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I))
45 |
46 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
47 |
48 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
49 |
50 |
51 | /*
52 | ** fast track for 'gettable': if 't' is a table and 't[k]' is not nil,
53 | ** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise,
54 | ** return 0 (meaning it will have to check metamethod) with 'slot'
55 | ** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise).
56 | ** 'f' is the raw get function to use.
57 | */
58 | #define luaV_fastget(L,t,k,slot,f) \
59 | (!ttistable(t) \
60 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
61 | : (slot = f(hvalue(t), k), /* else, do raw access */ \
62 | !ttisnil(slot))) /* result not nil? */
63 |
64 | /*
65 | ** standard implementation for 'gettable'
66 | */
67 | #define luaV_gettable(L,t,k,v) { const TValue *slot; \
68 | if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \
69 | else luaV_finishget(L,t,k,v,slot); }
70 |
71 |
72 | /*
73 | ** Fast track for set table. If 't' is a table and 't[k]' is not nil,
74 | ** call GC barrier, do a raw 't[k]=v', and return true; otherwise,
75 | ** return false with 'slot' equal to NULL (if 't' is not a table) or
76 | ** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro
77 | ** returns true, there is no need to 'invalidateTMcache', because the
78 | ** call is not creating a new entry.
79 | */
80 | #define luaV_fastset(L,t,k,slot,f,v) \
81 | (!ttistable(t) \
82 | ? (slot = NULL, 0) \
83 | : (slot = f(hvalue(t), k), \
84 | ttisnil(slot) ? 0 \
85 | : (luaC_barrierback(L, hvalue(t), v), \
86 | setobj2t(L, cast(TValue *,slot), v), \
87 | 1)))
88 |
89 |
90 | #define luaV_settable(L,t,k,v) { const TValue *slot; \
91 | if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
92 | luaV_finishset(L,t,k,v,slot); }
93 |
94 |
95 |
96 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
97 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
98 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
99 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
100 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
101 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
102 | StkId val, const TValue *slot);
103 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
104 | StkId val, const TValue *slot);
105 | LUAI_FUNC void luaV_finishOp (lua_State *L);
106 | LUAI_FUNC void luaV_execute (lua_State *L);
107 | LUAI_FUNC void luaV_concat (lua_State *L, int total);
108 | LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y);
109 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
110 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
111 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
112 |
113 | #endif
114 |
--------------------------------------------------------------------------------
/luajava/src/main/java/org/keplerproject/luajava/LuaStateFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: LuaStateFactory.java,v 1.4 2006/12/22 14:06:40 thiago Exp $
3 | * Copyright (C) 2003-2007 Kepler Project.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package org.keplerproject.luajava;
26 |
27 | import java.util.ArrayList;
28 | import java.util.List;
29 |
30 | /**
31 | * This class is responsible for instantiating new LuaStates.
32 | * When a new LuaState is instantiated it is put into a List
33 | * and an index is returned. This index is registred in Lua
34 | * and it is used to find the right LuaState when lua calls
35 | * a Java Function.
36 | *
37 | * @author Thiago Ponte
38 | */
39 | public final class LuaStateFactory {
40 |
41 | private final static String LUAJAVA_LIB = "luajava";
42 | private final static boolean isLuaLibLoaded;
43 |
44 | /**
45 | * Opens the library containing the luajava API
46 | */
47 | static {
48 | boolean loaded = false;
49 | try {
50 | System.loadLibrary(LUAJAVA_LIB);
51 | loaded = true;
52 | } catch (UnsatisfiedLinkError e) {
53 | e.printStackTrace();
54 | }
55 | isLuaLibLoaded = loaded;
56 | }
57 |
58 | /**
59 | * Array with all luaState's instances
60 | */
61 | private static final List