Visitar URL original
The Napi::Object class returns unstable results · Issue #699 · nodejs/node-addon-api · GitHub
The Wayback Machine - https://web.archive.org/web/20220405131836/https://github.com/nodejs/node-addon-api/issues/699
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Napi::Object class returns unstable results #699

Closed
younesZdDz opened this issue Apr 10, 2020 · 5 comments
Closed

The Napi::Object class returns unstable results #699

younesZdDz opened this issue Apr 10, 2020 · 5 comments

Comments

@younesZdDz
Copy link

@younesZdDz younesZdDz commented Apr 10, 2020

I am currently writing a c++ library that will be consumed by nodeJs using node-addon-api. The library has a function that takes a javascript object as input of this format :

{
  nclosest: 3,
  input: {
    riders: [
      {
        origin: {
          lat: 46.886,
          lng: 3.000002
        },
        destination: {
          lat: 47.88895,
          lng: 3.8876
        }
      }
    ],
    vehicles: [
      {
        _id: "_HxY87",
        location: {
          lat: 46.786,
          lng: 3.000002
        }
      }
    ]
  }
}

The library parses the input into c++ classes using Napi::Object finds the nclosest vehicle for each rider based on haversine formula and remove distant vehicles from the list, so the output is an object created also using Napi::Object and has the same format. The code for the parser from Js Object is as follows:

Input parseFromJsInput(Napi::Object wrappedInput, Napi::Env env) {

    Napi::Array wrappedRiders = (wrappedInput.Get(Napi::String::New(env, "riders"))).As<Napi::Array>();
    vector<Rider> riders;
    for (int i = 0; i < (int)wrappedRiders.Length(); i++) {
        Napi::Object wrappedRider = ((Napi::Value)wrappedRiders[i]).As<Napi::Object>();
        Napi::Object wrappedOrigin = (wrappedRider.Get(Napi::String::New(env, "origin"))).As<Napi::Object>();
        double p_lat = (wrappedOrigin.Get(Napi::String::New(env, "lat"))).As<Napi::Number>();
        double p_lng = (wrappedOrigin.Get(Napi::String::New(env, "lng"))).As<Napi::Number>();
        Napi::Object wrappedDestination = (wrappedRider.Get(Napi::String::New(env, "destination"))).As<Napi::Object>();
        double d_lat = (wrappedDestination.Get(Napi::String::New(env, "lat"))).As<Napi::Number>();
        double d_lng = (wrappedDestination.Get(Napi::String::New(env, "lng"))).As<Napi::Number>();
        riders.push_back(Rider(p_lng, p_lat, d_lng, d_lat));
    }

    Napi::Array wrappedVehicles = (wrappedInput.Get(Napi::String::New(env, "vehicles"))).As<Napi::Array>();
    vector<Vehicle> vehicles;
    for (int i = 0; i < (int)wrappedVehicles.Length(); i++) {
        Napi::Object wrappedVehicle = ((Napi::Value)wrappedVehicles[i]).As<Napi::Object>();
        Napi::Object wrappedLocation = (wrappedVehicle.Get(Napi::String::New(env, "location"))).As<Napi::Object>();
        double lat = (wrappedLocation.Get(Napi::String::New(env, "lat"))).As<Napi::Number>();
        double lng = (wrappedLocation.Get(Napi::String::New(env, "lng"))).As<Napi::Number>();
        vehicles.push_back(Vehicle(lng, lat));
    }

    Input input = Input(vehicles, riders);
    return input;
};

And the code of the parser to Js object is:

Napi::Object parseToJsInput(Input in, Napi::Env env) {
    Napi::Object inputObject = Napi::Object::New(env);

    Napi::Array ridersObject = Napi::Array::New(env);
    for (int i = 0; i < (int)in.riders.size(); i++) {
        Rider r = in.riders[i];
        Napi::Object riderObject = Napi::Object::New(env);
        Napi::Object originObject = Napi::Object::New(env);
        originObject.Set(Napi::String::New(env, "lat"), Napi::Number::New(env, r.p_lat));
        originObject.Set(Napi::String::New(env, "lng"), Napi::Number::New(env, r.p_lng));
        riderObject.Set(Napi::String::New(env, "origin"), originObject);
        Napi::Object destinationObject = Napi::Object::New(env);
        destinationObject.Set(Napi::String::New(env, "lat"), Napi::Number::New(env, r.d_lat));
        destinationObject.Set(Napi::String::New(env, "lng"), Napi::Number::New(env, r.d_lng));
        riderObject.Set(Napi::String::New(env, "destination"), destinationObject);
        ridersObject.Set(Napi::Number::New(env, i), riderObject);
    }
    inputObject.Set(Napi::String::New(env, "riders"), ridersObject);

    Napi::Array vehiclesObject = Napi::Array::New(env);
    for (int i = 0; i < (int)in.vehicles.size(); i++) {
        Napi::Object vehicleObject = Napi::Object::New(env);
        Napi::Object locationObject = Napi::Object::New(env);
        locationObject.Set(Napi::String::New(env, "lat"), Napi::Number::New(env, v.lat));
        locationObject.Set(Napi::String::New(env, "lng"), Napi::Number::New(env, v.lng));
        vehicleObject.Set(Napi::String::New(env, "location"), locationObject);
        vehiclesObject.Set(Napi::Number::New(env, i), vehicleObject);
    }
    inputObject.Set(Napi::String::New(env, "vehicles"), vehiclesObject);

    return inputObject;
}

And the library method is as follows :

Napi::Value filterInputSync(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();
    Napi::HandleScope scope(env);
        int length = info.Length();
    if (length != 1 || !info[0].IsObject()) {
       Napi::TypeError::New(env, "Typed error").ThrowAsJavaScriptException();
    }
    Napi::Object wrappedInstance = info[0].As<Napi::Object>();
    int nclosest = (wrappedInstance.Get("nclosest")).As<Napi::Number>();
    Napi::Object wrappedInput = (wrappedInstance.Get("input")).As<Napi::Object>();
    Input input = parseFromJsInput(wrappedInput, env);
    Input filteredInput = doFilter(input, nclosest);
    return parseToJsInput(filteredInput, env);
}

The problem is when i run the code the results for larger instance are often correct, but sometimes i get an unpredictable results, for example i get as a result a number or corrupted string instead of the object created. Any suggestions on what's causing this unpredictable behaviour.

@KevinEady
Copy link
Contributor

@KevinEady KevinEady commented Apr 14, 2020

Hi @younesZdDz ,

Not really sure... Are you using multiple threads? If so, parseToJsInput should only run on the main thread.

@younesZdDz
Copy link
Author

@younesZdDz younesZdDz commented Apr 14, 2020

Hello @KevinEady, yes parseToJsInput is running on the main thread.
After narrowing down the problem, it seems when using .Get and .Set methods of the class Napi::Object for large test instances in the same thread, the output becomes very unstable and unpredictable.

@KevinEady
Copy link
Contributor

@KevinEady KevinEady commented Apr 14, 2020

Weeeird @younesZdDz , do you have some replicable test case we can see? Or something that describes the data you are passing to .Get and .Set?

@younesZdDz
Copy link
Author

@younesZdDz younesZdDz commented Apr 14, 2020

@KevinEady I will prepare a github repo with all the code and data used and provide the link here.

@gabrielschulhof
Copy link
Contributor

@gabrielschulhof gabrielschulhof commented Sep 16, 2020

@younesZdDz please re-open when you've had a chance to create a scenario that reproduces the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants