HighTechTalks DotNet Forums  

Global objects get lost during JScript evaluations

Dotnet VSA microsoft.public.dotnet.vsa


Discuss Global objects get lost during JScript evaluations in the Dotnet VSA forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Tiaan via .NET 247
 
Posts: n/a

Default Global objects get lost during JScript evaluations - 06-09-2004 , 02:23 PM






The C# code below shows a problem I'm having with the VSA enginewhere a global object seems to get lost during scriptevaluation. It looks like the engine recognizes the name of theglobal object, but simply returns null when accessing it from anevaluation string -- although it works just fine when accessingthe global object from within other code items (as long as thoseitems are not evaluated themselves).

The example code can be compiled into a console executable thattakes a single argument as input from the command line (e.g.,compile with "csc.exe /t:exe ExampleCode.cs/r:Microsoft.JScript.dll,Microsoft.Vsa.dll"), which in turn isevaluated as some JScript code. To illustrate the problem, thesupplied argument gets executed in four different ways. Runningthe application with an argument like"System.Console.WriteLine(123);" would produce the followingoutput:
Executing code directly...
123
Executing code as a function...
123
Evaluating code as a function...
123
Evaluating code directly...
123
Done

However, when trying to access the global object, named"globStrDict", with an argument such as"System.Console.WriteLine(globStrDict != null);" would show theproblem:
Executing code directly...
True
Executing code as a function...
True
Evaluating code as a function...
False
Evaluating code directly...
False
Done

Obviously, an argument such as"System.Console.WriteLine(globStrDict.Count);" results in anexception as soon as it hits the first evaluation point in theapplication. Why does the evaluator know that the global objectexists, but not return a reference to it? Is there any way toaccess the global object with a simple call to JScriptEvaluateso that I don't need to compile the code that needs to beevaluated?

-- Tiaan.


==============================


using System;
using System.Collections.Specialized;
using Microsoft.Vsa;
using Microsoft.JScript;
using Microsoft.JScript.Vsa;

class ScriptSite : IVsaSite
{
private StringDictionary m_StringDictionary = newStringDictionary();
public object GetGlobalInstance(string name)
{
if (name == "globStrDict")
return m_StringDictionary;
return null;
}
public void GetCompiledState(out byte[] pe, out byte[]dbgInfo)
{
pe = null; dbgInfo = null;
}
public object GetEventSourceInstance(string itemName, stringsourceName)
{
return null;
}
public void Notify(string notify, object info) {}
public bool OnCompilerError(IVsaError error)
{
Console.WriteLine("Error: {0}", error.Description);
return false;
}
}

