Skip to content
Closed
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ is not a general Automation or native Win32 projection, and it does not project
flat DLL exports.

The current CI baseline against
`Microsoft.Windows.SDK.Win32Metadata` 71.0.14-preview is **5,567 of 7,929
eligible interfaces (70.21%)** with complete safe code generation. Supported
`Microsoft.Windows.SDK.Win32Metadata` 71.0.14-preview is **5,681 of 7,929
eligible interfaces (71.65%)** with complete safe code generation. Supported
contracts include generated coclass activation and QueryInterface views,
managed interface ownership, native POD layouts, typed counted buffers,
BSTR/HSTRING, validated VARIANT, SAFEARRAY and PROPVARIANT subsets, and
Expand Down
134 changes: 32 additions & 102 deletions bindings/js/__test__/generated-unsafe-companion.e2e.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bindings/js/__test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ test('package facades exactly partition native exports', (t) => {
'DynComNativeStruct',
'DynComNativeStructArray',
'DynComNativeUnion',
'DynComOwnedHandle',
'DynComPropVariant',
'DynComSafeArray',
'DynComStatStg',
Expand Down
1 change: 1 addition & 0 deletions bindings/js/scripts/generate-entrypoints.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const comExports = new Set([
'DynComNativeStruct',
'DynComNativeStructArray',
'DynComNativeUnion',
'DynComOwnedHandle',
'DynComPropVariant',
'DynComSafeArray',
'DynComStatStg',
Expand Down
204 changes: 204 additions & 0 deletions bindings/js/src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub(super) enum PointerProvenance {
ComOutput,
CoTaskMemOutput,
BstrOutput,
OwnedHandleOutput(dynwinrt::com::OwnedHandleCleanup),
}

pub(super) struct NativeInvocationLeases {
Expand Down Expand Up @@ -901,6 +902,7 @@ fn pointer(value: Unknown) -> napi::Result<DynWinRTValue> {
dynwinrt::WinRTValue::RawPtr(std::ptr::null_mut()),
));
}

if value_type == sys::ValueType::napi_bigint {
let bigint = unsafe { BigInt::from_napi_value(env, raw) }?;
let (negative, bits, lossless) = bigint.get_u64();
Expand Down Expand Up @@ -962,6 +964,26 @@ fn pointer(value: Unknown) -> napi::Result<DynWinRTValue> {
))
}

fn exact_null_pointer(value: Unknown) -> napi::Result<DynWinRTValue> {
use napi::sys;

let env = value.value().env;
let raw = value.value().value;
let mut value_type = sys::ValueType::napi_undefined;
unsafe { sys::napi_typeof(env, raw, &mut value_type) };
if !matches!(
value_type,
sys::ValueType::napi_null | sys::ValueType::napi_undefined
) {
return Err(napi::Error::from_reason(
"exactNullPointer(): value must be null or undefined",
));
}
Ok(DynWinRTValue::with_borrowed_pointer(
dynwinrt::WinRTValue::RawPtr(std::ptr::null_mut()),
))
}

fn safe_data_pointer(value: Unknown, nullable: bool) -> napi::Result<DynWinRTValue> {
use napi::sys;

Expand Down Expand Up @@ -1308,6 +1330,24 @@ fn take_bstr(value: &mut DynWinRTValue) -> napi::Result<String> {
String::try_from(&value).map_err(|error| napi::Error::from_reason(error.to_string()))
}

fn take_owned_handle(
value: &mut DynWinRTValue,
cleanup: dynwinrt::com::OwnedHandleCleanup,
description: &str,
) -> napi::Result<DynComOwnedHandle> {
let ptr = take_native_output_pointer(
value,
PointerProvenance::OwnedHandleOutput(cleanup),
description,
)?;
if ptr.is_null() {
return Err(napi::Error::from_reason(format!(
"{description} returned a null handle on success"
)));
}
Ok(DynComOwnedHandle::new(ptr.addr(), cleanup))
}

fn copy_callback_bstr(value: &DynWinRTValue) -> napi::Result<Option<String>> {
let dynwinrt::com::Value::Bstr(value) = value.to_com_value()? else {
return Err(napi::Error::from_reason("COM callback value is not a BSTR"));
Expand Down Expand Up @@ -2462,6 +2502,13 @@ impl DynComUnsafe {
self::pointer(value)
}

#[napi]
pub fn exact_null_pointer(
#[napi(ts_arg_type = "null | undefined")] value: Unknown,
) -> napi::Result<DynWinRTValue> {
self::exact_null_pointer(value)
}

#[napi]
pub fn wide_string_pointer(
#[napi(ts_arg_type = "string | bigint | number | Buffer | Uint8Array | null | undefined")]
Expand Down Expand Up @@ -3020,6 +3067,81 @@ pub struct DynComStatStg {
value: Option<dynwinrt::com::StatStgValue>,
}

#[napi]
#[derive(Debug)]
pub struct DynComOwnedHandle {
address: Option<usize>,
cleanup: dynwinrt::com::OwnedHandleCleanup,
}

impl DynComOwnedHandle {
fn new(address: usize, cleanup: dynwinrt::com::OwnedHandleCleanup) -> Self {
Self {
address: Some(address),
cleanup,
}
}

fn cleanup_address(
address: usize,
cleanup: dynwinrt::com::OwnedHandleCleanup,
) -> napi::Result<()> {
let succeeded = match cleanup {
dynwinrt::com::OwnedHandleCleanup::DeleteObject => unsafe {
windows::Win32::Graphics::Gdi::DeleteObject(windows::Win32::Graphics::Gdi::HGDIOBJ(
std::ptr::with_exposed_provenance_mut(address),
))
.as_bool()
},
};
if succeeded {
Ok(())
} else {
Err(napi::Error::from_reason(
"DeleteObject failed for an owned GDI handle",
))
}
}
}

impl Drop for DynComOwnedHandle {
fn drop(&mut self) {
if let Some(address) = self.address.take() {
let _ = Self::cleanup_address(address, self.cleanup);
}
}
}

#[napi]
impl DynComOwnedHandle {
#[napi(getter)]
pub fn value(&self) -> napi::Result<BigInt> {
self
.address
.map(|address| BigInt::from(address as u64))
.ok_or_else(|| napi::Error::from_reason("Owned handle has been released"))
}

#[napi(getter)]
pub fn is_released(&self) -> bool {
self.address.is_none()
}

#[napi]
pub fn release(&mut self) -> napi::Result<()> {
let Some(address) = self.address.take() else {
return Ok(());
};
match Self::cleanup_address(address, self.cleanup) {
Ok(()) => Ok(()),
Err(error) => {
self.address = Some(address);
Err(error)
}
}
}
}

impl DynComStatStg {
fn new(value: dynwinrt::com::StatStgValue) -> Self {
Self {
Expand Down Expand Up @@ -4507,6 +4629,13 @@ impl DynCom {
DynComType(dynwinrt::com::Type::borrowed_handle_output())
}

#[napi]
pub fn delete_object_handle_output_type() -> DynComType {
DynComType(dynwinrt::com::Type::owned_handle_output(
dynwinrt::com::OwnedHandleCleanup::DeleteObject,
))
}

#[napi]
pub fn owned_com_pointer_type() -> DynComType {
DynComType(dynwinrt::com::Type::owned_com_pointer())
Expand Down Expand Up @@ -4824,6 +4953,13 @@ impl DynCom {
value.bind_current_com_apartment()
}

#[napi]
pub fn exact_null_pointer(
#[napi(ts_arg_type = "null | undefined")] value: Unknown,
) -> napi::Result<DynWinRTValue> {
self::exact_null_pointer(value)
}

#[napi]
pub fn pointer(
#[napi(ts_arg_type = "bigint | number | Buffer | Uint8Array | null | undefined")]
Expand Down Expand Up @@ -4905,6 +5041,18 @@ impl DynCom {
)
}

#[napi]
pub fn null_string_array(wide: bool) -> DynWinRTValue {
DynWinRTValue::from_com_value(
dynwinrt::com::Value::Buffer(dynwinrt::com::ComBufferValue::null_string_array(if wide {
dynwinrt::com::StringEncoding::Utf16
} else {
dynwinrt::com::StringEncoding::Ansi
})),
dynwinrt::com::PointerOutputKind::None,
)
}

#[napi]
pub fn null_native_struct_pointer() -> DynWinRTValue {
DynWinRTValue::new(dynwinrt::WinRTValue::Null)
Expand Down Expand Up @@ -5534,6 +5682,15 @@ impl DynCom {
Ok(DynComStatStg::new(result))
}

#[napi]
pub fn take_delete_object_handle(value: &mut DynWinRTValue) -> napi::Result<DynComOwnedHandle> {
take_owned_handle(
value,
dynwinrt::com::OwnedHandleCleanup::DeleteObject,
"DeleteObject-owned handle",
)
}

#[napi]
pub fn wide_string_pointer(
#[napi(ts_arg_type = "string | bigint | number | Buffer | Uint8Array | null | undefined")]
Expand Down Expand Up @@ -6443,6 +6600,53 @@ mod tests {
assert!(matches!(value.0, dynwinrt::WinRTValue::RawPtr(raw) if raw == ptr));
}

#[test]
fn delete_object_owned_handle_consumes_exact_provenance_and_releases_once() {
let bitmap = unsafe { windows::Win32::Graphics::Gdi::CreateBitmap(1, 1, 1, 1, None) };
assert!(!bitmap.is_invalid());
let ptr = bitmap.0;
let mut value = DynWinRTValue::from_com_result(
dynwinrt::WinRTValue::RawPtr(ptr),
dynwinrt::com::PointerOutputKind::OwnedHandle(
dynwinrt::com::OwnedHandleCleanup::DeleteObject,
),
);

let mut owner = take_owned_handle(
&mut value,
dynwinrt::com::OwnedHandleCleanup::DeleteObject,
"test bitmap",
)
.unwrap();
assert!(matches!(value.0, dynwinrt::WinRTValue::Null));
assert!(!owner.is_released());
owner.release().unwrap();
assert!(owner.is_released());
owner.release().unwrap();
assert!(!unsafe {
windows::Win32::Graphics::Gdi::DeleteObject(windows::Win32::Graphics::Gdi::HGDIOBJ(ptr))
}
.as_bool());
}

#[test]
fn delete_object_owned_handle_rejects_other_pointer_provenance() {
let mut value = DynWinRTValue::from_com_result(
dynwinrt::WinRTValue::RawPtr(0x1234usize as *mut std::ffi::c_void),
dynwinrt::com::PointerOutputKind::CoTaskMem,
);
assert!(take_owned_handle(
&mut value,
dynwinrt::com::OwnedHandleCleanup::DeleteObject,
"test bitmap",
)
.unwrap_err()
.reason
.contains("CoTaskMemOutput"));
value.0 = dynwinrt::WinRTValue::Null;
value.2 = PointerProvenance::None;
}

#[test]
fn takes_and_frees_bstr() {
let raw = windows::core::BSTR::from("dynwinrt").into_raw();
Expand Down
9 changes: 9 additions & 0 deletions bindings/js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,9 @@ impl DynWinRTValue {
dynwinrt::com::PointerOutputKind::Com => com::PointerProvenance::ComOutput,
dynwinrt::com::PointerOutputKind::CoTaskMem => com::PointerProvenance::CoTaskMemOutput,
dynwinrt::com::PointerOutputKind::Bstr => com::PointerProvenance::BstrOutput,
dynwinrt::com::PointerOutputKind::OwnedHandle(cleanup) => {
com::PointerProvenance::OwnedHandleOutput(cleanup)
}
}
} else {
com::PointerProvenance::None
Expand Down Expand Up @@ -1248,6 +1251,12 @@ impl DynWinRTValue {
com::PointerProvenance::BstrOutput => {
drop(unsafe { windows::core::BSTR::from_raw(ptr.cast()) });
}
com::PointerProvenance::OwnedHandleOutput(
dynwinrt::com::OwnedHandleCleanup::DeleteObject,
) => unsafe {
let _ =
windows::Win32::Graphics::Gdi::DeleteObject(windows::Win32::Graphics::Gdi::HGDIOBJ(ptr));
},
com::PointerProvenance::None
| com::PointerProvenance::Borrowed
| com::PointerProvenance::DetachedCom
Expand Down
1 change: 1 addition & 0 deletions crates/dynwinrt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ features = [
"Win32_System_Threading",
"Win32_System_Variant",
"Win32_System_WinRT",
"Win32_Graphics_Gdi",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",
"Win32_Storage_Packaging_Appx",
Expand Down
Loading
Loading