gpu

source of pkg::alloy::com

1// COM on Windows, the package `pkg::alloy::com` (PLAN.md, F6b;2// a package of its own since F10, which `window` and `text` share):3// interface ids, calls through another object's table of functions, and4// the strings, arrays and variants UI Automation, the file dialogs, drops5// and DirectWrite pass, with UTF-16 text both ways. The tables' slots6// each user reads are checked against MinGW's headers by7// `tests/windows/com_layout.sh`.8//9// ## How it was built10//11// Notes kept from each stage as it was finished, the sizes and numbers12// of their day; what changed since is in REVIEW.md and the history.13//14// Built in F6b inside `window` and moved here in F10a: see15// pkg::alloy::window's notes.1617import std::string;18import std::vector;19import std::option;20import std::ffi;2122extern CoInitializeEx(reserved: c_pointer, flags: u32) -> i32;23extern OleInitialize(reserved: c_pointer) -> i32;24extern CoCreateInstance(25    class: c_pointer,26    outer: c_pointer,27    context: u32,28    iid: c_pointer,29    out: c_pointer30) -> i32;31extern CoTaskMemFree(memory: c_pointer);32extern SysAllocString(text: c_pointer) -> c_pointer;33extern SysFreeString(text: c_pointer);34extern SafeArrayCreateVector(kind: VarType, lower: i32, count: u32) -> c_pointer;35extern SafeArrayPutElement(array: c_pointer, index: c_pointer, value: c_pointer) -> i32;3637exp fn initialize_ole() {38    OleInitialize(0);39}4041exp fn free_task_memory(memory: c_pointer) {42    CoTaskMemFree(memory);43}4445exp const S_OK: i32 = 0;46exp const E_NOINTERFACE: i32 = (0x80004002 to u32) as i32;4748// A GUID's 16 bytes from its text, `d6dd68d1-86fd-4332-8666-9abedea2d24c`:49// the first three groups little-endian, as Windows keeps them.50exp fn guid(text: &[u8]) -> [u8 : 16] {51    var digits: [u8 : 32] = [0 : 32];52    var count: u64 = 0;53    for (text) |c| {54        if (c != '-') {55            digits[count] = nibble(c);56            count += 1;57        }58    }59    var bytes: [u8 : 16] = [0 : 16];60    for ([..16 to u64]) |k| {61        bytes[k] = (digits[2 * k] << 4) | digits[2 * k + 1];62    }63    // Data1 (4 bytes), Data2 and Data3 (2 each) in little-endian order64    var out = bytes;65    for ([..4 to u64]) |k| {66        out[k] = bytes[3 - k];67    }68    out[4] = bytes[5];69    out[5] = bytes[4];70    out[6] = bytes[7];71    out[7] = bytes[6];72    return out;73}7475fn nibble(c: u8) -> u8 {76    if (c >= '0' && c <= '9') {77        return c - '0';78    }79    if (c >= 'a' && c <= 'f') {80        return c - 'a' + 10;81    }82    return c - 'A' + 10;83}8485exp const IID_UNKNOWN: [u8 : 16] = guid(&"00000000-0000-0000-c000-000000000046");8687// Whether the GUID at the address is this one.88exp fn is_guid(at: c_pointer, id: &[u8 : 16]) -> bool {89    const bytes = &at.view<u8>(16);90    for ([..16 to u64]) |k| {91        if (bytes[k] != id[k]) {92            return false;93        }94    }95    return true;96}9798// An object's function at the index in its table, as the C prototype99// `F`, which takes the object first.100exp fn method<F: CFunction>(object: c_pointer, index: u64) -> F {101    const table = object.view<c_pointer>(1)[0];102    return copy table.view<c_pointer>(index + 1)[index] as F;103}104105// IUnknown's Release.106exp fn release(object: c_pointer) {107    if (object != 0) {108        method<extern (c_pointer) -> u32>(object, 2)(object);109    }110}111112// An object of the class with the interface, if one can be made.113exp fn create(class: &[u8 : 16], iid: &[u8 : 16]) -> Option<c_pointer> {114    var out: c_pointer = 0;115    // CLSCTX_INPROC_SERVER116    const result = CoCreateInstance(117        c_pointer::of(&class[..]),118        0,119        1,120        c_pointer::of(&iid[..]),121        c_pointer::from(&out)122    );123    return if (result == S_OK) ::Some(out) else ::None;124}125126// Writes an address into an out parameter.127exp fn put(out: c_pointer, value: c_pointer) {128    if (out != 0) {129        out.view_var<c_pointer>(1)[0] = value;130    }131}132133// Frees a BSTR the program made and kept.134exp fn free_bstr(text: c_pointer) {135    SysFreeString(text);136}137138// A BSTR holding the text, which its receiver frees.139exp fn bstr(text: &[u8]) -> c_pointer {140    const units = wide(&text);141    return SysAllocString(c_pointer::of(&units.view()));142}143144// A VARIANT: its type, then its value.145exp type Variant = struct {146    exp kind: VarType,147    exp reserved: [u16 : 3],148    exp value: u64,149    exp extra: u64,150};151152exp type VarType = enum : u16 {153    Empty = 0,154    I4 = 3,155    R8 = 5,156    Bstr = 8,157    Bool = 11,158    Unknown = 13,159};160161// A variant of the type holding the value's bits.162fn Variant::of(kind: VarType, value: u64) -> Variant => Variant {163    .kind = kind,164    .reserved = [0 : 3],165    .value = value,166    .extra = 0167};168169exp fn Variant::empty() -> Variant => Variant::of(::Empty, 0);170171exp fn Variant::integer(value: i32) -> Variant => Variant::of(::I4, value as u32 to u64);172173exp fn Variant::boolean(value: bool) -> Variant => Variant::of(::Bool, if (value) 0xFFFF else 0);174175exp fn Variant::number(value: f64) -> Variant {176    var bits = value;177    return Variant::of(::R8, bits as u64);178}179180exp fn Variant::text(text: &[u8]) -> Variant => Variant::of(::Bstr, bstr(&text) to u64);181182// A variant holding a COM object, which its receiver releases.183exp fn Variant::unknown(object: c_pointer) -> Variant => Variant::of(::Unknown, object to u64);184185// A SAFEARRAY of 32-bit integers, which its receiver destroys.186exp fn integers(values: &[i32]) -> c_pointer {187    const array = SafeArrayCreateVector(::I4, 0, values.length() to u32);188    for ([..values.length()]) |k| {189        var index = k as i64 to i32;190        var value = values[k];191        SafeArrayPutElement(array, c_pointer::from(&index), c_pointer::from(&value));192    }193    return array;194}195196// A SAFEARRAY of doubles, which its receiver destroys.197exp fn doubles(values: &[f64]) -> c_pointer {198    const array = SafeArrayCreateVector(::R8, 0, values.length() to u32);199    for ([..values.length()]) |k| {200        var index = k as i64 to i32;201        var value = values[k];202        SafeArrayPutElement(array, c_pointer::from(&index), c_pointer::from(&value));203    }204    return array;205}206207// A SAFEARRAY of COM objects, each given a reference of its own, which208// its receiver destroys.209exp fn unknowns(values: &[c_pointer]) -> c_pointer {210    const array = SafeArrayCreateVector(::Unknown, 0, values.length() to u32);211    for ([..values.length()]) |k| {212        var index = k as i64 to i32;213        SafeArrayPutElement(array, c_pointer::from(&index), values[k]);214    }215    return array;216}217218// UTF-16 ending in a NUL, from UTF-8.219exp fn wide(text: &[u8]) -> Vector<u16> {220    var out = Vector<u16>::with_capacity(text.length() + 1);221    var k: u64 = 0;222    while (k < text.length()) {223        const lead = text[k] to u32;224        var length: u64 = 1;225        var code = lead;226        if (lead >= 0xF0) {227            length = 4;228            code = lead & 0x07;229        } else if (lead >= 0xE0) {230            length = 3;231            code = lead & 0x0F;232        } else if (lead >= 0xC0) {233            length = 2;234            code = lead & 0x1F;235        }236        for ([1..length]) |i| {237            code = (code << 6) | (if (k + i < text.length()) text[k + i] to u32 & 0x3F else 0);238        }239        k += length;240        if (code >= 0x10000) {241            code -= 0x10000;242            out.push((0xD800 + (code >> 10)) to u16);243            out.push((0xDC00 + (code & 0x3FF)) to u16);244        } else {245            out.push(code to u16);246        }247    }248    out.push(0);249    return move out;250}251252// UTF-8 from UTF-16 units, carriage returns left out when `lines`.253exp fn narrow(units: &[u16], lines: bool) -> String {254    var out = String::empty();255    var k: u64 = 0;256    while (k < units.length()) {257        var code = units[k] to u32;258        k += 1;259        if (260            code >= 0xD800261            && code < 0xDC00262            && k < units.length()263            && units[k] >= 0xDC00264            && units[k] < 0xE000265        ) {266            code = 0x10000 + ((code - 0xD800) << 10) + (units[k] to u32 - 0xDC00);267            k += 1;268        }269        if (lines && code == '\r' to u32) {270            continue;271        }272        if (code < 0x80) {273            out.push(code to u8);274        } else if (code < 0x800) {275            out.push((0xC0 | (code >> 6)) to u8);276            out.push((0x80 | (code & 0x3F)) to u8);277        } else if (code < 0x10000) {278            out.push((0xE0 | (code >> 12)) to u8);279            out.push((0x80 | ((code >> 6) & 0x3F)) to u8);280            out.push((0x80 | (code & 0x3F)) to u8);281        } else {282            out.push((0xF0 | (code >> 18)) to u8);283            out.push((0x80 | ((code >> 12) & 0x3F)) to u8);284            out.push((0x80 | ((code >> 6) & 0x3F)) to u8);285            out.push((0x80 | (code & 0x3F)) to u8);286        }287    }288    return move out;289}290291// UTF-8 from UTF-16 ending in a NUL, carriage returns left out when292// `lines`.293exp fn narrow_c(at: c_pointer, lines: bool) -> String {294    if (at == 0) {295        return String::empty();296    }297    var length: u64 = 0;298    while (at.view<u16>(length + 1)[length] != 0) {299        length += 1;300    }301    return narrow(&at.view<u16>(length), lines);302}