Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bug. #46

Closed
treenewlyn opened this issue Jun 23, 2011 · 8 comments
Closed

A bug. #46

treenewlyn opened this issue Jun 23, 2011 · 8 comments
Assignees
Milestone

Comments

@treenewlyn
Copy link

JS Code:

show(Add(123,4));

C# code:
static void Print(object value)
{
MessageBox.Show(value.ToString());
}
static double Add(double x, double y)
{
return x + y;
}

And..... I changed the source with this:

    [Serializable]
    public class TestClass : IronJS.Hosting.CSharp.Context
    {
        private static readonly Type UtilsType = typeof(IronJS.Native.Utils);
        private static readonly System.Reflection.MethodInfo CreateFunctionMethod = UtilsType.GetMethod("CreateFunction", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
        private delegate FunctionObject CreateFunctionHandler<T>(IronJS.Environment env, int? length, T @delegate);

        public void SetFunction<T>(string name, T @delegate)
        {
            var d = Delegate.CreateDelegate(typeof(CreateFunctionHandler<T>), CreateFunctionMethod.MakeGenericMethod(typeof(T))) as CreateFunctionHandler<T>;
            var f = d(this.Environment, null, @delegate) as FunctionObject;
            this.SetGlobal(name, f);
        }
    }

.......

NOW.

 var c = new TestClass();
        context = c;
        context.CreatePrintFunction();
        context.Environment.BreakPoint = breakPoint;

        c.SetFunction("Add", new Func<double, double, double>(Add));
        c.SetFunction("show", new Action<object>(Print));

It's so easy.

@treenewlyn
Copy link
Author

Oh, the bug is display Text:

MessageBox.Show("IronJS.BoxedValue");

@treenewlyn
Copy link
Author

I must change the Print method:
But,It's ugly.

static void Print(object value)
{
if(value is BoxedValue)
{
value = ((BoxedValue)value).ClrBoxed;
}
MessageBox.Show(value.ToString());
}

@fholm
Copy link
Owner

fholm commented Jun 23, 2011

What is the actual bug/issue here? Not following your code/logic at all.

@treenewlyn
Copy link
Author

In the javascript code:

ClrFunction1(ClrFuntion2());

the ClrFunction1 argument is a BoxedValue.
But i want the ClrFuntion2() Value.

@fholm
Copy link
Owner

fholm commented Jun 23, 2011

There is currently no support for passing delegates directly into IronJS, they have to be wrapped as HostFunctionObject instances, check this https://github.com/fholm/IronJS/wiki/Exposing-a-CLR-function-as-a-native-JavaScript-function post on the wiki for reference.

@ghost ghost assigned fholm Jun 23, 2011
@treenewlyn
Copy link
Author

The second:

If the C# has a Method like this:

static void ClrInvokeJS(Action s)
{
s("Hello");

}

Now, I register this Method to JSFunction.

In the jsCode,I wan :

function jsfunctionTest(s)
{
print(s);
}

ClrInvokeJS(jsfunctionTest);

Of course, Compile failed now. But this is a good support ideas!

@treenewlyn
Copy link
Author

Oh ,sorry. The second is support!!

    static void ClrInvokeJS(BoxedValue s)
    {
        if(s.IsFunction)
        {
            s.Func.Call(null, "Hello");
        }
       // s("Hello");
    }

@treenewlyn
Copy link
Author

Closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants