javascript嵌入式解释器MuJS官方参考手册

6 篇文章 0 订阅
4 篇文章 0 订阅

摘要:
MuJS 是一个轻量级的 JavaScript 解释器,可用于嵌入式开发。使用可移植 C 编写,实现了 ECMA-262 规定的 ECMAScript 标准。与 V8、SpiderMonkey 和 JavaScriptCore等相比,MuJS非常精简。网络上MuJS的相关说明很少,只有官网上有一些相对详尽的资料。

参考手册是官网扒过来的,而且是直译
http://dev.mujs.com/docs/reference.html
受限于本人的英文水平以及对javascript和MuJS的熟悉程度,翻译的不是很好,有可能存在错误,后续如果对MuJS有更深入的了解后再进行修正。欢迎留言指正。

1. Introduction

MuJS is a library, written in clean and simple C. Being an extension library, MuJS has no notion of a main program: it only works embedded in a host client program. The host program can invoke functions to execute Javascript code, read and write Javascript variables, and register C functions to be called by Javascript.

MuJS是用简洁的c语言实现的库。作为扩展库MuJS不打算作主程序来用,它只是嵌入到主机客户端程序中。主程序可以在函数中执行javascript脚本,读写javascript变量,注册由javascript调用的c函数。

The MuJS distribution includes a sample host program called “mujs”, which uses the MuJS library to offer a standalone Javascript interpreter for interactive or batch use.

MuJS包含一个简单的可执行程序mujs,它通过调用MuJS库提供一套标准的javascript解释器,作为javasript的交互终端或者批处理命令执行平台。
如下图,虚拟机中安装的mujs
这里写图片描述

This reference manual assumes that you are already familiar with the Javascript language, in particular the type system and object prototype mechanisms.

本手册假定您已经熟悉了javascript语言,特别是类型系统和对象原型机制。

2.Basic Concepts
基本概念

Values and Types
值和类型

There are six basic types in Javascript: undefined, null, boolean, number, string and object.

javascript中有6种基本类型:undefined, null, boolean, number, string 和 object。

Each object also has a class: object, array, function, userdata, regular expression, etc.

每个Object对象都具有一个类,类型可以是:object, array, function, userdata, regular expression等等

Javascript can call functions written in C provided by the host program, as well as other Javascript functions.

Javascript既可以调用宿主程序提供的c函数,也可以调用其他的javascript脚本函数。

Objects with the userdata class are provided to allow arbitrary C data to be attached to Javascript objects. A userdata object has a pointer to a block of raw memory, which is managed by the host. Userdata values cannot be created or modified in Javascript, only through the C API. This guarantees the integrity of data owned by the host program.

具有 userdata类的Objects对象允许任意的c数据附加到javascript的objects上。userdata 对象有一个指针,它指向一块由主程序管理的原始内存。关联到Javascript脚本UserData对象的所有值只能通过c的api来创建和修改。这一策略保证了主程序数据的完整性。

Custom properties on userdata objects can be implemented using getter and setter property accessor functions.

userdata 对象的自定义属性的对象可以使用getter和setter属性访问器函数来实现。

Numbers are represented using double precision floating point values.

Number使用双精度的浮点数来表示

Strings in the C interface are zero-terminated byte arrays in CESU-8 encoding. CESU-8 is a variant of UTF-8 which encodes supplementary unicode characters as surrogate pairs. This maintains compatibility with the UTF-16 nature of JavaScript, but requires attention when passing strings using supplementary unicode characters to and from the MuJS library. It also means that you cannot have any JavaScript strings with a zero character value in MuJS.

c接口中的字符串是一串CESU-8编码方式的以0结尾的字节序列。CESU-8是UTF-8的变种, 补充的unicode编码作为代理对。这种编码方式保证能够和javascript的utf-16编码兼容,但从MuJS库导入或者导出补充的unicode字符时需要留意;在MuJS中javascript脚本string不会有0字符。

Environments
环境(变量)

Each function executes within an environment which defines which variables are accessible. This is a chain of all environment records in scope, with the global environment at the top. Each environment record in MuJS is represented as an object with the null prototype, including the global environment object.

每个函数都在某种环境下执行,该环境定义了哪些变量是可用的。这是所有范围内环境的集合链表,全局环境在顶端。在MuJS中的每个环境记录都是以null原型对象呈现的,包括全局对象的环境。

