You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write `length` bytes to the file handle `fd` from the ArrayBuffer `buffer` at byte position `offset`. Return the number of written bytes or < 0 if error.
@@ -587,41 +594,41 @@ C API的设计简单而有效。C API在`quickjs.h`标头中定义。
587
594
588
595
#### 3.4.1 运行时和上下文
589
596
590
-
`JSRuntime` represents a Javascript runtime corresponding to an object heap. Several runtimes can exist at the same time but they cannot exchange objects. Inside a given runtime, no multi-threading is supported.
`JSContext` represents a Javascript context (or Realm). Each JSContext has its own global objects and system objects. There can be several JSContexts per JSRuntime and they can share objects, similary to frames of the same origin sharing Javascript objects in a web browser.
`JSValue` represents a Javascript value which can be a primitive type or an object. Reference counting is used, so it is important to explicitely duplicate (`JS_DupValue()`, increment the reference count) or free (`JS_FreeValue()`, decrement the reference count) JSValues.
C functions can be created with `JS_NewCFunction()`. `JS_SetPropertyFunctionList()` is a shortcut to easily add functions, setters and getters properties to a given object.
Unlike other embedded Javascript engines, there is no implicit stack, so C functions get their parameters as normal C parameters. As a general rule, C functions take constant `JSValue`s as parameters (so they don’t need to free them) and return a newly allocated (=live) `JSValue`.
Exceptions: most C functions can return a Javascript exception. It must be explicitely tested and handled by the C code. The specific `JSValue``JS_EXCEPTION` indicates that an exception occured. The actual exception object is stored in the `JSContext` and can be retrieved with `JS_GetException()`.
Use `JS_Eval()` to evaluate a script or module source.
617
+
使用`JS_Eval()`来评估脚本或模块源代码。
611
618
612
-
If the script or module was compiled to bytecode with `qjsc`, `JS_EvalBinary()` achieves the same result. The advantage is that no compilation is needed so it is faster and smaller because the compiler can be removed from the executable if no `eval` is required.
Note: the bytecode format is linked to a given QuickJS version. Moreover, no security check is done before its execution. Hence the bytecode should not be loaded from untrusted sources. That’s why there is no option to output the bytecode to a binary file in `qjsc`.
C opaque data can be attached to a Javascript object. The type of the C opaque data is determined with the class ID (`JSClassID`) of the object. Hence the first step is to register a new class ID and JS class (`JS_NewClassID()`, `JS_NewClass()`). Then you can create objects of this class with `JS_NewObjectClass()` and get or set the C opaque point with `JS_GetOpaque()`/`JS_SetOpaque()`.
When defining a new JS class, it is possible to declare a finalizer which is called when the object is destroyed. A `gc_mark` method can be provided so that the cycle removal algorithm can find the other objects referenced by this object. Other methods are available to define exotic object behaviors.
The Class ID are globally allocated (i.e. for all runtimes). The JSClass are allocated per `JSRuntime`. `JS_SetClassProto()` is used to define a prototype for a given class in a given JSContext. `JS_NewObjectClass()` sets this prototype in the created object.
@@ -637,7 +644,7 @@ Examples are available in js\_libc.c.
637
644
638
645
#### 3.4.9 执行超时和中断
639
646
640
-
Use `JS_SetInterruptHandler()` to set a callback which is regularly called by the engine when it is executing code. This callback can be used to implement an execution timeout.
@@ -646,35 +653,35 @@ Use `JS_SetInterruptHandler()` to set a callback which is regularly called by th
646
653
647
654
### 4.1 Bytecode
648
655
649
-
The compiler generates bytecode directly with no intermediate representation such as a parse tree, hence it is very fast. Several optimizations passes are done over the generated bytecode.
656
+
编译器直接生成字节码,没有中间表示(如解析树),因此非常快速。在生成的字节码上进行了多个优化步骤。
650
657
651
-
A stack-based bytecode was chosen because it is simple and generates compact code.
658
+
选择了基于堆栈的字节码,因为它简单且生成的代码紧凑。
652
659
653
-
For each function, the maximum stack size is computed at compile time so that no runtime stack overflow tests are needed.
660
+
对于每个函数,编译时计算最大堆栈大小,因此不需要运行时堆栈溢出测试。
654
661
655
-
A separate compressed line number table is maintained for the debug information.
662
+
为调试信息维护了一个单独的压缩行号表。
656
663
657
-
Access to closure variables is optimized and is almost as fast as local variables.
664
+
对闭包变量的访问进行了优化,并且几乎与局部变量一样快。
658
665
659
-
Direct `eval` in strict mode is optimized.
666
+
对严格模式下的直接`eval`进行了优化。
660
667
661
668
### 4.2 Executable generation
662
669
663
670
#### 4.2.1 `qjsc` 编译器
664
671
665
-
The `qjsc` compiler generates C sources from Javascript files. By default the C sources are compiled with the system compiler (`gcc` or `clang`).
The generated C source contains the bytecode of the compiled functions or modules. If a full complete executable is needed, it also contains a `main()` function with the necessary C code to initialize the Javascript engine and to load and execute the compiled functions and modules.
In order to have smaller executables, specific Javascript features can be disabled, in particular `eval` or the regular expressions. The code removal relies on the Link Time Optimization of the system compiler.
`qjsc` works by compiling scripts or modules and then serializing them to a binary format. A subset of this format (without functions or modules) can be used as binary JSON. The example test\_bjson.js shows how to use it.
Warning: the binary JSON format may change without notice, so it should not be used to store persistent data. The test\_bjson.js example is only used to test the binary object format functions.
@@ -686,31 +693,31 @@ C API提供将Javascript字符串转换为C UTF-8编码字符串的函数。最
686
693
687
694
#### 4.3.2 Objects
688
695
689
-
The object shapes (object prototype, property names and flags) are shared between objects to save memory.
696
+
对象形状(对象原型、属性名称和标志)在对象之间共享,以节省内存。
690
697
691
-
Arrays with no holes (except at the end of the array) are optimized.
698
+
优化了没有洞(除了数组末尾)的数组。
692
699
693
700
TypedArray访问已优化。
694
701
695
702
#### 4.3.3 Atoms
696
703
697
-
Object property names and some strings are stored as Atoms (unique strings) to save memory and allow fast comparison. Atoms are represented as a 32 bit integer. Half of the atom range is reserved for immediate integer literals from _0_ to _2^{31}-1_.
Numbers are represented either as 32-bit signed integers or 64-bit IEEE-754 floating point values. Most operations have fast paths for the 32-bit integer case.
引用计数用于自动和准确地释放对象。A separate cycle removal pass is done when the allocated memory becomes too large. The cycle removal algorithm only uses the reference counts and the object content, so no explicit garbage collection roots need to be manipulated in the C code.
It is a Javascript value which can be a primitive type (such as Number, String, ...) or an Object. NaN boxing is used in the 32-bit version to store 64-bit floating point numbers. The representation is optimized so that 32-bit integers and reference counted values can be efficiently tested.
0 commit comments