Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore and extend feature index generation with method support #3486

Merged
merged 19 commits into from
Feb 25, 2025
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
2 changes: 2 additions & 0 deletions crates/libs/bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ targets = []

[dependencies]
rayon = "1.10"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
139 changes: 139 additions & 0 deletions crates/libs/bindgen/src/index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use super::*;
use serde::Serialize;

#[derive(Default, Serialize)]
struct Index {
version: u8,
namespaces: Vec<String>,
items: BTreeMap<usize, Vec<IndexItem>>,
}

#[derive(Default, Serialize)]
struct IndexItem {
#[serde(rename = "n")]
name: String,
#[serde(rename = "f")]
features: Vec<usize>,
}

impl Index {
fn new() -> Self {
Self {
version: 2,
..Default::default()
}
}

fn get_or_create_namespace_index(&mut self, namespace: &str) -> usize {
match self.namespaces.iter().position(|ns| ns == namespace) {
Some(idx) => idx,
None => {
self.namespaces.push(namespace.to_string());
self.namespaces.len() - 1
}
}
}

fn add_item(&mut self, namespace: &str, name: &str, features: BTreeSet<String>) {
let namespace_idx = self.get_or_create_namespace_index(namespace);

let mut compact = BTreeSet::new();
for feature in features.iter().rev() {
if feature.is_empty() {
continue;
}

if !compact
.iter()
.any(|c: &&str| namespace_starts_with(c, feature))
{
compact.insert(feature.as_str());
}
}

let features: Vec<usize> = compact
.iter()
.map(|dep| self.get_or_create_namespace_index(dep))
.collect();
let items = self.items.entry(namespace_idx).or_default();

if let Some(existing_item) = items.iter_mut().find(|item| item.name == name) {
for &feature in &features {
if !existing_item.features.contains(&feature) {
existing_item.features.push(feature);
}
}
existing_item.features.sort();
return;
}

let index_item = IndexItem {
name: name.to_string(),
features,
};

items.push(index_item);
}
}

const EXCLUDED_NAMESPACES: &[&str] = &["Windows.Foundation", "Windows.Win32.Foundation"];

#[doc(hidden)]
pub fn write(types: &TypeMap, output: &str) {
let mut feature_index = Index::new();
let mut all_types: Vec<_> = types.values().flatten().collect();

all_types.sort_by(|a, b| {
let a_name = a.type_name();
let b_name = b.type_name();
(a_name.namespace(), a_name.name()).cmp(&(b_name.namespace(), b_name.name()))
});

for ty in all_types {
let type_deps = ty.dependencies();
let type_name = ty.type_name();

let namespace = type_name.namespace();
if namespace.is_empty() {
continue;
}

let features: BTreeSet<String> = type_deps
.keys()
.filter(|tn| !EXCLUDED_NAMESPACES.contains(&tn.namespace()))
.map(|tn| tn.namespace().to_string())
.chain(std::iter::once(namespace.to_string()))
.collect();

feature_index.add_item(namespace, type_name.name(), features);

let methods = match ty {
Type::CppInterface(ty) => {
let interface_name = ty.def.name();
Some((ty.def.methods(), interface_name))
}
Type::Interface(ty) => {
let interface_name = ty.def.name();
Some((ty.def.methods(), interface_name))
}
_ => None,
};

if let Some((methods, interface_name)) = methods {
for method in methods {
let method_deps = method.signature(namespace, &[]).dependencies();
let method_features: BTreeSet<String> = method_deps
.keys()
.filter(|tn| !EXCLUDED_NAMESPACES.contains(&tn.namespace()))
.map(|tn| tn.namespace().to_string())
.chain(std::iter::once(namespace.to_string()))
.collect();

let scoped_name = format!("{}.{}", interface_name, method.name());
feature_index.add_item(namespace, &scoped_name, method_features);
}
}
}

write_to_file(output, serde_json::to_string(&feature_index).unwrap());
}
8 changes: 8 additions & 0 deletions crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod derive;
mod derive_writer;
mod filter;
mod guid;
mod index;
mod io;
mod libraries;
mod param;
Expand Down Expand Up @@ -95,6 +96,7 @@ where
let mut output = String::new();
let mut sys = false;
let mut link = "windows_link".to_string();
let mut index = false;

for arg in &args {
if arg.starts_with('-') {
Expand All @@ -118,6 +120,7 @@ where
"--sys" => sys = true,
"--implement" => implement = true,
"--link" => kind = ArgKind::Link,
"--index" => index = true,
_ => panic!("invalid option `{arg}`"),
},
ArgKind::Output => {
Expand Down Expand Up @@ -215,6 +218,11 @@ where
};

writer.write(tree);

if index {
index::write(&config.types, &format!("{}/features.json", config.output));
}

config.warnings.build()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/windows/features.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/tools/bindings/src/windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
--out crates/libs/windows
--package --no-comment --no-allow
--rustfmt max_width=800,newline_style=Unix
--index

--filter
Windows
Expand Down
7 changes: 7 additions & 0 deletions web/features/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"tabWidth": 4,
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 100
}
103 changes: 58 additions & 45 deletions web/features/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,61 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react/recommended',
'plugin:react-hooks/recommended',
],
overrides: [
import js from '@eslint/js';
import ts from '@typescript-eslint/eslint-plugin';
import tsParser from '@typescript-eslint/parser';
import react from 'eslint-plugin-react';
import reactHooks from 'eslint-plugin-react-hooks';

export default [
{
env: {
node: true,
},
files: ['.eslintrc.{js,cjs}'],
parserOptions: {
sourceType: 'script',
},
ignores: [
'**/webpack.config.*',
'**/eslint.*.js',
'**/react-app-env.d.ts',
'dist/**',
'build/**',
'node_modules/**',
]
},
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'react', 'react-hooks'],
rules: {
indent: [
'error',
4,
{
SwitchCase: 1,
},
],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single'],
semi: ['error', 'always'],
'react/jsx-uses-react': 'off',
'react/react-in-jsx-scope': 'off',
},
settings: {
react: {
version: 'detect',
js.configs.recommended,
{
files: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'],
languageOptions: {
parser: tsParser,
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
},
},
plugins: {
'@typescript-eslint': ts,
react,
'react-hooks': reactHooks,
},
rules: {
...ts.configs['recommended'].rules,
...react.configs.recommended.rules,
'indent': ['error', 4, { 'SwitchCase': 1 }],
'quotes': ['error', 'single'],
'semi': ['error', 'always'],
'react/jsx-uses-react': 'off',
'react/react-in-jsx-scope': 'off',
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
'@typescript-eslint/no-unused-vars': 'warn',
'no-undef': 'off',
},
settings: {
react: {
version: 'detect',
},
}
},
{
files: ['.eslintrc.{js,cjs}'],
languageOptions: {
sourceType: 'script',
}
},
},
};
];
Loading