Update TS strings and commit (broken) test updates
authorMatt Corallo <git@bluematt.me>
Thu, 28 Jan 2021 23:11:51 +0000 (18:11 -0500)
committerMatt Corallo <git@bluematt.me>
Thu, 28 Jan 2021 23:11:51 +0000 (18:11 -0500)
ts/test/index.html
typescript_strings.py

index 42f6815874d0693af62a6795a8941cee046b05c2..9358a47c0ce1062642c88880000b2afa89d1af9a 100644 (file)
                imports.env["abort"] = function() {
                        console.error("ABORT");
                };
+               imports.env["js_log"] = function(argument) {
+                       console.log("HI");
+                       const res = decodeUint8Array(argument, false);
+                       console.log(res);
+               };
                imports.env["js_free"] = function(argument) {
                        console.log('integer passed from wasm:', argument);
                };
                const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
                const wasm = wasmInstance.exports;
 
+
+               const encodeUint8Array = (inputArray) => {
+                       const cArrayPointer = wasm.TS_malloc(inputArray.length + 4);
+                       const arrayLengthView = new Uint32Array(memory.buffer, cArrayPointer, 1);
+                       arrayLengthView[0] = inputArray.length;
+                       const arrayMemoryView = new Uint8Array(memory.buffer, cArrayPointer + 4, inputArray.length);
+                       arrayMemoryView.set(inputArray);
+                       return cArrayPointer;
+               }
+
+               const encodeUint32Array = (inputArray) => {
+                       const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4);
+                       const arrayMemoryView = new Uint32Array(memory.buffer, cArrayPointer, inputArray.length);
+                       arrayMemoryView[0] = inputArray.length;
+                       arrayMemoryView.set(inputArray, 1);
+                       return cArrayPointer;
+               }
+
+               const getArrayLength = (arrayPointer) => {
+                       const arraySizeViewer = new Uint32Array(
+                               memory.buffer, // value
+                               arrayPointer, // offset
+                               1 // one int
+                       );
+                       return arraySizeViewer[0];
+               }
+               const decodeUint8Array = (arrayPointer, free = true) => {
+                       const arraySize = getArrayLength(arrayPointer);
+                       const actualArrayViewer = new Uint8Array(
+                               memory.buffer, // value
+                               arrayPointer + 4, // offset (ignoring length bytes)
+                               arraySize // uint8 count
+                       );
+                       // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
+                       // will free the underlying memory when it becomes unreachable instead of copying here.
+                       const actualArray = actualArrayViewer.slice(0, arraySize);
+                       if (free) {
+                               wasm.TS_free(arrayPointer);
+                       }
+                       return actualArray;
+               }
+
                const result = wasm.TS_CResult_boolLightningErrorZ_ok(true);
                console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_result_ok(result));
                console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_get_ok(result));
                console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_result_ok(result));
                console.assert(!wasm.TS_LDKCResult_boolLightningErrorZ_get_ok(result));
                wasm.TS_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_PublicKeySecpErrorZ_ok(pk_bytes);
+               console.assert(wasm.TS_LDKCResult_PublicKeySecpErrorZ_result_ok(pk_res));
+               const pk_res_bytes = wasm.TS_LDKCResult_PublicKeySecpErrorZ_get_ok(pk_res);
+               wasm.TS_LDKCResult_PublicKeySecpErrorZ_free(pk_res);
+
                console.log("pass");
        })();
 </script>
index b5fcdc9696ba2397d07275dc4ca6830b6027d9fd..3ac304149a6bee481b10c6503cf92c00a2dcff81 100644 (file)
@@ -218,10 +218,6 @@ _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen),
 
 _Static_assert(sizeof(void*) == 4, "Pointers mut be 32 bits");
 
-//typedef struct int64_tArray { uint32_t *len; /* len + 1 is data */ } int64_tArray;
-//typedef struct uint32_tArray { uint32_t *len; /* len + 1 is data */ } uint32_tArray;
-//typedef struct ptrArray { uint32_t *len; /* len + 1 is data */ } ptrArray;
-//typedef struct int8_tArray { uint32_t *len; /* len + 1 is data */ } int8_tArray;
 typedef uint32_t int64_tArray;
 typedef uint32_t int8_tArray;
 typedef uint32_t uint32_tArray;
@@ -243,6 +239,12 @@ jstring str_ref_to_ts(const char* chars, size_t len) {
 
 typedef bool jboolean;
 
+uint32_t __attribute__((visibility("default"))) TS_malloc(uint32_t size) {
+       return (uint32_t)MALLOC(size, "JS-Called malloc");
+}
+void __attribute__((visibility("default"))) TS_free(uint32_t ptr) {
+       FREE((void*)ptr);
+}
 """
 
         self.hu_struct_file_prefix = f"""
@@ -337,9 +339,18 @@ const nextMultipleOfFour = (value: number) => {
     return Math.ceil(value / 4) * 4;
 }
 
-const encodeArray = (inputArray) => {
-       const cArrayPointer = wasm.wasm_malloc((inputArray.length + 1) * 4);
-       const arrayMemoryView = new Uint32Array(memory.buffer, cArrayPointer + 4, inputArray.length);
+const encodeUint8Array = (inputArray) => {
+       const cArrayPointer = wasm.TS_malloc(inputArray.length + 4);
+       const arrayLengthView = new Uint32Array(memory.buffer, cArrayPointer, 1);
+    arrayLengthView[0] = inputArray.length;
+       const arrayMemoryView = new Uint8Array(memory.buffer, cArrayPointer + 4, inputArray.length);
+       arrayMemoryView.set(inputArray);
+       return cArrayPointer;
+}
+
+const encodeUint32Array = (inputArray) => {
+       const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4);
+       const arrayMemoryView = new Uint32Array(memory.buffer, cArrayPointer, inputArray.length);
        arrayMemoryView.set(inputArray, 1);
     arrayMemoryView[0] = inputArray.length;
        return cArrayPointer;
@@ -364,7 +375,7 @@ const decodeUint8Array = (arrayPointer, free = true) => {
        // will free the underlying memory when it becomes unreachable instead of copying here.
        const actualArray = actualArrayViewer.slice(0, arraySize);
        if (free) {
-               wasm.free(arrayPointer);
+               wasm.TS_free(arrayPointer);
        }
        return actualArray;
 }
@@ -379,7 +390,7 @@ const decodeUint32Array = (arrayPointer, free = true) => {
        // will free the underlying memory when it becomes unreachable instead of copying here.
        const actualArray = actualArrayViewer.slice(0, arraySize);
        if (free) {
-               wasm.free(arrayPointer);
+               wasm.TS_free(arrayPointer);
        }
        return actualArray;
 }
@@ -387,7 +398,7 @@ const decodeUint32Array = (arrayPointer, free = true) => {
 const encodeString = (string) => {
     // make malloc count divisible by 4
     const memoryNeed = nextMultipleOfFour(string.length + 1);
-    const stringPointer = wasm.wasm_malloc(memoryNeed);
+    const stringPointer = wasm.TS_malloc(memoryNeed);
     const stringMemoryView = new Uint8Array(
         memory.buffer, // value
         stringPointer, // offset