/* open source routing machine Copyright (C) Dennis Luxen, others 2010 This program is free software; you can redistribute it and/or modify it under the terms of the GNU AFFERO General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or see http://www.gnu.org/licenses/agpl.txt. */ #ifndef V8HELPER_H_ #define V8HELPER_H_ //Most of the stuff below is from http://blog.owned.co.za provided with an MIT License template v8::Handle WrapClass(T* y) { // Handle scope for temporary handles, v8::HandleScope handle_scope; v8::Persistent class_template_; v8::Handle 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::New(raw_template); //Create the new handle to return, and set its template type v8::Handle result = class_template_->NewInstance(); v8::Handle class_ptr = v8::External::New(static_cast(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 v8::Handle ExposeClass(v8::Persistent context, T* y, v8::Handle exposedName, v8::PropertyAttribute props) { v8::HandleScope handle_scope; v8::Handle obj = WrapClass(y); context->Global()->Set(exposedName, obj, props); return handle_scope.Close(obj); } template T* UnwrapClass(v8::Handle obj) { v8::Handle field = v8::Handle::Cast(obj->GetInternalField(0)); void* ptr = field->Value(); return static_cast(ptr); } void Expose(v8::Handle intoObject, v8::Handle namev8String, v8::InvocationCallback funcID) { v8::HandleScope handle_scope; v8::Handle thisFunc = v8::FunctionTemplate::New(funcID); intoObject->Set(namev8String, thisFunc->GetFunction()); } template v8::Handle GetMember(v8::Local property, const v8::AccessorInfo &info) { v8::Local self = info.Holder(); v8::Local wrap = v8::Local::Cast(self->GetInternalField(0)); void* ptr = wrap->Value(); int value = static_cast(ptr)->*MemVar; return PropertyType::New(value); } template void SetIntMember(v8::Local property, v8::Local value, const v8::AccessorInfo& info) { v8::Local self = info.Holder(); v8::Local wrap = v8::Local::Cast(self->GetInternalField(0)); void * ptr = wrap->Value(); static_cast(ptr)->*MemVar = value->Int32Value(); } template v8::Handle GetInt(v8::Local property, const v8::AccessorInfo &info) { v8::Local self = info.Holder(); v8::Local wrap = v8::Local::Cast(self->GetInternalField(0)); void* ptr = wrap->Value(); CPPProperyType value = static_cast(ptr)->member1; return JSPropertyType::New(value); } #endif /* V8HELPER_H_ */