This method to inject native code will blow your mind

Something in the mak/audiotest example caught my eye:

Code:
Extern
Global UserAgent:String="navigator.userAgent"
Public

If UserAgent.Contains( "MSIE " ) ...
So I thought: How..?

The answer is pretty easy: Whatever you define as a global in the extern block will be treated as such - doesn't really matter if it's a variable or a class member or method or whatever. I mean, how could the CX transpiler even possibly know this?

This means you can for example define an extern global "document.getElementById('GameConsole').value" for your HTML5 target which in JS is not only a variable but much more a method call plus a member access - but luckily, CX doesn't really care.

It gets weirder:
You can also define an external global that's basically just a function call. But how to get CX to invoke that? Easy: Just fake some assignment or property access and prevent it from happening by starting a comment in the external declaration! That way, what follows the variable access in CX will simply be ignored in JS. E.G:
Code:
Extern
' finish string with ;// so whatever follows the invoke in .cxs gets ignored in .js
Global jshack_target_refer:String = "var target = document.getElementById('CubicBezierFunction'); if(!target) target = document.getElementById('GameConsole');//"
Global jshack_target_value:String = "target.innerHTML"
Public

            ' what's behind jshack_target_refer doesn't matter
            ' since it's commented out by extern global declaration
            ' it just has to look "normal" to CX's trans
            jshack_target_refer.Length()
            jshack_target_value = "newtext"
(I guess you can tell already this is exactly what I do in the Cubic Bézier Ease App)

Yes: it's ugly, it's target-dependent, it's a hack. But on the plus side it's ugly, target-dependent and a hack which is pretty cool (you have to admit).

Pretty sure stuff like this could work for other targets too. E.g. for getting a variable/class pointer in C or calling any platform specific system function.
 

Comments

Top Bottom