What are people using JScript.Net for?
Asked Answered
A

12

43

I'm interested to know who uses JScript.Net and for what sort of applications. Whenever I'm reading MSDN .Net documentation, I always notice the JScript samples but in all the years I've been a C# dev I've never actually known anyone to use it.

What sort of applications are people using this for, and how does it measure, in terms of flexibility, power and general usage, to C#?

[Edit: Just to clarify - I'm not asking what JScript.Net is, I'm asking what people are actually using it for - i.e. interested to know actual usage scenarios and how people have found it to work with]

Alevin answered 13/12, 2009 at 13:25 Comment(2)
Fiddler's scripting engine is based on JScript.NET.Decasyllabic
Thanks @EricLaw-MSFT- I guess you'd know that better than anyone :)Alevin
F
31

Rob - I have used JScript.NET in anger in one place in my code, which is essentially to expose the functionality of its eval method. Here is a cut-down version:

static public class Evaluator
{
    private const string _jscriptSource =
        @"package Evaluator
        {
           class Evaluator
           {
              public function Eval(expr : String) : String 
              { 
                 return eval(expr); 
              }
           }
        }";

    static private object _evaluator;
    static private Type _evaluatorType;

    static Evaluator()
    {
        InstantiateInternalEvaluator();
    }

    static private void InstantiateInternalEvaluator()
    {
        JScriptCodeProvider compiler = new JScriptCodeProvider();

        CompilerParameters parameters;
        parameters = new CompilerParameters();
        parameters.GenerateInMemory = true;

        CompilerResults results;
        results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource);

        Assembly assembly = results.CompiledAssembly;
        _evaluatorType = assembly.GetType("Evaluator.Evaluator");

        _evaluator = Activator.CreateInstance(_evaluatorType);
    }

    static public object EvaluateToObject(string statement)
    {
        try
        {
            return _evaluatorType.InvokeMember(
                "Eval",
                BindingFlags.InvokeMethod,
                null,
                _evaluator,
                new object[] {statement}
                );
        }
        catch (Exception)
        {
            InstantiateInternalEvaluator();
            return null;
        }
    }

You can obviously create overloads for other return types. I can't claim the original idea for this was mine! Uses for this would be, for example, to evaluate a string expression like 3 + 4 to 7 without expression parsing.

Fortyfive answered 14/12, 2009 at 9:10 Comment(8)
Since you are using the JScript compiler here, how is this any simpler than creating a C# snippet and compiling that using the CSharp compiler?Fr
C# doesn't support the eval methodFortyfive
Cool, but is this really using JScript.NET, or just connecting a C# project to the JScript.NET interpreter as a scripting engine, similar to the DLR? I was under the impression the question was what "applications" the language was being used for.Ephemerality
@Ephemerality - "application" has more than one meaning. This is not using it as a scripting engine - the JScript is compiled once and then run as any other compiled .NET code.Fortyfive
Yep that's what I've used it for, the eval. Very very useful since the days of .NET 1.0Zellner
There is a much easier way of doing it : just put the Evaluator class in a source file, compile it with jsc.exe, and reference the resulting DLL. That's also more efficient...Bladdernose
@ThomasLevesque What is in the source file? Only the string contents of _jscriptSource variable, or the entire Evaluator class?Lothians
@Lothians - in Thomas's (excellent) solution, it's just what I have in the string variable in mine.Fortyfive
S
15

I recommend to everybody not to use it or only in homeopathic doses. Unfortunately I have to work with it a lot for legacy reasons, and as was stated before it was developed for .NET 1.1 and was not upgraded since, has no support for generics, no support for delegates, cannot declare events etc. It was never very widespread and is currently not developed anymore (although sometimes Microsoft still fixes a bug I submit). But that's the minor issue: There are major problems in the compiler, valid code may crash it easily and then all you have is an exception thrown somewhere in the depth of jsc.exe with no hint which file is causing the crash let alone which line of code. Here's an example of what happens (I was casting a strongly typed array into an ICollection):