The registry is a hidden environment record which is only accessible to C. This is where Javascript values and objects that should only be accessible to C functions may be stored.

注册表是一个仅能被c访问的隐藏的环境记录。这些只能通过c函数访问的javascript脚本的值和对象会存到注册表中。

Error Handling
错误处理

All Javascript actions start from C code in the host program calling a function from the MuJS library. Whenever an exception is thrown during the compilation or execution of Javascript, control returns to the host, which can take appropriate measures (such as printing an error message). C code can also throw exceptions by calling functions to create an error object and return control to Javascript.

javascript脚本的所有操作都是从c编码的主程序调用MuJS库开始的。当编译或者执行javascript脚本抛出异常时,控制权返回给主程序,主程序可以采取相应的处理措施(比如输出一条错误信息)。也可以在c代码中调用函数抛出异常来创建错误对象object,并将控制返回到javascript脚本中。

Internally, MuJS uses the C longjmp facility to handle errors. A protected environment uses setjmp to set a recovery point. The try statement in Javascript creates such a recovery point, as does calling js_dostring, js_dofile, js_ploadstring, js_ploadfile, js_pcall and js_pconstruct.

MuJS内部使用c语言的longjmp处理异常错误。使用setjmp将环境保护起来,设置为一个恢复点(指针)。在javascript脚本“try”声明里创建这类可恢复点(指针),调用js_dostring, js_dofile, js_ploadstring, js_ploadfile, js_pcall和js_pconstruct需要做错误处理。

When an error occurs or an exception is thrown from Javascript, it does a long jump to the most recent active recovery point.

如果错误产生或者javasript抛出异常, 系统通过long jump的跳转到最新的活动的恢复节点。

If an error occurs outside any protected environment, MuJS first calls the panic function and then calls abort, thus exiting the host application. Your panic function can avoid this exit by never returning (for example by doing a long jump to your own recovery point outside MuJS).

如果错误发生在受保护的环境变量外部, MuJS首先调用panic函数,然后调用abort退出进程,从而退出主程序。你的panic函数如果没有返回则可以避免退出(比如通过long jump到你自己的MuJS外部的可恢复指针)。

Garbage Collection
垃圾回收

MuJS performs automatic memory management using a basic mark-and-sweep collector. Collection is automatically triggered when enough allocations have accumulated. You can also force a collection pass from C.

MuJS使用基本的mark-and-sweep收集器来实现内存的自我管理,当累计到足够多的垃圾时收集器自动触发,你也可以通过c来强制进行垃圾回收操作。

Userdata objects have an associated C finalizer function that is called when the correspending object is freed.

userdata对象有一个与c相关联的终结函数,当对应的对象free时调用此函数回收资源。

The Stack
堆栈

MuJS uses a virtual stack to pass values to and from C. Each element in this stack represents a Javascript value (null, number, string, etc).

MuJS 使用虚拟的栈来与c进行值的交互。 栈中的每个元素都能体现javascript的值,如null, number, string等等。

Whenever Javascript calls C, the called function gets a new stack. This stack initially contains the this value and any arguments passed to the function. When the C function returns, the top value on the stack is passed back to the caller as the return value.

每当javascript调用c时, 所调用的函数获得一个新的栈。 这个栈最初包含this value和函数参数。 当c函数return返回时,栈顶的值将作为返回值传给调用者。

The stack values are accessed using stack indices. Index 0 always contains the this value, and function arguments are index 1 and up. Negative indices count down from the top of the stack, so index -1 is the top of the index and index -2 is the one below that.

堆栈值使用堆栈索引访问,索引0总是包含this value,而函数的参数则是索引1向上递增。负索引是从栈顶倒数,所以-1对应的是栈顶,-2是栈顶的下面一个。

3.The Application Program Interface
应用程序接口

State

typedef struct js_State js_State;
The interpreter state is bundled up in the opaque struct js_State. This state contains the value stacks, protected environments, and environment records.

解释器的state被捆绑到不透明结构体js_State,此state包含堆栈,受保护的环境变量以及环境变量记录。注:mujs所有操作都是围绕state进行的。

js_State *js_newstate(js_Alloc alloc, void *context, int flags);
Create a new state using the allocator function and allocator context. Pass NULL to use the default allocator.

