首页 > 解决方案 > JavaScript 到 Python(使用 JS2PY)

问题描述

我有一个使用 JavaScript 的种子计算工具(功能正确)。我正在尝试使用 JS2PY 库将其转换为 Python(对于更详细的项目)。

我的代码:

import js2py

hasCode = js2py.eval_js('function hashCode(s) {let h = 0; for (let i = 0; i < s.length; i++) {h = (Math.imul(31, h) + s.charCodeAt(i)) | 0;} return h;}')
getSeed = js2py.eval_js('function getSeed(s) {let x = BigInt(hashCode(s)); if (/^[\-\+0-9]*$/.test(s)) {try {let y = BigInt(s); if (BigInt.asIntN(64, y) !== y) {throw RangeError();} x = y;} catch (err) {}} return { high: x >> 32n, low: BigInt.asIntN(32, x) };}')
output = js2py.eval_js('function output() {let seed = -734744700160476640; print("Copy this output - " + seed.high, seed.low);}')

output()

如果您在 HTML 文件中使用它,JavaScript 将生成种子输入的“高”和“低”(这里是 -734744700160476640,但我允许用户输入自己的种子)。

命令日志解释了我假设来自库的文件中存在语法错误。您可能会说我有点缺乏经验,所以如果是我自己的代码坏了,请大声说出来。我不知道 JS2PY 是否已经过时,或者是否有更好的选择。我自己一个都找不到。

标签: javascriptpython

解决方案


推荐阅读