***INTERNAL COMPILER ERROR***
Microsoft.Vsa.VsaException: InternalCompilerError (0x80133021): System.NotSupportedException: Not supported in a non-reflected type.
   at System.Reflection.Emit.SymbolType.GetMember(String name, MemberTypes type, BindingFlags bindingAttr)
   at System.Type.GetMember(String name, BindingFlags bindingAttr)
   at Microsoft.JScript.Convert.GetToXXXXMethod(IReflect ir, Type desiredType, Boolean explicitOK)
   at Microsoft.JScript.Convert.EmittedCallToConversionMethod(AST ast, ILGenerator il, Type source_type, Type target_type)
   at Microsoft.JScript.Convert.Emit(AST ast, ILGenerator il, Type source_type, Type target_type, Boolean truncationPermitted)
   at Microsoft.JScript.Binding.TranslateToILCall(ILGenerator il, Type rtype, ASTList argList, Boolean construct, Boolean brackets)
   at Microsoft.JScript.Lookup.TranslateToILCall(ILGenerator il, Type rtype, ASTList argList, Boolean construct, Boolean brackets)
   at Microsoft.JScript.Call.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.Binding.TranslateToILSet(ILGenerator il, AST rhvalue)
   at Microsoft.JScript.Lookup.TranslateToILSet(ILGenerator il, Boolean doBoth, AST rhvalue)
   at Microsoft.JScript.VariableDeclaration.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.Block.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.FunctionObject.TranslateToIL(CompilerGlobals compilerGlobals)
   at Microsoft.JScript.FunctionDeclaration.TranslateToILInitializer(ILGenerator il)
   at Microsoft.JScript.Block.TranslateToILInstanceInitializers(ILGenerator il)
   at Microsoft.JScript.Class.TranslateToCOMPlusClass()
   at Microsoft.JScript.Class.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.Package.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.Block.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.ScriptBlock.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.ScriptBlock.TranslateToILClass(CompilerGlobals compilerGlobals, Boolean pushScope)
   at Microsoft.JScript.VsaStaticCode.TranslateToIL()
   at Microsoft.JScript.Vsa.VsaEngine.DoCompile()
   at Microsoft.Vsa.BaseVsaEngine.Compile()

Good luck if you have to find the problem with such a message.

JScript.NET has some features that could be interesting for some projects, like prototyping (adding new properties and I think also methods to an existing instance of any object), has kind of Lamda-Functions already since .NET 1.1 (maybe 1.0) etc. But to reach this totally different behavior from what was possible with .NET 1.1 they had to wrap everything in runtime-helpers, JScript.NET has an own String class, that is converted into System.String to and fro whenever needed. Same with arrays, JScript has its own implementation and has to convert them whenever a framework function is called with an array as parameter etc.

I experienced such funny things like that a request to the database was executed twice if I passed it directly as a parameter to a method (DoSomething(myRequest.Execute()), the result of the request was of type System.String[][]) but the whole thing is working fine if I assign the result first to a variable and then pass it to the method, or if you try to sort an array with System.Array.Sort(..) it is simply ignored and so on.

I have the feeling JScript.NET was a case study to proof that it's also possible to implement languages with a very different philosophy, and the Microsoft gave it up as they realized its approach was not well chosen, but they learned the lesson and did an excellent job with PowerShell which has a somewhat similar philosophy.

But to cut a long story short: Don't use JScript.NET if you don't have to, use a modern language like C# or VB.NET instead, and if you need scripting functionality, I recommend using CSScript.

Social answered 10/3, 2011 at 15:28 Comment(1)
+1 one from - that's some very interesting information, that sounds like it comes from bitter experience! Thank you.Alevin
F
7

JScript .NET is one of the languages supported for writing application extensions for Sony's family of media editing products. Sound Forge & Vegas.

In particular Sound Forge has a window that you can use to edit and run scripts to manipulate audio files.

It turns out the Microsoft made it pretty easy to add scripting to your application using one (or all) of the .NET supported languages. VB, JScript & C#. Just just have to expose some objects from your application and hand those objects (assemblies actually) to the compiler along with the user's script text.

They have a web forum for script writers for Sound Forge

Footlambert answered 22/12, 2009 at 4:20 Comment(2)
Would you say it's easier to mess with audio thru JScript.NET than with VB.NET or C#?Chromolithograph
@Shimmy: I think JScript and C# are about equal, while VB.NET is a bit less capable due to the fact that VB has no native unsigned integer types.Footlambert
S
7

I use it quite a lot, almost exclusively, for ASP.NET and related web-stuff.

You can use it for pretty much all the stuff you can use the other .NET languages for. It makes MSIL, just like the others ;) Compiled with the "fast" option which turns of some of the native javascript traits, like prototype extensions and eval, it performs just like any other code on the CLR.

It's certainly not a full-fledged member of the family, in the sense that it seems somewhat left behind in .NET 1.1 in terms of features, the tool support is non-existant, and the documentation is downright appaling. That's part of why I like it so much, actually, makes me feel like I'm programming.

If you have a lot of (j)scripts lying around that needs to be converted to .NET, this is the obvious choice as you will usually need very little rewriting for that to work. Otherwise there's no particular reason for choosing JScript.NET over any other .NET language, and many reasons not to.

