-1

I am writing a raytracer as a part of my complete 3d engine. I am planning on using javascript for the scripting language instead of writing my own. The question is how can I use it? Btw the raytracer and the UI are written in C#.

Belos
  • 281
  • 1
  • 3
  • 14
  • Does your ray tracer actually require scripting? Can you describe the nature of the scripts? It's possible there's a better technology to use for this scripting. – John Saunders Aug 25 '12 at 00:03
  • It will be much easier to create animations. Currently I am using xml and it is very lengthy and harder for animations. Javascript will be less verbose. – Belos Aug 25 '12 at 00:16
  • 1
    Sorry, I guess I have an outdated concept of what a "ray tracer" is. I knew them as programs which helped render a 3D graphic by tracing the path taken by rays of light from the lighting sources. Such a thing would have had no use for scripting. In any case, I question whether it's necessary to jump all the way from XML to JavaScript. Perhaps a domain-specific language would be a good intermediate step. Such a thing could produce code that could be compiled to provide higher performance. – John Saunders Aug 25 '12 at 00:37
  • I want to use javascript as part of the UI not the raytracing library. Javascript will be an easy and fast way to write code. – Belos Aug 25 '12 at 00:50
  • @JohnSaunders but with an answer to this question, it would be richer and less work than a domain specific language from scratch. I'm not answering since I haven't done so since before .NET, but adding full support for a well-known scripting language in half a day through COM sure beats putting a few days into just getting the initial spec down on a custom language. – Jon Hanna Aug 25 '12 at 01:41
  • @JonHanna: to end what is devolving into a discussion, by showing what I meant by "DSL": http://visualstudiogallery.msdn.microsoft.com/b46b7059-1bd6-4efe-bbbe-f6a77acdac9d. It's all about what's the best experience for the users. If the users are familiar with JavaScript, then go for it. Otherwise, it may be best to use something more in line with the problem domain. – John Saunders Aug 25 '12 at 16:41
  • possible duplicate of [Embedding JavaScript engine into .NET (C#)](http://stackoverflow.com/questions/172753/embedding-javascript-engine-into-net-c) – John Saunders Aug 25 '12 at 16:44
  • @JohnSaunders Cool, thanks for that. It looks a bit more involved, but then any new thing looks more involved than the things you already know, so that could be a very unfair impression. – Jon Hanna Aug 25 '12 at 17:00

3 Answers3

5

This shows the two-way interaction between Javascript and c#.

  1. Javascript calls a c# method
  2. C# gets the result of an expression in Javascript

-

Type scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));

dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "javascript";
obj.AddObject("MyClass",new JSAccessibleClass());

obj.Eval("MyClass.MsgBox('Hello World')"); //<--1
var result = obj.Eval("3+5"); //<--2


[ComVisible(true)]
public class JSAccessibleClass
{
    public void MsgBox(string s)
    {
        MessageBox.Show(s);
    }
}
L.B
  • 114,136
  • 19
  • 178
  • 224
1

There are many standalone implementations of JavaScript that you may be able to leverage for this purpose:

Here is a link to the stackoverflow info page on JavaScript which lists at the top many of these implementations: https://stackoverflow.com/tags/javascript/info

Community
  • 1
  • 1
marteljn
  • 6,446
  • 3
  • 30
  • 43
1

While it is possible to use JavaScript as scripting for .Net application it is not most popular choice. If you can pick other language - IronPython or LUA.Net may be choices you'll get better support. I listed several links in similar question recently: Write npc scripts to be executed by c# using class with npc methods.

Otherwise JavaScript.Net (part of .Net framework) or other implementations either explicitly .Net or with .Net bindings will work as scripting language.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179