Conditional Compilation / Unmodified JS & Macros

57 views
Skip to first unread message

dlee

unread,
Nov 11, 2010, 7:35:41 AM11/11/10
to Jangaroo Users
There are three features that would make my life a lot easier with
Jangaroo. I just wanted to check if any were already implemented / if
they had been considered previously or if anyone feels the same about
them.

In flash you can do conditional compilation, you can define a boolean
variable and wrap code in statements like this:

CONFIG::MyVariable
{
//Code to include if MyVariable is true
}

I think this would be very useful for maintaining one code base to
compile to multiple targets as anything I know needs to be different
in the browser can be wrapped inside conditional compilation.



I find myself writing a lot of global functions which I implement in
regular javascript to get around type errors when I am using a browser/
lib feature not defined with an AS3 interface. It would be nice if I
could specify a block of code to not be validated / compiled so
regular JS can be inserted into an AS3 function.



Finally I can see a performance advantage to introducing macros into
the compiler. If I want to support a feature such as the external
interface i would end up writing something like this:

class ExternalInterface
{
public static function call(...args){
eval(args[0] + "("........
}
}

This doesn't really make sense, the code goes through class lookup,
closure wrappers, the call function and an eval before it can execute
javascript which could have been executed in line. With a macro system
a macro could be defined to replace ExternalInterface.call with just
the javascript code.

e.g.

ExternalInterface.call("foo", "bah"); -> foo("bah");


Once again, great tool with lots of features, it definitely does the
job as is but I would certainly like to see some of the above features.

Frank

unread,
Nov 11, 2010, 3:41:53 PM11/11/10
to Jangaroo Users
> In flash you can do conditional compilation, you can define a boolean
> variable and wrap code in statements like this:
>
> CONFIG::MyVariable
> {
>     //Code to include if MyVariable is true
>
> }
>
> I think this would be very useful for maintaining one code base to
> compile to multiple targets as anything I know needs to be different
> in the browser can be wrapped inside conditional compilation.

Sounds like a good feature to add! Do you have any pointer to where
conditional compilation in Flash is documented officially?
Unfortunately, IntelliJ IDEA 9 does not seem to support the syntax you
mention. :-(

> I find myself writing a lot of global functions which I implement in
> regular javascript to get around type errors when I am using a browser/
> lib feature not defined with an AS3 interface. It would be nice if I
> could specify a block of code to not be validated / compiled so
> regular JS can be inserted into an AS3 function.

We chose ActionScript instead of e.g. Java (=> GWT) to compile to
JavaScript, because they are so similar. You can almost paste any
JavaScript code into an AS3 function body and compile it with
Jangaroo, there are only a few things to be aware of:
* To access global JavaScript identifiers (variables, functions), use
window.foo or window['foo'], not just foo.
* Generally, use obj['foo'] if obj.foo leads to a type error.
* Take care when using 'this' in a function inside a non-static
method: Jangaroo will bind the inner function's 'this' to the outer
object. To prevent this, define the inner function inside a static
method.
* Note that static code is not executed immediately, only when the
class is actually used. Trigger by calling a static method from
JavaScript in your bootstrap code.

> Finally I can see a performance advantage to introducing macros into
> the compiler. If I want to support a feature such as the external
> interface i would end up writing something like this:
>
> class ExternalInterface
> {
>       public static function call(...args){
>            eval(args[0] + "("........
>       }
>
> }
>
> This doesn't really make sense, the code goes through class lookup,
> closure wrappers, the call function and an eval before it can execute
> javascript which could have been executed in line. With a macro system
> a macro could be defined to replace ExternalInterface.call with just
> the javascript code.
>
> e.g.
>
> ExternalInterface.call("foo", "bah"); ->  foo("bah");

You should not use eval(), because it is "evil" (performance,
security, debugging, ...). Instead, use Function#apply, like so:

public static function call(functionName:String, ... arguments):* {
return window[functionName] ? window[functionName].apply(null,
arguments) : null;
}

I don't see a need for compiler macros, and honestly don't like them.
Reminds me too much of C preprocessor...

Greetings,
-Frank-

Philippe

unread,
Dec 14, 2010, 2:23:32 PM12/14/10
to Jangaroo Users
Conditional compilation (& constant inlining):
http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html

Frank

unread,
Dec 15, 2010, 5:42:59 AM12/15/10
to Jangaroo Users
Thanks a lot, Philippe, that is the reference I've been looking for! I
don't know why it is so hard to find stuff in the Adobe livedocs with
Google, maybe I am too stupid...

Frank

unread,
May 10, 2013, 3:31:19 AM5/10/13
to Jangaroo Users
Hi Steven,

sorry, no conditional compilation support in Jangaroo yet.
To prioritize this, please file a feature request at
http://jangaroo.myjetbrains.com/youtrack/issues/JOO

Greetings
-Frank-

Steven Stark

unread,
May 10, 2013, 1:01:11 PM5/10/13
to jangaro...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages