Convert a v8 object to a custom C ++ object

advertisements

I'm currently developing a nodejs module, written in C++ and I've been looking for a way to convert a v8::Object to a cv::Mat object from opencv, without any luck for now.

I saw that nan library could help to convert objects, but i couldn't find how, I don't even know if it's possible to convert them in my case.

The v8 juice project would meet my expectations but since it has been abandoned, I just don't know how to do this.

Here is a snippet of what I'm trying to do :

void
BRMatcher::run(const v8::FunctionCallbackInfo<v8::Value>& args)
{
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);

  if (args.Length() < 1)
  {
    isolate->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, "Missing parameter [Mat img].")));
    return ;
  }

  if (!args[0]->IsObject())
  {
    isolate->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, "Parameter [Mat img] must be an object.")));
    return ;
  }

  v8::Local<v8::Object> cvMat(args[0]->ToObject());

  Mat img = ??? // This is where I ended up...
  // ...
}

All the posts talking about this on StackOverflow are outdated (older versions or tools that are not working anymore...)

So my questions are: how can I convert the argument I receive in my function to a cv::Mat object ? To any type I want ?

Any help would be appreciated, thanks!


At first I would recommend to look on existing openCV bindings for Node.js like node-opencv.

If you need to bind C++ and JavaScript code, there are several libraries. As an author of one of them, v8pp, I know about several other:

As I know, to convert a C++ object into v8::Object all of them use v8::Object::SetAlignedPointerInInternalField() function.

C++ object to v8::Object conversions are usually performed with mapping of a C++ pointer to persistent handle of the V8 object in a map container.