Monday, May 26, 2008

OpenLaszlo Conditional Compilation

In a previous blog entry, I talked about Flex 3's support for conditional compilation. In this blog entry, I cover OpenLaszlo's conditional compilation support.

In the OpenLaszlo Application Developer's Guide, Chapter 49 ("Understanding Compilation") discusses two types of condition compilation available in OpenLaszlo (Conditional Compilation of Runtime-Dependent Code and Compile-time Constants). A third specific type of conditional compilation available in OpenLaszlo is discussed in Conditional Compilation of Debugging Code in Chapter 51 ("Debugging") of the same OpenLaszlo Application Developer's Guide.

The OpenLaszlo blog entry Conditional Compilation also discusses all three conditional compilation types, but does not provide examples as thorough as what are in the OpenLaszlo Application Developer's Guide or in my blog entry here.

Here is a code sample that attempts to demonstrate the three primary types of conditional compilation in OpenLaszlo. I'll break this overall code sample down into the individual parts for each of the three types of conditional compilation in OpenLaszlo after the code listing.

<canvas width="750" height="500">
// See
// for additional details on conditional compilation in OpenLaszlo. See also
// Chapter 49 ("Understanding Compilation") in the OpenLaszlo Application
// developer's guide.
<window id="MainWindow"
title="Demonstrate Conditional Compilation">
<simplelayout axis="y" spacing="10" />
<statictext id="mainLabel"
height="25">Enter Who To Greet
<edittext id="inputTextArea" width="500"/>
Click Me to See Greeting
<handler name="onclick">
if ($formal)
greetingStr = "Good day, ";
greetingStr = "What do you think of this weather?, ";
greetingStr += inputTextArea.text;
<statictext id="greeting" width="500" height="25" />
<when runtime="dhtml">
<statictext>You specified DHTML runtime.</statictext>
<when runtime="swf7">
<statictext>You specified Flash 7 runtime.</statictext>
<when runtime="swf8">
<statictext>You specified Flash 8 runtime.</statictext>
<statictext>You didn't specify a runtime explicitly.</statictext>

<text id="debugStatus" width="500" height="25">
<handler name="onconstruct">
if ($debug)
debugStatus.setText("Debug is turned ON.");
debugStatus.setText("Debug is turned OFF.");

The code highlighted above in dark green is part of the illustration of compile-time constants conditional compilation. The code highlighted in brown demonstrates use of debugging conditional compilation and the code highlighted in blue illustrates runtime-dependent conditional compilation.

The first compilation with the OpenLaszlo command-line compiler lzc is shown in the next screen snapshot.

The lzc use in this snapshot shows that the code above was compiled to SWF8, was compiled with debug turned on (the -g2 option), and was compiled with a -Dformal=true option.

The next two screen snapshots show the initial OpenLaszlo application rendered when the resultant SWF is run followed by how it appears when a value is submitted in the form. The example shows that the application is able to tell that debugging was set (the -g2 option to the lzc compiler) and that the runtime the application was compiled to was indeed SWF8. However, the compile-time constant did not seem to work. In the next example, we'll add a $ in front of the name passed to the -D option.

The next example compiles the same source code, but this time the source code is compiled without the debug flag (no -g2 option), with the SWF7 runtime, and with an attempt at trying to get the compile-time constant to work by adding a $ to the name passed to the -D option.

When the code is compiled as above, the application looks as follows when the SWF is executed and text is supplied to the input text field.

In the rendered output, we see that the Flash Player is version 7 and we also see that it is not in debug mode both because our conditional told us that and because the OpenLaszlo debugger window is not present. Unfortunately, the presence of the "What do you think of this weather?" in this output indicates that the compile-time constant is still not being recognized even though we added a dollar sign ($) in front of the name passed to the compiler with the lzc compiler.

To this point, I have not been able to get compile-time constants to work correctly with OpenLaszlo. If someone else has, please let me know what I'm doing incorrectly here. However, as the code examples and output examples above demonstrate, the OpenLaszlo runtime conditional compilation and debug conditional compilation are both easy to use.

No comments: