0
|
1
|
|
2 /* Digital Mars DMDScript source code.
|
|
3 * Copyright (c) 2000-2002 by Chromium Communications
|
|
4 * D version Copyright (c) 2004-2006 by Digital Mars
|
|
5 * All Rights Reserved
|
|
6 * written by Walter Bright
|
|
7 * www.digitalmars.com
|
|
8 * Use at your own risk. There is no warranty, express or implied.
|
|
9 * License for redistribution is by the GNU General Public License in gpl.txt.
|
|
10 *
|
|
11 * A binary, non-exclusive license for commercial use can be
|
|
12 * purchased from www.digitalmars.com/dscript/buy.html.
|
|
13 *
|
|
14 * DMDScript is implemented in the D Programming Language,
|
|
15 * www.digitalmars.com/d/
|
|
16 *
|
|
17 * For a C++ implementation of DMDScript, including COM support,
|
|
18 * see www.digitalmars.com/dscript/cppscript.html.
|
|
19 */
|
|
20
|
|
21
|
3
|
22 module dmdscript_tango.ddeclaredfunction;
|
0
|
23
|
4
|
24 //import std.stdio;
|
|
25 //import std.c.stdlib;
|
|
26 import tango.stdc.stdlib;
|
|
27 import tango.io.Stdout;
|
0
|
28
|
3
|
29 import dmdscript_tango.script;
|
|
30 import dmdscript_tango.dobject;
|
|
31 import dmdscript_tango.dfunction;
|
|
32 import dmdscript_tango.darguments;
|
|
33 import dmdscript_tango.opcodes;
|
|
34 import dmdscript_tango.ir;
|
|
35 import dmdscript_tango.identifier;
|
|
36 import dmdscript_tango.value;
|
|
37 import dmdscript_tango.functiondefinition;
|
|
38 import dmdscript_tango.text;
|
|
39 import dmdscript_tango.property;
|
0
|
40
|
|
41 /* ========================== DdeclaredFunction ================== */
|
|
42
|
|
43 class DdeclaredFunction : Dfunction
|
|
44 {
|
|
45 FunctionDefinition fd;
|
|
46
|
|
47 this(FunctionDefinition fd)
|
|
48 {
|
|
49 super(fd.parameters.length, Dfunction.getPrototype());
|
|
50 assert(Dfunction.getPrototype());
|
|
51 assert(internal_prototype);
|
|
52 this.fd = fd;
|
|
53
|
|
54 Dobject o;
|
|
55
|
|
56 // ECMA 3 13.2
|
|
57 o = new Dobject(Dobject.getPrototype()); // step 9
|
|
58 Put(TEXT_prototype, o, DontEnum); // step 11
|
|
59 o.Put(TEXT_constructor, this, DontEnum); // step 10
|
|
60 }
|
|
61
|
|
62 void *Call(CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
63 {
|
|
64 // 1. Create activation object per ECMA 10.1.6
|
|
65 // 2. Instantiate function variables as properties of
|
|
66 // activation object
|
|
67 // 3. The 'this' value is the activation object
|
|
68
|
|
69 Dobject actobj; // activation object
|
|
70 Darguments args;
|
|
71 Value[] locals;
|
|
72 uint i;
|
|
73 void *result;
|
|
74
|
|
75 //writefln("DdeclaredFunction.Call() '%s'", toString());
|
|
76 //writefln("this.scopex.length = %d", this.scopex.length);
|
|
77 //writefln("\tinstantiate(this = %x, fd = %x)", cast(uint)cast(void*)this, cast(uint)cast(void*)fd);
|
|
78
|
|
79 // if it's an empty function, just return
|
|
80 if (fd.code[0].opcode == IRret)
|
|
81 {
|
|
82 return null;
|
|
83 }
|
|
84
|
|
85 // Generate the activation object
|
|
86 // ECMA v3 10.1.6
|
|
87 actobj = new Dobject(null);
|
|
88
|
|
89 // Instantiate the parameters
|
|
90 {
|
|
91 uint a = 0;
|
|
92 foreach (Identifier* p; fd.parameters)
|
|
93 {
|
|
94 Value* v = (a < arglist.length) ? &arglist[a++] : &vundefined;
|
|
95 actobj.Put(p.toString(), v, DontDelete);
|
|
96 }
|
|
97 }
|
|
98
|
|
99 // Generate the Arguments Object
|
|
100 // ECMA v3 10.1.8
|
|
101 args = new Darguments(cc.caller, this, actobj, fd.parameters, arglist);
|
|
102
|
|
103 actobj.Put(TEXT_arguments, args, DontDelete);
|
|
104
|
|
105 // The following is not specified by ECMA, but seems to be supported
|
|
106 // by jscript. The url www.grannymail.com has the following code
|
|
107 // which looks broken to me but works in jscript:
|
|
108 //
|
|
109 // function MakeArray() {
|
|
110 // this.length = MakeArray.arguments.length
|
|
111 // for (var i = 0; i < this.length; i++)
|
|
112 // this[i+1] = arguments[i]
|
|
113 // }
|
|
114 // var cardpic = new MakeArray("LL","AP","BA","MB","FH","AW","CW","CV","DZ");
|
|
115 Put(TEXT_arguments, args, DontDelete); // make grannymail bug work
|
|
116
|
|
117 auto scoperootsave = cc.scoperoot;
|
|
118
|
|
119 /* If calling a nested function, need to increase scoperoot
|
|
120 */
|
|
121 if (fd.enclosingFunction == cc.callerf)
|
|
122 { assert(cc.scoperoot < cc.scopex.length);
|
|
123 cc.scoperoot++;
|
|
124 }
|
|
125 else
|
|
126 {
|
|
127 auto df = cast(DdeclaredFunction)cc.caller;
|
|
128 if (df && !fd.isanonymous)
|
|
129 {
|
|
130 version (none)
|
|
131 {
|
|
132 writefln("current nestDepth = %d, calling %d, cc.scopex.length = %d",
|
|
133 df.fd.nestDepth,
|
|
134 fd.nestDepth,
|
|
135 cc.scopex.length);
|
|
136 }
|
|
137 int diff = df.fd.nestDepth - fd.nestDepth;
|
|
138 if (diff > 0)
|
|
139 { if (diff >= cc.scoperoot)
|
4
|
140 Stdout.format("diff {} cc.scoperoot {}", diff, cc.scoperoot);
|
|
141 //writefln("diff %s cc.scoperoot %s", diff, cc.scoperoot);
|
0
|
142 else
|
|
143 cc.scoperoot -= diff;
|
|
144 assert(cc.scoperoot >= 1);
|
|
145 }
|
|
146 }
|
|
147 }
|
|
148
|
|
149 Dobject[] scopesave = cc.scopex;
|
|
150
|
|
151 Dobject[] scopex;
|
|
152 //scopex = cc.scopex[0 .. cc.scoperoot].dup;
|
|
153 scopex = this.scopex.dup;
|
|
154 if (scopex.length == 0)
|
|
155 { this.scopex = cc.scopex[0 .. cc.scoperoot];
|
|
156 scopex = this.scopex.dup;
|
|
157 }
|
|
158 scopex ~= actobj;
|
|
159
|
|
160 fd.instantiate(scopex, actobj, DontDelete);
|
|
161
|
|
162 cc.scopex = scopex;
|
|
163 Dobject variablesave = cc.variable;
|
|
164 cc.variable = actobj;
|
|
165 auto callersave = cc.caller;
|
|
166 cc.caller = this;
|
|
167 auto callerfsave = cc.callerf;
|
|
168 cc.callerf = fd;
|
|
169
|
|
170 Value[] p1;
|
|
171 Value* v;
|
|
172 if (fd.nlocals < 128)
|
|
173 v = cast(Value*) alloca(fd.nlocals * Value.sizeof);
|
|
174 if (v)
|
|
175 locals = v[0 .. fd.nlocals];
|
|
176 else
|
|
177 {
|
|
178 p1 = new Value[fd.nlocals];
|
|
179 locals = p1;
|
|
180 }
|
|
181
|
|
182 result = IR.call(cc, othis, fd.code, ret, locals.ptr);
|
|
183
|
|
184 delete p1;
|
|
185
|
|
186 cc.callerf = callerfsave;
|
|
187 cc.caller = callersave;
|
|
188 cc.variable = variablesave;
|
|
189 cc.scopex = scopesave;
|
|
190 cc.scoperoot = scoperootsave;
|
|
191
|
|
192 // Remove the arguments object
|
|
193 //Value* v;
|
|
194 //v=Get(TEXT_arguments);
|
|
195 //writef("1v = %x, %s, v.object = %x\n", v, v.getType(), v.object);
|
|
196 Put(TEXT_arguments, &vundefined, 0);
|
|
197 //actobj.Put(TEXT_arguments, &vundefined, 0);
|
|
198
|
|
199 version (none)
|
|
200 {
|
|
201 writef("args = %x, actobj = %x\n", args, actobj);
|
|
202 v=Get(TEXT_arguments);
|
|
203 writef("2v = %x, %s, v.object = %x\n", v, v.getType(), v.object);
|
|
204 v.object = null;
|
|
205
|
|
206 {
|
|
207 uint *p = cast(uint *)0x40a49a80;
|
|
208 uint i;
|
|
209 for (i = 0; i < 16; i++)
|
|
210 {
|
|
211 writef("p[%x] = %x\n", &p[i], p[i]);
|
|
212 }
|
|
213 }
|
|
214 }
|
|
215
|
|
216 return result;
|
|
217 }
|
|
218
|
|
219 void *Construct(CallContext *cc, Value *ret, Value[] arglist)
|
|
220 {
|
|
221 // ECMA 3 13.2.2
|
|
222 Dobject othis;
|
|
223 Dobject proto;
|
|
224 Value* v;
|
|
225 void *result;
|
|
226
|
|
227 v = Get(TEXT_prototype);
|
|
228 if (v.isPrimitive())
|
|
229 proto = Dobject.getPrototype();
|
|
230 else
|
|
231 proto = v.toObject();
|
|
232 othis = new Dobject(proto);
|
|
233 result = Call(cc, othis, ret, arglist);
|
|
234 if (!result)
|
|
235 {
|
|
236 if (ret.isPrimitive())
|
|
237 ret.putVobject(othis);
|
|
238 }
|
|
239 return result;
|
|
240 }
|
|
241
|
|
242 d_string toString()
|
|
243 {
|
|
244 d_string s;
|
|
245
|
|
246 //writef("DdeclaredFunction.toString()\n");
|
|
247 fd.toBuffer(s);
|
|
248 return s;
|
|
249 }
|
|
250 }
|
|
251
|
|
252
|