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

calling .net method using params from javascript

Jan 29, 2012 at 6:20 PM

  I was wondering if there is a way to export methods from .Net to javascript that allow a variable number of arguments?  I'm trying to host some particular code that expects some of its hooks to support multiple parameters.

  Thanks.  I've just started using Jurassic, it seems good so far and the documentation is excellent.

Coordinator
Jan 29, 2012 at 7:43 PM

Have you tried using a params object[] argument?  This should do what you want.

Jan 29, 2012 at 11:51 PM

Thanks that did work.

Coordinator
Jan 30, 2012 at 3:40 AM
Edited Jan 30, 2012 at 3:40 AM

Thinking about it a bit more, you should also be able to use params string[], params int[] or whatever type you need, not just object.

Jan 30, 2012 at 4:17 AM
Edited Jan 30, 2012 at 4:29 AM

Thats true, I did end up using params string[].

Still learning, now I have an ObjectInstance with a method (marked with JSMethodAttribute) that returns another ObjectInstance.  The only problem is, I can't see to access member methods (also marked with JSMethodAttribute) on the return value.

In short, should the below work?  When I call isFile() on the result of statSync, I get exception "Jurassic.JavaScriptException : TypeError: 'fileStats.isFile' is not a function":

public class NativeFS : NativeBase  // NativeBase inherits from IbhectInstance
{
    ...  
    [JSFunction(Name = "statSync")]
    public ObjectInstance statSync(string path)
    {
        return new NativeFileStat(_engine, AbsolutePath(path));
    }
}

public class NativeFileStat : ObjectInstance
{
    ...
    [JSFunction(Name = "isFile")]
    public bool isFile()
    {
        return System.IO.File.Exists(_path);
    }
}
Coordinator
Jan 30, 2012 at 4:52 AM

Yep, that should work fine.  Are you calling PopulateFunctions() in the constructor?  The PopulateFunctions() method is the bit that looks up the [JSFunction] attributes and hooks them up as JavaScript-callable functions.

Jan 30, 2012 at 5:12 AM

Ahh yes, I wasn't calling PopulateFunctions() from those objects.  Good call (here's the hackery: https://github.com/fschwiet/YUNoAMD/commit/afdbc8f469639f289d4b0b62e1274d859e73a591, trying to run requireJS optimization in .NET)