加载打字稿编译成ClearScript," WScript的是未定义",不可能完成的任务?的是、不可能、加载、任务

由网友(小鸟爱天空丶)分享简介:我尝试使用 ClearScript 加载的打字稿编译器来编译一些基本的打字code。不幸的是,执行打字稿编译器源代码,当我得到这个错误:的WScript未定义这是 LINQPad 计划我已经使用,将ClearScript的DLL和TypeScript编译文件旁边的.linq程序:无效的主要(){使用(VAR JS =...

我尝试使用 ClearScript 加载的打字稿编译器来编译一些基本的打字code。

不幸的是,执行打字稿编译器源代码,当我得到这个错误:

  

的WScript未定义

这是 LINQPad 计划我已经使用,将ClearScript的DLL和TypeScript编译文件旁边的.linq程序:

 无效的主要()
{
    使用(VAR JS =新Microsoft.ClearScript.Windows.JScriptEngine(Microsoft.ClearScript.Windows.WindowsScriptEngineFlags.DisableSourceManagement))
    {
        变种typeScriptSource = File.ReadAllText(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath),tsc.js));
        js.Execute(typeScriptSource);
        常量字符串打字code = @
            一流的迎宾{
                问候:字符串;
                构造函数(消息:字符串){
                    this.greeting =消息;
                }
                迎接() {
                    返回,你好,+ this.greeting;
                }
            }
            功能测试()
            {
                VAR迎宾=迎宾(世界);
                返回greeter.greet();
            }
        ;
        js.Script.TypeScript.Compile(打字code);

        对象result = js.Script.test();
        result.Dump();
    }
}

#区域复制ClearScript到正确的位置

静态UserQuery()
{
    的foreach(VAR文件名在新的[] {ClearScriptV8-32.dll,ClearScriptV8-64.dll,V8-ia32.dll,V8-x64.dll})
    {
        尝试
        {
            字符串SOURCEPATH = Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath),文件名);
            字符串TARGETPATH​​ = Path.Combine(Path.GetDirectoryName(typeof运算(的Util).Assembly.Location),文件名);

            File.Copy(SOURCEPATH,TARGETPATH​​,真正的);
        }
        赶上(IOException异常前)
        {
            未选中
            {
                const int的fileInUseHresult =(INT)0x80070020;
                如果(ex.HResult!= fileInUseHresult)
                    扔;
            }
        }
    }
}

#endregion
 

该错误在这条线出现:

  js.Execute(typeScriptSource);
 
帮忙做一个简单的php mysql用户登录验证的代码

我创建了一个.zip文件的一切,你需要 LINQPad 加载.linq文件和实验。该ClearScript DLL的是从未经修改的源代码创建的,但如果你不相信我,你应该能够重现那些自己(如果你没有他们的)。

这可以在这里找到: Dropbox的链接SO19023498.zip

我已经试过什么:

我第一次尝试执行此code:

  VAR的WScript =新的ActiveXObject(WSH.WScript);
 

这仅生产了这个错误:自动化服务器不能创建对象

我没有看到 WSH.WScript 在注册表中的HKEY_CLASSES_ROOT下,这样可能是吧。

我想搞清楚如何从.NET创建对象并将其设置到脚本方面,但我显然不是找对了地方。

解决方案

下面是一个使用 typescript.js 当前版本的骨骼样本。需要注意的是打字稿API是逆向工程;据我可以告诉唯一的官方界面在 TSC 命令行。也请记住这是不确实的错误处理或报告最小的样本:

 使用系统;
使用System.IO;
使用Microsoft.ClearScript;
使用Microsoft.ClearScript.V8;

命名空间测试
{
    公共类TypeScriptCompiler
    {
        私人只读动态_compiler;
        私人只读动态_snapshot;
        私人只读动态_ioHost;

        公共TypeScriptCompiler(的ScriptEngine引擎)
        {
            engine.Evaluate(File.ReadAllText(typescript.js));
            _compiler = engine.Evaluate(新TypeScript.TypeScriptCompiler);
            _snapshot = engine.Script.TypeScript.ScriptSnapshot;
            _ioHost = engine.Evaluate(@({
                WRITEFILE:功能(路径,内容){this.output =内容; }
            }));
        }

        公共字符串编译(字符串code)
        {
            _compiler.addSourceUnit(foo.ts,_snapshot.fromString(code));
            _compiler.pullTypeCheck();
            _compiler.emitUnit(foo.ts,_ioHost);
            返回_ioHost.output;
        }
    }

    公共静态类TestApplication
    {
        公共静态无效的主要()
        {
            使用(VAR发动机=新V8ScriptEngine())
            {
                VAR编译器=新TypeScriptCompiler(发动机);
                Console.WriteLine(compiler.Compile(类Foo< T> {}));
            }
        }
    }
}
 

