Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bindings/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,22 @@ I8/U8/Char16. For example, I32 `255` is accepted for U8; I32 `257` throws instea
of wrapping. Reference inputs are retained and queried for the declared IID;
an incompatible interface throws, while a null reference remains null.

For admitted POD structs, vector `IndexOf` and map key operations compare
metadata-declared fields by value, including nested fields, and ignore padding.
Floating fields use numerical equality: `+0` and `-0` match, while NaN does not
match even the same NaN bits. This also applies to collection views; a
NaN-containing struct map key is not found by lookup. Stored field bits are
not normalized.
Map construction uses the same comparison as `Insert`: the last value for an
equal key wins, retaining the first key's field bits and iteration position.

The JavaScript helpers use validated typed core factories. Rust callers can use
the safe `create_vector_from_values` / `create_map_from_values` factories for
the same checks. The metadata-free Rust `create_value_vector`, `create_vector`,
and `create_map` constructors require `unsafe`: callers must prove the native
ABI, ownership, element types, and complete IID set themselves.
The metadata-free `create_value_vector` retains packed-byte equality, including
padding, rather than metadata-directed field equality.
Complete native struct collection support is tracked in
[microsoft/dynwinrt#161](https://github.com/microsoft/dynwinrt/issues/161).

Expand Down
81 changes: 81 additions & 0 deletions bindings/js/__test__/collection-struct-equality.e2e.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import assert from 'node:assert/strict'
import { test } from 'node:test'
import { createRequire } from 'node:module'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

const require = createRequire(import.meta.url)
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const { DynWinRtType, DynWinRtStruct, DynWinRtValue, DynWinRtMethodSig, WinGuid, roInitialize } = require(
resolve(process.env.DYNWINRT_JS_PACKAGE ?? packageRoot),
)
roInitialize(1)

const pointType = DynWinRtType.structType('Windows.Foundation.Point', [DynWinRtType.f32(), DynWinRtType.f32()])
const mapIid = DynWinRtType.parameterized(WinGuid.parse('3c2925fe-8519-45c1-aa79-197b6718c1c1'), [
pointType,
DynWinRtType.i32(),
]).iid()
const reader = DynWinRtType.registerInterface('StructEquality.PointMap', mapIid)
.addMethod('Lookup', new DynWinRtMethodSig().addIn(pointType).addOut(DynWinRtType.i32()))
.addMethod('get_Size', new DynWinRtMethodSig().addOut(DynWinRtType.u32()))

function own(t) {
const values = []
t.after(() => {
for (const value of values.reverse()) value.release()
})
return (value) => {
values.push(value)
return value
}
}

function point(x, y) {
const value = DynWinRtStruct.create(pointType)
value.setF32(0, x)
value.setF32(1, y)
return value.toValue()
}

test('checked Point map keys compare fields numerically', { skip: process.arch !== 'x64' }, (t) => {
const keep = own(t)
for (const [stored, query, equal] of [
[[+0, 1], [-0, 1], true],
[[-0, 1], [+0, 1], true],
[[2, 1], [2, 1], true],
[[2, 1], [3, 1], false],
[[2, 1], [2, 3], false],
[[NaN, 1], [NaN, 1], false],
[[1, NaN], [1, NaN], false],
]) {
const source = keep(point(...stored))
const needle = keep(point(...query))
const value = keep(DynWinRtValue.i32(7))
const map = keep(DynWinRtValue.createMap([source], [value], pointType, DynWinRtType.i32()))
const typed = keep(map.cast(mapIid))
assert.equal(keep(reader.method(7).invoke(typed, [])).toNumber(), 1)
if (equal) {
assert.equal(keep(reader.method(6).invoke(typed, [needle])).toNumber(), 7)
} else {
assert.throws(() => reader.method(6).invoke(typed, [needle]))
}
assert.ok(Object.is(source.asStruct().getF32(0), stored[0]))
assert.ok(Object.is(source.asStruct().getF32(1), stored[1]))
}
})

test('Point collection admission is unchanged on non-x64 targets', { skip: process.arch === 'x64' }, (t) => {
const keep = own(t)
const source = keep(point(0, 1))
const value = keep(DynWinRtValue.i32(7))
for (const items of [[], [source]]) {
assert.throws(() => DynWinRtValue.createVector(items, pointType))
assert.throws(() =>
DynWinRtValue.createMap(items, items.map(() => value), pointType, DynWinRtType.i32()),
)
}
})
2 changes: 1 addition & 1 deletion bindings/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"test:native-classes": "node --test __test__/native-class-boundary.e2e.mjs",
"test:native-classes:hooks": "node --test __test__/native-class-boundary-hooks.e2e.mjs",
"test:imports": "ava __test__/native-imports.spec.ts",
"test:collection-factories": "node --test __test__/collection-factories.e2e.mjs",
"test:collection-factories": "node --test __test__/collection-factories.e2e.mjs __test__/collection-struct-equality.e2e.mjs",
"test:generated-unsafe": "npm run build:test-hooks && npm run build:entrypoints && ava __test__/generated-unsafe-companion.e2e.ts",
"test:winrt-implementation": "npm run build:test-hooks && npm run build:entrypoints && ava __test__/winrt-implementation.spec.ts",
"test:native-completion": "npm run build:test-hooks && npm run build:entrypoints && ava __test__/native-completion.e2e.ts",
Expand Down
11 changes: 11 additions & 0 deletions bindings/py/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,22 @@ I8/U8/Char16. For example, `from_i32(255)` is accepted for U8;
queried for the declared IID; an incompatible interface raises, while
`DynWinRTValue.null_value()` remains a null reference.

For admitted POD structs, vector `IndexOf` and map key operations compare
metadata-declared fields by value, including nested fields, and ignore padding.
Floating fields use numerical equality: `+0` and `-0` match, while NaN does not
match even the same NaN bits. This also applies to collection views; a
NaN-containing struct map key is not found by lookup. Stored field bits are
not normalized.
Map construction uses the same comparison as `Insert`: the last value for an
equal key wins, retaining the first key's field bits and iteration position.

The Python helpers use validated typed core factories. Rust callers can use
the safe `create_vector_from_values` / `create_map_from_values` factories for
the same checks. The metadata-free Rust `create_value_vector`, `create_vector`,
and `create_map` constructors require `unsafe`: callers must prove the native
ABI, ownership, element types, and complete IID set themselves.
The metadata-free `create_value_vector` retains packed-byte equality, including
padding, rather than metadata-directed field equality.
Unsupported layouts raise
`RuntimeError`; HRESULT-backed type, range, or QueryInterface failures raise
`OSError`. Complete native struct collection support is tracked in
Expand Down
Loading
Loading