It all comes down to personal preferences since it all ends up as the same CLR code, regardless of language. I find JScript.NET easy-going, natural to write and with a flexible simplicity like no other language I tried, so I use that until I encounter a problem that the language just can't handle. Hasn't happened yet.

Skycap answered 6/4, 2010 at 21:57 Comment(1)
Thanks for the answer Torben - that's interesting stuff.Alevin
T
4

With the Business Process Management tool Metastorm BPM you can use JScript.Net to extend your workflows. For example you can add custom .Net .dlls to the engine and interact with them in your processes using JScript.Net.

My experience was quite positive. I've implemented just some small glue scripts but I'd prefer scripting with JScript.Net to VBScript (the second supported Scripting Language) anytime.

Taeniafuge answered 22/12, 2009 at 8:37 Comment(0)
G
4

Well, I'm trying to use it. I can't say I've been successful.

I've not done a lot of web coding in any environment, but here is my 2 cents.

I wanted to expose some database tables to a web page, so I threw together a quick Classic ASP (yeah, yeah, I know) page that pulled the data from SQL Server and returned the table as a JSON object. Rather than locate a JSON serializer for VBScript I just wrote both sides in J(ava)script and imported json2.js on the server to provide serialization. It worked great; honestly it took less than an hour to have it spit back a nice JSON representation of the table, 50 minutes of which was a fruitless attempt to get JScript to import json2.min.js directly rather than throwing <% %> wrappers around it and renaming it json2.min.asp.

But everyone says it is sick and wrong to use Classic ASP in this day and age, so I'm trying to replace my quick & dirty ASP + Jscript + ADO implementation with ASP.NET + Jscript.NET + ADO.NET. After some head-pounding-keyboard moments I have ASP.NET v4.0.30319 up on IIS6 and have a "hello, world" page that does some simple loops javascript via the <@ language = "JScript"> directive. So far so good.

I were following Best Practices here I'd be wrapping everything in a class etc, but as I'm just try to get out of the driveway I tried doing a quick port of my existing code, which imports various useful bits of Javascript from other files using ASP include statements.

Including some simple stuff using <--#incude file="somejsconstants.asp" --> worked fine and let me access constants from that file. But my attempt to pull in the JSON serializer with<--#incude file="json2.min.asp" --> didn't work, and firing up a version of json2.js suitably modified to be a standalone aspx page and threw the same error. (The compiler error is Objects of type 'jscript_aspx' do not have such a member.) JScript.NET seems to be unhappy with the use of closures, a near-omnipresent construction in today's javascript libraries.

From postings above it's clear there are plenty of ways to serialize JSON objects in the .NET platform, but my gut feeling is that if JScript.NET barfs on Javascript written by Douglas Crockford, then it is a quirky, incompatible rathole not worth investing my time in. I might as well follow the ASP.NET masses and use C#.

Gesticulative answered 14/7, 2010 at 17:41 Comment(0)
T
3

As an enhancement for batch files and for console applications as it allows a neat hybridization (without unnecessary output) thanks to its directives.Save the following example with .bat extension and check its result:

@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal

for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
   set "jsc=%%v"
)

if not exist "%~n0.exe" (
    "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)

 %~n0.exe %*

endlocal & exit /b %errorlevel%


*/

import System;

Console.WriteLine("hello from jscript.net");

Here some examples can be found.

Taut answered 15/3, 2015 at 9:48 Comment(0)
S
2

I'm late to the party here, but I want to add my take:

I see JScript.NET being useful for server-side code. It's pretty natural to use something server-side that is essentially the same as what you use client-side. Also, it makes working with JSON that much easier so can be a boon if you're creating a web service that communicates via JSON.

I wrote a web service where I work in C# but am thinking of migrating to JScript.NET because of those reasons.

Siddur answered 11/2, 2010 at 23:3 Comment(0)
I
1

I don't know how it compares performance-wise, but I know that JScript.NET is one of the languages you can use with Unity 3D.

Isotonic answered 18/12, 2009 at 22:16 Comment(1)
Unity3D exposes a JScript like language called UnityScript, but it's not JScript.NET that is used behind. It's the Boo compiler with a JScript frontend.Letitialetizia
C
-2

JScript .NET is a modern scripting language with a wide variety of applications. It is a true object-oriented language, and yet it still keeps its "scripting" feel. JScript .NET maintains full backwards compatibility with previous versions of JScript, while incorporating great new features and providing access to the common language runtime and .NET Framework (from JScript.NET).

Besides, you might find this stackoverflow post helpful as well: Can JScript.NET be used to script a .NET application?

As for who is using, I am honestly not aware of (I am pretty sure there are out there).

Read Introducing JScript.NET.

My personal thought was that it could be awsome for web developers who're used to write in plain JScript and want to get the benefits of .NET without need to learn a new language, or for those who want to have all their code written in one language.

Chromolithograph answered 13/12, 2009 at 13:36 Comment(4)
It should be noted that, with respect to "providing access to CLR and .NET Framework", JScript.NET is somewhat dated. For example, it doesn't know anything about generics, and won't let you reference generic types, or call generic methods. In terms of implementation, it uses reflection heavily with no additional optimizing layers (like DLR), and so is rather slower than e.g. IronPython. Given that it hasn't been updated since VS2005 (and seemingly isn't updated in .NET 4), it seems that IronPython is the official .NET scripting language of choice now.Inlet
@Pavel, definitely agreed, was just trying referring to the OP's request; I am with you on the same page.Chromolithograph
In addition to what Pavel has stated, JScript.Net has not only stagnated since VS2005 - it has been, for the most part, deprecated. JScript.net also does not support ref/out arguments, which is a major pain if you stumble upon it. Fortunately, replacing it with IronPython is quite simple (we use JScript.Net/IronPython as a scripting interface into our apps).Horribly
@Kevin lol, yes right... No Generic support, no Entity-Framework support, no linq no nuttn, I think it's basically a .NET 'scripting' language, rather than a "CLR" language, even it still is.Chromolithograph
P
-3

