org.luaj.vm
Class Platform

java.lang.Object
  extended by org.luaj.vm.Platform

public abstract class Platform
extends java.lang.Object

Singleton to manage platform-specific behaviors.

Here is the sample code to set up the platform instance and create a new LuaState instance.

    Platform.setInstance(new J2meMidp20Cldc11Platform());
    LuaState vm = Platform.newLuaState();
 


Field Summary
static java.lang.String LUA_STATE_CLASS_NAME
          When non-null, name of a Java Class that implements LuaState and has a default constructor which will be used to construct a new lua state using Platform.newLuaState()
 
Constructor Summary
Platform()
           
 
Method Summary
abstract  java.io.Reader createReader(java.io.InputStream inputStream)
          Create Reader from an InputStream
static Platform getInstance()
          Singleton to be used for platform operations.
abstract  java.lang.String getName()
          Get the name of the platform
abstract  java.lang.String getProperty(java.lang.String propertyName)
          Returns the value for the given platform property.
protected abstract  void installOptionalLibs(LuaState vm)
          Install optional libraries on the LuaState.
abstract  LNumber mathop(int id, LNumber x)
          Compute a math operation that takes a single double argument and returns a double
abstract  LNumber mathop(int id, LNumber x, LNumber y)
          Compute a math operation that takes a two double arguments and returns a double
abstract  LNumber mathPow(LNumber base, LNumber exponent)
          Compute math.pow() for two numbers using double math when available.
static LuaState newLuaState()
          Creates a new instance of LuaState.
abstract  java.io.InputStream openFile(java.lang.String fileName)
          Return an InputStream or null if not found for a particular file name.
static void setInstance(Platform platform)
          Set the Platform instance.
 LNumber unsupportedMathOp()
          Throw an error indicating the math operation is not accepted
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LUA_STATE_CLASS_NAME

public static java.lang.String LUA_STATE_CLASS_NAME
When non-null, name of a Java Class that implements LuaState and has a default constructor which will be used to construct a new lua state using Platform.newLuaState()

See Also:
newLuaState
Constructor Detail

Platform

public Platform()
Method Detail

getInstance

public static Platform getInstance()
Singleton to be used for platform operations. The default Platform gets files as resources, and converts them to characters using the default InputStreamReader class.


setInstance

public static void setInstance(Platform platform)
Set the Platform instance. This may be useful to define a file search path, or custom character encoding conversion properties.


newLuaState

public static LuaState newLuaState()
Creates a new instance of LuaState. If Platform.LUA_STATE_CLASS_NAME is not null, this method will attempt to create an instance using a construction such as (LuaState) Class.forName(Platform.LUA_STATE_CLASS_NAME).newInstance() If Platform.LUA_STATE_CLASS_NAME is null or the specified class cannot be instantiated for some reason, a new instance of LuaState will be created and used. In either case, the method LuaState.init() will be invoked, followed by Platform.installOptionalProperties(LuaState) on the new instance.

Returns:
a new instance of LuaState initialized via init() and installOptionalProperties()
See Also:
LUA_STATE_CLASS_NAME, LuaState

getName

public abstract java.lang.String getName()
Get the name of the platform


openFile

public abstract java.io.InputStream openFile(java.lang.String fileName)
Return an InputStream or null if not found for a particular file name.

Parameters:
fileName - Name of the file to open
Returns:
InputStream or null if not found.

createReader

public abstract java.io.Reader createReader(java.io.InputStream inputStream)
Create Reader from an InputStream

Parameters:
inputStream - InputStream to read from
Returns:
Reader instance to use for character input

getProperty

public abstract java.lang.String getProperty(java.lang.String propertyName)
Returns the value for the given platform property.

Parameters:
propertyName - Property name
Returns:
Property value

installOptionalLibs

protected abstract void installOptionalLibs(LuaState vm)
Install optional libraries on the LuaState.

Parameters:
vm - LuaState instance

mathPow

public abstract LNumber mathPow(LNumber base,
                                LNumber exponent)
Compute math.pow() for two numbers using double math when available.

Parameters:
lhs - LNumber base
rhs - LNumber exponent
Returns:
base ^ exponent as a LNumber, throw RuntimeException if not implemented

mathop

public abstract LNumber mathop(int id,
                               LNumber x)
Compute a math operation that takes a single double argument and returns a double

Parameters:
id - the math op, from MathLib constants
x - the argument
Returns:
the value as an LNumber
Throws:
LuaErrorException - if the id is not supported by this platform.

mathop

public abstract LNumber mathop(int id,
                               LNumber x,
                               LNumber y)
Compute a math operation that takes a two double arguments and returns a double

Parameters:
id - the math op, from MathLib constants
x - the first argument as an LNumber
y - the second arugment as an LNumber
Returns:
the value as an LNumber
Throws:
LuaErrorException - if the id is not supported by this platform.

unsupportedMathOp

public LNumber unsupportedMathOp()
Throw an error indicating the math operation is not accepted



Copyright © 2007-2013 Luaj.org. All Rights Reserved.