Groups | Blog | Home
all groups > dotnet jscript > april 2005 >

dotnet jscript : Prototype object calling c# code problem


Pete
4/20/2005 9:13:24 AM
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
Serge Baltic
4/21/2005 8:18:28 AM
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

Pete
4/21/2005 9:42:41 AM
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..
Serge Baltic
4/22/2005 4:13:10 AM
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

zwetan
4/24/2005 12:00:00 AM
[quoted text, click to view]

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]
[quoted text, click to view]


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

[quoted text, click to view]

use codeDOM for example

zwetan

zwetan
4/26/2005 12:00:00 AM
[quoted text, click to view]

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

Serge Baltic
4/26/2005 4:18:46 AM
z> override
z> Optional. A string that determines which security permissions to
z> apply to
z> the code in codeString.

"unsafe" value?

(H) Serg

Pete
4/27/2005 6:06:59 AM
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
Serge Baltic
4/27/2005 9:26:56 AM
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

AddThis Social Bookmark Button