class ScriptGlobals
{
public static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("USAGE: <ThisAppName> <JScriptCode>");
Console.WriteLine("EXAMPLE: <ThisAppName> " +
"\"System.Console.WriteLine(globStrDict.Count);\"" );
return;
}
string code = args[0];
try
{
VsaEngine eng = new VsaEngine();
eng.RootMoniker = "ScriptGlobals://JScript";
eng.Site = new ScriptSite();
eng.InitNew();
eng.RootNamespace = "blah";
eng.Name = "ScriptGlobals JScript Engine";
IVsaReferenceItem refItem;
refItem = (IVsaReferenceItem)eng.Items.CreateItem(
"mscorlib", VsaItemType.Reference, VsaItemFlag.None);
refItem.AssemblyName = "mscorlib.dll";
refItem = (IVsaReferenceItem)eng.Items.CreateItem(
"system", VsaItemType.Reference, VsaItemFlag.None);
refItem.AssemblyName = "System.dll";
IVsaGlobalItem globItem;
globItem = (IVsaGlobalItem)eng.Items.CreateItem(
"globStrDict", VsaItemType.AppGlobal, VsaItemFlag.None);
globItem.TypeString ="System.Collections.Specialized.StringDictionary" ;
IVsaCodeItem codeItem;
codeItem = (IVsaCodeItem)eng.Items.CreateItem(
"intro", VsaItemType.Code, VsaItemFlag.None);
codeItem.SourceText = "import System;" +
"function WrapperFunc() { " + code + " } " +
"System.Console.WriteLine(\"Executing codedirectly...\");";
codeItem = (IVsaCodeItem)eng.Items.CreateItem(
"code", VsaItemType.Code, VsaItemFlag.None);
codeItem.SourceText = code;
codeItem = (IVsaCodeItem)eng.Items.CreateItem(
"wrap", VsaItemType.Code, VsaItemFlag.None);
codeItem.SourceText = "import System;" +
"System.Console.WriteLine(\"Executing code as afunction...\");" +
"WrapperFunc();";
if (!eng.Compile())
return;
eng.Run();
System.Console.WriteLine("Evaluating code as afunction...");
Eval.JScriptEvaluate("WrapperFunc();", eng);
System.Console.WriteLine("Evaluating code directly...");
Eval.JScriptEvaluate(code, eng);
Console.WriteLine("Done");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>XbY2l/UcXUWT2mmd+7r+fQ==</Id>

Reply With Quote
  #2  
Old   
bruce barker
 
Posts: n/a

Default Re: Global objects get lost during JScript evaluations - 06-09-2004 , 04:48 PM






this is a known restriction. as there is little active development on vsa
its not likely to be lifted. the problem i believe is that the eval code is
not running in the correct context, so the global data is not found.

the simple fix for you is to switch from context based global data to
instance based global data. this means instead of calling static functions,
you call a static that returns an object that has props for the global data
(I generally pass the global objects to the constructor) . then call your
script methods off the class instance.


-- bruce (sqlwork.com)






"Tiaan via .NET 247" <anonymous (AT) dotnet247 (DOT) com> wrote

The C# code below shows a problem I'm having with the VSA engine where a
global object seems to get lost during script evaluation. It looks like the
engine recognizes the name of the global object, but simply returns null
when accessing it from an evaluation string -- although it works just fine
when accessing the global object from within other code items (as long as
those items are not evaluated themselves).

The example code can be compiled into a console executable that takes a
single argument as input from the command line (e.g., compile with "csc.exe
/t:exe ExampleCode.cs /r:Microsoft.JScript.dll,Microsoft.Vsa.dll"), which in
turn is evaluated as some JScript code. To illustrate the problem, the
supplied argument gets executed in four different ways. Running the
application with an argument like "System.Console.WriteLine(123);" would
produce the following output:
Executing code directly...
123
Executing code as a function...
123
Evaluating code as a function...
123
Evaluating code directly...
123
Done

However, when trying to access the global object, named "globStrDict", with
an argument such as "System.Console.WriteLine(globStrDict != null);" would
show the problem:
Executing code directly...
True
Executing code as a function...
True
Evaluating code as a function...
False
Evaluating code directly...
False
Done

Obviously, an argument such as
"System.Console.WriteLine(globStrDict.Count);" results in an exception as
soon as it hits the first evaluation point in the application. Why does the
evaluator know that the global object exists, but not return a reference to
it? Is there any way to access the global object with a simple call to
JScriptEvaluate so that I don't need to compile the code that needs to be
evaluated?

-- Tiaan.


==============================


using System;
using System.Collections.Specialized;
using Microsoft.Vsa;
using Microsoft.JScript;
using Microsoft.JScript.Vsa;

class ScriptSite : IVsaSite
{
private StringDictionary m_StringDictionary = new StringDictionary();
public object GetGlobalInstance(string name)
{
if (name == "globStrDict")
return m_StringDictionary;
return null;
}
public void GetCompiledState(out byte[] pe, out byte[] dbgInfo)
{
pe = null; dbgInfo = null;
}
public object GetEventSourceInstance(string itemName, string sourceName)
{
return null;
}
public void Notify(string notify, object info) {}
public bool OnCompilerError(IVsaError error)
{
Console.WriteLine("Error: {0}", error.Description);
return false;
}
}

class ScriptGlobals
{
public static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("USAGE: <ThisAppName> <JScriptCode>");
Console.WriteLine("EXAMPLE: <ThisAppName> " +
"\"System.Console.WriteLine(globStrDict.Count);\"" );
return;
}
string code = args[0];
try
{
VsaEngine eng = new VsaEngine();
eng.RootMoniker = "ScriptGlobals://JScript";
eng.Site = new ScriptSite();
eng.InitNew();
eng.RootNamespace = "blah";
eng.Name = "ScriptGlobals JScript Engine";
IVsaReferenceItem refItem;
refItem = (IVsaReferenceItem)eng.Items.CreateItem(
"mscorlib", VsaItemType.Reference, VsaItemFlag.None);
refItem.AssemblyName = "mscorlib.dll";
refItem = (IVsaReferenceItem)eng.Items.CreateItem(
"system", VsaItemType.Reference, VsaItemFlag.None);
refItem.AssemblyName = "System.dll";
IVsaGlobalItem globItem;
globItem = (IVsaGlobalItem)eng.Items.CreateItem(
"globStrDict", VsaItemType.AppGlobal, VsaItemFlag.None);
globItem.TypeString =
"System.Collections.Specialized.StringDictionary";
IVsaCodeItem codeItem;
codeItem = (IVsaCodeItem)eng.Items.CreateItem(
"intro", VsaItemType.Code, VsaItemFlag.None);
codeItem.SourceText = "import System;" +
"function WrapperFunc() { " + code + " } " +
"System.Console.WriteLine(\"Executing code directly...\");";
codeItem = (IVsaCodeItem)eng.Items.CreateItem(
"code", VsaItemType.Code, VsaItemFlag.None);
codeItem.SourceText = code;
codeItem = (IVsaCodeItem)eng.Items.CreateItem(
"wrap", VsaItemType.Code, VsaItemFlag.None);
codeItem.SourceText = "import System;" +
"System.Console.WriteLine(\"Executing code as a function...\");" +
"WrapperFunc();";
if (!eng.Compile())
return;
eng.Run();
System.Console.WriteLine("Evaluating code as a function...");
Eval.JScriptEvaluate("WrapperFunc();", eng);
System.Console.WriteLine("Evaluating code directly...");
Eval.JScriptEvaluate(code, eng);
Console.WriteLine("Done");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>XbY2l/UcXUWT2mmd+7r+fQ==</Id>



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.