Prototype object calling c# code problem - Javascript

This is a discussion on Prototype object calling c# code problem - Javascript ; The application that I am working on create new objects at run-time and attach behavior to these run-time objects by asssociating them with JScript. This application is being developed in C#. To be able to execute the method that are ...

+ Reply to Thread
Results 1 to 9 of 9

Prototype object calling c# code problem

  1. Default Prototype object calling c# code problem

    The application that I am working on create new objects at run-time and
    attach behavior to these run-time objects by asssociating them with
    JScript. This application is being developed in C#.

    To be able to execute the method that are created at runtime (and
    implemented in JScript) I have wrapped the eval function in a
    JScript.NET dll. I send in the script I want to execute to this dll and
    it runs the eval method with the script as parameter.

    Sometimes an object want to call an other objects method. Since the
    other object doesnt really have any methods but have scripts associated
    with them to simulate methods I have to go through a c# class that will
    run the right script.

    In the JScript for the calling object the code might look like...

    MethodInvoker.InvokeMethod("ObjectX.MethodX);

    MethodInvoker is my c# class that figures out where the scripts is
    located.

    This is not that pretty and to make it look nice I thought I could
    create a prototype object that hides the complexity for the person
    writing the script. The JScript would look like..

    function ObjectX()
    {
    this.MethodX = function()
    {
    MethodInvoker.InvokeMethod("ObjectX.MethodX");
    }
    }

    Now the script writer only have to write...

    var objectX = new ObjectX();
    objectX.MethodX();

    This looked very good to me and I started implemented it and have run
    into major problems.

    When the first script is executing and calls to my C# class I want to
    evaluate the MethodX script. So I call my wrapped JScript eval function
    and then get a an exception saying that the JSOBject cant be converted
    to IActionObject.

    This error message didnt really help me so I started to dig deeper and
    it looks like when the prototype object calls my c# method it puts
    itself on the ScopeStack. The first thing the JScript.Net interpreter
    does when evaluating a script it pushes a new stack frame on its
    internal stack.

    To me it looks like you cant use a prototype object when you want to
    call to c# call and then do a new eval.

    Have anyone ever had this problem before? I am very thankful for any
    help or guidance I can get in this area. Maybe there is a better way of
    doing? etc...

    Thanks


  2. Default Re: Prototype object calling c# code problem

    p> The application that I am working on create new objects at run-time
    p> and attach behavior to these run-time objects by asssociating them
    p> with JScript. This application is being developed in C#.
    p>
    p> To be able to execute the method that are created at runtime (and
    p> implemented in JScript) I have wrapped the eval function in a
    p> JScript.NET dll. I send in the script I want to execute to this dll
    p> and it runs the eval method with the script as parameter.

    OKAY, eval() is a nice thing, but it's dedicated to much simpler use, for
    evaluating expressions, and all about that.

    Seems like for the things you wish to do some VSA or CodeDom will fit better.

    VSA to .NET looks much like ActiveScripting to ActiveX/OLE (ontop of which
    JScript and VBScript for IE are based), at least that's my impression after
    some playing with it. You create an execution engine object, add some namespaces
    to it, add some objects as globals (for example, your callee may go there
    too), you can make properties&methods of some object global to the script.
    Then you add one or more blocks of script. After that, compile the whole
    thing and use it just like any ordinary .NET class, eg call functions defined
    there. Global code executes upon compilation.

    It's even available in source code, thru the Rotor thing. It's quite nice
    due to the fact there's nearly no help on it, and one that exists is quite
    sketchy.

    However, I didn't quite manage to deal with the VSA thing up to the complete
    production state. So should you have a significant advantage in the topic,
    please write back a few words about that into this newsgroup

    ---

    Should look something like this: you implement IVsaSite interface, create
    a Microsoft.JScript.Vsa.VsaEngine instance, InitVsaEngine giving it your
    site, add some objects, namespaces or source code thru engine.Items.CreateItem
    (the return value may be casted to IVsa***Item), then — Compile() and Run().

    I have not digged into it further yet … but the above thing ends in quite
    strange exceptions.

    (H) Serg



  3. Default Re: Prototype object calling c# code problem

    Thanks for your reply.

    If you check the VSA classes in the .net 2.0 framework you will find
    that they have all been marked as obsoleted. So going down that path is
    not something I can do. I could use CodeDom put then you have the
    problem that you cant unload dynamic assemblies. You would have to
    create a new appdomain instead and unload that appdomain when done. All
    of that is just to much work for me.

    I know that eval isnt the best way to do things..performance, security,
    maintainability etc.. however..will continue looking for solutions..


  4. Default Re: Prototype object calling c# code problem

    p> If you check the VSA classes in the .net 2.0 framework you will find
    p> that they have all been marked as obsoleted.

    Hmm. They should have offered something instead, I think? Or is CodeDOM and
    reflection-emit the only way to generate some dynamic code since 2.0?

    p> So going down that path
    p> is not something I can do.

    I don't think that "obsolete" really has much sence if it's a must …

    Also, I'm not sure if "eval" approach has much difference from VSA as they
    have common nature in depth, as it seems to me.

    BTW, have you managed to get VSA running in the full scale?

    p> I could use CodeDom put then you have the
    p> problem that you cant unload dynamic assemblies. You would have to
    p> create a new appdomain instead and unload that appdomain when done.
    p> All of that is just to much work for me.

    Yes, we've stepped into the problem also …

    p> I know that eval isnt the best way to do things..performance,
    p> security, maintainability etc.. however..will continue looking for
    p> solutions..

    Good luck!

    (H) Serg



  5. Default Re: Prototype object calling c# code problem

    > The application that I am working on create new objects at run-time and
    > attach behavior to these run-time objects by asssociating them with
    > JScript. This application is being developed in C#.
    >
    > To be able to execute the method that are created at runtime (and
    > implemented in JScript) I have wrapped the eval function in a
    > JScript.NET dll. I send in the script I want to execute to this dll and
    > it runs the eval method with the script as parameter.


    di you use the second parameter for the eval method ?

    cf JScript.NET eval doc
    ms-help://MS.NETFrameworkSDKv1.1/jscriptnet/html/jsmtheval.htm

    function eval(codeString : String [, override : String])

    override
    Optional. A string that determines which security permissions to apply to
    the code in codeString.

    [snip]
    >
    > When the first script is executing and calls to my C# class I want to
    > evaluate the MethodX script. So I call my wrapped JScript eval function
    > and then get a an exception saying that the JSOBject cant be converted
    > to IActionObject.
    >
    > This error message didnt really help me so I started to dig deeper and
    > it looks like when the prototype object calls my c# method it puts
    > itself on the ScopeStack. The first thing the JScript.Net interpreter
    > does when evaluating a script it pushes a new stack frame on its
    > internal stack.
    >
    > To me it looks like you cant use a prototype object when you want to
    > call to c# call and then do a new eval.



    well, it depends of how you compiled your JScript.NET dll

    did you use /fast- option ?
    because eval in fast mode has not the same behaviour than in classic (fast-)
    mode

    > Have anyone ever had this problem before? I am very thankful for any
    > help or guidance I can get in this area. Maybe there is a better way of
    > doing? etc...
    >


    use codeDOM for example

    zwetan



  6. Default Re: Prototype object calling c# code problem

    z> override
    z> Optional. A string that determines which security permissions to
    z> apply to
    z> the code in codeString.

    "unsafe" value?

    (H) Serg



  7. Default Re: Prototype object calling c# code problem

    > z> override
    > z> Optional. A string that determines which security permissions to
    > z> apply to
    > z> the code in codeString.
    >
    > "unsafe" value?
    >


    yep

    if you don't use "unsafe" you can not make the eval global
    it will have its own safe context and so it can not interact wiht already
    existing code.

    Check Eric Lippert blog for more in depth explanation of eval and
    JScript.NET
    http://blogs.msdn.com/EricLippert/

    zwetan



  8. Default Re: Prototype object calling c# code problem

    I have used unsafe. I got it to work..instead of having a constructor
    function for my wrapper class I associated a method with the prototype
    instead and then I got it to work... thanks for your replies


  9. Default Re: Prototype object calling c# code problem

    z> if you don't use "unsafe" you can not make the eval global
    z> it will have its own safe context and so it can not interact wiht
    z> already
    z> existing code.
    z> Check Eric Lippert blog for more in depth explanation of eval and
    z> JScript.NET
    z> http://blogs.msdn.com/EricLippert/

    Ya, have this feed in my Omea Reader …

    Also there's the JScript.NET source code from the Rotor project.

    But still there are some questions … and compiler bugs also

    PS is there any *working* example of VSA for JScript.NET? Just something
    simple …

    (H) Serg



+ Reply to Thread

Similar Threads

  1. prototypal inheritance: the prototype object
    By Application Development in forum Javascript
    Replies: 8
    Last Post: 12-02-2007, 12:13 PM
  2. Release only problem calling a delegate from unmanaged code
    By Application Development in forum DOTNET
    Replies: 3
    Last Post: 11-22-2007, 04:12 AM
  3. Is it not well to extend Object.prototype derictly?
    By Application Development in forum Javascript
    Replies: 3
    Last Post: 08-12-2007, 01:41 PM
  4. Problem in Calling native code from managed c++ dll.
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 08-01-2006, 12:08 AM
  5. Calling clarion DLL function from C++ prototype
    By Application Development in forum Clarion
    Replies: 3
    Last Post: 05-12-2006, 10:27 PM