当前位置: 首页 > news >正文

bellard.org‌ : QuickJS 如何使用 qjs 执行 js 脚本

参阅上一篇:Fabrice Bellard(个人网站:‌bellard.org‌)介绍

Fabrice Bellard(个人网站:‌bellard.org‌)是计算机领域最具影响力的程序员之一,其贡献跨越多个技术领域并持续推动开源生态发展。

QuickJS
小型嵌入式JavaScript引擎,强调低内存占用与ES6规范支持,适用于IoT设备与脚本扩展开发。

从 bellard.org 下载 quickjs-2025-04-26.tar.xz 到 D:\tcc\ , 然后解压。
从 bellard.org 下载 quickjs-cosmo-2025-04-26.zip 到 D:\tcc\ , 然后解压。看 readme.txt

D:\tcc\quickjs-cosmo-2025-04-26> rename qjs qjs.exeD:\tcc\quickjs-cosmo-2025-04-26> qjs -h
QuickJS version 2025-04-26
usage: qjs [options] [file [args]]
-h  --help         list options
-e  --eval EXPR    evaluate EXPR
-i  --interactive  go to interactive mode
-m  --module       load as ES6 module (default=autodetect)--script       load as ES6 script (default=autodetect)
-I  --include file include an additional file--std          make 'std' and 'os' available to the loaded script
-T  --trace        trace memory allocation
-d  --dump         dump the memory usage stats--memory-limit n  limit the memory usage to 'n' bytes (SI suffixes allowed)--stack-size n    limit the stack size to 'n' bytes (SI suffixes allowed)--no-unhandled-rejection  ignore unhandled promise rejections
-s                    strip all the debug info--strip-source    strip the source code
-q  --quit         just instantiate the interpreter and quit

计算圆周率 pi ,用 BigInt :
copy D:\tcc\quickjs-2025-04-26\examples\pi_bigint.js D:\tcc\quickjs-cosmo-2025-04-26\
cd D:\tcc\quickjs-cosmo-2025-04-26
 qjs pi_bigint.js 100
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

为了计算 斐波那契数列(Fibonacci sequence),编写 fib_bigint.js  如下

/** Fibonacci sequence computation in Javascript using the BigInt type*/
"use strict";function calc_fib(n)
{var a = BigInt(0);var b = BigInt(1);var c = BigInt(0);if (n <= 0)return a;for (var i = 2; i <= n; i++) {c = a + b;a = b;b = c;}return b;
}function main(args) {var r, n, n_bits, out;if (args.length < 1) {print("usage: qjs fib_bigint.js  n_digits");return;}n = args[0] | 0;if (n <=0) return;var r = calc_fib(n);var result = r.toString();print(result);
}var args;
if (typeof scriptArgs != "undefined") {args = scriptArgs;args.shift();
} else if (typeof arguments != "undefined") {args = arguments;
} else {/* default: 100 digits */args=[100];
}main(args);

cd D:\tcc\quickjs-cosmo-2025-04-26
运行  qjs fib_bigint.js 365
8531073606282249384383143963212896619394786170594625964346924608389878465365

校验,运行 python fibonacci.py 365
F(365): 8531073606282249384383143963212896619394786170594625964346924608389878465365

相关文章:

  • 一种实波束前视扫描雷达目标二维定位方法——论文阅读
  • 搭建一个 gRPC 服务端和客户端
  • 青少年ctf练习平台--做题wp(2)
  • 使用python加edge-tts实现文字转语音
  • C++ 简单工厂模式详解
  • 游戏开发的TypeScript(3)匿名函数的常见和不常见用法
  • 基于 vue-flow 实现可视化流程图
  • Java学习手册:关系型数据库基础
  • C++ 中 virtual 的作用
  • 什么是函数重载?
  • 【开源免费】二维码批量识别-未来之窗——C#-仙盟创梦IDE
  • 在 Ubuntu 上安装 cPanel
  • 20:深度学习-多层感知器原理
  • Linxu基本操作
  • 计算机系统结构 第二章 :缓存优化
  • Java中深拷贝与浅拷贝的深入探讨
  • C++抽象基类三重防线:纯虚函数与保护构造的深度实践
  • springAop代理责任链模式源码解析
  • 《解锁GCC版本升级:开启编程新世界大门》
  • Python蓝桥杯真题代码
  • 成为中国骑手孵化器,上海环球马术冠军赛是最好的历练舞台
  • 马克思主义理论研究教学名师系列访谈|薛念文:回应时代课题,才能彰显强大生命力
  • 最火“五一”预订!小长假前两日多地接待游客量两位数增长,出境游订单井喷
  • 苏州一直升机坠落致1死4伤,事故调查正展开
  • CMG亚太总站:没有邀请韩国偶像团体举办巡回演出
  • 龚正盛秋平王晓真共同启动2025国际消费季暨第六届上海“五五购物节”