Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing and using JScript.NET "functions only" exe assembly

1. Compiled Assembly from JSC

I've compiled what is intended to be client-side JavaScript using the JScript compiler (jsc.exe) on the server side in an attempt to make something that can be tested from a unit testing project, and maybe even something that can be debugged on the server side.

The compiled file contains only functions as follows (just for example) and it compiles fine into BitField.exe. Notice, no wrapper class or package in the source code.

------ BEGIN FILE (BitField.js) -------

function BitField(){
    this.values = [];
}
// more functions ...

------- END FILE -------

jsc /fast-  /out:BitField.exe Bitfield.js

Results in a BitField.exe assembly.

Success! Well, kind of ....


2. Testing Assembly / Access Point?

Secondly I've created a test project (in C#) and referenced in the BitField.exe assembly successfully. (The type of project is irrelevant but I'm providing more description to paint a full picture.)

The problem seems to be: I cannot find the namespace or a point at which I can access the BitField functions inside the BitField.exe assembly from my C# test project. The assembly doesn't seem to be a "normal".

In other words I need in C#

using ???WHAT???

Note: I don't want to use JScript "extensions", meaning keywords that won't run client-side (in a web browser), for example, class, package etc because I want the code to be clean as possible for copy & paste back into client side script environment (Regardless said "clean" code compiles fine by jsc.exe without use of those extensions). When I try to wrap the functions in package and class it starts producing compile errors so that's another reason not to use them - because they appear to make me alter my code.

Any suggestions as to how I can use the functions of the compiled JScript assembly (by having it referenced into another assembly) when there are no explicit containers in it?


Update / Proof

.NET Reflector view
alt text

like image 890
John K Avatar asked Aug 21 '10 00:08

John K


1 Answers

After playing around with it for a while, and trying various combinations of command-line switches for jsc.exe, I'm pretty sure that what you're trying to do won't work as you'd wish it to. If you try to compile a js file that contains functions into a .Net library assembly, you get an error:

BitField.js(1,1) : error JS1234: Only type and package definitions are allowed inside a library

But, there is hope, yet! Here's what I would do...

I would keep your "clean" BitField.js file just as it is, and then create a batch file that wraps it in a JScript class and writes it out to a "dirty" js file. It's pretty clean if you think of it as part of the compilation of the code into the DLL. The code to wrap the BitField.js into BitFieldClass.js would look like this:

merge-into-class.js

var fso = new ActiveXObject("Scripting.FileSystemObject");
var ForReading = 1;
var inputFile = fso.OpenTextFile("BitField.js",ForReading, false);
var outputFile = fso.CreateTextFile("BitFieldClass.js", true);

outputFile.write("class BitFieldClass{\n");
while (!inputFile.AtEndOfStream)
{
    var textLine = inputFile.ReadLine();
    outputFile.write (textLine + "\n");
}
outputFile.write("}");
outputFile.close();

Then the batch file to wrap it and compile it is really simple:

compile-js.bat

cscript merge-into-class.js
jsc /t:library /out:BitFieldClass.dll bitFieldClass.js

Of course, if you wanted to do multiple files, you'd have to parameterize things a bit, but hopefully this is enough to demonstrate the idea.

like image 165
Chris Jaynes Avatar answered Jan 04 '23 11:01

Chris Jaynes