Skip to content

Auto gen update #166

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
68 changes: 68 additions & 0 deletions generate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {spawnSync} from 'node:child_process';
import fs from 'node:fs';

export function execute(cmd, args, options) {
const { error, status } = spawnSync(cmd, args, {...options || {}, shell: true, stdio: 'inherit'});
if (error) {
throw new(Error);
}
if (status !== 0) {
throw new Error(`${cmd} exited with status code: ${status}`);
}
}

/**
* Transform generated to something closer to what we want
* so we have less manual work to do.
*/
function fixupGenerated(filename) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most (but not all) of these things would ideally be changed in the upstream generator. That repo is external but it was created for webgpu so we can change it freely.

Since some of these do make sense to keep here, it's not a big deal, since we need this script anyway. I'm fine with accepting it as is.

let s = fs.readFileSync(filename, { encoding: 'utf-8' });

// replace ' | null>' with ' null | undefined>'
// replace ' | null,' with ' null | undefined,'
s = s.replace(/ \| null(>|,)/g, ' | null | undefined$1');

// replace Array<x> with Iterable<x>
s = s.replace(/\bArray<(.*?)>/g, 'Iterable<$1>');

// add new(): never;
s = s.replace(/(\ndeclare\svar\s\w+:\s\{\n\s+prototype:.*?\n)\};/g, '$1 new(): never;\n};');

// replace : GPUExtent3D -> : GPUExtent3DStrict
s = s.replace(/: GPUExtent3D\b/g, ': GPUExtent3DStrict');

// replace AllowSharedBufferSource -> BufferSource | SharedArrayBuffer
s = s.replace(/AllowSharedBufferSource/g, 'BufferSource | SharedArrayBuffer');

// replace Promise<... | undefined> with Promise<...>
s = s.replace(/( Promise<[^>]*?)\s+\|\s+undefined\s*>/g, '$1>');

// fix constants
const constants = [
'GPUBufferUsage',
'GPUColorWrite',
'GPUMapMode',
'GPUShaderStage',
'GPUTextureUsage',
];
const constantRE = new RegExp(`interface (${constants.join('|')}) \\{`, 'g');
s = s.replace(constantRE, 'declare var $1: {');

// fix constructables
const constructables = [
'GPUInternalError',
'GPUPipelineError',
'GPUOutOfMemoryError',
'GPUUncapturedErrorEvent',
'GPUValidationError',
];
const constructableRE = new RegExp(`(\\ndeclare var (${constructables.join('|')}):\\s{\\n[\\s\\S]*?\\snew\\s\\([^;]*?\\));\\n};`, 'g');
s = s.replace(constructableRE, '$1: $2;\n};');

fs.writeFileSync(filename, s);
}

execute('./node_modules/.bin/bikeshed-to-ts', ['--in', './gpuweb/spec/index.bs', '--out', './generated/index.d.ts', '--forceGlobal', '--nominal'])
fixupGenerated('./generated/index.d.ts');
execute('./node_modules/.bin/prettier', ['-w', 'generated/index.d.ts']);

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"test": "for t in tests/*/ ; do (cd \"$t\" && npm i && npm test); done",
"build-docs": "cd tsdoc-src && npm ci && npm run build",
"generate": "bikeshed-to-ts --in ./gpuweb/spec/index.bs --out ./generated/index.d.ts --forceGlobal --nominal && prettier -w generated/index.d.ts",
"generate": "node generate.mjs",
"format": "prettier -w dist/index.d.ts"
},
"devDependencies": {
Expand Down