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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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