1 /**
2 Contains functions to mixin to simplify code reuse. For these to work, the parameters of a native function
3 must be called context, thisObj, args, and nfe. If placed in if-statement they MUST be surrounded by
4 braces.
5 
6 ────────────────────────────────────────────────────────────────────────────────
7 
8 Copyright (C) 2021 pillager86.rf.gd
9 
10 This program is free software: you can redistribute it and/or modify it under 
11 the terms of the GNU General Public License as published by the Free Software 
12 Foundation, either version 3 of the License, or (at your option) any later 
13 version.
14 
15 This program is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17 PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License along with 
20 this program.  If not, see <https://www.gnu.org/licenses/>.
21  */
22 module mildew.binder;
23 
24 import std.format: format;
25 import std.conv: to;
26 
27 /**
28  * Check for a minimum number of arguments. This must be used if using TO_ARG
29  */
30 string CHECK_MINIMUM_ARGS(int num)()
31 {
32     return format(q{
33         if(args.length < %1$s)
34         {
35             nfe = NativeFunctionError.WRONG_NUMBER_OF_ARGS;
36             return ScriptAny.UNDEFINED;
37         }
38     }, num.to!string);
39 }
40 
41 /**
42  * Shorthand for validating a this object as a native type
43  */
44 string CHECK_THIS_NATIVE_OBJECT(string varName, alias dclass)()
45 {
46     return format(q{
47         auto %1$s = thisObj.toNativeObject!%2$s;
48         if(%1$s is null)
49         {
50             nfe = NativeFunctionError.WRONG_TYPE_OF_ARG;
51             return ScriptAny.UNDEFINED;
52         }
53     }, varName, dclass.stringof);
54 }
55 
56 /**
57  * Uses .init value of a variable if argument doesn't exist. Arguments length MUST be checked first
58  */
59 string TO_ARG(string varName, int index, alias type)()
60 {
61     return format(q{
62         auto %1$s = args[%2$s].toValue!%3$s;
63     }, varName, index.to!string, type.stringof);
64 }
65 
66 /**
67  * Get an optional argument and default value
68  */
69 string TO_ARG_OPT(string varName, int index, alias defaultValue, alias type)()
70 {
71     return format(q{
72         auto %1$s = %3$s;
73         if(%2$s < args.length)
74         {
75             %1$s = args[%2$s].toValue!%4$s;
76         }
77     }, varName, index.to!string, defaultValue.stringof, type.stringof);
78 }
79 
80 /**
81  * Shorthand for extracting an argument without validating its type
82  */
83 string TO_ARG_CHECK_INDEX(string varName, int index, alias type)()
84 {   
85     return format(q{
86         if(args.length < %2$s + 1)
87         {
88             nfe = NativeFunctionError.WRONG_NUMBER_OF_ARGS;
89             return ScriptAny.UNDEFINED;
90         }
91         auto %1$s = args[%2$s].toValue!%3$s;
92     }, varName, index.to!string, type.stringof);
93 }
94