make std/string test ok again

This commit is contained in:
dcode 2019-03-16 14:48:22 +01:00
parent 05a35f42f6
commit 0c388ca4c6
9 changed files with 3929 additions and 4779 deletions

View File

@ -62,11 +62,12 @@ import {
Field,
Global,
DecoratorFlags,
ClassPrototype
ClassPrototype,
Local
} from "./program";
import {
FlowFlags
FlowFlags, Flow
} from "./flow";
import {
@ -4214,14 +4215,21 @@ export function compileArraySet(
valueExpression: Expression,
contextualType: Type
): ExpressionRef {
var program = compiler.program;
var type = typedArraySymbolToType(target.internalName);
if (type) {
return compileTypedArraySet(compiler, target, type, thisExpression,
elementExpression, valueExpression, contextualType);
if (!type) {
assert(target.prototype == program.arrayPrototype);
type = assert(target.typeArguments)[0];
}
assert(target.prototype == compiler.program.arrayPrototype);
type = assert(target.typeArguments)[0];
throw new Error("not implemented");
return compileTypedArraySet(
compiler,
target,
type,
thisExpression,
elementExpression,
valueExpression,
contextualType
);
}
function compileTypedArraySet(
@ -4233,7 +4241,6 @@ function compileTypedArraySet(
valueExpression: Expression,
contextualType: Type
): ExpressionRef {
var type = typedArraySymbolToType(target.internalName);
var module = compiler.module;
var dataStart = assert(target.lookupInSelf("dataStart"));
@ -4267,16 +4274,22 @@ function compileTypedArraySet(
}
}
var typeIsManaged = type.is(TypeFlags.REFERENCE); // FIXME: .isManaged
var usizeType = compiler.options.usizeType;
var nativeSizeType = compiler.options.nativeSizeType;
var thisExpr = compiler.compileExpression(
thisExpression,
target.type,
ConversionKind.IMPLICIT,
WrapMode.NONE
);
var tempThis: Local | null = null;
if (typeIsManaged) {
tempThis = compiler.currentFlow.getTempLocal(target.type, false);
thisExpr = module.createTeeLocal(tempThis.index, thisExpr);
}
var dataStartExpr = module.createLoad(usizeType.byteSize, true,
compiler.compileExpression(
thisExpression,
target.type,
ConversionKind.IMPLICIT,
WrapMode.NONE
),
nativeSizeType, (<Field>dataStart).memoryOffset
thisExpr, nativeSizeType, (<Field>dataStart).memoryOffset
);
var typeAlignLog2 = type.alignLog2;
@ -4316,10 +4329,33 @@ function compileTypedArraySet(
WrapMode.NONE
);
// clamp
if (target.internalName == BuiltinSymbols.Uint8ClampedArray) {
// handle Array<Ref>: value = LINK<T, TArray>(value, this), value
if (typeIsManaged) {
let program = compiler.program;
let linkPrototype = assert(program.linkPrototype);
let linkInstance = compiler.resolver.resolveFunction(linkPrototype, [ type, target.type ]);
if (!linkInstance) return module.createUnreachable();
let previousFlow = compiler.currentFlow;
let tempValue = previousFlow.getTempLocal(type, false);
let flow = Flow.createInline(previousFlow.parentFunction, linkInstance);
compiler.currentFlow = flow;
flow.addScopedAlias(linkInstance.signature.getParameterName(0), type, tempValue.index);
flow.addScopedAlias(linkInstance.signature.getParameterName(1), target.type, assert(tempThis).index);
let body = compiler.compileFunctionBody(linkInstance);
body.unshift(
module.createSetLocal(tempValue.index, valueExpr)
);
body.push(
module.createGetLocal(tempValue.index, nativeSizeType)
);
previousFlow.freeTempLocal(tempValue);
previousFlow.freeTempLocal(tempThis!); tempThis = null;
compiler.currentFlow = previousFlow;
valueExpr = module.createBlock(flow.inlineReturnLabel, body, nativeSizeType);
// handle Uint8ClampedArray: value = ~(value >> 31) & (((255 - value) >> 31) | value)
} else if (target.internalName == BuiltinSymbols.Uint8ClampedArray) {
let tempLocal = compiler.currentFlow.getAndFreeTempLocal(Type.i32, true);
// ~(value >> 31) & (((255 - value) >> 31) | value)
valueExpr = module.createBinary(BinaryOp.AndI32,
module.createBinary(BinaryOp.XorI32,
module.createBinary(BinaryOp.ShrI32,
@ -4340,6 +4376,7 @@ function compileTypedArraySet(
)
);
}
assert(!tempThis);
var nativeType = type.toNativeType();

View File

@ -1065,7 +1065,7 @@ export class Compiler extends DiagnosticEmitter {
}
/** Compiles the body of a function within the specified flow. */
private compileFunctionBody(instance: Function): ExpressionRef[] {
compileFunctionBody(instance: Function): ExpressionRef[] {
var module = this.module;
var bodyNode = assert(instance.prototype.bodyNode);
var returnType = instance.signature.returnType;
@ -4689,22 +4689,22 @@ export class Compiler extends DiagnosticEmitter {
case ElementKind.CLASS: {
let elementExpression = resolver.currentElementExpression;
if (elementExpression) { // indexed access
let arrayBufferView = this.program.arrayBufferViewInstance;
if (arrayBufferView) {
if ((<Class>target).prototype.extends(arrayBufferView.prototype)) {
return compileArraySet(
this,
<Class>target,
assert(this.resolver.currentThisExpression),
elementExpression,
valueExpression,
contextualType
);
}
}
let isUnchecked = flow.is(FlowFlags.UNCHECKED_CONTEXT);
let indexedSet = (<Class>target).lookupOverload(OperatorKind.INDEXED_SET, isUnchecked);
if (!indexedSet) {
let arrayBufferView = this.program.arrayBufferViewInstance;
if (arrayBufferView) {
if ((<Class>target).prototype.extends(arrayBufferView.prototype)) {
return compileArraySet(
this,
<Class>target,
assert(this.resolver.currentThisExpression),
elementExpression,
valueExpression,
contextualType
);
}
}
let indexedGet = (<Class>target).lookupOverload(OperatorKind.INDEXED_GET, isUnchecked);
if (!indexedGet) {
this.error(

View File

@ -280,8 +280,8 @@ export abstract class DiagnosticEmitter {
var message = DiagnosticMessage.create(code, category, arg0, arg1, arg2).withRange(range);
if (relatedRange) message.relatedRange = relatedRange;
this.diagnostics.push(message);
// console.log(formatDiagnosticMessage(message, true, true) + "\n"); // temporary
// console.log(<string>new Error("stack").stack);
console.log(formatDiagnosticMessage(message, true, true) + "\n"); // temporary
console.log(<string>new Error("stack").stack);
}
/** Emits an informatory diagnostic message. */

View File

@ -346,12 +346,14 @@ export class Array<T> extends ArrayBufferView {
}
reverse(): Array<T> {
var base = this.dataStart;
for (let front = 0, back = this.length_ - 1; front < back; ++front, --back) {
let temp = load<T>(base, front);
let dest = base + (<usize>back << alignof<T>());
store<T>(base + (<usize>front << alignof<T>()), load<T>(dest));
store<T>(dest, temp);
var front = this.dataStart;
var back = this.dataEnd - sizeof<T>();
while (front < back) {
let temp = load<T>(front);
store<T>(front, load<T>(back));
store<T>(back, temp);
front += sizeof<T>();
back -= sizeof<T>();
}
return this;
}

View File

@ -192,7 +192,7 @@ function assertUnregistered(ref: usize): void {
/** Asserts that a managed object has already been registered. */
// @ts-ignore: decorator
function assertRegistered(ref: usize): void {
assert(ref > HEAP_BASE); // must be a heap object
// may be a static string or buffer (not a heap object)
assert(changetype<HEADER>(ref - HEADER_SIZE).classId != HEADER_MAGIC);
}

View File

@ -1,4 +1,4 @@
import { ALLOCATE, REGISTER, HEADER, HEADER_SIZE } from "./runtime";
import { ALLOCATE, REGISTER, HEADER, HEADER_SIZE, ArrayBufferView, LINK } from "./runtime";
import { MAX_SIZE_32 } from "./util/allocator";
import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./util/string";
@ -359,22 +359,20 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
// split by chars
length = min<isize>(length, <isize>limit);
let result = new Array<String>(length);
let buffer = unreachable(); // TODO
// let buffer = <ArrayBuffer>result.buffer_;
let resultStart = changetype<ArrayBufferView>(result).dataStart;
for (let i: isize = 0; i < length; ++i) {
let char = ALLOCATE(2);
let charStr = ALLOCATE(2);
store<u16>(
changetype<usize>(char),
load<u16>(
changetype<usize>(this) + (<usize>i << 1)
)
charStr,
load<u16>(changetype<usize>(this) + (<usize>i << 1))
);
store<usize>(changetype<usize>(buffer) + (<usize>i << 1), char);
store<String>(resultStart + (<usize>i << alignof<String>()), REGISTER<String>(charStr));
LINK(charStr, result);
}
return result;
} else if (!length) {
let result = new Array<String>(1);
unchecked(result[0] = changetype<String>(""));
store<string>(changetype<ArrayBufferView>(result).dataStart, ""); // no need to register/link
return result;
}
var result = new Array<String>();

View File

@ -1,12 +1,12 @@
import "allocator/arena";
import { Array } from "array";
import { COMPARATOR } from "internal/sort";
import { COMPARATOR } from "util/sort";
// Obtains the internal capacity of an array from its backing buffer.
function internalCapacity<T>(array: Array<T>): i32 {
// the memory region used by the backing buffer might still be larger in that the ArrayBuffer
// pre-allocates a power of 2 sized buffer itself and reuses it as long as it isn't exceeded.
var buffer: ArrayBuffer = array.buffer_;
var buffer: ArrayBuffer = array.data;
return buffer.byteLength >> alignof<T>();
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff