我嘗試使用ClearScript加載編譯器TypeScript以編譯一些基本的TypeScript代碼。將TypeScript編譯器加載到ClearScript中,「WScript未定義」,不可能的任務?
不幸的是,在執行打字稿編譯器源代碼,當我得到這個錯誤:
'WScript' is undefined
這是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");
這隻能產生這樣的錯誤:
Automation server can't create object
我沒有看到
WSH.WScript
註冊表在HKEY_CLASSES_ROOT下,這可能是它。我試着搞清楚如何從.NET創建對象並將其設置到腳本上下文中,但我顯然沒有在正確的位置查找。
請注意,我不知道如果調用編譯打字稿代碼是正確的,也許它返回結果的JavaScript代碼,但程序在該步驟之前失敗。 –
WScript對象不是可創建的,儘管您可以用C#類很容易地模擬它。 tsc.js腳本還需要ActiveXObject,它是一個JScript-ism,但如果你想使用V8(我假設你這樣做,因爲你要經歷安裝ClearScript的V8 DLLs),也可能被模擬出來。儘管如此,所有的工作都是有點工作的,所以我會推薦Ela的方法如下。 – BitCortex