does a "parameters"-parameter overwrite the "parameters"-object?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Florian Loitsch

    #1

    does a "parameters"-parameter overwrite the "parameters"-object?

    I'm currently writing a JS->Scheme compiler (which, using Bigloo,
    automatically yields a JS->C, JS->JVM, JS->.NET compiler), and have a
    question concerning the function-parameters:
    According to the spec (10.1.6) the activation-object already contains the
    the parameters-object when the variables are instantiated (10.1.3). The
    question is now: does a parameter "parameters " overwrite the
    "parameters "-object or not?
    All three implementations I tested (Konqueror, Mozilla and Rhino) do not
    overwrite the object, but I've already seen all three fail on other simple
    examples.
    Here's a minimal example:
    ===
    function g(parameters) {
    alert(arguments .length);
    }
    g({length:"obje ct"}); // => "object"? 1?
    ===
    // florian loitsch
  • Dietmar Meier

    #2
    Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

    Florian Loitsch wrote:
    [color=blue]
    > function g(parameters) {
    > alert(arguments .length);
    > }
    > g({length:"obje ct"}); // => "object"? 1?[/color]

    1.

    In your function "g", either "parameters ", or "arguments[0]",
    but not "arguments" would reference the anonymous object you
    supplied as the first argument.

    ciao, dhgm

    Comment

    • Florian Loitsch

      #3
      Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

      Obviously I was tired when I wrote this test case...
      replace all "parameters " with "arguments" in my post.
      -> question becomes:
      ===
      function g(arguments) {
      alert(arguments .length);
      }
      g({length:"obje ct"}); // "object"? or 1?
      ===

      and in this case Konqueror, Rhino as well as Mozilla return "object" (what I
      expect it to be, eventhough I'm not completely sure, if it is defined in
      the spec).
      sorry for the typo...
      // florian loitsch

      Comment

      • Michael Winter

        #4
        Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

        Florian Loitsch wrote:

        [snip]
        [color=blue]
        > ===
        > function g(arguments) {
        > alert(arguments .length);
        > }
        > g({length:"obje ct"}); // "object"? or 1?
        > ===
        >
        > and in this case Konqueror, Rhino as well as Mozilla return
        > "object" (what I expect it to be, eventhough I'm not completely
        > sure, if it is defined in the spec).[/color]

        The specification implies the order in section 10.1.6 - Activation Object:

        "When control enters an execution context for function code, an
        object called the activation object is created and associated
        with the execution context. The activation object is initialised
        with a property with name arguments and attributes
        { DontDelete }. The initial value of this property is the
        arguments object described below.

        The activation object is then used as the variable object for
        the purposes of variable instantiation."

        The activation object is initialised with the arguments property,
        after which the same object is used for variable instantiation. As the
        arguments property does not possess the ReadOnly attribute, it is safe
        to assume that any local variable that is instantiated with the same
        name should overwrite the arguments object. Also notice the use of the
        phrase "initial value", which implies that the arguments property can
        take on another value.

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Dietmar Meier

          #5
          Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

          Michael Winter wrote:
          [color=blue]
          > As the
          > arguments property does not possess the ReadOnly attribute, it is safe
          > to assume that any local variable that is instantiated with the same
          > name should overwrite the arguments object.[/color]

          It does not really overwrite the arguments object, it just assigns
          another reference to the execution context's property "arguments" .
          The arguments object is still accessible via
          g[0]..g[g.lenght-1] in Gecko based browsers and via
          g.arguments[0]..g.arguments[g.length-1] in MSIE (if the function's
          identifier is "g").

          ciao, dhgm

          Comment

          • Michael Winter

            #6
            Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

            Dietmar Meier wrote:
            [color=blue]
            > Michael Winter wrote:
            >[color=green]
            >> [...] any local variable that is instantiated with the same name
            >> should overwrite the arguments object.[/color]
            >
            > It does not really overwrite the arguments object, [...][/color]

            Yes, it does.

            function testArguments() {
            var arguments = 'string';

            alert(arguments + ' ' + testArguments.a rguments);
            }

            The alert will contain the text, string, twice not once followed by
            the toString result of the arguments object.

            Though this behaviour might not be what one would expect with regard
            to object references, it is what happens with respect to the arguments
            object.
            [color=blue]
            > The arguments object is still accessible [as a property of the
            > function][/color]

            As I just demonstrated, it isn't. Moreover, that approach is
            deprecated as of JS1.4 and is not present in the ECMAScript standard.
            Why should it be? It's not necessary.

            If you want to access the arguments object, don't create a local
            variable (of any origin) with the same name.

            [snip]

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • Dietmar Meier

              #7
              Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

              Michael Winter wrote:
              [color=blue]
              > function testArguments() {
              > var arguments = 'string';
              >
              > alert(arguments + ' ' + testArguments.a rguments);
              > }[/color]

              There was no "var arguments" statement in Florian's code, but an
              argument identifier "arguments" . Look at this example:

              function testArguments(a rguments, baz) {
              alert([
              "1: " + arguments,
              "2: " + (testArguments. arguments && testArguments.a rguments[1]),
              "3: " + testArguments[1]
              ].join("\n"));
              }
              testArguments(" foo", "bar");
              [color=blue]
              > that approach is
              > deprecated as of JS1.4 and is not present in the ECMAScript standard.[/color]

              Referencing the arguments array as a property of the function object
              only. MSIE does not care about this. I didn't say I'd like that, I just
              wanted to show that the initial arguments object is not deleted in both
              major browsers.
              [color=blue]
              > Why should it be? It's not necessary. [...] don't create a local
              > variable (of any origin) with the same name[/color]

              Didn't I say that I wouldn't ever do so? :-)

              ciao, dhgm

              Comment

              • Florian Loitsch

                #8
                Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

                Michael Winter wrote:
                [color=blue]
                > Florian Loitsch wrote:
                >
                > [snip]
                >[color=green]
                >> ===
                >> function g(arguments) {
                >> alert(arguments .length);
                >> }
                >> g({length:"obje ct"}); // "object"? or 1?
                >> ===
                >>[/color][/color]
                [SNIP][color=blue]
                >
                > The activation object is initialised with the arguments property,
                > after which the same object is used for variable instantiation. As the
                > arguments property does not possess the ReadOnly attribute, it is safe
                > to assume that any local variable that is instantiated with the same
                > name should overwrite the arguments object. Also notice the use of the
                > phrase "initial value", which implies that the arguments property can
                > take on another value.[/color]
                but the "initial value" could have been overwritten by the
                function-declarations (where it is explicitly stated, that existing
                properties are overwritten). There's just no mention of already existing
                properties in the parameter-list instantiation, and as I (incorrectly)
                thought, that all existing JS-implementations don't overwrite the
                arguments-property I was confused.
                thx for the clarifications.
                // florian

                Comment

                • Michael Winter

                  #9
                  Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

                  Dietmar Meier wrote:
                  [color=blue]
                  > Michael Winter wrote:
                  >[color=green]
                  >> function testArguments() {
                  >> var arguments = 'string';
                  >>
                  >> alert(arguments + ' ' + testArguments.a rguments);
                  >> }[/color]
                  >
                  > There was no "var arguments" statement in Florian's code,[/color]

                  It was an example. :P I used variations that included all local types:
                  variable, function, and formal argument declarations (though the
                  former was a declaration/initialisation pair, as posted). In each
                  cases, the arguments object was overwritten in at least one browser.
                  As Firefox overwrote the object in every case, which browsers exhibit
                  what effects is immaterial from a Web scripting perspective.
                  [color=blue]
                  > [...] both major browsers.[/color]

                  Which would those be? :D

                  Mike

                  --
                  Michael Winter
                  Replace ".invalid" with ".uk" to reply by e-mail.

                  Comment

                  • Michael Winter

                    #10
                    Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

                    Florian Loitsch wrote:

                    [snip]
                    [color=blue]
                    > the "initial value" could have been overwritten by the
                    > function-declarations (where it is explicitly stated, that existing
                    > properties are overwritten).[/color]

                    When I referred to "any local variable", I meant /any/ local;
                    function, variable, or formal argument. That said, as variable
                    declarations are not supposed to replace the value of existing locals,
                    the variable would also have to undergo explicit initialisation.
                    [color=blue]
                    > There's just no mention of already existing properties in the
                    > parameter-list instantiation[/color]

                    Most likely because it's presumed that no argument will be given the
                    name of a known identifier, and that amongst user-defined locals,
                    formal parameters are instantiated first so there isn't a standard
                    case where there would be existing properties (with the exception of
                    arguments with the same name, which is covered, but that still isn't
                    standard).
                    [color=blue]
                    > I (incorrectly) thought, that all existing JS-implementations don't
                    > overwrite the arguments-property [...][/color]

                    If a property isn't noted for possessing the ReadOnly attribute, you
                    can assume that the value can be overwritten. The only exceptions
                    might be where an implementation has added the ReadOnly attribute on
                    its own initiative (which is acceptable for conforming
                    implementations , but sometimes annoying).

                    Mike

                    --
                    Michael Winter
                    Replace ".invalid" with ".uk" to reply by e-mail.

                    Comment

                    • Dietmar Meier

                      #11
                      Re: does a "parameter s"-parameter overwrite the "parameter s"-object?

                      Michael Winter wrote:
                      [color=blue][color=green]
                      >> There was no "var arguments" statement in Florian's code,[/color][/color]
                      [color=blue]
                      > It was an example. :P I used variations that included all local types:
                      > variable, function, and formal argument declarations (though the
                      > former was a declaration/initialisation pair, as posted). In each
                      > cases, the arguments object was overwritten in at least one browser.[/color]

                      Did you even look at the example in <39o81kF60m1uqU 1@individual.ne t>?

                      ciao, dhgm

                      Comment

                      • Michael Winter

                        #12
                        Re: does a &quot;parameter s&quot;-parameter overwrite the &quot;parameter s&quot;-object?

                        Dietmar Meier wrote:

                        [snip]
                        [color=blue]
                        > Did you even look at the example in <39o81kF60m1uqU 1@individual.ne t>?[/color]

                        Of course. In Firefox, testArguments.a rguments is undefined. Without
                        the arguments formal argument, it is an object reference. What's your
                        point?

                        Mike

                        --
                        Michael Winter
                        Replace ".invalid" with ".uk" to reply by e-mail.

                        Comment

                        Working...