全局函数内的定界符中的 Beestings

发布于 2024-10-27 10:10:38 字数 889 浏览 3 评论 0原文

我在全局函数的定界文档中使用 beestings 时遇到问题。运行时抛出错误“异常:arg2未定义”。这是一个例子:

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;
  }
}

它从不抱怨 arg1 未定义,这表明在常规字符串中使用 beesting 就可以了。

我做错了什么,或者这是一个错误?

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;
  }
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然 2024-11-03 10:10:38

这实际上是一个错误,但有一个解决方法。将您的函数 def 替换为以下修改后的代码:

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}";
};

请注意,最后一个语句(返回值)是带引号的 beesting。这会强制解决定界符内的任何问题,并且它有效。

出现该问题的原因是 KRL 后期绑定了 beesting 替换,直到 javascript 执行,但闭包生成中存在错误,导致变量不可用。通过引用的 beesting 强制解决该问题可以解决这个问题。

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.

秋叶绚丽 2024-11-03 10:10:38

我在自己的测试中证实,您确实偶然发现了一个错误。我会将其归档,我们将尽快解决此问题。谢谢。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文