11

Can any one point out as where can I get some tutorials about IronJS and how to call a method written in IronJS from C# 4.0

Thanks

C#4.0, IronJS

Chris Pietschmann
  • 29,502
  • 35
  • 121
  • 166

4 Answers4

10

There is now some good information from the author on the GitHub project wiki:

https://github.com/fholm/IronJS/wiki

There is a 'first steps' blog post here:

http://blog.dotsmart.net/2011/04/20/first-steps-with-ironjs-0-2/

And I have written several blog posts on IronJS including one that stej has linked. The post stej linked is actually current, but it only covers some basic aspects of embedding. IronJS has undergone a radical rewrite since my first posts so I have put notices on those posts directing to newer updates.

This post specifically covers the original poster's question about how to call JS code from C#:

http://newcome.wordpress.com/2011/03/13/embedding-ironjs-part-ii/

Here is a quick summary:

IronJS.Hosting.Context ctx = IronJS.Hosting.Context.Create();
ctx.Execute("hello = function() { return 'hello from IronJS' }");
IronJS.Box obj = ctx.GetGlobal("hello");
Func<IronJS.Function,IronJS.Object,IronJS.Box> fun =
    obj.Func.Compiler.compileAs<Func<IronJS.Function,IronJS.Object,IronJS.Box>>(obj.Func);

IronJS.Box res = fun.Invoke(obj.Func, obj.Func.Env.Globals);
Console.WriteLine( res.String );
dnewcome
  • 2,045
  • 17
  • 15
  • I updated the tag wiki to include some of the links and content from the readme. – Paul Tyng Jan 09 '12 at 16:33
  • With the latest IronJS this code snippet is depreciated as there isn't a 'IronJS.Box' there is an 'IronJS.BoxedValue' but it doesn't have a 'Func.Compile' method. The docs for IronJS are frustratingly lacking. – CmdrTallen Feb 03 '12 at 14:24
7

Checkout https://github.com/fholm/IronJS/wiki for guides on using IronJS

thr
  • 19,160
  • 23
  • 93
  • 130
5

If you have a Context, you can call Context.CompileSource() and pass its results to Context.InvokeCompiled(), or just call Context.Execute() and pass it the source code. Roughly, this:

IronJS.Hosting.Context ijsCtx;
ijsCtx = IronJS.Hosting.Context.Create();
ijsCtx.Execute("(function(){return 42;})()");
Gabe
  • 84,912
  • 12
  • 139
  • 238
  • Sir, 1 more question.. Last day i was trying to get the IronJS class from the available dlls. But not able to find out. Could you please let me nknow in which assembly it is ? –  Oct 26 '10 at 03:19
  • IronJS is a namespace, not a class. – Gabe Oct 26 '10 at 03:21
  • Yes its a typo.. I mean in which dll i will get this IronJS.Hosting.Context ? –  Oct 26 '10 at 05:45
  • It's in the IronJS.dll which will be in the `bin` directory after you build the project – Gabe Oct 26 '10 at 06:44
1

You might have a look at Embedding IronJs. But it looks outdated as well as the answer by @Gabe.

Currently it should be called like this:

var o = new IronJS.Hosting.Csharp.Context
o.Execute('var a = 10; a');
stej
  • 28,745
  • 11
  • 71
  • 104