696 lines · 20.4 KB · Objective-C
RawDownload
## jni -- java native interface## this module defines the limbo helper# functions for java class library # native methods (limbo). ## This module is based on the JNI # specification from JavaSoft for# VM implementations supporting a C/C++# interface.# include "sys.m";include "math.m";include "string.m";include "loader.m";include "classloader.m";include "cast.m";	 JNI : module{	# where to find jni.dis	PATH   : con "/dis/java/jni.dis"; 	#	# exported read-only vars	# upon JNI initialization the following	# variables are assigned. Native method modules	# can use the following instances rather than	# loading their own copies. These variables	# should never be assigned.	sys     : Sys;                 # std inferno sys module	math    : Math;                # std inferno math module	str     : String;              # std inferno string module       	jldr    : JavaClassLoader;     # primary java class loader	jassist : JavaAssist;          # java assist module 	# boolean constants	FALSE  : con 0;	TRUE   : con 1;	BFALSE : con byte 0;	BTRUE  : con byte 1; 	# return error flags for JNI functions	OK     : con 0;   	ERR    : con -1;   	# JVM Type Constants: used for identifying Java primitive types.	T_JVMSTART : con 4;	T_BOOLEAN, T_CHAR, T_FLOAT, T_DOUBLE, T_BYTE, T_SHORT, 	T_INT, T_LONG, T_OBJECT, T_ARRAY, T_VOID,	T_JVMLAST	: con (iota + T_JVMSTART); 	# JVM class flags constants	ACC_PUBLIC:         con 16r0001;	ACC_PRIVATE:        con 16r0002;	ACC_PROTECTED:      con 16r0004;	ACC_STATIC:         con 16r0008;	ACC_FINAL:          con 16r0010;	ACC_SUPER:          con 16r0020;	ACC_SYNCHRONIZED:   con 16r0020; 	ACC_VOLATILE:       con 16r0040;	ACC_TRANSIENT:      con 16r0080;	ACC_NATIVE:         con 16r0100;	ACC_INTERFACE:      con 16r0200; 	ACC_ABSTRACT:       con 16r0400; 	ACC_WRITTEN_FLAGS:  con 16r0fff;  	# common java types 	#	# The following two types are used to represent	# Java classes. The 'ClassData' type contains all	# of the information pertaining to a class and is	# used to obtain class information and instantiate	# objects.  Although there  is information in the	# structure it should normally be treated as an	# opaque type by client native method modules.	#	ClassModule : type ref Loader->Niladt;	ClassData   : type ref JavaClassLoader->Class; 	#	# This is the "generic" type of any Java instance.	# Operations on the object can be performed via this	# generic handle.	#	JObject  : type ref JavaClassLoader->Object; 	#	# This represents an instance of java.lang.Class	# It is an object which contains a ptr to the	# actual class data it represents.	#	JClass   : type ref JavaClassLoader->ClassObject; 	#	# This is an instance of java.lang.String.	# This class is a wrapper of a Dis string.	#	JString  : type ref JavaClassLoader->JavaString; 	JBoolean : type int; 	# Java arrays are actually classes	# which wrap a Dis array.  The array	# class matches the adt 'Array' defined	# in JavaClassLoader. The following arrays	# should mirror that adt and are used by	# limbo modules to access the array contents	# directly. Java Arrays should only be created	# in Java code or via the Array functions	# below.  A simple limbo instance of the	# array adt's is not good enough.  	# JArray is used as a "generic" array and to represent	# multi-dimensional arrays.	JArray        : type ref JavaClassLoader->Array;  # x[][].. 	IntArray : adt	{		cl_mod:		ref Loader->Niladt;     # object's class data 		ary:		array of int;           # actual dis array		dims:		int;                    # number of dimensions		class:		ClassData;              # class data of base type		primitive:	int;                    # set if base type is primitive	}; 	ByteArray : adt	{		cl_mod:		ref Loader->Niladt;     # object's class data 		ary:		array of byte;          # actual dis array		dims:		int;                    # number of dimensions		class:		ClassData;              # class data of base type		primitive:	int;                    # set if base type is primitive	}; 	BigArray : adt	{		cl_mod:		ref Loader->Niladt;     # object's class data 		ary:		array of big;           # actual dis array 		dims:		int;                    # number of dimensions		class:		ClassData;              # class data of base type		primitive:	int;                    # set if base type is primitive	}; 	RealArray : adt	{		cl_mod:		ref Loader->Niladt;     # object's class data 		ary:		array of real;          # actual dis array		dims:		int;                    # number of dimensions		class:		ClassData;              # class data of base type		primitive:	int;                    # set if base type is primitive	}; 	StringArray : adt	{		cl_mod:		ref Loader->Niladt;     # object's class data 		ary:		array of JString;       # actual dis array		dims:		int;                    # number of dimensions		class:		ClassData;              # class data of base type		primitive:	int;                    # set if base type is primitive	}; 	ClassArray : adt	{		cl_mod:		ref Loader->Niladt;     # object's class data 		ary:		array of JClass;        # actual dis array		dims:		int;                    # number of dimensions		class:		ClassData;              # class data of base type		primitive:	int;                    # set if base type is primitive	};		ObjectArray : adt	{		cl_mod:		ref Loader->Niladt;     # object's class data 		ary:		array of JObject;       # actual dis array		dims:		int;                    # number of dimensions		class:		ClassData;              # class data of base type		primitive:	int;                    # set if base type is primitive	}; 	ArrayArray : adt	{		cl_mod:		ref Loader->Niladt;     # object's class data 		ary:		array of JArray;        # actual dis array		dims:		int;                    # number of dimensions		class:		ClassData;              # class data of base type		primitive:	int;                    # set if base type is primitive	}; 	#	# Common type names emitted by 'javal'.	#	JArrayI : type ref IntArray;    # int[]	JArrayC : type ref IntArray;    # char[]	JArrayB : type ref ByteArray;   # byte[]	JArrayS : type ref IntArray;    # short[]	JArrayJ : type ref BigArray;    # long[]	JArrayF : type ref RealArray;   # float[]	JArrayD : type ref RealArray;   # double[]	JArrayZ : type ref ByteArray;   # boolean[] 	JArrayJClass  : type ref ClassArray;  # Class[]	JArrayJString : type ref StringArray; # String[]	JArrayJObject : type ref ObjectArray; # Object[] 	ThreadObject : adt    {        cl_mod               : ClassModule;        name                 : JArrayC;        priority             : int;        #threadQ             : cyclic ref ThreadObject;        PrivateInfo          : ref JavaClassLoader->ThreadData;		was_interrupted      : byte;    #boolean        #eetop               : int;        #single_step         : byte;    #boolean        daemon               : byte;    #boolean        stillborn            : byte;    #boolean        target               : JObject;        group                : JObject;        #initial_stack_memory : int;    };	JThread : type ref ThreadObject;  	#	# This union type is used for setting/getting fields	# of an arbitrary Java object/class, and also for	# calling arbitrary object/class methods.	# To set Java reference types (i.e. objects, arrays)	# they must be converted to a JObject (i.e. use TObject).	#	# One way to create an instance of this type is:	#     val := ref Value.TObject(obj);	#	# To retrieve a value use a 'pick' statement or one	# of the "getter" functions below.	#	# NOTE: the Low module is aware of the order of the "pick" tags 	#	Value :adt	{		pick 		{			TObject   => jobj    : JObject; 			TByte     => jbyte   : int;			TBoolean  => jboolean: int;			TChar     => jchar   : int;			TShort    => jshort  : int;			TInt      => jint    : int;			TLong     => jlong   : big;			TFloat    => jfloat  : real;			TDouble   => jdouble : real;		} 		#		# Get a value of the expected type. If the		# actual type is not the expected type then		# a FatalError() is called.		#		Boolean : fn( this : self ref Value ) : JBoolean;		Byte    : fn( this : self ref Value ) : int;		Short   : fn( this : self ref Value ) : int;		Int     : fn( this : self ref Value ) : int;		Long    : fn( this : self ref Value ) : big;		Float   : fn( this : self ref Value ) : real;		Double  : fn( this : self ref Value ) : real;		Char    : fn( this : self ref Value ) : int;		Object  : fn( this : self ref Value ) : JObject;  	}; 	# 	# This structure is used to "describe" a Java type. A Java signature	# stored as a string is converted into this ADT for better comparisons.	# Depending on the actual type vairous parts of the ADT will be filled	# in. The 'sig' field will contain the appropriate java signagure string	# associated with the type. The following details how to distinguish the 	# type:	# non-array ref: 	#     'class' represents the class info; other fields are 0/nil	# array ref:	#     "class" is nil for primitive array and 'prim' contains one of	#     the "Java Type Constants" representing the array's base type.	#     for non-primitive arrays 'class' is the ClassData for the base	#     type; 'prim' is 0; 'dims' contains the number of dimensions	# primitive:  	#     'class' is nil; 'dims' is 0; 'prim' is one of Java Type Consts	# method:	#     'params' is a list of Descriptor's for each param -- nil if the	#     method has no parameters. The method return value is represented 	#     by the other fields as described above; a method w/ no return 	#     value will have (nil,T_VOID,0,"V",nil) descriptor. NOTE: the param 	#     list is in (Right-to-Left order -- i.e right most param is the 	#     list-head).	# 	Descriptor : adt	{		class   : ClassData; 		prim    : int;       		dims    : int;       		sig     : string;       #type's java signature		params  : cyclic list of ref Descriptor;	}; 		#	# argument passed to AttachCurrentThread	#	ThreadAttachArgs : adt	{		name   : string;  #can be nil		daemon : byte;    #1=true	    group  : JObject; #nil ==> main group	};  	#	# JNI Functions	# 	# 	# Return JNI version number in the form	# of 0xVVVVIIII where VVVV=major version	# and IIII= minor version	#	GetVersion : fn() : int;    	##	## Class/Object access	## 	#	# Find the class data for a given class name.	# Will cause a load of the class if necessary.	# NOTE: this does not return a java.lang.Class	# object, but rather the internal structure.	#	FindClass  : fn( name : string ) : ClassData; 	#	# Find the class data for a given class name.	# Will NOT load the class if necessary.	#	InternalFindClass : fn( name : string ) : ClassData; 	#	# Return the class's superclass data.	#	GetSuperclass  : fn( cl_data : ClassData ) : ClassData; 	#	# Return the java.lang.Class instance associated	# with this instance.	#	GetObjectClass : fn( obj : JObject ) : JClass; 	#	# Return the internal Class Data information	# associated with this instance	#	GetObjectClassData : fn( obj : JObject ) : ClassData; 	#	# return the associated java.lang.Class instance	# associated with the given internal Class	# Data.	#	GetClassObject : fn( cl_data : ClassData ) : JClass; 	#	# return the java.lang.Class instance associated	# with the array class that has signature 'sig'.	#	LookupArrayClass : fn( sig : string ) : JClass; 	#	# Allocate an instance of java.lang.Class that 	# represents the class indicated by 'cl_data'.	# The 'cl_data' can be obtained via the functions above.	#	NewClassObject : fn( cl_data : ClassData ) : JClass; 	#	# Allocate a new instance of the class; no constructor called.	#	AllocObject    : fn( cl_data : ClassData ) : JObject; 	#	# Allocate a new instance of the class and 	# attempt to call the "default" constructor.	# NOTE: this routine should be used with care, 	# because not all classes have default constructors.		#	NewObject      : fn( cl_data : ClassData ) : JObject; 	#	# Duplicate the object. This function will create	# a new instance of the object's class and perform	# a shallow copy of the the objects data contents	# into the new instance.	# NOTE: the constructor of the new object is not invoked	#	DupObject      : fn( obj : JObject ) : JObject; 	#	# Return a hash code based on the object's reference,	# not its contents. This is called by 	# java.lang.Object.hashCode() as well as 	# System.identityHashCode().	#	IdentityHash   : fn( obj : JObject ) : int; 	#	# Allocate a java string object and place the	# dis string as its contents.	#	NewString      : fn( val : string ) : JString; 	#	# Just like NewString but return as a	# JObject type, rather then JString	#	NewStringObject : fn( val : string ) : JObject;    	#	# Set object/class data fields.	# Must pass the "object instance"/"class data" along with the	# field name and the value it should be set to. The contents of	# the 'val' union type is used to determine the field type and if	# they are not compatable the function fails.	#	# RETURN:  JNI->OK=> success;  	#          JNI->ERR=> failure	#	SetStaticField : fn( cl_data : ClassData, name : string, val : ref Value ) : int;	SetObjField    : fn( obj : JObject, name : string, val : ref Value ) : int; 	# Get object/class data fields.	# Must pass the "object instance"/"class data" along with the field	# name and a "type signature" (see JVM reference). The type sig must	# match the actual field sig exactly. 	# 	# RETURN: nil=> failure; 	#         non-nil=> a 'Value' union type with the field contents	#	GetStaticField : fn( cl_data : ClassData, name : string, typ : string ) : ref Value;	GetObjField    : fn( obj : JObject, name : string, typ : string ) : ref Value; 		#	# Call a method:	#   cl_data/obj : the class or object being called	#   meth        : name of method to call	#   sig         : signature of method being called	#   args        : array of args to pass (nil==void) 'this' is implicitly	#                 passed for dynamic (object) method calls	#   return      : tuple: value of method call (nil==void), error code	#	#   example:  	#   v := Value.Int(99);	#   args := array[] of {v};	#   (val,err) := CallMeth( o, "foo", "(I)I", args );	#   if ( err == JNI->OK )	#		sys->print( "return: %d\n", val.Int() );	#	CallStatic     : fn( cl_data : ClassData, meth : string, sig : string, args : array of ref Value ) : (ref Value,int);	CallMethod     : fn( obj : JObject, meth : string, sig : string, args : array of ref Value ) : (ref Value,int);  	#	# Method call error codes	#	CALLERR_NOMETHOD,   # method not found	CALLERR_BADSIG,     # bad signature	CALLERR_BADARGCNT,  # arg count mismatch	CALLERR_BADARG      # argument mismatch: errcode-CALLERR_BADARG==argument position	                    # 'badarg' must be last.				        : con (iota+1);  	##	## Object/Class comparison functions	## 	IsInstanceOf    : fn( obj : JObject, cl_data : ClassData )  : JBoolean;	IsAssignable    : fn( assignee, assignTo : JClass )         : JBoolean;	IsSameObject    : fn( obj1, obj2 : JObject )                : JBoolean;	 	# Check if 'ary' can be assigned to an instance with matching 'desc'.	# If 'ary' or 'desc' are nil then return FALSE.	ArrayInstanceOf : fn( ary : JArray, desc : ref Descriptor ) : JBoolean; 

Lines 1–500 of 696.