Wednesday, November 25, 2009

Extending WCF Part I


by Mosfiqur Rahman


Senior Software Engineer @Kaz



Introduction



Microsoft WCF Framework (System.ServiceModel) provides several extension points
from where developer can extend the framework when it requires. In this Part I article
I'm going to cover two perspective of extending WCF framework:




1. Lets think about a WPF desktop application or a WPf browser application or a
windows desktop application or a .net web application, built on service oriented
architecture and all the back end services are WCF services. Now for several reasons
you may need to change the server for your service which in turn changes your service
URL or you may need to change the configuration of service bindings/behaviors. If
you change any WCF configuration at the server you need to provide the same bindings/behaviors
configuration to the clients also to work the client-service interaction properly.
And this is very difficult in typical clients like XBAP (WPF Browser application)
clients. My ExtendingWCFPartI.WcfExtentions.dll component provides a solution to
this. You can have an external configuration file (e.g. by downloading it from server
before client application starts up) where service endpoints are configured properly
and use that external configuration file to create the WCF service proxy instances.





2. In many cases we want to get exactly the same exception thrown from WCF service
at WCF client. ExtendingWCFPartI.WcfExtentions.dll component also provides this.



Background



Extending ChannelFactory<T>




This extension is to serve the first introduction item.
In general System.ServiceModel.ChannelFactory<T> provides us the option to
get a proxy instance programmatically instead of using the proxy generated through
svcutil.exe (.NET Framework tool). I've extended this class to write my own ExtendingWCFPartI.WcfExtentions.ExtendedChannelFactory<T>
class to provide the external configuration file functionality.
The constructor argument "configurationPath" should contain the full path of the
external configuration file. Then I override the ChannelFactory<T>.CreateDescription()
method to apply the external configuration file. The external configuration file
looks exactly the same as conventional client's app.config or web.config file. For
sample external configuration, you can look at the ExtendingWCFPartI.Client.WcfClientConfiguration.xml
file.




Extending WCF Service and WCF client behavior



To provide the second introduction item, we need to
extend behavior of service, endpoint and contract.


public class ExtendedServiceErrorHandler:IErrorHandler
{
#region IErrorHandler Members

bool IErrorHandler.HandleError(Exception error)
{
if (error is FaultException)
{
return false; // Let WCF do normal processing
}
else
{
return true; // Fault message is already generated
}
}

void IErrorHandler.ProvideFault(Exception error,
MessageVersion version,
ref Message fault)
{
if (error is FaultException)
{
// Let WCF do normal processing
}
else
{
// Generate fault message manually
MessageFault messageFault = MessageFault.CreateFault(
new FaultCode("Sender"),
new FaultReason(error.Message),
error,
new NetDataContractSerializer());
fault = Message.CreateMessage(version, messageFault, null);
}
}

#endregion
}// end of class

ExtendedServiceErrorHandler should be injected into

ChannelDispatcher's
error handler list. Then WCF Framework will use our
implemented IErrorHandler.ProvideFault method when any exception is thrown from
service. Inside the method implementation I used the NetDataContractSerializer to
serialize the MessageFault which will be trasmitted to client.

public class ExtendedClientMessageInspector:IClientMessageInspector
{
#region Methods

/// Reads the soap message to find the
/// node. If found than deserialize it using the
/// NetDataContractSerializer to construct the exception.
private static object ReadFaultDetail(Message reply)
{
const string detailElementName = "Detail";

using (var reader = reply.GetReaderAtBodyContents())
{
// Find

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.LocalName == detailElementName)
{
break;
}
}

// Did we find it?
if (reader.NodeType != XmlNodeType.Element || reader.LocalName != detailElementName)
{
return null;
}

// Move to the contents of
if (!reader.Read())
{
return null;
}

// Deserialize the fault
var serializer = new NetDataContractSerializer();
try
{
return serializer.ReadObject(reader);
}
catch (FileNotFoundException)
{
// Serializer was unable to find assembly where exception is defined
return null;
}
}
}

#endregion


#region IClientMessageInspector Members

/// Create a copy of the original reply to allow
/// default processing of the message. Then its reads
/// the copied reply to find Fault Detail using the ReadFaultDetail method
void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
{
if (reply.IsFault)
{
// Create a copy of the original reply to allow default processing of the message
var buffer = reply.CreateBufferedCopy(Int32.MaxValue);
var copy = buffer.CreateMessage(); // Create a copy to work with
reply = buffer.CreateMessage(); // Restore the original message

var faultDetail = ReadFaultDetail(copy);
var exception = faultDetail as Exception;
if (exception != null)
{
throw exception;
}
}
}

object IClientMessageInspector.BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
return null;
}

#endregion
}// end of class

Now we need to extend the IClientMessageInspector to get back the same exception
at client end. At IClientMessageInspector.AfterReceiveReply method implementation,
NetDataContractSerializer has been used to get desrialize the fault exception and
get the actual exception back again.


public class ExtendedServiceBehavior:Attribute, IServiceBehavior, IEndpointBehavior, IContractBehavior
{
private void ApplyDispatchBehavior(ChannelDispatcher dispatcher)
{
// Don't add an error handler if it already exists
foreach (IErrorHandler errorHandler in dispatcher.ErrorHandlers)
{
if (errorHandler is ExtendedServiceErrorHandler)
{
return;
}
}
dispatcher.ErrorHandlers.Add(new ExtendedServiceErrorHandler());
}


private void ApplyClientBehavior(ClientRuntime runtime)
{
// Don't add a message inspector if it already exists
foreach (IClientMessageInspector messageInspector in runtime.MessageInspectors)
{
if (messageInspector is ExtendedClientMessageInspector)
{
return;
}
}

runtime.MessageInspectors.Add(new ExtendedClientMessageInspector());
}
}

To tell the .NET runtime to use my ExtendedServiceErrorHandler at Service end error
handling and ExtendedClientMessageInspector at client error halding we need this
ExtendedServiceBehavior class which implemented service, endpoint and contract behavior.
All behaviors have an AddBindingParameters method, an ApplyDispatchBehavior method,
a Validate method, and an ApplyClientBehavior method with one exception: Because
IServiceBehavior cannot execute in a client, it does not implement ApplyClientBehavior.
For more details you can read the
msdn
documentation.



Using the Code




For simplicity, I've hosted the WCF service at console. To run the sample code and
see it working, follow the steps below:

