I'm having a problem with the pairs iterator. The code uses the hasOwnProperty function:
code here
Why is that? Doesn't for var in obj already guarantee that obj has the key?
const pattern = /(?<name>\w+) (?<age>\d+)/gm
const match = pattern .exec("soreto 90");
console.log(match.hasOwnProperty("groups")) // true
console.log(match.groups) // {name: 'soreto', age: '90'}
console.log(match.groups.hasOwnProperty("name")) // Uncaught TypeError: match.groups.hasOwnProperty is not a function
let pattern: cstring = r"(?<name>\w+) (?<age>\d+)"
let reg = newRegExp(pattern, "gm")
let match = reg.exec("soreto 90")
let obj = cast[JsObject](match)
echo obj.hasOwnProperty("groups") # true
for k, v in obj.groups.pairs: # error
echo k
The pairs iterator works fine without that check, at least in this case:
iterator unsafe_pairs(obj: JsObject): (cstring, JsObject) =
var k = cstring("")
var v = JsObject()
{.emit: "for (var `k` in `obj`) {".}
{.emit: "`v` = `obj`[`k`];".}
yield (k, v)
{.emit: "}".}
The doc actually says that casting isn't available, but still, no compiler warning or error was triggered. The underlying object is still there, and casting to JsObject and using console.log will show all the available properties.
It's fine though, I can write another function specifically for this case instead of using exec with cast. What confused me was the presence of this check in the iterator. After some research, I found out that this check is used to filter out properties on the prototype.
The problem is that some objects are created with no prototype attached (Object.create(null).hasOwnProperty === undefined). So this iterator will throw an error in cases like this.
Should this be considered a bug? Should I create an issue?