| 1 | /* |
|---|
| 2 | |
|---|
| 3 | NewtonScript function object dump for NTK |
|---|
| 4 | |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | /* ex. |
|---|
| 8 | |
|---|
| 9 | run on NTK: |
|---|
| 10 | dumpFn:dump("local foo := 1; bar := foo + 3"); |
|---|
| 11 | |
|---|
| 12 | "local foo := 1; bar := foo + 3" |
|---|
| 13 | {instructions: "<#instructions: 0x24, 0xA3, 0x7B, 0x27, 0x00, 0x0C, 0xC0, 0xA8, 0x70, 0x02>", |
|---|
| 14 | literals: [literals: bar], |
|---|
| 15 | argFrame: {_nextArgFrame: NIL, _parent: NIL, _implementor: NIL, foo: NIL}, |
|---|
| 16 | numArgs: 0, |
|---|
| 17 | DebuggerInfo: 2} |
|---|
| 18 | |
|---|
| 19 | run on NEWT/0: |
|---|
| 20 | DumpBC(<#instructions: 0x24, 0xA3, 0x7B, 0x27, 0x00, 0x0C, 0xC0, 0xA8, 0x70, 0x02>); |
|---|
| 21 | |
|---|
| 22 | 0 : 24 push-constant (b = 4) |
|---|
| 23 | 1 : a3 set-var (b = 3) |
|---|
| 24 | 2 : 7b get-var (b = 3) |
|---|
| 25 | 3 : 27 00 0c push-constant (b = 12) |
|---|
| 26 | 6 : c0 add |
|---|
| 27 | 7 : a8 find-and-set-var (b = 0) |
|---|
| 28 | 8 : 70 find-var (b = 0) |
|---|
| 29 | 9 : 02 return |
|---|
| 30 | |
|---|
| 31 | */ |
|---|
| 32 | |
|---|
| 33 | dumpFn := { |
|---|
| 34 | hexstr: func(v) |
|---|
| 35 | begin |
|---|
| 36 | local h := "0123456789ABCDEF"; |
|---|
| 37 | local s; |
|---|
| 38 | |
|---|
| 39 | s := h[v >> 4]; |
|---|
| 40 | s := s & h[band(v, 0x0F)]; |
|---|
| 41 | |
|---|
| 42 | "0x" & s; |
|---|
| 43 | end, |
|---|
| 44 | |
|---|
| 45 | convert: func(fn) |
|---|
| 46 | begin |
|---|
| 47 | local b; |
|---|
| 48 | local s; |
|---|
| 49 | |
|---|
| 50 | fn := clone(fn); |
|---|
| 51 | removeslot(fn, 'class); |
|---|
| 52 | |
|---|
| 53 | for i := 0 to length(fn.instructions) - 1 do begin |
|---|
| 54 | b := extractByte(fn.instructions, i); |
|---|
| 55 | s := s && :hexstr(b) & ","; |
|---|
| 56 | end; |
|---|
| 57 | |
|---|
| 58 | trimString(s); |
|---|
| 59 | setlength(s, length(s) - 1); |
|---|
| 60 | s := "<#instructions:" && s & ">"; |
|---|
| 61 | |
|---|
| 62 | fn.instructions := s; |
|---|
| 63 | |
|---|
| 64 | return fn; |
|---|
| 65 | end, |
|---|
| 66 | |
|---|
| 67 | dump: func(fnStr) |
|---|
| 68 | begin |
|---|
| 69 | local fn; |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | print(fnStr); |
|---|
| 73 | fn := compile(fnStr); |
|---|
| 74 | print(:convert(fn)); |
|---|
| 75 | |
|---|
| 76 | end |
|---|
| 77 | }; |
|---|