File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 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 ( ) ;
Original file line number Diff line number Diff line change 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+ });
You can’t perform that action at this time.
0 commit comments