diff --git a/src/lower.ts b/src/lower.ts index e35e419..bfae5d7 100644 --- a/src/lower.ts +++ b/src/lower.ts @@ -102,7 +102,7 @@ function appendData(cx: Context, newData: Uint8Array): number { return 0; } else { console.log("appending", newData); - + const data = datas[0]; const idx = data.init.length; const init = new Uint8Array(data.init.length + newData.length); @@ -970,7 +970,11 @@ function lowerExpr( // TODO: scratch locals... const ptrLocal = fcx.wasmType.params.length + fcx.wasm.locals.length; 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. expr.fields.forEach((field, i) => { @@ -1169,13 +1173,15 @@ function sizeOfValtype(type: wasm.ValType): number { export function layoutOfStruct(ty: TyStruct): StructLayout { const fieldWasmTys = ty.fields.map(([, field]) => wasmTypeForBody(field)); + // TODO: Use the max alignment instead. const align = fieldWasmTys.some((field) => field.some((type) => type === "i64") ) ? 8 : 4; - let offset = 0; + // i32 refcount is at offset 0. + let offset = 4; const fields: StructFieldLayout[] = fieldWasmTys.map((field, i) => { const value: StructFieldLayout = { diff --git a/std.nil b/std.nil index d140594..74a4716 100644 --- a/std.nil +++ b/std.nil @@ -98,12 +98,12 @@ mod rt ( // 16 pages, very arbitrary. let result = __memory_grow(16_I32); // 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"); ); ); - 0_I32 + actualObjPtr ); );