Skip to content
Open
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
6 changes: 6 additions & 0 deletions bindings/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ helper. Apartment, message-pumping, and shutdown requirements are unchanged.
This is not a general older-Windows compatibility or addon feature-isolation
guarantee.

For a WinRT action converted to a Promise while the WinUI queue is registered,
the completion notification schedules `GetResults` on the originating JS/UI
thread. Its HRESULT determines whether the Promise resolves or rejects, even
when queue shutdown routes settlement back through Node. Data-returning
operations retain their existing off-thread result collection and marshaling.

`npm run test:imports` checks every `.node` file in `dist` directly, including
x64 and ARM64 artifacts, without loading it or requiring Visual Studio discovery
or `dumpbin`. It rejects CoreMessaging DLL imports (including ordinal imports)
Expand Down
20 changes: 20 additions & 0 deletions bindings/js/__test__/dispatcher-queue-winui-child.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fs = require('node:fs')
const Module = require('node:module')
const path = require('node:path')
const { spawn } = require('node:child_process')
const createOwnerAction = require('./fixtures/owner-async-action.cjs')

const applicationModule = process.argv[2]
const bootstrapDll = process.argv[3]
Expand Down Expand Up @@ -242,6 +243,21 @@ async function main() {
})
}
app = Application.create(() => {
let action
let actionResult
let actionError
if (startMode === 'scheduled') {
action = createOwnerAction(runtime, { completed: true })
action.operation.toPromise().then(
(result) => {
actionResult = result
},
(error) => {
actionError = error
},
)
assert.equal(action.resultCalls, 0)
}
exitTimer = startExitTimer(runtime, 5000, () => {
timerElapsed = performance.now() - startedAt
order.push('timer')
Expand All @@ -250,6 +266,10 @@ async function main() {
assert.ok(reactionElapsed < 4500, `Promise reaction was delayed ${reactionElapsed}ms`)
assert.ok(reactionElapsed < timerElapsed - 500)
if (startMode === 'scheduled') {
assert.ifError(actionError)
assert.equal(actionResult?.toNumber(), 0, 'action must settle inside Application.Start')
assert.equal(action.resultCalls, 1)
action.dispose()
assert.ok(nextTickElapsed < 1000, `nextTick was delayed ${nextTickElapsed}ms`)
assert.deepEqual(order, ['nextTick', 'promise', 'timer'])
} else {
Expand Down
16 changes: 15 additions & 1 deletion bindings/js/__test__/dispatcher-shutdown-child.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import assert from 'node:assert/strict'
import asyncHooks from 'node:async_hooks'
import { createRequire } from 'node:module'
import createOwnerAction from './fixtures/owner-async-action.cjs'

const require = createRequire(import.meta.url)
const runtime = require(process.env.DYNWINRT_TEST_RUNTIME ?? '../dist/winrt.js')
const {
DynWinRtMethodSig,
DynWinRtType,
Expand All @@ -14,7 +16,7 @@ const {
registerWinuiDispatcherQueue,
roInitialize,
unregisterWinuiDispatcherQueue,
} = require(process.env.DYNWINRT_TEST_RUNTIME ?? '../dist/winrt.js')
} = runtime

roInitialize(0)

Expand All @@ -35,6 +37,10 @@ const operations = Array.from({ length: 16 }, () =>
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 250)

registerWinuiDispatcherQueue()
const queuedAction = createOwnerAction(runtime, { completed: true })
const lateAction = createOwnerAction(runtime, { fail: true })
const queuedPromise = queuedAction.operation.toPromise()
const lateRejection = assert.rejects(lateAction.operation.toPromise(), /0x80004005/i)
const settlementCounts = operations.map(() => 0)
const order = []
const asyncPromiseIds = new Set()
Expand Down Expand Up @@ -67,15 +73,23 @@ assert.deepEqual(
)

insideUnregister = true
assert.equal(queuedAction.resultCalls, 0)
unregisterWinuiDispatcherQueue()
insideUnregister = false
assert.equal(queuedAction.resultCalls, 1)
lateAction.finish()
order.push('unregister-returned')
assert.equal(directSettlementCallbacks, operations.length)
assert.deepEqual(
settlementCounts,
operations.map(() => 0),
)
await Promise.all(promises)
assert.equal((await queuedPromise).toNumber(), 0)
await lateRejection
assert.equal(lateAction.resultCalls, 1)
queuedAction.dispose()
lateAction.dispose()
await new Promise((resolve) => setImmediate(resolve))
hook.disable()
assert.deepEqual(
Expand Down
87 changes: 87 additions & 0 deletions bindings/js/__test__/fixtures/owner-async-action.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

'use strict'

// A native IAsyncAction backed by an owner-thread WinRT implementation. A
// foreign GetResults returns RPC_E_WRONG_THREAD before entering JavaScript.
module.exports = function createOwnerAction(runtime, { completed = false, fail = false } = {}) {
const { DynWinRtType: T, DynWinRtMethodSig: Sig, DynWinRtValue: V, WinGuid } = runtime
const action = T.iAsyncAction()
const infoIid = WinGuid.parse('00000036-0000-0000-c000-000000000046')
const handlerIid = WinGuid.parse('a4ed5c81-76c9-40bd-8be6-b1d90fb20ae7')
const handlerType = T.delegate(handlerIid)
const statusType = T.enumType('Windows.Foundation.AsyncStatus')
const invoke = new Sig().addIn(action).addIn(statusType)
const plan = (name, iid, methods, requires = []) =>
runtime.DynWinRtInterfacePlan.create(
name,
T.interface(iid),
methods.map(([name, signature], index) => ({ name, signature, vtableIndex: 6 + index })),
requires,
)
const actionPlan = plan(
'IAsyncAction',
action.iid(),
[
['put_Completed', new Sig().addIn(handlerType)],
['get_Completed', new Sig().addOut(handlerType)],
['GetResults', new Sig()],
],
[infoIid],
)
const infoPlan = plan('IAsyncInfo', infoIid, [
['get_Id', new Sig().addOut(T.u32())],
['get_Status', new Sig().addOut(statusType)],
['get_ErrorCode', new Sig().addOut(T.hresult())],
['Cancel', new Sig()],
['Close', new Sig()],
])
const sourceIid = WinGuid.parse('970dd88b-f056-4a63-a5a2-f41236e11215')
const start = new Sig().addOut(action)
const source = T.registerInterface('ITestActionSource', sourceIid).addMethod('Start', start)
const sourcePlan = plan('ITestActionSource', sourceIid, [['Start', start]])
let handler
let resultCalls = 0
const status = () => V.enumValue(statusType, completed ? (fail ? 3 : 1) : 0)
const notify = () => handler.invokeDelegate(handlerIid, invoke, [operation, status()])
const owner = runtime.DynWinRtImplementation.create([actionPlan, infoPlan, sourcePlan], (iface, slot, args) => {
if (iface === 2) return [identity]
if (iface === 0) {
if (slot === 6) {
handler = args[0]
if (completed) notify()
} else if (slot === 7) {
return [handler ?? V.nullValue()]
} else {
resultCalls++
if (!completed || fail) throw new Error('owner action GetResults failed')
}
} else {
if (slot === 6) return [V.u32(1)]
if (slot === 7) return [status()]
if (slot === 8) return [V.hresult(completed && fail ? 0x80004005 | 0 : 0)]
}
return []
})
const identity = owner.toValue()
const sourceView = identity.cast(sourceIid)
const operation = source.method(6).invoke(sourceView, [])
sourceView.release()
return {
operation,
finish() {
completed = true
notify()
},
get resultCalls() {
return resultCalls
},
dispose() {
owner.dispose()
handler?.release()
identity.release()
operation.release()
},
}
}
Loading
Loading