I tried using ClearScript to load the TypeScript compiler in order to compile some basic TypeScript code.

Unfortunately, when executing the TypeScript compiler source I get this error:

'WScript' is undefined

This is the LINQPad program I've used, place the ClearScript dll's and the TypeScript compiler file alongside the .linq program:

void Main()
{
    using (var js = new Microsoft.ClearScript.Windows.JScriptEngine(Microsoft.ClearScript.Windows.WindowsScriptEngineFlags.DisableSourceManagement))
    {
        var typeScriptSource = File.ReadAllText(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), "tsc.js"));
        js.Execute(typeScriptSource);
        const string typeScriptCode = @"
            class Greeter {
                greeting: string;
                constructor(message: string) {
                    this.greeting = message;
                }
                greet() {
                    return ""Hello, "" + this.greeting;
                }
            }
            function test()
            {
                var greeter = Greeter(""world"");
                return greeter.greet();
            }
        ";
        js.Script.TypeScript.Compile(typeScriptCode);

        object result = js.Script.test();
        result.Dump();
    }
}

#region Copy ClearScript to correct location

static UserQuery()
{
    foreach (var filename in new[] { "ClearScriptV8-32.dll", "ClearScriptV8-64.dll", "v8-ia32.dll", "v8-x64.dll" })
    {
        try
        {
            string sourcePath = Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), filename);
            string targetPath = Path.Combine(Path.GetDirectoryName(typeof(Util).Assembly.Location), filename);

            File.Copy(sourcePath, targetPath, true);
        }
        catch (IOException ex)
        {
            unchecked
            {
                const int fileInUseHresult = (int)0x80070020;
                if (ex.HResult != fileInUseHresult)
                    throw;
            }
        }
    }
}

#endregion

The error occurs on this line:

js.Execute(typeScriptSource);

I have created a .zip file with everything, you need LINQPad to load the .linq file and experiment. The ClearScript dll's are created from the unmodified source but if you don't trust me you should be able to reproduce those yourself (if you don't have them that is).

It is available here: Dropbox Link to SO19023498.zip.

What I've tried:

I tried executing this code first:

var WScript = new ActiveXObject("WSH.WScript");

This only produced this error: Automation server can't create object

I did not see WSH.WScript in the registry under HKEY_CLASSES_ROOT so that might be it.

I tried figuring out how to create the object from .NET and setting it into the script context, but I am apparently not looking in the right place.

解决方案

Here's a skeletal sample using the current version of typescript.js. Note that the TypeScript API is reverse-engineered; as far as I can tell the only official interface is at the tsc command line. Also keep in mind that this is a minimal sample that does no error handling or reporting:

using System;
using System.IO;
using Microsoft.ClearScript;
using Microsoft.ClearScript.V8;

namespace Test
{
    public class TypeScriptCompiler
    {
        private readonly dynamic _compiler;
        private readonly dynamic _snapshot;
        private readonly dynamic _ioHost;

        public TypeScriptCompiler(ScriptEngine engine)
        {
            engine.Evaluate(File.ReadAllText("typescript.js"));
            _compiler = engine.Evaluate("new TypeScript.TypeScriptCompiler");
            _snapshot = engine.Script.TypeScript.ScriptSnapshot;
            _ioHost = engine.Evaluate(@"({
                writeFile: function (path, contents) { this.output = contents; }
            })");
        }

        public string Compile(string code)
        {
            _compiler.addSourceUnit("foo.ts", _snapshot.fromString(code));
            _compiler.pullTypeCheck();
            _compiler.emitUnit("foo.ts", _ioHost);
            return _ioHost.output;
        }
    }

    public static class TestApplication
    {
        public static void Main()
        {
            using (var engine = new V8ScriptEngine())
            {
                var compiler = new TypeScriptCompiler(engine);
                Console.WriteLine(compiler.Compile("class Foo<T> {}"));
            }
        }
    }
}

阅读全文

相关推荐

最新文章