aruZeta / QRgen

A QR code generation library.
https://aruzeta.github.io/QRgen/
MIT License
103 stars 8 forks source link

Having an issue with compiling to JS #31

Closed JohnRSim closed 2 years ago

JohnRSim commented 2 years ago
nim js -r "qrgen.nim"
C:\work\tmp\QRgen-main\src>nim js -r "qrgen.nim"
Hint: used config file 'C:\nim-1.6.6\config\nim.cfg' [Conf]
Hint: used config file 'C:\nim-1.6.6\config\config.nims' [Conf]
Hint: used config file 'C:\work\tmp\QRgen-main\config.nims' [Conf]
.............................................................................
C:\work\tmp\QRgen-main\src\QRgen\private\DrawedQRCode\print.nim(26, 9) Warning: implicit conversion to 'cstring' from a non-const location: if self.drawing[x, y]: "██" else: "  "; this will become a compile time error in the future [CStringConv]
Hint: opt: none (DEBUG BUILD, `-d:release` generates faster code)
67924 lines; 0.563s; 61.062MiB peakmem; proj: C:\work\tmp\QRgen-main\src\QRgen.nim; out: C:\work\tmp\QRgen-main\src\build\QRgen.js [SuccessX]
Hint: "C:\Program Files\nodejs\node.exe" --unhandled-rejections=strict C:\work\tmp\QRgen-main\src\build\QRgen.js  [Exec]

When I run the compiler I get this as the build?

/* Generated by the Nim Compiler v1.6.6 */
var framePtr = null;
var excHandler = 0;
var lastJSError = null;
if (!Math.trunc) {
    Math.trunc = function(v) {
        v = +v;
        if (!isFinite(v))
            return v;
        return (v - v % 1) || (v < 0 ? -0 : v === 0 ? v : 0);
    }
    ;
}

var numericModeCapacities_788529157 = [new Uint16Array([41, 77, 127, 187, 255, 322, 370, 461, 552, 652, 772, 883, 1022, 1101, 1250, 1408, 1548, 1725, 1903, 2061, 2232, 2409, 2620, 2812, 3057, 3283, 3517, 3669, 3909, 4158, 4417, 4686, 4965, 5253, 5529, 5836, 6153, 6479, 6743, 7089]), new Uint16Array([34, 63, 101, 149, 202, 255, 293, 365, 432, 513, 604, 691, 796, 871, 991, 1082, 1212, 1346, 1500, 1600, 1708, 1872, 2059, 2188, 2395, 2544, 2701, 2857, 3035, 3289, 3486, 3693, 3909, 4134, 4343, 4588, 4775, 5039, 5313, 5596]), new Uint16Array([27, 48, 77, 111, 144, 178, 207, 259, 312, 364, 427, 489, 580, 621, 703, 775, 876, 948, 1063, 1159, 1224, 1358, 1468, 1588, 1718, 1804, 1933, 2085, 2181, 2358, 2473, 2670, 2805, 2949, 3081, 3244, 3417, 3599, 3791, 3993]), new Uint16Array([17, 34, 58, 82, 106, 139, 154, 202, 235, 288, 331, 374, 427, 468, 530, 602, 674, 746, 813, 919, 969, 1056, 1108, 1228, 1286, 1425, 1501, 1581, 1677, 1782, 1897, 2022, 2157, 2301, 2361, 2524, 2625, 2735, 2927, 3057])];
var alphanumericModeCapacities_788529159 = [new Uint16Array([25, 47, 77, 114, 154, 195, 224, 279, 335, 395, 468, 535, 619, 667, 758, 854, 938, 1046, 1153, 1249, 1352, 1460, 1588, 1704, 1853, 1990, 2132, 2223, 2369, 2520, 2677, 2840, 3009, 3183, 3351, 3537, 3729, 3927, 4087, 4296]), new Uint16Array([20, 38, 61, 90, 122, 154, 178, 221, 262, 311, 366, 419, 483, 528, 600, 656, 734, 816, 909, 970, 1035, 1134, 1248, 1326, 1451, 1542, 1637, 1732, 1839, 1994, 2113, 2238, 2369, 2506, 2632, 2780, 2894, 3054, 3220, 3391]), new Uint16Array([16, 29, 47, 67, 87, 108, 125, 157, 189, 221, 259, 296, 352, 376, 426, 470, 531, 574, 644, 702, 742, 823, 890, 963, 1041, 1094, 1172, 1263, 1322, 1429, 1499, 1618, 1700, 1787, 1867, 1966, 2071, 2181, 2298, 2420]), new Uint16Array([10, 20, 35, 50, 64, 84, 93, 122, 143, 174, 200, 227, 259, 283, 321, 365, 408, 452, 493, 557, 587, 640, 672, 744, 779, 864, 910, 958, 1016, 1080, 1150, 1226, 1307, 1394, 1431, 1530, 1591, 1658, 1774, 1852])];
var byteModeCapacities_788529161 = [new Uint16Array([17, 32, 53, 78, 106, 134, 154, 192, 230, 271, 321, 367, 425, 458, 520, 586, 644, 718, 792, 858, 929, 1003, 1091, 1171, 1273, 1367, 1465, 1528, 1628, 1732, 1840, 1952, 2068, 2188, 2303, 2431, 2563, 2699, 2809, 2953]), new Uint16Array([14, 26, 42, 62, 84, 106, 122, 152, 180, 213, 251, 287, 331, 362, 412, 450, 504, 560, 624, 666, 711, 779, 857, 911, 997, 1059, 1125, 1190, 1264, 1370, 1452, 1538, 1628, 1722, 1809, 1911, 1989, 2099, 2213, 2331]), new Uint16Array([11, 20, 32, 46, 60, 74, 86, 108, 130, 151, 177, 203, 241, 258, 292, 322, 364, 394, 442, 482, 509, 565, 611, 661, 715, 751, 805, 868, 908, 982, 1030, 1112, 1168, 1228, 1283, 1351, 1423, 1499, 1579, 1663]), new Uint16Array([7, 14, 24, 34, 44, 58, 64, 84, 98, 119, 137, 155, 177, 194, 220, 250, 280, 310, 338, 382, 403, 439, 461, 511, 535, 593, 625, 658, 698, 742, 790, 842, 898, 958, 983, 1051, 1093, 1139, 1219, 1273])];
var totalDataCodewords_788529163 = [new Uint16Array([19, 34, 55, 80, 108, 136, 156, 194, 232, 274, 324, 370, 428, 461, 523, 589, 647, 721, 795, 861, 932, 1006, 1094, 1174, 1276, 1370, 1468, 1531, 1631, 1735, 1843, 1955, 2071, 2191, 2306, 2434, 2566, 2702, 2812, 2956]), new Uint16Array([16, 28, 44, 64, 86, 108, 124, 154, 182, 216, 254, 290, 334, 365, 415, 453, 507, 563, 627, 669, 714, 782, 860, 914, 1000, 1062, 1128, 1193, 1267, 1373, 1455, 1541, 1631, 1725, 1812, 1914, 1992, 2102, 2216, 2334]), new Uint16Array([13, 22, 34, 48, 62, 76, 88, 110, 132, 154, 180, 206, 244, 261, 295, 325, 367, 397, 445, 485, 512, 568, 614, 664, 718, 754, 808, 871, 911, 985, 1033, 1115, 1171, 1231, 1286, 1354, 1426, 1502, 1582, 1666]), new Uint16Array([9, 16, 26, 36, 46, 60, 66, 86, 100, 122, 140, 158, 180, 197, 223, 253, 283, 313, 341, 385, 406, 442, 464, 514, 538, 596, 628, 661, 701, 745, 793, 845, 901, 961, 986, 1054, 1096, 1142, 1222, 1276])];
var group1Blocks_788529165 = [new Uint8Array([1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 4, 2, 4, 3, 5, 5, 1, 5, 3, 3, 4, 2, 4, 6, 8, 10, 8, 3, 7, 5, 13, 17, 17, 13, 12, 6, 17, 4, 20, 19]), new Uint8Array([1, 1, 1, 2, 2, 4, 4, 2, 3, 4, 1, 6, 8, 4, 5, 7, 10, 9, 3, 3, 17, 17, 4, 6, 8, 19, 22, 3, 21, 19, 2, 10, 14, 14, 12, 6, 29, 13, 40, 18]), new Uint8Array([1, 1, 2, 2, 2, 4, 2, 4, 4, 6, 4, 4, 8, 11, 5, 15, 1, 17, 17, 15, 17, 7, 11, 11, 7, 28, 8, 4, 1, 15, 42, 10, 29, 44, 39, 46, 49, 48, 43, 34]), new Uint8Array([1, 1, 2, 4, 2, 4, 4, 4, 4, 6, 3, 7, 12, 11, 11, 3, 2, 2, 9, 15, 19, 34, 16, 30, 22, 33, 12, 11, 19, 23, 23, 19, 11, 59, 22, 2, 24, 42, 10, 20])];
var group1BlockDataCodewords_788529167 = [new Uint8Array([19, 34, 55, 80, 108, 68, 78, 97, 116, 68, 81, 92, 107, 115, 87, 98, 107, 120, 113, 107, 116, 111, 121, 117, 106, 114, 122, 117, 116, 115, 115, 115, 115, 115, 121, 121, 122, 122, 117, 118]), new Uint8Array([16, 28, 44, 32, 43, 27, 31, 38, 36, 43, 50, 36, 37, 40, 41, 45, 46, 43, 44, 41, 42, 46, 47, 45, 47, 46, 45, 45, 45, 47, 46, 46, 46, 46, 47, 47, 46, 46, 47, 47]), new Uint8Array([13, 22, 17, 24, 15, 19, 14, 18, 16, 19, 22, 20, 20, 16, 24, 19, 22, 22, 21, 24, 22, 24, 24, 24, 24, 22, 23, 24, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24]), new Uint8Array([9, 16, 13, 9, 11, 15, 13, 14, 12, 15, 12, 14, 11, 12, 12, 15, 14, 14, 13, 15, 16, 13, 15, 16, 15, 16, 15, 15, 15, 15, 15, 15, 15, 16, 15, 15, 15, 15, 15, 15])];
var group2Blocks_788529169 = [new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 1, 1, 1, 5, 1, 4, 5, 4, 7, 5, 4, 4, 2, 4, 10, 7, 10, 3, 0, 1, 6, 7, 14, 4, 18, 4, 6]), new Uint8Array([0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 4, 2, 1, 5, 5, 3, 1, 4, 11, 13, 0, 0, 14, 14, 13, 4, 3, 23, 7, 10, 29, 23, 21, 23, 26, 34, 14, 32, 7, 31]), new Uint8Array([0, 0, 0, 0, 2, 0, 4, 2, 4, 2, 4, 6, 4, 5, 7, 2, 15, 1, 4, 5, 6, 16, 14, 16, 22, 6, 26, 31, 37, 25, 1, 35, 19, 7, 14, 10, 10, 14, 22, 34]), new Uint8Array([0, 0, 0, 0, 2, 0, 1, 2, 4, 2, 8, 4, 4, 5, 7, 13, 17, 19, 16, 10, 6, 0, 14, 2, 13, 4, 28, 31, 26, 25, 28, 35, 46, 1, 41, 64, 46, 32, 67, 61])];
var group2BlockDataCodewords_788529171 = [new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 93, 0, 116, 88, 99, 108, 121, 114, 108, 117, 112, 122, 118, 107, 115, 123, 118, 117, 116, 116, 0, 116, 116, 122, 122, 123, 123, 118, 119]), new Uint8Array([0, 0, 0, 0, 0, 0, 0, 39, 37, 44, 51, 37, 38, 41, 42, 46, 47, 44, 45, 42, 0, 0, 48, 46, 48, 47, 46, 46, 46, 48, 47, 47, 47, 47, 48, 48, 47, 47, 48, 48]), new Uint8Array([0, 0, 0, 0, 16, 0, 15, 19, 17, 20, 23, 21, 21, 17, 25, 20, 23, 23, 22, 25, 23, 25, 25, 25, 25, 23, 24, 25, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25]), new Uint8Array([0, 0, 0, 0, 12, 0, 14, 15, 13, 16, 13, 15, 12, 13, 13, 16, 15, 15, 14, 16, 17, 0, 16, 17, 16, 17, 16, 16, 16, 16, 16, 16, 16, 17, 16, 16, 16, 16, 16, 16])];
var blockECCodewords_788529173 = [new Uint8Array([7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]), new Uint8Array([10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28]), new Uint8Array([13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]), new Uint8Array([17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30])];
var alignmentPatternLocations_788529334 = [[], [18], [22], [26], [30], [34], [22, 38], [24, 42], [26, 46], [28, 50], [30, 54], [32, 58], [34, 62], [26, 46, 66], [26, 48, 70], [26, 50, 74], [30, 54, 78], [30, 56, 82], [30, 58, 86], [34, 62, 90], [28, 50, 72, 94], [26, 50, 74, 98], [30, 54, 78, 102], [28, 54, 80, 106], [32, 58, 84, 110], [30, 58, 86, 114], [34, 62, 90, 118], [26, 50, 74, 98, 122], [30, 54, 78, 102, 126], [26, 52, 78, 104, 130], [30, 56, 82, 108, 134], [34, 60, 86, 112, 138], [30, 58, 86, 114, 142], [34, 62, 90, 118, 146], [30, 54, 78, 102, 126, 150], [24, 50, 76, 102, 128, 154], [28, 54, 80, 106, 132, 158], [32, 58, 84, 110, 136, 162], [26, 54, 82, 110, 138, 166], [30, 58, 86, 114, 142, 170]];

Seeing alot of this with the debug build and no exposed functions.

var F = {procname: "module QRgen", prev: framePtr, filename: "C:\\work\\tmp\\QRgen-main\\src\\QRgen.nim", line: 0};
framePtr = F;
framePtr = F.prev;
var F = {procname: "module DrawedQRCode", prev: framePtr, filename: "C:\\work\\tmp\\QRgen-main\\src\\QRgen\\private\\DrawedQRCode\\DrawedQRCode.nim", line: 0};
framePtr = F;
framePtr = F.prev;
aruZeta commented 2 years ago
C:\work\tmp\QRgen-main\src\QRgen\private\DrawedQRCode\print.nim(26, 9) Warning: implicit conversion to 'cstring' from a non-const location: if self.drawing[x, y]: "██" else: "  "; this will become a compile time error in the future [CStringConv]

That could be fixed easily with

modified   src/QRgen/private/DrawedQRCode/print.nim
@@ -15,7 +15,7 @@ proc printTerminal*(self: DrawedQRCode) =
   ## Print a `DrawedQRCode` to the terminal using `stdout`.
   template log(s: string) =
     when defined(js):
-      console.log s
+      console.log s.cstring
     else:
       stdout.write s
   log "\n\n\n\n\n"

About the generated JS, do you mean the arrays? Those are let {.compileTime.} variables which are used by QRgen for various tasks, like calculating minimum version, block positions, etc.