1. After opening the "ExtendingWCFPartI" application with VS, right click on the
ExtendingWCFPartI.Service project then click Debug -- > Start new instance. Doing
so, you have ensured that the service is running at console host.

2. Then run the ExtendingWCFPartI.Client project under debug mode.

ExtendingWCFPartI.WcfExtentions.dll is the reusable component which is going to
be used at both WCF client and WCF service. Also make sure that you wrote the custom
exceptions in a common assembly which will be used at both WCF client and WCF service.



Using the code at WCF Service




At first let me show you how we can use this component to host our WCF services. You
can host the WCF services at IIS or at Windows Service or at console, no matter
where you host it, add the following section at your app.config or web.config ServiceModel
section. Add a reference the ExtendingWCFPartI.WcfExtentions.dll to your service
host application


<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="ExtendedServiceBehavior" type="ExtendingWCFPartI.WcfExtentions.ServiceBehaviorExtension, ExtendingWCFPartI.WcfExtentions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

</behaviorExtensions>
</extensions>
</system.serviceModel>

ExtendingWCFPartI.WcfExtentions.ServiceBehaviorExtension has been used to tell WCF
framework to use ExtendedServiceBehavior at its CreateBehavior() method.

<system.serviceModel>
<behaviors>

<serviceBehaviors>
<behavior name="CustomServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceThrottling maxConcurrentCalls="16" maxConcurrentSessions="16" />
<ExtendedServiceBehavior />

</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

Now create a service behavior like the example above. <ExtendedServiceBehavior
/> ensures that this behavior configuration will use the ExtendedServiceBehavior.


<system.serviceModel>
<services>
<service behaviorConfiguration="CustomServiceBehavior" name="ExtendingWCFPartI.Service.CustomerService">
...
...
Your service endpoint's configuration goes here.
</service>
</services>
</system.serviceModel>


Thats all you need to do to host your WCF service which will have the two functionalities
explained at introduction.



Using the code at WCF Client



Add a reference to the ExtendingWCFPartI.WcfExtentions.dll at your client application.
Now create the external configuration file. It may look like following:



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="ExtendedServiceEndpointBehavior" type="ExtendingWCFPartI.WcfExtentions.ServiceBehaviorExtension, ExtendingWCFPartI.WcfExtentions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>

</extensions>
<bindings>
.. tips: provide the same binding which has been used at service
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="CustomServiceEndpointBehavior">

<ExtendedServiceEndpointBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost:8036/Services/CustomerService"
binding="wsHttpBinding" bindingConfiguration="CustomWSHttpBinding"
behaviorConfiguration="CustomServiceEndpointBehavior"
contract="ExtendingWCFPartI.Common.Services.ICustomerService" name="CustomerServiceEndPoint" />

</client>
</system.serviceModel>
</configuration>

Now save this external configuration file at any location of your hard drive with
.xml or .config extension. e.g. "C:\Temp\MyAppServices.xml".
I've provided a very simple inerface ExtendingWCFPartI.WcfExtentions.WcfClientHelper
to get the instance of proxy.

public static T GetProxy<T>(string externalConfigPath)
{
var channelFactory = new ExtendedChannelFactory<T>(externalConfigPath);
channelFactory.Open();
return channelFactory.CreateChannel();
}


At client code use this interface to call your service method:

var externalConfigPath = @"C:\Temp\MyAppServices.xml";
var proxy = WcfClientHelper.GetProxy<IMyWCFService>(externalConfigPath);
proxy.CallServiceMethod();



Points of Interest



There are many more extension points at WCF framework which are very interesting
and usefull. I'll try my best to cover few more examples later in my next article.
In brief, I found the WCF framework so well designed that programmers have almost
all the options to do whatever he wants.

Wednesday, November 4, 2009

Designer = can do XAML !!!

Sunday, October 25, 2009


It means a combination of “Creativity and Coding”. Yes now a Designer can be a coder as well. Perhaps you are thinking that it’s a crap :-). Nope it is real. I never wanted to go to the programming world. I think it is a bit boring and monotonous for a designer. Some couple of month back our CTO comes and requested me to learn XAML. He explained all the prospects of XAML in a positive mode. At that time I was thinking that what my CTO is saying is truly a fantasy web world. One question was hitting repeatedly in my mind and that is “CAN I DO THAT?”.


Yes XAML is truly a fantasy playground for a designer. After getting 2 months successful training (Thanks to our CTO and special thanks to Taufiq for being an excellent XAML teacher) on XAML I got the confident and started doing coding immediately with the other developers :-). Ok cut off the story of mine and let discuss the prospect of XAML.


XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next generation managed applications. Extensible Application Markup Language or is a new declarative language that is used in Windows Framework and Windows Presentation Foundation. Introduced with .NET Framework 3.0, XAML is harbinger of declarative (in contrast with imperative using coding) programming model. In Workflow Foundation, workflows can be created using code as well as XAML. Even fully XAML-based solutions are possible with no code at all! This not only gives flexibility of no-compilation-needed to applications, it also opens up a window of possibility for entirely non-technical persons (like business analysts) to design workflows. (Though Workflow Foundation is targeted for programmers, experiments are underway around the globe for making it feasible for business analysts to model processes in it).


Let me ask a simple question. Can you build a Combo box on a button? Or can you customize a button for a Combo box? Very difficult or even not possible in other programming languages but it can be done in XAML with a few lines of code. So a Designer can play around wherever he/she wants to. So the bottom line is Designer can now think out of the box, can do outstanding mockups for web or desktop application.


xaml.jpg


For XAML, we need to have a basic knowledge on XML, CSS. You will get lot of tutorials for XAML on the net. So learn XAML and discover the future.


Enjoy!

Thursday, March 12, 2009

Shadows of Sinensis

- Md. Ferdous Bhuiyan

Shadows of Sinensis

Shadows of Sinensis, a memorable trip to Srimongol, will remain afresh in the mind of Kaz family. Fun-loving Kaz Family went on rampage to obtain unlimited ‘Furti’ and relaxation during our 3-day tour to Srimongol from 15 to 17 February, 2009. Eventful Srimongol trip offered us relaxation, fun and most importantly the joy to share with our colleagues and family members. The team of Srimongol trip encompasses each and every one of Kaz Family. Young stars of the team aged 3-month to 3-year provided the team with pure and refreshing entertainment in the lap of nature. All of us along with our spouses and kids looted the unlimited joy by swimming in the pool, climbing the mountains, wandering in the wilderness and having delicious foods.

