Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

Commented Issue: Adding host primitive types [49]

$
0
0
Dear Sir or Madam,
I need to be able to pass in primitive types.

I cannot use AddHostObject(). Calling AddHostObject() for boxed primitive types (double, int, etc.) raises exception "Invalid host item".

I cannot use AddRestrictedHostObject(). Although exception is not raised when using AddRestrictedHostObject(), however, the resulted value cannot be used within the script: any arithmetic operation results in NaN.

A workaround that I currently employed is to wrap the primitive type into a class or Array. I used Array successfully. However this is less convenient as a script writer would have to be aware of this. I wonder if it is possible to pass primitive types directly. Note that I cannot use 'engine.Script.p = 2.1' construct as parameters are added at run time and are not known during build.

I would perhaps have expected something like AddHostValue(), but could find any.

Sample script:
function foo()
{
string s;

try
{
double d = 2.1;
object paramValue = (object)d;
ScriptEngine engine = new JScriptEngine();
// exception is rasied:
engine.AddHostObject("p", paramValue);
// same exception is rasied: engine.AddHostObject("p", d);
// cannot do this: engine.Script.p = 2.1;
}
catch (Exception ex)
{
s = ex.Message;
}
}

Please help,
Yours faithfully

MW
Comments: The suggestion above appears to work, and MW hasn't sent an update in 10 days. Marking as resolved.

Viewing all articles
Browse latest Browse all 2297

Latest Images

Trending Articles





Latest Images