1 /** 2 This module implements functions in the System namespace such as currentTimeMillis 3 and getenv. 4 5 ──────────────────────────────────────────────────────────────────────────────── 6 7 Copyright (C) 2021 pillager86.rf.gd 8 9 This program is free software: you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation, either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT ANY 15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 16 PARTICULAR PURPOSE. See the GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <https://www.gnu.org/licenses/>. 20 */ 21 module mildew.stdlib.system; 22 23 import core.memory: GC; 24 import std.datetime.systime; 25 import std.process: environment; 26 27 import mildew.environment; 28 import mildew.interpreter; 29 import mildew.types; 30 31 /** 32 * Initializes the System namespace. Documentation for this library can be found at 33 * https://pillager86.github.io/dmildew/ 34 * Params: 35 * interpreter = The Interpreter instance to load the System namespace into. 36 */ 37 void initializeSystemLib(Interpreter interpreter) 38 { 39 auto systemNamespace = new ScriptObject("System", null, null); 40 systemNamespace["currentTimeMillis"] = new ScriptFunction("System.currentTimeMillis", 41 &native_System_currentTimeMillis); 42 systemNamespace["gc"] = new ScriptFunction("System.gc", &native_System_gc); 43 systemNamespace["getenv"] = new ScriptFunction("System.getenv", &native_System_getenv); 44 interpreter.forceSetGlobal("System", systemNamespace); 45 } 46 47 private: 48 49 ScriptAny native_System_currentTimeMillis(Environment environment, 50 ScriptAny* thisObj, 51 ScriptAny[] args, 52 ref NativeFunctionError nfe) 53 { 54 return ScriptAny(Clock.currStdTime() / 10_000); 55 } 56 57 ScriptAny native_System_gc(Environment env, 58 ScriptAny* thisObj, 59 ScriptAny[] args, 60 ref NativeFunctionError nfe) 61 { 62 GC.collect(); 63 return ScriptAny.UNDEFINED; 64 } 65 66 ScriptAny native_System_getenv(Environment env, 67 ScriptAny* thisObj, 68 ScriptAny[] args, 69 ref NativeFunctionError nfe) 70 { 71 auto aa = environment.toAA(); 72 auto obj = new ScriptObject("env", null); 73 foreach(k,v ; aa) 74 { 75 obj[k] = ScriptAny(v); 76 } 77 return ScriptAny(obj); 78 } 79