Skip to content

napi_get_property_names() coerces strings to numbers if string contains a number #27496

Closed
@darknoon

Description

@darknoon

Summary

napi_get_property_names is documented to return "A napi_value representing an array of JavaScript values that represent the property names of the object". It's very unexpected that it would convert strings to numbers, since that doesn't match the output of Object.getOwnPropertyNames:

> Object.getOwnPropertyNames({b:123, "44":56})
[ '44', 'b' ]

napi_get_property_names() output is [44,"b"]

Context

I am developing an extension with N-API via node-addon-api, and I have a function that takes arbitrary values. To ensure that it was complete, I have a unit test that passes in an object with string keys that have numbers in them:

const a = { b: 123, "44": 56 };
myFunc(a);

Inside the implementation of myFunc, I have this code

auto ov = info[0].As<Napi::Object>();
Napi::Array props = ov.GetPropertyNames();
for (uint32_t i = 0; i < length; i++) {
  if (key.IsString()) {
    // OK
  } else {
    // Throw exception for non-string key
  }
}

and I'm hitting the exception case!

in the lldb debugger, I print out the list:

> debug_json(props)
"[44,\"b\"]"

Looking at the source of Object::GetPropertyNames(), it just calls N-API napi_get_property_names(), which calls into V8 v8::Object::GetPropertyNames(), at which point it suggests to me that this is a V8 bug.

  • Version: v11.12.0
  • Platform: Darwin 2012MBP13.local 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64
  • Subsystem: V8 / N-API

Metadata

Metadata

Assignees

No one assigned

    Labels

    node-apiIssues and PRs related to the Node-API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions