Quantcast
Channel: ClearScript
Viewing all 2297 articles
Browse latest View live

New Post: add function to scriptengine

$
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");

New Post: add function to scriptengine

$
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!

New Post: add function to scriptengine

$
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);
            }
        }
    }

New Post: add function to scriptengine

$
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.

New Post: add function to scriptengine

$
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

New Post: add function to scriptengine

$
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!

New Post: add function to scriptengine

$
0
0
i thin the problem is the delegate.

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

New Post: add function to scriptengine

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

New Post: add function to scriptengine

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

New Post: add function to scriptengine

$
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?

New Post: add function to scriptengine

$
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?

New Post: add function to scriptengine

New Post: add function to scriptengine

$
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?

New Post: Global camel case support

$
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).

New Post: add function to scriptengine

$
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));

New Post: add function to scriptengine

$
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)));

New Post: add function to scriptengine

New Post: add function to scriptengine

New Post: Passing Action/Func into JS?

$
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 */ });

New Post: Passing Action/Func into JS?

$
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!
Viewing all 2297 articles
Browse latest View live


Latest Images