Skip to content

Commit 101fd64

Browse files
committed
Metamodule implementation
1 parent c5f4cda commit 101fd64

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

JavaScript/g-metamodule.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const fs = require('node:fs').promises;
4+
const vm = require('node:vm');
5+
6+
const RUN_OPTIONS = { timeout: 5000, displayErrors: false };
7+
8+
const load = async (filePath, sandbox) => {
9+
const src = await fs.readFile(filePath, 'utf8');
10+
const code = `'use strict';\n${src}`;
11+
const script = new vm.Script(code);
12+
const context = vm.createContext(Object.freeze({ ...sandbox }));
13+
const exports = script.runInContext(context, RUN_OPTIONS);
14+
return exports;
15+
};
16+
17+
const main = async () => {
18+
const sandbox = { Map: class PseudoMap {} };
19+
const exported = await load('./h-example.mm', sandbox);
20+
console.log(exported);
21+
};
22+
23+
main();

JavaScript/h-example.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
({
2+
doSomething(a, b) {
3+
console.log({ a, b });
4+
},
5+
6+
async doSomethingElse(name) {
7+
console.log({ name });
8+
},
9+
10+
collection: new Map(),
11+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy