mirror of
https://github.com/HeyPuter/puter
synced 2024-11-15 06:15:47 +00:00
eb05fbd2dc
A package called "useapi" is introduced to provide a dynamic import system. This import system, rather than relying on the state of the filesystem, is populated as modules are installed into Puter's kernel. The "useapi" package is then used to add support for loading external mod directories as Puter kernel modules, making it possible to mod puter without any tooling.
101 lines
2.3 KiB
JavaScript
101 lines
2.3 KiB
JavaScript
const globalwith = (vars, fn) => {
|
|
const original_values = {};
|
|
const keys = Object.keys(vars);
|
|
|
|
for ( const key of keys ) {
|
|
if ( key in globalThis ) {
|
|
original_values[key] = globalThis[key];
|
|
}
|
|
globalThis[key] = vars[key];
|
|
}
|
|
|
|
try {
|
|
return fn();
|
|
} finally {
|
|
for ( const key of keys ) {
|
|
if ( key in original_values ) {
|
|
globalThis[key] = original_values[key];
|
|
} else {
|
|
delete globalThis[key];
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
const aglobalwith = async (vars, fn) => {
|
|
const original_values = {};
|
|
const keys = Object.keys(vars);
|
|
|
|
for ( const key of keys ) {
|
|
if ( key in globalThis ) {
|
|
original_values[key] = globalThis[key];
|
|
}
|
|
globalThis[key] = vars[key];
|
|
}
|
|
|
|
try {
|
|
return await fn();
|
|
} finally {
|
|
for ( const key of keys ) {
|
|
if ( key in original_values ) {
|
|
globalThis[key] = original_values[key];
|
|
} else {
|
|
delete globalThis[key];
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
let default_fn = () => {
|
|
const use = name => {
|
|
const parts = name.split('.');
|
|
let obj = use;
|
|
for ( const part of parts ) {
|
|
if ( ! obj[part] ) {
|
|
obj[part] = {};
|
|
}
|
|
obj = obj[part];
|
|
}
|
|
|
|
return obj;
|
|
};
|
|
const library = {
|
|
use,
|
|
def: (name, value) => {
|
|
const parts = name.split('.');
|
|
let obj = use;
|
|
for ( const part of parts.slice(0, -1) ) {
|
|
if ( ! obj[part] ) {
|
|
obj[part] = {};
|
|
}
|
|
obj = obj[part];
|
|
}
|
|
|
|
obj[parts[parts.length - 1]] = value;
|
|
},
|
|
withuse: fn => {
|
|
return globalwith({
|
|
use,
|
|
def: library.def,
|
|
}, fn);
|
|
},
|
|
awithuse: async fn => {
|
|
return await aglobalwith({
|
|
use,
|
|
def: library.def,
|
|
}, fn);
|
|
}
|
|
};
|
|
|
|
return library;
|
|
};
|
|
|
|
const useapi = function useapi () {
|
|
return default_fn();
|
|
};
|
|
|
|
// We export some things on the function itself
|
|
useapi.globalwith = globalwith;
|
|
|
|
module.exports = useapi;
|