1
- const { LuaFactory, LuaMultiReturn } = require ( 'wasmoon' )
1
+ const { LuaFactory, LuaEngine } = require ( 'wasmoon' )
2
2
const path = require ( 'path' )
3
3
const FunctionClassTypeExtension = require ( './legacyclasses' )
4
4
const fs = require ( 'fs/promises' )
5
5
6
- const start = async ( entryFile , arg ) => {
7
- const factory = new LuaFactory ( undefined , process . env )
6
+ module . exports = class {
7
+ /** @returns {Promise<LuaEngine> } */
8
+ async getLuaEngine ( ) {
9
+ await this . #setupIfNeeded( )
10
+ return this . #engine
11
+ }
8
12
9
- const fullEntryFile = path . resolve ( process . cwd ( ) , entryFile )
10
- const fullStdFile = path . resolve ( __dirname , "std.lua" )
13
+ /**
14
+ * @param {string } file
15
+ * @returns {Promise<void> }
16
+ */
17
+ async runFile ( file ) {
18
+ await this . #setupIfNeeded( )
11
19
12
- await factory . mountFile ( fullEntryFile , await fs . readFile ( fullEntryFile ) )
13
- await factory . mountFile ( fullStdFile , await fs . readFile ( fullStdFile ) )
20
+ this . #factory. mountFileSync ( await this . #factory. getLuaModule ( ) , file , await fs . readFile ( file ) )
14
21
15
- const engine = await factory . createEngine ( { injectObjects : true } )
22
+ try {
23
+ const thread = this . #engine. global . newThread ( )
24
+ thread . loadFile ( file )
16
25
17
- engine . global . registerTypeExtension ( 10 , new FunctionClassTypeExtension )
18
- engine . global . set ( 'arg' , arg )
19
- engine . global . set ( 'typeof' , value => typeof value )
20
- engine . global . set ( 'instanceof' , ( value , type ) => value instanceof type )
21
- engine . global . set ( 'new' , ( constructor , ...args ) => new constructor ( ...args ) )
22
- engine . global . set ( 'global' , global )
23
- engine . global . set ( 'mountFile' , factory . mountFileSync . bind ( factory ) )
24
- engine . global . set ( 'jsRequire' , ( modulename , metaDirectory ) => {
25
- if ( metaDirectory ) {
26
- if ( modulename . startsWith ( '.' ) ) {
27
- modulename = path . resolve ( metaDirectory , '..' , modulename )
28
- }
26
+ this . #engine. global . set ( 'jsRequire' , ( modulename , metaDirectory ) => {
27
+ if ( metaDirectory ) {
28
+ if ( modulename . startsWith ( '.' ) ) {
29
+ modulename = path . resolve ( metaDirectory , '..' , modulename )
30
+ }
29
31
30
- modulename = require . resolve ( modulename , { paths : [ fullEntryFile ] } )
31
- }
32
+ modulename = require . resolve ( modulename , { paths : [ file ] } )
33
+ }
34
+
35
+ return module . require ( modulename )
36
+ } )
32
37
33
- return module . require ( modulename )
34
- } )
35
-
36
- try {
37
- engine . doFileSync ( fullStdFile )
38
-
39
- const thread = engine . global . newThread ( )
40
- thread . loadFile ( fullEntryFile )
41
-
42
- await thread . run ( 0 )
43
- } catch ( e ) {
44
- console . error ( e )
38
+ await thread . run ( 0 )
39
+ } catch ( e ) {
40
+ console . error ( e )
41
+ }
45
42
}
46
- }
47
43
48
- module . exports = { start }
44
+ /** @type {LuaEngine | undefined } */
45
+ #engine
46
+
47
+ /** @type {LuaFactory | undefined } */
48
+ #factory
49
+
50
+ async #setupIfNeeded( ) {
51
+ if ( this . #factory) return
52
+
53
+ this . #factory = new LuaFactory ( undefined , process . env )
54
+ const luamodule = await this . #factory. getLuaModule ( )
55
+
56
+ const fullStdFile = path . resolve ( __dirname , "std.lua" )
57
+ this . #factory. mountFileSync ( luamodule , fullStdFile , await fs . readFile ( fullStdFile ) )
58
+
59
+ this . #engine = await this . #factory. createEngine ( { injectObjects : true } )
60
+
61
+ this . #engine. global . registerTypeExtension ( 10 , new FunctionClassTypeExtension )
62
+ this . #engine. global . set ( 'typeof' , value => typeof value )
63
+ this . #engine. global . set ( 'instanceof' , ( value , type ) => value instanceof type )
64
+ this . #engine. global . set ( 'new' , ( constructor , ...args ) => new constructor ( ...args ) )
65
+ this . #engine. global . set ( 'global' , global )
66
+ this . #engine. global . set ( 'mountFile' , ( path , content ) => this . #factory. mountFileSync ( luamodule , path , content ) )
67
+ this . #engine. global . set ( 'jsRequire' , ( modulename , metaDirectory ) => {
68
+ if ( metaDirectory ) {
69
+ if ( modulename . startsWith ( '.' ) ) {
70
+ modulename = path . resolve ( metaDirectory , '..' , modulename )
71
+ }
72
+
73
+ modulename = require . resolve ( modulename )
74
+ }
75
+
76
+ return module . require ( modulename )
77
+ } )
78
+ this . #engine. doFileSync ( fullStdFile )
79
+ }
80
+ }
0 commit comments