Rough start to Deno runtime implemenation

This commit is contained in:
Timothy Warren 2023-10-27 16:02:54 -04:00
parent fdb1097a9c
commit fba91bde10
7 changed files with 144 additions and 2 deletions

10
.editorconfig Normal file
View File

@ -0,0 +1,10 @@
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = tab
insert_final_newline = false
max_line_length = 120
tab_width = 2

20
deno.jsonc Normal file
View File

@ -0,0 +1,20 @@
{
"imports": {
"std": "https://deno.land/std@0.204.0/",
// "/": "./src/",
},
"lint": {
"include": ["src/"],
"rules": {
"tags": ["recommended"],
}
},
"fmt": {
"useTabs": true,
"lineWidth": 80,
"indentWidth": 2,
"semiColons": true,
"singleQuote": true,
},
"nodeModulesDir": true,
}

14
justfile Normal file
View File

@ -0,0 +1,14 @@
# Lists the available actions
default:
@just --list
# Code linting with deno
deno-lint:
deno lint
# Code linting with bun
bun-lint:
# Run with deno
deno-run:
deno run --allow-all --allow-ffi --deny-net --deny-hrtime --unstable ./src/scroll.ts

1
src/bun/ffi.ts Normal file
View File

@ -0,0 +1 @@
// Bun-specific ffi code

64
src/deno/ffi.ts Normal file
View File

@ -0,0 +1,64 @@
// Deno-specific ffi code
// Determine library extension based on
// your OS.
let libSuffix = '';
switch (Deno.build.os) {
case 'windows':
libSuffix = 'dll';
break;
case 'darwin':
libSuffix = 'dylib';
break;
default:
libSuffix = 'so';
break;
}
const cSharedLib = `./libc.${libSuffix}`;
const cStdLib = Deno.dlopen(
cSharedLib,
{
termios: {
type: {
struct: [
// c_iflag
'u32',
// c_oflag
'u32',
// c_cflag
'u32',
// c_lflag
'u32',
// c_cc[20]
'u8', 'u8', 'u8', 'u8', 'u8',
'u8', 'u8', 'u8', 'u8', 'u8',
'u8', 'u8', 'u8', 'u8', 'u8',
'u8', 'u8', 'u8', 'u8', 'u8',
// __ispeed
'i32',
// __ospeed
'i32',
]
}
},
tcgetattr: {
parameters: ['i32', 'pointer'],
result: 'i32',
},
tcsetattr: {
parameters: ['i32', 'i32', 'pointer'],
result: 'i32',
},
cfmakeraw: {
parameters: ['pointer'],
result: 'void',
},
} as const,
);
export default cStdLib.symbols;
export const termios = cStdLib.symbols.termios;
export const tcgetattr = cStdLib.symbols.tcgetattr;
export const tcsetattr = cStdLib.symbols.tcsetattr;
export const cfmakeraw = cStdLib.symbols.cfmakeraw;

15
src/deno/index.ts Normal file
View File

@ -0,0 +1,15 @@
/**
* The main entrypoint when using Deno as the runtime
*/
export async function main(): Promise<number> {
const decoder = new TextDecoder();
for await (const chunk of Deno.stdin.readable) {
const char = String(decoder.decode(chunk)).trim();
if (char === 'q') {
return 0;
}
}
return -1;
}

View File

@ -1,6 +1,24 @@
/** /**
* The starting point for running scroll * The starting point for running scroll
*/ */
(() => {
// Now, to do stuff export enum RunTime {
Bun = 'bun',
Deno = 'deno',
Unknown = 'common',
}
/**
* Determine the runtime strategy, and go!
*/
(async () => {
let RUNTIME = RunTime.Unknown;
if ('Deno' in globalThis) {
RUNTIME = RunTime.Deno;
const { main } = await import('./deno/index.ts');
await main();
}
if ('Bun' in globalThis) {
RUNTIME = RunTime.Bun;
}
})(); })();