Shadows of Sinensis

Powered by 41-member, Kaz team reveals the sportive frenzy of Kaz family. Many of our enthusiastic players as well as cheering audiences enjoyed watching and playing Cricket, Badminton, Football and Table Tennis at daylight and even at night using light. Frenzied bedding accompanied by enthusiastic cheering gave the events a special glow.
Silence of the evenings was shattered with the sound of laughter provoked by lively ‘Adda’ and the melody of harmonica and songs sung by us ruled the isolation of night. Monotony or tiered is something that failed to touch the mind even for a second. Luxurious and well-furnished bungalows became the place to spend last few hours of night to start a new day with more enthusiasm.

Shadows of Sinensis

A spur-of-the-moment decision fixed the day scheduled. Going to ‘Lawachara’ or finding out the tea-stall named ‘Nilkantha’ to have multilayered tea was decided within a few minutes with the urge to know the unknown. Such whimsical decisions to visit outside of the Tea Resort made the trip more attractive and gave us the chance to experience something new and adventurous. Those who were more attracted to water of appealing swimming pool were busy with swimming and diving throughout the whole noon.
Delicious Food must be there to color a memorable time. We the KAZ people vowed not to get hungry and have delicious good food with every single chance of celebrating party. There is no exception of this oath even at Srimongol trip. Being the fan of delicious food we grilled chicken and included delicious dishes for a sensational dinner and enjoyed the last night having spicy food and dessert.

Shadows of Sinensis

Sensible traditional ethos of our firm revives with this lovely trip. And it would be no rhetoric to reiterate the truth that ‘Shadows of Sinensis’ is a great step forward for the neatly tied Kaz family to enhance its fellow-feelings and team spirit as ever.

Shadows of Sinensis

Thursday, July 31, 2008

Emperors on the move

At last August his here!

August at Kaz is the long awaited month of the anniversary party! (Kaz actually started in June, but we do the anniversary party in August because it fits with the software world's tendency of taking it easy in August worldwide!)

This year we are off to the great triangle trip of Delhi - Agra and Jaipur. The trip is code named "Emperors on the move!"

Ten days of non-stop furti!

Emperors on the move

Saturday, March 29, 2008

Home Made Java Virtual Machine


by Maruf Maniruzzaman




Introduction


Back in year 2004 I had to choose a thesis topic as a prerequisite of completion of my undergraduate course in
Computer Science and Engineering. I had choosen Process Migration. And our teacher Mahmud Shahriar Hossain agreed to
supervise my work. My partner was Md. Helal Uddin. As part of the thesis I had to implement a Java Virtual Machine.
I wanted to write an article since then. But it never actually happened. Today (March 2) is my birth day and
I want to start it. The virtual machine is also used in my new
project Morpheus - a prototype of Silverlight 1.1.
The seminar presentation downloadable from above link shows how a JVM works. You may also look at the JVM
source code from above link. Please note that most the implementation decision taken may not match with other
commercially available JVM implementation. Whenever JVM Spec is found to say nothing, the most easiest approach
is taken to save time.

Java Virtual Machine Parts



Class File Structure


The java virtual machine needs an application that is made up of collection of java classes. At the beginning of any
class there is a defined structure like JavaClassFileFormat.


struct JavaClassFileFormat
{
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info **constant_pool; //[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2* interfaces; //[interfaces_count];
u2 fields_count;
field_info_ex *fields; //[fields_count];
u2 methods_count;
method_info_ex* methods; //[methods_count];
u2 attributes_count;
attribute_info** attributes; //[attributes_count];
};

Following are the structures used in the format. They represents constant pool (constant values used in class files), fields,
methods and attributes in a class file. I'll describe them in details later.


struct cp_info
{
u1 tag;
u1* info;
};

struct field_info
{
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info* attributes; //[attributes_count];
};

struct method_info
{
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info* attributes; //[attributes_count];
};

struct attribute_info
{
u2 attribute_name_index;
u4 attribute_length;
u1* info;//[attribute_length];
};

We first load the class file in memory as raw byte and then use JavaClass object to parse the raw bytes
and identify the fields, methods, exception table etc. The JavaClass class represent a class in memory in
structured form. It holds a pointer to the raw byte stream that was loaded from class file.

Java Class in Memory


Here we just inherit the JavaClassFileFormat for simplicity of storing the values in in memory class
representation. We must parse the raw class file in memory to get the values for JavaClassFileFormat fields.


class JavaClass: public JavaClassFileFormat
{
public:
JavaClass(void);
virtual ~JavaClass(void);

public:
virtual BOOL LoadClassFromFile(CString lpszFilePath);
void SetByteCode(void* pByteCode);

BOOL ParseClass(void);
BOOL ParseInterfaces(char* &p);
BOOL ParseFields(char* &p);
BOOL ParseMethods(char* &p);
BOOL ParseAttributes(char* &p);
BOOL GetConstantPool(u2 nIndex, cp_info& const_pool);

BOOL GetStringFromConstPool(int nIndex,CString& strValue);
CString GetName(void);
CString GetSuperClassName(void);
BOOL ParseMethodCodeAttribute(int nMethodIndex, Code_attribute* pCode_attr);
int GetMethodIndex(CString strMethodName, CString strMethodDesc,JavaClass* &pClass);
int GetFieldIndex(CString strName, CString& strDesc);
void SetClassHeap(ClassHeap *pClassHeap){this->m_pClassHeap=pClassHeap;}
virtual u4 GetObjectSize(void);
virtual u4 GetObjectFieldCount(void);
JavaClass* GetSuperClass(void);
BOOL CreateObject(u2 index, ObjectHeap *pObjectHeap, Object& object);
BOOL CreateObjectArray(u2 index, u4 count, ObjectHeap *pObjectHeap, Object& object);
private:
size_t m_nByteCodeLength;
void *m_pByteCode;
u2 m_nObjectFieldsCount;
BOOL ParseConstantPool(char* &p);
int GetConstantPoolSize(char* p);
ClassHeap *m_pClassHeap;
};

To do that we first load the file in memory and then call the ParseClass method which is shown
in the next section.

Class Loader


As there are some variable length fields it is not possible to directly load the structure. So we load the
values one by one. First we load the value of magic which is an unsigned integer (u4) value. It must be value
of 0xCafeBabe. If it is not the class file may be either corrupted or not a java class file at all. Then we
load other values and structures. To load structures we first load the count and then load the structure.
For example first we load short (u2) value constant_pool_count and then load that number of constant pool.
To parse I used definitions getu4(p) or similar which just picks 4 bytes starting at p and returns the unsigned
int value. To parse structures hare use sepaate methods like ParseConstantPool.
It takes the reference of the byte stream pointer and increments in that class. I did this only for simplicity.
It would be more readable if I'd return the total length and increent in ParseClass method but that would me less managable.


BOOL JavaClass::ParseClass(void )
{
//just to be safe
if (m_pByteCode==NULL ||
m_nByteCodeLength < sizeof (JavaClassFileFormat)+20)
return FALSE;
char *p=( char *)m_pByteCode;
magic = getu4(p); p+=4;
ASSERT(magic == 0xCAFEBABE);

if(magic != 0xCAFEBABE)
return FALSE;

minor_version=getu2(p); p+=2;
major_version=getu2(p); p+=2;
constant_pool_count=getu2(p); p+=2;

if (constant_pool_count>0)
ParseConstantPool(p);

access_flags=getu2(p); p+=2;
this_class=getu2(p); p+=2;
super_class=getu2(p); p+=2;
interfaces_count=getu2(p); p+=2;

if (interfaces_count>0)
ParseInterfaces(p);

fields_count=getu2(p); p+=2;

if (fields_count > 0)
ParseFields(p);

methods_count = getu2(p);p+=2;

if (methods_count > 0)
{
ParseMethods(p);
}
attributes_count = getu2(p);p+=2;

if (attributes_count > 0)
ParseAttributes(p);

return 0;
}

Constant Pool


In a java class several constant value is stored. It stores numeric, string and reference values
in a pool and those are used in machine codes known as 'Java Byte Code'. Constant pool contains
constant_pool_count items in a sequential list of following structure.


struct cp_info
{
u1 tag;
u1* info;
};

Constant pool information structure starts with one byte of tag information that
indicates the type of the constant pool. Constant pool structure length is variable
depending on the type of constant. Constant pool tag value can be one of the
following values depending on the type of constant.


#define CONSTANT_Integer 3
#define CONSTANT_Float 4
#define CONSTANT_Long 5
#define CONSTANT_Double 6
#define CONSTANT_Utf8 1
#define CONSTANT_String 8

#define CONSTANT_Class 7
#define CONSTANT_Fieldref 9
#define CONSTANT_Methodref 10
#define CONSTANT_InterfaceMethodref 11
#define CONSTANT_NameAndType 12

Depending on the value of tag we can cast the cp_info structure in more precise
structures listed here.

CONSTANT_Integer_info


If tag value equals CONSTANT_Integer in cp_info structure then it is an integer
constant. We can cast the cp_info structure in following structure.


struct CONSTANT_Integer_info {
u1 tag;
u4 bytes;
};

This structure does not have any reference to any other constant. It represents
direct 4 byte integer value.

CONSTANT_Float_info


If tag value equals CONSTANT_Float in cp_info structure then it is a float
constant. We can cast the cp_info structure in following structure.


struct CONSTANT_Float_info {
u1 tag;
u4 bytes;
};

It is a direct value constant without any reference.

CONSTANT_Long_info


If tag value equals CONSTANT_Long in cp_info structure then it is a long
constant. We can cast the cp_info structure in following structure.


struct CONSTANT_Long_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
};

