1 /**
2 This module implements functions for the "console" namespace in the scripting language.
3 See https://pillager86.github.io/dmildew/console.html for more details.
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.console;
22 
23 import mildew.environment;
24 import mildew.interpreter;
25 import mildew.types;
26 
27 /** 
28  * Initializes the console library. This is called by Interpreter.initializeStdlib. The console
29  * functions are stored in the "console" global variable and are accessed such as "console.log".
30  * Documentation for these functions can be found at https://pillager86.github.io/dmildew/console.html
31  * Params:
32  *  interpreter = The Interpreter object for which to load the namespace and functions.
33  */
34 public void initializeConsoleLibrary(Interpreter interpreter)
35 {
36     auto consoleNamespace = new ScriptObject("Console", null);
37     consoleNamespace["log"] = ScriptAny(new ScriptFunction("console.log", &native_console_log));
38     consoleNamespace["put"] = ScriptAny(new ScriptFunction("console.put", &native_console_put));
39     consoleNamespace["error"] = ScriptAny(new ScriptFunction("console.error", &native_console_error));
40     interpreter.forceSetGlobal("console", consoleNamespace, true);
41 }
42 
43 private ScriptAny native_console_log(Environment environment,
44         ScriptAny* thisObj,
45         ScriptAny[] args,
46         ref NativeFunctionError nfe)
47 {
48     import std.stdio: write, writeln;
49     if(args.length > 0)
50         write(args[0].toString());
51     if(args.length > 1)
52         foreach(arg ; args[1..$])
53             write(" " ~ arg.toString);
54     writeln();
55     return ScriptAny.UNDEFINED;
56 }
57 
58 private ScriptAny native_console_put(Environment environment,
59         ScriptAny* thisObj,
60         ScriptAny[] args,
61         ref NativeFunctionError nfe)
62 {
63 import std.stdio: write, writeln;
64     if(args.length > 0)
65         write(args[0].toString());
66     if(args.length > 1)
67         foreach(arg ; args[1..$])
68             write(" " ~ arg.toString);
69     return ScriptAny.UNDEFINED;
70 }
71 
72 private ScriptAny native_console_error(Environment environment,
73         ScriptAny* thisObj,
74         ScriptAny[] args,
75         ref NativeFunctionError nfe)
76 {
77     import std.stdio: stderr;
78     foreach(arg ; args)
79         stderr.write(arg.toString ~ " ");
80     stderr.writeln();
81     return ScriptAny.UNDEFINED;
82 }