Closed dkottow closed 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.
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
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:
Any suggestions how this can be done? Thank you