It is a direct value constant without any reference. It uses two four bute values
to construct the 8 byte long value.

CONSTANT_Long_info


If tag value equals CONSTANT_Double in cp_info structure then it is a double
constant. We can cast the cp_info structure in following structure.


struct CONSTANT_Double_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
};

It is a direct value constant without any reference. It uses two four bute values
to construct the 8 byte double value.

CONSTANT_Utf8_info


If tag value equals CONSTANT_Utf8 in cp_info structure then it is a utf8 string
constant. We can cast the cp_info structure in following structure.


struct CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1* bytes;//[length];
};

It is a direct value constant without any reference. The short value length defines
the length of the byte array followed by length number of bytes.

CONSTANT_String_info


If tag value equals CONSTANT_String in cp_info structure then it is a string reference
constant. We can cast the cp_info structure in following structure.


struct CONSTANT_String_info {
u1 tag;
u2 string_index;
};

It is a reference value constant. The short value string_index refers
to a CONSTANT_Utf8_info index in the constant pool.

CONSTANT_Class_info


If tag value equals CONSTANT_Class in cp_info structure then it is a class reference
constant. We can cast the cp_info structure in following structure.


struct CONSTANT_Class_info {
u1 tag;
u2 name_index;
};

It is a reference value constant. The short value name_index refers
to a CONSTANT_Utf8_info index in the constant pool that is the fully
qualified name (ie java/lang/String) of the class- dot replaced by
slash.

CONSTANT_Fieldref_info


If tag value equals CONSTANT_Fieldref in cp_info structure then
it is a field reference constant. We can cast the cp_info structure
in following structure.


struct CONSTANT_Fieldref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
};

It is a reference value constant. The short value class_index refers
to a CONSTANT_Class_info index in the constant pool and
name_and_type_index refers to a string index in the constant pool that
is the fully qualified name (ie java/lang/String@valueOf(F)Ljava/lang/String;)
of the class- dot replaced by slash.

CONSTANT_Methodref_info


If tag value equals CONSTANT_Methodref in cp_info structure then
it is a method reference constant. We can cast the cp_info structure
in following structure.


struct CONSTANT_Methodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
};

It is a reference value constant. The short value class_index refers
to a CONSTANT_Class_info index in the constant pool and
name_and_type_index refers to a string index in the constant pool that
is the fully qualified name (ie java/lang/String@valueOf(F)Ljava/lang/String;)
of the class- dot replaced by slash.

CONSTANT_InterfaceMethodref_info


If tag value equals CONSTANT_InterfaceMethodref in cp_info structure then
it is an interface method reference constant. We can cast the cp_info structure
in following structure.


struct CONSTANT_InterfaceMethodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
};

It is a reference value constant. The short value class_index refers
to a CONSTANT_Class_info index in the constant pool and
name_and_type_index refers to a string index in the constant pool that
is the fully qualified name (ie java/lang/String@valueOf(F)Ljava/lang/String;)
of the class- dot replaced by slash.

CONSTANT_NameAndType_info


If tag value equals CONSTANT_NameAndType in cp_info structure then
it is an interface method reference constant. We can cast the cp_info structure
in following structure.


