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