4 Star 36 Fork 8

GongKe / ratjs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Rat Javascript - 小型javascript/ecmascript解释器

English

RATJS是一个用C语言实现的小型javascript/ecmascript解释器。你可以用它来运行自己的javascript程序,也可以将它作为脚本引擎嵌入自己开发的程序中。

特性

  • 兼容ECMA262标准第14版
    • Symbol
    • Generator
    • Promise
    • Async function
    • Arrow function
    • Async module
    • Big integer
    • Typed array
    • Array buffer/Shared array buffer
    • DataView
    • Atomics
    • Map/Set/WeakSet/WeakMap
    • WeakRef/FinalizationRegistry
    • Private identifier
    • Multiply realm
    • Module/Async module
  • 扩展
    • Hash bang 注释
    • Native module
    • JSON module
    • File system functions
    • FileState object
    • File object
    • Directory object
  • 完全用C语言实现
  • 低占用空间
  • 丰富的配置选项

依赖

  • icu4c: 当配置选项设置 ENC_CONV=icu 后需要此库
  • gmp: 当配置选项设置 ENABLE_BIG_INT=gmp 后需要此库

构建

RATJS 使用 GNU make 作为代码构建工具。 在构建时需要安装以下的库和对应的头文件:

  • c-json
  • libyaml: 如果需要生成 test262 测试程序需要此库

配置选项

显示全部配置选项:

$ make help

在Linux系统中构建

项目配置。

$ make config

生成RATJS链接库和可执行程序。

$ make

安装RATJS连接库和可执行程序。

$ make install

清除构建中间文件。

$ make clean

清除构建目录。

$ make dist-clean

在Windows系统中构建

在Windows系统中构建需要安装MinGW开发环境。

项目配置。

$ make ARCH=win config

生成RATJS链接库和可执行程序。

$ make

安装RATJS连接库和可执行程序。

$ make install

清除构建中间文件。

$ make clean

清除构建目录。

$ make dist-clean

使用

执行javascript

运行可执行程序"ratjs"解释你的javascript脚本。

运行 “js”脚本:

$ ratjs -s your_script.js arguments...

"ratjs"会加载并运行脚本"your_script.js"。如果脚本中定义了函数"main","main"函数将被调用且"arguments"将作为函数的参数被传入。

将"js"脚本作为ECMA262模块运行:

$ ratjs your_module.js arguments

"ratjs"将加载、链接并执行模块"your_module.js"。如果模块中定义了函数"main","main"函数将被调用且"arguments"将作为函数的参数被传入。

将参数字符串作为脚本源码调用"eval()"运行:

$ ratjs -e "script_string"

显示可执行程序"ratjs"的全部选项:

$ ratjs --help

嵌入程序

首先,在你的程序中包含头文件"ratjs.h"。然后通过调用 RATJS API加载并执行javascript脚本。

#include <ratjs.h>

...

RJS_Runtime *rt;
RJS_Value   *source, *script;

rt = rjs_runtime_new();
rjs_realm_load_extension(rt, NULL);

source = rjs_value_stack_push(rt);
script = rjs_value_stack_push(rt);

rjs_string_from_enc_chars(rt, source, "print(\"hello, world!\")", -1, NULL);
rjs_script_from_string(rt, script, source, NULL, RJS_FALSE);
rjs_script_evaluation(rt, script, NULL);

rjs_runtime_free(rt);

将你的程序连接库"libratjs"。

$ gcc -o your_program -lratjs -lm your_program_source.c

运行命令"doxygen"生成RATJS的API文档。

你可以参考"demo"目录下的示例程序学习如何将RATJS嵌入你的程序中。

原生模块

你可以用其他编译语言开发原生模块扩展javascript的功能,可参考njsgen原生模块生成工具快速创建原生模块。

扩展

扩展函数和对象

许可证

RATJS采用许可证MIT license发布。

The MIT License (MIT) Copyright 2022 Gong Ke Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

小型javascript/ecmascript解释器 展开 收起
MIT
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/gongke1978/ratjs.git
git@gitee.com:gongke1978/ratjs.git
gongke1978
ratjs
ratjs
master

搜索帮助