Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

In C/C++, there is a keyword that allows you to enter assembly language directly into a method.

Example

int Main()
{
    __asm // notify the compiler that this block is assembly language.
    {
        push ax;
        xor ax, ax;
        int 33h;
        mov ax, 1;
        int 33h;
        pop ax;
        retf;
    }

    return 0;
}

Believe it or not, my memory has this little snippet of assembly language imprinted for life, or at least until I become senile! It's a little old-school DOS command that calls the mouse interrupt and shows the mouse cursor. That aside, depending on the C/C++ compiler in question, this would be compiled to run on execution of the Main() method.

A few years on, I am a C# and JavaScript developer...can you guess where I'm going with this?

In ASP.NET, We can develop the server-side functionality of a web application with C# (or other .NET languages), and the client-side functionality with JavaScript.

Sometimes we need to be able to pass JavaScript scripts to the client, from the server, because the server is going to provide some information which will be required by the script. Currently these need to be written as strings, which can cause a headache when it comes to debugging!

Example

string js = "function test() { alert(\"" + message + "\"); }";
aspLiteral.Text = ScriptWrapper.Wrap(js); //assume this just wraps a script into a <script> tag.

What if we could enter JavaScript directly into the server-side code, thus giving more exposure to the debugger, intellisense, code-completion etc.

Example

DateTime x = DateTime.Now;
string message = "Hello World";
var script = javascript //special keyword notifies the compiler that this block is JavaScript.
{
    function test() {
        alert(@message); //Tell the compiler to evaluate C# message to JS message
        var x = @x; //Tell the compiler to evaluate C# x to JS x
    }
}

aspLiteral.Content = ScriptWrapper.Wrap(script);

// output

<script type="text/javascript">
    function test() {
        alert("Hello World");
        var x = \/Date(1371561853440)\/;
    }
</script>

I know this seems like a monumental mountain to climb, possibly to the point of insanity...Am I the only one who thinks this could be useful, or, does its, somewhat limited use, just not warrant the implementation of this into future compilers / IDEs?

share|improve this question

marked as duplicate by FrustratedWithFormsDesigner, Martijn Pieters, Dynamic, gnat, Jalayn Jun 18 '13 at 17:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I suspect you are trying to figure out how to wedge this feature that you are familiar with in C/C++ into JavaScript because you're as not familiar with the latter and how it can be used to insert dynamic elements into the client. Can you give some more specifics as to exactly what you working on? To me, it feels that you are trying to drive a square peg through a round hole when you don't need to. –  Derek Jun 18 '13 at 13:56
6  
The problem isn't that we're dealing with 2+ languages (C#, Javascript) - the problem is that the code runs on two different machines (C# on the server, Javascript on the client). –  Dan Pichelman Jun 18 '13 at 13:59
    
@Derek, there's nothing in particular I'm working on which led me to ask. However whilst working on a project in my workplace, I came across a very long JavaScript function (written as a string), which just initializes some logic for a jQuery datepicker, but it requires some server-side variables. I know there are definitely better ways of doing it..(it's not my code). So I thought to myself, surely this would be better if we could instruct the compiler that this was just a JavaScript block, so do nothing with it, except evaluate it as a string, ready for passing to the client. –  series0ne Jun 18 '13 at 14:02
    
@DanPichelman, I know...I'm not expecting the javascript to run on the server, I'm just stating that the compiler shouldn't try to compile the JS block, rather, just instruct the compiler to express this as a string, but allowing you to have things like debugging, intellisense and code-completion in the IDE. –  series0ne Jun 18 '13 at 14:04
1  
show 1 more comment

2 Answers

You're clearly not the only one who finds this useful, as it is done all over the place as you've shown above. From putting compiler optimized code into methods for speed, to using scripting languages for their simplicity, people have been co-mingling code for quite a while. The real trick here though, is getting the compiler to know what the heck you're trying to do.

Most compilers are made to interpret 1 language, that is the one they are designed for. The reason that the ASM keyword works so well in C is that ASM is an intermediary step for the C compiler, that is it puts all the C code it finds into ASM equivalents, so when you specify with the __asm keyword, you're saying "Hey compiler, don't worry about turning this into assembly, I've done that for you!" and the compiler goes on its merry way inserting your premade code wherever it needs it before converting it to binary.

When you're doing things like assembling javascript inside of java, that gets more complicated. Why? Because javascript is NOT an intermediary step from java to binary. This means that not only does the compiler have to be able to read java, it has to be able to read javascript as well! On top of that, it has to know how to integrate the two of them when their only common step is binary! As you can see, this gets a little more tricky. That's often the reason that the javascript is put into strings, as it is often passed off to another method that will interpret the javascript and then turn that into java, which the compiler then recognizes and compiles.

So to sum it up, if you want to be able to do this, every compiler would need to be able to successful interpret N languages, where N is the number of different languages you would use in each source file. It'll work for 1 or 2 languages, but when you start getting into 3+ you're looking at exponential code growth. What happens if language 2 is nested in language 3 which is nested in language 1? How does the compiler interpret that? It seems simple on a case by case basis, but coming up with some pre-defined algorithm to automatically sort those kinds of problems out is no small task.

share|improve this answer
    
That's a great answer, and I see your point about asm being an intermediary for C. This example would work differently (as you pointed out, JS is not an intermediary for C#, or Java), instead the javascript keyword would say "hey compiler, this block is JavaScript, so give me debugging, little red squigglies when I make a typo, intellisense and code completion please...and then give me the javascript back as a fully evaluated string, thanks (oh and don't bother trying to compile it...you won't understand it!)...you see? –  series0ne Jun 18 '13 at 14:08
4  
Yeah but if you want code completion and intellisense, you're already 7/8 of the way to compilation. The reason that you get red squiglys under words or methods when writing code is because there is a compiler sitting in the background attempting to compile each line/method/class as you write it and saying "Hey, I've got an error on line X at position N" and then the IDE highlights that word and says "Hey! That won't work!" So you still have the problem of having to compile multiple languages. –  Ampt Jun 18 '13 at 14:11
    
I see what you mean, I guess looking a little deeper the complications seem to bubble up out of the think-tank. In terms of compilation, the C# compiler would still need to provide a definition for message and x to the javascript block, and it would need to do so at runtime as for example, DateTime.Now is always different, so it can't just compile this as a constant. The only other way I can think of expressing this is like the code you see in ASPX/CSHTML pages...this is like telling the compiler "hey ignore all of the HTML markup, just concentrate on the C# code and give me back some HTML" –  series0ne Jun 18 '13 at 14:25
    
@series0ne, I can understand why you would find this technique useful, as Ampt eloquently points out above, but I think your example of trying to generate JavaScript in C# code isn't the best. Since you can have C# and JavaScript code in the same project, what you probably are wanting to accomplish can most likely already be done using the standard features of JavaScript and ASP.NET. A better example would be if you can insert assembly code in the middle of a C# program. –  Derek Jun 18 '13 at 14:34
    
@Ampt Yeah, but that 1/8 is the one that deals with assembly/bytecode. So I'm not sure that your argument is valid. –  svick Jun 18 '13 at 15:19
show 3 more comments

This is exactly what ASP.NET (or the more modern Razor) already does, only in reverse. So, you can't easily have C# with inline JS, but you can have JS/HTML with inline C#, which I think will work just as well.

share|improve this answer
    
I believe that you sir, have given me a viable solution! –  series0ne Jun 18 '13 at 14:48
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.