jni-0.7.0: Complete JNI raw bindings.
Safe HaskellNone
LanguageHaskell2010

Foreign.JNI.Unsafe

Description

Low-level bindings to the Java Native Interface (JNI).

Read the JNI spec for authoritative documentation as to what each of the functions in this module does. The names of the bindings in this module were chosen to match the names of the functions in the JNI spec.

All bindings in this module access the JNI via a thread-local variable of type JNIEnv *. If the current OS thread has not yet been "attached" to the JVM, it needs to be attached. See runInAttachedThread.

The String type in this module is the type of JNI strings. See Foreign.JNI.String.

The functions in this module are considered unsafe in opposition to those in Foreign.JNI.Safe, which ensure that local references are not leaked.

Synopsis

JNI functions

VM management

withJVM :: [ByteString] -> IO a -> IO a Source #

Create a new JVM, with the given arguments. Destroy it once the given action completes. Can only be called once. Best practice: use it to wrap your main function.

newJVM :: [ByteString] -> IO JVM Source #

Create a new JVM, with the given arguments. Can only be called once. Best practice: use withJVM instead. Only useful for GHCi.

destroyJVM :: JVM -> IO () Source #

Deallocate a JVM created using newJVM.

Class loading

defineClass Source #

Arguments

:: Coercible o (J ('Class "java.lang.ClassLoader")) 
=> ReferenceTypeName

Class name

-> o

Loader

-> ByteString

Bytecode buffer

-> IO JClass 

String wrappers

data Signature Source #

A string representing a signature, well-formed by construction.

Instances

Instances details
Eq Signature Source # 
Instance details

Defined in Foreign.JNI.Internal

Ord Signature Source # 
Instance details

Defined in Foreign.JNI.Internal

Show Signature Source # 
Instance details

Defined in Foreign.JNI.Internal

Exceptions

newtype JVMException Source #

A JNI call may cause a (Java) exception to be raised. This module raises it as a Haskell exception wrapping the Java exception.

Constructors

JVMException JThrowable 

throw :: Coercible o (J a) => o -> IO () Source #

Query functions

findClass Source #

Arguments

:: ReferenceTypeName

Class name

-> IO JClass 

getFieldID Source #

Arguments

:: JClass

A class object as returned by findClass

-> String

Field name

-> Signature

JNI signature

-> IO JFieldID 

getStaticFieldID Source #

Arguments

:: JClass

A class object as returned by findClass

-> String

Field name

-> Signature

JNI signature

-> IO JFieldID 

getMethodID Source #

Arguments

:: JClass

A class object as returned by findClass

-> String

Field name

-> MethodSignature

JNI signature

-> IO JMethodID 

getStaticMethodID Source #

Arguments

:: JClass

A class object as returned by findClass

-> String

Field name

-> MethodSignature

JNI signature

-> IO JMethodID 

Reference manipulation

newGlobalRef :: Coercible o (J ty) => o -> IO o Source #

Creates a global reference to the object referred to by the given reference.

Arranges for a finalizer to call deleteGlobalRef when the global reference is no longer reachable on the Haskell side.

deleteGlobalRef :: Coercible o (J ty) => o -> IO () Source #

newGlobalRefNonFinalized :: Coercible o (J ty) => o -> IO o Source #

Like newGlobalRef but it doesn't attach a finalizer to destroy the reference when it is not longer reachable. Use deleteGlobalRefNonFinalized to destroy this reference.

deleteGlobalRefNonFinalized :: Coercible o (J ty) => o -> IO () Source #

Like deleteGlobalRef but it can be used only on references created with newGlobalRefNonFinalized.

newLocalRef :: Coercible o (J ty) => o -> IO o Source #

deleteLocalRef :: Coercible o (J ty) => o -> IO () Source #

popLocalFrame :: Coercible o (J ty) => o -> IO o Source #

Field accessor functions

Get fields

Get static fields

Set fields

setObjectField :: Coercible o (J a) => o -> JFieldID -> JObject -> IO () Source #

setBooleanField :: Coercible o (J a) => o -> JFieldID -> Word8 -> IO () Source #

setIntField :: Coercible o (J a) => o -> JFieldID -> Int32 -> IO () Source #

setLongField :: Coercible o (J a) => o -> JFieldID -> Int64 -> IO () Source #

setCharField :: Coercible o (J a) => o -> JFieldID -> Word16 -> IO () Source #

setShortField :: Coercible o (J a) => o -> JFieldID -> Int16 -> IO () Source #

setByteField :: Coercible o (J a) => o -> JFieldID -> CChar -> IO () Source #

setDoubleField :: Coercible o (J a) => o -> JFieldID -> Double -> IO () Source #

setFloatField :: Coercible o (J a) => o -> JFieldID -> Float -> IO () Source #

Set static fields

Method invocation

callVoidMethod :: Coercible o (J a) => o -> JMethodID -> [JValue] -> IO () Source #

Object construction

Array manipulation

data ArrayCopyFailed Source #

Thrown when GetPrimitiveTypeArrayElements returns a null pointer, because it wanted to copy the array contents but couldn't. In this case the JVM doesn't throw OutOfMemory according to the JNI spec.

Constructors

ArrayCopyFailed 

getObjectArrayElement :: forall a o. (IsReferenceType a, Coercible o (J a)) => JArray a -> Int32 -> IO o Source #

setObjectArrayElement :: forall a o. (IsReferenceType a, Coercible o (J a)) => JArray a -> Int32 -> o -> IO () Source #

Thread management

runInAttachedThread :: IO a -> IO a Source #

Attaches the calling thread to the JVM, runs the given IO action and then detaches the thread.

If the thread is already attached no attaching and detaching is performed.

data ThreadNotAttached Source #

A JNI call is made from a thread not attached to the JVM.

Constructors

ThreadNotAttached 

NIO support