Quantcast
Are you the publisher? Claim or contact us about this channel


Embed this content in your HTML

Search

Report adult content:

click to rate:

Account: (login)

More Channels


Showcase


Channel Catalog



Channel Description:

ClearScript is a library that makes it easy to add scripting to your .NET applications. It currently supports JavaScript (via V8 and JScript) and VBScript.

older | 1 | .... | 68 | 69 | (Page 70) | 71 | 72 | .... | 115 | newer

    0 0

    thanks but it desnt work correctly.

    i use this:
    var mf = new MethodFunc(me.Invoke);
    se.AddHostObject(mi.Name, mf);
    it throws an Comexception when i run the script who is calling the function via:
    var host = new HostFunctions();
                    ((IScriptableObject)host).OnExposedToScriptCode(e);
                    var del = (Delegate)host.func<object>(p.Length, e.Script[func]);
    
                    return del.DynamicInvoke(p); // here is the Exception
    return filesize("Std.dll");

    0 0

    Hi again,

    Can you provide more information? The two code blocks above appear to be unrelated, with several variables that aren't defined anywhere (mi, func, p).

    If you could post a complete test program that demonstrates the problem, that would be perfect!

    Thanks!

    0 0

    publicstaticclass ModuleLoader
        {
            publicdelegateobject MethodFunc(object target, paramsobject[] args);
    
            publicstaticvoid Load(WindowsScriptEngine se, Type t)
            {
                var ca = t.GetCustomAttribute<ScriptModuleAttribute>();
    
                var tmp = Activator.CreateInstance(t);
    
                if (ca != null)
                {
                    if (ca.AsType)
                    {
                        se.AddHostType(ca.Name != null ? ca.Name : t.Name, t);
                    }
                    foreach (var me in t.GetMethods())
                    {
                        var meca = me.GetCustomAttribute<ScriptFunctionAttribute>();
                        if (meca != null)
                        {
                            var mf = new MethodFunc(me.Invoke);
                            se.AddHostObject(meca.Name != null ? meca.Name : me.Name, mf);
                        }
                    }
                }
                foreach (var me in t.GetProperties())
                {
                    var meca = me.GetCustomAttribute<ScriptMemberAttribute>();
                    if (meca != null)
                    {
                        se.AddHostObject(meca.Name != null ? meca.Name : me.Name, me.GetValue(tmp, null));
                    }
                }
            }
    
            publicstaticvoid Load(WindowsScriptEngine se, Assembly ass)
            {
                foreach (var t in ass.GetTypes())
                {
                    Load(se, t);
                }
            }
        }

    0 0

    It looks like ModuleLoader is designed to install functions and other data into a script engine, but how are you using it? If possible, please post the code that actually executes script code and raises the exception you're seeing.

    0 0

     ModuleLoader.Load(se, Assembly.LoadFile(Application.StartupPath + "\\Std.dll"));
    
    se.Evaluate(File.ReadAllText(p))
    
    var fs = PluginLoader.Call("init"); // run init function who run the imported function

    0 0

    Unfortunately we can't help you without knowing more about the contents of Std.dll, the script code being executed, PluginLoader, the "init function", the exception stack and message, etc.

    If you'd like to post a minimal but complete program that demonstrates the issue you're seeing, we'll be happy to take a look, but it's difficult to figure out what's going on based on the bits and pieces you've provided so far. Sorry!

    0 0

    i thin the problem is the delegate.

    i want to call the delegate myfunc(object,params) as filesize("filename")

    0 0

    Can you post the definition of the .NET method you're calling from script code? The method signature should be enough.

    0 0

    [ScriptFunction(Name = "filesize")]
            publicint FileSize(string filename)

    0 0

    OK, thanks. Note that this is an instance method (as opposed to a static method), so it requires a target (a this reference).

    When script code calls this method, what should the target be?

    There are two ways to go. The caller can provide the target at the call site, or you can expose a delegate that's bound to a target in advance. Both are easy to do. Which shall it be?

    0 0

    i changed all methods to static but now i have a new exception: object expected

    *ive steped through the code, the functions are not added. there are static in the signature but in reflection is isstatic=false

    what do i wrong?

    0 0

    i want to know both

    0 0

    In JScript, "Object expected" can indicate an attempt to call an undefined function. This seems likely given your statement that "the functions are not added". Can you confirm that?

    As for why you aren't seeing static methods in reflected types, it's difficult to tell. Are you sure you've rebuilt the assembly you're inspecting?

    0 0

    +1 for this feature (different cases would confuse any users who are editing scripts and do not have a lot of experience with many languages).

    It would also be nice to have options for blocking all member access by default, and only allowing access through the ScriptMember attribute or conventions which one registers (i.e. all properties of type x, or all types in assembly y).

    0 0

    thanks i forgot to rebuild, but i have the same error like first: The type must be inferred from the delegate
    se.AddHostObject(meca.Name != null ? meca.Name : me.Name, me.CreateDelegate(t));

    0 0

    Right, that won't work, assuming your code is as you posted it earlier.

    If me represents a static method, first declare an appropriate delegate type:
    publicdelegateobject StaticMethodFunc(paramsobject[] args);
    Now you can expose a delegate that invokes the static method:
    engine.AddHostObject(me.Name, new StaticMethodFunc(args => me.Invoke(null, args)));
    By the way, if me represents an instance method, you can still expose a delegate that doesn't require an explicit target. All you have to do is provide the target in advance:
    var target = Activator.CreateInstance(t);  // or create the target some other way
    engine.AddHostObject(me.Name, new StaticMethodFunc(args => me.Invoke(target, args)));

    0 0

    thanks it works

    0 0

    Glad to hear it, thanks!

    0 0

    I am trying to pass a C# delegate/Func/Action as a parameter to a Javascript function (on('event',handler)). Right now, the thing that shows up in the JS side is not a function. What is the expected implementation of a function like this?
    function myJsFunction(callback) {
      callback();
    }
    scriptObject.myJsFunction(new Action(() => { /* YAY IT WORKED */ });

    0 0

    Hello!

    What you're doing should work. The delegate may not appear to be a JavaScript function (that is, the typeof operator would return "object" rather than "function"), but it should be invocable just like a function.

    For example, this code prints "Hello!" as expected:
    engine.Execute("function foo(x) { x(); }");
    engine.Script.foo(new Action(() => Console.WriteLine("Hello!")));
    Are you doing something differently?

    Cheers!

older | 1 | .... | 68 | 69 | (Page 70) | 71 | 72 | .... | 115 | newer