<?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>TURBU Tech &#187; Class Creation</title>
	<atom:link href="http://tech.turbu-rpg.com/category/delphi/class-creation-delphi/feed" rel="self" type="application/rss+xml" />
	<link>http://tech.turbu-rpg.com</link>
	<description>My thoughts on Delphi programming in general, and particularly on the technical aspects of developing the TURBU engine and editor.</description>
	<lastBuildDate>Fri, 27 Jan 2012 19:53:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>RTTI Generation code now available</title>
		<link>http://tech.turbu-rpg.com/296/rtti-generation-code-now-available</link>
		<comments>http://tech.turbu-rpg.com/296/rtti-generation-code-now-available#comments</comments>
		<pubDate>Tue, 08 Feb 2011 05:00:42 +0000</pubDate>
		<dc:creator>Mason Wheeler</dc:creator>
				<category><![CDATA[Class Creation]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[RTTI]]></category>

		<guid isPermaLink="false">http://tech.turbu-rpg.com/?p=296</guid>
		<description><![CDATA[Over the past couple weeks, I&#8217;ve been working on refining and testing my RTTI generation and the scripting system I&#8217;ve been building on top of it, which I&#8217;ve decided to call RTTI Script.  I think I&#8217;m finally starting to get something ready for public consumption.  I set up a Google Code repository this morning for [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past couple weeks, I&#8217;ve been working on refining and testing my RTTI generation and the scripting system I&#8217;ve been building on top of it, which I&#8217;ve decided to call RTTI Script.  I think I&#8217;m finally starting to get something ready for public consumption.  I set up a Google Code repository this morning for <a href="http://code.google.com/p/rtti-script/">RTTI Script</a>, but so far all it contains is the RTTI generation code.  The actual compiler and script executor still need some work.<span id="more-296"></span></p>
<p>I&#8217;m able to build and run some simple scripts, but I don&#8217;t have all the language features implemented yet, and I&#8217;m waiting on a bugfix for an issue in <a href="http://alex.ciobanu.org/?p=430">Collections</a> that&#8217;s causing trouble for me.  (A big thanks to Alex for putting up with all my bug reports and feature requests, BTW!)</p>
<p>RTTI generation probably sounds intimidating, but the framework for it all condenses down to about 1200 lines of code spread across 4 units.  There are a couple more minor things I still need to add to it, but it&#8217;s not all that complicated when you get down to it.  Here&#8217;s a quick overview of the four units and what they do:</p>
<p><strong>vmtStructure</strong>: A very simple unit that redefines the VMT of a class and several of the RTTI structures from TypInfo.pas in ways that are easier to work with.  A lot of it is based on <a href="http://hallvards.blogspot.com/2006/03/hack-8-explicit-vmt-calls.html">work by Hallvard Vassbotn</a>.</p>
<p><strong>vmtBuilder</strong>: This unit has four routines for creating parts of the VMT and RTTI tables.  The RTTI structures can be very fiddly; they pack data in ways that is not actually supported by the Object Pascal language, such as inline variable-length arrays.  Reading them (or writing them) requires a lot of pointer-based math, so I wrote up these routines to encapsulate the scary work.  Just pass in the data that the structures are supposed to contain, and they&#8217;ll arrange it for you.</p>
<p>The vmtBuilder unit also introduces an interface called IBuffer, which provides a simple way to pack arbitrary data together.  It&#8217;s mostly just for internal use by the other RTTI-creation routines.</p>
<p><strong>newClass</strong>: Here&#8217;s where the real fun begins.  The TNewClass class contains a bunch of methods for defining your new class itself.  You create the TNewClass, call the Add*Field methods (AddIntegerField, AddObjectField, etc.) in order, call AddMethod for each method, and then call the ClassPtr method, which takes all the information you&#8217;ve fed it and turns it into a new VMT and accompanying RTTI tables.  You can optionally pass a TPrivateHeap instance to ClassPtr, which will place the new data into the private heap instead of the standard application memory.  (I put that in because it&#8217;s very useful for dealing with an arcane but important detail of the RTTI system.)</p>
<p>TNewClass currently does not support creating properties, because they&#8217;re just syntactic sugar and I was focused on real functionality.  This will change once my script compiler gets to the point that it requires them.  It also doesn&#8217;t support creating classes that implement interfaces.  That would add a lot of complexity and I don&#8217;t think it&#8217;s worth the effort.  But the whole thing&#8217;s open source, so if someone needs it and feels like creating a patch for it, they&#8217;d be welcome to.</p>
<p><strong>rttiPackage</strong>: Here&#8217;s the part where I stop drawing on Hallvard&#8217;s work and instead owe a big debt of gratitude to Barry Kelly for explaining some of the trickier details.  If you only want to create a new class, you could stop with the three units above.  But if you want to integrate it into the RTTI system so you can get at its members, (which is basically the only way to use it since they aren&#8217;t available at compile time,) you need a way to feed all this new RTTI you&#8217;ve created into RTTI.pas.  That&#8217;s what the rttiPackage unit does.</p>
<p>It contains two new interfaces, INewUnit and IRttiPackage, and classes that implement them.  INewUnit can contain any number of TNewClass instances, and it organizes them so the RTTI system thinks they were all declared in the same unit.  Then IRttiPackage is the construct that pulls everything together.  It collects INewUnit instances and creates and registers a new TLibModule record, which makes RTTI.pas think you&#8217;ve loaded a new runtime package, so it loads the RTTI tables for the (fake) new package, containing the data for your new classes.</p>
<p>IRttiPackage.Install, the method that sets up all the data, can take an optional reference to a procedure that allows you to alter the code address of your new classes&#8217; methods at install time.  This was to support the script engine.  When the script compiler builds the metadata for the methods, the actual code address doesn&#8217;t exist yet because the RTTI hasn&#8217;t been loaded, so it sets the code address as the offset within the script.  (This will also make it a lot easier to save the script and load it later.)  Then when the RTTI table gets loaded as the script engine is starting up, it can use TRttiMethod.CreateImplementation to build a bridge between native Delphi land and the script engine, and set the implementation stub&#8217;s address to the official code address for the new method.  It&#8217;s kind of an ugly trick, but it works, and it&#8217;s the only way I could find to make it work.</p>
<p>I mentioned in my last post that the compiler will require Delphi XE because it uses this trick. I&#8217;ll have to revise that further.  There&#8217;s a glitch in the setup code for Method Implementations in XE RTM that can cause bogus FPU exceptions if you use it enough. This was fixed in XE Update 1, so that will be the minimum requirements for the script compiler when it&#8217;s ready.  The RTTI generation code itself, though, will work under D2010, if anyone has other uses for it.</p>
<p>More to come soon&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.turbu-rpg.com/296/rtti-generation-code-now-available/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Dynamic class creation: moving beyond the theoretical</title>
		<link>http://tech.turbu-rpg.com/284/dynamic-class-creation-moving-beyond-the-theoretical</link>
		<comments>http://tech.turbu-rpg.com/284/dynamic-class-creation-moving-beyond-the-theoretical#comments</comments>
		<pubDate>Sat, 22 Jan 2011 00:35:19 +0000</pubDate>
		<dc:creator>Mason Wheeler</dc:creator>
				<category><![CDATA[Class Creation]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[RTTI]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://tech.turbu-rpg.com/?p=284</guid>
		<description><![CDATA[A few years back, I ran across this post by Hallvard Vassbotn.  (It&#8217;s a shame he stopped blogging, because he always had some very interesting stuff about the technical details of how stuff in Delphi works.)  At the bottom was a paragraph that really fascinated me: On a more technical level it suffices to say [...]]]></description>
			<content:encoded><![CDATA[<p>A few years back, I ran across <a href="http://hallvards.blogspot.com/2007/05/hack17-virtual-class-variables-part-ii.html">this  post</a> by Hallvard Vassbotn.  (It&#8217;s a shame he stopped blogging, because he always had some very interesting stuff about the technical details of how stuff in Delphi works.)  At the bottom was a paragraph that really fascinated me:</p>
<blockquote><p>On a more technical level it suffices to say that they use custom and  extremely compact and fast data structures, tricks and hacks to be able  to represent millions and millions of objects within the constraints of a  32-bit Windows system. Throw in the use of Physical Address Extensions,  storing per-class information in &#8220;virtual&#8221; class vars to reduce object  instance size, creation of classes and their VMTs dynamically at runtime  (!!), pointer packing, multithreading, the list just goes on and on.</p></blockquote>
<p><span id="more-284"></span>I figured if Hallvard&#8217;s friend can create classes and their VMTs dynamically at runtime, then anyone could, with the right level of technical knowledge about how a VMT works.  My CodeRage presentation in 2009 was about theoretical research in that area, demonstrating that it could be done.  But then I just sorta sat on it.  It was a cool trick, but I didn&#8217;t have anything useful I could do with it, and  I had more important things to work on, like my game engine.</p>
<p>But a few months ago I started ran into some serious issues with making the scripting for my game engine work.  I&#8217;ve been using PascalScript, but its support for units is badly broken (and there&#8217;s no good way to fix it without basically rearchitecting the whole system) and its GOTO support is just broken enough to cause lots of trouble for me.  (I need to support GOTOs properly.  Don&#8217;t ask.) <img src='http://tech.turbu-rpg.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>So I looked at DWS, which has been getting a lot of hype lately with all the recent development.  But it also has bad unit support, no GOTOs at all, and no way to save a compiled script.  I raised each of these issues with Eric and he gave reasons why he has no intention to fix each of them.</p>
<p>There&#8217;s one interesting thing DWS can do, though: you can define new classes and use them in the script engine.  Being able to do this in my game engine would allow for a lot of customization opportunities.  But a bit of testing demonstrated that, like the holographic Professor Moriarty on Star Trek: The Next Generation, these newly-defined objects are purely virtual constructs that can&#8217;t step out into the real world.  If you try to bring a reference to one of these script objects into Delphi-land, you get a <strong>nil</strong> instead.  This greatly reduces the opportunity for customization.</p>
<p>So I figured I may as well try my hand at my own version, drawing on my class-creation research.  For the last few months, with a lot of hard work (and a fair bit of technical assistance from Barry Kelly on the arcane details of the RTTI system) I&#8217;ve been putting together a new Object Pascal-based script compiler.  It&#8217;s not completely finished yet, but the features that will set it apart are up and running:</p>
<ul>
<li>Unit-based compilation.  All your code doesn&#8217;t have to be in one unit.  It also doesn&#8217;t expect that you&#8217;re building a single script to be executed directly.  You <em>can</em> do that, but you can also run a valid compile cycle made up entirely of one or more units, with no <strong>program</strong> unit and no main routine, and use it as a library, calling individual routines from the script engine.  Units can be compiled and saved individually, similar to Delphi&#8217;s DCU system, and linked with other units to form a script program.</li>
<li>Dynamic, RTTI-based creation of new classes.  You can define a new class in the script engine and the compiler will generate a new VMT and all the extended RTTI necessary to use this class from native Delphi code just like any other.  For example, you can create a class that descends from a native type, override a virtual method, pass an instance out of the script engine to a variable reference, call the virtual method, and the script engine will be invoked to run it.  (This particular trick require the TMethodImplementation class to set up, so Delphi XE or later will be required.)</li>
</ul>
<p>The plan is to use the RTTI capabilities to achieve the most seamless integration possible between scripts and native code.  In my next few posts I&#8217;ll be going over some details as to how I made this all work.</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.turbu-rpg.com/284/dynamic-class-creation-moving-beyond-the-theoretical/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Smaller, cleaner RTTI coming?</title>
		<link>http://tech.turbu-rpg.com/274/smaller-cleaner-rtti-coming</link>
		<comments>http://tech.turbu-rpg.com/274/smaller-cleaner-rtti-coming#comments</comments>
		<pubDate>Fri, 31 Dec 2010 02:47:51 +0000</pubDate>
		<dc:creator>Mason Wheeler</dc:creator>
				<category><![CDATA[Class Creation]]></category>
		<category><![CDATA[crystal ball]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[RTTI]]></category>

		<guid isPermaLink="false">http://tech.turbu-rpg.com/?p=274</guid>
		<description><![CDATA[One of the biggest complaints about the extended RTTI introduced in Delphi 2010 is the way it adds so much to the size of your EXE.  Well, in a recent StackOverflow answer, Barry Kelly hinted that the format of the basic RTTI structures in TypInfo.pas are &#8220;more likely to change from version to version now [...]]]></description>
			<content:encoded><![CDATA[<p>One of the biggest complaints about the extended RTTI introduced in Delphi 2010 is the way it adds so much to the size of your EXE.  Well, in <a href="http://stackoverflow.com/questions/4562982/rtti-delphi-create-as-tvalue-an-n-dimensional-matrix/4567252#4567252">a recent StackOverflow answer</a>, Barry Kelly hinted that the format of the basic RTTI structures in TypInfo.pas are &#8220;more likely to change from version to version now that it has a much higher level abstraction in the Rtti unit.&#8221;<span id="more-274"></span></p>
<p>I mentioned in a comment that I wouldn&#8217;t like to see much change in the RTTI data structures because it would make <a href="http://tech.turbu-rpg.com/3/dynamic-class-creation">dynamic class creation</a> code difficult to maintain, and I&#8217;ve been doing some serious work in that area recently. Barry responded with the rationale:</p>
<blockquote><p>It&#8217;s less a case of completely changing, than  having more data appended on the end. It&#8217;s a real PITA to jam in extra  data without breaking code. As it is, the RTTI format contributes a  fairly substantial size penalty largely because its encoding can&#8217;t be  optimized much. It would be nice to consider something like e.g. .NET  metadata tables, which use a conciser representation. Instead, Delphi  RTTI is left with every xref taking up a minimum of 4 bytes, and  frequently with redundant strings for names that could be pooled. But  that can&#8217;t happen within the current framework.</p></blockquote>
<p>He mentions two things here: changing the way references work to make the RTTI take up less space,  and making it less difficult to update the RTTI structures with new information.  I could get behind something like that, especially since the update would almost by necessity involve cleaning up the type definitions in the process.  It&#8217;s a real pain to work with some of the existing RTTI structures, because you get stuff like this:</p>
<pre>
<div class="codesnip-container" >
<div class="delphi codesnip" style="font-family:monospace;"><span class="coMULTI">{ vmtFieldTable entry in VMT }</span>
&nbsp; PVmtFieldTable <span class="sy3">=</span> <span class="sy2">^</span>TVmtFieldTable<span class="sy1">;</span>
&nbsp; TVmtFieldTable <span class="sy3">=</span> <span class="kw1">packed</span> <span class="kw1">record</span>
&nbsp; &nbsp; Count<span class="sy1">:</span> <span class="kw4">Word</span><span class="sy1">;</span> <span class="co1">// Published fields</span>
&nbsp; &nbsp; ClassTab<span class="sy1">:</span> PVmtFieldClassTab<span class="sy1">;</span>
&nbsp; &nbsp;<span class="coMULTI">{Entry: array[1..Count] of TVmtFieldEntry;
&nbsp; &nbsp; ExCount: Word;
&nbsp; &nbsp; ExEntry: array[1..ExCount] of TVmtFieldExEntry;}</span>
&nbsp; <span class="kw1">end</span><span class="sy1">;</span></div>
</div>
</pre>
<p>Why are those last three fields commented out?  Because they&#8217;re declared as static arrays, but the upper bound isn&#8217;t known at compile time and changes from one instance to another.  Basically, they contain dynamic arrays inserted inline into the record, which is something that Delphi&#8217;s type system doesn&#8217;t support.  And since the type system doesn&#8217;t support it and the compiler can&#8217;t locate the members of the records, extracting data from a TVmtFieldTable (or several other structures that do similar things) is a real pain.</p>
<p>RTTI that&#8217;s easier to work with <em>and</em> takes up less space in the EXE?  If they can pull it off, I&#8217;d love to see it!</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.turbu-rpg.com/274/smaller-cleaner-rtti-coming/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dynamic class creation</title>
		<link>http://tech.turbu-rpg.com/3/dynamic-class-creation</link>
		<comments>http://tech.turbu-rpg.com/3/dynamic-class-creation#comments</comments>
		<pubDate>Wed, 23 Sep 2009 19:06:24 +0000</pubDate>
		<dc:creator>Mason Wheeler</dc:creator>
				<category><![CDATA[Class Creation]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[CodeRage]]></category>

		<guid isPermaLink="false">http://turbu-rpg.com/?p=3</guid>
		<description><![CDATA[NOTE: The download link in the original version of this post got mangled by WordPress. It&#8217;s been corrected now. A couple weeks ago, I gave a presentation at the CodeRage 4 online conference about dynamic class creation at runtime in Delphi.  The replays were just posted yesterday at Embarcadero&#8217;s CodeRage site.  My session can be [...]]]></description>
			<content:encoded><![CDATA[<p>NOTE: The download link in the original version of this post got mangled by WordPress.  It&#8217;s been corrected now.</p>
<p>A couple weeks ago, I gave a presentation at the CodeRage 4 online conference about dynamic class creation at runtime in Delphi.  The replays were just posted yesterday at <a href="http://conferences.embarcadero.com/coderage/sessions" target="_blank">Embarcadero&#8217;s CodeRage site</a>. <span id="more-3"></span> My session can be downloaded <a href="http://cc.embarcadero.com/download.aspx?id=27307" target="_blank">here</a> or viewed online <a href="http://cc.embarcadero.com/javascript/play.html?u=y&amp;w=1024&amp;h=768&amp;s=14.7MB&amp;f=http%3a%2f%2fcc.embarcadero.com%2fdownload.aspx%3fid%3d27307%26file%3dCodeRage.swf&amp;d=http%3a%2f%2fcc.embarcadero.com%2fdownload.aspx%3fid%3d27307" target="_blank">here</a>.  The sound quality was terrible, unfortunately.  I&#8217;m not sure what was going on with my headset the evening I recorded this, but it sounds like I was recording inside a giant water pipe, instead of sitting in my quiet, ordinary apartment room.  (And this was after editing.  It sounded even worse at first!)  Despite this, it was received fairly well, with a bunch of good questions and comments.</p>
<p>I showed a demo of a tool I&#8217;d written to explore the structure of a class, plus a code library for creating new classes at runtime.  I just put it up for download if anyone wants to play around with it.  You can find it <a href="http://turbu-rpg.com/downloads/class_creation.zip">here</a>.  The dproj file is in Delphi 2010 format, and I haven&#8217;t tested it with any earlier versions, but I&#8217;ve tried to use IFDEFs to define out code that wouldn&#8217;t run on earlier versions, and this ought to work on any version from D5 on.  Take a look, and feel free to use it in your own projects if you can find some use for it.  And of course, if there&#8217;s any way you find to improve on it, please let me know.<br />
Mason Wheeler</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.turbu-rpg.com/3/dynamic-class-creation/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

