https://perchance.org/8swe0svvsk#edit

The same exact call in a code block vs a script tag behaves differently. The values seem to arrive to the plugin okay, but then get funky at some point–no idea why.

  • perchance@lemmy.worldM
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    29 days ago

    why is this 0.5? 👉 [cat : statue : 0.5]

    This is being executed as JavaScript (like all square blocks), and in this case you’ve got two nested labelled statements, with the final/leaf statement being 0.5, which is the return value of the overall statement.

    You can paste cat : statue : 0.5 in Chrome console to see this. Let me know if that didn’t domino over the other questions you have

    Edit: Oh and RE the two outputs not being the same - one uses innerHTML and one uses square blocks to output the HTML, and square blocks always evaluate the result before outputing it. So I’m guessing that’s the cause of the difference. Setting innerHTML is a “lower level” operation, so to speak.

    • wthit56@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      29 days ago

      On the same link, I’ve simplified it down and found the issue is something to do with createPerchanceTree. If called from a code block, it keeps the escapes. If called from JS it removes the escapes.

      And that’s even if the code block is just calling a JS function. And the JS strips escapes even if calling a function defined in perchance code.

    • wthit56@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      29 days ago

      After a while stepping through code, I’ve tracked it down to… __primitiveValueGetterKeepEscapesYolo. (buh buh buuuuuh~)

      While processing [code blocks], __primitiveValueGetterKeepEscapesYolo is true, it seems. And while processing scripts it is undefined. So… I guess it’s on purpose that it strips out escapes in one case and not the other? Why is that?

      But also… how can I get it to stop it??! 🤣

      • wthit56@lemmy.worldOP
        link
        fedilink
        English
        arrow-up
        1
        ·
        28 days ago

        I’ve hacked it, just setting that to true during my plugin’s function, and setting it back to whatever it was after. Still not sure why this is there.

        And it actually affects getting values–not the generation of them. So I can’t just hack it for the createPerchanceTree call, but up to the point it’s completely processed and never going to be used again, basically.

    • wthit56@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      28 days ago

      Okay I think this is the last update… the problem is, accessing a property of a perchance object. If I can avoid that, then it doesn’t auto-strip escapes.

      I noticed this function: __evaluateText which takes a root object as the context, a “this” object, and a string to evaluate. So I’m using that now, which also means I can use any object to evaluate against too! And I can pass it window or $root (or this includes both it seems?). And no need for the YOLO hack, because that whole thing is bypassed anyway. 👍

      Here’s my test for this method: https://perchance.org/elbvabvg9g#edit. (Didn’t even need an iframe to isolate it either!)

      I’ve now integrated this into my advanced image-gen plugin.

      • perchance@lemmy.worldM
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        26 days ago

        __evaluateText is an engine-internal thing, and your code could break if you rely on that. In general, in JS, you should only use variables/functions that begin with _ if you were the one that made it. But it’s my bad because I can’t expect people to know that, and it’s my bad for leaving it accessible - I need to tidy up all those globals. I’ll add a warning for generators that use it, and now that I know people could be using stuff like this, I’ll make sure to keep these functions around as much as I possibly can (seems possible for __evaluateText, but there are other internal functions that may be much harder to keep).

        So what I’m gathering from the various comments in this thread is that there are two thing to report:

        • Feature request: You want something like evaluateItem (which can be called on strings like "{1-2}".evaluateItem), except it should allow passing in a “root”? So like "[b]".evaluateWith(newRoot) where newRoot was created with createPerchanceTree? So b gets pulled from newRoot. This seems like a fair request. How important is it for you to also be able to choose the this reference? E.g. do you need/want "[b]".evaluateWith(newRoot, parent)?
        • Bug report: If you have foo = \n, and you write [console.log(foo)] then it logs the literal characters \ and n whereas if you write <script>console.log(foo)</script> then it logs a newline character. Example: https://perchance.org/9tffz7swhn#edit I think I actually designed it like this on purpose so that you don’t have to add extra backslashes before characters depending on how “deeply” you’ve referenced it:
        output
          output: [depth1]
        
        depth1
          depth1: [depth2]
        
        depth2
          depth2: \\n
        

        https://perchance.org/0u6l500kua#edit

        If I fixed this “bug” then it would output a newline instead of the literal \n characters. With the current code, no escape-character-stripping is done in square block evaluation - it’s instead done when the square block’s output actually gets placed into the HTML. I.e. processing escaped characters is only done right at the end. This means that if you refactor your code and the depth of “references” to various items changes, then you don’t have to go through and add/remove backslashes.

        But I agree that the inconsistency of behavior here RE script tags is very strange. This seems big enough that it may have to wait for v2 of the engine though - I’m struggling to think of a way to fix this without causing some generators to break. If you have any thoughts here, please let me know, and thanks for your in-depth exploration here! It’s very helpful. I’ll link this comment from the perchance.org/known-bugs page.

        • wthit56@lemmy.worldOP
          link
          fedilink
          English
          arrow-up
          1
          ·
          26 days ago

          Yeah I did realise it’s an internal thing. But, if that’s the only way of me making it work without just copying that entire function someplace, then it’ll do. I actually like being able to see and use those internal things. Lets me sort out my own solutions to weird fiddly things like this. (Albeit with a lot of studying and stepping through to even figure out how to use it 😂)

          If you just make a set-in-stone function like you did for createPerchanceTree that would be find with me to be honest. Or yeah, a method on strings.

          I didn’t personally need a this for what I’m using it for, but if it’s just as easy to add that in as an optional parameter that would be good. Oh, and the root should just be any object, really. In my plugin I’m allowing people to use things like window, or an object, etc. So… please don’t throw if it’s not actually a root. Just let any object be used to “evaluate against.” Same goes for this I guess.

          I could see it could break a load of things, as you said. It’s likely only an issue because I was specifically doing deeper perchance stuff like building roots and evaluating strings myself. Probably most people won’t even find this discrepancy or notice it.

        • wthit56@lemmy.worldOP
          link
          fedilink
          English
          arrow-up
          1
          ·
          26 days ago

          Honestly, I think the real issue is that everything is monkey-patched. Which means you cannot escape everything getting “perchanced” all over the place. Which means if you want things to work “normally” you have to re-escape everything just in case. And even then it might blow up–who knows?

          If there was simply a difference between a string made with perchance (or explicitly perchance-ified), which has all the extras attached, and a string you just make in JavaScript, then none of this would be an issue. (Is my guess.) And same goes for all objects. Like, nothing is monkey-patched unless it’s perchance-ified, and then it works in the perchance-y way.

          There’s a lot of head-scratching, frustrations, and table-flipping that happens when I do certain things in simple JS that’s then “taken over” or otherwise messed with by the perchance engine. There are so many caveats and edge cases and weird behaviours you wouldn’t expect if you actually know JS, know what I mean?

          I understand the goal of all these things is to make it easy for non-programmers who don’t know JS to use it. But if you do know JS at times it can feel like you’ve stepped into bizarro-land where everything is upside-down and all your knowledge and skill just works against you. 😂😭😵‍💫

          • perchance@lemmy.worldM
            link
            fedilink
            English
            arrow-up
            1
            ·
            26 days ago

            If there was simply a difference between a string made with perchance (or explicitly perchance-ified), which has all the extras attached, and a string you just make in JavaScript, then none of this would be an issue.

            Wait, this sounds like a separate bug report. The issue I’m talking about is (I thought) only relevant to Perchance nodes, not raw strings. Can you give an example where Perchance interferes with your <script> tag where the text you’re manipulating doesn’t come from a Perchance node/property?

            • wthit56@lemmy.worldOP
              link
              fedilink
              English
              arrow-up
              1
              ·
              26 days ago

              The example I linked you to in the original post uses pure JS to produce the problem, yes. 😅 https://perchance.org/8swe0svvsk#edit

              The original issue was a script tag passing a JS-made string into a function. Versus a code block passing a JS-made string into a function. The same exact string, declared in JS. But the function call was from a script tag vs from a code block. That JS-made string would evaluate differently depending on whether a script was being processed at the time or a code block was being processed at the time. Something like that.

              • perchance@lemmy.worldM
                link
                fedilink
                English
                arrow-up
                1
                ·
                edit-2
                25 days ago

                Oh, right, yes, when using evaluateText on said string, that makes sense - due to square blocks having the policy of always “saving escape-char-processing until right at the end” as mentioned above. And yeah I agree this inconsistency is very annoying!

    • wthit56@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      29 days ago

      Oh labels! Yeah that would do it. Very head-scratching, that one 😅

      (In case you’re confused as to why I’m even using that code in the first place, [before : after : 0.9] is a StableDiffusion thing.)

      Makes sense that code blocks might evaluateItem. But calling evaluateItem when setting innerHTML doesn’t fix it.

      And the way in which it is different is what is so confusing to me. In both cases I’m sending a string to the plugin, and logging out the value through each step of the process.

      • In the script case after running it through createPerchanceTree() it becomes 0.5, which is how that label issue happens. Even though I escape it beforehand so that it won’t be read as a [code block]. You can see it just sends 0.5, through the image generated by it.

      • In the code block case after running the same escaped string through createPerchanceTree() it stays as the normal string. You can see it gets the full [cat : statue : 0.5] prompt, through that other generated image that actually looks like a cat.

      I can’t see any reason why internally my function behaves differently, before it’s got anywhere near back to the page or evaluateItem etc.

      I’ll work on making a simplified case to get closer to the cause…