Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some definisions and constants for Buffer #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { BLOCK_MAXSIZE, BLOCK, BLOCK_OVERHEAD } from "rt/common";
import { E_INVALIDLENGTH, E_INDEXOUTOFRANGE } from "util/error";
import { Uint8Array } from "typedarray";

export const kMaxLength = constants.MAX_LENGTH;
export const kMaxStringLength = constants.MAX_STRING_LENGTH;

export namespace constants {
export const MAX_LENGTH = i32.MAX_VALUE; // (2 ^ 31) - 1 (~2GB)
export const MAX_STRING_LENGTH = BLOCK_MAXSIZE >>> alignof<u16>();
}

export class Buffer extends Uint8Array {
constructor(size: i32) {
super(size);
Expand Down Expand Up @@ -250,7 +258,7 @@ export class Buffer extends Uint8Array {
}
return this;
}

swap32(): Buffer {
let dataLength = this.dataLength;
// Make sure dataLength is divisible by 4
Expand All @@ -263,7 +271,7 @@ export class Buffer extends Uint8Array {
}
return this;
}

swap64(): Buffer {
let dataLength = this.dataLength;
// Make sure dataLength is divisible by 8
Expand Down
16 changes: 16 additions & 0 deletions assembly/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,19 @@ declare namespace Buffer {
export function decodeUnsafe(ptr: usize, byteLength: i32): string;
}
}

declare module "buffer" {
export type TranscodeEncoding = "ascii" | "utf8" | "utf16le" | "ucs2" | "latin1" | "binary";

export const kMaxLength: i32;
export const kStringMaxLength: i32;

export const constants: {
MAX_LENGTH: i32;
MAX_STRING_LENGTH: i32;
};

// To export the buffer, we must obtain the `typeof Buffer`
const BuffType: typeof Buffer;
export { BuffType as Buffer };
}