Skip to content

Commit

Permalink
[ts] add readonly modifier to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbouaziz committed Nov 27, 2024
1 parent ba652aa commit 627720d
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 292 deletions.
8 changes: 4 additions & 4 deletions packages/dev/src/skdb-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ async function getCreds(host: string, port: number, database: string) {
}

class DevServer {
database: string;
creds: Map<string, string>;
host: string;
port: number;
private readonly database: string;
private creds: Map<string, string>;
private readonly host: string;
private readonly port: number;

constructor(
database: string,
Expand Down
6 changes: 1 addition & 5 deletions skiplang/prelude/ts/src/sk_browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import type { float, int, Environment, Wrk, Shared } from "./sk_types.js";
import { MemFS, MemSys } from "./sk_mem_utils.js";

class WrkImpl implements Wrk {
worker: Worker;

constructor(worker: Worker) {
this.worker = worker;
}
constructor(private readonly worker: Worker) {}

static fromPath(url: URL, options?: WorkerOptions) {
return new this(new Worker(url, options));
Expand Down
6 changes: 3 additions & 3 deletions skiplang/prelude/ts/src/sk_mem_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class File {
}

export class MemFS implements FileSystem {
private fileDescrs: Map<string, number>;
private readonly fileDescrs: Map<string, number>;
private fileDescrNbr: int;
private files: File[];
private readonly files: File[];
// invariant: range(fileDescrs) ⊆ domain(files)

constructor() {
Expand Down Expand Up @@ -153,7 +153,7 @@ export class MemFS implements FileSystem {
}

export class MemSys implements System {
private env: Map<string, string>;
private readonly env: Map<string, string>;

constructor() {
this.env = new Map();
Expand Down
6 changes: 1 addition & 5 deletions skiplang/prelude/ts/src/sk_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import * as crypto from "crypto";
import { Worker } from "worker_threads";

class WrkImpl implements Wrk {
worker: Worker;

constructor(worker: Worker) {
this.worker = worker;
}
constructor(private readonly worker: Worker) {}

static fromPath(url: URL, options: WorkerOptions | undefined): Wrk {
const filename = "./" + path.relative(process.cwd(), url.pathname);
Expand Down
8 changes: 2 additions & 6 deletions skiplang/prelude/ts/src/sk_posix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface ToWasm {
}

class LinksImpl implements Links, ToWasm {
private fs: FileSystem;
private readonly fs: FileSystem;
SKIP_check_if_file_exists!: (skPath: ptr<Internal.String>) => boolean;
SKIP_js_open!: (skPath: ptr<Internal.String>, flags: int, mode: int) => int;
SKIP_js_close!: (fd: int) => void;
Expand Down Expand Up @@ -126,11 +126,7 @@ class LinksImpl implements Links, ToWasm {
}

class Manager implements ToWasmManager {
private environment: Environment;

constructor(environment: Environment) {
this.environment = environment;
}
constructor(private readonly environment: Environment) {}

prepare = (wasm: object) => {
const toWasm = wasm as ToWasm;
Expand Down
11 changes: 2 additions & 9 deletions skiplang/prelude/ts/src/sk_runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ import { Stream } from "./sk_types.js";
import type * as Internal from "./sk_internal_types.js";

class LinksImpl implements Links {
env: Environment | undefined;
lineBuffer!: int[];
lastTime!: int;

constructor(env?: Environment) {
this.env = env;
}
constructor(private readonly env?: Environment) {}

SKIP_read_line_fill!: () => int;
SKIP_read_to_end_fill!: () => int;
Expand Down Expand Up @@ -193,11 +190,7 @@ class LinksImpl implements Links {
}

class Manager implements ToWasmManager {
env: Environment | undefined;

constructor(env?: Environment) {
this.env = env;
}
constructor(private readonly env?: Environment) {}

prepare = (wasm: object) => {
const toWasm = wasm as ToWasm;
Expand Down
62 changes: 21 additions & 41 deletions skiplang/prelude/ts/src/sk_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,14 @@ const O_TRUNC = 1000;
const O_APPEND = 4000;

export class Options {
read: boolean;
write: boolean;
append: boolean;
truncate: boolean;
create: boolean;
create_new: boolean;

constructor(
read: boolean = true,
write: boolean = false,
append: boolean = false,
truncate: boolean = false,
create: boolean = true,
create_new: boolean = false,
) {
this.read = read;
this.write = write;
this.append = append;
this.truncate = truncate;
this.create = create;
this.create_new = create_new;
}
public read: boolean = true,
public write: boolean = false,
public append: boolean = false,
public truncate: boolean = false,
public create: boolean = true,
public create_new: boolean = false,
) {}

static w() {
return new Options(false, true);
Expand Down Expand Up @@ -226,18 +212,18 @@ export type App = {
};

export class Utils {
private exports: Exported;
private env: Environment;
private state: State;
private states: Map<string, any>;
private readonly exports: Exported;
private readonly env: Environment;
private readonly state: State;
private readonly states: Map<string, any>;

args: string[];
private current_stdin: number;
private stdin: Uint8Array;
private stdout: string[];
private stderr: string[];
private stddebug: string[];
private mainFn?: string;
private readonly mainFn?: string;
private exception?: Error;
private stacks: Map<ptr<Internal.Exception>, string>;

Expand Down Expand Up @@ -713,7 +699,7 @@ export interface Text {
}

export class Raw implements Text {
text: string;
private readonly text: string;

constructor(text: string, _category?: string) {
this.text = text;
Expand All @@ -731,13 +717,10 @@ export class Raw implements Text {
}

export class Locale implements Text {
text: string;
category: Nullable<string>;

constructor(text: string, category?: string) {
this.text = text;
this.category = category ? category : null;
}
constructor(
private readonly text: string,
private readonly category: Nullable<string> = null,
) {}

toJSON: () => object = () => {
return {
Expand Down Expand Up @@ -768,13 +751,10 @@ export const check: (value: Text | string) => Text = (value: Text | string) => {
};

export class Format implements Text {
format: Text | string;
parameters: (Text | string)[];

constructor(format: Text | string, parameters: (Text | string)[]) {
this.format = format;
this.parameters = parameters;
}
constructor(
private readonly format: Text | string,
private readonly parameters: (Text | string)[],
) {}

toJSON: () => object = () => {
return {
Expand Down
86 changes: 26 additions & 60 deletions skiplang/prelude/ts/src/sk_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,11 @@ export class Wrappable {
class UnmanagedMessage extends Error {}

export class Function {
fn: string;
parameters: any[];
wrap?: { wrap: boolean; autoremove: boolean };

constructor(
fn: string,
parameters: any[],
wrap?: { wrap: boolean; autoremove: boolean },
) {
this.fn = fn;
this.parameters = parameters;
this.wrap = wrap;
}
public fn: string,
public parameters: any[],
public wrap?: { wrap: boolean; autoremove: boolean },
) {}

static as(obj: object) {
if (!("fn" in obj) || !("parameters" in obj)) return null;
Expand All @@ -37,22 +29,12 @@ export class Function {
}

export class Caller {
wrapped: number;
fn: string;
parameters: any[];
remove: boolean;

constructor(
wrapped: number,
fn: string,
parameters: any[],
remove: boolean = false,
) {
this.wrapped = wrapped;
this.fn = fn;
this.parameters = parameters;
this.remove = remove;
}
public wrapped: number,
public fn: string,
public parameters: any[],
public remove: boolean = false,
) {}

static convert(obj: object) {
if (
Expand All @@ -77,13 +59,10 @@ export class Caller {
}

export class Return {
success: boolean;
value: any;

constructor(success: boolean, value: any) {
this.success = success;
this.value = value;
}
constructor(
public success: boolean,
public value: any,
) {}

static as(obj: object) {
if (!("success" in obj) || !("value" in obj)) return null;
Expand All @@ -92,13 +71,10 @@ export class Return {
}

export class MessageId {
source: number;
id: number;

constructor(source: number, id: number) {
this.source = source;
this.id = id;
}
constructor(
public source: number,
public id: number,
) {}

static as(obj: object) {
if (!("source" in obj) || !("id" in obj)) return null;
Expand All @@ -107,11 +83,7 @@ export class MessageId {
}

export class Wrapped {
wrapped: number;

constructor(wrapped: number) {
this.wrapped = wrapped;
}
constructor(public wrapped: number) {}

static as(obj: object) {
if (!("wrapped" in obj)) return null;
Expand All @@ -124,23 +96,17 @@ function asKey(messageId: MessageId) {
}

export class Sender {
close: () => void;
send: <T>() => Promise<T>;

constructor(close: () => void, send: <T>() => Promise<T>) {
this.close = close;
this.send = send;
}
constructor(
public close: () => void,
public send: <T>() => Promise<T>,
) {}
}

export class Message {
id: MessageId;
payload: unknown;

constructor(id: MessageId, payload: unknown) {
this.id = id;
this.payload = payload;
}
constructor(
public id: MessageId,
public payload: unknown,
) {}

private static convert(f: (_: object) => unknown, obj: object) {
if (!("id" in obj && typeof obj.id === "object")) return null;
Expand Down
8 changes: 2 additions & 6 deletions skiplang/skjson/ts/src/skjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class LinksImpl implements Links {
SKJSON_console!: (json: ptr<Internal.CJSON>) => void;
SKJSON_error!: (json: ptr<Internal.CJSON>) => void;

constructor(public env: Environment) {}
constructor(private readonly env: Environment) {}

complete = (utils: Utils, exports: object) => {
const fromWasm = exports as FromWasm;
Expand Down Expand Up @@ -405,11 +405,7 @@ class LinksImpl implements Links {
}

class Manager implements ToWasmManager {
env: Environment;

constructor(env: Environment) {
this.env = env;
}
constructor(private readonly env: Environment) {}

prepare = (wasm: object) => {
const toWasm = wasm as ToWasm;
Expand Down
4 changes: 2 additions & 2 deletions skipruntime-ts/examples/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ActiveUsers implements Mapper<GroupID, Group, GroupID, UserID> {

// Mapper function to filter out those active users who are also friends with `user`
class FilterFriends implements Mapper<GroupID, UserID, GroupID, UserID> {
constructor(private user: User) {}
constructor(private readonly user: User) {}

mapEntry(
gid: GroupID,
Expand All @@ -71,7 +71,7 @@ class FilterFriends implements Mapper<GroupID, UserID, GroupID, UserID> {
}

class ActiveFriends implements Resource<ResourceInputs> {
private uid: UserID;
private readonly uid: UserID;

constructor(params: { [param: string]: string }) {
if (!params["uid"]) throw new Error("Missing required parameter 'uid'");
Expand Down
Loading

0 comments on commit 627720d

Please sign in to comment.