1 /* Digital Mars DMDScript source code.
2  * Copyright (c) 2000-2002 by Chromium Communications
3  * D version Copyright (c) 2004-2010 by Digital Mars
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  * written by Walter Bright
7  * http://www.digitalmars.com
8  *
9  * D2 port by Dmitry Olshansky 
10  *
11  * DMDScript is implemented in the D Programming Language,
12  * http://www.digitalmars.com/d/
13  *
14  * For a C++ implementation of DMDScript, including COM support, see
15  * http://www.digitalmars.com/dscript/cppscript.html
16  */
17 
18 module dmdscript.dboolean;
19 
20 import dmdscript.script;
21 import dmdscript.dobject;
22 import dmdscript.value;
23 import dmdscript.threadcontext;
24 import dmdscript.dfunction;
25 import dmdscript.text;
26 import dmdscript.property;
27 import dmdscript.errmsgs;
28 import dmdscript.dnative;
29 
30 /* ===================== Dboolean_constructor ==================== */
31 
32 class DbooleanConstructor : Dfunction
33 {
34     this()
35     {
36         super(1, Dfunction_prototype);
37         name = "Boolean";
38     }
39 
40     override void *Construct(CallContext *cc, Value *ret, Value[] arglist)
41     {
42         // ECMA 15.6.2
43         d_boolean b;
44         Dobject o;
45 
46         b = (arglist.length) ? arglist[0].toBoolean() : false;
47         o = new Dboolean(b);
48         ret.putVobject(o);
49         return null;
50     }
51 
52     override void *Call(CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
53     {
54         // ECMA 15.6.1
55         d_boolean b;
56 
57         b = (arglist.length) ? arglist[0].toBoolean() : false;
58         ret.putVboolean(b);
59         return null;
60     }
61 }
62 
63 
64 /* ===================== Dboolean_prototype_toString =============== */
65 
66 void* Dboolean_prototype_toString(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
67 {
68     // othis must be a Boolean
69     if(!othis.isClass(TEXT_Boolean))
70     {
71         ErrInfo errinfo;
72 
73         ret.putVundefined();
74         return Dobject.RuntimeError(&errinfo, errmsgtbl[ERR_FUNCTION_WANTS_BOOL],
75                                     TEXT_toString,
76                                     othis.classname);
77     }
78     else
79     {
80         Value *v;
81 
82         v = &(cast(Dboolean)othis).value;
83         ret.putVstring(v.toString());
84     }
85     return null;
86 }
87 
88 /* ===================== Dboolean_prototype_valueOf =============== */
89 
90 void* Dboolean_prototype_valueOf(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
91 {
92     //FuncLog f("Boolean.prototype.valueOf()");
93     //logflag = 1;
94 
95     // othis must be a Boolean
96     if(!othis.isClass(TEXT_Boolean))
97     {
98         ErrInfo errinfo;
99 
100         ret.putVundefined();
101         return Dobject.RuntimeError(&errinfo, errmsgtbl[ERR_FUNCTION_WANTS_BOOL],
102                                     TEXT_valueOf,
103                                     othis.classname);
104     }
105     else
106     {
107         Value *v;
108 
109         v = &(cast(Dboolean)othis).value;
110         Value.copy(ret, v);
111     }
112     return null;
113 }
114 
115 /* ===================== Dboolean_prototype ==================== */
116 
117 class DbooleanPrototype : Dboolean
118 {
119     this()
120     {
121         super(Dobject_prototype);
122         //Dobject f = Dfunction_prototype;
123 
124         Put(TEXT_constructor, Dboolean_constructor, DontEnum);
125 
126         static enum NativeFunctionData[] nfd =
127         [
128             { TEXT_toString, &Dboolean_prototype_toString, 0 },
129             { TEXT_valueOf, &Dboolean_prototype_valueOf, 0 },
130         ];
131 
132         DnativeFunction.initialize(this, nfd, DontEnum);
133     }
134 }
135 
136 
137 /* ===================== Dboolean ==================== */
138 
139 class Dboolean : Dobject
140 {
141     this(d_boolean b)
142     {
143         super(Dboolean.getPrototype());
144         value.putVboolean(b);
145         classname = TEXT_Boolean;
146     }
147 
148     this(Dobject prototype)
149     {
150         super(prototype);
151         value.putVboolean(false);
152         classname = TEXT_Boolean;
153     }
154 
155     static Dfunction getConstructor()
156     {
157         return Dboolean_constructor;
158     }
159 
160     static Dobject getPrototype()
161     {
162         return Dboolean_prototype;
163     }
164 
165     static void initialize()
166     {
167         Dboolean_constructor = new DbooleanConstructor();
168         Dboolean_prototype = new DbooleanPrototype();
169 
170         Dboolean_constructor.Put(TEXT_prototype, Dboolean_prototype, DontEnum | DontDelete | ReadOnly);
171     }
172 }
173