javascript - 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 是否已经过时,或者是否有更好的选择。我自己一个都找不到。
解决方案
推荐阅读
- javascript - 调整不同类型图像的大小
- python-3.x - 尽管少于 2000 个字符,嵌入会导致不和谐错误 400
- database - 缓慢插入/下降
- amazon-web-services - 可用区中的 EBS 卷如何仅限于特定的 AWS 账户及其用户?
- javascript - 如何使用javascript编辑数组项
- jquery - 在表中计算
- angular - 错误静态注入器:StaticInjectorError(AppModule)[GameService -> HttpClient]
- javascript - 多次排序数组并存储在一个对象中
- android-studio - 为什么我在 Android Studio 3.3 的“新”菜单下看不到所有选项
- arrays - 验证数组中的 Laravel 嵌套规则