Snippet CodeVar

Jesus4Lyf

Good Idea™
Reaction score
397
Ever noticed that WC3 does not allow code type arrays?
In other words...
JASS:
globals
    code array SomeName
endglobals

Absolutely breaks. Can't do it!

Introducing the answer to the question nobody asked:
CodeVar!

Interface:
JASS:
local CodeVar myCodeVar = CodeVar.create(function x)
OR
local CodeVar myCodeVar
set myCodeVar.code=function x

myCodeVar.code --> function x

call myCodeVar.exec()
call myCodeVar.eval()

if myCodeVar.eval() then
    call TriggerAddCondition(SomeTrig, Condition(myCodeVar.code))
endif
etc...


Library:
JASS:
library CodeFuncs
    struct CodeVar // By Jesus4Lyf
        private static constant trigger Trig=CreateTrigger()
        static method create takes code f returns CodeVar
            return f
            call DoNothing()
            return 0
        endmethod
        method operator code takes nothing returns code
            return this
            call DoNothing()
            return null
        endmethod
        method operator code= takes code f returns CodeVar // Crashes if changed to int
            return .create(f)
        endmethod
        private static method execCode takes code f returns nothing
            call TriggerClearActions(.Trig)
            call TriggerAddAction(.Trig,f)
            call TriggerExecute(.Trig)
        endmethod
        private static method evalCode takes code f returns boolean
            call TriggerClearConditions(.Trig)
            call TriggerAddCondition(.Trig,Condition(f))
            return TriggerEvaluate(.Trig)
        endmethod
        method exec takes nothing returns nothing
            call .execCode(.code)
        endmethod
        method eval takes nothing returns boolean
            return .evalCode(.code)
        endmethod
    endstruct
endlibrary


Demonstration:
JASS:
function Test takes nothing returns boolean
    call BJDebugMsg("Magic.")
    return false
endfunction
function NotMagic takes nothing returns nothing
    call BJDebugMsg(&quot;NOT magic! Just JASS. Or so they say... <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink    ;)" loading="lazy" data-shortname=";)" />&quot;)
endfunction

globals
    CodeVar array demo
endglobals
function Demonstration takes nothing returns nothing
    set demo[1].code=function Test // Same as line 3
    set demo[2]=CodeVar.create(function NotMagic)
    set demo[3]=CodeVar.create(function Test) // Same as line 1
    call demo[1].eval()
    call demo[2].exec()
    call demo[3].eval()
    if demo[1].code==function Test then
        call BJDebugMsg(&quot;Yep, same&quot;)
    endif
    if demo[2]==CodeVar.create(function NotMagic) then
        call BJDebugMsg(&quot;Zomg, also same&quot;)
    endif
endfunction


Furthermore, these structs don't leak. As in, you never need to destroy them. Calling CodeVar.create(function SomeSpecificFunc) will always result in the exact same struct. These pretty much are conditions, except you can get the code back out of them, and use code that doesn't return a boolean, and tell it to execute/evaluate directly.

Imagine that!

You can actually skip lines of code by incrementing the struct by 8.

If you ignore how retarded this is or never realise, this could be really cool to code with! :p
 
Reaction score
333
I would not encourage the use of this. This return bug stuff with code is supposedly buggy and vJass allows you to use functions as objects so code arrays are unnecessary anyway.
 

Jesus4Lyf

Good Idea™
Reaction score
397
>I2H? Didn't think you liked such things.
Well done! Didn't take long to see the dodgeyness.

HOWEVER! That's not I2H! :p
Code does not extend a handle.

>This return bug stuff with code is supposedly buggy
I actually found that myself, and incorporated the bug fix internally. (I hope. let me know if anyone gets bugs with this.)

Hey, I wouldn't encourage it's use either! But I thought this was actually kind of cool. ^_^

Oh, and when you say functions as objects, I don't think you can get the code back out... Which means you can't pass it, say, into KT2.

I wanted to post this moreso because I thought it was funny. Although I can see practical reasons for this if it's secure.

Just think! You could attach code to units and pass that code into KT2 or even execute it directly or whatever else. It does open some windows. I still think it's pretty useless though. :p
 

Hatebreeder

So many apples
Reaction score
381
>I2H? Didn't think you liked such things.
Well done! Didn't take long to see the dodgeyness.

HOWEVER! That's not I2H! :p
Code does not extend a handle.

>This return bug stuff with code is supposedly buggy
I actually found that myself, and incorporated the bug fix internally. (I hope. let me know if anyone gets bugs with this.)

Hey, I wouldn't encourage it's use either! But I thought this was actually kind of cool. ^_^

Oh, and when you say functions as objects, I don't think you can get the code back out... Which means you can't pass it, say, into KT2.

I wanted to post this moreso because I thought it was funny. Although I can see practical reasons for this if it's secure.

Just think! You could attach code to units and pass that code into KT2 or even execute it directly or whatever else. It does open some windows. I still think it's pretty useless though. :p

Speaking generally, attching things to units is... stupid xD
But, I can imagine some awesome Spells...
Anyways, if you think it's useless, then why post it? I think it says someting in the Rules, if don't find it good, then it'll be closed or something... Checking Rules in a minute =P

EDIT:
We want Quality, not Quantity. Thus, submit only what you think is good! If your post contains something like "check out my crappy spell" it will hit graveyard right away.
 
Reaction score
333
Oh, and when you say functions as objects, I don't think you can get the code back out... Which means you can't pass it, say, into KT2.

Since KT2 allows you to pass data to the callback function, you could easily pass the function object to a small wrapper function. It is better than using some hacky typecasting stuff, at any rate.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Hatebreeder
>think it says someting in the Rules, if don't find it good, then it'll be closed or something