The JScript.NET Wikipedia page is pretty explanatory: http://en.wikipedia.org/wiki/JScript_.NET

In short, it seems to be JavaScript with the .NET library.

Pam answered 13/12, 2009 at 13:36 Comment(5)
Hi vrutberg, thanks for taking time to answer, but I wasn't really asking what JScript.Net is, I'm asking what people are actually using it for (in the wild), and how they find it in comparison to other .Net languages, especially C#.Alevin
I don't think I'm capable of answering that question. However I guess I could make a sort of educated guess. Since it needs CLR to run I don't think you could use it very efficiently on the web (if at all), so we can put aside client side web programming. With that in mind, I think the answer is pretty close to what Shimmy wrote: "it could be awsome for web developers who're used to write in plain JScript and want to get the benefits of .NET without need to learn a new language". So I think that's part of your answer, web developers who want to try out desktop application programming. :)Pam
I'll be honest - I'm kinda interested to see whether anyone actually replies and says "I use JScript.Net". I've just never known anyone to actually use it in anger.Alevin
I've once used JScript.NET to very quickly write a simple tool to complement the main application, on-site, when the customer asked for this functionality and it was urgently needed right there and then.Inlet
"I've just never known anyone to actually use it in anger" LOLVaricocele
E
-3

I'd just assumed it was used by Microsoft to inflate the number of languages .NET supported at launch for marketing purposes.

Ephemerality answered 21/12, 2009 at 17:30 Comment(10)
-1: Doesn't answer the question, and besides, they implemented the language! That's a lot of work for marketing purposes.Hutto
I think it does answer the question if at least one of its uses is as a bullet point somewhere. Besides, it isn't as if they didn't have several JavaScript implementations laying around already. ;)Ephemerality
The question was who uses it, and what for. The fact that you have made assumptions is not an answer.Hutto
Who uses it? Microsoft! What for? Selling .NET! The fact that no one has yet responded (in much more time than it took SO to generate a superior algorithm for leettime generation, surprisingly) with anything other than simple JScript.NET exercises, and that it is missing basic .NET features like "out" params certainly makes it the most likely answer to the question, doesn't it?Ephemerality
Seriously though, there are some useful answer posted all over this question. Even from some people that say they used to use it before iron python. All of which posted before you. So what was your point again?Henrie
Listen, I like C#, and I'm excited about F#, but let's just be honest here: JScript was created to add a bullet point when .NET needed to show CLR flexibility. You'd better read those posts about IronPython again: "dated", "deprecated", "stagnated", no generics, slower than IronPython, &c. And that single mention, not "all over this question", point to someone embedding it as one of two potential scripting engines, not something being used for development of the project.Ephemerality
I have to say I'm pretty disappointed in the downvotes and the overall level of discourse. So far this the only reply that answers the question, by essentially saying "none" to the question of real-world JScript.NET application development. For some reason, that has angered some folks who would, I guess, like to shoot the messenger.Ephemerality
@John Saunders: I won't say this is a good answer, given that the questioner asked for "What sort of applications ... terms of flexibility, power and general usage" but the actual, rather than declared, purpose of a language is a useful thing to bear in mind when evaluating a language. So -(-1) from mePolonaise
-1 Not created for marketing purposes, but to allow people who used JavaScript in their classic ASP pages to easily port their work to .NetHart
@AshrafSabry Was that really a common enough use case to justify development of a new language?Ephemerality

© 2022 - 2024 — McMap. All rights reserved.