fholm / IronJS

IronJS - A JavaScript implementation for .NET
http://ironjs.wordpress.com
Apache License 2.0
680 stars 79 forks source link

how to return null from a CLR function exposed to Javascript #91

Closed dkottow closed 12 years ago

dkottow commented 12 years ago

I want to expose a C# function to Javascript that normally returns a number but may also return null (undefined). I have not found a way to handle this, I have tried to convert Undefined to a CommonObject but without success.

I am copying a simple example below. Here we define a function inverse that should return the inverse of a number if not zero, otherwise, undefined:

class Test
{
   IronJS.Hosting.CSharp.Context mContext;

    public Test()
    {
        mContext = new IronJS.Hosting.CSharp.Context();
        var inverse= JSUtils.CreateFunction<Func<double, CommonObject>>(mContext.Environment, 1, inverse);
        mContext.SetGlobal("inv", inverse);

        ret = ctx.Execute("inv(2)"); //returns 0.5
        ret = ctx.Execute("inv(0)"); //Exception  
    }

    CommonObject inverse(double arg)
    {
        if (arg > 0) return TypeConverter.ToObject(mContext.Environment, 1.0 / arg);
        return TypeConverter.ToObject(mContext.Environment, Undefined.Instance); 
    }
}

Any suggestions how this can be done? Thank you

fholm commented 12 years ago

The function to use is TypeConverter.ToBoxedValue(double d), and the IronJS.Runtime.BoxedValue struct. You're trying to convert undefined into an object which is not a valid operation in JavaScript. And boxing is not done with the JS object type, but rather with a special internal type inside IronJS (it's faster, smaller and resides on the stack) called BoxedValue.

Your inverse function should be written like this:

BoxedValue inverse(double arg)
    {
        if (arg > 0) return TypeConverter.ToBoxedValue(1.0 / arg);
        return Undefined.Boxed;
    }

Note that I have not tested this as I don't have access to visual studio on the computer I'm on currently, but it shows you the basics.

dkottow commented 12 years ago

Thank you Frederik,  worked fine.


From: Fredrik Holmström reply@reply.github.com To: dkottow dkottowk@yahoo.com Sent: Monday, May 7, 2012 12:22 PM Subject: Re: [IronJS] how to return null from a CLR function exposed to Javascript (#91)

The function to use is TypeConverter.ToBoxedValue(double d), and the IronJS.Runtime.BoxedValue struct. You're trying to convert undefined into an object which is not a valid operation in JavaScript. And boxing is not done with the JS object type, but rather with a special internal type inside IronJS (it's faster, smaller and resides on the stack) called BoxedValue.

Your inverse function should be written like this:

BoxedValue inverse(double arg)
    {
        if (arg > 0) return TypeConverter.ToBoxedValue(1.0 / arg);
        return Undefined.Boxed;
    }

Note that I have not tested this as I don't have access to visual studio on the computer I'm on currently, but it shows you the basics.


Reply to this email directly or view it on GitHub: https://github.com/fholm/IronJS/issues/91#issuecomment-5553907