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

Use BigInt for i64 and u64 based types #9380

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions bindings/generator/templates/binding_web_meths.rs.j2
Original file line number Diff line number Diff line change
Expand Up @@ -501,28 +501,28 @@ JsValue::from({{ rs_value }})
{%- elif type.kind == "i64_based" -%}
{%- if type.custom_to_rs_i64 %}
{
let custom_to_rs_i64 = {{ type.custom_to_rs_i32 }};
let custom_to_rs_i64 = {{ type.custom_to_rs_i64 }};
let v = match custom_to_rs_i64({{ rs_value }}) {
Ok(ok) => ok,
Err(err) => return Err(JsValue::from(TypeError::new(err.as_ref()))),
};
JsValue::from(v)
}
{% else -%}
JsValue::from({{ rs_value }})
JsValue::from(BigInt::from({{ rs_value }}))
{%- endif -%}
{%- elif type.kind == "u64_based" -%}
{%- if type.custom_to_rs_u64 %}
{
let custom_to_rs_u64 = {{ type.custom_to_rs_i32 }};
let custom_to_rs_u64 = {{ type.custom_to_rs_u64 }};
let v = match custom_to_rs_u64({{ rs_value }}) {
Ok(ok) => ok,
Err(err) => return Err(JsValue::from(TypeError::new(err.as_ref()))),
};
JsValue::from(v)
}
{% else -%}
JsValue::from({{ rs_value }})
JsValue::from(BigInt::from({{ rs_value }}))
{%- endif -%}
{%- elif type.kind == "f64_based" -%}
{%- if type.custom_to_rs_f64 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ export type {{ type.name }} = Uint8Array
{% endif %}
{% endfor %}
{# Number-based types #}
{% for type in api.u8_based_types + api.i32_based_types + api.u32_based_types + api.i64_based_types + api.u64_based_types + api.f64_based_types %}
{% for type in api.u64_based_types + api.i64_based_types %}
{% if type.custom_ts_type_declaration %}
{{ type.custom_ts_type_declaration }}
{% else %}
export type {{ type.name }} = bigint
{% endif %}
{% endfor %}
{% for type in api.u8_based_types + api.i32_based_types + api.u32_based_types + api.f64_based_types %}
{% if type.custom_ts_type_declaration %}
{{ type.custom_ts_type_declaration }}
{% else %}
Expand Down
24 changes: 12 additions & 12 deletions bindings/web/src/meths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ fn struct_file_stat_rs_to_js(rs_obj: libparsec::FileStat) -> Result<JsValue, JsV
Reflect::set(&js_obj, &"isPlaceholder".into(), &js_is_placeholder)?;
let js_need_sync = rs_obj.need_sync.into();
Reflect::set(&js_obj, &"needSync".into(), &js_need_sync)?;
let js_size = JsValue::from(rs_obj.size);
let js_size = JsValue::from(BigInt::from(rs_obj.size));
Reflect::set(&js_obj, &"size".into(), &js_size)?;
Ok(js_obj)
}
Expand Down Expand Up @@ -3457,7 +3457,7 @@ fn struct_workspace_history_file_stat_rs_to_js(
Reflect::set(&js_obj, &"updated".into(), &js_updated)?;
let js_version = JsValue::from(rs_obj.version);
Reflect::set(&js_obj, &"version".into(), &js_version)?;
let js_size = JsValue::from(rs_obj.size);
let js_size = JsValue::from(BigInt::from(rs_obj.size));
Reflect::set(&js_obj, &"size".into(), &js_size)?;
Ok(js_obj)
}
Expand Down Expand Up @@ -3677,7 +3677,7 @@ fn variant_active_users_limit_rs_to_js(
match rs_obj {
libparsec::ActiveUsersLimit::LimitedTo(x1, ..) => {
Reflect::set(&js_obj, &"tag".into(), &"LimitedTo".into())?;
let js_x1 = JsValue::from(x1);
let js_x1 = JsValue::from(BigInt::from(x1));
Reflect::set(&js_obj, &"x1".into(), &js_x1.into())?;
}
libparsec::ActiveUsersLimit::NoLimit { .. } => {
Expand Down Expand Up @@ -5190,11 +5190,11 @@ fn variant_client_event_rs_to_js(rs_obj: libparsec::ClientEvent) -> Result<JsVal
.as_ref()
});
Reflect::set(&js_obj, &"entryId".into(), &js_entry_id)?;
let js_blocks = JsValue::from(blocks);
let js_blocks = JsValue::from(BigInt::from(blocks));
Reflect::set(&js_obj, &"blocks".into(), &js_blocks)?;
let js_block_index = JsValue::from(block_index);
let js_block_index = JsValue::from(BigInt::from(block_index));
Reflect::set(&js_obj, &"blockIndex".into(), &js_block_index)?;
let js_blocksize = JsValue::from(blocksize);
let js_blocksize = JsValue::from(BigInt::from(blocksize));
Reflect::set(&js_obj, &"blocksize".into(), &js_blocksize)?;
}
libparsec::ClientEvent::WorkspaceOpsOutboundSyncStarted {
Expand Down Expand Up @@ -6968,7 +6968,7 @@ fn variant_entry_stat_rs_to_js(rs_obj: libparsec::EntryStat) -> Result<JsValue,
Reflect::set(&js_obj, &"isPlaceholder".into(), &js_is_placeholder)?;
let js_need_sync = need_sync.into();
Reflect::set(&js_obj, &"needSync".into(), &js_need_sync)?;
let js_size = JsValue::from(size);
let js_size = JsValue::from(BigInt::from(size));
Reflect::set(&js_obj, &"size".into(), &js_size)?;
}
libparsec::EntryStat::Folder {
Expand Down Expand Up @@ -9759,7 +9759,7 @@ fn variant_parsed_parsec_addr_rs_to_js(
.as_ref()
});
Reflect::set(&js_obj, &"workspaceId".into(), &js_workspace_id)?;
let js_key_index = JsValue::from(key_index);
let js_key_index = JsValue::from(BigInt::from(key_index));
Reflect::set(&js_obj, &"keyIndex".into(), &js_key_index)?;
let js_encrypted_path = JsValue::from(Uint8Array::from(encrypted_path.as_ref()));
Reflect::set(&js_obj, &"encryptedPath".into(), &js_encrypted_path)?;
Expand Down Expand Up @@ -12139,7 +12139,7 @@ fn variant_workspace_history_entry_stat_rs_to_js(
Reflect::set(&js_obj, &"updated".into(), &js_updated)?;
let js_version = JsValue::from(version);
Reflect::set(&js_obj, &"version".into(), &js_version)?;
let js_size = JsValue::from(size);
let js_size = JsValue::from(BigInt::from(size));
Reflect::set(&js_obj, &"size".into(), &js_size)?;
}
libparsec::WorkspaceHistoryEntryStat::Folder {
Expand Down Expand Up @@ -16727,7 +16727,7 @@ pub fn workspaceFdWrite(workspace: u32, fd: u32, offset: u64, data: Uint8Array)
Ok(value) => {
let js_obj = Object::new().into();
Reflect::set(&js_obj, &"ok".into(), &true.into())?;
let js_value = JsValue::from(value);
let js_value = JsValue::from(BigInt::from(value));
Reflect::set(&js_obj, &"value".into(), &js_value)?;
js_obj
}
Expand Down Expand Up @@ -16767,7 +16767,7 @@ pub fn workspaceFdWriteConstrainedIo(
Ok(value) => {
let js_obj = Object::new().into();
Reflect::set(&js_obj, &"ok".into(), &true.into())?;
let js_value = JsValue::from(value);
let js_value = JsValue::from(BigInt::from(value));
Reflect::set(&js_obj, &"value".into(), &js_value)?;
js_obj
}
Expand Down Expand Up @@ -16800,7 +16800,7 @@ pub fn workspaceFdWriteStartEof(workspace: u32, fd: u32, data: Uint8Array) -> Pr
Ok(value) => {
let js_obj = Object::new().into();
Reflect::set(&js_obj, &"ok".into(), &true.into())?;
let js_value = JsValue::from(value);
let js_value = JsValue::from(BigInt::from(value));
Reflect::set(&js_obj, &"value".into(), &js_value)?;
js_obj
}
Expand Down
4 changes: 2 additions & 2 deletions client/src/common/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ function size(bytes: number, system: [number, string][]): Translatable {
return { key: key, data: { size: formattedAmount } };
}

export function formatFileSize(bytesize: number): Translatable {
export function formatFileSize(bytesize: bigint | number): Translatable {
const SYSTEM: [number, string][] = [
[Math.pow(1024, 0), 'common.filesize.bytes'],
[Math.pow(1024, 1), 'common.filesize.kilobytes'],
[Math.pow(1024, 2), 'common.filesize.megabytes'],
[Math.pow(1024, 3), 'common.filesize.gigabytes'],
[Math.pow(1024, 4), 'common.filesize.terabytes'],
];
return size(bytesize, SYSTEM);
return size(Number(bytesize), SYSTEM);
}

/* File icons */
Expand Down
6 changes: 3 additions & 3 deletions client/src/common/fileTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function detectFileContentType(workspaceHandle: WorkspaceHandle, path: FsP
return { type: FileContentType.Text, extension: ext, mimeType: TEXTS.get(ext) as string };
}

const READ_CHUNK_SIZE = 512;
const READ_CHUNK_SIZE = 512n;
let fd: FileDescriptor | null = null;
try {
let openResult;
Expand All @@ -110,9 +110,9 @@ async function detectFileContentType(workspaceHandle: WorkspaceHandle, path: FsP
fd = openResult.value;
let readResult;
if (at) {
readResult = await readHistoryFile(workspaceHandle, fd, 0, READ_CHUNK_SIZE);
readResult = await readHistoryFile(workspaceHandle, fd, 0n, READ_CHUNK_SIZE);
} else {
readResult = await readFile(workspaceHandle, fd, 0, READ_CHUNK_SIZE);
readResult = await readFile(workspaceHandle, fd, 0n, READ_CHUNK_SIZE);
}
if (!readResult.ok) {
return;
Expand Down
49 changes: 25 additions & 24 deletions client/src/parsec/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
WorkspaceStatEntryError,
WorkspaceStatFolderChildrenError,
} from '@/parsec/types';
import type { U64 } from '@/plugins/libparsec';
import { MoveEntryModeTag, libparsec } from '@/plugins/libparsec';
import { DateTime } from 'luxon';

Expand Down Expand Up @@ -277,7 +278,7 @@ export async function closeFile(workspaceHandle: WorkspaceHandle, fd: FileDescri
export async function resizeFile(
workspaceHandle: WorkspaceHandle,
fd: FileDescriptor,
length: number,
length: U64,
): Promise<Result<null, WorkspaceFdResizeError>> {
if (workspaceHandle && !needsMocks()) {
return await libparsec.workspaceFdResize(workspaceHandle, fd, length, true);
Expand All @@ -292,25 +293,25 @@ export async function resizeFile(
export async function writeFile(
workspaceHandle: WorkspaceHandle,
fd: FileDescriptor,
offset: number,
offset: U64,
data: Uint8Array,
): Promise<Result<number, WorkspaceFdWriteError>> {
): Promise<Result<U64, WorkspaceFdWriteError>> {
if (!needsMocks()) {
return await libparsec.workspaceFdWrite(workspaceHandle, fd, offset, data);
} else {
if (!MOCK_OPENED_FILES.has(fd)) {
return { ok: false, error: { tag: WorkspaceFdWriteErrorTag.BadFileDescriptor, error: 'Invalid file descriptor' } };
}
await wait(100);
return { ok: true, value: data.length };
return { ok: true, value: BigInt(data.length) };
}
}

export async function readFile(
workspaceHandle: WorkspaceHandle,
fd: FileDescriptor,
offset: number,
size: number,
offset: U64,
size: U64,
): Promise<Result<Uint8Array, WorkspaceFdReadError>> {
if (!needsMocks()) {
return await libparsec.workspaceFdRead(workspaceHandle, fd, offset, size);
Expand All @@ -325,29 +326,29 @@ export async function readFile(

switch (ext) {
case 'xlsx':
offset === 0 && console.log('Using XLSX content');
return { ok: true, value: MockFiles.XLSX.slice(offset, offset + size) };
offset === 0n && console.log('Using XLSX content');
return { ok: true, value: MockFiles.XLSX.slice(Number(offset), Number(offset + size)) };
case 'png':
offset === 0 && console.log('Using PNG content');
return { ok: true, value: MockFiles.PNG.slice(offset, offset + size) };
offset === 0n && console.log('Using PNG content');
return { ok: true, value: MockFiles.PNG.slice(Number(offset), Number(offset + size)) };
case 'docx':
offset === 0 && console.log('Using DOCX content');
return { ok: true, value: MockFiles.DOCX.slice(offset, offset + size) };
offset === 0n && console.log('Using DOCX content');
return { ok: true, value: MockFiles.DOCX.slice(Number(offset), Number(offset + size)) };
case 'txt':
offset === 0 && console.log('Using TXT content');
return { ok: true, value: MockFiles.TXT.slice(offset, offset + size) };
offset === 0n && console.log('Using TXT content');
return { ok: true, value: MockFiles.TXT.slice(Number(offset), Number(offset + size)) };
case 'py':
offset === 0 && console.log('Using PY content');
return { ok: true, value: MockFiles.PY.slice(offset, offset + size) };
offset === 0n && console.log('Using PY content');
return { ok: true, value: MockFiles.PY.slice(Number(offset), Number(offset + size)) };
case 'pdf':
offset === 0 && console.log('Using PDF content');
return { ok: true, value: MockFiles.PDF.slice(offset, offset + size) };
offset === 0n && console.log('Using PDF content');
return { ok: true, value: MockFiles.PDF.slice(Number(offset), Number(offset + size)) };
case 'mp3':
offset === 0 && console.log('Using MP3 content');
return { ok: true, value: MockFiles.MP3.slice(offset, offset + size) };
offset === 0n && console.log('Using MP3 content');
return { ok: true, value: MockFiles.MP3.slice(Number(offset), Number(offset + size)) };
case 'mp4':
offset === 0 && console.log('Using MP4 content');
return { ok: true, value: MockFiles.MP4.slice(offset, offset + size) };
offset === 0n && console.log('Using MP4 content');
return { ok: true, value: MockFiles.MP4.slice(Number(offset), Number(offset + size)) };
}
console.log('Using default file content');
return {
Expand All @@ -362,15 +363,15 @@ export async function readFile(
}

export interface EntryTree {
totalSize: number;
totalSize: U64;
entries: Array<EntryStatFile>;
maxRecursionReached: boolean;
maxFilesReached: boolean;
}

export async function listTree(workspaceHandle: WorkspaceHandle, path: FsPath, depthLimit = 12, filesLimit = 10000): Promise<EntryTree> {
async function _innerListTree(workspaceHandle: WorkspaceHandle, path: FsPath, depth: number): Promise<EntryTree> {
const tree: EntryTree = { totalSize: 0, entries: [], maxRecursionReached: false, maxFilesReached: false };
const tree: EntryTree = { totalSize: 0n, entries: [], maxRecursionReached: false, maxFilesReached: false };

if (depth > depthLimit) {
console.warn('Max depth reached for listTree');
Expand Down
9 changes: 5 additions & 4 deletions client/src/parsec/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
WorkspaceHistoryStatEntryError,
WorkspaceHistoryStatFolderChildrenError,
} from '@/parsec/types';
import type { U64 } from '@/plugins/libparsec';
import { libparsec } from '@/plugins/libparsec';
import { DateTime } from 'luxon';

Expand Down Expand Up @@ -125,8 +126,8 @@ export async function closeHistoryFile(
export async function readHistoryFile(
workspaceHandle: WorkspaceHandle,
fd: FileDescriptor,
offset: number,
size: number,
offset: U64,
size: U64,
): Promise<Result<ArrayBuffer, WorkspaceHistoryFdReadError>> {
if (!needsMocks()) {
return await libparsec.workspaceHistoryFdRead(workspaceHandle, fd, offset, size);
Expand Down Expand Up @@ -178,7 +179,7 @@ export async function readHistoryFile(
}

export interface HistoryEntryTree {
totalSize: number;
totalSize: U64;
entries: Array<WorkspaceHistoryEntryStatFile>;
maxRecursionReached: boolean;
maxFilesReached: boolean;
Expand All @@ -192,7 +193,7 @@ export async function listTreeAt(
filesLimit = 10000,
): Promise<HistoryEntryTree> {
async function _innerListTreeAt(workspaceHandle: WorkspaceHandle, path: FsPath, at: DateTime, depth: number): Promise<HistoryEntryTree> {
const tree: HistoryEntryTree = { totalSize: 0, entries: [], maxRecursionReached: false, maxFilesReached: false };
const tree: HistoryEntryTree = { totalSize: 0n, entries: [], maxRecursionReached: false, maxFilesReached: false };

if (depth > depthLimit) {
console.warn('Max depth reached for listTree');
Expand Down
3 changes: 2 additions & 1 deletion client/src/parsec/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ import type {
WorkspaceHistoryEntryStatFolder as ParsecWorkspaceHistoryEntryStatFolder,
WorkspaceInfo as ParsecWorkspaceInfo,
Path,
U64,
UserID,
UserProfile,
VlobID,
Expand Down Expand Up @@ -313,7 +314,7 @@ interface OrganizationInfo {
data: number;
};
outsidersAllowed: boolean;
userLimit?: number;
userLimit?: U64;
hasUserLimit: boolean;
organizationAddr: ParsecOrganizationAddr;
organizationId: OrganizationID;
Expand Down
8 changes: 4 additions & 4 deletions client/src/plugins/libparsec/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export type SASCode = string
export type UserID = string
export type VlobID = string
export type SequesterVerifyKeyDer = Uint8Array
export type IndexInt = bigint
export type SizeInt = bigint
export type U64 = bigint
export type I64 = bigint
export type NonZeroU8 = number
export type U8 = number
export type I32 = number
Expand All @@ -96,10 +100,6 @@ export type FileDescriptor = number
export type Handle = number
export type U32 = number
export type VersionInt = number
export type I64 = number
export type IndexInt = number
export type SizeInt = number
export type U64 = number
export type { DateTime } from 'luxon'; import type { DateTime } from 'luxon';

export interface AvailableDevice {
Expand Down
Loading
Loading