创建一个新的js_State变量state对象,使用 allocator函数和allocator对应的上下文。如果使用默认的allocator则写入NULL。

The available flags:
JS_STRICT: compile and run code using ES5 strict mode.

flag可用参数:JS_STRICT,执行ES5的strict模式(唯一宏选项)
附上ES5 strict mode介绍:
https://www.cnblogs.com/fuheng01/articles/JS.html

void js_freestate(js_State *J);
Destroy the state and free all dynamic memory used by the state.

删除state并释放所占用的动态内存

Allocator
分配器

The interpreter uses a host provided function for all memory allocation needs:

解释器使用主程序提供的函数来按需分配内存。

typedef void *(*js_Alloc)(void *memctx, void *ptr, int size);
When size is zero, the allocator should behave like free and return NULL. When size is not zero, the allocator should behave like realloc. The allocator should return NULL if it cannot fulfill the request. The default allocator uses malloc, realloc and free.

如果参数size为0, allocator分配器类似free功能,并返回NULL。如果size非0,分配器则类似于realloc内存分配函数。如果无法满足内存分配需求,分配器返回NULL。默认的内存分配器使用的是malloc, realloc和free。

Panic

typedef void (*js_Panic)(js_State *J);
js_Panic js_atpanic(js_State *J, js_Panic panic);
Set a new panic function, and return the old one.

设置新的panic函数,同时返回老的panic

Report

typedef void (*js_Report)(js_State *J, const char *message);
void js_setreport(js_State *J, js_Report report);
Set a callback function for reporting various warnings and garbage collection

statistics. 设置回调函数report用于上报warnings和垃圾回收统计结果

Garbage collection
垃圾回收

js_gc(js_State *J, int report);
Force a garbage collection pass. If the report argument is non-zero, send a summary of garbage collection statistics to the report callback function.

强制垃圾回收,如果report的值为非0,则发送垃圾回收统计的概要给report回调函数。

Loading and compiling scripts
加载和编译scrpts脚本

A script is compiled by calling js_loadstring or js_loadfile. The result of a successful compilation is a function on the top of the stack. This function can then be executed with js_call.

通过调用js_loadstring或者js_loadfile接口编译script脚本,编译成功后的结果作为函数保存在栈顶,此函数可以使用js_call来执行。

void js_loadstring(js_State *J, const char *filename, const char *source);
void js_loadfile(js_State *J, const char *filename);
Compile the script and push the resulting function.

编译加载javascript脚本
J为js_newstate创建的js_State指针,filename指定的js脚本,source脚本命令

int js_ploadstring(js_State *J, const char *filename, const char *source);
int js_ploadfile(js_State *J, const char *filename);
Like js_loadstring/js_loadfile but in a protected environment. In case of success, return 0 with the result as a function on the stack. In case of failure, return 1 with the error object on the stack.

这些接口函数类似于js_loadstring/js_loadfile,只在受保护的环境变量中。 如果执行成功,返回0,同样作为函数放在栈里。如果失败,返回1错误object放到栈里。

Calling functions
函数调用

void js_call(js_State *J, int n);
To call a function, you must use the following protocol: 1) push the function to call onto the stack, 2) push the this value to be used by the function, 3) push the arguments to the function in order, 4) finally, call js_call with the number of arguments pushed in step 3.

为了调用js脚本里的函数,你必须先执行下面的操作:
1)将需调用的函数压入到栈里–js_getglobal
2)将函数需要的this value压入栈里–js_pushxx,一般为js_pushnull
3)按顺序压入函数参数–js_pushxx
4)最后,调用js_call指定第3步压入参数的数量n

Pop the function, the this value, and all arguments; execute the function; then push the return value from the function.

调用js_call,在函数中包含这些操作
1、弹出函数,this value和所有参数;
2、执行函数;
3、将函数执行返回值压栈。

void js_construct(js_State *J, int n);
The construct function implements the ‘new’ expression in Javascript. This is similar to js_call, but without pushing a this value: 1) push the constructor function to call onto the stack, 2) push the arguments to the constructor function in order, 3) finally, call js_construct with the number of arguments pushed in step 2.

此构造函数实现javascript中的new表达式; 类似js_call, 但无需压入this value。步骤如下
1)将构造函数压栈
2)将参数按需压栈
3)最后,调用js_construct带入参数个数n

int js_pcall(js_State *J, int n);
int js_pconstruct(js_State *J, int n);
Like js_call and js_construct but in a protected environment. In case of success, return 0 with the result on the stack. In case of failure, return 1 with the error object on the stack.

类似js_call和js_construct,包含异常处理。如果成功返回0,作为结果放到栈中;如果失败返回1,错误信息object放到栈中。

Script helpers
脚本帮助

There are two convenience functions for loading and executing code.

有两种便利的函数用于加载和执行代码

int js_dostring(js_State *J, const char *source);
Compile and execute the script in the zero-terminated string in source argument. If any errors occur, call the report callback function and return 1. Return 0 on success.

编译和执行script脚本,source为javascript脚本命令,是以0结尾的字符串。如果发生错误,此函数调用report上报异常,并返回1; 返回0表示成功。

int js_dofile(js_State *J, const char *filename);
Load the script from the file with the given filename, then compile and execute it. If any errors occur, call the report callback function and return 1. Return 0 on success.

加载filename指定的script脚本, 然后编译并执行。如果发生错误,则调用report回调函数上报错误信息并返回1,成功返回0.
filename:指定的js脚本文件

Protected environments
受保护的环境

The js_try macro pushes a new protected environment and calls setjmp. If it returns true, an error has occurred. The protected environment has been popped and the error object is located on the top of the stack.

js_try宏函数将新的protected enviroment压栈并调用setjmp跳转。如果返回true时则有错误发生,那么protected environment会被弹出,错误对象object保存在栈顶位置。

At the end of the code you want to run in the protected environment you must call js_endtry in order to pop the protected environment. Note: you should not call js_endtry when an error has occurred and you are in the true-branch of js_try.

在代码的最后你想回到protected environment, 你必须调用js_endtry来弹出protected environment. 备注:如果有错误生成,你不能调用js_endtry并且你已经进入到js_try的true分支中。

Since the macro is a wrapper around setjmp, the usual restrictions apply. Use the following example as a guide for how to use js_try:

由于这个宏是围绕setjmp进行封装的, 所以一般的限制作用。举个例子来看看如何使用js_try

if (js_try(J)) {
fprintf(stderr, “error: %s”, js_tostring(J, -1));
js_pop(J, 1);
return;
}
do_some_stuff();
js_endtry(J);

Most of the time you shouldn’t need to worry about protected environments. The functions prefixed with ‘p’ (js_pcall, js_ploadstring, etc) handle setting up the protected environment and return simple error codes.

大部分时间,你无需担心protected environments这个环境变量,带’p’的函数如js_pcall js_ploadstring等,会处理封装这个处理功能并返回简单的错误编码。
请看js_pcall函数,中已经做过js_try处理。

int js_pcall(js_State *J, int n)
{
    int savetop = TOP - n - 2;
    if (js_try(J)) {
        /* clean up the stack to only hold the error object */
        STACK[savetop] = STACK[TOP-1];
        TOP = savetop + 1;
        return 1;
    }
    js_call(J, n);
    js_endtry(J);
    return 0;
}

Errors
错误处理

void js_throw(js_State *J);
Pop the error object on the top of the stack and return control flow to the most recent protected environment.

弹出错误栈顶的对象error object,返回控制流程到最新的protected environment

void js_newerror(js_State *J, const char *message);
void js_newevalerror(js_State *J, const char *message);
void js_newrangeerror(js_State *J, const char *message);
void js_newreferenceerror(js_State *J, const char *message);
void js_newsyntaxerror(js_State *J, const char *message);
void js_newtypeerror(js_State *J, const char *message);
void js_newurierror(js_State *J, const char *message);

Push a new error object on the stack. 将新的error object压栈

void js_error(js_State *J, const char *fmt, …);
void js_evalerror(js_State *J, const char *fmt, …);
void js_rangeerror(js_State *J, const char *fmt, …);
void js_referenceerror(js_State *J, const char *fmt, …);
void js_syntaxerror(js_State *J, const char *fmt, …);
void js_typeerror(js_State *J, const char *fmt, …);
void js_urierror(js_State *J, const char *fmt, …);
Wrapper to push a new error object on the stack using a printf formatting string and call js_throw.

函数封装,将错误对象error object压栈同时格式化输出string并调用js_throw

Stack manipulation
栈操作

int js_gettop(js_State *J);
void js_settop(js_State *J, int idx); – not implemented yet
void js_pop(js_State *J, int n); //n 弹出堆栈元素的数量
void js_rot(js_State *J, int n);
void js_copy(js_State *J, int idx); // idx 拷贝idx应的元素
void js_remove(js_State *J, int idx);
void js_insert(js_State *J, int idx); – not implemented yet
void js_replace(js_State* J, int idx); – not implemented yet

:idx如果为正,则从栈底开始数,如果为负责从栈顶开始数。所有mujs的idx参数都遵循这个原则,而且作用都是定位堆栈中对应序号的元素。
请看源码

static js_Value *stackidx(js_State *J, int idx)
{
    static js_Value undefined = { {0}, {0}, JS_TUNDEFINED };
    idx = idx < 0 ? TOP + idx : BOT + idx; // 根据idx正负判断起始
    if (idx < 0 || idx >= TOP)
        return &undefined;
    return STACK + idx;
}

Comparisons and arithmetic
比较和计算

void js_concat(js_State *J);
int js_compare(js_State *J, int *okay);
int js_equal(js_State *J);
int js_strictequal(js_State *J);
int js_instanceof(js_State *J);
The equivalent of the ‘+’, comparison, and instanceof operators. The okay argument to js_compare is set to 0 if any of the values are NaN, otherwise it is set to 1.

concat等价于’+’、compare、equal、strictequal是比较和instanceof operators。如果有值为NaN参数okay被赋值为0,否则为1。

Primitive values
原始值(元值),(与wrapper values相对)

void js_pushundefined(js_State *J);
void js_pushnull(js_State *J);
void js_pushboolean(js_State *J, int v);
void js_pushnumber(js_State *J, double v);
void js_pushstring(js_State *J, const char *v);
void js_pushliteral(js_State *J, const char *v);
Push primitive values. js_pushstring makes a copy of the string, so it may be freed or changed after passing it in. js_pushliteral keeps a pointer to the string, so it must not be changed or freed after passing it in.

压入元值(未封装的值,即基本单元值)。js_pushstring函数赋值一份string的拷贝,因此当string压栈后可以删除或者修改。
js_pushliteral则保持指向string的指针,所以即使在压入后也不能改动或者释放,
注:类似c语言的指针传参,在函数内部可以修改传入参数的值。
Primitive 和 Wrapper说明:
http://blog.csdn.net/zt_soft/article/details/1437249

int js_isdefined(js_State *J, int idx);
int js_isundefined(js_State *J, int idx);
int js_isnull(js_State *J, int idx);
int js_isboolean(js_State *J, int idx);
int js_isnumber(js_State *J, int idx);
int js_isstring(js_State *J, int idx);
int js_isprimitive(js_State *J, int idx);
Test if a primitive value is of a given type.

检查,判断一个原始值是否为给定的数据类型。

int js_toboolean(js_State *J, int idx);
double js_tonumber(js_State *J, int idx);
double js_tointeger(js_State *J, int idx);
int js_toint32(js_State *J, int idx);
unsigned int js_touint32(js_State *J, int idx);
short js_toint16(js_State *J, int idx);
unsigned short js_touint16(js_State *J, int idx);
const char *js_tostring(js_State *J, int idx);
Convert the value at the given index into a C value. If the value is an object, invoke the toString and/or valueOf methods to do the conversion.

类型转换,将给定的类型转换为c值类型。如果值的类型是对象object,则调用toString以及(或者)valueOf的方法来执行转换。

注:关于对象object的toString和valueOf函数,请参考链接
https://www.cnblogs.com/peakleo/p/6248242.html

The conversion may change the actual value in the stack!

转换可能会改变栈中的实际值!

There is no guarantee that the pointer returned by js_tostring will be valid after the
corresponding value is removed from the stack.

如果栈中某一个值被移除,则无法保证调用js_tostring返回的字符串指针是有效的

Objects
对象
enum {
JS_REGEXP_G = 1,
JS_REGEXP_I = 2,
JS_REGEXP_M = 4,
};

void js_newobject(js_State *J);
void js_newarray(js_State *J);
void js_newboolean(js_State *J, int v);
void js_newnumber(js_State *J, double v);
void js_newstring(js_State *J, const char *v);
void js_newregexp(js_State *J, const char *pattern, int flags);
Creat and push objects on the stack.

创建并压入对象到栈中,留意与js_pushnumber等的区别,这些都是作为object来封装压入的,比如i=10 和t={i:10},都包含number,但后者是个object。

int js_isobject(js_State *J, int idx);
int js_isarray(js_State *J, int idx);
int js_iscallable(js_State *J, int idx);
int js_isregexp(js_State *J, int idx);
Test the type and class of an object on the stack.

测试栈中某一对象的类型

Properties
属性(名值对)

The property functions all work on an object. If the stack slot referenced by the index does not contain an object, they will throw an error.
属性相关函数都是针对object对象的,如果栈中的“槽位”没有包含object对象,则会抛出异常。

enum {
JS_READONLY = 1,
JS_DONTENUM = 2,
JS_DONTCONF = 4,
};
Property attribute bit-mask values.

成员属性对应的bit-mask值。

int js_hasproperty(js_State *J, int idx, const char *name);
If the object has a property with the given name, return 1 and push the value of the property; otherwise return 0 and leave the stack untouched.

在object的属性列表中查找name的属性,如果存在将对应的值压入当前stack堆栈中并返回1;否则返回0,不对栈做任何操作并退出。
注:在查询property后,如果返回1,我们可以直接通过js_toxxx(J, -1)获得该property。

void js_getproperty(js_State *J, int idx, const char *name);
Push the value of the named property of the object. If the object does not have the named property, push undefined instead.

获取object的名字为name的属性,将其压入到stack堆栈中,如果对象不具备这个属性,则压入undefined代替。
注:这个函数一般与js_getglobal配套使用,由js_getglobal获取object,object压栈。
调用js_getproperty获得object的属性,idx取值:取第1个属性时 idx = -1, 继续取第2个时,idx=-2,第3个以此类推。原因是没取一个属性,就会将当前属性压栈,而object就会下沉一次。

void js_setproperty(js_State *J, int idx, const char *name);
Pop a value from the top of the stack and set the value of the named property of the object.

找到堆栈中idx的object,从栈顶弹出value值,将其与name绑定并加入到object的属性链表中。
注:一般是先压入需要设置的属性值,比如js_pushnumber(J, 100), 再调用js_setproperty来设置属性。idx一般为-2,即栈顶为value,下一个栈元素为要设置属性的object。

void js_defproperty(js_State *J, int idx, const char *name, int atts);
Pop a value from the top of the stack and set the value of the named property of the object. Also define the property attributes.

从栈顶弹出值,设置对象的属性为name对应的值,同时定义属性

void js_defaccessor(js_State *J, int idx, const char *name, int atts);
Define the getter and setter attributes of a property on the object. Pop the two getter and setter functions from the stack. Use null instead of a function object if you want to leave any of the functions unset.

定义对象特性的getter和setter属性, 从栈中弹出两个getter和setter函数。如果你想从未设置的函数中离开则使用null来代替object

void js_delproperty(js_State *J, int idx, const char *name);
Delete the named property from the object.

删除对象的name属性。

Array properties
数组属性

int js_getlength(js_State *J, int idx);
void js_setlength(js_State *J, int idx, int len);
Wrappers to get and set the “length” property of an object.

对象get和set length

int js_hasindex(js_State *J, int idx, int i);
void js_getindex(js_State *J, int idx, int i);
void js_setindex(js_State *J, int idx, int i);
void js_delindex(js_State *J, int idx, int i);
These array index functions functions are simple wrappers around the equivalent property functions. They convert the numeric index to a string to use as the property name.

这些带序号的数组函数是对等价属性函数的一种简单封装,他们将数组序号转换为string,转换后的 string作为属性名称来使用

Globals
全局global

void js_pushglobal(js_State *J);
Push the object representing the global environment record.

压入对象object代表全局环境记录

void js_getglobal(js_State *J, const char *name);
void js_setglobal(js_State *J, const char *name);
void js_defglobal(js_State *J, const char *name, int atts);
Wrappers around js_pushglobal and js_get/set/defproperty to read and write the values of global variables.

围绕js_pushglobal进行封装,js_get/set/defproperty读写全局变量的值,建议阅读另一篇《MuJS官网示例讲解–linux》

C Functions
c函数
封装c函数,使得在javascript脚本中可以直接调用

void js_newcfunction(js_State *J, js_CFunction fun, const char *name, int length);
Push a function object wrapping a C function pointer.

压入函数对象封装为c函数指针
参数说明
J–state对象
fun—函数指针,遵照js_CFunction定义来编写
name–在javascript脚本对应的函数名称
length–函数参数个数

The length argument is the number of arguments to the function. If the function is called with fewer arguments, the argument list will be padded with undefined.

参数的长度是函数参数的个数,如果函数的参数少于长度,则参数列表用undefined不全。

void js_newcconstructor(js_State *J,
js_CFunction fun, js_CFunction con,
const char *name, int length);
Pop the object to set as the “prototype” property for the constructor function object. Push a function object wrapping a C function pointer, allowing for separate function pointers for regular calls and ‘new’ operator calls.

弹出对象,以便为构造函数对象设置“标准”属性。压入函数对象封装成c函数指针,为常规调用以及“new”对象操作而进行分离函数指针

void js_currentfunction(js_State *J);
Push the currently executing function object.

压入当前正执行的函数对象

Userdata

typedef void (*js_Finalize)(js_State *J, void *data);
typedef int (*js_HasProperty)(js_State *J, void *data, const char *name);
typedef int (*js_Put)(js_State *J, void *data, const char *name);
typedef int (*js_Delete)(js_State *J, void *data, const char *name);

void js_newuserdata(js_State *J, const char *tag, void *data,
js_Finalize finalize);

void js_newuserdatax(js_State *J, const char *tag, void *data,
js_HasProperty has,
js_Put put,
js_Delete delete,
js_Finalize finalize);
Pop an object from the top of the stack to use as the internal prototype property for the new object. Push a new userdata object wrapping a pointer to C memory. The userdata object is tagged using a string, to represent the type of the C memory.

从栈顶弹出一个对象作为一个新对象的内部标准属性使用。压入新的userdata对象封装成指向c内存的指针。userdata对象用字符串作为标签,代表着c内存的某个类型

The finalize callback, if it is not NULL, will be called when the object is freed by the garbage collector.

finalize的回调,如果是NULL,则在对象被垃圾回收器释放后会被调用

The extended function also has callback functions for overriding property accesses. If these are set, they can be used to override accesses to certain properties. Any property accesses that are not overridden will be handled as usual in the runtime. The “HasProperty” callback should push a value and return true if it wants to handle the property, otherwise it should do nothing and return false. “Put” should pop a value and return true if it wants to handle the property. Likewise, “Delete” should return true if it wants to handle the property.

扩展函数拥有一些回调函数,可以重写原来的属性。如果设置了回调,它们可以用来修改某些属性。任何未覆盖的属性访问会作一般的处理。”HasProperty”回调函数如果想要处理属性,则需要压入值并返回true,否则不做任何处理并返回false.”put”函数如果需要操作属性则要弹出值成功返回true。同样的,”Delete”函数如果要处理属性也要返回true。

int js_isuserdata(js_State *J, int idx, const char *tag);
Test if an object is a userdata object with the given type tag string.

测试某一对象object是否为给定string标签的usedata对象

void *js_touserdata(js_State *J, int idx, const char *tag);
Return the wrapped pointer from a userdata object. If the object is undefined or null, return NULL. If the object is not a userdata object with the given type tag string, throw a type error.

返回已经封装成指针的userdata对象。如果对象是undefined或者null,返回NULL。如果对象不是给定类型标签的userdata,则抛出异常。

Registry
注册表

The registry can be used to store references to Javascript objects accessible from C, but hidden from Javascript to prevent tampering.

注册表可用于存储对C可访问的JavaScript对象的引用,但隐藏在JavaScript中以防止篡改

void js_getregistry(js_State *J, const char *name);
void js_setregistry(js_State *J, const char *name);
void js_delregistry(js_State *J, const char *name);
Access properties on the hidden registry object.

在隐藏注册表对象上访问属性

const char *js_ref(js_State *J);
WIP: Pop a value from the stack and store it in the registry using a new unique property name. Return the property name.

从栈中弹出值,使用一个新的唯一属性保存到注册表。 返回属性名称。

void js_unref(js_State *J, const char *ref);
WIP: Delete the reference from the registry.

删除注册表中的引用

想继续学习MuJS,可以看看下一篇的官网示例讲解

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值