V8 Javascript engine: v8::Arguments and function templates

I wrapped a C++ standard library std::map<T1,T2> that I want to expose as a Node.JS addon. I want to have two functions, Set for adding a new value to the hash table, and Get for looking up value from the map. I want a single function that would work for "arbitrary" type. This means Get would have to extract value of type T1 from Args[0] (note that Args is of type v8::Arguments). See the code below.

template<typename T1, typename T2>
class Hash : public node::ObjectWrap {
public:
    static v8::Persistent<v8::FunctionTemplate> constructor;
    static void Init(v8::Handle<v8::Object> target);
protected:
    /* ... */
    static v8::Handle<v8::Value> New(const v8::Arguments& Args); // new object 
    static v8::Handle<v8::Value> Set(const v8::Arguments& Args); // set H[x]=y
    static v8::Handle<v8::Value> Get(const v8::Arguments& Args); // return H[x]
private:
    std::map<T1, T2> H;
};
/* ... */
template<typename T1, typename T2>
v8::Handle<v8::Value> Hash<T1, T2>::Get(const v8::Arguments& Args) {
    v8::HandleScope HandleScope;

    THash<T1, T2>* H = ObjectWrap::Unwrap<Hash<T1, T2> >(Args.This());
    // TODO: I want to extract argument Args[0] of type T1, call it x, and then
    // return argument y=H->get(x) of type T2.
    return v8::Undefined();
}

Is there a way to do this? If yes, what is the way of doing it?

In case there is no way of extracting an arbitrary type, what is the best practice in case I am willing to restrict to several predefined types? For example, T=int, T=std::string, T=MyType1, and T=MyType2.

You need helper functions that will convert from T1 and T2 to v8::Value and vice versa. The problem is, this conversion is type-specific so you won't get around type traits or overloaded functions for each type as suggested here. Something like this should work:

#include <type_traits> // C++0x

template<typename T>
T fromV8Value(v8::Handle<v8::Value> value)
{
  if (std::is_same<T,std::string>::value)
  {
    v8::String::Utf8Value stringValue(value);
    return std::string(*stringValue, stringValue.length());
  }
  else if (std::is_same<T,int>::value)
    return value->IntegerValue();
  else
    throw new std::exception("Unsupported type");
}

v8::Handle<v8::Value> toV8Value(std::string& value)
{
  return v8::String::New(value.c_str(), value.length());
}

v8::Handle<v8::Value> toV8Object(int value)
{
  return v8::Number::New(value);
}

...

v8::Handle<v8::Value> Hash::Set(const v8::Arguments& Args)
{
  T1 key = fromV8Value<T1>(Args[0]);
  T2 value = fromV8Value<T2>(Args[1]);
  return ...;
}

v8::Handle<v8::Value> Hash::Get(const v8::Arguments& Args)
{
  T1 key = fromV8Value<T1>(Args[0]);
  T2 value = ...;
  return toV8Object(value);
}