Skip to content

Commit

Permalink
Finish u64/Result support.
Browse files Browse the repository at this point in the history
  • Loading branch information
kazcw committed Sep 27, 2023
1 parent e93ce83 commit d9ac1c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/gui2/parser-codegen/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ class Type {
case 'i32':
return new Type(tsf.createTypeReferenceNode('number'), cursorMethods.readI32, 4)
case 'u64':
return new Type(tsf.createTypeReferenceNode('number'), cursorMethods.readU64, 8)
return new Type(tsf.createTypeReferenceNode('bigint'), cursorMethods.readU64, 8)
case 'i64':
return new Type(tsf.createTypeReferenceNode('number'), cursorMethods.readI64, 8)
return new Type(tsf.createTypeReferenceNode('bigint'), cursorMethods.readI64, 8)
case 'char':
return new Type(tsf.createTypeReferenceNode('number'), cursorMethods.readU32, 4)
case 'string':
Expand Down
21 changes: 11 additions & 10 deletions app/gui2/src/util/parserSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class LazyObject {
}
}

export type Result<T, E> = { Ok: T } | { Err: E }

export const builtin = {
Array: Array,
} as const
Expand Down Expand Up @@ -45,14 +47,14 @@ export class Cursor {
}
}

readResult<Ok, Err>(readOk: (cursor: Cursor) => Ok, readErr: (cursor: Cursor) => Err): Ok | Err {
readResult<Ok, Err>(readOk: (cursor: Cursor) => Ok, readErr: (cursor: Cursor) => Err): Result<Ok, Err> {
const data = this.readPointer()
const discriminant = data.readU32()
switch (discriminant) {
case 0:
return readOk(data.seek(4))
return { Ok: readOk(data.seek(4)) }
case 1:
return readErr(data.seek(4))
return { Err: readErr(data.seek(4)) }
default:
throw new Error(`Invalid Result discriminant: 0x${discriminant.toString(16)}.`)
}
Expand All @@ -74,13 +76,12 @@ export class Cursor {
return this.blob.getInt32(0, true)!
}

readU64(): number {
const lo = this.readU32()
const hi = this.seek(4).readU32()
//if (hi !== 0) {
// throw new RangeError()
//}
return lo
readU64(): bigint {
return this.blob.getBigUint64(0, true)!
}

readI64(): bigint {
return this.blob.getBigInt64(0, true)!
}

readBool(): boolean {
Expand Down

0 comments on commit d9ac1c8

Please sign in to comment.