problem with accessing functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • [RaZoR]

    problem with accessing functions

    hello,

    main script creates IE window, put an array there and then calls a child
    script. chils script makes an array element equal to some object and
    returns control to main script. then main script doesn't see that object
    and its functions. why?

    here is the code I implemented:

    //in main script
    oWSH = new ActiveXObject(W Script.Shell);
    oIEWindow.docum ent.Script.MyAr ray = new Array()
    oWSH.Run("child .js", 2, true);

    //in child.js script
    var MyObj = function() {
    var Value = 3.14159;
    var getValue = function() { return Value; }
    var setValue = function(arg) { Value = arg; }
    this.gV = getValue;
    this.sV = setValue;
    }
    oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj();
    oIEWindow.docum ent.Script.MyAr ray[0].sV(2.71); // <-- this works
    oIEWindow.docum ent.Script.MyAr ray[0].gV(); // <-- this works

    //returning back to main
    oIEWindow.docum ent.Script.MyAr ray[0].gV() // <-- this doesn't work

    is it possible to set an object in array and call its functions from a
    child script and then call'em again from main script?

    what did I assume wrong in this code?

    please help,
    regards,
    mirek

    ps. I also tried the following definition of MyObj with no success:

    var MyObj = function() {
    mo = new Object();
    mo.Value = 3.14159;
    mo.gV = function() { return mo.Value; }
    mo.sV = function(arg) { mo.Value = arg; }
    return mo;
    }

  • kaeli

    #2
    Re: problem with accessing functions

    In article <4GH5b.118164$2 k4.1281406@news .chello.at>,
    nonexistent@ant ispam.address enlightened us with...[color=blue]
    > hello,
    >
    > main script creates IE window, put an array there and then calls a child
    > script. chils script makes an array element equal to some object and
    > returns control to main script. then main script doesn't see that object
    > and its functions. why?
    >[/color]

    [guess]
    It's probably out of scope.

    [color=blue]
    > here is the code I implemented:
    >
    > //in main script
    > oWSH = new ActiveXObject(W Script.Shell);
    > oIEWindow.docum ent.Script.MyAr ray = new Array()
    > oWSH.Run("child .js", 2, true);
    >
    > //in child.js script
    > var MyObj = function() {[/color]

    This var MuObj is not global (?). It will go out of scope and become
    nothing when the child finishes. You don't return the object to a var in
    the parent, like

    var myObj = oWSH.Run("child .js",2,true);

    That's a guess - I've never really done OOP JS. I do this in java,
    though.
    [color=blue]
    >
    > is it possible to set an object in array and call its functions from a
    > child script and then call'em again from main script?
    >[/color]

    Well, I had a problem in JSP where the object being placed in the array
    was actually a reference, not a copy, and the reference became nothing
    when the scope reverted to main. I can't say if that's a problem for you
    or not, as I've never tried this in JS.

    -------------------------------------------------
    ~kaeli~
    Press any key to continue or any other key to quit.
    Who is General Failure and why is he reading
    my hard disk?


    -------------------------------------------------

    Comment

    • [RaZoR]

      #3
      Re: problem with accessing functions

      hi,
      [color=blue]
      > [guess]
      > It's probably out of scope.[/color]

      well, it looks like.. but I don't really understand.. the second
      definition returns the object. I mean that "MyArray[0]" contains MyObj
      object with its all methods (as I understand it). I also somehow
      understand that first definition (without var mo = new Object()) can
      loose its properties and methods, because it doesn't return anything to
      MyArray[0] (but I am beginner and I doubt myself)

      on the other side when I do a small change in code, namely I set
      MyArray[0] in main script:

      //in main script
      oWSH = new ActiveXObject(W Script.Shell);
      oIEWindow.docum ent.Script.MyAr ray = new Array()
      oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj(); // <-- added line
      oWSH.Run("child .js", 2, true);

      and then call child.js, everything works fine. from child.js I can call
      MyArray[0].somefunction() and results are visible in main.js after
      returning back.

      generally I don't understand what's visible, what's not, and why in this
      example..

      regards,
      mirek

      Comment

      • kaeli

        #4
        Re: problem with accessing functions

        In article <ASI5b.119600$2 k4.1307382@news .chello.at>,
        nonexistent@ant ispam.address enlightened us with...[color=blue]
        >
        > well, it looks like.. but I don't really understand.. the second
        > definition returns the object.[/color]

        Not to main, it doesn't. It returns it to another function in child. The
        reference goes away when child is done.
        [color=blue]
        > on the other side when I do a small change in code, namely I set
        > MyArray[0] in main script:
        >
        > //in main script
        > oWSH = new ActiveXObject(W Script.Shell);
        > oIEWindow.docum ent.Script.MyAr ray = new Array()
        > oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj(); // <-- added line
        > oWSH.Run("child .js", 2, true);
        >
        > and then call child.js, everything works fine. from child.js I can call
        > MyArray[0].somefunction() and results are visible in main.js after
        > returning back.
        >[/color]

        Then the problem is indeed scope. You made a variable local to main and
        child merely changed it, so it stayed there.
        Using the "new" keyword makes a new object, so that object would be
        local to child or main depending on where you put it.

        Here's a couple good explanations of scope.

        Business technology, IT news, product reviews and enterprise IT strategies.

        [color=blue]
        > generally I don't understand what's visible, what's not, and why in this
        > example..
        >[/color]

        Well, scope works pretty much the same (excluding blocks in C) in all
        the languages I've used...

        outside any function...
        var v = something;
        v is global and visible anywhere

        in function f...
        var v = something;
        v is local to f and not visible anywhere else.

        in function g...
        var x = v;
        will fail if v was declared in f, will succeed if declared outside a
        function.

        So, if you have a main file with functions and statements, variables
        declared outside any function are global and variables declared in a
        function are local to that function and go away when the function is
        done.

        I am only vaguely familiar with WSH, so I am not sure what Run does. I
        will assume it parses / executes "child.js" - therefore, anything in
        child.js stays there. MyObj would be local to child.js and go away when
        execution finishes (return control to main).

        The solution, as you discovered, is to declare the object in main, then
        execute the child.
        You could probably also return the object and assign it to the array in
        main, but I'm not certain. I don't think it would work the way you are
        using the Run command, as I don't think you can return something that
        way. If you were just using functions, it would work.

        -------------------------------------------------
        ~kaeli~
        Press any key to continue or any other key to quit.
        Who is General Failure and why is he reading
        my hard disk?


        -------------------------------------------------

        Comment

        • [RaZoR]

          #5
          Re: problem with accessing functions

          hello,
          [color=blue][color=green]
          >>well, it looks like.. but I don't really understand.. the second
          >>definition returns the object.[/color]
          >
          > Not to main, it doesn't. It returns it to another function in child. The
          > reference goes away when child is done.[/color]

          to what function..? :( I don't understand that.. because actually both
          main and client operate on the third, separate object: IE window with
          its array.

          it looks like this:
          main creates this IE window via new ActivexObject and puts array there.
          main initialize the [0]-element of this array as an MyObj object. then
          main calls client as a separate process. client sees this IE window --
          it looks for it and finds. then client calls MyArray[0] methods (which
          is MyObj now set by main), and returns control back. then again main
          sees the client's changes in IE window.

          alternatively it looks like this:
          main creates this IE window via new ActivexObject and puts array there.
          then main calls client as a separate process. client does
          MyArray[0]=MyObj, calls its [0]-element methods etc. after returning
          back to main changes in IE window are not visible.
          [color=blue]
          > Here's a couple good explanations of scope.
          > http://sharkysoft.com/tutorials/jsa/content/031.html
          > http://www.javaworld.com/javaworld/j...avascript.html[/color]

          surely I will put an eye there. thanks. :)
          [color=blue]
          > Well, scope works pretty much the same (excluding blocks in C) in all
          > the languages I've used...[/color]

          yes, but this is about "global" and "local" scope for *separate
          processes*, not for functions.. does it propagate similar way?
          [color=blue]
          > I am only vaguely familiar with WSH, so I am not sure what Run does. I
          > will assume it parses / executes "child.js" - therefore, anything in
          > child.js stays there. MyObj would be local to child.js and go away when
          > execution finishes (return control to main).[/color]

          no, no.. Run executes another script as a separate process. as I
          understand (please correct me), it is *completely separate*, except
          envoironment variables in "USER" space. any communication between these
          processes is then via this IE window, visible for main and client.
          [color=blue]
          > The solution, as you discovered, is to declare the object in main, then
          > execute the child.[/color]

          yes, but that ruins my algorithm a little :)
          [color=blue]
          > You could probably also return the object and assign it to the array in
          > main, but I'm not certain. I don't think it would work the way you are
          > using the Run command, as I don't think you can return something that
          > way. If you were just using functions, it would work.[/color]

          yes, it works for functions. but it is about processes. I would like to
          return an object created in one process to another process via third
          window, visible to both of them.

          thanks ~kaeli~, I will look at those urls. As I know inter processes
          communication differ from function-2-function.. If you think that I'm
          wrong, please tell me..

          regards,
          mirek

          Comment

          • kaeli

            #6
            Re: problem with accessing functions

            In article <STJ5b.120690$2 k4.1331644@news .chello.at>,
            nonexistent@ant ispam.address enlightened us with...[color=blue][color=green]
            >>[/color]
            > to what function..? :( I don't understand that.. because actually both
            > main and client operate on the third, separate object: IE window with
            > its array.
            >[/color]

            That depends on the setup. From what you say below, I misworded my reply
            because I didn't understand the whole of what you were doing. I kinda
            still don't. :)

            If you program in C, I could explain by saying you aren't extern-ing
            your vars...
            [color=blue]
            > it looks like this:
            > main creates this IE window via new ActivexObject and puts array there.
            > main initialize the [0]-element of this array as an MyObj object. then
            > main calls client as a separate process.[/color]

            Key words - separate process. That process has its own space for things
            (memory), often called a "heap". The "new" keyword allocates the space
            on the *current* heap - the child's.
            [color=blue]
            > client sees this IE window --
            > it looks for it and finds. then client calls MyArray[0] methods (which
            > is MyObj now set by main), and returns control back. then again main
            > sees the client's changes in IE window.
            >[/color]

            Main sees it because it was allocated during main and is in main's
            memory heap (scope).
            [color=blue]
            > alternatively it looks like this:
            > main creates this IE window via new ActivexObject and puts array there.[/color]

            Array is a pointer to a block of memory. Empty memory. Just a space to
            put stuff.
            (js people correct me if I'm wrong, 'cuz I do this stuff in java and c)
            [color=blue]
            > then main calls client as a separate process.[/color]

            Process gets its own heap. Allocates new objects to that space.
            But the problem is this:
            oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj();

            When that runs from child, MyObj points to a spot in the *child's* heap
            - which disappears when the child process exits!
            The array then points to dead space.
            [color=blue]
            > client does
            > MyArray[0]=MyObj, calls its [0]-element methods etc. after returning
            > back to main changes in IE window are not visible.
            >[/color]

            MyObj was allocated to the heap in child. When child process dies, heap
            goes bye-bye. Array now points to nothing.
            [color=blue]
            >
            > yes, but this is about "global" and "local" scope for *separate
            > processes*, not for functions.. does it propagate similar way?
            >[/color]

            Yes.
            As long as main is active, its heap is viable.
            [color=blue][color=green]
            > > The solution, as you discovered, is to declare the object in main, then
            > > execute the child.[/color]
            >
            > yes, but that ruins my algorithm a little :)
            >[/color]

            Solutions often do. :)
            [color=blue]
            >
            > yes, it works for functions. but it is about processes. I would like to
            > return an object created in one process to another process via third
            > window, visible to both of them.[/color]

            There has to be a way, but I don't know it...

            I'm now over my head a bit - did you try
            microsoft.publi c* newsgroups? They do a bit more WSH and processes stuff
            with IE than most...

            -------------------------------------------------
            ~kaeli~
            Press any key to continue or any other key to quit.
            Who is General Failure and why is he reading
            my hard disk?


            -------------------------------------------------

            Comment

            • [RaZoR]

              #7
              Re: problem with accessing functions

              hi,
              [color=blue][color=green]
              >>alternative ly it looks like this:
              >>main creates this IE window via new ActivexObject and puts array there.[/color]
              >
              > Array is a pointer to a block of memory. Empty memory. Just a space to
              > put stuff.
              > (js people correct me if I'm wrong, 'cuz I do this stuff in java and c)
              >[color=green]
              >> then main calls client as a separate process.[/color]
              >
              > Process gets its own heap. Allocates new objects to that space.
              > But the problem is this:
              > oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj();
              >
              > When that runs from child, MyObj points to a spot in the *child's* heap
              > - which disappears when the child process exits!
              > The array then points to dead space.
              >[color=green]
              >>client does
              >>MyArray[0]=MyObj, calls its [0]-element methods etc. after returning
              >>back to main changes in IE window are not visible.[/color]
              >
              > MyObj was allocated to the heap in child. When child process dies, heap
              > goes bye-bye. Array now points to nothing.[/color]

              well.. if it is as you say then it starts to convice me :) Let me
              summarize it:

              main creates IE Window, and says "let there be an array". this is just
              pointing to some place in memory, the beginning of the first element,
              but "length" and it's end are unknown yet. then main executes MyArray[0]
              = new MyObj() and by doing so, it really reserves a chunk of memory and
              fills it with MyObj() elements. then main calls client. client can see
              the object, even created by another process because MyObj[0] points
              where MyObj *really* exists. after returning back mains sees the changes.

              the alternative (doesn't work):
              [...]unknown yet. then main calls client.js. client reserves and fills a
              chunk of memory with MyObj() but this is at its own memory space.
              (please correct me: if for example, in this very moment yet another
              process wanted to execute MyArray[0] (via IE window) it would so
              succesfully, right? because this object now *really* exists in memory,
              and it doesn't matter where, because IE window MyArray[0] points exactly
              there. right?). after returning back mains does not see the changes,
              because MyArray[0] still points to a chunk of mamory, but this chunk
              does not contain the object already.

              right? :)

              regards,
              mirek

              Comment

              • kaeli

                #8
                Re: problem with accessing functions

                In article <QlL5b.122774$2 k4.1373302@news .chello.at>,
                nonexistent@ant ispam.address enlightened us with...[color=blue]
                >
                > well.. if it is as you say then it starts to convice me :) Let me
                > summarize it:
                >
                > main creates IE Window, and says "let there be an array". this is just
                > pointing to some place in memory, the beginning of the first element,[/color]

                No - even more abstract. It points to the place the first element would
                be at if there were a first element - which there isn't.

                This is C, so forgive me if JS is more forgiving with pointers and
                arrays and objects...

                var MyArray;

                myArray is now simply declared and points to nothing and its value is
                NULL/undefined.

                var myArray = new Array();

                myArray, to the computer, now points to some memory address, say 12345,
                that is the start of a block of memory large enough to hold an Array
                object. That memory address either has nothing or has garbage. What it
                doesn't have is any meaningful value.
                [color=blue]
                > but "length" and it's end are unknown yet. then main executes MyArray[0]
                > = new MyObj() and by doing so, it really reserves a chunk of memory and
                > fills it with MyObj() elements.[/color]

                When main says
                myArray[0] = new myObj();
                the computer goes to 12345 and blocks out a chunk of memory large enough
                to fit a myObj object in and initializes it however the constructor for
                myObj tells it to.
                [color=blue]
                > then main calls client. client can see
                > the object,[/color]

                Client can see the object - as the object beginning at 12345 and ending
                at 12345+size(myOb j).
                [color=blue]
                > even created by another process because MyObj[0] points
                > where MyObj *really* exists. after returning back mains sees the changes.[/color]

                Yup.
                Any changes made by child change the contents of memory block 12345+size
                (myObj).
                [color=blue]
                >
                > the alternative (doesn't work):[/color]

                myArray points to nothing. It is null. It has no memory block assigned.
                [color=blue]
                > [...]unknown yet. then main calls client.js. client reserves and fills a
                > chunk of memory with MyObj() but this is at its own memory space.[/color]

                Yup. The block is now 98765+size(myOb j).
                [color=blue]
                > (please correct me: if for example, in this very moment yet another
                > process wanted to execute MyArray[0] (via IE window) it would so
                > succesfully, right? because this object now *really* exists in memory,
                > and it doesn't matter where, because IE window MyArray[0] points exactly
                > there. right?).[/color]

                You know, I can't be really sure on that. I don't do multithreading
                well. Actually, I never do multithreading.
                [color=blue]
                > after returning back mains does not see the changes,[/color]

                Because main still has array[0] pointing to 98765 - which no longer has
                anything in it, since the child released it!

                Does that help?

                -------------------------------------------------
                ~kaeli~
                Press any key to continue or any other key to quit.
                Who is General Failure and why is he reading
                my hard disk?


                -------------------------------------------------

                Comment

                • [RaZoR]

                  #9
                  Re: problem with accessing functions

                  > Does that help?

                  yes! thanks a lot :)

                  regards,
                  mirek

                  Comment

                  Working...