struct CONSTANT_NameAndType_info {
u1 tag;
u2 name_index;
u2 descriptor_index;
};

It is a reference value constant. The short value name_index refers
to a string index in the constant pool and descriptor_index refers to another
string index in the constant pool.

Parsing constant pool


Here we set the values of constant pool list pointers. When we need to retrieve the
actual value we look at the tag and pick the value directly.


BOOL JavaClass::ParseConstantPool(char* &p)
{
constant_pool = new cp_info*[constant_pool_count-1];
if(constant_pool == NULL) return FALSE;
for(int i=1;i<constant_pool_count;i++)
{
//We set the constant pointer here
constant_pool[i]=(cp_info*)p;

//We now calculate constant size. If it is an integer we get size = 5
int size = GetConstantPoolSize(p);
p+= size;

// If constant type is long or double constant pool takes two entries. Second entry
// is not used by virtual machine but kept NULL to walk constant pool correctly.
if(constant_pool[i]->tag == CONSTANT_Long || constant_pool[i]->tag == CONSTANT_Double)
{
constant_pool[i+1]=NULL;
i++;
}
}
return TRUE;
}

Interfaces


In the interfaces field of a class there are interfaces_count
number of short (u2) values. Each value is a reference to a constant pool entry
of type CONSTANT_Class. We parse them and store in our in-memory object-


BOOL JavaClass::ParseInterfaces(char* &p)
{
interfaces = new u2[interfaces_count];
for(int i=0;i<interfaces_count;i++)
{
interfaces[i] = getu2(p); p+=2;
}

return TRUE;
}

Fields


A class may contain zero, one or more fields. The actual number is stored in the
fields_count field. A list of field_info structure followes
this value.


struct field_info
{
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info* attributes;//[attributes_count];
};

The short value access_flags describes the allowed field access. Here is
the possible access flags values which is shared also by methods and classes-


#define ACC_PUBLIC 0x0001
/*Declared public; may be accessed from outside its package. */

#define ACC_PRIVATE 0x0002
/*Declared private; accessible only within the defining class. */

#define ACC_PROTECTED 0x0004
/*Declared protected; may be accessed within subclasses. */

#define ACC_STATIC 0x0008 /*Declared static. */

#define ACC_FINAL 0x0010
/*Declared final; may not be overridden. */

#define ACC_SYNCHRONIZED 0x0020
/*Declared synchronized; invocation is wrapped in a monitor lock. */

#define ACC_NATIVE 0x0100
/*Declared native; implemented in a language other than Java. */

#define ACC_ABSTRACT 0x0400
/*Declared abstract; no implementation is provided. */

#define ACC_STRICT 0x0800
/*Declared strictfp; floating-point mode is FP-strict */

The name_index and descriptor_index are reference to two
constant pool of type utf8 string. The attributes field defined the
attributes of the field. Attributes are described later. Here is how we parse fields
in a class's raw bytes-


BOOL JavaClass::ParseFields(char* &p)
{
fields = new field_info_ex[fields_count];
if(fields == NULL) return FALSE;

for(int i=0;i<fields_count;i++)
{
fields[i].pFieldInfoBase = (field_info*)p;

fields[i].access_flags= getu2(p); p+=2; //access_flags
fields[i].name_index= getu2(p);p+=2; //
fields[i].descriptor_index= getu2(p);p+=2; //
fields[i].attributes_count=getu2(p); p+=2;

if(fields[i].attributes_count>0)
{
//skip attributes - we do not need in simple cases
for(int a=0;a<fields[i].attributes_count;a++)
{
u2 name_index=getu2(p); p+=2;
//printf("Attribute name index = %dn", name_index);
u4 len=getu4(p);p+=4;
p+=len;
}
}
}
return TRUE;
}

Methods


A java class file may contain arbitrary number of methods. The count is stored in
methods_count member of class file structure. As it is a two byte field
the theoritical upper limit is essentially 2^16. Like fields info, method info structure
contains access flags, name index, descriptor index, and attributes.


struct method_info
{
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info* attributes;//[attributes_count];
};

Method body (if any) is stored in an attribute named Code- which contains the actual
'Java Byte Code'. Here is how we parse methods in out virtual machine-


//TODO: Cashe the findings here
BOOL JavaClass::ParseMethods(char* &p)
{
methods = new method_info_ex[methods_count];

if(methods == NULL) return FALSE;

for(int i=0;i<methods_count;i++)
{
//methods[i] = new method_info_ex;
methods[i].pMethodInfoBase=(method_info*)p;
methods[i].access_flags= getu2(p); p+=2; //access_flags
methods[i].name_index = getu2(p); p+=2; //name_index
methods[i].descriptor_index= getu2(p); p+=2; //descriptor_index
methods[i].attributes_count=getu2(p); p+=2;

CString strName, strDesc;
GetStringFromConstPool(methods[i].name_index, strName);
GetStringFromConstPool(methods[i].descriptor_index, strDesc);

TRACE(_T("Method = %s%sn"),strName, strDesc);

TRACE("Method has total %d attributesn",methods[i].attributes_count);

methods[i].pCode_attr=NULL;
if(methods[i].attributes_count>0)
{
//skip attributes
for(int a=0;a<methods[i].attributes_count;a++)
{
u2 name_index=getu2(p); p+=2;

TRACE("Attribute name index = %dn", name_index);
u4 len=getu4(p);p+=4;
p+=len;
}

methods[i].pCode_attr = new Code_attribute;
ParseMethodCodeAttribute(i, methods[i].pCode_attr);
}
}

return TRUE;
}

In case of method structure ( and also same in fields structure) I have used method_info_ex instead
of method_info structure. This extended structure on pointer to pint to raw
method info in the in-memory bytestream of class file. Here with other fields we parse the Code attribute.
The details id given later in attributes section.

Attributes


In most classes attributes takes most of the space in file. Class has attributes, method has attributes,
field has attributes. The raw definition of attribute is like this-


struct attribute_info
{
u2 attribute_name_index;
u4 attribute_length;
u1* info;//[attribute_length];
};

The attribute_name_index field is the reference index in the constant pool
for string type constant. The attribute_length field is the length of info field-
which is another structure depending on the type/ name of the attribute. An arrtibute can be
a constant value, exception table or code type.

Constant value attribute




struct ConstantValue_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 constantvalue_index;
};

Code Attribute


