fix struct literals

This commit is contained in:
nora 2023-08-01 10:42:46 +02:00
parent b0b92dae0f
commit 1551847d8c
2 changed files with 11 additions and 5 deletions

View file

@ -102,7 +102,7 @@ function appendData(cx: Context, newData: Uint8Array): number {
return 0; return 0;
} else { } else {
console.log("appending", newData); console.log("appending", newData);
const data = datas[0]; const data = datas[0];
const idx = data.init.length; const idx = data.init.length;
const init = new Uint8Array(data.init.length + newData.length); const init = new Uint8Array(data.init.length + newData.length);
@ -970,7 +970,11 @@ function lowerExpr(
// TODO: scratch locals... // TODO: scratch locals...
const ptrLocal = fcx.wasmType.params.length + fcx.wasm.locals.length; const ptrLocal = fcx.wasmType.params.length + fcx.wasm.locals.length;
fcx.wasm.locals.push("i32"); fcx.wasm.locals.push("i32");
instrs.push({ kind: "local.set", imm: ptrLocal }); instrs.push({ kind: "local.tee", imm: ptrLocal });
// Store the refcount
instrs.push({ kind: "i32.const", imm: 0n });
instrs.push({ kind: "i32.store", imm: { align: 4 } });
// Now, set all fields. // Now, set all fields.
expr.fields.forEach((field, i) => { expr.fields.forEach((field, i) => {
@ -1169,13 +1173,15 @@ function sizeOfValtype(type: wasm.ValType): number {
export function layoutOfStruct(ty: TyStruct): StructLayout { export function layoutOfStruct(ty: TyStruct): StructLayout {
const fieldWasmTys = ty.fields.map(([, field]) => wasmTypeForBody(field)); const fieldWasmTys = ty.fields.map(([, field]) => wasmTypeForBody(field));
// TODO: Use the max alignment instead.
const align = fieldWasmTys.some((field) => const align = fieldWasmTys.some((field) =>
field.some((type) => type === "i64") field.some((type) => type === "i64")
) )
? 8 ? 8
: 4; : 4;
let offset = 0; // i32 refcount is at offset 0.
let offset = 4;
const fields: StructFieldLayout[] = fieldWasmTys.map((field, i) => { const fields: StructFieldLayout[] = fieldWasmTys.map((field, i) => {
const value: StructFieldLayout = { const value: StructFieldLayout = {

View file

@ -98,12 +98,12 @@ mod rt (
// 16 pages, very arbitrary. // 16 pages, very arbitrary.
let result = __memory_grow(16_I32); let result = __memory_grow(16_I32);
// If allocation failed we get -1. We don't have negative numbers yet, lol. // If allocation failed we get -1. We don't have negative numbers yet, lol.
if result > 100000000_I32 then ( if result > 4294967295_I32 then (
std.abort("failed to grow memory"); std.abort("failed to grow memory");
); );
); );
0_I32 actualObjPtr
); );
); );