github.com/primecitizens/pcz/std@v0.2.1/builtin/print/bindings/ffi_bindings.ts (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 import { Application, Pointer, importModule } from "@ffi"; 5 6 importModule("stdprint", (App: Application) => { 7 const buf = new Array<string>(); 8 let timeoutID: any; // using type any to workaround node js Timeout type. 9 10 return { 11 "print": (str: Pointer, len: number): void => { 12 let s = App.load.String(str, len); 13 const lf = s.lastIndexOf("\n"); 14 if (lf >= 0) { 15 if (timeoutID) { 16 clearTimeout(timeoutID); 17 timeoutID = undefined; 18 } 19 20 console.log(buf.splice(0, buf.length).join("") + s.substring(0, lf)); 21 if (lf === s.length - 1) { 22 return; 23 } 24 25 s = s.substring(lf + 1); 26 } 27 28 buf.push(s); 29 if (timeoutID) { 30 return; // only keep one timeout job going 31 } 32 33 timeoutID = setTimeout(() => { 34 timeoutID = undefined; 35 console.log(buf.splice(0, buf.length).join("")); 36 }, 100); 37 return; 38 }, 39 }; 40 });