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.dglobal;
|
0
|
23
|
|
24 import std.uri;
|
|
25 import std.c.stdlib;
|
|
26 import std.c.string;
|
|
27 import std.stdio;
|
|
28 import std.math;
|
|
29
|
3
|
30 import dmdscript_tango.script;
|
|
31 import dmdscript_tango.protoerror;
|
|
32 import dmdscript_tango.parse;
|
|
33 import dmdscript_tango.text;
|
|
34 import dmdscript_tango.dobject;
|
|
35 import dmdscript_tango.value;
|
|
36 import dmdscript_tango.statement;
|
|
37 import dmdscript_tango.threadcontext;
|
|
38 import dmdscript_tango.functiondefinition;
|
|
39 import dmdscript_tango.scopex;
|
|
40 import dmdscript_tango.opcodes;
|
|
41 import dmdscript_tango.property;
|
0
|
42
|
3
|
43 import dmdscript_tango.dstring;
|
|
44 import dmdscript_tango.darray;
|
|
45 import dmdscript_tango.dregexp;
|
|
46 import dmdscript_tango.dnumber;
|
|
47 import dmdscript_tango.dboolean;
|
|
48 import dmdscript_tango.dfunction;
|
|
49 import dmdscript_tango.dnative;
|
0
|
50
|
|
51 d_string arg0string(Value[] arglist)
|
|
52 {
|
|
53 Value* v = arglist.length ? &arglist[0] : &vundefined;
|
|
54 return v.toString();
|
|
55 }
|
|
56
|
|
57 /* ====================== Dglobal_eval ================ */
|
|
58
|
|
59 void* Dglobal_eval(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
60 {
|
|
61 // ECMA 15.1.2.1
|
|
62 Value* v;
|
|
63 d_string s;
|
|
64 FunctionDefinition fd;
|
|
65 ErrInfo errinfo;
|
|
66
|
|
67 //FuncLog funclog(L"Global.eval()");
|
|
68
|
|
69 v = arglist.length ? &arglist[0] : &vundefined;
|
|
70 if (v.getType() != TypeString)
|
|
71 {
|
|
72 Value.copy(ret, v);
|
|
73 return null;
|
|
74 }
|
|
75 s = v.toString();
|
|
76 //writef("eval('%ls')\n", s);
|
|
77
|
|
78 // Parse program
|
|
79 TopStatement[] topstatements;
|
|
80 Parser p = new Parser("eval", s, 0);
|
|
81 if (p.parseProgram(topstatements, &errinfo))
|
|
82 goto Lsyntaxerror;
|
|
83
|
|
84 // Analyze, generate code
|
|
85 fd = new FunctionDefinition(topstatements);
|
|
86 fd.iseval = 1;
|
|
87 {
|
|
88 Scope sc;
|
|
89 sc.ctor(fd);
|
|
90 sc.src = s;
|
|
91 fd.semantic(&sc);
|
|
92 errinfo = sc.errinfo;
|
|
93 sc.dtor();
|
|
94 }
|
|
95 if (errinfo.message)
|
|
96 goto Lsyntaxerror;
|
|
97 fd.toIR(null);
|
|
98
|
|
99 // Execute code
|
|
100 Value[] locals;
|
|
101 Value[] p1 = null;
|
|
102
|
|
103 Value* v1 = null;
|
|
104 if (fd.nlocals < 128)
|
|
105 v1 = cast(Value*) alloca(fd.nlocals * Value.sizeof);
|
|
106 if (v1)
|
|
107 locals = v1[0 .. fd.nlocals];
|
|
108 else
|
|
109 {
|
|
110 p1 = new Value[fd.nlocals];
|
|
111 locals = p1;
|
|
112 }
|
|
113
|
|
114 void *result;
|
|
115 version (none)
|
|
116 {
|
|
117 Array scopex;
|
|
118 scopex.reserve(cc.scoperoot + fd.withdepth + 2);
|
|
119 for (uint u = 0; u < cc.scoperoot; u++)
|
|
120 scopex.push(cc.scopex.data[u]);
|
|
121
|
|
122 Array *scopesave = cc.scopex;
|
|
123 cc.scopex = &scopex;
|
|
124 Dobject variablesave = cc.variable;
|
|
125 cc.variable = cc.global;
|
|
126
|
|
127 fd.instantiate(cc.variable, 0);
|
|
128
|
|
129 // The this value is the same as the this value of the
|
|
130 // calling context.
|
|
131 result = IR.call(cc, othis, fd.code, ret, locals);
|
|
132
|
|
133 delete p1;
|
|
134 cc.variable = variablesave;
|
|
135 cc.scopex = scopesave;
|
|
136 return result;
|
|
137 }
|
|
138 else
|
|
139 {
|
|
140 // The scope chain is initialized to contain the same objects,
|
|
141 // in the same order, as the calling context's scope chain.
|
|
142 // This includes objects added to the calling context's
|
|
143 // scope chain by WithStatement.
|
|
144 // cc.scopex.reserve(fd.withdepth);
|
|
145
|
|
146 // Variable instantiation is performed using the calling
|
|
147 // context's variable object and using empty
|
|
148 // property attributes
|
|
149 fd.instantiate(cc.scopex, cc.variable, 0);
|
|
150
|
|
151 // The this value is the same as the this value of the
|
|
152 // calling context.
|
|
153 assert(cc.callerothis);
|
|
154 result = IR.call(cc, cc.callerothis, fd.code, ret, locals.ptr);
|
|
155 if (p1)
|
|
156 delete p1;
|
|
157 fd = null;
|
|
158 //if (result) writef("result = '%s'\n", d_string_ptr(((Value* )result).toString()));
|
|
159 return result;
|
|
160 }
|
|
161
|
|
162 Lsyntaxerror:
|
|
163 Dobject o;
|
|
164
|
|
165 // For eval()'s, use location of caller, not the string
|
|
166 errinfo.linnum = 0;
|
|
167
|
|
168 ret.putVundefined();
|
|
169 o = new syntaxerror.D0(&errinfo);
|
|
170 Value* v2 = new Value;
|
|
171 v2.putVobject(o);
|
|
172 return v2;
|
|
173 }
|
|
174
|
|
175 /* ====================== Dglobal_parseInt ================ */
|
|
176
|
|
177 void* Dglobal_parseInt(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
178 {
|
|
179 // ECMA 15.1.2.2
|
|
180 Value* v2;
|
|
181 tchar *s;
|
|
182 tchar *z;
|
|
183 d_int32 radix;
|
|
184 int sign = 1;
|
|
185 d_number number;
|
|
186 uint i;
|
|
187 d_string string;
|
|
188
|
|
189 string = arg0string(arglist);
|
|
190
|
|
191 //writefln("Dglobal_parseInt('%s')", string);
|
|
192
|
|
193 while (i < string.length)
|
|
194 { uint idx = i;
|
|
195 dchar c = std.utf.decode(string, idx);
|
|
196 if (!isStrWhiteSpaceChar(c))
|
|
197 break;
|
|
198 i = idx;
|
|
199 }
|
|
200 s = string.ptr + i;
|
|
201 i = string.length - i;
|
|
202
|
|
203 if (i)
|
|
204 {
|
|
205 if (*s == '-')
|
|
206 { sign = -1;
|
|
207 s++;
|
|
208 i--;
|
|
209 }
|
|
210 else if (*s == '+')
|
|
211 { s++;
|
|
212 i--;
|
|
213 }
|
|
214 }
|
|
215
|
|
216 radix = 0;
|
|
217 if (arglist.length >= 2)
|
|
218 {
|
|
219 v2 = &arglist[1];
|
|
220 radix = v2.toInt32();
|
|
221 }
|
|
222
|
|
223 if (radix)
|
|
224 {
|
|
225 if (radix < 2 || radix > 36)
|
|
226 {
|
|
227 number = d_number.nan;
|
|
228 goto Lret;
|
|
229 }
|
|
230 if (radix == 16 && i >= 2 && *s == '0' &&
|
|
231 (s[1] == 'x' || s[1] == 'X'))
|
|
232 {
|
|
233 s += 2;
|
|
234 i -= 2;
|
|
235 }
|
|
236 }
|
|
237 else if (i >= 1 && *s != '0')
|
|
238 {
|
|
239 radix = 10;
|
|
240 }
|
|
241 else if (i >= 2 && (s[1] == 'x' || s[1] == 'X'))
|
|
242 {
|
|
243 radix = 16;
|
|
244 s += 2;
|
|
245 i -= 2;
|
|
246 }
|
|
247 else
|
|
248 radix = 8;
|
|
249
|
|
250 number = 0;
|
|
251 for (z = s; i; z++, i--)
|
|
252 { d_int32 n;
|
|
253 tchar c;
|
|
254
|
|
255 c = *z;
|
|
256 if ('0' <= c && c <= '9')
|
|
257 n = c - '0';
|
|
258 else if ('A' <= c && c <= 'Z')
|
|
259 n = c - 'A' + 10;
|
|
260 else if ('a' <= c && c <= 'z')
|
|
261 n = c - 'a' + 10;
|
|
262 else
|
|
263 break;
|
|
264 if (radix <= n)
|
|
265 break;
|
|
266 number = number * radix + n;
|
|
267 }
|
|
268 if (z == s)
|
|
269 {
|
|
270 number = d_number.nan;
|
|
271 goto Lret;
|
|
272 }
|
|
273 if (sign < 0)
|
|
274 number = -number;
|
|
275
|
|
276 version (none) // ECMA says to silently ignore trailing characters
|
|
277 {
|
|
278 while (z - &string[0] < string.length)
|
|
279 {
|
|
280 if (!isStrWhiteSpaceChar(*z))
|
|
281 {
|
|
282 number = d_number.nan;
|
|
283 goto Lret;
|
|
284 }
|
|
285 z++;
|
|
286 }
|
|
287 }
|
|
288
|
|
289 Lret:
|
|
290 ret.putVnumber(number);
|
|
291 return null;
|
|
292 }
|
|
293
|
|
294 /* ====================== Dglobal_parseFloat ================ */
|
|
295
|
|
296 void* Dglobal_parseFloat(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
297 {
|
|
298 // ECMA 15.1.2.3
|
|
299 d_number n;
|
|
300 size_t endidx;
|
|
301
|
|
302 d_string string = arg0string(arglist);
|
|
303 n = StringNumericLiteral(string, endidx, 1);
|
|
304
|
|
305 ret.putVnumber(n);
|
|
306 return null;
|
|
307 }
|
|
308
|
|
309 /* ====================== Dglobal_escape ================ */
|
|
310
|
|
311 int ISURIALNUM(dchar c)
|
|
312 {
|
|
313 return (c >= 'a' && c <= 'z') ||
|
|
314 (c >= 'A' && c <= 'Z') ||
|
|
315 (c >= '0' && c <= '9');
|
|
316 }
|
|
317
|
|
318 tchar TOHEX[16+1] = "0123456789ABCDEF";
|
|
319
|
|
320 void* Dglobal_escape(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
321 {
|
|
322 // ECMA 15.1.2.4
|
|
323 d_string s;
|
|
324 d_string R;
|
|
325 uint escapes;
|
|
326 uint unicodes;
|
|
327 size_t slen;
|
|
328
|
|
329 s = arg0string(arglist);
|
|
330 escapes = 0;
|
|
331 unicodes = 0;
|
|
332 foreach (dchar c; s)
|
|
333 {
|
|
334 slen++;
|
|
335 if (c >= 0x100)
|
|
336 unicodes++;
|
|
337 else
|
|
338 if (c == 0 || c >= 0x80 || (!ISURIALNUM(c) && std.string.find("*@-_+./", c) == -1))
|
|
339 escapes++;
|
|
340 }
|
|
341 if ((escapes + unicodes) == 0)
|
|
342 {
|
|
343 R = s;
|
|
344 }
|
|
345 else
|
|
346 {
|
|
347 //writefln("s.length = %d, escapes = %d, unicodes = %d", s.length, escapes, unicodes);
|
|
348 R = new tchar[slen + escapes * 2 + unicodes * 5];
|
|
349 tchar* r = R.ptr;
|
|
350 foreach (dchar c; s)
|
|
351 {
|
|
352 if (c >= 0x100)
|
|
353 {
|
|
354 r[0] = '%';
|
|
355 r[1] = 'u';
|
|
356 r[2] = TOHEX[(c >> 12) & 15];
|
|
357 r[3] = TOHEX[(c >> 8) & 15];
|
|
358 r[4] = TOHEX[(c >> 4) & 15];
|
|
359 r[5] = TOHEX[c & 15];
|
|
360 r += 6;
|
|
361 }
|
|
362 else if (c == 0 || c >= 0x80 || (!ISURIALNUM(c) && std.string.find("*@-_+./", c) == -1))
|
|
363 {
|
|
364 r[0] = '%';
|
|
365 r[1] = TOHEX[c >> 4];
|
|
366 r[2] = TOHEX[c & 15];
|
|
367 r += 3;
|
|
368 }
|
|
369 else
|
|
370 {
|
|
371 r[0] = cast(tchar)c;
|
|
372 r++;
|
|
373 }
|
|
374 }
|
|
375 assert(r - R.ptr == R.length);
|
|
376 }
|
|
377 ret.putVstring(R);
|
|
378 return null;
|
|
379 }
|
|
380
|
|
381 /* ====================== Dglobal_unescape ================ */
|
|
382
|
|
383 void* Dglobal_unescape(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
384 {
|
|
385 // ECMA 15.1.2.5
|
|
386 d_string s;
|
|
387 d_string R;
|
|
388
|
|
389 s = arg0string(arglist);
|
|
390 //writefln("Dglobal.unescape(s = '%s')", s);
|
|
391 for (size_t k = 0; k < s.length; k++)
|
|
392 { tchar c = s[k];
|
|
393
|
|
394 if (c == '%')
|
|
395 {
|
|
396 if (k + 6 <= s.length && s[k + 1] == 'u')
|
|
397 { uint u;
|
|
398
|
|
399 u = 0;
|
|
400 for (int i = 2; ; i++)
|
|
401 { uint x;
|
|
402
|
|
403 if (i == 6)
|
|
404 {
|
|
405 std.utf.encode(R, cast(dchar)u);
|
|
406 k += 5;
|
|
407 goto L1;
|
|
408 }
|
|
409 x = s[k + i];
|
|
410 if ('0' <= x && x <= '9')
|
|
411 x = x - '0';
|
|
412 else if ('A' <= x && x <= 'F')
|
|
413 x = x - 'A' + 10;
|
|
414 else if ('a' <= x && x <= 'f')
|
|
415 x = x - 'a' + 10;
|
|
416 else
|
|
417 break;
|
|
418 u = (u << 4) + x;
|
|
419 }
|
|
420 }
|
|
421 else if (k + 3 <= s.length)
|
|
422 { uint u;
|
|
423
|
|
424 u = 0;
|
|
425 for (int i = 1; ; i++)
|
|
426 { uint x;
|
|
427
|
|
428 if (i == 3)
|
|
429 {
|
|
430 std.utf.encode(R, cast(dchar)u);
|
|
431 k += 2;
|
|
432 goto L1;
|
|
433 }
|
|
434 x = s[k + i];
|
|
435 if ('0' <= x && x <= '9')
|
|
436 x = x - '0';
|
|
437 else if ('A' <= x && x <= 'F')
|
|
438 x = x - 'A' + 10;
|
|
439 else if ('a' <= x && x <= 'f')
|
|
440 x = x - 'a' + 10;
|
|
441 else
|
|
442 break;
|
|
443 u = (u << 4) + x;
|
|
444 }
|
|
445 }
|
|
446 }
|
|
447 R ~= c;
|
|
448 L1:
|
|
449 ;
|
|
450 }
|
|
451
|
|
452 ret.putVstring(R);
|
|
453 return null;
|
|
454 }
|
|
455
|
|
456 /* ====================== Dglobal_isNaN ================ */
|
|
457
|
|
458 void* Dglobal_isNaN(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
459 {
|
|
460 // ECMA 15.1.2.6
|
|
461 Value* v;
|
|
462 d_number n;
|
|
463 d_boolean b;
|
|
464
|
|
465 if (arglist.length)
|
|
466 v = &arglist[0];
|
|
467 else
|
|
468 v = &vundefined;
|
|
469 n = v.toNumber();
|
|
470 b = isnan(n) ? true : false;
|
|
471 ret.putVboolean(b);
|
|
472 return null;
|
|
473 }
|
|
474
|
|
475 /* ====================== Dglobal_isFinite ================ */
|
|
476
|
|
477 void* Dglobal_isFinite(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
478 {
|
|
479 // ECMA 15.1.2.7
|
|
480 Value* v;
|
|
481 d_number n;
|
|
482 d_boolean b;
|
|
483
|
|
484 if (arglist.length)
|
|
485 v = &arglist[0];
|
|
486 else
|
|
487 v = &vundefined;
|
|
488 n = v.toNumber();
|
|
489 b = isfinite(n) ? true : false;
|
|
490 ret.putVboolean(b);
|
|
491 return null;
|
|
492 }
|
|
493
|
|
494 /* ====================== Dglobal_ URI Functions ================ */
|
|
495
|
|
496 void* URI_error(char[] s)
|
|
497 {
|
|
498 Dobject o = new urierror.D0(s ~ "() failure");
|
|
499 Value* v = new Value;
|
|
500 v.putVobject(o);
|
|
501 return v;
|
|
502 }
|
|
503
|
|
504 void* Dglobal_decodeURI(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
505 {
|
|
506 // ECMA v3 15.1.3.1
|
|
507 d_string s;
|
|
508
|
|
509 s = arg0string(arglist);
|
|
510 try
|
|
511 {
|
|
512 s = std.uri.decode(s);
|
|
513 }
|
|
514 catch (URIerror u)
|
|
515 {
|
|
516 ret.putVundefined();
|
|
517 return URI_error(TEXT_decodeURI);
|
|
518 }
|
|
519 ret.putVstring(s);
|
|
520 return null;
|
|
521 }
|
|
522
|
|
523 void* Dglobal_decodeURIComponent(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
524 {
|
|
525 // ECMA v3 15.1.3.2
|
|
526 d_string s;
|
|
527
|
|
528 s = arg0string(arglist);
|
|
529 try
|
|
530 {
|
|
531 s = std.uri.decodeComponent(s);
|
|
532 }
|
|
533 catch (URIerror u)
|
|
534 {
|
|
535 ret.putVundefined();
|
|
536 return URI_error(TEXT_decodeURIComponent);
|
|
537 }
|
|
538 ret.putVstring(s);
|
|
539 return null;
|
|
540 }
|
|
541
|
|
542 void* Dglobal_encodeURI(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
543 {
|
|
544 // ECMA v3 15.1.3.3
|
|
545 d_string s;
|
|
546
|
|
547 s = arg0string(arglist);
|
|
548 try
|
|
549 {
|
|
550 s = std.uri.encode(s);
|
|
551 }
|
|
552 catch (URIerror u)
|
|
553 {
|
|
554 ret.putVundefined();
|
|
555 return URI_error(TEXT_encodeURI);
|
|
556 }
|
|
557 ret.putVstring(s);
|
|
558 return null;
|
|
559 }
|
|
560
|
|
561 void* Dglobal_encodeURIComponent(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
562 {
|
|
563 // ECMA v3 15.1.3.4
|
|
564 d_string s;
|
|
565
|
|
566 s = arg0string(arglist);
|
|
567 try
|
|
568 {
|
|
569 s = std.uri.encodeComponent(s);
|
|
570 }
|
|
571 catch (URIerror u)
|
|
572 {
|
|
573 ret.putVundefined();
|
|
574 return URI_error(TEXT_encodeURIComponent);
|
|
575 }
|
|
576 ret.putVstring(s);
|
|
577 return null;
|
|
578 }
|
|
579
|
|
580 /* ====================== Dglobal_print ================ */
|
|
581
|
|
582 static void dglobal_print(CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
583 {
|
|
584 // Our own extension
|
|
585 if (arglist.length)
|
|
586 { uint i;
|
|
587
|
|
588 for (i = 0; i < arglist.length; i++)
|
|
589 {
|
|
590 d_string s = arglist[i].toString();
|
|
591
|
|
592 writef("%s", s);
|
|
593 }
|
|
594 }
|
|
595
|
|
596 ret.putVundefined();
|
|
597 }
|
|
598
|
|
599 void* Dglobal_print(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
600 {
|
|
601 // Our own extension
|
|
602 dglobal_print(cc, othis, ret, arglist);
|
|
603 return null;
|
|
604 }
|
|
605
|
|
606 /* ====================== Dglobal_println ================ */
|
|
607
|
|
608 void* Dglobal_println(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
609 {
|
|
610 // Our own extension
|
|
611 dglobal_print(cc, othis, ret, arglist);
|
|
612 writef("\n");
|
|
613 return null;
|
|
614 }
|
|
615
|
|
616 /* ====================== Dglobal_readln ================ */
|
|
617
|
|
618 void* Dglobal_readln(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
619 {
|
|
620 // Our own extension
|
|
621 dchar c;
|
|
622 tchar[] s;
|
|
623
|
|
624 for (;;)
|
|
625 {
|
|
626 version (linux)
|
|
627 {
|
|
628 c = std.c.stdio.getchar();
|
|
629 if (c == EOF)
|
|
630 break;
|
|
631 }
|
|
632 else version (Windows)
|
|
633 {
|
|
634 c = std.c.stdio.getchar();
|
|
635 if (c == EOF)
|
|
636 break;
|
|
637 }
|
|
638 else version (OSX)
|
|
639 {
|
|
640 c = std.c.stdio.getchar();
|
|
641 if (c == EOF)
|
|
642 break;
|
|
643 }
|
|
644 else version (FreeBSD)
|
|
645 {
|
|
646 c = std.c.stdio.getchar();
|
|
647 if (c == EOF)
|
|
648 break;
|
|
649 }
|
|
650 else
|
|
651 {
|
|
652 static assert(0);
|
|
653 }
|
|
654 if (c == '\n')
|
|
655 break;
|
|
656 std.utf.encode(s, c);
|
|
657 }
|
|
658 ret.putVstring(s);
|
|
659 return null;
|
|
660 }
|
|
661
|
|
662 /* ====================== Dglobal_getenv ================ */
|
|
663
|
|
664 void* Dglobal_getenv(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
665 {
|
|
666 // Our own extension
|
|
667 ret.putVundefined();
|
|
668 if (arglist.length)
|
|
669 {
|
|
670 d_string s = arglist[0].toString();
|
|
671 tchar* p = getenv(std.string.toStringz(s));
|
|
672 if (p)
|
|
673 ret.putVstring(p[0 .. strlen(p)].dup);
|
|
674 else
|
|
675 ret.putVnull();
|
|
676 }
|
|
677 return null;
|
|
678 }
|
|
679
|
|
680
|
|
681 /* ====================== Dglobal_ScriptEngine ================ */
|
|
682
|
|
683 void* Dglobal_ScriptEngine(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
684 {
|
|
685 ret.putVstring(TEXT_DMDScript);
|
|
686 return null;
|
|
687 }
|
|
688
|
|
689 void* Dglobal_ScriptEngineBuildVersion(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
690 {
|
|
691 ret.putVnumber(BUILD_VERSION);
|
|
692 return null;
|
|
693 }
|
|
694
|
|
695 void* Dglobal_ScriptEngineMajorVersion(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
696 {
|
|
697 ret.putVnumber(MAJOR_VERSION);
|
|
698 return null;
|
|
699 }
|
|
700
|
|
701 void* Dglobal_ScriptEngineMinorVersion(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
702 {
|
|
703 ret.putVnumber(MINOR_VERSION);
|
|
704 return null;
|
|
705 }
|
|
706
|
|
707 /* ====================== Dglobal =========================== */
|
|
708
|
|
709 class Dglobal : Dobject
|
|
710 {
|
|
711 this(tchar[][] argv)
|
|
712 {
|
|
713 super(Dobject.getPrototype()); // Dglobal.prototype is implementation-dependent
|
|
714
|
|
715 //writef("Dglobal.Dglobal(%x)\n", this);
|
|
716 ThreadContext *tc = ThreadContext.getThreadContext();
|
|
717 assert(tc);
|
|
718
|
|
719 Dobject f = Dfunction.getPrototype();
|
|
720
|
|
721 classname = TEXT_global;
|
|
722
|
|
723 // ECMA 15.1
|
|
724 // Add in built-in objects which have attribute { DontEnum }
|
|
725
|
|
726 // Value properties
|
|
727
|
|
728 Put(TEXT_NaN, d_number.nan, DontEnum);
|
|
729 Put(TEXT_Infinity, d_number.infinity, DontEnum);
|
|
730
|
|
731 static NativeFunctionData nfd[] =
|
|
732 [
|
|
733 // Function properties
|
|
734 { &TEXT_eval, &Dglobal_eval, 1 },
|
|
735 { &TEXT_parseInt, &Dglobal_parseInt, 2 },
|
|
736 { &TEXT_parseFloat, &Dglobal_parseFloat, 1 },
|
|
737 { &TEXT_escape, &Dglobal_escape, 1 },
|
|
738 { &TEXT_unescape, &Dglobal_unescape, 1 },
|
|
739 { &TEXT_isNaN, &Dglobal_isNaN, 1 },
|
|
740 { &TEXT_isFinite, &Dglobal_isFinite, 1 },
|
|
741 { &TEXT_decodeURI, &Dglobal_decodeURI, 1 },
|
|
742 { &TEXT_decodeURIComponent, &Dglobal_decodeURIComponent, 1 },
|
|
743 { &TEXT_encodeURI, &Dglobal_encodeURI, 1 },
|
|
744 { &TEXT_encodeURIComponent, &Dglobal_encodeURIComponent, 1 },
|
|
745
|
|
746 // Dscript unique function properties
|
|
747 { &TEXT_print, &Dglobal_print, 1 },
|
|
748 { &TEXT_println, &Dglobal_println, 1 },
|
|
749 { &TEXT_readln, &Dglobal_readln, 0 },
|
|
750 { &TEXT_getenv, &Dglobal_getenv, 1 },
|
|
751
|
|
752 // Jscript compatible extensions
|
|
753 { &TEXT_ScriptEngine, &Dglobal_ScriptEngine, 0 },
|
|
754 { &TEXT_ScriptEngineBuildVersion, &Dglobal_ScriptEngineBuildVersion, 0 },
|
|
755 { &TEXT_ScriptEngineMajorVersion, &Dglobal_ScriptEngineMajorVersion, 0 },
|
|
756 { &TEXT_ScriptEngineMinorVersion, &Dglobal_ScriptEngineMinorVersion, 0 },
|
|
757 ];
|
|
758
|
|
759 DnativeFunction.init(this, nfd, DontEnum);
|
|
760
|
|
761 // Now handled by AssertExp()
|
|
762 // Put(TEXT_assert, tc.Dglobal_assert(), DontEnum);
|
|
763
|
|
764 // Constructor properties
|
|
765
|
|
766 Put(TEXT_Object, tc.Dobject_constructor, DontEnum);
|
|
767 Put(TEXT_Function, tc.Dfunction_constructor, DontEnum);
|
|
768 Put(TEXT_Array, tc.Darray_constructor, DontEnum);
|
|
769 Put(TEXT_String, tc.Dstring_constructor, DontEnum);
|
|
770 Put(TEXT_Boolean, tc.Dboolean_constructor, DontEnum);
|
|
771 Put(TEXT_Number, tc.Dnumber_constructor, DontEnum);
|
|
772 Put(TEXT_Date, tc.Ddate_constructor, DontEnum);
|
|
773 Put(TEXT_RegExp, tc.Dregexp_constructor, DontEnum);
|
|
774 Put(TEXT_Error, tc.Derror_constructor, DontEnum);
|
|
775
|
|
776 foreach (d_string key, Dfunction ctor; tc.ctorTable)
|
|
777 {
|
|
778 Put(key, ctor, DontEnum);
|
|
779 }
|
|
780
|
|
781 // Other properties
|
|
782
|
|
783 assert(tc.Dmath_object);
|
|
784 Put(TEXT_Math, tc.Dmath_object, DontEnum);
|
|
785
|
|
786 // Build an "arguments" property out of argv[],
|
|
787 // and add it to the global object.
|
|
788 Darray arguments;
|
|
789
|
|
790 arguments = new Darray();
|
|
791 Put(TEXT_arguments, arguments, DontDelete);
|
|
792 arguments.length.putVnumber(argv.length);
|
|
793 for (int i = 0; i < argv.length; i++)
|
|
794 {
|
|
795 arguments.Put(i, argv[i].dup, DontEnum);
|
|
796 }
|
|
797 arguments.Put(TEXT_callee, &vnull, DontEnum);
|
|
798 }
|
|
799 }
|