pluotsorbet/types.ts

319 строки
8.9 KiB
TypeScript
Исходник Обычный вид История

2014-10-18 02:41:38 +04:00
module J2ME {
import assert = Debug.assert;
2015-05-08 01:17:05 +03:00
import Bytecodes = Bytecode.Bytecodes;
2014-10-18 02:41:38 +04:00
var writer = new IndentingWriter();
2015-08-21 06:10:46 +03:00
export const enum Kind {
2014-10-18 02:41:38 +04:00
Boolean,
Byte,
Short,
Char,
Int,
Float,
Long,
Double,
Reference,
Void,
Illegal,
2015-08-07 03:10:32 +03:00
High,
Store
2014-10-18 02:41:38 +04:00
}
2015-08-21 06:10:46 +03:00
export function getKindName(kind: Kind): string {
return (<any>J2ME).Kind[kind];
}
export function isTwoSlot(kind: Kind) {
return kind === Kind.Long || kind === Kind.Double;
}
export var valueKinds = [
Kind.Boolean,
Kind.Char,
Kind.Float,
Kind.Double,
Kind.Byte,
Kind.Short,
Kind.Int,
Kind.Long
];
2015-05-08 01:17:05 +03:00
export function returnKind(op: Bytecodes): Kind {
switch (op) {
case Bytecodes.LRETURN:
return Kind.Long;
case Bytecodes.DRETURN:
return Kind.Double;
case Bytecodes.IRETURN:
return Kind.Int;
case Bytecodes.FRETURN:
return Kind.Float;
case Bytecodes.ARETURN:
return Kind.Reference;
case Bytecodes.RETURN:
return Kind.Void;
}
}
2014-10-22 08:53:53 +04:00
export function stackKind(kind: Kind): Kind {
switch (kind) {
case Kind.Boolean: return Kind.Int;
case Kind.Byte: return Kind.Int;
case Kind.Short: return Kind.Int;
case Kind.Char: return Kind.Int;
case Kind.Int: return Kind.Int;
case Kind.Float: return Kind.Float;
case Kind.Long: return Kind.Long;
case Kind.Double: return Kind.Double;
case Kind.Reference: return Kind.Reference;
default: throw Debug.unexpected("Unknown stack kind: " + kind);
}
}
2015-05-16 21:08:48 +03:00
export function kindCharacterToKind(kindCharacter: string): Kind {
switch (kindCharacter[0]) {
2015-05-16 21:08:48 +03:00
case 'Z': return Kind.Boolean;
case 'B': return Kind.Byte;
case 'S': return Kind.Short;
case 'C': return Kind.Char;
case 'I': return Kind.Int;
case 'F': return Kind.Float;
case 'J': return Kind.Long;
case 'D': return Kind.Double;
case 'V': return Kind.Void;
2015-05-20 03:22:18 +03:00
case '[': // Fallthrough
case 'L': return Kind.Reference;
2015-05-16 21:08:48 +03:00
default: throw Debug.unexpected("Unknown kind character: " + kindCharacter);
}
}
2014-10-22 08:53:53 +04:00
export function arrayTypeCodeToKind(typeCode): Kind {
switch (typeCode) {
case 4: return Kind.Boolean;
case 5: return Kind.Char;
case 6: return Kind.Float;
case 7: return Kind.Double;
case 8: return Kind.Byte;
case 9: return Kind.Short;
case 10: return Kind.Int;
case 11: return Kind.Long;
default: throw Debug.unexpected("Unknown array type code: " + typeCode);
}
}
2015-05-09 07:14:44 +03:00
export function kindSize(kind: Kind): number {
if (isTwoSlot(kind)) {
return 8;
}
return 4;
}
export function getKindCheck(kind: Kind): (l: any, h: number) => boolean {
switch (kind) {
case Kind.Boolean:
return (l, h) => l === 0 || l === 1;
case Kind.Byte:
return (l, h) => (l | 0) === l && l >= Constants.BYTE_MIN && l <= Constants.BYTE_MAX;
case Kind.Short:
return (l, h) => (l | 0) === l && l >= Constants.SHORT_MIN && l <= Constants.SHORT_MAX;
case Kind.Char:
return (l, h) => (l | 0) === l && l >= Constants.CHAR_MIN && l <= Constants.CHAR_MAX;
case Kind.Int:
return (l, h) => (l | 0) === l;
case Kind.Float:
return (l, h) => (l | 0) === l;
case Kind.Long:
return (l, h) => ((l | 0) === l) && ((h | 0) === h);
case Kind.Double:
return (l, h) => ((l | 0) === l) && ((h | 0) === h);
case Kind.Reference:
return (l, h) => l === null || l instanceof Object;
case Kind.Void:
return (l, h) => typeof l === "undefined";
default:
throw Debug.unexpected("Unknown kind: " + kind);
}
}
2015-03-11 23:05:03 +03:00
export function getSignatureKind(signature: Uint8Array): Kind {
switch (signature[0]) {
2015-03-11 23:05:03 +03:00
case UTF8Chars.Z:
return Kind.Boolean;
2015-03-11 23:05:03 +03:00
case UTF8Chars.B:
return Kind.Byte;
2015-03-11 23:05:03 +03:00
case UTF8Chars.S:
return Kind.Short;
2015-03-11 23:05:03 +03:00
case UTF8Chars.C:
return Kind.Char;
2015-03-11 23:05:03 +03:00
case UTF8Chars.I:
return Kind.Int;
2015-03-11 23:05:03 +03:00
case UTF8Chars.F:
return Kind.Float;
2015-03-11 23:05:03 +03:00
case UTF8Chars.J:
return Kind.Long;
2015-03-11 23:05:03 +03:00
case UTF8Chars.D:
return Kind.Double;
2015-03-11 23:05:03 +03:00
case UTF8Chars.OpenBracket:
case UTF8Chars.L:
return Kind.Reference;
2015-03-11 23:05:03 +03:00
case UTF8Chars.V:
return Kind.Void;
}
}
/**
* MethodDescriptor:
* ( ParameterDescriptor* ) ReturnDescriptor
* ParameterDescriptor:
* FieldType
* ReturnDescriptor:
* FieldType
* VoidDescriptor
* VoidDescriptor:
* V
* FieldDescriptor:
* FieldType
* FieldType:
* BaseType
* ObjectType
* ArrayType
* BaseType:
* B
* C
* D
* F
* I
* J
* S
* Z
* ObjectType:
* L ClassName ;
* ArrayType:
* [ ComponentType
* ComponentType:
* FieldType
*/
// Global state for signature parsing, kind of hackish but fast.
var globalNextIndex = 0;
var descriptorKinds = [];
/**
2015-03-14 01:17:28 +03:00
* Returns an array of kinds that appear in a method signature. The first element is always the
* return kind. The returned array is shared, so you if you need a copy of it, you'll need to
* clone it.
*
* The parsing algorithm needs some global state to keep track of the current position in the
* descriptor, namely |globalNextIndex| which always points to the next index in the descriptor
* after a token has been consumed.
*/
export function parseMethodDescriptorKinds(value: Uint8Array, startIndex: number): Kind [] {
globalNextIndex = 0;
if ((startIndex > value.length - 3) || value[startIndex] !== UTF8Chars.OpenParenthesis) {
assert(false, "Invalid method signature.");
}
descriptorKinds.length = 0;
descriptorKinds.push(Kind.Void); // placeholder until the return type is parsed
var i = startIndex + 1;
while (value[i] !== UTF8Chars.CloseParenthesis) {
var kind = parseTypeDescriptorKind(value, i);
descriptorKinds.push(kind);
i = globalNextIndex;
if (i >= value.length) {
assert(false, "Invalid method signature.");
}
}
i++;
var kind = parseTypeDescriptorKind(value, i);
if (globalNextIndex !== value.length) {
assert(false, "Invalid method signature.");
}
// Plug in the return type
descriptorKinds[0] = kind;
return descriptorKinds;
}
function parseTypeDescriptorKind(value: Uint8Array, startIndex: number): Kind {
globalNextIndex = startIndex + 1;
switch (value[startIndex]) {
case UTF8Chars.Z:
return Kind.Boolean;
case UTF8Chars.B:
return Kind.Byte;
case UTF8Chars.C:
return Kind.Char;
case UTF8Chars.D:
return Kind.Double;
case UTF8Chars.F:
return Kind.Float;
case UTF8Chars.I:
return Kind.Int;
case UTF8Chars.J:
return Kind.Long;
case UTF8Chars.S:
return Kind.Short;
case UTF8Chars.V:
return Kind.Void;
case UTF8Chars.L: {
// parse a slashified Java class name
var endIndex = parseClassNameKind(value, startIndex + 1, UTF8Chars.Slash);
if (endIndex > startIndex + 1 && endIndex < value.length && value[endIndex] === UTF8Chars.Semicolon) {
globalNextIndex = endIndex + 1;
return Kind.Reference;
}
2015-03-14 01:17:28 +03:00
Debug.unexpected("Invalid signature.");
}
case UTF8Chars.OpenBracket: {
// compute the number of dimensions
var index = startIndex;
while (index < value.length && value[index] === UTF8Chars.OpenBracket) {
index++;
}
var dimensions = index - startIndex;
if (dimensions > 255) {
2015-03-14 01:17:28 +03:00
Debug.unexpected("Array with more than 255 dimensions.");
}
var component = parseTypeDescriptorKind(value, index);
return Kind.Reference;
}
default:
2015-03-14 01:17:28 +03:00
Debug.unexpected("Unexpected type descriptor prefix: " + value[startIndex]);
}
}
function parseClassNameKind(value: Uint8Array, index: number, separator: number): number {
var position = index;
var length = value.length;
while (position < length) {
var nextch = value[position];
if (nextch === UTF8Chars.Dot || nextch === UTF8Chars.Slash) {
if (separator !== nextch) {
return position;
}
} else if (nextch === UTF8Chars.Semicolon || nextch === UTF8Chars.OpenBracket) {
return position;
}
position++;
}
return position;
}
export function signatureHasTwoSlotArguments(signatureKinds: Kind []): boolean {
for (var i = 1; i < signatureKinds.length; i++) {
if (isTwoSlot(signatureKinds[i])) {
return true;
}
}
return false;
}
export function signatureArgumentSlotCount(signatureKinds: Kind []): number {
var count = 0;
for (var i = 1; i < signatureKinds.length; i++) {
count += isTwoSlot(signatureKinds[i]) ? 2 : 1;
}
return count;
}
2015-01-14 03:47:42 +03:00
}