views:

93

answers:

1

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.


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 is: I cannot find the namespace or a point at which I can access the BitField functions inside the BitField.exe assembly file within my test project. It doesn't seem to be a "normal" assembly.

In other words I need 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:
.NET Reflector view
alt text

+1  A: 

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.

Chris Jaynes
That is a viable solution and I like that it can be automated.
John K
So my answer wasn't good enough to earn the bounty? :(
Chris Jaynes
In fact I'm hoping for a more direct solution instead of a workaround, but that might be in vain. Therefore I decided to leave the bounty decision to the community through the upvote mechanism. I have the feeling there's an answer out there to describe why this problem is occurring with a direct solution. But only time will tell. Even the bounty didn't draw in much activity so I suspect this is a tough one. In the meantime I will use the workaround and appreciate it.
John K
That's fair. I don't think you're going to find a direct solution, though, based on the way the compiler works. It seems to compile all of the code outside of a class into an overall "script" class, similar to how Groovy compiles scripts. The folks who wrote the compiler seem to have gone to great lengths to make sure you can't access that script class directly, so I'm guessing you're out of luck, but I hope you find what you're looking for!
Chris Jaynes