2 * Byte-code file handling.
3 * Copyright (c) 1998 New Generation Software (NGS) Oy
5 * Author: Markku Rossi <mtr@ngs.fi>
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.
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.
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,
26 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/jsdas/jsdas.js,v $
30 function ByteCode
$SymtabEntry (name
, offset
)
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
;
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
;
61 function ByteCode
$sectionName (type
)
63 var name
= type
.toString ();
65 if (type
== JSC
$BC_SECT_CODE
)
67 else if (type
== JSC
$BC_SECT_CONSTANTS
)
68 name
+= " (constants)";
69 else if (type
== JSC
$BC_SECT_SYMTAB
)
71 else if (type
== JSC
$BC_SECT_DEBUG
)
78 function ByteCode
$lookupSection (sect
)
81 for (i
= 0; i
< this.sect_types
.length
; i
++)
82 if (this.sect_types
[i
] == sect
)
83 return this.sect_data
[i
];
85 System
.print ("jsdas: no section ", this.sectionName (sect
), "\n");
90 function ByteCode
$parseConstants ()
93 return this.constants
;
95 var data
= this.lookupSection (JSC
$BC_SECT_CONSTANTS
);
99 this.constants
= new Array ();
101 for (pos
= 0; pos
< data
.length
; count
++)
103 var type
= data
[pos
++];
106 if (type
== JSC
$CONST_INT
)
108 val
= data
.substr (pos
, 4).unpack ("N")[0];
111 else if (type
== JSC
$CONST_STRING
)
113 var len
= data
.substr (pos
, 4).unpack ("N")[0];
116 val
= "\"" + data
.substr (pos
, len
);
119 else if (type
== JSC
$CONST_FLOAT
)
121 val
= data
.substr (pos
, 8).unpack ("d")[0];
124 else if (type
== JSC
$CONST_SYMBOL
)
126 val
= new String ("S");
127 for (; data
[pos
] != #'\0'; pos
++)
128 val
.append (data
.charAt (pos
));
131 else if (type
== JSC
$CONST_NAN
)
133 val
= parseFloat ("****");
135 else if (type
== JSC
$CONST_REGEXP
)
137 var flags
= data
.substr (pos
, 1).unpack ("C")[0];
140 var len
= data
.substr (pos
, 4).unpack ("N")[0];
143 val
= "/" + File
.byteToString (flags
) + data
.substr (pos
, len
);
148 /* Unknown constant type. */
149 this.constants
= false;
153 this.constants
[count
] = val
;
156 return this.constants
;
160 function ByteCode
$parseSymtab ()
165 var data
= this.lookupSection (JSC
$BC_SECT_SYMTAB
);
169 this.symtab
= new Array ();
171 /* Fetch the number of symtab entries. */
172 var nentries
= data
.unpack ("N")[0];
175 for (pos
= 4; pos
< data
.length
; count
++)
177 /* Read the symbol name. */
178 var name
= new String ("");
179 for (; data
[pos
] != #'\0'; pos
++)
180 name
.append (data
.charAt (pos
));
183 /* Read the offset. */
184 var offset
= data
.substr (pos
, 4).unpack ("N")[0];
187 this.symtab
.push (new ByteCode
$SymtabEntry (name
, offset
));
190 if (count
!= nentries
)
191 System
.print ("jsdas: warning: expected ", nentries
,
192 " symtab entries, but got ", count
, "\n");
198 function ByteCode
$prettyPrintString (str
)
204 /* Determine the type. */
216 if (flags
& JSC
$CONST_REGEXP_FLAG_G
)
218 if (flags
& JSC
$CONST_REGEXP_FLAG_I
)
224 System
.print (ender
);
225 for (; i
< str
.length
; i
++)
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 ("\\\"");
248 System
.print (str
.charAt (i
));
250 System
.print (ender
, tail
);
253 function ByteCode
$opIsSymbol (op
)
255 return (DASM
$op_flags
[op
] & 0x01) != 0;
258 function ByteCode
$opIsJump (op
)
260 return (DASM
$op_flags
[op
] & 0x02) != 0;
264 function ByteCode
$parse (stream
)
269 ch
= stream
.readByte ();
272 if (stream
.readByte () != #'!')
275 /* Skip the first comment line. */
276 this.first_line
= "#!" + stream
.readln ();
280 stream
.ungetByte (ch
);
281 this.first_line
= false;
284 buf
= stream
.read (4);
288 var a
= buf
.unpack ("N");
289 if (a
[0] != JSC
$BC_MAGIC
)
291 System
.print ("jsdas: illegal magic: ", a
[0].toString (16),
292 ", should be: ", JSC
$BC_MAGIC
.toString (16), "\n");
296 buf
= stream
.read (4);
300 var num_sections
= buf
.unpack ("N")[0];
301 this.sect_types
= new Array ();
302 this.sect_data
= new Array ();
305 for (i
= 0; i
< num_sections
; i
++)
307 /* Type and length. */
308 buf
= stream
.read (8);
312 a
= buf
.unpack ("NN");
314 this.sect_types
[i
] = a
[0];
317 buf
= stream
.read (len
);
318 if (buf
.length
!= len
)
320 System
.stdout
.writeln ("couldn't read section " + i
.toString ()
321 + ", expected=" + len
.toString ()
322 + ", got=" + buf
.length
.toString ());
326 this.sect_data
[i
] = buf
;
333 function ByteCode
$printInfo ()
337 for (i
= 0; i
< this.sect_types
.length
; i
++)
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");
347 function ByteCode
$printConstants ()
349 if (!this.parseConstants ())
351 System
.print ("jsdas: couldn't find a parse the constants section\n");
356 for (i
= 0; i
< this.constants
.length
; i
++)
358 System
.print (" ", i
, ":\t");
360 var c
= this.constants
[i
];
361 if (typeof c
== "number")
363 else if (typeof c
== "string")
365 /* Must be a string, regexp or symbol. */
368 System
.print (c
.substr (1));
370 this.prettyPrintString (c
);
374 System
.print ("jsdas: illegal element in the constans array: type=",
384 function ByteCode
$printSymtab ()
386 var symtab
= this.parseSymtab ();
389 System
.print ("jsdas: couldn't find or parse symtab section\n");
394 for (i
= 0; i
< symtab
.length
; i
++)
396 var name
= symtab
[i
].name
;
397 var offset
= symtab
[i
].offset
;
399 System
.print (" ", name
);
401 var pad
= name
.length
;
402 for (; pad
< 40; pad
++)
405 System
.print (offset
, "\n");
410 function ByteCode
$printDebug ()
412 var data
= this.lookupSection (JSC
$BC_SECT_DEBUG
);
417 var filename
= "<unknown>";
418 for (pos
= 0; pos
< data
.length
; count
++)
420 var type
= data
[pos
++];
422 if (type
== JSC
$DEBUG_FILENAME
)
424 var len
= data
.substr (pos
, 4).unpack ("N")[0];
427 filename
= data
.substr (pos
, len
);
430 else if (type
== JSC
$DEBUG_LINENUMBER
)
432 var a
= data
.substr (pos
, 8).unpack ("NN");
435 System
.print (" ", a
[0], "\t", filename
, ":", a
[1], "\n");
439 System
.print ("jsdas: unknown debug entry: ",
440 "skipping all remaining data\n");
447 function ByteCode
$printCode ()
449 var data
= this.lookupSection (JSC
$BC_SECT_CODE
);
453 var consts
= this.parseConstants ();
456 System
.print ("jsdas: couldn't find or parse constants\n");
460 var symtab
= this.parseSymtab ();
463 System
.print ("jsdas: couldn't find or parse symtab\n");
468 var next_symtab_offset
= -1;
471 for (pos
= 0; pos
< data
.length
; )
473 /* Handle symtab entries. */
474 if (pos
>= next_symtab_offset
)
476 while (pos
> next_symtab_offset
)
478 /* Lookup the next offset. */
480 if (symtab_pos
>= symtab
.length
)
481 next_symtab_offset
= data
.length
+ 1;
483 next_symtab_offset
= symtab
[symtab_pos
].offset
;
486 if (pos
== next_symtab_offset
)
487 System
.print (pos
== 0 ? "" : "\n",
488 symtab
[symtab_pos
].name
, ":\n");
491 System
.print (" ", pos
, "\t");
493 var op
= data
[pos
++];
494 if (!DASM
$op_names
[op
])
496 System
.print ("jsdas: unknown operand: ",
497 "skipping all remaining data\n");
501 System
.print (DASM
$op_names
[op
], "\t");
502 if (DASM
$op_names
[op
].length
< 8)
505 var ds
= DASM
$op_data
[op
];
511 val
= data
.substr (pos
, 2).unpack ("n")[0];
513 val
= data
.substr (pos
, 4).unpack ("N")[0];
515 if (op
== JSC
$OP_CONST
|| this.opIsSymbol (op
))
517 /* Handle symbols and constants. */
520 if (typeof c
== "string")
523 System
.print (c
.substr (1));
525 this.prettyPrintString (c
);
530 else if (this.opIsJump (op
))
538 /* Handle all the rest. */
550 function ByteCode
$linkSection (type
, data
)
553 for (i
= 0; i
< this.sect_types
.length
; i
++)
554 if (this.sect_types
[i
] == type
)
556 this.sect_data
[i
] = data
;
560 /* Create a new section. */
561 this.sect_types
.push (type
);
562 this.sect_data
.push (data
);
566 function ByteCode
$removeSection (type
)
569 for (i
= 0; i
< this.sect_types
.length
; i
++)
570 if (this.sect_types
[i
] == type
)
573 this.sect_types
.splice (i
, 1);
574 this.sect_data
.splice (i
, 1);
582 function ByteCode
$write (name
)
584 var fp
= new File (name
);
587 /* Possible first line. */
589 fp
.writeln (this.first_line
);
592 fp
.write (String
.pack ("N", JSC
$BC_MAGIC
));
594 /* Number of sections. */
595 fp
.write (String
.pack ("N", this.sect_types
.length
));
598 for (i
= 0; i
< this.sect_types
.length
; i
++)
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
));
604 /* Write the data. */
605 fp
.write (this.sect_data
[i
]);
621 * Operand definitions for the JavaScript byte-code.
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.
628 DASM
$op_names
= new Array ();
629 DASM
$op_data
= new Array ();
630 DASM
$op_flags
= new Array ();
631 DASM
$op_names
[0] = "halt";
633 DASM
$op_flags
[0] = 0x0;
634 DASM
$op_names
[1] = "done";
636 DASM
$op_flags
[1] = 0x0;
637 DASM
$op_names
[2] = "nop";
639 DASM
$op_flags
[2] = 0x0;
640 DASM
$op_names
[3] = "dup";
642 DASM
$op_flags
[3] = 0x0;
643 DASM
$op_names
[4] = "pop";
645 DASM
$op_flags
[4] = 0x0;
646 DASM
$op_names
[5] = "pop_n";
648 DASM
$op_flags
[5] = 0x0;
649 DASM
$op_names
[6] = "apop";
651 DASM
$op_flags
[6] = 0x0;
652 DASM
$op_names
[7] = "swap";
654 DASM
$op_flags
[7] = 0x0;
655 DASM
$op_names
[8] = "roll";
657 DASM
$op_flags
[8] = 0x0;
658 DASM
$op_names
[9] = "const";
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;
857 * Disassembler for JavaScript byte-code files.
858 * Copyright (c) 1998 New Generation Software (NGS) Oy
860 * Author: Markku Rossi <mtr@ngs.fi>
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.
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.
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,
881 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/jsdas/jsdas.js,v $
886 * Variables and definitions.
889 version_number
= "0.0.1";
898 * Print code section.
906 * Print constants section.
909 opt_constants
= false;
914 * Print the debug section.
922 * Print short help and exit successfully.
928 * Show general information about the file.
934 * -l TYPE DATA, --link TYPE DATA
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.
945 * -r TYPE, --remove TYPE
947 * Remove section TYPE.
956 * Print symtab section.
964 * Remove the debug section from byte-code files.
970 * Print version information and exit successfully.
980 var opt_default
= true;
982 var idx
= ARGS
[0].lastIndexOf ("/");
984 program
= ARGS
[0].substr (idx
+ 1);
988 /* Handle arguments. */
990 for (i
= 1; i
< ARGS
.length
; i
++)
992 if (ARGS
[i
][0] == #'-')
994 if (ARGS
[i
] == "-c" || ARGS
[i
] == "--code")
999 else if (ARGS
[i
] == "-C" || ARGS
[i
] == "--constants")
1001 opt_constants
= true;
1002 opt_default
= false;
1004 else if (ARGS
[i
] == "-d" || ARGS
[i
] == "--debug")
1007 opt_default
= false;
1009 else if (ARGS
[i
] == "-i" || ARGS
[i
] == "--info")
1012 opt_default
= false;
1014 else if (ARGS
[i
] == "-l" || ARGS
[i
] == "--link")
1017 opt_default
= false;
1019 if (i
+ 2 >= ARGS
.length
)
1021 System
.error (program
, ": no arguments for option --link\n");
1025 opt_link_type
= parseInt (ARGS
[++i
]);
1026 if (isNaN (opt_link_type
))
1028 System
.error (program
, ": illegal section type `",
1032 opt_link_data
= (ARGS
[++i
]);
1034 else if (ARGS
[i
] == "-r" || ARGS
[i
] == "--remove")
1037 opt_default
= false;
1039 if (i
+ 1 >= ARGS
.length
)
1041 System
.error (program
,
1042 ": no arguments for option --remove\n");
1046 opt_remove_type
= parseInt (ARGS
[++i
]);
1047 if (isNaN (opt_remove_type
))
1049 System
.error (program
, ": illegal section type `",
1054 else if (ARGS
[i
] == "-s" || ARGS
[i
] == "--symtab")
1057 opt_default
= false;
1059 else if (ARGS
[i
] == "-S" || ARGS
[i
] == "--strip")
1062 opt_default
= false;
1063 opt_remove_type
= JSC
$BC_SECT_DEBUG
;
1065 else if (ARGS
[i
] == "-h" || ARGS
[i
] == "--help")
1070 else if (ARGS
[i
] == "-V" || ARGS
[i
] == "--version")
1077 /* Unrecognized option. */
1078 System
.error (program
, ": unrecognized option `",
1080 System
.error ("Try `", program
,
1081 " --help' for more information.\n");
1087 /* End of arguments. */
1092 if (i
>= ARGS
.length
)
1094 System
.error (program
, ": no files specified\n");
1098 /* Process files. */
1100 for (; i
< ARGS
.length
; i
++)
1105 System
.print ("\n");
1107 System
.print (ARGS
[i
], ":\n");
1108 var is
= new File (ARGS
[i
]);
1111 var bc
= new ByteCode ();
1112 var result
= bc
.parse (is
);
1116 System
.print (program
, ": couldn't parse byte-code file `",
1120 var write_back
= false;
1124 System
.print ("\n* byte-code file information\n\n");
1130 System
.print ("\n* section `Constants'\n\n");
1131 bc
.printConstants ();
1134 if (opt_default
|| opt_code
)
1136 System
.print ("\n* section `Code'\n\n");
1142 System
.print ("\n* section `Symtab'\n\n");
1148 System
.print ("\n* section `Debug'\n\n");
1154 var df
= new File (opt_link_data
);
1157 var len
= df
.getLength ();
1158 var data
= df
.read (len
);
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");
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
);
1176 System
.error (program
, ": couldn't open data file `",
1177 opt_link_data
, "': ",
1178 System
.strerror (System
.errno
), "\n");
1183 System
.print (program
, ": removing section ",
1184 opt_remove_type
, "\n");
1185 if (bc
.removeSection (opt_remove_type
))
1191 /* Write the byte-code file back to its original file. */
1192 if (!bc
.write (ARGS
[i
]))
1194 System
.error (program
, ": write failed: ",
1195 System
.strerror (System
.errno
),
1203 System
.error (program
, ": couldn't open bc file `", ARGS
[i
], "': ",
1204 System
.strerror (System
.errno
), "\n");
1212 Usage: ", program
, " [OPTION]... FILE...\n\
1213 Mandatory arguments to long options are mandatory for short options too.\n");
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\
1229 System
.print ("\nReport bugs to mtr@ngs.fi.\n");
1235 System
.print ("NGS JavaScript disassembler ", version_number
, "\n");
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\