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