.
675 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | TYPES=$(wildcard *.d.ts)
2 | DOCS=docs
3 | README=README.md
4 |
5 | FOOTER=$(DOCS)/footer.html
6 | HEADER=$(DOCS)/header.html
7 | INDEX=$(DOCS)/index.html
8 | STYLES=$(DOCS)/styles.css
9 |
10 | ARTIFACT=quickjs-types.tar.gz
11 |
12 | PANDOC=pandoc
13 |
14 | ifdef PANDOC_DOCKER
15 | PANDOC=docker run --rm --volume "`pwd`:/data" --user `id -u`:`id -g` pandoc/core:latest
16 | endif
17 |
18 | $(DOCS): $(shell mkdir -p $(DOCS))
19 | $(DOCS): $(INDEX)
20 |
21 | $(FOOTER):
22 | @echo "
Last update: $(shell date)
" \
23 | > $@
24 |
25 | $(HEADER):
26 | @echo "\
27 | \
28 | \
29 | Github \
30 | \
31 |
" > $@
32 |
33 | $(INDEX): $(README) $(HEADER) $(FOOTER) $(STYLES)
34 | @$(PANDOC) \
35 | --css $(notdir $(STYLES)) \
36 | --from gfm \
37 | --include-after-body $(FOOTER) \
38 | --include-before-body $(HEADER) \
39 | --metadata pagetitle="$$(head -1 $(README) | cut -c 3-)" \
40 | --output $@ \
41 | --standalone \
42 | --to html \
43 | $(README)
44 |
45 | $(STYLES):
46 | @curl -s -L https://gist.githubusercontent.com/killercup/5917178/raw/40840de5352083adb2693dc742e9f75dbb18650f/pandoc.css \
47 | > $@
48 |
49 | .PHONY: test
50 | test:
51 | tsc -p .
52 |
53 | .PHONY: release
54 | release: test $(ARTIFACT)
55 | $(ARTIFACT): $(TYPES) $(README) LICENSE package.json
56 | tar cf $@ $?
57 |
58 | .PHONY: clean
59 | clean:
60 | rm -rf $(DOCS) *.tgz *.tar.gz
61 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # QuickJS Types
2 |
3 | > TypeScript definitions for the QuickJS Engine!
4 |
5 |
6 |
7 | [](https://github.com/mgred/quickjs-types/actions/workflows/build_page.yaml)
8 | [](https://www.gnu.org/licenses/gpl-3.0.en.html)
9 |
10 | ## Usage
11 |
12 | ### Node
13 |
14 | Add using a package manager, e.g.: npm
15 |
16 | ```bash
17 | npm i -D quickjs-types
18 | ```
19 |
20 | Add types to `tsconfig.json`:
21 |
22 | ```json
23 | {
24 | "compilerOptions": {
25 | "types": ["quickjs-types"]
26 | }
27 | }
28 | ```
29 |
30 | ### Git Submodule
31 |
32 | Add a submodule to a project, e.g. `types/quickjs`:
33 |
34 | ```bash
35 | git submodule add git@github.com:mgred/quickjs-types.git types/quickjs
36 | ```
37 |
38 | Extend `tsconfig.json` to use types:
39 |
40 | ```json
41 | {
42 | "compilerOptions": {
43 | "typeRoots": ["types"]
44 | }
45 | }
46 | ```
47 |
48 | ## Tests
49 |
50 | All tests live in `test.ts`.
51 | To verify everyting passes, run:
52 |
53 | ```bash
54 | tsc -p .
55 | ```
56 |
57 | ## License
58 |
59 | Code in this repository is licensed under the [GNU General Public License v3](https://www.gnu.org/licenses/gpl-3.0.en.html)
60 |
61 | ## Todos
62 |
63 | - [ ] Add more tests
64 | - [ ] Add more documentation (comments)
65 |
--------------------------------------------------------------------------------
/globals.d.ts:
--------------------------------------------------------------------------------
1 | declare var scriptArgs: string[];
2 | declare var print: (...args: any[]) => void;
3 | declare var console: {
4 | log(...args: any[]): void;
5 | };
6 |
--------------------------------------------------------------------------------
/index.d.ts:
--------------------------------------------------------------------------------
1 | ///
2 | ///
3 | ///
4 | ///
5 |
--------------------------------------------------------------------------------
/os.d.ts:
--------------------------------------------------------------------------------
1 | declare module "os" {
2 | import { Seek, Error } from "std";
3 |
4 | type Success = 0;
5 | type OSOperationResult = Success | Error;
6 | type OSOperationTuple = [str: string, error: OSOperationResult];
7 | type Callback = () => any;
8 | type TimeoutHandle = number;
9 |
10 | export interface File {
11 | close(): number;
12 | puts(str: string): void;
13 | printf(fmt: string, ...args: any[]): void;
14 | flush(): void;
15 | seek(offset: number, whence: Seek): number;
16 | tell(): number;
17 | tello(): BigInt;
18 | eof(): boolean | unknown;
19 | fileno(): unknown;
20 | error(): Error | unknown;
21 | clearerr(): void;
22 | read(buffer: ArrayBuffer, position: number, length: number): void;
23 | write(buffer: ArrayBuffer, position: number, length: number): void;
24 | getline(): string;
25 | readAsString(max_size?: number): string;
26 | getByte(): number;
27 | putByte(c: number): void;
28 | }
29 |
30 | export interface FileStatus {
31 | readonly dev: number;
32 | readonly ino: number;
33 | readonly mode: number;
34 | readonly nlink: number;
35 | readonly uid: number;
36 | readonly gid: number;
37 | readonly rdev: number;
38 | readonly size: number;
39 | readonly blocks: number;
40 | readonly atime: number;
41 | readonly mtime: number;
42 | readonly ctime: number;
43 | }
44 |
45 | export interface ExecOptions {
46 | block?: boolean;
47 | usePath?: boolean;
48 | file?: string;
49 | cwd?: string;
50 | stdin?: File;
51 | stdout?: File;
52 | stderr?: File;
53 | env?: { readonly [key: string]: string };
54 | uid?: number;
55 | gid?: number;
56 | }
57 |
58 | export class Worker {
59 | static parent: Worker;
60 | constructor(filename: string);
61 | postMessage(msg: any): void;
62 | onmessage: (data: any) => void | null;
63 | }
64 |
65 | export const SIGINT: 2;
66 | export const SIGABRT: 6;
67 | export const SIGFPE: 8;
68 | export const SIGILL: 4;
69 | export const SIGSEGV: 11;
70 | export const SIGTERM: 15;
71 |
72 | export const WNOHANG: 1;
73 |
74 | export const platform: "linux" | "darwin" | "win32" | "js";
75 |
76 | export const O_RDONLY: 0;
77 | export const O_WRONLY: 1;
78 | export const O_RDWR: 2;
79 | export const O_CREAT: 64;
80 | export const O_EXCL: 128;
81 | export const O_TRUNC: 512;
82 | export const O_APPEND: 1024;
83 |
84 | export function open(filename: string, flag: number, mode?: unknown): File | -1;
85 | export function close(file: File): number;
86 | export function seek(file: File, offset: number, whence: Seek): number;
87 | export function seek(
88 | file: File,
89 | offset: BigInt,
90 | whence: Seek
91 | ): BigInt;
92 | export function read(
93 | file: File,
94 | buffer: ArrayBuffer,
95 | offset: number,
96 | length: number
97 | ): number;
98 | export function write(
99 | file: File,
100 | buffer: ArrayBuffer,
101 | offset: number,
102 | length: number
103 | ): number;
104 | export function isatty(file: File): boolean;
105 | export function ttyGetWinSize(
106 | file: File
107 | ): [width: number, height: number] | null;
108 | export function ttySetRaw(file: File): void;
109 | export function remove(filename: string): OSOperationResult;
110 | export function rename(oldname: string, newname: string): OSOperationResult;
111 | export function realpath(path: string): OSOperationTuple;
112 | export function getcwd(): OSOperationTuple;
113 | export function chdir(path: string): OSOperationResult;
114 | export function mkdir(path: string, mode?: string): OSOperationResult;
115 | export function stat(path: string): [status: FileStatus, error: Error];
116 | export function lstat(path: string): [status: FileStatus, error: Error];
117 | export function utimes(
118 | path: string,
119 | atime: number,
120 | mtime: number
121 | ): OSOperationResult;
122 | export function symlink(target: string, linkpath: string): OSOperationResult;
123 | export function readlink(path: string): OSOperationTuple;
124 | export function readdir(path: string): [files: string[], error: Error];
125 | export function setReadHandler(file: File, cb: Callback | null): void;
126 | export function setReadHandler(file: File, cb: null): void;
127 | export function setWriteHandler(file: File, cb: Callback): void;
128 | export function setWriteHandler(file: File, cb: null): void;
129 | export function signal(signal: number, cb: Callback): void;
130 | export function signal(signal: number, cb: null): void;
131 | export function signal(signal: number, cb: undefined): void;
132 | export function kill(pid: number, signal: number): void;
133 | export function exec(args: string[], options?: ExecOptions): number;
134 | export function waitpid(
135 | pid: number,
136 | options: number
137 | ): [ret: unknown | Error, status: any];
138 | export function dup(file: File): void;
139 | export function dup2(oldFile: File, newFile: File): void;
140 | export function pipe(): [readFile: File, writeFile: File] | null;
141 | export function sleep(delay: number): void;
142 | export function setTimeout(cb: Callback, delay: number): TimeoutHandle;
143 | export function clearTimeout(handle: TimeoutHandle): void;
144 | }
145 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "quickjs-types",
3 | "version": "1.1.0",
4 | "description": "TypeScript definitions for the QuickJS Engine!",
5 | "license": "GPL-3.0-or-later",
6 | "types": "./index.d.ts",
7 | "files": [
8 | "*.d.ts",
9 | "LICENSE",
10 | "README.md"
11 | ],
12 | "repository": {
13 | "type": "git",
14 | "url": "git+https://github.com/mgred/quickjs-types.git"
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/std.d.ts:
--------------------------------------------------------------------------------
1 | declare module "std" {
2 | import { File } from "os";
3 |
4 | export interface EvalOptions {
5 | backtrace_barrier?: boolean;
6 | }
7 |
8 | export interface ErrorOptions {
9 | errorno: Error;
10 | }
11 |
12 | export interface URLGetOptions {
13 | binary?: boolean;
14 | full?: boolean;
15 | }
16 |
17 | export interface URLGetResponse {
18 | readonly response: string | null;
19 | readonly responseHeaders: string;
20 | readonly status: number;
21 | }
22 |
23 | export const SEEK_SET: number; // 0
24 | export const SEEK_CUR: number; // 1
25 | export const SEEK_END: number; // 2
26 |
27 | export const S_IFMT: number;
28 | export const S_IFIFO: number;
29 | export const S_IFCHR: number;
30 | export const S_IFDIR: number;
31 | export const S_IFBLK: number;
32 | export const S_IFREG: number;
33 | export const S_IFSOCK: number;
34 | export const S_IFLNK: number;
35 | export const S_ISGID: number;
36 | export const S_ISUID: number;
37 |
38 | export type Seek = unknown;
39 | export const enum Error {
40 | EACCES = 13,
41 | EBUSY = 16,
42 | EEXIST = 17,
43 | EINVAL = 22,
44 | EIO = 5,
45 | ENOENT = 2,
46 | ENOSPC = 28,
47 | ENOSYS = 38,
48 | EPERM = 1,
49 | EPIPE = 32,
50 | }
51 |
52 | export function exit(n: number): void;
53 | export function evalScript(script: string, options?: EvalOptions): void;
54 | export function loadScript(filename: string): void;
55 | export function loadFile(filename: string): void;
56 | export function open(
57 | filename: string,
58 | flags: unknown,
59 | errorObj?: ErrorOptions
60 | ): File | null;
61 | export function popen(
62 | command: string,
63 | flags: unknown,
64 | errorObj?: ErrorOptions
65 | ): File | null;
66 | export function fdopen(
67 | file: File,
68 | flags: unknown,
69 | errorObj?: ErrorOptions
70 | ): File | null;
71 | export function tmpFile(errorObj?: ErrorOptions): File | null;
72 | export function puts(str: string): void;
73 | export function printf(fmt: string, ...args: any[]): void;
74 | export function sprintf(fmt: string, ...args: any[]): void;
75 |
76 | export function strerror(errorno: Error): string;
77 | export function gc(): void;
78 | export function getenv(name: string): any | undefined;
79 | export function setenv(name: string, value: any): void;
80 | export function unsetenv(name: string): void;
81 | export function getenviron(): { readonly [key: string]: string };
82 | export function urlGet(url: string): string;
83 | export function urlGet(
84 | url: string,
85 | options: { full?: false; binary: false }
86 | ): string;
87 | export function urlGet(
88 | url: string,
89 | options: { full?: false; binary: true }
90 | ): ArrayBuffer;
91 | export function urlGet(
92 | url: string,
93 | options: { full: true; binary?: false }
94 | ): URLGetResponse;
95 | export function urlGet(
96 | url: string,
97 | options: { full: true; binary?: false }
98 | ): ArrayBuffer;
99 | export function parseExtJSON(str: string): any;
100 |
101 | const _in: File;
102 | export { _in as in };
103 | export const err: File;
104 | export const out: File;
105 | }
106 |
--------------------------------------------------------------------------------
/test.ts:
--------------------------------------------------------------------------------
1 | import { Worker } from "os";
2 |
3 | //
4 | // globals
5 | //
6 |
7 | scriptArgs.map((a) => a.toUpperCase());
8 |
9 | const w = new Worker("filename");
10 | w.postMessage(null);
11 | w.onmessage = (data) => console.log(data);
12 |
13 | Worker.parent = new Worker("filename");
14 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "ES2020",
4 | "module": "ES2020",
5 | "lib": ["ES2020"],
6 | "noEmit": true,
7 | "strict": true,
8 | "noImplicitAny": true,
9 | "strictNullChecks": true,
10 | "strictFunctionTypes": true,
11 | "noImplicitThis": true,
12 | "baseUrl": "."
13 | },
14 | "files": ["globals.d.ts", "index.d.ts", "os.d.ts", "std.d.ts", "test.ts"]
15 | }
16 |
--------------------------------------------------------------------------------