开发者

bug when function declaration is not followed by semicolon in emit of ruleset global section?

开发者 https://www.devze.com 2023-02-25 13:07 出处:网络
Here are two rulesets that illustrate a totally unexpected behavior. Is this a bug? Functions get executed even though they are not called anywhere or script breaks with a javascript error. The solut

Here are two rulesets that illustrate a totally unexpected behavior. Is this a bug?

Functions get executed even though they are not called anywhere or script breaks with a javascript error. The solution is to always end a declaration with a semicolon like this:

test1 = function(){console.log("test1");}  ;// <==

but this is dangerous I think because this is not mandatory in javascript.

Example 1 bookmarklet:

javascript:(function(){
  var d=document;var s=d.createElement('script');s.text=&quot;
  KOBJ_config={'rids':['a1135x27']};&quot;;d.body.appendChild(s);
  var l=d.createElement('script');l.src='http://init.kobj.net/js/shared/kobj-static.js';
  d.body.appendChild(l);})()

Example 2 bookmarklet :

javascript:(function(){
  var d=document;var s=d.createElement('script');s.text=&quot;
  KOBJ_config={'rids':['a1135x27']};&quot;;d.body.appendChild(s);
  var l=d.createElement('script');l.src='http://init.kobj.net/js/shared/kobj-static.js';
  d.body.appendChild(l);})()

Rulesets:

ruleset a1135x27 {
  meta {
    name "odd1"
    description <<
        demonstrate oddity 1
    >>
    author "Loïc Devaux"
    logging on
  }

  dispatch {
    // Some example dispatch domains
    // domain "example.com"
    // domain "other.example.com"
  }

  global {

    emit  <|



       /* this breaks javascript code*/
       // => 16:27:58 ERROR general Closure Executed with error a1135x27 - 13025536774875610960847698152Exception: test1 is not defined
       test1 = function(){console.log("test1")}

        /* this won't break */
        //test1 = function(){console.log("test1")};
        /* */

       /* this won't break */
       //test1 = function(){console.log("test1")}
       //test2 = function(){console.log("test2")};
       /* */

    |>;
  }

  rule first_rule {
    select when pageview ".*" setting ()
    pre { 

        }
    {
      emit <|

           test1();


          |>;  
    }

  }




}



ruleset a1135x28 {
  meta {
    name "odd2"
    description <<
        demonstrate oddity 2
    >>
    author "Loic Devaux"
    logging on
  }

 dispatch {
    // Some example dispatch domains
    // domain "example.com"
    // domain "other.example.com"
  }

  global {

    emit  <|

       /*  test1 will be executed even though it hasn't been called anywhere */
       //test1 = function(){console.log("test1");}
        /* */

        /* test1 won't get executed */
        /* 
            test1 = function(){console.log("test1");};
        */
        /* */


        /* this won't break a开发者_如何学运维nd test2 will be executed although it hasn't been called anywhere */

            test1 = function(){console.log("test1");}
            test2 = function(){console.log("test2");}

        /* */


    |>;
  }

  rule first_rule {
    select when pageview ".*" setting ()
    pre { 

        }
    {
      emit <|

          console.log('running first_rule');

          |>;  
    }

  }




}


The right thing to do here is write valid javascript. In your code example, you regularly leave off important semicolons. Remember that while javascript is fairly tolerant of syntactic problems, they can get you into trouble in unexpected ways.

test2 = function(){console.log("test2")};

should be

test2 = function(){console.log("test2");};

because both the assignment is a statement, is is the console.log() method call.

When you emit invalid javascript, all sorts of odd stuff is likely to occur. While most result in a javascript syntax error, the closures that KNS uses to wrap your code (and prevent unintended variable leakage) can have the side effects you observe.

In short: If there is a bug here, it's the javascript parser's flexibility in letting you omit important semicolons. This is not considered a KRL bug.

0

精彩评论

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