Loading the TypeScript compiler into ClearScript, "WScript is undefined", an impossible task?

I tried using ClearScript to load TypeScript to compile TypeScript base code.

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

'WScript' undefined

This is the LINQPad program that I used, placed the ClearScript dll and TypeScript compiler file along with 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 in this line is:

 js.Execute(typeScriptSource); 

I created a .zip file with everything, you need LINQPad to download the .linq file and experiment. A ClearScript disc is created from an unmodified source, but if you don't trust me, you should be able to play them yourself (if you don't have them).

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

What I tried:

  • I tried to execute this code first:

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

    This caused this error: Automation server can't create object

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

  • I tried to figure out how to create an object from .NET and set it in the context of the script, but I apparently don't look in the right place.

+6
source share
3 answers

Having worked a bit with ClearScript, the idea is pretty cool to make it work.

To get the code, at least a little more work: Instead of using tsc.js, just use typescript.js , which comes with the SDK and should be in the same folder.

 var typeScriptSource = File.ReadAllText("typescript.js"); 

typescript.js is a simple javascript compiler where tsc uses WSH and other com objects ...

So you can also use a V8 engine!

But in any case, this means that you have to code some JavaScript to actually wrap the TypeScript.TypeScriptCompiler functions.

So this means that you have to do the same thing as if you were using TypeScriptCompiler on your custom html page (e.g. on the playground). I could not find the documentation on how to use the compiler so far, though ... But I found one page that also tries to implement this http://10consulting.com/2012/10/12/introduction-to-typescript- presentation / If you look at the source of the presentation, you will find a compiler class.

 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: ', message, '\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; 

This looks pretty promising, but unfortunately, if I try to use it with ClearScript, it causes strange errors, maybe I'm doing something wrong ...

At least if I am debugging the code, js.Scripts.TypeScript is defined and contains all TypeScript objects!

In any case, I think this should take you a step forward;) let me know if you had success!

Alternatively, you can use the command line tool to simply call the compiler instead of doing it from within C # directly. I suppose this should be easier to implement ... Or play with the node.js package, which also provides you with all the necessary compiler services.

+5
source

Here is an example of a skeleton using the current version of typescript.js . Note that the TypeScript API has the opposite construction; as far as I can tell, the only official interface is on the tsc command line. Also keep in mind that this is a minimal sample that does not handle errors and does not report:

 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> {}")); } } } } 
+4
source

After 4 years, using TypeScript 2.7 Services at https://rawgit.com/Microsoft/TypeScript/master/lib/typescriptServices.js now it looks like a two-line:

  • initialize your favorite JS engine with this script
  • load the contents of the string of your resource * .ts
  • get a link to the ts.transpile function
  • pass the script type to it as a string and return the string with the passed source

Using, for example, the ClearScript engine in C # with the TS source code in the src line, this would be:

 dynamic transpile = engine.Evaluate("ts.transpile"); var js = transpile(src); // now feed the string js into a different JS engine 
+1
source

Source: https://habr.com/ru/post/954660/


All Articles