It is also a method specific attribute. The name of the attribute is hardcoded as 'Code'.
This attribute has maximum stack and maximum local values of the method. The code
field is variable length defined by code_length and it contains the actual
'Java Byte Code'.


struct Code_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 max_stack;
u2 max_locals;
u4 code_length;
u1* code;//[code_length];
u2 exception_table_length;
Exception_table* exception_table;//[exception_table_length];
u2 attributes_count;
attribute_info* attributes;//[attributes_count];
};

Exception table structure


This structure is used to define the exception table for methods. The exception
table describes the exception handler depending on the program counter value or
offset of byte code. The handler code is also an offset in the byte code.


struct Exception_table
{
u2 start_pc;
u2 end_pc;
u2 handler_pc;
u2 catch_type;
};

The field catch_type is a reference to a constant pool entry that describes the type
of the exception- for example reference to a class named 'java/lang/Exception'.

Java Instruction Set


Java has more than 200 instructions. The java language file, when compiled, is converted
to a class file that contains intrtuctions as byte codes. If we have a method like this-


public int mul(int a, int b)
{
return a * b;
}

we will get this method in byte code attribute like this- (java has also assembly like
representation for instructions to represent byte codes in human readable format)


Code Attribute:
Stack=2, Locals=3, Args_size=3, Code Length = 4
Code:
0: iload_1
1: iload_2
2: imul
3: ireturn

Here if we follow the instructions we go like this:


0: Push (load) the local variable 1 on stack
1: Push the local variable 2 on stack
3: Pop two values from stack, do an integer multipucation and push the result
4: Return the integer value from stack top.

What we need to do in our virtual machine is load classes and follow the instructions
in methods. There are methods to create new objects, to call methods of object. It is also
possible to call native methods from a java method. Please refer to source code
for most other codes (opcodes.h) or Java Virtual Machine Specification for a complete list.

Class heap


In the virtual machine we must maintain a heap where the class definition objects
can be stored. I have implemented it as a separate heap for simplity. In this heap
we load classes from files and store it in the heap. The ClassHeap class
is responsible for maintaining the class heap in meory.


class ClassHeap
{
CMapStringToPtr m_ClassMap;
FilePathManager *pFilePathManager;
public:
ClassHeap(void);
public:
virtual ~ClassHeap(void);
public:
BOOL AddClass(JavaClass* pJavaClass);
JavaClass* GetClass(CString strClassName);
BOOL LoadClass(CString strClassName, JavaClass *pClass);

};

We store JavaClass objects pointer in the m_ClassMap member
using the class name as key.

Object heap


Object heap is virtual machine's RAM. All objects are created on object heap and its
reference can be stored in another object or on the stack. Any reference is stored in
an union type storage named Variable. Any field of a class can be
represented using variable object. Anything can be stored in a Variable
object.


union Variable
{
u1 charValue;
u2 shortValue;
u4 intValue;
f4 floatValue;
LONG_PTR ptrValue;
Object object;
};

Object creation on heap is described later in detail.

Virtual Machine Stack


Java instruction set is designed in such a way that it can work with very limited set
of registers. Instead it uses its stack very extensively. The JVM stack element is one item
regardless of - it may be primitive ype or object type. Only long and double
type takes two stack spaces. The 'Execution Engine' maintains the JVM stack. For example when the execution
engine executes iadd instruction it pops two numeric values from the stack and
add the values and pushes the result on the stack. The virtual machine has empty stack initially and
the stack is populated after the initial thread and the initial method is started for execution. Each
method instruction is allowed to operate on stack within a limited boundary of the stack. The compiler
sets the upper limit (top) as max_stack field of each methods Code attribute. The
lower limit is the top+1 stack position of previous method. This boundary of stack is named as Stack Frame
of that method.

The Stack Frame


As we mentioned each methods boundary in the JVM stack is known as 'Stack Frame'. Each stack frame reserves
positions for the parameters and local variables of that method. If it is not a static method the first parameter
is the object reference ( the this parameter) of type of the class of the method. The Execution Engine
operates between the frame and when the method returns a value it pops every element from the current frame including
this reference and pushes the return value (if method is not void return type) value
on the frame of previous frames top. To keep the implementation simple I used slightly different methodology. I used
stack of stack. Each frame is stack type and it is pushed on the JVM stack. The stack grows on method invocation
and shrinks on method return.

Local Variables


In a stack frame local variables takes positions from zero to max_locals - 1
positions or less. If the method is not static the object takes the position zero and other
locals follow it. Local variables are accessed using putfield and getfield
instructions.

Native method stack


Unlike virtual machine stack, native methods stack is not maintained by JVM. It is
maintained by the native system. Actually while a native method is being executed the
virtual machine component that was managing the java thread waits until the native
method completes and returns.

Runtime Environment


Each java thread has its own frame stack. All java threads in a process share common class heap
and object heap. This things are bundled together in a RuntimeEnvironment object and
carried among the execution engine components.


class RuntimeEnvironment
{
public:
Frame *pFrameStack;
ClassHeap *pClassHeap;
ObjectHeap *pObjectHeap;
};

Execution Unit


This is the main module of the JVM. It interprates the instructons. Advanced JVMs may
use JIT compiler to convert java instructions into native instruction. But I did not do
that because of the complexity of the JIT compiler.
When a JVM starts it usually takes initial class name as parameter. Our JVM also takes
class name as a parameter. The class heap is then requested to load that class. Then the
JVM finds its main method (it can be any name like Entry in case of my first implementation),
creates the initial stack frame and requests the execution engine to start execution. The
heart of the Execution Unit is the Execute method. Here is the skeleton:


