开发者

Beestings in a heredoc within a global function

I'm having trouble with using beestings in a heredoc in a global function. The runtime throws an error "Exception: arg2 is not defined". Here is an example:

ruleset a163x59 {
  meta {
    name "Beesting in heredoc"
    description <<
        Demonstrate the error with beestings in global function heredocs
    >>
    author "Steve Nay"
    logging on
  }

  dispatch {
  }

  global {
    myFunc = function(arg1, arg2) {
        firstString = "This is a regular string: #{arg1}. No problem there.";
        secondString = <<
            This is a heredoc with a beesting: #{arg2}. Problem!
        >>;
        secondString;
    };
  }

  rule first_rule {
    select when pageview ".*" setting ()
    pre {
        msg = myFunc("First argument", "Second argument");
    }
    notify("Testing...", msg) with sticky = true;
  }
}
开发者_JAVA百科

It never complains about arg1 being undefined, which shows that using a beesting inside a regular string is just fine.

Is there something I'm doing wrong, or is this a bug?


It is in fact a bug, but there is a workaround. Replace your function def with this modified code:

myFunc = function(arg1, arg2) {
    firstString = "This is a regular string: #{arg1}. No problem there.";
    secondString = <<
        This is a heredoc with a beesting: #{arg2}. Problem!
    >>;
    "#{secondString}";
};

Notice that the last statement (the return value) is a quoted beesting. This forces the resolution of any beestings inside the heredoc, and it works.

The problem occurs because KRL late binds the beesting replacements until javascript execution, but there is a bug in the closure generation causing the variable not to be available. Forcing the resolution with the quoted beesting solves this problem.


I have confirmed in my own tests that you have indeed stumbled upon a bug. I will get it filed and we will resolve this as soon as possible. Thank you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