Forgot about that. So forget about that! :p
Actually, I'm not done with this yet. And I do think it's kinda cool. But I'm trying to develop a serious use for it (as you may see below).

>you could easily pass the function object to a small wrapper function
I don't believe you.

Anyway, I just successfully made a "SkipLine" hack for wc3.

THINK ABOUT THAT! :p

JASS:
call CodeVar(CodeVar.create(function WatchTheMagic)+4*8).exec() // Skip line 1.

A lot of research went into the above line. "code" variables actually don't point at functions. They point at lines. It's +4*8 for the header and first line, and 3*8 for every other line after that (as far as I've found so far). Putting more research into it to see if it gets useful. :thup:

Edit: YEP! Check this out!
JASS:
globals
    integer hax=1
endglobals
function WatchTheMagic takes nothing returns nothing
    set hax=0
    call BJDebugMsg(&quot;Clearly, there is no magic&quot;)
    loop
        exitwhen hax==0
        // Can&#039;t ever reach here.
        call BJDebugMsg(&quot;Wow, that&#039;s MAGIC!&quot;)
        set hax=hax-1
    endloop
    call BJDebugMsg(&quot;Did you see any magic?&quot;)
    call BJDebugMsg(&quot; &quot;)
endfunction

function Trig_Untitled_Trigger_002_Actions takes nothing returns nothing
set hax=1
call CodeVar(CodeVar.create(function WatchTheMagic)+0*8).exec() // Same.
set hax=1
call CodeVar(CodeVar.create(function WatchTheMagic)+(1+1+4)*8).exec() // header(1), set(1), func call(4).
endfunction


Edit: Bah, apparently according to Mind.Worx this has been done before. XD
 
Reaction score
333
I don't believe you.

Something like:

JASS:
function CodeWrapper takes nothing returns nothing
    call FunctionInterfaceA(GetData()).evaluate()
endfunction


A lot of research went into the above line. "code" variables actually don't point at functions. They point at lines. It's +4*8 for the header and first line, and 3*8 for every other line after that (as far as I've found so far). Putting more research into it to see if it gets useful. :thup:

On closer inspection, code variables are just pointers to bytecode. Cool as that may be, there is no practical use.
 

Romek

Super Moderator
Reaction score
964
Isn't this basically what function interfaces do?
JASS:
function interface Test takes nothing returns nothing

function Blah takes nothing returns nothing
endfunction

// Globals:
Test array Something

// Inside some func
set Something[0] = Test.Blah // Arrays. <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile    :)" loading="lazy" data-shortname=":)" />

// Inside another
call Something[0].evaluate()
call Something[2534].execute()


In fact, function interfaces allow arguments and the such, making them even more superior to this.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Yep. They're probably more efficient for that, too.

Betcha can't get the code back out though.

EG.

call TriggerAddCondition(trig, Condition(myCodeVar.code))

(Which I don't think actually works right now, need to fix it, but you get the point. The actual point of this isn't .eval and .exec, it's .code.)
 

Azlier

Old World Ghost
Reaction score
461
JASS:
private static constant trigger Trig=CreateTrigger()

I wouldn't do that. Last I checked the WC3 Optimizer inlines constant handles. So anything that contains Trig is replaced with CreateTrigger().
 

Romek

Super Moderator
Reaction score
964
> Betcha can't get the code back out though.
To be honest, I think arguments is considerably more useful than being able to get the code back.
Infact, I think getting the code back is somewhat useless. :)
 

Jesus4Lyf

Good Idea™
Reaction score
397
>the WC3 Optimizer inlines constant handles
Well that's lame.

>Infact, I think getting the code back is somewhat useless.
BINGO! That's the problem.

But actually, it has certain vague applications, like attaching code to a unit for later retrieval for use as trigger actions. Of course, you could just attach a trigger, and then make the later trigger simply run that trigger. But that's not as efficient.

That's pretty much the extent of the usefulness of this. But somewhere in my playground of code I discovered it, and at least wanted to hear some opinions on it. And there you go. I'm happy for this to be graveyarded if no one else finds it interesting at all.

But the bytecode princible I'm intending to use in a system later on at some point.
 
Reaction score
333
>call FunctionInterfaceA(GetData()).evaluate()
Efficiency loss. :thup:

If the person who needs that functionality isn't willing to rewrite the system, then I doubt they will mind the minor efficiency loss. At any rate, both approaches are kludges to get around a limitation in the system and represent less than optimal solutions. I think the function interfaces are still widely preferable to some typecasting hack.

Edit:

JASS:
scope Test initializer Init
    globals
        private integer array bytecode
    endglobals
    
    private function Init takes nothing returns nothing
        set bytecode[0] = 0x0C140600
        set bytecode[1] = 0x00000EB6
        set bytecode[2] = 0x13140000
        set bytecode[3] = 0x00000000
        set bytecode[4] = 0x16000000
        set bytecode[5] = 0x000009BF
        set bytecode[6] = 0x0B010000
        set bytecode[7] = 0x00000000
        set bytecode[8] = 0x27000000
        set bytecode[9] = 0x00000000
        set bytecode[10] = 0x04000000
        set bytecode[11] = 0x00000000
        set bytecode[12] = 0x03000000
        set bytecode[13] = 0x00000EB7

        call Code(0x15A70EE0).exec() // address of bytecode array (on my machine, at least)
    endfunction
endscope


Surprisingly, this works and prints a string from the string table.
 

Jesus4Lyf

Good Idea™
Reaction score
397
The reality is you don't need either. KT2 is a library, so there's no need to store functions to add, because you could always write a dummy adding wrapper (with the function hard coded inside it) and use an interface or .execute or whatever to call it. This you can attach to units nicely. :thup:

So yeah, this belong in the graveyard, because it doesn't have any reasonable use. The bytecode stuff didn't go anyway that could be generally applied either.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top