aruZeta commented 2 years ago

Will add the warning/error fix in the next patch release (which will most likely be 2.1.1, since I'm already almost done with 2.1.0)

JohnRSim commented 2 years ago

Thanks for the fast reply! I'm not seeing any exposed functions in the build just those variables.

I'm new to nim - but tried this example https://nim-lang.org/docs/backends.html and it built with js functions and looks good.

Not sure why the js functions aren't being exposed when I do the same with qrgen.nim

aruZeta commented 2 years ago

Maybe that's because they are not used, so the compiler doesn't expose them.

aruZeta commented 2 years ago

Take a look at the generated JS with this Nim code:

import
  QRgen,
  std/[jsconsole]

block:
  let qr = newQR("https://github.com/aruZeta/QRgen")
  qr.printTerminal

block:
  let qr = newQR("https://github.com/aruZeta/QRgen")
  console.log((qr.printSvg).cstring)
JohnRSim commented 2 years ago

ah - sorry now I understand. I thought when compiling the JS it auto-exposed newQR

So I could compile as JS and reuse the methods. I'll have a look at std/dom and see if I can expose it to the DOM window.

Thanks aruZeta.

aruZeta commented 2 years ago

No problem!

aruZeta commented 2 years ago

Will keep the issue Open till I add the fix to the warning/error you mentioned.

JohnRSim commented 2 years ago

For anyone else looking at this..

QRT.nim
import
  QRgen

block:
  let qr = newQR("https://github.com/aruZeta/QRgen")
  qr.printTerminal
nim js -d:release "QRT.nim"

QRT.js

//manually map to generated functions
const getMostEfficientMode = getMostEfficientMode_788529169;
const newQR = newQR_620756998;
const getSmallestVersion = getSmallestVersion_788529195;
const printSvg = printSvg_989855780;

function qrGen(txt,bgHexColor,qrHexColor,alignmentRadius,moduleRadius,moduleSeperation) {
    //check string
    if ((txt) && (txt.length === 0)) {
        console.error('Please supply QR String');
        return;
    }

    //define globals
    bgHexColor = bgHexColor || '#ffffff';
    qrHexColor = qrHexColor || '#000000';
    alignmentRadius = alignmentRadius || 0;
    moduleRadius = moduleRadius || 0;
    moduleSeperation = moduleSeperation || 0;

    //init
    const mkstring = makeNimstrLit(txt);
    const efficientMode = getMostEfficientMode(mkstring);
    const qr = nimCopy(null, newQR(mkstring, 0, efficientMode, getSmallestVersion(mkstring, efficientMode, 0)), NTI687865860);

    //return SVG
    return toJSStr(printSvg(qr, makeNimstrLit(bgHexColor), makeNimstrLit(qrHexColor), alignmentRadius, moduleRadius, moduleSeperation, makeNimstrLit("qrCode"), [], false));
}

//expose for window/import
window.qrGen = qrGen;
export default qrGen;
import qrGen from './lib/utils/QRT.js';
constmyQR = qrGen('s');

console.log(myQR);
aruZeta commented 2 years ago

The mentioned issue about the warning/error with implicit conversion has been fixed in v2.1.1