This project has moved. For the latest updates, please go here.

Calling Namespaced Functions / Objects in Jurassic

Oct 31, 2012 at 9:03 AM
Edited Oct 31, 2012 at 9:04 AM

is there any way to call a namespaced function directly within jurassic for eg:

//------------------------------- JS CODE -------------------------------

var Namespace1 = {}

Namespace1.Level1 = {}

Level1.sqr = function(x){ return x * x; }

 

the way I call "sqr" currently is

//------------------------ CS CODE (CURRENT) --------------------------

 var n= (Jurassic.Library.ObjectInstance)engine.GetGlobalValue("Namespace1");
 var l = (Jurassic.Library.ObjectInstance)Geo.GetPropertyValue("Level1");
 var s = (Jurassic.Library.FunctionInstance)m.GetPropertyValue("sqr");

s.Call(null, 5);

 

is there any way that I can do something like

engine.Call("Namespace1.Level1.sqr",5);

Coordinator
Oct 31, 2012 at 10:06 PM

Nope, but creating a helper method to do it is pretty straightforward.

Something like this:

object CallNamespacedFunction(ScriptEngine engine, string name, object[] parameters)
{
  var components = name.Split('.');
  var obj = engine.Global;
  foreach (var component in components)
  {
    obj = obj.GetPropertyValue(component);
  }
  ((FunctionInstance)obj).Call(null, parameters);
}

(Note: needs more error checking)

Nov 1, 2012 at 4:29 AM

Or simply do fn = engine.Evaluate("My.Namespaced.Function")

On Oct 31, 2012 11:06 PM, "paulbartrum" <notifications@codeplex.com> wrote:

From: paulbartrum

Nope, but creating a helper method to do it is pretty straightforward.

Something like this:

object CallNamespacedFunction(ScriptEngine engine, string name, object[] parameters)
{
var components = name.Split('.');
var obj = engine.Global;
foreach (var component in components)
{
obj = obj.GetPropertyValue(component);
}
((FunctionInstance)obj).Call(null, parameters);
}

(Note: needs more error checking)

Read the full discussion online.

To add a post to this discussion, reply to this email (jurassic@discussions.codeplex.com)

To start a new discussion for this project, email jurassic@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Nov 1, 2012 at 4:38 AM
paulbartrum wrote:

(Note: needs more error checking)

Thanks Paul,

for others if it may help, here the working version in form of an extension

 

 static class JurassicExtensions
    {
        public static object CallNamespacedFunction(this ScriptEngine engine, string name,params object[] parameters)
        {
            var components = name.Split('.');
            var len = components.Length - 1;
            var obj = engine.Global as ObjectInstance;
            for (var i = 0; i < len; i++)// component in components)
            {
                obj = (ObjectInstance)obj.GetPropertyValue(components[i]);
            }
            return obj.CallMemberFunction(components[len], parameters);
        }
    }

Nov 1, 2012 at 4:47 AM
Alxandr wrote:

Or simply do fn = engine.Evaluate("My.Namespaced.Function")

That seems to be the cleaner choice

(engine.Evaluate("My.Namespaced.Function") as FunctionInstance).Call(null, parameters);

Nov 1, 2012 at 5:18 AM

Though, ofcause, that'd add the overhead of parsing...

On Nov 1, 2012 5:47 AM, "suryapratap" <notifications@codeplex.com> wrote:

From: suryapratap

Alxandr wrote:

Or simply do fn = engine.Evaluate("My.Namespaced.Function")

That seems to be the cleaner choice

(engine.Evaluate("My.Namespaced.Function") as FunctionInstance).Call(null, parameters);

Read the full discussion online.

To add a post to this discussion, reply to this email (jurassic@discussions.codeplex.com)

To start a new discussion for this project, email jurassic@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Nov 1, 2012 at 6:38 AM
Alxandr wrote:

Though, ofcause, that'd add the overhead of parsing...

The parsing has to happen at least once in any given case after which we can cache the FunctionInstance in a static dictionary for future calls;