Skip to content

Commit

Permalink
polyfills: refactor Navigator and make it more spec-compliant
Browse files Browse the repository at this point in the history
- Make it a class with the right toStringTag
- Add version to userAgent
- Add platform matching browsers (we have this information in 2 places
  now, but I think this is ok since libraries can still rely on the
  navigator behavior)
- Add hardwareConcurrency, replacing tjs.system.availableParallelism
  • Loading branch information
saghul committed Sep 24, 2024
1 parent 7e961ef commit 84834f6
Show file tree
Hide file tree
Showing 7 changed files with 33,645 additions and 33,516 deletions.
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

0 comments on commit 84834f6

Please sign in to comment.