Skip to content

Instantly share code, notes, and snippets.

@chrisber
Created March 15, 2015 14:59
Show Gist options
  • Save chrisber/68e96c26c260ab0a9b57 to your computer and use it in GitHub Desktop.
Save chrisber/68e96c26c260ab0a9b57 to your computer and use it in GitHub Desktop.
return-values

#V8dotnet

##Examples

  • How to create a object
  • add it at function argument
  • add it to the global space
  • How to create a function like setTimeout
  • How to create a log function
  • How to access a the function of an object
  • How to pass a parameter to a function of an object.

Passing return values (handle) form callback functions

I observed that there are several possibilities to pass return values. I have a managed side function that gets called from the native side. What is the fastest solution to return an object handle?

###Return values

###Returning a value from a callback

public InternalHandle getfileName(){
	var scriptArray = host.getScriptFileNames();
	return v8engine.CreateValue(scriptArray[0]);
}

####Returning an array from a callback

public InternalHandle[] getScriptFileNames(){

	var scriptArray = host.getScriptFileNames();
	
	InternalHandle[] handleArray = 
    new InternalHandle[scriptArray.Length];
    
	for (int i = 0; i < scriptArray.Length; i++)
	{
	handleArray[i] = v8engine.CreateValue(scriptArray[i]);
	}	
}
return handleArray;

####Returning an object form a callback

#####Option 1:

public class V8ScriptAdapter :  IV8NativeObject
{
		string filename;
		string version;
		string content;
		
		public InternalHandle getFileChange()
		{
			var nativeFileChange = 
			v8engine.CreateObject<V8NativeObject>();
			
			nativeFileChange.SetProperty(
				"start", 
				v8engine.CreateValue(0) 
			);
			
				nativeFileChange.SetProperty(
				"length", 
				v8engine.CreateValue(oldSource.Length)
			);
		
			var nativeFile =  
			v8engine.CreateObject<V8NativeObject>();

			nativeFile.SetProperty(
				"changes", 
				nativeFileChange
			);

			nativeFile.SetProperty(
			"content", 
			v8engine.CreateValue(content);

			return nativeFile;
		}
}

#####Option 2:

Consider Option 1 with changed getFileChange() method. and a managed class File and class Change.

class Change

public class Change  : IV8NativeObject
{
	public string version {get; set;}
	public string content {get; set:}
}

class File : IV8NativeObject

public class File{
	public string filename {get; set;};
	public Change changes {get; set;}
}

The changed getFileChange method.

public InternalHandle getFileChange()
{
	var file = new File();
	v8Engine.RegisterType<File>(null, recursive: true);
	
	var guid = Guid.NewGuid().ToString();
	
	v8engine.GlobalObject.SetProperty(guid, file);
	
	var handle = v8engine.GlobalObject.GetProperty(guid); 
	
	return handle;
}

Doing the example by registering a type (class File : IV8NativeObject) I'm not able to access the the value on the native side it returns {}.

Is this example correct? And how to access the object. I created a guid to set it as property name of the for the object so that I don't override it accidentally.

#####GetTypeBinder I found also an example with v8Engine.GetTypeBinder on the discussions, can you maybe explain the usage of this further?

GetTypeBinder

Registers binding related schema for the given type on top an 'ObjectTemplate' instance.  If a type already exists that doesn't match the given parameters, it is replaced. This is done implicitly, so there's no need to register types before binding them; however, explicitly registering a type using this method gives the user more control over the behavior of the binding process.

I made this example this but I don't think it is correct.

var file = new File();
return v8Engine.GetTypeBinder(typeof(File)).CreateObject(file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment