一个嵌入式的JavaScript解释器:MuJS

mxf8 9年前

MuJS 是一个使用可移植 C 编写、轻量级的 JavaScript 解释器。用于嵌入到其他的应用来扩展脚本功能。实现了 ECMA-262 规定的 ECMAScript 标准。

开发 MuJS 的原因是 V8、SpiderMonkey 和 JavaScriptCore 都太大、太复杂了。MuJS 专注于提供一个非常精简、正确和简单的实现。

用于与本机代码的结合接口被设计为尽可能地简单易用,和类似的Lua。

A stand-alone interpreter

#include <stdio.h>  #include <mujs.h>    int main(int argc, char **argv)  {   char line[256];   js_State *J = js_newstate(NULL, NULL);   while (fgets(line, sizeof line, stdin))    js_dostring(J, line, 1);   js_freestate(J);  }

Hello, world!

#include <stdio.h>  #include <mujs.h>    static void hello(js_State *J)  {   const char *name = js_tostring(J, 1);   printf("Hello, %s!\n", name);   js_pushundefined(J);  }    int main(int argc, char **argv)  {   js_State *J = js_newstate(NULL, NULL);     js_newcfunction(J, hello, 1);   js_setglobal(J, "hello");     js_dostring(J, "hello('world');", 0);     js_freestate(J);  }

项目主页:http://www.open-open.com/lib/view/home/1426552806664