migrate substitution keywords to SVN
[reactos.git] / reactos / lib / kjs / jsdas / jsdas.js
1 /*
2 * Byte-code file handling.
3 * Copyright (c) 1998 New Generation Software (NGS) Oy
4 *
5 * Author: Markku Rossi <mtr@ngs.fi>
6 */
7
8 /*
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA
23 */
24
25 /*
26 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/jsdas/jsdas.js,v $
27 * $Id$
28 */
29
30 function ByteCode$SymtabEntry (name, offset)
31 {
32 this.name = name;
33 this.offset = offset;
34 }
35
36
37 function ByteCode ()
38 {
39 /* Private methods. */
40 this.sectionName = ByteCode$sectionName;
41 this.lookupSection = ByteCode$lookupSection;
42 this.parseConstants = ByteCode$parseConstants;
43 this.parseSymtab = ByteCode$parseSymtab;
44 this.prettyPrintString = ByteCode$prettyPrintString;
45 this.opIsSymbol = ByteCode$opIsSymbol;
46 this.opIsJump = ByteCode$opIsJump;
47
48 /* Public methods. */
49 this.parse = ByteCode$parse;
50 this.write = ByteCode$write;
51 this.printInfo = ByteCode$printInfo;
52 this.printCode = ByteCode$printCode;
53 this.printConstants = ByteCode$printConstants;
54 this.printSymtab = ByteCode$printSymtab;
55 this.printDebug = ByteCode$printDebug;
56 this.linkSection = ByteCode$linkSection;
57 this.removeSection = ByteCode$removeSection;
58 }
59
60
61 function ByteCode$sectionName (type)
62 {
63 var name = type.toString ();
64
65 if (type == JSC$BC_SECT_CODE)
66 name += " (code)";
67 else if (type == JSC$BC_SECT_CONSTANTS)
68 name += " (constants)";
69 else if (type == JSC$BC_SECT_SYMTAB)
70 name += " (symtab)";
71 else if (type == JSC$BC_SECT_DEBUG)
72 name += " (debug)";
73
74 return name;
75 }
76
77
78 function ByteCode$lookupSection (sect)
79 {
80 var i;
81 for (i = 0; i < this.sect_types.length; i++)
82 if (this.sect_types[i] == sect)
83 return this.sect_data[i];
84
85 System.print ("jsdas: no section ", this.sectionName (sect), "\n");
86 return false;
87 }
88
89
90 function ByteCode$parseConstants ()
91 {
92 if (this.constants)
93 return this.constants;
94
95 var data = this.lookupSection (JSC$BC_SECT_CONSTANTS);
96 if (!data)
97 return false;
98
99 this.constants = new Array ();
100 var pos, count = 0;
101 for (pos = 0; pos < data.length; count++)
102 {
103 var type = data[pos++];
104 var val;
105
106 if (type == JSC$CONST_INT)
107 {
108 val = data.substr (pos, 4).unpack ("N")[0];
109 pos += 4;
110 }
111 else if (type == JSC$CONST_STRING)
112 {
113 var len = data.substr (pos, 4).unpack ("N")[0];
114 pos += 4;
115
116 val = "\"" + data.substr (pos, len);
117 pos += len;
118 }
119 else if (type == JSC$CONST_FLOAT)
120 {
121 val = data.substr (pos, 8).unpack ("d")[0];
122 pos += 8;
123 }
124 else if (type == JSC$CONST_SYMBOL)
125 {
126 val = new String ("S");
127 for (; data[pos] != #'\0'; pos++)
128 val.append (data.charAt (pos));
129 pos++;
130 }
131 else if (type == JSC$CONST_NAN)
132 {
133 val = parseFloat ("****");
134 }
135 else if (type == JSC$CONST_REGEXP)
136 {
137 var flags = data.substr (pos, 1).unpack ("C")[0];
138 pos++;
139
140 var len = data.substr (pos, 4).unpack ("N")[0];
141 pos += 4;
142
143 val = "/" + File.byteToString (flags) + data.substr (pos, len);
144 pos += len;
145 }
146 else
147 {
148 /* Unknown constant type. */
149 this.constants = false;
150 return false;
151 }
152
153 this.constants[count] = val;
154 }
155
156 return this.constants;
157 }
158
159
160 function ByteCode$parseSymtab ()
161 {
162 if (this.symtab)
163 return this.symtab;
164
165 var data = this.lookupSection (JSC$BC_SECT_SYMTAB);
166 if (!data)
167 return false;
168
169 this.symtab = new Array ();
170
171 /* Fetch the number of symtab entries. */
172 var nentries = data.unpack ("N")[0];
173
174 var pos, count = 0;
175 for (pos = 4; pos < data.length; count++)
176 {
177 /* Read the symbol name. */
178 var name = new String ("");
179 for (; data[pos] != #'\0'; pos++)
180 name.append (data.charAt (pos));
181 pos++;
182
183 /* Read the offset. */
184 var offset = data.substr (pos, 4).unpack ("N")[0];
185 pos += 4;
186
187 this.symtab.push (new ByteCode$SymtabEntry (name, offset));
188 }
189
190 if (count != nentries)
191 System.print ("jsdas: warning: expected ", nentries,
192 " symtab entries, but got ", count, "\n");
193
194 return this.symtab;
195 }
196
197
198 function ByteCode$prettyPrintString (str)
199 {
200 var i;
201 var tail = "";
202 var ender;
203
204 /* Determine the type. */
205 if (str[0] == #'"')
206 {
207 i = 1;
208 ender = "\"";
209 }
210 else
211 {
212 /* Regexp. */
213 var flags = str[1];
214 ender = "/";
215
216 if (flags & JSC$CONST_REGEXP_FLAG_G)
217 tail += "g";
218 if (flags & JSC$CONST_REGEXP_FLAG_I)
219 tail += "i";
220
221 i = 2;
222 }
223
224 System.print (ender);
225 for (; i < str.length; i++)
226 {
227 if (str[i] == ender[0] || str[i] == #'\\')
228 System.print ("\\" + str.charAt (i));
229 else if (str[i] == #'\n')
230 System.print ("\\n");
231 else if (str[i] == #'\t')
232 System.print ("\\t");
233 else if (str[i] == #'\v')
234 System.print ("\\v");
235 else if (str[i] == #'\b')
236 System.print ("\\b");
237 else if (str[i] == #'\r')
238 System.print ("\\r");
239 else if (str[i] == #'\f')
240 System.print ("\\f");
241 else if (str[i] == #'\a')
242 System.print ("\\a");
243 else if (str[i] == #'\'')
244 System.print ("\\\'");
245 else if (str[i] == #'\"')
246 System.print ("\\\"");
247 else
248 System.print (str.charAt (i));
249 }
250 System.print (ender, tail);
251 }
252
253 function ByteCode$opIsSymbol (op)
254 {
255 return (DASM$op_flags[op] & 0x01) != 0;
256 }
257
258 function ByteCode$opIsJump (op)
259 {
260 return (DASM$op_flags[op] & 0x02) != 0;
261 }
262
263
264 function ByteCode$parse (stream)
265 {
266 var ch;
267 var buf;
268
269 ch = stream.readByte ();
270 if (ch == #'#')
271 {
272 if (stream.readByte () != #'!')
273 return false;
274
275 /* Skip the first comment line. */
276 this.first_line = "#!" + stream.readln ();
277 }
278 else
279 {
280 stream.ungetByte (ch);
281 this.first_line = false;
282 }
283
284 buf = stream.read (4);
285 if (buf.length != 4)
286 return false;
287
288 var a = buf.unpack ("N");
289 if (a[0] != JSC$BC_MAGIC)
290 {
291 System.print ("jsdas: illegal magic: ", a[0].toString (16),
292 ", should be: ", JSC$BC_MAGIC.toString (16), "\n");
293 return false;
294 }
295
296 buf = stream.read (4);
297 if (buf.length != 4)
298 return false;
299
300 var num_sections = buf.unpack ("N")[0];
301 this.sect_types = new Array ();
302 this.sect_data = new Array ();
303
304 /* Read sections. */
305 for (i = 0; i < num_sections; i++)
306 {
307 /* Type and length. */
308 buf = stream.read (8);
309 if (buf.length != 8)
310 return false;
311
312 a = buf.unpack ("NN");
313
314 this.sect_types[i] = a[0];
315
316 var len = a[1];
317 buf = stream.read (len);
318 if (buf.length != len)
319 {
320 System.stdout.writeln ("couldn't read section " + i.toString ()
321 + ", expected=" + len.toString ()
322 + ", got=" + buf.length.toString ());
323 return false;
324 }
325
326 this.sect_data[i] = buf;
327 }
328
329 return true;
330 }
331
332
333 function ByteCode$printInfo ()
334 {
335 var i;
336
337 for (i = 0; i < this.sect_types.length; i++)
338 {
339 var sectname = this.sectionName (this.sect_types[i]);
340 System.print (" section ", i, ": ");
341 System.print ("type=", sectname);
342 System.print (", length=", this.sect_data[i].length, "\n");
343 }
344 }
345
346
347 function ByteCode$printConstants ()
348 {
349 if (!this.parseConstants ())
350 {
351 System.print ("jsdas: couldn't find a parse the constants section\n");
352 return;
353 }
354
355 var i;
356 for (i = 0; i < this.constants.length; i++)
357 {
358 System.print (" ", i, ":\t");
359
360 var c = this.constants[i];
361 if (typeof c == "number")
362 System.print (c);
363 else if (typeof c == "string")
364 {
365 /* Must be a string, regexp or symbol. */
366 if (c[0] == #'S')
367 /* Symbol. */
368 System.print (c.substr (1));
369 else
370 this.prettyPrintString (c);
371 }
372 else
373 {
374 System.print ("jsdas: illegal element in the constans array: type=",
375 typeof c, "\n");
376 return;
377 }
378
379 System.print ("\n");
380 }
381 }
382
383
384 function ByteCode$printSymtab ()
385 {
386 var symtab = this.parseSymtab ();
387 if (!symtab)
388 {
389 System.print ("jsdas: couldn't find or parse symtab section\n");
390 return;
391 }
392
393 var i;
394 for (i = 0; i < symtab.length; i++)
395 {
396 var name = symtab[i].name;
397 var offset = symtab[i].offset;
398
399 System.print (" ", name);
400
401 var pad = name.length;
402 for (; pad < 40; pad++)
403 System.print (" ");
404
405 System.print (offset, "\n");
406 }
407 }
408
409
410 function ByteCode$printDebug ()
411 {
412 var data = this.lookupSection (JSC$BC_SECT_DEBUG);
413 if (!data)
414 return;
415
416 var pos, count = 0;
417 var filename = "<unknown>";
418 for (pos = 0; pos < data.length; count++)
419 {
420 var type = data[pos++];
421
422 if (type == JSC$DEBUG_FILENAME)
423 {
424 var len = data.substr (pos, 4).unpack ("N")[0];
425 pos += 4;
426
427 filename = data.substr (pos, len);
428 pos += len;
429 }
430 else if (type == JSC$DEBUG_LINENUMBER)
431 {
432 var a = data.substr (pos, 8).unpack ("NN");
433 pos += 8;
434
435 System.print (" ", a[0], "\t", filename, ":", a[1], "\n");
436 }
437 else
438 {
439 System.print ("jsdas: unknown debug entry: ",
440 "skipping all remaining data\n");
441 return;
442 }
443 }
444 }
445
446
447 function ByteCode$printCode ()
448 {
449 var data = this.lookupSection (JSC$BC_SECT_CODE);
450 if (!data)
451 return;
452
453 var consts = this.parseConstants ();
454 if (!consts)
455 {
456 System.print ("jsdas: couldn't find or parse constants\n");
457 return;
458 }
459
460 var symtab = this.parseSymtab ();
461 if (!symtab)
462 {
463 System.print ("jsdas: couldn't find or parse symtab\n");
464 return;
465 }
466
467 var symtab_pos = -1;
468 var next_symtab_offset = -1;
469
470 var pos;
471 for (pos = 0; pos < data.length; )
472 {
473 /* Handle symtab entries. */
474 if (pos >= next_symtab_offset)
475 {
476 while (pos > next_symtab_offset)
477 {
478 /* Lookup the next offset. */
479 symtab_pos++;
480 if (symtab_pos >= symtab.length)
481 next_symtab_offset = data.length + 1;
482 else
483 next_symtab_offset = symtab[symtab_pos].offset;
484 }
485
486 if (pos == next_symtab_offset)
487 System.print (pos == 0 ? "" : "\n",
488 symtab[symtab_pos].name, ":\n");
489 }
490
491 System.print (" ", pos, "\t");
492
493 var op = data[pos++];
494 if (!DASM$op_names[op])
495 {
496 System.print ("jsdas: unknown operand: ",
497 "skipping all remaining data\n");
498 return;
499 }
500
501 System.print (DASM$op_names[op], "\t");
502 if (DASM$op_names[op].length < 8)
503 System.print ("\t");
504
505 var ds = DASM$op_data[op];
506 var val;
507
508 if (ds == 1)
509 val = data[pos];
510 else if (ds == 2)
511 val = data.substr (pos, 2).unpack ("n")[0];
512 else if (ds == 4)
513 val = data.substr (pos, 4).unpack ("N")[0];
514
515 if (op == JSC$OP_CONST || this.opIsSymbol (op))
516 {
517 /* Handle symbols and constants. */
518
519 var c = consts[val];
520 if (typeof c == "string")
521 {
522 if (c[0] == #'S')
523 System.print (c.substr (1));
524 else
525 this.prettyPrintString (c);
526 }
527 else
528 System.print (c);
529 }
530 else if (this.opIsJump (op))
531 {
532 /* Local jumps. */
533 val += pos + ds;
534 System.print (val);
535 }
536 else
537 {
538 /* Handle all the rest. */
539 if (ds != 0)
540 System.print (val);
541 }
542
543 pos += ds;
544
545 System.print ("\n");
546 }
547 }
548
549
550 function ByteCode$linkSection (type, data)
551 {
552 var i;
553 for (i = 0; i < this.sect_types.length; i++)
554 if (this.sect_types[i] == type)
555 {
556 this.sect_data[i] = data;
557 return;
558 }
559
560 /* Create a new section. */
561 this.sect_types.push (type);
562 this.sect_data.push (data);
563 }
564
565
566 function ByteCode$removeSection (type)
567 {
568 var i;
569 for (i = 0; i < this.sect_types.length; i++)
570 if (this.sect_types[i] == type)
571 {
572 /* Found it. */
573 this.sect_types.splice (i, 1);
574 this.sect_data.splice (i, 1);
575 return true;
576 }
577
578 return false;
579 }
580
581
582 function ByteCode$write (name)
583 {
584 var fp = new File (name);
585 if (fp.open ("w"))
586 {
587 /* Possible first line. */
588 if (this.first_line)
589 fp.writeln (this.first_line);
590
591 /* Magic. */
592 fp.write (String.pack ("N", JSC$BC_MAGIC));
593
594 /* Number of sections. */
595 fp.write (String.pack ("N", this.sect_types.length));
596
597 var i;
598 for (i = 0; i < this.sect_types.length; i++)
599 {
600 /* Write type and the length of the section. */
601 fp.write (String.pack ("NN", this.sect_types[i],
602 this.sect_data[i].length));
603
604 /* Write the data. */
605 fp.write (this.sect_data[i]);
606 }
607
608 return fp.close ();
609 }
610
611 return false;
612 }
613
614 \f
615 /*
616 Local variables:
617 mode: c
618 End:
619 */
620 /* -*- c -*-
621 * Operand definitions for the JavaScript byte-code.
622 *
623 * This file is automatically create from the operands.def file.
624 * Editing is strongly discouraged. You should edit the file
625 * `extract-op-names.js' instead.
626 */
627
628 DASM$op_names = new Array ();
629 DASM$op_data = new Array ();
630 DASM$op_flags = new Array ();
631 DASM$op_names[0] = "halt";
632 DASM$op_data[0] = 0;
633 DASM$op_flags[0] = 0x0;
634 DASM$op_names[1] = "done";
635 DASM$op_data[1] = 0;
636 DASM$op_flags[1] = 0x0;
637 DASM$op_names[2] = "nop";
638 DASM$op_data[2] = 0;
639 DASM$op_flags[2] = 0x0;
640 DASM$op_names[3] = "dup";
641 DASM$op_data[3] = 0;
642 DASM$op_flags[3] = 0x0;
643 DASM$op_names[4] = "pop";
644 DASM$op_data[4] = 0;
645 DASM$op_flags[4] = 0x0;
646 DASM$op_names[5] = "pop_n";
647 DASM$op_data[5] = 1;
648 DASM$op_flags[5] = 0x0;
649 DASM$op_names[6] = "apop";
650 DASM$op_data[6] = 1;
651 DASM$op_flags[6] = 0x0;
652 DASM$op_names[7] = "swap";
653 DASM$op_data[7] = 0;
654 DASM$op_flags[7] = 0x0;
655 DASM$op_names[8] = "roll";
656 DASM$op_data[8] = 1;
657 DASM$op_flags[8] = 0x0;
658 DASM$op_names[9] = "const";
659 DASM$op_data[9] = 4;
660 DASM$op_flags[9] = 0x0;
661 DASM$op_names[10] = "const_null";
662 DASM$op_data[10] = 0;
663 DASM$op_flags[10] = 0x0;
664 DASM$op_names[11] = "const_true";
665 DASM$op_data[11] = 0;
666 DASM$op_flags[11] = 0x0;
667 DASM$op_names[12] = "const_false";
668 DASM$op_data[12] = 0;
669 DASM$op_flags[12] = 0x0;
670 DASM$op_names[13] = "const_undefined";
671 DASM$op_data[13] = 0;
672 DASM$op_flags[13] = 0x0;
673 DASM$op_names[14] = "const_i0";
674 DASM$op_data[14] = 0;
675 DASM$op_flags[14] = 0x0;
676 DASM$op_names[15] = "const_i1";
677 DASM$op_data[15] = 0;
678 DASM$op_flags[15] = 0x0;
679 DASM$op_names[16] = "const_i2";
680 DASM$op_data[16] = 0;
681 DASM$op_flags[16] = 0x0;
682 DASM$op_names[17] = "const_i3";
683 DASM$op_data[17] = 0;
684 DASM$op_flags[17] = 0x0;
685 DASM$op_names[18] = "const_i";
686 DASM$op_data[18] = 4;
687 DASM$op_flags[18] = 0x0;
688 DASM$op_names[19] = "load_global";
689 DASM$op_data[19] = 4;
690 DASM$op_flags[19] = 0x1;
691 DASM$op_names[20] = "store_global";
692 DASM$op_data[20] = 4;
693 DASM$op_flags[20] = 0x1;
694 DASM$op_names[21] = "load_arg";
695 DASM$op_data[21] = 1;
696 DASM$op_flags[21] = 0x0;
697 DASM$op_names[22] = "store_arg";
698 DASM$op_data[22] = 1;
699 DASM$op_flags[22] = 0x0;
700 DASM$op_names[23] = "load_local";
701 DASM$op_data[23] = 2;
702 DASM$op_flags[23] = 0x0;
703 DASM$op_names[24] = "store_local";
704 DASM$op_data[24] = 2;
705 DASM$op_flags[24] = 0x0;
706 DASM$op_names[25] = "load_property";
707 DASM$op_data[25] = 4;
708 DASM$op_flags[25] = 0x1;
709 DASM$op_names[26] = "store_property";
710 DASM$op_data[26] = 4;
711 DASM$op_flags[26] = 0x1;
712 DASM$op_names[27] = "load_array";
713 DASM$op_data[27] = 0;
714 DASM$op_flags[27] = 0x0;
715 DASM$op_names[28] = "store_array";
716 DASM$op_data[28] = 0;
717 DASM$op_flags[28] = 0x0;
718 DASM$op_names[29] = "nth";
719 DASM$op_data[29] = 0;
720 DASM$op_flags[29] = 0x0;
721 DASM$op_names[30] = "cmp_eq";
722 DASM$op_data[30] = 0;
723 DASM$op_flags[30] = 0x0;
724 DASM$op_names[31] = "cmp_ne";
725 DASM$op_data[31] = 0;
726 DASM$op_flags[31] = 0x0;
727 DASM$op_names[32] = "cmp_lt";
728 DASM$op_data[32] = 0;
729 DASM$op_flags[32] = 0x0;
730 DASM$op_names[33] = "cmp_gt";
731 DASM$op_data[33] = 0;
732 DASM$op_flags[33] = 0x0;
733 DASM$op_names[34] = "cmp_le";
734 DASM$op_data[34] = 0;
735 DASM$op_flags[34] = 0x0;
736 DASM$op_names[35] = "cmp_ge";
737 DASM$op_data[35] = 0;
738 DASM$op_flags[35] = 0x0;
739 DASM$op_names[36] = "cmp_seq";
740 DASM$op_data[36] = 0;
741 DASM$op_flags[36] = 0x0;
742 DASM$op_names[37] = "cmp_sne";
743 DASM$op_data[37] = 0;
744 DASM$op_flags[37] = 0x0;
745 DASM$op_names[38] = "sub";
746 DASM$op_data[38] = 0;
747 DASM$op_flags[38] = 0x0;
748 DASM$op_names[39] = "add";
749 DASM$op_data[39] = 0;
750 DASM$op_flags[39] = 0x0;
751 DASM$op_names[40] = "mul";
752 DASM$op_data[40] = 0;
753 DASM$op_flags[40] = 0x0;
754 DASM$op_names[41] = "div";
755 DASM$op_data[41] = 0;
756 DASM$op_flags[41] = 0x0;
757 DASM$op_names[42] = "mod";
758 DASM$op_data[42] = 0;
759 DASM$op_flags[42] = 0x0;
760 DASM$op_names[43] = "neg";
761 DASM$op_data[43] = 0;
762 DASM$op_flags[43] = 0x0;
763 DASM$op_names[44] = "and";
764 DASM$op_data[44] = 0;
765 DASM$op_flags[44] = 0x0;
766 DASM$op_names[45] = "not";
767 DASM$op_data[45] = 0;
768 DASM$op_flags[45] = 0x0;
769 DASM$op_names[46] = "or";
770 DASM$op_data[46] = 0;
771 DASM$op_flags[46] = 0x0;
772 DASM$op_names[47] = "xor";
773 DASM$op_data[47] = 0;
774 DASM$op_flags[47] = 0x0;
775 DASM$op_names[48] = "shift_left";
776 DASM$op_data[48] = 0;
777 DASM$op_flags[48] = 0x0;
778 DASM$op_names[49] = "shift_right";
779 DASM$op_data[49] = 0;
780 DASM$op_flags[49] = 0x0;
781 DASM$op_names[50] = "shift_rright";
782 DASM$op_data[50] = 0;
783 DASM$op_flags[50] = 0x0;
784 DASM$op_names[51] = "iffalse";
785 DASM$op_data[51] = 4;
786 DASM$op_flags[51] = 0x2;
787 DASM$op_names[52] = "iftrue";
788 DASM$op_data[52] = 4;
789 DASM$op_flags[52] = 0x2;
790 DASM$op_names[53] = "call_method";
791 DASM$op_data[53] = 4;
792 DASM$op_flags[53] = 0x1;
793 DASM$op_names[54] = "jmp";
794 DASM$op_data[54] = 4;
795 DASM$op_flags[54] = 0x2;
796 DASM$op_names[55] = "jsr";
797 DASM$op_data[55] = 0;
798 DASM$op_flags[55] = 0x0;
799 DASM$op_names[56] = "return";
800 DASM$op_data[56] = 0;
801 DASM$op_flags[56] = 0x0;
802 DASM$op_names[57] = "typeof";
803 DASM$op_data[57] = 0;
804 DASM$op_flags[57] = 0x0;
805 DASM$op_names[58] = "new";
806 DASM$op_data[58] = 0;
807 DASM$op_flags[58] = 0x0;
808 DASM$op_names[59] = "delete_property";
809 DASM$op_data[59] = 4;
810 DASM$op_flags[59] = 0x1;
811 DASM$op_names[60] = "delete_array";
812 DASM$op_data[60] = 0;
813 DASM$op_flags[60] = 0x0;
814 DASM$op_names[61] = "locals";
815 DASM$op_data[61] = 2;
816 DASM$op_flags[61] = 0x0;
817 DASM$op_names[62] = "min_args";
818 DASM$op_data[62] = 1;
819 DASM$op_flags[62] = 0x0;
820 DASM$op_names[63] = "load_nth_arg";
821 DASM$op_data[63] = 0;
822 DASM$op_flags[63] = 0x0;
823 DASM$op_names[64] = "with_push";
824 DASM$op_data[64] = 0;
825 DASM$op_flags[64] = 0x0;
826 DASM$op_names[65] = "with_pop";
827 DASM$op_data[65] = 1;
828 DASM$op_flags[65] = 0x0;
829 DASM$op_names[66] = "try_push";
830 DASM$op_data[66] = 4;
831 DASM$op_flags[66] = 0x2;
832 DASM$op_names[67] = "try_pop";
833 DASM$op_data[67] = 1;
834 DASM$op_flags[67] = 0x0;
835 DASM$op_names[68] = "throw";
836 DASM$op_data[68] = 0;
837 DASM$op_flags[68] = 0x0;
838 DASM$op_names[69] = "iffalse_b";
839 DASM$op_data[69] = 4;
840 DASM$op_flags[69] = 0x2;
841 DASM$op_names[70] = "iftrue_b";
842 DASM$op_data[70] = 4;
843 DASM$op_flags[70] = 0x2;
844 DASM$op_names[71] = "add_1_i";
845 DASM$op_data[71] = 0;
846 DASM$op_flags[71] = 0x0;
847 DASM$op_names[72] = "add_2_i";
848 DASM$op_data[72] = 0;
849 DASM$op_flags[72] = 0x0;
850 DASM$op_names[73] = "load_global_w";
851 DASM$op_data[73] = 4;
852 DASM$op_flags[73] = 0x1;
853 DASM$op_names[74] = "jsr_w";
854 DASM$op_data[74] = 4;
855 DASM$op_flags[74] = 0x1;
856 /*
857 * Disassembler for JavaScript byte-code files.
858 * Copyright (c) 1998 New Generation Software (NGS) Oy
859 *
860 * Author: Markku Rossi <mtr@ngs.fi>
861 */
862
863 /*
864 * This library is free software; you can redistribute it and/or
865 * modify it under the terms of the GNU Library General Public
866 * License as published by the Free Software Foundation; either
867 * version 2 of the License, or (at your option) any later version.
868 *
869 * This library is distributed in the hope that it will be useful,
870 * but WITHOUT ANY WARRANTY; without even the implied warranty of
871 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
872 * Library General Public License for more details.
873 *
874 * You should have received a copy of the GNU Library General Public
875 * License along with this library; if not, write to the Free
876 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
877 * MA 02111-1307, USA
878 */
879
880 /*
881 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/jsdas/jsdas.js,v $
882 * $Id$
883 */
884
885 /*
886 * Variables and definitions.
887 */
888
889 version_number = "0.0.1";
890
891 /*
892 * Options.
893 */
894
895 /*
896 * -c, --code
897 *
898 * Print code section.
899 */
900
901 opt_code = false;
902
903 /*
904 * -C, --constants
905 *
906 * Print constants section.
907 */
908
909 opt_constants = false;
910
911 /*
912 * -d, --debug
913 *
914 * Print the debug section.
915 */
916
917 opt_debug = false;
918
919 /*
920 * -h, --help
921 *
922 * Print short help and exit successfully.
923 */
924
925 /*
926 * -i, --info
927 *
928 * Show general information about the file.
929 */
930
931 opt_info = false;
932
933 /*
934 * -l TYPE DATA, --link TYPE DATA
935 *
936 * Link a new section to the byte-code file. The new section's type is
937 * TYPE and its data is in file DATA.
938 */
939
940 opt_link = false;
941 opt_link_type = 0;
942 opt_link_data = "";
943
944 /*
945 * -r TYPE, --remove TYPE
946 *
947 * Remove section TYPE.
948 */
949
950 opt_remove = false;
951 opt_remove_type = 0;
952
953 /*
954 * -s, --symtab
955 *
956 * Print symtab section.
957 */
958
959 opt_symtab = false;
960
961 /*
962 * -S, --strip
963 *
964 * Remove the debug section from byte-code files.
965 */
966
967 /*
968 * -V, --version
969 *
970 * Print version information and exit successfully.
971 */
972
973
974 /*
975 * Functions.
976 */
977
978 function main ()
979 {
980 var opt_default = true;
981
982 var idx = ARGS[0].lastIndexOf ("/");
983 if (idx >= 0)
984 program = ARGS[0].substr (idx + 1);
985 else
986 program = ARGS[0];
987
988 /* Handle arguments. */
989 var i;
990 for (i = 1; i < ARGS.length; i++)
991 {
992 if (ARGS[i][0] == #'-')
993 {
994 if (ARGS[i] == "-c" || ARGS[i] == "--code")
995 {
996 opt_code = true;
997 opt_default = false;
998 }
999 else if (ARGS[i] == "-C" || ARGS[i] == "--constants")
1000 {
1001 opt_constants = true;
1002 opt_default = false;
1003 }
1004 else if (ARGS[i] == "-d" || ARGS[i] == "--debug")
1005 {
1006 opt_debug = true;
1007 opt_default = false;
1008 }
1009 else if (ARGS[i] == "-i" || ARGS[i] == "--info")
1010 {
1011 opt_info = true;
1012 opt_default = false;
1013 }
1014 else if (ARGS[i] == "-l" || ARGS[i] == "--link")
1015 {
1016 opt_link = true;
1017 opt_default = false;
1018
1019 if (i + 2 >= ARGS.length)
1020 {
1021 System.error (program, ": no arguments for option --link\n");
1022 System.exit (1);
1023 }
1024
1025 opt_link_type = parseInt (ARGS[++i]);
1026 if (isNaN (opt_link_type))
1027 {
1028 System.error (program, ": illegal section type `",
1029 ARGS[i], "'\n");
1030 System.exit (1);
1031 }
1032 opt_link_data = (ARGS[++i]);
1033 }
1034 else if (ARGS[i] == "-r" || ARGS[i] == "--remove")
1035 {
1036 opt_remove = true;
1037 opt_default = false;
1038
1039 if (i + 1 >= ARGS.length)
1040 {
1041 System.error (program,
1042 ": no arguments for option --remove\n");
1043 System.exit (1);
1044 }
1045
1046 opt_remove_type = parseInt (ARGS[++i]);
1047 if (isNaN (opt_remove_type))
1048 {
1049 System.error (program, ": illegal section type `",
1050 ARGS[i], "'\n");
1051 System.exit (1);
1052 }
1053 }
1054 else if (ARGS[i] == "-s" || ARGS[i] == "--symtab")
1055 {
1056 opt_symtab = true;
1057 opt_default = false;
1058 }
1059 else if (ARGS[i] == "-S" || ARGS[i] == "--strip")
1060 {
1061 opt_remove = true;
1062 opt_default = false;
1063 opt_remove_type = JSC$BC_SECT_DEBUG;
1064 }
1065 else if (ARGS[i] == "-h" || ARGS[i] == "--help")
1066 {
1067 usage ();
1068 System.exit (0);
1069 }
1070 else if (ARGS[i] == "-V" || ARGS[i] == "--version")
1071 {
1072 version ();
1073 System.exit (0);
1074 }
1075 else
1076 {
1077 /* Unrecognized option. */
1078 System.error (program, ": unrecognized option `",
1079 ARGS[i], "'\n");
1080 System.error ("Try `", program,
1081 " --help' for more information.\n");
1082 System.exit (1);
1083 }
1084 }
1085 else
1086 {
1087 /* End of arguments. */
1088 break;
1089 }
1090 }
1091
1092 if (i >= ARGS.length)
1093 {
1094 System.error (program, ": no files specified\n");
1095 System.exit (1);
1096 }
1097
1098 /* Process files. */
1099 var first = true;
1100 for (; i < ARGS.length; i++)
1101 {
1102 if (first)
1103 first = false;
1104 else
1105 System.print ("\n");
1106
1107 System.print (ARGS[i], ":\n");
1108 var is = new File (ARGS[i]);
1109 if (is.open ("r"))
1110 {
1111 var bc = new ByteCode ();
1112 var result = bc.parse (is);
1113 is.close ();
1114
1115 if (!result)
1116 System.print (program, ": couldn't parse byte-code file `",
1117 ARGS[i], "'\n");
1118 else
1119 {
1120 var write_back = false;
1121
1122 if (opt_info)
1123 {
1124 System.print ("\n* byte-code file information\n\n");
1125 bc.printInfo ();
1126 }
1127
1128 if (opt_constants)
1129 {
1130 System.print ("\n* section `Constants'\n\n");
1131 bc.printConstants ();
1132 }
1133
1134 if (opt_default || opt_code)
1135 {
1136 System.print ("\n* section `Code'\n\n");
1137 bc.printCode ();
1138 }
1139
1140 if (opt_symtab)
1141 {
1142 System.print ("\n* section `Symtab'\n\n");
1143 bc.printSymtab ();
1144 }
1145
1146 if (opt_debug)
1147 {
1148 System.print ("\n* section `Debug'\n\n");
1149 bc.printDebug ();
1150 }
1151
1152 if (opt_link)
1153 {
1154 var df = new File (opt_link_data);
1155 if (df.open ("r"))
1156 {
1157 var len = df.getLength ();
1158 var data = df.read (len);
1159 df.close ();
1160
1161 if (data.length < len)
1162 System.error (program,
1163 ": couldn't read data from file `",
1164 opt_link_data, "': ",
1165 System.strerror (System.errno), "\n");
1166 else
1167 {
1168 System.print (program, ": linking ", data.length,
1169 " bytes of data to section ",
1170 opt_link_type.toString (), "\n");
1171 bc.linkSection (opt_link_type, data);
1172 write_back = true;
1173 }
1174 }
1175 else
1176 System.error (program, ": couldn't open data file `",
1177 opt_link_data, "': ",
1178 System.strerror (System.errno), "\n");
1179 }
1180
1181 if (opt_remove)
1182 {
1183 System.print (program, ": removing section ",
1184 opt_remove_type, "\n");
1185 if (bc.removeSection (opt_remove_type))
1186 write_back = true;
1187 }
1188
1189 if (write_back)
1190 {
1191 /* Write the byte-code file back to its original file. */
1192 if (!bc.write (ARGS[i]))
1193 {
1194 System.error (program, ": write failed: ",
1195 System.strerror (System.errno),
1196 "\n");
1197 System.exit (1);
1198 }
1199 }
1200 }
1201 }
1202 else
1203 System.error (program, ": couldn't open bc file `", ARGS[i], "': ",
1204 System.strerror (System.errno), "\n");
1205 }
1206 }
1207
1208
1209 function usage ()
1210 {
1211 System.print ("\
1212 Usage: ", program, " [OPTION]... FILE...\n\
1213 Mandatory arguments to long options are mandatory for short options too.\n");
1214
1215 System.print ("\
1216 -c, --code print code section (default)\n\
1217 -C, --constants print constants section\n\
1218 -d, --debug print debug section\n\
1219 -h, --help print this help and exit\n\
1220 -i, --info print the byte-code file header and general\n\
1221 information about the sections\n\
1222 -l, --link TYPE DATA link data from file DATA to section TYPE\n\
1223 -r, --remove TYPE remove section TYPE\n\
1224 -s, --symtab print symtab section\n\
1225 -S, --strip remove debug section\n\
1226 -V, --version print version number\n\
1227 ");
1228
1229 System.print ("\nReport bugs to mtr@ngs.fi.\n");
1230 }
1231
1232
1233 function version ()
1234 {
1235 System.print ("NGS JavaScript disassembler ", version_number, "\n");
1236 System.print ("\
1237 Copyright (C) 1998 New Generation Software (NGS) Oy.\n\
1238 NGS JavaScript Interpreter comes with NO WARRANTY, to the extent\n\
1239 permitted by law. You may redistribute copies of NGS JavaScript\n\
1240 Interpreter under the terms of the GNU Library General Public License.\n\
1241 For more information about these matters, see the files named COPYING.\n\
1242 ");
1243 }
1244
1245
1246 main ();
1247
1248 \f
1249 /*
1250 Local variables:
1251 mode: c
1252 End:
1253 */