u4 ExecutionEngine::Execute(Frame* pFrameStack)
{
ASSERT(pFrameStack);
ASSERT(pFrame);

Frame* pFrame=&pFrameStack[0];

DbgPrint(_T("Current Frame %ld Stack start at %ldn"),
pFrame-Frame::pBaseFrame, pFrame->stack-Frame::pOpStack );

if(pFrame->pMethod->access_flags & ACC_NATIVE)
{
ExecuteNativeMethod(pFrame);
return 0;
}

u1 *bc=pFrame->pMethod->pCode_attr->code + pFrame->pc;

i4 error=0;
JavaClass *pClass = pFrame->pClass;

CString strMethod;
pClass->GetStringFromConstPool(pFrame->pMethod->name_index, strMethod);

DbgPrint(_T("Execute At Class %s Method %s n"), pClass->GetName(), strMethod);
i4 index=0;
i8 longVal;
while(1)
{
switch(bc[pFrame->pc])
{
case nop: //Do nothing
pFrame->pc++;
break;

//Integer Arithmetic
case iadd: //96 : Pop two int values from stack add them and push result
pFrame->stack[pFrame->sp-1].intValue=pFrame->stack[pFrame->sp-1].intValue
+ pFrame->stack[pFrame->sp].intValue;
pFrame->sp--;
pFrame->pc++;
break;

//Method return instructions
case ireturn: //172 (0xac) : Pop everything from stack and push return value (int)
pFrame->stack[0].intValue=pFrame->stack[pFrame->sp].intValue;
return ireturn; // here we break the while loop
break;

// Method invokation Instructions

// Here actually we do a recursive call to Execute
// to keep things simple- after the java method return we
// also return from Execute- some memory waste for simplicity
case invokevirtual: //182: Invoke a virtual method.
// The object reference and parameters are on stack by java instructions
ExecuteInvoke(pFrame, invokevirtual);
pFrame->pc+=3;
break;
}

//Instructions that deal with objects

case _new:// 187 (0xbb)
ExecuteNew(pFrame);
pFrame->pc+=3;
break;
case putfield: //181 (0xb5): Set field in object from stack top
PutField(pFrame);
pFrame->sp-=2;
pFrame->pc+=3;
break;

case getfield: //180 (0xb4) Fetch field from object and push on stack
GetField(pFrame);
pFrame->pc+=3;
break;
}
return 0;
}

Creating object on Object Heap


An object is usually created by JVM when a new or newarray or
multinewarray instruction is executed. When a virtual machine creates an object it
first calculate the size of the object. To calculate the object size we we first take the
fields_count value in the class structure then we add its super classes
fields_count value with it then we add super classes super classes fields_count
and so on recursively until we reach the final base class java.lang.Object. This way we
calculate total fields of the object and add one with it that holds the class pointer
in the ClassHeap. Now we multiply the sizeof(Variable) to the count and
get number of bytes required for the object. We now allocate the required bytes and
return the pointer to that memory in a Variable object on the stack top. Here is
the implementation.


int ExecutionEngine::ExecuteNew(Frame* pFrame)
{
pFrame->sp++;
u1 *bc=pFrame->pMethod->pCode_attr->code;
u2 index=getu2(&bc[pFrame->pc+1]);
if(!pFrame->pClass->CreateObject(index, this->pObjectHeap, pFrame->stack[pFrame->sp].object))
return -1;
return 0;
}

BOOL JavaClass::CreateObject(u2 index, ObjectHeap *pObjectHeap, Object& object)
{
char *cp=(char*)this->constant_pool[index];
ASSERT(cp[0] == CONSTANT_Class);
ASSERT(pObjectHeap);
if(cp[0] != CONSTANT_Class)
return FALSE;

u2 name_index=getu2(&cp[1]);
CString strClassName;
if(!this->GetStringFromConstPool(name_index, strClassName))
return FALSE;

JavaClass *pNewClass=this->m_pClassHeap->GetClass(strClassName);
if(pNewClass == NULL) return FALSE;
object=pObjectHeap->CreateObject(pNewClass);
return TRUE;
}

Setting or getting value in object


The putfield instruction is used to set a value (from stack) of a field and
the getfield instruction is used to load a variables value on the
stack. When the execution engine needs to execute a getfield
instruction it pops two values from the stack. One value is the object pointer
another is field position (zero based index). Here is my implementation:


// Gets value or reference from stack and set in object
void ExecutionEngine::PutField(Frame* pFrameStack)
{
u2 nIndex = getu2(&pFrameStack[0].pMethod->pCode_attr->code[pFrameStack[0].pc+1]);
Variable obj=pFrameStack[0].stack[pFrameStack[0].sp-1];
Variable value=pFrameStack[0].stack[pFrameStack[0].sp];
Variable *pVarList=this->pObjectHeap->GetObjectPointer(obj.object);
pVarList[nIndex+1]=value;
}

//Gets the value from variable and push on stack
void ExecutionEngine::GetField(Frame* pFrame)
{
//TODO: Bug check for long and double
u2 nIndex = getu2(&pFrame->pMethod->pCode_attr->code[pFrame->pc+1]);
Variable obj=pFrame->stack[pFrame->sp];
Variable *pVarList=this->pObjectHeap->GetObjectPointer(obj.object);
pFrame->stack[pFrame->sp]=pVarList[nIndex+1];
}

Invoking method


When execution engine requires a method invocation it needs to creates a new 'Stack Frame' and
the pc or Program Counter is set to the first byte of the methods byte code. Before that,
the execution engine must save the current methods pc so that it can come back to
resume method execution after the callee method returns. Here is our implementation. Please note how
we handle static method invocation - simply we do not have the this reference on our stack.


void ExecutionEngine::ExecuteInvoke(Frame* pFrameStack, u2 type)
{
u2 mi=getu2(&pFrameStack[0].pMethod->pCode_attr->code[pFrameStack[0].pc+1]);
Variable objectRef = pFrameStack[0].stack[pFrameStack[0].sp];
char *pConstPool = (char *)pFrameStack[0].pClass->constant_pool[mi];

ASSERT(pConstPool[0] == CONSTANT_Methodref);

u2 classIndex = getu2(&pConstPool[1]);
u2 nameAndTypeIndex = getu2(&pConstPool[3]);

//get class at pool index
pConstPool = (char *)pFrameStack[0].pClass->constant_pool[classIndex];

ASSERT(pConstPool[0] == CONSTANT_Class);

u2 ni=getu2(&pConstPool[1]);

CString strClassName;
pFrameStack[0].pClass->GetStringFromConstPool(ni, strClassName);

JavaClass *pClass=pClassHeap->GetClass(strClassName);
pConstPool = (char *)pFrameStack[0].pClass->constant_pool[nameAndTypeIndex];

ASSERT(pConstPool[0] == CONSTANT_NameAndType);

method_info_ex method;
method.name_index = getu2(&pConstPool[1]);
method.descriptor_index = getu2(&pConstPool[3]);
method.access_flags = 0; // set later

CString strName, strDesc;
pFrameStack[0].pClass->GetStringFromConstPool(method.name_index, strName);
pFrameStack[0].pClass->GetStringFromConstPool(method.descriptor_index, strDesc);

JavaClass *pVirtualClass=pClass;
int nIndex=pClass->GetMethodIndex(strName, strDesc, pVirtualClass);

memset(&pFrameStack[1],0,sizeof(pFrameStack[1]));
pFrameStack[1].pMethod = &pClass->methods[nIndex];

method.access_flags = getu2((char *)pFrameStack[1].pMethod);
if( ACC_SUPER & method.access_flags)
{
pFrameStack[1].pClass = pVirtualClass->GetSuperClass();
}
else
{
pFrameStack[1].pClass=pVirtualClass;
}

int params=GetMethodParametersStackCount(strDesc)+1;

//invokestatic - there is no this pointer
if(type==invokestatic) params--;
// else invokevirtual has this pointer

int nDiscardStack =params;
if(pFrameStack[1].pMethod->access_flags & ACC_NATIVE)
{
}
else
{
nDiscardStack+=pFrameStack[1].pMethod->pCode_attr->max_locals;
}

pFrameStack[1].stack =
&Frame::pOpStack[pFrameStack->stack-Frame::pOpStack+pFrameStack[0].sp-params+1];
pFrameStack[1].sp=nDiscardStack-1;

this->Execute(&pFrameStack[1]);

//if returns then get on stack
if(strDesc.Find(_T(")V")) < 0)
{
nDiscardStack--;
}
//Before we return to caller make the stack of caller right
pFrameStack[0].sp-=nDiscardStack;
}

