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

polyfills: refactor Navigator and make it more spec-compliant #655

Merged
merged 1 commit into from
Sep 24, 2024
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
5,961 changes: 2,976 additions & 2,985 deletions src/bundles/c/core/core.c

Large diffs are not rendered by default.

59,564 changes: 29,832 additions & 29,732 deletions src/bundles/c/core/polyfills.c

Large diffs are not rendered by default.

1,556 changes: 778 additions & 778 deletions src/bundles/c/core/run-main.c

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions src/js/core/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ Object.defineProperty(system, 'arch', {
value: uname.machine
});

Object.defineProperty(system, 'availableParallelism', {
enumerable: true,
configurable: false,
get: core.availableParallelism
});


Object.defineProperty(system, 'cpus', {
enumerable: true,
configurable: false,
Expand Down
66 changes: 58 additions & 8 deletions src/js/polyfills/navigator.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
const navigator = {};
const core = globalThis[Symbol.for('tjs.internal.core')];
const uname = core.uname();

Object.defineProperty(navigator, 'userAgent', {
enumerable: true,
configurable: false,
writable: false,
value: 'txiki.js'
});

function getNavigatorPlatform(arch, platform) {
if (platform === 'darwin') {
// On macOS, modern browsers return 'MacIntel' even if running on Apple Silicon.
return 'MacIntel';
} else if (platform === 'windows') {
// On Windows, modern browsers return 'Win32' even if running on a 64-bit version of Windows.
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#usage_notes
return 'Win32';
} else if (platform === 'linux') {
if (arch === 'ia32') {
return 'Linux i686';
} else if (arch === 'x64') {
return 'Linux x86_64';
}

return `Linux ${arch}`;
} else if (platform === 'freebsd') {
if (arch === 'ia32') {
return 'FreeBSD i386';
} else if (arch === 'x64') {
return 'FreeBSD amd64';
}

return `FreeBSD ${arch}`;
} else if (platform === 'openbsd') {
if (arch === 'ia32') {
return 'OpenBSD i386';
} else if (arch === 'x64') {
return 'OpenBSD amd64';
}

return `OpenBSD ${arch}`;
}

return `${platform} ${arch}`;
}

class Navigator {
get userAgent() {
return `txiki.js/${core.version}`;
}

get hardwareConcurrency() {
return core.availableParallelism();
}

get platform() {
return getNavigatorPlatform(uname.arch, core.platform);
}

get [Symbol.toStringTag]() {
return 'Navigator';
}
}

Object.defineProperty(globalThis, 'navigator', {
enumerable: true,
configurable: false,
writable: false,
value: navigator
value: new Navigator()
});
2 changes: 1 addition & 1 deletion src/js/run-main/run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export async function runTests(d) {
}

let failed = 0;
const testConcurrency = tjs.env.TJS_TEST_CONCURRENCY ?? tjs.system.availableParallelism;
const testConcurrency = tjs.env.TJS_TEST_CONCURRENCY ?? navigator.hardwareConcurrency;
const running = new Set();

// eslint-disable-next-line no-constant-condition
Expand Down
5 changes: 0 additions & 5 deletions types/src/txikijs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,11 +874,6 @@ declare global {
*/
readonly arch: string;

/**
* An estimate of the default amount of parallelism a program should use.
*/
readonly availableParallelism: number;

/**
* Information about the CPUs in the system.
*/
Expand Down
Loading