سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

Try adding this to your AssemblyInfo.cs file:

[module: DefaultCharSet(CharSet.Unicode)]

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top