Move sources into maven modules
This commit is contained in:
34
luaj-jme/pom.xml
Normal file
34
luaj-jme/pom.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.luaj</groupId>
|
||||
<artifactId>luaj-parent</artifactId>
|
||||
<version>3.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>luaj-jme</artifactId>
|
||||
|
||||
<name>luaj-jme</name>
|
||||
<description>LuaJ for Java ME</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.luaj</groupId>
|
||||
<artifactId>luaj-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.bcel</groupId>
|
||||
<artifactId>bcel</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.mcpat.apistubs</groupId>
|
||||
<artifactId>cldc-1.1-stub</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
230
luaj-jme/src/main/java/org/luaj/vm2/lib/jme/JmeIoLib.java
Normal file
230
luaj-jme/src/main/java/org/luaj/vm2/lib/jme/JmeIoLib.java
Normal file
@@ -0,0 +1,230 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2009-2011 Luaj.org. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.lib.jme;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import javax.microedition.io.Connector;
|
||||
import javax.microedition.io.StreamConnection;
|
||||
|
||||
import org.luaj.vm2.Globals;
|
||||
import org.luaj.vm2.LuaString;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.IoLib;
|
||||
import org.luaj.vm2.lib.LibFunction;
|
||||
|
||||
/**
|
||||
* Subclass of {@link IoLib} and therefore {@link LibFunction} which implements the lua standard {@code io}
|
||||
* library for the JSE platform.
|
||||
* <p>
|
||||
* The implementation of the is based on CLDC 1.0 and StreamConnection.
|
||||
* However, seek is not supported.
|
||||
* <p>
|
||||
* Typically, this library is included as part of a call to
|
||||
* {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
||||
* <pre> {@code
|
||||
* Globals globals = JmePlatform.standardGlobals();
|
||||
* globals.get("io").get("write").call(LuaValue.valueOf("hello, world\n"));
|
||||
* } </pre>
|
||||
* <p>
|
||||
* For special cases where the smallest possible footprint is desired,
|
||||
* a minimal set of libraries could be loaded
|
||||
* directly via {@link Globals#load(LuaValue)} using code such as:
|
||||
* <pre> {@code
|
||||
* Globals globals = new Globals();
|
||||
* globals.load(new JmeBaseLib());
|
||||
* globals.load(new PackageLib());
|
||||
* globals.load(new JmeIoLib());
|
||||
* globals.get("io").get("write").call(LuaValue.valueOf("hello, world\n"));
|
||||
* } </pre>
|
||||
* <p>However, other libraries such as <em>MathLib</em> are not loaded in this case.
|
||||
* <p>
|
||||
* This has been implemented to match as closely as possible the behavior in the corresponding library in C.
|
||||
* @see LibFunction
|
||||
* @see org.luaj.vm2.lib.jse.JsePlatform
|
||||
* @see org.luaj.vm2.lib.jme.JmePlatform
|
||||
* @see IoLib
|
||||
* @see org.luaj.vm2.lib.jse.JseIoLib
|
||||
* @see <a href="http://www.lua.org/manual/5.2/manual.html#6.8">Lua 5.2 I/O Lib Reference</a>
|
||||
*/
|
||||
public class JmeIoLib extends IoLib {
|
||||
|
||||
protected File wrapStdin() throws IOException {
|
||||
return new FileImpl(globals.STDIN);
|
||||
}
|
||||
|
||||
protected File wrapStdout() throws IOException {
|
||||
return new FileImpl(globals.STDOUT);
|
||||
}
|
||||
|
||||
protected File wrapStderr() throws IOException {
|
||||
return new FileImpl(globals.STDERR);
|
||||
}
|
||||
|
||||
protected File openFile( String filename, boolean readMode, boolean appendMode, boolean updateMode, boolean binaryMode ) throws IOException {
|
||||
String url = "file:///" + filename;
|
||||
int mode = readMode? Connector.READ: Connector.READ_WRITE;
|
||||
StreamConnection conn = (StreamConnection) Connector.open( url, mode );
|
||||
File f = readMode?
|
||||
new FileImpl(conn, conn.openInputStream(), null):
|
||||
new FileImpl(conn, conn.openInputStream(), conn.openOutputStream());
|
||||
/*
|
||||
if ( appendMode ) {
|
||||
f.seek("end",0);
|
||||
} else {
|
||||
if ( ! readMode )
|
||||
conn.truncate(0);
|
||||
}
|
||||
*/
|
||||
return f;
|
||||
}
|
||||
|
||||
private static void notimplemented() throws IOException {
|
||||
throw new IOException("not implemented");
|
||||
}
|
||||
|
||||
protected File openProgram(String prog, String mode) throws IOException {
|
||||
notimplemented();
|
||||
return null;
|
||||
}
|
||||
|
||||
protected File tmpFile() throws IOException {
|
||||
notimplemented();
|
||||
return null;
|
||||
}
|
||||
|
||||
private final class FileImpl extends File {
|
||||
private final StreamConnection conn;
|
||||
private final InputStream is;
|
||||
private final OutputStream os;
|
||||
private boolean closed = false;
|
||||
private boolean nobuffer = false;
|
||||
private int lookahead = -1;
|
||||
private FileImpl( StreamConnection conn, InputStream is, OutputStream os ) {
|
||||
this.conn = conn;
|
||||
this.is = is;
|
||||
this.os = os;
|
||||
}
|
||||
private FileImpl( InputStream i ) {
|
||||
this( null, i, null );
|
||||
}
|
||||
private FileImpl( OutputStream o ) {
|
||||
this( null, null, o );
|
||||
}
|
||||
public String tojstring() {
|
||||
return "file ("+this.hashCode()+")";
|
||||
}
|
||||
public boolean isstdfile() {
|
||||
return conn == null;
|
||||
}
|
||||
public void close() throws IOException {
|
||||
closed = true;
|
||||
if ( conn != null ) {
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
public void flush() throws IOException {
|
||||
if ( os != null )
|
||||
os.flush();
|
||||
}
|
||||
public void write(LuaString s) throws IOException {
|
||||
if ( os != null )
|
||||
os.write( s.m_bytes, s.m_offset, s.m_length );
|
||||
else
|
||||
notimplemented();
|
||||
if ( nobuffer )
|
||||
flush();
|
||||
}
|
||||
public boolean isclosed() {
|
||||
return closed;
|
||||
}
|
||||
public int seek(String option, int pos) throws IOException {
|
||||
/*
|
||||
if ( conn != null ) {
|
||||
if ( "set".equals(option) ) {
|
||||
conn.seek(pos);
|
||||
return (int) conn.getFilePointer();
|
||||
} else if ( "end".equals(option) ) {
|
||||
conn.seek(conn.length()+1+pos);
|
||||
return (int) conn.length()+1;
|
||||
} else {
|
||||
conn.seek(conn.getFilePointer()+pos);
|
||||
return (int) conn.getFilePointer();
|
||||
}
|
||||
}
|
||||
*/
|
||||
notimplemented();
|
||||
return 0;
|
||||
}
|
||||
public void setvbuf(String mode, int size) {
|
||||
nobuffer = "no".equals(mode);
|
||||
}
|
||||
|
||||
// get length remaining to read
|
||||
public int remaining() throws IOException {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// peek ahead one character
|
||||
public int peek() throws IOException {
|
||||
if ( lookahead < 0 )
|
||||
lookahead = is.read();
|
||||
return lookahead;
|
||||
}
|
||||
|
||||
// return char if read, -1 if eof, throw IOException on other exception
|
||||
public int read() throws IOException {
|
||||
if ( lookahead >= 0 ) {
|
||||
int c = lookahead;
|
||||
lookahead = -1;
|
||||
return c;
|
||||
}
|
||||
if ( is != null )
|
||||
return is.read();
|
||||
notimplemented();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return number of bytes read if positive, -1 if eof, throws IOException
|
||||
public int read(byte[] bytes, int offset, int length) throws IOException {
|
||||
int n,i=0;
|
||||
if (is!=null) {
|
||||
if ( length > 0 && lookahead >= 0 ) {
|
||||
bytes[offset] = (byte) lookahead;
|
||||
lookahead = -1;
|
||||
i += 1;
|
||||
}
|
||||
for ( ; i<length; ) {
|
||||
n = is.read(bytes, offset+i, length-i);
|
||||
if ( n < 0 )
|
||||
return ( i > 0 ? i : -1 );
|
||||
i += n;
|
||||
}
|
||||
} else {
|
||||
notimplemented();
|
||||
}
|
||||
return length;
|
||||
}
|
||||
}
|
||||
}
|
||||
135
luaj-jme/src/main/java/org/luaj/vm2/lib/jme/JmePlatform.java
Normal file
135
luaj-jme/src/main/java/org/luaj/vm2/lib/jme/JmePlatform.java
Normal file
@@ -0,0 +1,135 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2009 Luaj.org. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.lib.jme;
|
||||
|
||||
import org.luaj.vm2.Globals;
|
||||
import org.luaj.vm2.LoadState;
|
||||
import org.luaj.vm2.LuaThread;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.compiler.LuaC;
|
||||
import org.luaj.vm2.lib.BaseLib;
|
||||
import org.luaj.vm2.lib.Bit32Lib;
|
||||
import org.luaj.vm2.lib.CoroutineLib;
|
||||
import org.luaj.vm2.lib.DebugLib;
|
||||
import org.luaj.vm2.lib.MathLib;
|
||||
import org.luaj.vm2.lib.OsLib;
|
||||
import org.luaj.vm2.lib.PackageLib;
|
||||
import org.luaj.vm2.lib.ResourceFinder;
|
||||
import org.luaj.vm2.lib.StringLib;
|
||||
import org.luaj.vm2.lib.TableLib;
|
||||
|
||||
/** The {@link org.luaj.vm2.lib.jme.JmePlatform} class is a convenience class to standardize
|
||||
* how globals tables are initialized for the JME platform.
|
||||
* <p>
|
||||
* The JME platform, being limited, cannot implement all libraries in all aspects. The main limitations are
|
||||
* <ul>
|
||||
* <li>Some math functions are not implemented, see {@link MathLib} for details</li>
|
||||
* <li>Scripts are loaded via Class.getResourceAsStream(), see {@link BaseLib} for details</li>
|
||||
* <li>OS functions execute(), remove(), rename(), and tmpname() vary, see {@link OsLib} for details</li>
|
||||
* <li>I/O seek is not implemented, see {@link org.luaj.vm2.lib.jme.JmeIoLib} for details</li>
|
||||
* <li>luajava is not available, see {@link org.luaj.vm2.lib.jse.LuajavaLib} for details</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* It is used to allocate either a set of standard globals using
|
||||
* {@link #standardGlobals()} or debug globals using {@link #debugGlobals()}
|
||||
* <p>
|
||||
* A simple example of initializing globals and using them from Java is:
|
||||
* <pre> {@code
|
||||
* Globals global = JmePlatform.standardGlobals();
|
||||
* global.get("print").call(LuaValue.valueOf("hello, world"));
|
||||
* } </pre>
|
||||
* <p>
|
||||
* Once globals are created, a simple way to load and run a script is:
|
||||
* <pre> {@code
|
||||
* LoadState.load( getClass().getResourceAsStream("main.lua"), "main.lua", globals ).call();
|
||||
* } </pre>
|
||||
* <p>
|
||||
* although {@code require} could also be used:
|
||||
* <pre> {@code
|
||||
* globals.get("require").call(LuaValue.valueOf("main"));
|
||||
* } </pre>
|
||||
* For this to succeed, the file "main.lua" must be a resource in the class path.
|
||||
* See {@link BaseLib} for details on finding scripts using {@link ResourceFinder}.
|
||||
* <p>
|
||||
* The standard globals will contain all standard libraries in their JME flavors:
|
||||
* <ul>
|
||||
* <li>{@link Globals}</li>
|
||||
* <li>{@link BaseLib}</li>
|
||||
* <li>{@link PackageLib}</li>
|
||||
* <li>{@link Bit32Lib}</li>
|
||||
* <li>{@link TableLib}</li>
|
||||
* <li>{@link StringLib}</li>
|
||||
* <li>{@link CoroutineLib}</li>
|
||||
* <li>{@link MathLib}</li>
|
||||
* <li>{@link org.luaj.vm2.lib.jme.JmeIoLib}</li>
|
||||
* <li>{@link OsLib}</li>
|
||||
* </ul>
|
||||
* In addition, the {@link LuaC} compiler is installed so lua files may be loaded in their source form.
|
||||
* <p>
|
||||
* The debug globals are simply the standard globals plus the {@code debug} library {@link DebugLib}.
|
||||
* <p>
|
||||
* <p>
|
||||
* The class ensures that initialization is done in the correct order.
|
||||
*
|
||||
* @see Globals
|
||||
* @see org.luaj.vm2.lib.jse.JsePlatform
|
||||
*/
|
||||
public class JmePlatform {
|
||||
|
||||
/**
|
||||
* Create a standard set of globals for JME including all the libraries.
|
||||
*
|
||||
* @return Table of globals initialized with the standard JME libraries
|
||||
* @see #debugGlobals()
|
||||
* @see org.luaj.vm2.lib.jse.JsePlatform
|
||||
* @see org.luaj.vm2.lib.jme.JmePlatform
|
||||
*/
|
||||
public static Globals standardGlobals() {
|
||||
Globals globals = new Globals();
|
||||
globals.load(new BaseLib());
|
||||
globals.load(new PackageLib());
|
||||
globals.load(new Bit32Lib());
|
||||
globals.load(new OsLib());
|
||||
globals.load(new MathLib());
|
||||
globals.load(new TableLib());
|
||||
globals.load(new StringLib());
|
||||
globals.load(new CoroutineLib());
|
||||
globals.load(new JmeIoLib());
|
||||
LoadState.install(globals);
|
||||
LuaC.install(globals);
|
||||
return globals;
|
||||
}
|
||||
|
||||
/** Create standard globals including the {@link DebugLib} library.
|
||||
*
|
||||
* @return Table of globals initialized with the standard JSE and debug libraries
|
||||
* @see #standardGlobals()
|
||||
* @see org.luaj.vm2.lib.jse.JsePlatform
|
||||
* @see org.luaj.vm2.lib.jme.JmePlatform
|
||||
* @see DebugLib
|
||||
*/
|
||||
public static Globals debugGlobals() {
|
||||
Globals globals = standardGlobals();
|
||||
globals.load(new DebugLib());
|
||||
return globals;
|
||||
}
|
||||
}
|
||||
0
luaj-jme/src/main/resources/.keep
Normal file
0
luaj-jme/src/main/resources/.keep
Normal file
0
luaj-jme/src/test/java/.keep
Normal file
0
luaj-jme/src/test/java/.keep
Normal file
0
luaj-jme/src/test/resources/.keep
Normal file
0
luaj-jme/src/test/resources/.keep
Normal file
Reference in New Issue
Block a user