1

I'm attempting to execute a tiny piece of JS via the following code (utilising the Native class from the Chakra Host example from MSDN):

        var runtime = default(JavaScriptRuntime);
        Native.ThrowIfError(Native.JsCreateRuntime(JavaScriptRuntimeAttributes.None, JavaScriptRuntimeVersion.VersionEdge, null, out runtime));

        var context = default(JavaScriptContext);
        Native.ThrowIfError(Native.JsCreateContext(runtime, (Native.IDebugApplication64)null, out context));
        Native.ThrowIfError(Native.JsSetCurrentContext(context));

        var script = @"var bob = 1;";
        var result = default(JavaScriptValue);
        var contextCookie = default(JavaScriptSourceContext);

        Native.ThrowIfError(Native.JsRunScript(script, contextCookie, "source", out result));

The problem is that it returns a "ScriptCompile" error with no additional details that I'm able to spot.

Is anyone able to reveal what I've missed / done dumb / gotten confused over?

lzcd
  • 1,325
  • 6
  • 12
  • I tried compiling it by replacing the main method of the C# sample and it all ran fine on my machine. I'd try Justin's code below to see if you can get more details on the error to figure out what's going wrong. – panopticoncentral Feb 11 '14 at 06:56
  • Did you try running it as an STA thread? I'm not sure if that's necessary or not. – justin.m.chase Feb 22 '14 at 19:26

3 Answers3

3

I doubt you're still wrestling with this... but I just did and figured out the answer.

If you look in jsrt.h you'll see that all the native functions use a wchar_t when using string parameters, however the DllImport attribute doesn't specify the charset, so it defaults to ANSI.

I did a find/replace in the Native.cs file and changed all the DllImport attributes to read...

[DllImport("jscript9.dll", CharSet = CharSet.Unicode)]

... and now my code works fine. I've sent a pull request to the sample owner on GitHub to get this fixed up. The update is currently in my fork at https://github.com/billti/chakra-host

Bill Ticehurst
  • 1,728
  • 12
  • 14
1

Try adding this to your AssemblyInfo.cs file:

[module: DefaultCharSet(CharSet.Unicode)]
malthe
  • 1,237
  • 13
  • 25
0

I modified my implementation of Native.ThrowIfError to, in the case of a ScriptCompile error, get some values out of the errorObject. Like so:

var message = errorObject
    .GetProperty("message")
    .ToString();
var source = errorObject
    .GetProperty("source")
    .ToString();
var line = (int)errorObject
    .GetProperty("line")
    .ToDouble();
var column = (int)errorObject
    .GetProperty("column")
    .ToDouble();
var length = (int)errorObject
    .GetProperty("length")
    .ToDouble();

throw new JavaScriptParseException(error, message, source, line, column, length);

This should get you more information at least.

justin.m.chase
  • 13,061
  • 8
  • 52
  • 100