Wednesday, 24 May 2017

Undefined:0 typerror object is not a function node c++ addon

I am trying to create an async c++ addon function. First I create an object then call the function. The object is successfully created and the function is called. The problem is that the function does not return its result to the javascript side. Instead I get "Undefined:0 typerror object is not a function". Does anyone have any idea what im doing wrong.

This is the function I call

void MyObject::UpdateImage(const Nan::FunctionCallbackInfo<v8::Value>& args) {
//    image_window win;
cout << "INSIDE UPDATE IMAGE "<<endl;
 Nan::HandleScope scope;
    Isolate* isolate = args.GetIsolate();
     Work * work = new Work();
     cout << "Created work "<<endl;
     work->request.data = work;

    v8::Local<v8::Object> location_obj = args[0]->ToObject();
    v8::Handle<v8::Value> img_Value = location_obj->Get(v8::String::NewFromUtf8(isolate,"img"));

    dlib::array2d<bgr_pixel> dlibImage;
    dlib::assign_image(dlibImage, dlib::cv_image<bgr_pixel>(Nan::ObjectWrap::Unwrap<node_opencv::Matrix>(img_Value->ToObject())->mat));

    MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
    dlib::assign_image(work->img, dlibImage);
//    work->img (dlibImage);
    work->value_ = obj->value_;

    Local<Function> callback = Local<Function>::Cast(args[0]);
    work->callback.Reset(isolate, callback);

    uv_queue_work(uv_default_loop(),&work->request,WorkAsync,WorkAsyncComplete);

    args.GetReturnValue().Set(Undefined(isolate));
}

Work asyc function

static void WorkAsync(uv_work_t *req)
{
cout << "Work async"<<endl;
    Work *work = static_cast<Work *>(req->data);

    work->results.resize(work->value_.size());
       for (unsigned int i=0; i < work->value_.size(); i++){
             obj->value_[i].update(work->img);

             cv::Rect r = cv::Rect(obj->value_[i].get_position().left(), obj->value_[i].get_position().top(), obj->value_[i].get_position().width(), obj->value_[i].get_position().height());
               work->results[i].xpoint = r.x;
               work->results[i].ypoint = r.y;
               work->results[i].width = r.width;
               work->results[i].height = r.height;
        }
}

Work async complete

static void WorkAsyncComplete(uv_work_t *req,int status){

 Isolate * isolate = Isolate::GetCurrent();

     v8::HandleScope handleScope(isolate);

    Work *work = static_cast<Work *>(req->data);

     Local<Array> arr = Array::New(isolate);

  for (unsigned int i=0; i < work->results.size(); i++){
              v8::Local < v8::Object > x = Nan::New<v8::Object>();
              x->Set(Nan::New("x").ToLocalChecked(), Nan::New < Number > (work->results[i].xpoint));
              x->Set(Nan::New("y").ToLocalChecked(), Nan::New < Number > (work->results[i].ypoint));
              x->Set(Nan::New("width").ToLocalChecked(), Nan::New < Number > (work->results[i].width));
              x->Set(Nan::New("height").ToLocalChecked(), Nan::New < Number > (work->results[i].height));
              arr->Set(i, x);
              cout << work->results[i].xpoint<<endl;
        }
        cout << "WORK AYSC COMPLETE "<<endl;
         Handle<Value> argv1[] = {arr};
          Local<Function>::New(isolate, work->callback)->Call(isolate->GetCurrentContext()->Global(), 1, argv1);
           work->callback.Reset();
              delete work;
}

Javascript create object

 image_obj = new addon.MyObject(details, obj1);
  image_obj.updateImage(obj1, function(res){
               console.log(res)
            });

INIT function

void MyObject::Init(v8::Local<v8::Object> exports) {
Nan::HandleScope scope;
 v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
  tpl->SetClassName(Nan::New("MyObject").ToLocalChecked());
  tpl->InstanceTemplate()->SetInternalFieldCount(1);

  Nan::SetPrototypeMethod(tpl, "updateImage", UpdateImage);
  constructor.Reset(tpl->GetFunction());
  exports->Set(Nan::New("MyObject").ToLocalChecked(), tpl->GetFunction());

}

Thank you.



via vladimir999

No comments:

Post a Comment