Invoking native method


In a java class a method may be marked as native-


public class Test
{
public native void Print(string message);
}

In byte code ACC_NATIVE is set in the access_flags field of the
method_info structure. We decide here like this:


if(pFrame->pMethod->access_flags & ACC_NATIVE)
{
ExecuteNativeMethod(pFrame);
return 0;
}

Each native method usually has a fixed predefined prototype. Here is the type definitation for our
JVM:


typedef Variable (*pNativeMethod)(RuntimeEnvironment* pRuntimeEnvironment);

Here is how we handle native methods in the JVM:


u4 ExecutionEngine::ExecuteNativeMethod(Frame* pFrameStack)
{
ASSERT(pFrameStack);

ASSERT(pFrame->pMethod->access_flags & ACC_NATIVE);
Frame* pFrame=&pFrameStack[0];

JavaClass *pClass = pFrame->pClass;
CString strClassName, strMethod, strDesc, strSignature;
strClassName=pClass->GetName();
pClass->GetStringFromConstPool(pFrame->pMethod->name_index, strMethod);
pClass->GetStringFromConstPool(pFrame->pMethod->descriptor_index, strDesc);
DbgPrint(_T("Execute At Class %s Method %s%s n"),strClassName , strMethod, strDesc);
strSignature=strClassName+_T("@")+strMethod+strDesc;
pNativeMethod pNativeMethod=GetNativeMethod(strSignature);
RuntimeEnvironment rte;
rte.pFrameStack=pFrameStack;
rte.pClassHeap= pClassHeap;
rte.pObjectHeap= pObjectHeap;

if(pNativeMethod == NULL)
{
// what should I do here??
// System Panic??
ASSERT(FALSE);
return -1;
}
else
{
//Here we go native
Variable retVal = pNativeMethod(&rte);

//if returns then get on stack
if(strDesc.Find(_T(")V")) < 0)
{
pFrame->stack[0]=retVal;
}
}
return 0;
}

Here is the implementation of Print method of Test class:


//Signature: _T("Test@Print(Ljava/lang/String;)V")
Variable Print(RuntimeEnvironment* pRuntimeEnvironment)
{
Variable returnVal;
Frame *pFrame=&pRuntimeEnvironment->pFrameStack[0];
Object object=pFrame->stack[pFrame->sp].object;
Variable *pVar=pRuntimeEnvironment->pObjectHeap->GetObjectPointer(object);
if(pVar)
{
CString *pString = (CString *)pVar[1].ptrValue;
if(pString) wprintf(_T("%s"),*pString);
}

returnVal.intValue=0;
return returnVal;
}

It is the native methods responsibility to correctly operate on stack. The java instruction is
here out of scope. Everything is running on real machine here.
So, here we pop the string type object reference from stack, convert it to CString
object and do our native printing to console. This way we can handle any native operation like
creating new window, drawing or do network operation. All these things are done in the
implementation of Morpheus project.

The Garbage Collector


Java language does not have memory release mechanism. So the JVM must take the responsibility to release
memory when some objects are out of scope no longer required or referenced by the application. To do this
the JVM may take a number of strategy like reference count, mark and sweep etc. I used mark and sweep method
because of its simplicily and accuracy. We start from the stack. We mark each object that is being referenced
from the stack references. Then we mark all the objects that is being referenced by the marked objects and so
on recursively. After the mark operation we know which objects are connected and which are out of scope. Then
we take each object one by one and release its memory from heap. Before that we must call the finalize
method of that object to do any cleanup required by the object itself programetically.

Conclusion


Thats all about to say for now about how we can implement a simple JVM. The JVM I present here is very
limited implementation - though most of the java instructions are supported. It lacks heavily
for library and native interface. Please look at the seminar
presentation downloadable above for a visual description of JVM and how instructions are executed in
a JVM. Full screen view of the presentation is best viewed with spacebar to go to next slide.
I am busy with the
implementation of Morpheus project at my free time
and wish to come with that as well as with a new JVM with a new Window subsystem implementation having
Windows Vista and Office 2007 look and feel (ok, ok, thats not technically a big deal but good to have
for user interface degign) and oh yes also with a .NET Virtual Execution System with Morpheus for .NET
support also. The .NET VES is similar except the complex onfile structure is really painfull when I
decode them. All these things I do because I love to do- so I do not try to make a zero bug system.
I leave when it seems to work for 'Hello World ++' applications.



This article is also published in Codeproject. Click here to view.

Kaza Coralina


We had our annual picnic (codenamed "kaza coralina") this year at the St. Martin island. A huge surge of techies in a coral island must have distorted local space time structure. But whatever distortion it was, it must have been positive for humans. We had a grand time!

Tuesday, January 22, 2008

Kaza Coralina

Coral hunt in the St. Martin Island for all the long "party-less work, work and work" suffering Kazites. At last the fun is back! Big decisions have to be made (should we have bbq by the sea or take a cook with us to cook us biriyani?) and big sacrifices have to be made (should I miss the beautiful traffic jams in Dhaka?).

kazacoralina.JPG