1 module as.mod;
2 import as.def;
3 import as.engine;
4 import as.func;
5 import std..string;
6 
7 enum ModuleCreateFlags : asEGMFlags {
8     /**
9         Only load the module if it exists
10     */
11     OnlyIfExists = asEGMFlags.asGM_ONLY_IF_EXISTS,
12 
13     /**
14         Try to create the module if it doesn't exist
15     */
16     CreateIfNotExists = asEGMFlags.asGM_CREATE_IF_NOT_EXISTS,
17 
18     /**
19         Always create the module, overwriting old modules with the same name
20     */
21     AlwaysCreate = asEGMFlags.asGM_ALWAYS_CREATE
22 }
23 
24 class Module {
25 private:
26     ScriptEngine engine;
27 
28     ~this() {
29         this.engine = null;
30     }
31 
32 package(as):
33     asIScriptModule* mod;
34     
35     this(ScriptEngine engine, asIScriptModule* mod) {
36         this.engine = engine;
37         this.mod = mod;
38     }
39 
40 public:
41 
42     /**
43         Gets the engine this module belongs to
44     */
45     ScriptEngine getEngine() {
46         return engine;
47     }
48 
49     /**
50         Sets the name of the module
51     */
52     void setName(string name) {
53         asModule_SetName(mod, name.toStringz);
54     }
55 
56     /**
57         Gets the name of the module
58     */
59     string getName() {
60         return cast(string)asModule_GetName(mod).fromStringz;
61     }
62 
63     /**
64         Discards the module, removing it from the engine
65     */
66     void discard() {
67         asModule_Discard(mod);
68     }
69 
70     /**
71         Adds a script section to the module
72     */
73     void addScriptSection(string name, string code, int lineOffset = 0) {
74         int err = asModule_AddScriptSection(mod, name.toStringz, code.toStringz, code.length, lineOffset);
75     }
76 
77     /**
78         Builds scripts
79     */
80     void build() {
81         int err = asModule_Build(mod);
82         assert(err != asERetCodes.asINVALID_CONFIGURATION, "Invalid configuration");
83         assert(err != asERetCodes.asERROR, "Failed to compile script");
84         assert(err != asERetCodes.asBUILD_IN_PROGRESS, "Another thread is currently building");
85         assert(err != asERetCodes.asINIT_GLOBAL_VARS_FAILED, "Unable to initialize at least one global variable");
86         assert(err != asERetCodes.asNOT_SUPPORTED, "Compiler support disabled");
87         assert(err != asERetCodes.asMODULE_IS_IN_USE, "Code is in use and can't be removed");
88     }
89 
90     /**
91         Gets function by declaration
92     */
93     Function getFunctionByDecl(string decl) {
94         return new Function(engine, asModule_GetFunctionByDecl(mod, decl.toStringz));
95     }
96 
97     /**
98         Gets function by name
99     */
100     Function getFunctionByName(string name) {
101         return new Function(engine, asModule_GetFunctionByName(mod, name.toStringz));
102     }
103 }