<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RasterGrid Blog &#187; byte code</title>
	<atom:link href="http://rastergrid.com/blog/tag/byte-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://rastergrid.com/blog</link>
	<description>A technical blog from Daniel Rákos (aka aqnuep)</description>
	<lastBuildDate>Tue, 24 Aug 2010 19:34:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Never seen flexibility for Delphi</title>
		<link>http://rastergrid.com/blog/2010/01/never-seen-flexibility-for-delphi/</link>
		<comments>http://rastergrid.com/blog/2010/01/never-seen-flexibility-for-delphi/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 18:53:30 +0000</pubDate>
		<dc:creator>Daniel Rákos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[byte code]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Pascal]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://rastergrid.com/blog/?p=95</guid>
		<description><![CDATA[

As far as I can tell people still prefer using python for scripting in their Delphi projects. This is probably because python scripting in Delphi has a long history and it is a proven method with easy integration. What if I would tell you that there is a free solution which enables you to write [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frastergrid.com%252Fblog%252F2010%252F01%252Fnever-seen-flexibility-for-delphi%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F5saVqb%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Never%20seen%20flexibility%20for%20Delphi%22%20%7D);"></div>
<p>As far as I can tell people still prefer using python for scripting in their Delphi projects. This is probably because python scripting in Delphi has a long history and it is a proven method with easy integration. What if I would tell you that there is a free solution which enables you to write also your scripts in pascal? A few years ago I discovered an alternative that makes this possible. This highly influenced my vision about the importance of scripting. Since that I accompanied all my hobby projects with full scripting support.</p>
<p><span id="more-95"></span></p>
<p><img title="More..." src="http://rastergrid.com/blog/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt="" />The first question of the reader might be that why I would need a script language in my program? It is, however, very easy to answer this question. The presence of some sort of scripting support in a software product has many benefits both from developer and customer point of view:</p>
<ul>
<li>Less wasted time on software building by customizing application without the need for recompilation.</li>
<li>Unusually flexible reflection capabilities by creating script code on-the-fly with string manipulation.</li>
<li>Very flexible debugging possibilities by modifying executed code during the debug session.</li>
<li>Easy separation of often changing high level logic and rarely changing internal framework.</li>
<li>Powerful tool for the user to customize application for own needs.</li>
</ul>
<p>These are just a few examples what additions a scripting engine can give. Personally my favorite one is the second mentioned example in the list above. As a result that I&#8217;ve worked as a PHP developer for over a year I really got used to the ability to compose code on-the-fly by just putting some strings together. It is a built-in feature of PHP and most other interpreted languages, however it is fairly uncommon that such functionality exists in native programming languages. I knew that to emulate the presence of this facility I have to use some scripting language but I never thought it will be so easy to find one which suits my needs.</p>
<p>First, I was looking around to find out what other Delphi developers use in their projects. That&#8217;s how I first met with python. The language itself has a very powerful OOP tool-set but the syntax was too extraneous for me. After checking tons of other scripting languages out there which had mostly no easy integration possibilities with Delphi, finally I found RemObjects Pascal Script.</p>
<h2>Pascal Script for Delphi</h2>
<p>First, I just said &#8220;Wow! This is exactly what I was looking for&#8221;. It not just enables you to write your scripts using the language what you also use to develop the application itself but it also provides incredibly easy integration with Delphi. Just to mention a few features of Pascal Script:</p>
<ul>
<li>Calling Pascal Script code from Delphi code or vice versa.</li>
<li>Using external DLLs from within the script without requiring any type of function header.</li>
<li>Sharing any kind of data between the script and the Delphi code in both direction.</li>
</ul>
<p>For a full list of features please refer to the <a title="RemObjects Pascal Script" href="http://www.remobjects.com/ps.aspx" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.remobjects.com/ps.aspx?referer=');">official site</a> of RemObjects Pascal Script.</p>
<p>One thing that most Delphi developers got used to is that most of the third-party components and tools available for Delphi are with a commercial licensing. Well, this is not the case with Pascal Script. It is not just freely available but also comes with full source code. This further increases the potentials of it as you can add new features to it if you wish. However, it is already powerful enough for most use cases.</p>
<h3>How it works?</h3>
<p>In a nutshell, using Pascal Script is piece of cake. You just have to put together your source code string, compile it and then execute. Yes, Pascal Script needs a compilation phase which produces a byte code as an intermediate format. By telling this, you&#8217;ve probably already figured out that Pascal Script is not just very flexible but also provides adequate performance by not having to interpret directly the source code. Let&#8217;s start with a very simple script:</p>
<pre class="brush: delphi">{ some very simple pascal script }
begin
  PrintString('Hello World!');
end.</pre>
<p>This will call the procedure PrintString with the string parameter &#8220;Hello World!&#8221; . But where this procedure comes from? Well, actually you have to bind it using the run-time library that comes with Pascal Script. To execute this script having an actual Delphi procedure bound to the procedure name used in the script you&#8217;ll need some code similar to the following:</p>
<pre class="brush: delphi">program HelloWorldScript; { host application that executes the script }

uses
  uPSCompiler, uPSRuntime;

procedure PrintString(message: String);
begin
  writeln('Message from script: ', message);
end;

function LoadScript: String;
begin
  { load the script from some file and return it }
end;

var
  Compiler: TPSPascalCompiler;
  Exec: TPSExec;
  ByteCode: tbtString;
begin
  Compiler := TPSPascalCompiler.Create;  // create an instance of the compiler
  // register user procedure to compiler
  Compiler.AddDelphiFunction('procedure PrintString(message: String)');
  if Compiler.Compile(LoadScript) then  // compile the script to byte code
  begin
    Compiler.GetOutput(ByteCode);
    Exec := TPSExec.Create;  // create an instance of the executer
    // register user procedure to executer
    Exec.RegisterDelphiFunction(@PrintString, 'PRINTSTRING', cdRegister);
    if Exec.LoadData(ByteCode) then  // load byte code to executer
      Exec.RunScript;  // run the script
  end;
  Compiler.Free;
  Exec.Free;
end.</pre>
<p>If a newbie looks at this code, probably thinks that it&#8217;s overcomplicated. Well, actually this is true. Pascal Script needs some basic setup stuff but if you take the time to learn it, you will figure out that it does not involve too much code to use it. One thing that may still need some clarification is the registration of the user procedure provided to the script&#8230;</p>
<h3>Connecting the script to the Delphi code</h3>
<p>Pascal Script needs that all the procedures, functions, variables, types and classes that we would like to provide to the executing script needs to be registered both to the compiler and the executer. The compiler needs this information for syntax validation and to produce the byte code, that&#8217;s why it&#8217;s enough to provide the signature of the particular language element to it. The registration to the executer is which actually binds the concrete object to the script.</p>
<p>All these manual registrations looks like a pain in the a** at first sight, however, they are unavoidable. Fortunately, Pascal Script provides a very handy tool for us to automate this process, namely the UnitImporter. This tool can create an import unit based on an actual production code unit that will provide one parameterless procedure that registers all the classes, methods, etc. to the script compiler and run-time.</p>
<p>Beside this, Pascal Script provides ready baked units for the registration of the standard units of Delphi and a component set that integrates into the IDE, although I haven&#8217;t used them so far. Anyway, even if the framework seems to be a bit too complex and needs too much auxiliary code, in practice it is very easy to use it and most of the support code for scripting can be generated with the UnitImporter. If you don&#8217;t believe me, try using it for a while and you will see.</p>
<h3>Portability and usage limitations</h3>
<p>Maybe Pascal Script is not the Holy Grail of Delphi programmers but it is a very powerful scripting engine. The first question in those who are not developing on/for Windows platform can be that &#8220;Is it portable?&#8221;. This is another advantage of Pascal Script as it is as portable as Delphi can be. This means that it will work fine also with the Free Pascal compiler and it comes with the same component set for the Lazarus IDE as well.</p>
<p>So if not the portability and the ease of use then has it any disadvantages by the way? Unfortunately, yes. We&#8217;ve already seen that it has both-way interworking capability with Delphi, it has tools for automating the integration process and it can be used also for cross-platform projects, but it has small problem: it supports only a subset of the Delphi language so you cannot define classes in the scripts. You can still use classes and objects inside the script, but only those which were defined in the host Delphi application.</p>
<p>Anyway, this does not necessarily mean that Pascal Script is not flexible enough. I used it in my project mainly for the following purposes:</p>
<ul>
<li>Using the services provided to the script (classes, objects, etc.) to manipulate application behavior at run-time.</li>
<li>Handling Delphi events with procedures/functions implemented in scripts.</li>
<li>Baking a console that makes debugging tasks much easier by correcting errors while the program is running.</li>
</ul>
<p>Personally I don&#8217;t understand how Embarcadero Technologies (the current supplier of Delphi) haven&#8217;t integrated it into the core Delphi distribution as they did with many other popular frameworks like DUnit.</p>
<h2>Conclusion</h2>
<p>Pascal Script is probably not a script language that can have a future on it&#8217;s own but it is still a great addition to the feature set of Delphi as it provides a way to push the flexibility of the language above it&#8217;s limits with minimal effort. It is fast, robust and stable, and it offers most of the facilities that usual developers expect from a scripting language. Maybe I&#8217;m quite biased as I really enjoyed working with Pascal Script, but I think you Delphi programmers out there should definitely take a look at it and give it a try.</p>

]]></content:encoded>
			<wfw:commentRss>http://rastergrid.com/blog/2010/01/never-seen-flexibility-for-delphi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manage code yourself</title>
		<link>http://rastergrid.com/blog/2010/01/manage-code-yourself/</link>
		<comments>http://rastergrid.com/blog/2010/01/manage-code-yourself/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 16:41:25 +0000</pubDate>
		<dc:creator>Daniel Rákos</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[byte code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://rastergrid.com/blog/?p=10</guid>
		<description><![CDATA[

Those who know me know it well that I am not a big fan of languages which produce managed code. In this article I would like to cover the reasons behind my skepticism. Also I would like to dispel the myths around such languages and try to prove them with facts (we will see how [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frastergrid.com%252Fblog%252F2010%252F01%252Fmanage-code-yourself%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F4FH6No%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Manage%20code%20yourself%22%20%7D);"></div>
<p>Those who know me know it well that I am not a big fan of languages which produce managed code. In this article I would like to cover the reasons behind my skepticism. Also I would like to dispel the myths around such languages and try to prove them with facts (we will see how well I manage to achieve this). If you disagree with me, you&#8217;ll most probably hate me because of making this post but please, respect my personal point of view.</p>
<p><span id="more-10"></span>As I&#8217;ll mostly talk in this post in general about managed code I would like to emphasize that I have worthwhile experience only with Java and have barely used C# and other managed languages so my primary arguments maybe apply directly only to Java but still would like to present my arguments in general as they still apply in some way to all such languages.</p>
<p>Without the intension to insult anybody, I would like to start with a citation from one of my friends, who often says that &#8220;Java is just a pseudo-language&#8221;. Mostly I agree with him and it&#8217;s not just because I have my not so delightful experiences with the language but also because it introduced a brand new way of coding style that is a complete nightmare for an old-school coder like me. As I don&#8217;t want to start another religious war, I&#8217;ll stop my preposterous arguments here as I would like to prove my concerns with facts that nobody can disclaim.</p>
<blockquote><p>&#8220;&#8230; nowadays Java is almost as fast as native code&#8230;&#8221;</p></blockquote>
<p>This used to be a common myth in the last few years. For those who are still arguing by shouting this loud on forums and blogs, I would like to draw their attention to the fact that Java is <strong>still</strong> an interpreted language no matter if the latest JVM versions do some on-the-fly compilation of the code and as such it is inherently slower than any language that generates native code.</p>
<p>As my hobby is computer graphics I have to talk a bit about the case of PC games. Have anybody seen a game fully written in Java or any other interpreter based language that produces state-of-the-art graphics which competes with the best professional games and it also provides the same performance? I think not, and this is not a coincidence.</p>
<p>GPUs are getting more horsepower in a much faster pace than CPUs. That is what resulted in the batch crisis lately. It&#8217;s already happening for several years that the CPU is simply not powerful enough to feed the GPU with enough data in complex real-time rendering situations. There have been already tons of different software and hardware technologies which already targeted this issue a long time ago but as a perfect example that <strong>is</strong> the reason which resulted in the appearance of the completely redesigned DirectX 10 API and which emerged the need for a cleaner architecture for OpenGL as well. That&#8217;s why there are no advanced games written in interpreted languages as they waste those very valuable CPU cycles which would have great benefits otherwise.</p>
<blockquote><p>&#8220;&#8230; Java has a much greater expression power&#8230;&#8221;</p></blockquote>
<p>I tend to agree with this one. As these languages are interpreted by a virtual machine they provide great room for such features that would be impossible or simply very inefficient in a native language. That&#8217;s why one of my favorite programming language is PHP as an example. It is even more flexible then Java, for example, but never forget that PHP never meant to try to replace native languages as instead it is used in a totally different domain.</p>
<p>There are many language features that are not available in native languages but they weren&#8217;t left out unintentionally. Their lack is primarily justified by the fact that most of these features are rather inefficient and they simply not fit in the philosophy of a performance oriented language like C++. However, the most important ones like reflection, delegates and most OOP related facilities are present for example in Delphi, which is one if not the best OOP language that doesn&#8217;t make too much trade of between flexibility and run-time performance. Also, even if most of these features are not present in C++, Objective-C and other popular native languages, they can be made available with a little programming knowledge in the particular language (maybe not just little).</p>
<blockquote><p>&#8220;&#8230; you don&#8217;t have to care about resource deallocation as there is the garbage collector&#8230;&#8221;</p></blockquote>
<p>This is one of the most annoying &#8220;features&#8221; of managed code based languages. I understand that the prevention of memory leaks can have a great impact on development effort, however the unpredictable behavior of garbage collectors is simply unacceptable in certain situations. The issue of lost references has been already addressed in almost all native languages with the use of reference counted objects so I simply don&#8217;t get it why is this still an important feature. For efficient resource management you need much finer control over the time and way how resources are released.</p>
<blockquote><p>&#8220;&#8230; you just have to care about the basics and Java will make you the rest&#8230;&#8221;</p></blockquote>
<p>While I strongly support the idea of COTS, the way how Java and friends interprets this concept is simply unacceptable for me. Sometimes you need to know what is going on under-the-hood. This is basically the same issue like that of the garbage collector. Beside that, according to my personal taste, I don&#8217;t like if the compiler tells me how to do a particular thing. I rather do it in my way and even if I tend to agree that the compile time static analysis what the Java compiler does can be very valuable in some situations, simply denying code generation even in the case of a very minor semantic problem is simply makes me angry.</p>
<blockquote><p>&#8220;&#8230; Java eliminated almost all unsafe features inherited from C++&#8230;&#8221;</p></blockquote>
<p>This is one of the things that hurts me most. While Java really managed to achieve this, it sacrificed flexibility, ease of use and the fun of programming to make this possible. One such removed feature is operator overloading. I love them! As an example a linear algebraic library, which is often used in graphics programming, or the string library is not just makes the coding more natural, it eases the reading of code which is written with these in mind and that greatly effects the maintenance cost of a particular software as the code is read ten times more than modified. Java in this domain seems too verbose for me and it is much harder to read the long method invocation chains, that is not just used to be used by the developers, but is the recommended way how to code in Java.</p>
<p>This post already ran wildly long so I will close the topic for now. As a conclusion, I have to point it out that this article presented the subject from a rather subjective perspective at the end. Sorry for that, I will recap on the topic in a later post where I will try to be more detached and I&#8217;ll present actual use case examples but for now, this is all that came from my heart so please, don&#8217;t be very harsh with me if I was very biased. I hope you enjoyed the article anyway.</p>

]]></content:encoded>
			<wfw:commentRss>http://rastergrid.com/blog/2010/01/manage-code-yourself/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
