Suggestions? Comments? Ideas?

Coordinator
Apr 16, 2009 at 1:54 AM
Thanks for visiting! Do you have a suggestion, an idea, or a comment? Post your feedback on the discussion board.
Jun 29, 2010 at 4:13 PM
Edited Jun 29, 2010 at 4:14 PM

Is this project still active? Will there ever be support for functions/delegates? I'm using it in one of my projects and it works great except for the lack of function/delegate support

Coordinator
Jun 30, 2010 at 3:14 AM

I am glad to hear that it works great in your project. I haven't done much with it due to an apparent lack of community interest. I most certainly can add delegate support. In terms of priorities, would it be more important to be able to expose a delegate in a .NET class to a JS script or be more important to be able to pass a JS function to a .NET class?

Jun 30, 2010 at 7:37 AM

I'd like to have a JS function exposed to .Net. I actually started trying to implement this and it sort of worksish (I get a memory violation, probably because I don't know how to use V8 and I'm new to C++ :P). Basically I made a V8FunctionWrapper Class which gets returned instead of a delegate^. The .Net code would just call do something like this:

http://pastie.org/1024622

I'm having some trouble getting it work right though (when I call function->Call() from c++ I get a AccessViolation exception). I can send you what I have if you want but I wouldn't recommend using it too much :P

 

The plus side to use V8FunctionWrapper is that since it keeps track of the native pointer you just do

theFunctionWrapper->GetV8Handle()
to get the native handle back.

Jun 30, 2010 at 7:41 AM
Edited Jun 30, 2010 at 7:57 AM

 

Here's the V8FunctionWrapper class

#include "v8functionwrapper.h"
#include "v8value.h";


v8sharp::V8FunctionWrapper::V8FunctionWrapper(v8::Handle<v8::Value> value)
{
        // _value is a v8::Handle<v8::Value>*. we need to do that to avoid errors with mixed types in VS2008
	this->_value = new v8::Handle<v8::Value>(value);
	// this returns true so we're good here
	Console::WriteLine("Constructor: {0}", (*this->_value)->IsFunction());
	
	
}

// This function is called by C# and triggers the javascript function
// this lets you do wrapper.Call(any, amount, of, args, here, work);
Object^ v8sharp::V8FunctionWrapper::Call([ParamArray]array<Object ^> ^ args)
{
	// Get a refence to the function
	
	// True
	Console::WriteLine("Is Function: {0}", (*this->_value)->IsFunction());
	// True
	Console::WriteLine("Is Object: {0}", (*this->_value)->IsObject());

        // dont look at me like that
       // i have no idea if this is the proper usage
       // this looks believable to me though
       // webkit source has something like this too
       // so i went with it
	v8::Handle<v8::Object> obj((*this->_value)->ToObject());
	v8::Handle<v8::Function> function(v8::Function::Cast(*obj));

	// True again
	Console::WriteLine("function: is function:  {0}", function->IsFunction());

	

	v8::Handle<v8::Value>* jargs;
	
	// convert the args into a v8 compatiable array
	if(args->Length > 0)
	{
		 jargs = new v8::Handle<v8::Value>[args->Length];
	// Convert .net objects to v8::Handle<Value> s
	for(int i = 0; i < args->Length; i++)
		jargs[i] = v8sharp::V8ValueWrapper::Unwrap(args[i]);
	}
	else
	{
		jargs = new v8::Handle<v8::Value>[1];
		jargs[0] = v8::Null();
	}
	
	
	
	// fail here..
	function->Call(obj->ToObject(), 0, jargs);
	// just return null for now until i figure out how to get the above working..
	return v8sharp::V8ValueWrapper::Wrap(v8::Null());
}
v8::Handle<v8::Value> v8sharp::V8FunctionWrapper::Unwrap()
{
	v8::Handle<v8::Value> ret = *this->_value;
	return ret;
}
v8sharp::V8FunctionWrapper::~V8FunctionWrapper()
{
	this->!V8FunctionWrapper();
}
v8sharp::V8FunctionWrapper::!V8FunctionWrapper()
{
	// Free up the memory
	(*this->_value).Clear();
	//~(*this->_value);
	delete this->_value;
}


 

Coordinator
Jul 1, 2010 at 3:29 AM

I just added support for registering a delegate with the script engine and for allowing JS scripts to pass in functions into .NET methods. Right now it is not as clean as I would like but I wanted to get something out there. I am considering revamping the registration APIs to make maintenance easier. At that time, I plan to clean it up quite a bit and add some unit tests to the project as well.

Jul 3, 2010 at 7:37 AM

Thanks I can't wait to give a test run :D. Is it possible to have a function that works like this:

 

V8Script script = engine.Comile(js);

Delegate func = script.GetFunctionByName("update");


Coordinator
Jul 6, 2010 at 12:06 AM

Petros,

Although I haven't tried it, you could possibly do this:

engine.Compile(js);

Delegate func = (Delegate)engine.Execute("update");

Jul 7, 2010 at 2:32 AM
Edited Jul 7, 2010 at 2:51 PM

I haven't been able to actually invoke any of the delegates returned because I keep getting parameter mismatches (even though the function takes no parameters, I tried with null and an empty object[] too). Is there any specific way I have to invoke them?

 

UPDATE:

I got it working by modifying the source a little. I made it return a V8Function^ delegate instead of just a Delegate^ and it works great now. I also changed it to [ParamArray] so you can call functions with no args like this

func.Invoke();
instead of

func.Invoke(new object[]{}); or func.Invoke(null); 

Oct 5, 2010 at 11:44 AM
smbecker wrote:

I am glad to hear that it works great in your project. I haven't done much with it due to an apparent lack of community interest. I most certainly can add delegate support. In terms of priorities, would it be more important to be able to expose a delegate in a .NET class to a JS script or be more important to be able to pass a JS function to a .NET class?

Hi,

There is a MASSIVE community interest in this, it's just that your little project is hidden in obscurity and suffering the fate of every other open source project that despite how wonderful they are, appear to have no community interest. You need to do three things:

1) Keep working on it! The more stable and mature it is, the more people who will feel confident in using it.
2) Document it! Detailed examples/guides/tutorials and good API documentation.
3) Spread the word! The most amazing technology in the world is worthless if it sits in someone's basement and nobody knows about it.

Doing all of those three things shows people that YOU are sold on your project. If people don't think the project is active and they don't think you have bought into the merits of your own project, they will have a hard time buying into it also.

Nathan