Skip to content

Updated coincident to its latest v4 #136

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

Merged
merged 10 commits into from
Jul 1, 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: 1 addition & 1 deletion docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/service-worker.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import s from"https://cdn.jsdelivr.net/npm/sabayon/dist/polyfill.js";if(!globalThis.crossOriginIsolated){const{isArray:r}=Array,t=s=>s.length&&"object"==typeof s[0]&&null!==s[0];globalThis.Blob=class extends Blob{constructor(s,...e){t(e)&&"text/javascript"===e[0].type&&r(s)&&"string"==typeof s.at(0)&&(s[0]=s[0].replace(/^\/\*@\*\//,'import "https://cdn.jsdelivr.net/npm/sabayon/dist/polyfill.js";')),super(s,...e)}},globalThis.Worker=class extends Worker{constructor(r,...e){if(t(e)){const r=e[0].serviceWorker||e[0].service_worker;r&&s(r)}super(r,...e)}}}

1 change: 0 additions & 1 deletion docs/toml-CkEFU7ly.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,3 @@ const parse = toml => {
/* c8 ignore stop */

export { parse };
//# sourceMappingURL=toml-CkEFU7ly.js.map
2 changes: 1 addition & 1 deletion docs/toml-DiUM0_qs.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions docs/zip-CAMAhqMX.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/zip-CAMAhqMX.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions esm/interpreter/_python.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as FLATTED from 'coincident/flatted';
const JSON = { parse: FLATTED.decode, stringify: FLATTED.encode };
import * as DIRECT from 'reflected-ffi/direct';
const JSON = { parse: DIRECT.decode, stringify: DIRECT.encode };

import { fetchFiles, fetchJSModules, fetchPaths } from './_utils.js';
import { IDBMapSync, dedent } from '../utils.js';
Expand Down
8 changes: 3 additions & 5 deletions esm/interpreter/webr.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { create } from 'gc-hook';
import { dedent } from '../utils.js';
import { fetchFiles, fetchJSModules, fetchPaths } from './_utils.js';
import { io, stdio } from './_io.js';

const type = 'webr';
const r = new WeakMap();
const fr = new FinalizationRegistry(fn => fn());

// REQUIRES INTEGRATION TEST
/* c8 ignore start */
const run = async (interpreter, code) => {
const { shelter, destroy, io } = r.get(interpreter);
const { output, result } = await shelter.captureR(dedent(code));
for (const { type, data } of output) io[type](data);
// this is a double proxy but it's OK as the consumer
// of the result here needs to invoke explicitly a conversion
// or trust the `(await p.toJs()).values` returns what's expected.
return create(result, destroy, { token: false });
fr.register(result, destroy);
return result;
};

export default {
Expand Down
68 changes: 35 additions & 33 deletions esm/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import sabayon from 'https://cdn.jsdelivr.net/npm/sabayon/dist/polyfill.js';
// Let's avoid 404s *but* this should not be needed anymore*

// ignore browsers that already support SharedArrayBuffer
if (!globalThis.crossOriginIsolated) {
const { isArray } = Array;
const isOptions = args => args.length && typeof args[0] === 'object' && args[0] !== null;
// import sabayon from 'https://cdn.jsdelivr.net/npm/sabayon/dist/polyfill.js';

// early patch Blob to inject sabayon polyfill for service-worker
globalThis.Blob = class extends Blob {
constructor(blobParts, ...args) {
if (
isOptions(args) &&
args[0].type === 'text/javascript' &&
isArray(blobParts) &&
typeof blobParts.at(0) === 'string'
) {
blobParts[0] = blobParts[0].replace(
/^\/\*@\*\//,
'import "https://cdn.jsdelivr.net/npm/sabayon/dist/polyfill.js";'
);
}
super(blobParts, ...args);
}
};
// // ignore browsers that already support SharedArrayBuffer
// if (!globalThis.crossOriginIsolated) {
// const { isArray } = Array;
// const isOptions = args => args.length && typeof args[0] === 'object' && args[0] !== null;

// early patch Worker to bootstrap sabayon for service-worker
globalThis.Worker = class extends Worker {
constructor(url, ...args) {
if (isOptions(args)) {
const sw = args[0].serviceWorker || args[0].service_worker;
if (sw) sabayon(sw);
}
super(url, ...args);
}
};
}
// // early patch Blob to inject sabayon polyfill for service-worker
// globalThis.Blob = class extends Blob {
// constructor(blobParts, ...args) {
// if (
// isOptions(args) &&
// args[0].type === 'text/javascript' &&
// isArray(blobParts) &&
// typeof blobParts.at(0) === 'string'
// ) {
// blobParts[0] = blobParts[0].replace(
// /^\/\*@\*\//,
// 'import "https://cdn.jsdelivr.net/npm/sabayon/dist/polyfill.js";'
// );
// }
// super(blobParts, ...args);
// }
// };

// // early patch Worker to bootstrap sabayon for service-worker
// globalThis.Worker = class extends Worker {
// constructor(url, ...args) {
// if (isOptions(args)) {
// const sw = args[0].serviceWorker || args[0].service_worker;
// if (sw) sabayon(sw);
// }
// super(url, ...args);
// }
// };
// }
15 changes: 8 additions & 7 deletions esm/worker/_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import IDBMap from '@webreflection/idb-map';
import IDBMapSync from '@webreflection/idb-map/sync';

import { decoder } from 'coincident/flatted/decoder';
import coincident from 'coincident/window/worker';

import { assign, create, createFunction, createOverload, createResolved, dispatch, registerJSModules } from '../utils.js';
Expand Down Expand Up @@ -35,22 +34,23 @@ const add = (type, fn) => {

const {
proxy: sync,
sync: polyfill,
native,
window,
isWindowProxy
isWindowProxy,
ffi,
} = await coincident({
decoder,
transfer: false,
transform: value => transform ? transform(value) : value
});

const xworker = {
// propagate the fact SharedArrayBuffer is polyfilled
polyfill: !native,
polyfill,
// allows synchronous utilities between this worker and the main thread
sync,
// allow access to the main thread world whenever it's possible
window: native ? window : null,
window: (native || polyfill) ? window : null,
// allow introspection for foreign (main thread) refrences
isWindowProxy,
// standard worker related events / features
Expand Down Expand Up @@ -129,7 +129,7 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })
// there's no way to query the DOM, use foreign CustomEvent and so on
// in case there's no SharedArrayBuffer around.
let CustomEvent, document, notify, currentScript = null, target = '';
if (native) {
if (native || polyfill) {
({ CustomEvent, document } = window);
currentScript = id && document.getElementById(id) || null;
notify = kind => dispatch(currentScript, custom || type, kind, true, CustomEvent);
Expand All @@ -147,6 +147,7 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })
currentScript,
config: resolved.config,
js_modules: JSModules,
ffi,
get target() {
if (!target && currentScript) {
if (tag === 'SCRIPT') {
Expand Down Expand Up @@ -219,7 +220,7 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })
add('error');
add('message');
add('messageerror');
if (native) {
if (native || polyfill) {
addEventListener('py:progress', ({ type, detail }) => {
window.dispatchEvent(new window.CustomEvent(type, { detail }));
});
Expand Down
8 changes: 7 additions & 1 deletion esm/worker/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export default (...args) =>
const [ config ] = getConfigURLAndType(options.config, options.configURL);

const serviceWorker = options?.serviceWorker || options?.service_worker;
const worker = xworker({ serviceWorker });
const worker = xworker({
serviceWorker,
reflected_ffi_timeout: globalThis.reflected_ffi_timeout ?? -1,
});
const { postMessage } = worker;
const isHook = this instanceof Hook;

Expand All @@ -62,6 +65,9 @@ export default (...args) =>
defineProperties(worker, {
sync: { value: sync },
ready: { value: resolver.promise },
ffi: {
direct: worker.direct,
},
postMessage: {
value: (data, ...rest) => bootstrap.then(
() => postMessage.call(worker, data, ...rest),
Expand Down
2 changes: 1 addition & 1 deletion esm/worker/hook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import toJSONCallback from 'to-json-callback';
import toJSONCallback from 'reflected-ffi/to-json-callback';

import { dedent } from '../utils.js';
import { js as jsHooks, code as codeHooks } from '../hooks.js';
Expand Down
Loading
Loading