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 
19 module dmdscript.protoerror;
20 
21 import dmdscript.script;
22 import dmdscript.dobject;
23 import dmdscript.value;
24 import dmdscript.threadcontext;
25 import dmdscript.text;
26 import dmdscript.dfunction;
27 import dmdscript.property;
28 
29 /* ===================== D0_constructor ==================== */
30 
31 class D0_constructor : Dfunction
32 {
33     d_string text_d1;
34     Dobject function(d_string) newD0;
35 
36     this(d_string text_d1, Dobject function(d_string) newD0)
37     {
38         super(1, Dfunction_prototype);
39         this.text_d1 = text_d1;
40         this.newD0 = newD0;
41     }
42 
43     override void *Construct(CallContext *cc, Value *ret, Value[] arglist)
44     {
45         // ECMA 15.11.7.2
46         Value* m;
47         Dobject o;
48         d_string s;
49 
50         m = (arglist.length) ? &arglist[0] : &vundefined;
51         // ECMA doesn't say what we do if m is undefined
52         if(m.isUndefined())
53             s = text_d1;
54         else
55             s = m.toString();
56         o = (*newD0)(s);
57         ret.putVobject(o);
58         return null;
59     }
60 
61     override void *Call(CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
62     {
63         // ECMA v3 15.11.7.1
64         return Construct(cc, ret, arglist);
65     }
66 }
67 
68 
69 template proto(alias TEXT_D1)
70 {
71     /* ===================== D0_prototype ==================== */
72 
73     class D0_prototype : D0
74     {
75         this()
76         {
77             super(Derror_prototype);
78 
79             d_string s;
80 
81             Put(TEXT_constructor, ctorTable[TEXT_D1], DontEnum);
82             Put(TEXT_name, TEXT_D1, 0);
83             s = TEXT_D1 ~ ".prototype.message";
84             Put(TEXT_message, s, 0);
85             Put(TEXT_description, s, 0);
86             Put(TEXT_number, cast(d_number)0, 0);
87         }
88     }
89 
90     /* ===================== D0 ==================== */
91 
92     class D0 : Dobject
93     {
94         ErrInfo errinfo;
95 
96         this(Dobject prototype)
97         {
98             super(prototype);
99             classname = TEXT_Error;
100         }
101 
102         this(d_string m)
103         {
104             this(D0.getPrototype());
105             Put(TEXT_message, m, 0);
106             Put(TEXT_description, m, 0);
107             Put(TEXT_number, cast(d_number)0, 0);
108             errinfo.message = m;
109         }
110 
111         this(ErrInfo * perrinfo)
112         {
113             this(perrinfo.message);
114             errinfo = *perrinfo;
115             Put(TEXT_number, cast(d_number)perrinfo.code, 0);
116         }
117 
118         override void getErrInfo(ErrInfo *perrinfo, int linnum)
119         {
120             if(linnum && errinfo.linnum == 0)
121                 errinfo.linnum = linnum;
122             if(perrinfo)
123                 *perrinfo = errinfo;
124             //writefln("getErrInfo(linnum = %d), errinfo.linnum = %d", linnum, errinfo.linnum);
125         }
126 
127         static Dfunction getConstructor()
128         {
129             return ctorTable[TEXT_D1];
130         }
131 
132         static Dobject getPrototype()
133         {
134             return protoTable[TEXT_D1];
135         }
136 
137         static Dobject newD0(d_string s)
138         {
139             return new D0(s);
140         }
141 
142         static void init()
143         {
144             Dfunction constructor = new D0_constructor(TEXT_D1, &newD0);
145             ctorTable[TEXT_D1] = constructor;
146 
147             Dobject prototype = new D0_prototype();
148             protoTable[TEXT_D1] = prototype;
149 
150             constructor.Put(TEXT_prototype, prototype, DontEnum | DontDelete | ReadOnly);
151         }
152     }
153 }
154 
155 alias proto!(TEXT_SyntaxError) syntaxerror;
156 alias proto!(TEXT_EvalError) evalerror;
157 alias proto!(TEXT_ReferenceError) referenceerror;
158 alias proto!(TEXT_RangeError) rangeerror;
159 alias proto!(TEXT_TypeError) typeerror;
160 alias proto!(TEXT_URIError) urierror;
161 
162 /**********************************
163  * Register initializer for each class.
164  */
165 
166 static this()
167 {
168     threadInitTable ~= &syntaxerror.D0.init;
169     threadInitTable ~= &evalerror.D0.init;
170     threadInitTable ~= &referenceerror.D0.init;
171     threadInitTable ~= &rangeerror.D0.init;
172     threadInitTable ~= &typeerror.D0.init;
173     threadInitTable ~= &urierror.D0.init;
174 }