c# – 将TypeScript编译器加载到ClearScript中,“WScript未定义
我尝试使用
ClearScript加载
TypeScript编译器以编译一些基本的TypeScript代码.
不幸的是,在执行TypeScript编译器源时,我收到此错误:
这是我用过的LINQPad程序,将ClearScript dll和TypeScript compiler file放在.linq程序旁边: 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 此行发生错误: js.Execute(typeScriptSource); 我创建了一个包含所有内容的.zip文件,您需要LINQPad来加载.linq文件并进行实验. ClearScript dll是从未修改的源创建的,但是如果你不相信我,你应该能够自己重现这些(如果你没有那些). 它在这里:Dropbox Link to SO19023498.zip. 我尝试过的: >我尝试先执行此代码: var WScript = new ActiveXObject("WSH.WScript"); 这只会产生此错误:自动化服务器无法创建对象 我没有在HKEY_CLASSES_ROOT下的注册表中看到WSH.WScript,所以可能就是这样. 解决方法
在使用ClearScript玩一下之后,实际上让它运行的想法非常酷.
要使代码至少运行一点: var typeScriptSource = File.ReadAllText("typescript.js"); typescript.js是普通的javascript编译器,其中tsc使用WSH和其他com对象… 这样,您也可以使用V8引擎! 但无论如何,这意味着您必须编写一些JavaScript来实际包装TypeScript.TypeScriptCompiler函数. 所以这意味着你必须像在自定义html页面中使用TypeScriptCompiler一样(就像操场一样). Compiler.prototype.compile = function (src) { var outfile = { source: "",Write: function (s) { this.source += s; },WriteLine: function (s) { this.source += s + "n"; },Close: function () { } }; var compiler = new TypeScript.TypeScriptCompiler(outfile); try { compiler.parser.errorRecovery = true; compiler.setErrorCallback(function (start,len,message,block) { console.log('Compilation error: ','n Code block: ',block,' Start position: ',start,' Length: ',len); }); compiler.addUnit(Compiler.libfile,'lib.d.ts'); compiler.addUnit(src,''); compiler.typeCheck(); compiler.emit(false,function createFile(fileName) { console.log(outfile); return outfile; }); console.log('compiled: ' + outfile.source); return outfile.source; } catch (e) { console.log('Fatal error: ',e); } return ''; }; return Compiler; 这看起来很有希望,但遗憾的是,如果我尝试将它与ClearScript一起使用,它会抛出奇怪的错误,也许我做错了… 至少,如果我通过代码调试,js.Scripts.TypeScript被定义并包含所有TypeScript对象! 无论如何,我想这应该会让你更进一步;)让我知道你是否成功了! 作为替代方案,您可以使用命令行工具来简单地调用编译器而不是直接在C#中执行它.我想这应该更容易实现…或者使用node.js包,它还为您提供所需的所有编译器服务. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |