From fd56c3464060f7df8ec0c21a831682d7b085fd10 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 5 Jan 2022 06:18:33 +0000 Subject: [PATCH] Switch to .mts files and build typescript bindings in genbindings.sh --- genbindings.sh | 29 +++++++++-- ts/node/fs.d.ts | 16 +++++++ ts/node/index.d.ts | 3 ++ ts/node/package.json | 8 ++++ ts/test/index.html | 109 +----------------------------------------- ts/test/test.mjs | 24 ++++++++++ ts/tsconfig.json | 29 +++++++++++ typescript_strings.py | 4 +- 8 files changed, 109 insertions(+), 113 deletions(-) create mode 100755 ts/node/fs.d.ts create mode 100755 ts/node/index.d.ts create mode 100755 ts/node/package.json create mode 100644 ts/test/test.mjs create mode 100644 ts/tsconfig.json diff --git a/genbindings.sh b/genbindings.sh index 13dba4a0..a378a1e1 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -1,15 +1,17 @@ #!/bin/bash usage() { - echo "USAGE: path/to/ldk-c-bindings \"JNI_CFLAGS\" debug android" + echo "USAGE: path/to/ldk-c-bindings \"JNI_CFLAGS\" debug android web" echo "For JNI_CFLAGS you probably want -I/usr/lib/jvm/java-11-openjdk-amd64/include/ -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux/" echo "debug should either be true, false, or leaks" echo "debug of leaks turns on leak tracking on an optimized release bianry" echo "android should either be true or false" + echo "web should either be true or false" exit 1 } [ "$1" = "" ] && usage [ "$3" != "true" -a "$3" != "false" -a "$3" != "leaks" ] && usage [ "$4" != "true" -a "$4" != "false" ] && usage +[ "$5" != "true" -a "$5" != "false" ] && usage set -x @@ -173,8 +175,12 @@ fi echo "Creating TS bindings..." mkdir -p ts/{enums,structs} -rm -f ts/{enums,structs}/*.ts -./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript +rm -f ts/{enums,structs,}/*.{mjs,mts} +if [ "$5" = "true" ]; then + ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript node +else + ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript browser +fi rm -f ts/bindings.c if [ "$3" = "true" ]; then echo "#define LDK_DEBUG_BUILD" > ts/bindings.c @@ -198,3 +204,20 @@ if [ "$3" = "true" ]; then else $COMPILE -o liblightningjs_release.wasm -s -Os -I"$1"/lightning-c-bindings/include/ ts/bindings.c "$1"/lightning-c-bindings/target/wasm32-wasi/release/libldk.a $EXTRA_LINK fi + +if [ -x "$(which tsc)" ]; then + cd ts +rm -r structs # TODO: Make the human-types compile + if [ "$5" = "false" ]; then + tsc + else + tsc --types node --typeRoots . + cd .. + if [ -x "$(which node)" ]; then + NODE_V="$(node --version)" + if [ "${NODE_V:1:2}" -gt 14 ]; then + node ts/test/ + fi + fi + fi +fi diff --git a/ts/node/fs.d.ts b/ts/node/fs.d.ts new file mode 100755 index 00000000..af20d139 --- /dev/null +++ b/ts/node/fs.d.ts @@ -0,0 +1,16 @@ +// Minimal subset of the node 'fs' library that we depend on. +// May be (c) Microsoft licensed under the MIT license, but API's are generally not copyrightable per recent precedent. +declare module 'fs' { + export type PathLike = string | Buffer | URL; + export type PathOrFileDescriptor = PathLike | number; + export function readFileSync( + path: PathOrFileDescriptor, + options?: { + encoding?: null | undefined; + flag?: string | undefined; + } | null + ): Buffer; +} +declare module 'node:fs' { + export * from 'fs'; +} diff --git a/ts/node/index.d.ts b/ts/node/index.d.ts new file mode 100755 index 00000000..516375fd --- /dev/null +++ b/ts/node/index.d.ts @@ -0,0 +1,3 @@ +// Minimal subset of the node 'fs' library that we depend on. +// May be (c) Microsoft licensed under the MIT license, but API's are generally not copyrightable per recent precedent. +/// diff --git a/ts/node/package.json b/ts/node/package.json new file mode 100755 index 00000000..c0b75897 --- /dev/null +++ b/ts/node/package.json @@ -0,0 +1,8 @@ +{ + "name": "@types/node", + "version": "17.0.8", + "description": "TypeScript definitions for Node.js", + "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node", + "contributors": [], + "types": "index.d.ts" +} diff --git a/ts/test/index.html b/ts/test/index.html index 3e7c6694..3b0ba69f 100644 --- a/ts/test/index.html +++ b/ts/test/index.html @@ -3,114 +3,7 @@ - diff --git a/ts/test/test.mjs b/ts/test/test.mjs new file mode 100644 index 00000000..1bcf0f1a --- /dev/null +++ b/ts/test/test.mjs @@ -0,0 +1,24 @@ +import * as ldk from "../bindings.mjs"; +async function run_tests() { + await ldk.initializeWasm("../../liblightningjs.wasm"); + const result = ldk.CResult_boolLightningErrorZ_ok(true); + console.assert(ldk.CResult_boolLightningErrorZ_is_ok(result)); + console.assert(ldk.CResult_boolLightningErrorZ_get_ok(result)); + ldk.CResult_boolLightningErrorZ_free(result); + console.assert(ldk.CResult_boolLightningErrorZ_ok(false) == result); // malloc doesn't need to guarantee this, but currently does + console.assert(ldk.CResult_boolLightningErrorZ_is_ok(result)); + console.assert(!ldk.CResult_boolLightningErrorZ_get_ok(result)); + ldk.CResult_boolLightningErrorZ_free(result); + + /*var pk_arr = []; + for (var i = 0; i < 33; i++) { pk_arr[i] = 42; } + const pk_bytes = encodeUint8Array(pk_arr); + const pk_res = wasm.TS_CResult_PublicKeyErrorZ_ok(pk_bytes); + console.assert(wasm.TS_CResult_PublicKeyErrorZ_is_ok(pk_res)); + const pk_res_bytes = wasm.TS_LDKCResult_PublicKeyErrorZ_get_ok(pk_res); + wasm.TS_CResult_PublicKeyErrorZ_free(pk_res);*/ + + console.log("pass"); +} + +run_tests(); diff --git a/ts/tsconfig.json b/ts/tsconfig.json new file mode 100644 index 00000000..922bd173 --- /dev/null +++ b/ts/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "target": "es2021", + "module": "es2020", + "forceConsistentCasingInFileNames": true, + + "strict": true, + "noImplicitAny": false, + "strictNullChecks": false, + "strictFunctionTypes": true, + "strictBindCallApply": true, + "strictPropertyInitialization": false, + "noImplicitThis": true, + "useUnknownInCatchVariables": true, + "alwaysStrict": true, + "noUnusedLocals": false, + "noUnusedParameters": false, + "exactOptionalPropertyTypes": false, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedIndexedAccess": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "allowUnusedLabels": false, + "allowUnreachableCode": false, + + "skipLibCheck": true + } +} diff --git a/typescript_strings.py b/typescript_strings.py index 29ccc3e4..914c0189 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -282,11 +282,11 @@ void __attribute__((visibility("default"))) TS_free(uint32_t ptr) { self.hu_struct_file_prefix = f""" import CommonBase from './CommonBase'; -import * as bindings from '../bindings' // TODO: figure out location +import * as bindings from '../bindings.mjs' """ self.c_fn_ty_pfx = "" - self.file_ext = ".ts" + self.file_ext = ".mts" self.ptr_c_ty = "uint32_t" self.ptr_native_ty = "number" self.result_c_ty = "uint32_t" -- 2.39.5