 [code ExposeClass] 
		//http://blog.owned.co.za v8 Javascript Helper Function ,
        //Give this function the c++ object (an instance) to wrap, and it returns a JS object to expose things under.
		//Note theres 2 functions here , the top one is used by the ExposeClass function
		
		template<class T> 
        v8::Handle<v8::Object> WrapClass(T* y)
		{
			// Handle scope for temporary handles, 
			v8::HandleScope handle_scope;
			v8::Persistent<v8::ObjectTemplate>  class_template_;

			v8::Handle<v8::ObjectTemplate> raw_template = v8::ObjectTemplate::New();

			//The raw template is the ObjectTemplate (that can be exposed to script too)
				//but is maintained internally.
			raw_template->SetInternalFieldCount(1);

			//Create the actual template object, 
            class_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template);

			//Create the new handle to return, and set its template type 
			v8::Handle<v8::Object> result = class_template_->NewInstance();
			v8::Handle<v8::External> class_ptr = v8::External::New(static_cast<T*>(y)); 

			//Point the 0 index Field to the c++ pointer for unwrapping later
			result->SetInternalField(0, class_ptr);

			//Return the value, releasing the other handles on its way.
			return handle_scope.Close(result);
		} 
		
        template<class T>
        v8::Handle<v8::Object> ExposeClass(v8::Persistent<v8::Context> context, T* y, v8::Handle<v8::Value> exposedName, v8::PropertyAttribute props)
        {
            v8::HandleScope handle_scope;

             v8::Handle<v8::Object> obj = Core::Script::WrapClass<T>(y);
            context->Global()->Set(exposedName, obj, props);

            return handle_scope.Close(obj);
        }			
		
	[/code]
	
	
	[code UnwrapClass]
	
		//http://blog.owned.co.za v8 Javascript Helper Function ,
        //Give this function the v8 JS object to unwrap, and the class to unwrap to.
		
        template<class T> 
        T* UnwrapClass(v8::Handle<v8::Object> obj) 
	    {
			    v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(obj->GetInternalField(0));
			    void* ptr = field->Value();
			    return static_cast<T*>(ptr);
	    }
		
	[/code]

	 [code Step 1]
		class cCore
		{

		public:

			//This is a simple example, 
			//do not copy this as a real class.
			cCore(){};
			~cCore() {};

			//We need a persistant handle to the core in scripts, a non persistant
			//v8::Handle<v8::Object> will get lost in the handle scopes, and throw some
			//weird exceptions to hunt down. Wrap class and unwrap class return 
			//NON persistant handles, so store your own persistant ones if you want to 
			//use it outside of the first handle scope.

			v8::Persistent<v8::Object> cso_core;                                    //core script object,  "core" in scripts.

			//We expose some functions in our class to the scripts from c++, 
			//By wrapping an object (cso_core above) and the attaching functions
			//to that object. Those functions, when called from javascript - end up here.

			//The functions are self explanatory.

			static v8::Handle<v8::Value> xecho(const v8::Arguments& args);	        //Print some text to the console/ui console
			static v8::Handle<v8::Value> xexec(const v8::Arguments& args);	        //execute a javascript file
			static v8::Handle<v8::Value> xexit(const v8::Arguments& args);	        //exit all systems and shutdown
					
		}; //end class declaration
 
 [/code]
 
	[code Step 2]
		
		//Just for clarity sake, so you can see my workflow.
		void cScriptSystem::startup()
		{
			//Create a script engine for the core
			v8::HandleScope handle_scope;

			//Create a context for the system to use (i have just one at this point)
			coreContext = v8::Context::New(NULL, global);

			//Enter the main context, making it active.
			v8::Context::Scope context_scope(coreContext);

			exposeCore(); //see below
		};
		
		//And the code to expose the class, 
		void cScriptSystem::exposeCore()
		{
			v8::HandleScope handlescope;

			//This will be reused to expose multiple classes, short name for easy code.
			v8::Handle<v8::Object> cc;
			
			//get the JS version of the C++ object, note that core is simply Core::cCore* core; 
			cc = ExposeClass< Core::cCore >(coreContext, core,  v8::String::New("core"), v8::ReadOnly);
			
			//set the persistant handle for use outside of this handlescospe
			core->cso_core = v8::Persistent<v8::Object>::New(cc);

			//Give the functions to the object. This makes the functions come out as core.echo(), core.exec(), core.exit()
			Expose(core->cso_core, v8::String::New("echo") , v8::InvocationCallback(Core::cCore::xecho));
			Expose(core->cso_core, v8::String::New("exec") , v8::InvocationCallback(Core::cCore::xexec));
			Expose(core->cso_core, v8::String::New("exit") , v8::InvocationCallback(Core::cCore::xexit));

		};
	
	[/code]

	[code Step 3]
		v8::Handle<v8::Value> cCore::xexit(const v8::Arguments& args)
		{
			//args.Holder() is the object that the function was invoked upon. 
			//This means it could be exposed to 2 classes, under different types so be sure
			//that you dont just randomly unwrap into the wrong container type.
			
			Core::cCore* core = UnwrapClass<Core::cCore>( args.Holder() );
				
				//The code inside the normal exit function is identical, so i can just call the 
				//normal c++ code directly.
				
				core->startShutdown();
			
			//This is to return void, basically.
			return v8::Undefined();
		}
	[/code]
	
	[code Step 4]
		void Expose(v8::Handle<v8::Object> intoObject, v8::Handle<v8::Value> namev8String, v8::InvocationCallback funcID)
        {
            v8::HandleScope handle_scope;

            v8::Handle<v8::FunctionTemplate> thisFunc = v8::FunctionTemplate::New(funcID);
            intoObject->Set(namev8String, thisFunc->GetFunction());
        }
	[/code]

	[code Conclusion]
			//you can now use it std::string(*somestdstring)
		v8::String::Utf8Value	somestdstring ( args[0] );  
		
			//you can now use it *somecstring (same as const char* somecstring;)
		v8::String::Utf8Value	somecstring ( args[1] );	

			//normal double, 
		double somedouble = args[2]->ToNumber()->Value();
			//booleans
		bool somebool = args[3]->ToBoolean()->BooleanValue(); 
			//and int's.
		int someint = args[4]->ToInt32()->Int32Value();	
		
		//And one last thing, giving javascript meaningful return values. 
		//This should be more than enough to let you figure out how simple
		//v8 makes things for you on the c++ side.
		
		return handle_scope.Close(v8::Number::New(somedouble));
		return handle_scope.Close(v8::Int32::New(someint));
		return handle_scope.Close(v8::Boolean::New(somebool));
		
	[/code]
 
