26 #ifndef __TERRALIB_BINDING_V8_COMMON_INTERNAL_UTILS_H 27 #define __TERRALIB_BINDING_V8_COMMON_INTERNAL_UTILS_H 60 TEV8COMMONEXPORT ::v8::Handle<::v8::String>
ReadFile(
const std::string& fileName);
73 TEV8COMMONEXPORT void Cache(::v8::Local<::v8::Object>& obj,
const std::string& methodName, ::v8::Persistent<::v8::Function>& outFtor);
82 TEV8COMMONEXPORT ::v8::Handle<::v8::Value>
Print(const ::v8::Arguments& args);
93 template<
class T>
inline T*
Unwrap(::v8::Handle<::v8::Object> obj)
95 ::v8::Handle<::v8::External> field = ::v8::Handle<::v8::External>::Cast(obj->GetInternalField(0));
96 void*
p = field->Value();
98 return static_cast<T*
>(jsObj->
m_handle);
112 ::v8::Handle<::v8::External> field = ::v8::Handle<::v8::External>::Cast(obj->GetInternalField(0));
113 void*
p = field->Value();
116 return static_cast<T*
>(jsObj->
m_handle);
130 ::v8::Handle<::v8::External> field = ::v8::Handle<::v8::External>::Cast(obj->GetInternalField(0));
131 void*
p = field->Value();
145 template<
class T>
void JsObjectRelease(::v8::Persistent<::v8::Value> obj,
void* parameter)
149 ::v8::Handle<::v8::Object> oobj = obj->ToObject();
151 ::v8::Handle<::v8::External> field = ::v8::Handle<::v8::External>::Cast(oobj->GetInternalField(0));
153 void*
p = field->Value();
157 assert(cobj ==
static_cast<JsObject<T>*
>(parameter));
176 template<
class T,
class TF> inline ::v8::Local<::v8::Object>
Make(T* obj, TF tfunc,
const bool isOwner)
180 ::v8::Persistent<::v8::FunctionTemplate>& objFuncTemplate = tfunc();
181 ::v8::Local<::v8::ObjectTemplate> objTemplate = objFuncTemplate->InstanceTemplate();
182 objTemplate->SetInternalFieldCount(1);
183 ::v8::Local<::v8::Object> jsObj = objTemplate->NewInstance();
188 ::v8::Local<::v8::External> objPtr = ::v8::External::New(o);
189 jsObj->SetInternalField(0, objPtr);
192 ::v8::Persistent<::v8::Object> pjsObj = ::v8::Persistent<::v8::Object>::New(jsObj);
194 pjsObj.MakeWeak(o, JsObjectRelease<T>);
209 inline ::v8::Local<::v8::Object>
Convert2Js(
const std::map<std::string, std::string>& m)
211 ::v8::Local<::v8::Object> jsObj = ::v8::Object::New();
213 std::map<std::string, std::string>::const_iterator it = m.begin();
214 std::map<std::string, std::string>::const_iterator itend = m.begin();
218 ::v8::Local<::v8::String> key = ::v8::String::New(it->first.c_str());
219 ::v8::Local<::v8::String> value = ::v8::String::New(it->second.c_str());
221 jsObj->Set(key, value);
238 inline void Convert2Cpp(const ::v8::Local<::v8::Object>& jsmap, std::map<std::string, std::string>& cppmap)
240 assert(!jsmap.IsEmpty());
242 ::v8::Local<::v8::Array> amap = jsmap->GetPropertyNames();
244 unsigned int size = amap->Length();
246 for(
unsigned int i = 0; i < size; ++i)
248 ::v8::Local<::v8::Value> key = amap->Get(i);
249 ::v8::Local<::v8::Value> value = jsmap->Get(key);
251 ::v8::String::Utf8Value ukey(key->ToString());
252 ::v8::String::Utf8Value uvalue(value->ToString());
254 cppmap[*ukey] = *uvalue;
265 #endif // __TERRALIB_BINDING_V8_COMMON_INTERNAL_UTILS_H ::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
void JsObjectRelease(::v8::Persistent<::v8::Value > obj, void *parameter)
The call-back function for releasing objects.
T * m_handle
A pointer to a C++ object.
TEV8COMMONEXPORT::v8::Handle<::v8::String > ReadFile(const std::string &fileName)
It reads a file into a v8 string.
An auxiliary data structure for helping to control the garbage collection of wrapped C++ objects asso...
inline::v8::Local<::v8::Object > Convert2Js(const std::map< std::string, std::string > &m)
It converts the input map to an object representing the associative conteiner.
bool m_isOwner
If true it specifies that JsObject has the ownership of the C++ handle.
#define TEV8COMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
void LooseOwnership(::v8::Handle<::v8::Object > obj)
An utility function that extracts the C++ object from a wrapped object.
T * UnwrapAndLooseOwnership(::v8::Handle<::v8::Object > obj)
An utility function that extracts the C++ object from a wrapped object.
T * Unwrap(::v8::Handle<::v8::Object > obj)
An utility function that extracts the C++ object from a wrapped object.
void Convert2Cpp(const ::v8::Local<::v8::Object > &jsmap, std::map< std::string, std::string > &cppmap)
It converts the input map to an object representing the associative conteiner.
TEV8COMMONEXPORT void Cache(::v8::Local<::v8::Object > &obj, const std::string &methodName,::v8::Persistent<::v8::Function > &outFtor)
It caches the object method into the persistent output functor.
An auxiliary data structure for helping to control the garbage collection of wrapped C++ objects asso...
TEV8COMMONEXPORT::v8::Handle<::v8::Value > Print(const ::v8::Arguments &args)
This function will print all the arguments to the standard output.