Skip to content

Instantly share code, notes, and snippets.

@jfparadis
Last active January 24, 2020 03:01
Show Gist options
  • Select an option

  • Save jfparadis/07c48dbfe32b669ced0c1769df96a292 to your computer and use it in GitHub Desktop.

Select an option

Save jfparadis/07c48dbfe32b669ced0c1769df96a292 to your computer and use it in GitHub Desktop.
A compartment module resolver function for XS

A compartment module resolver function for XS

This gist shows that the compartment module map doesn't have to be deterministic.

On each subsequent run, the module map resolves to different modules depending on the current seconds on the clock.

Output

Example of executing the same code without recompile before the second run:

$ mcconfig -m -d
# xsc main.xsb
# xsl modules
# cc mc.xs.c
# ld mc.so
Current date Thu Jan 23 2020 18:58:08 GMT-0800
Current seconds 8
even
$ mcconfig -m -d
Current date Thu Jan 23 2020 18:58:13 GMT-0800
Current seconds 13
odd
export default function() {
return "even";
}
const d = new Date();
trace(`Current date ${d}\n`)
const s = d.getSeconds();
trace(`Current seconds ${s}\n`)
function resolver(referer, specifier) {
let target;
switch(specifier) {
case 'vary':
const f = s % 2 === 1;
target = f ? 'odd' : 'even';
break;
default:
target = specifier;
}
return Compartment.map[target];
}
const map = {
mod: resolver('', 'mod'),
vary: resolver('', 'vary'),
};
const cmp = new Compartment("mod", {}, map);
cmp.export.test();
{
"include": "$(MODDABLE)/examples/manifest_base.json",
"modules": {
"*": [
"./main",
"./mod",
"./odd",
"./even",
],
},
"preload": [
],
}
import vary from "vary";
export function test() {
trace(vary()+'\n');
}
export default function() {
return "odd";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment