dymokomi/luce-js
Source of release 0.6.0
0.6.0 / src / engine / types_bytecode.lucb
#==============================================================================================
#
# types_bytecode - Strings, closure variables, bytecode and the per-class records
#
# DESCRIPTION:
# Port of the declarations of quickjs.c lines 580-842: JSString and ropes, closure
# variables, variable kinds, the pc2line constants, JSFunctionBytecode, and the
# per-class data records (bound functions, for-in iterators, RegExp, Proxy,
# ArrayBuffer, typed arrays, async function states). A flexible array member is
# omitted; its elements follow the struct in memory and are reached by pointer
# arithmetic (the helpers are in the fragment that owns the type).
#
#==============================================================================================
# mark: Strings =================================================================================
## The kinds of atoms (JS_ATOM_TYPE_*).
let js_atom_type_string: u8 = 1
let js_atom_type_global_symbol: u8 = 2
let js_atom_type_symbol: u8 = 3
let js_atom_type_private: u8 = 4
## What kind of key an atom is (JSAtomKindEnum).
enum AtomKind as i32:
string = 0
symbol = 1
private = 2
let js_atom_hash_mask: u32 = (1 << 30) - 1
let js_atom_hash_private: u32 = js_atom_hash_mask
## A flat string (JSString), also the storage of an atom (JSAtomStruct). Its characters
## follow the header: `len` bytes plus a NUL when 8-bit, `len` u16 units when wide.
pub struct String:
## At most 2^31 - 1.
var len: u32
## For js_atom_type_symbol: hash = weakref_count, atom_type = 3; for
## js_atom_type_private: hash = js_atom_hash_private, atom_type = 3. At most 2^30 - 1.
var hash: u32
## atom_index for js_atom_type_symbol.
var hash_next: u32
## False: 8-bit characters, true: 16-bit characters.
var is_wide_char: bool
## Nonzero if an atom: js_atom_type_*.
var atom_type: u8
## An atom's storage is a string (JSAtomStruct).
type AtomStruct = String
## A string concatenation not yet flattened (JSStringRope).
struct StringRope:
var len: u32
## False: 8-bit characters, true: 16-bit characters.
var is_wide_char: u8
## Max depth of the rope tree.
var depth: u8
# XXX: could reduce memory usage by using a direct pointer with bit 0 to select rope or
# string
var left: Value
## Might be the empty string.
var right: Value
# mark: Closures and bytecode ===================================================================
## Where a closure variable comes from (JSClosureTypeEnum).
enum ClosureType as u8:
## 'var_idx' is the index of a local variable in the parent function.
local_ = 0
## 'var_idx' is the index of an argument variable in the parent function.
arg = 1
## 'var_idx' is the index of a closure variable in the parent function.
ref = 2
## 'var_idx' is the index of a closure variable in the parent function referencing a
## global variable.
global_ref = 3
## Global variable declaration (eval code only).
global_decl = 4
## Global variable (eval code only).
global = 5
## Definition of a module variable (eval code only).
module_decl = 6
## Definition of a module import (eval code only).
module_import = 7
## A variable a closure captures (JSClosureVar).
struct ClosureVar:
var closure_type: ClosureType
## Lexical variable.
var is_lexical: bool
## Const variable (is_lexical is true if is_const is).
var is_const: bool
## See VarKind.
var var_kind: VarKind
## is_local: index to a normal variable of the parent function; otherwise: index to a
## closure variable of the parent function.
var var_idx: u16
var var_name: Atom
let arg_scope_index: i32 = 1
let arg_scope_end: i32 = -2
## The kinds of variables (JSVarKindEnum).
enum VarKind as u8:
normal = 0
## Lexical var with function declaration.
function_decl = 1
## Lexical var with async/generator function declaration.
new_function_decl = 2
catch_ = 3
## Function expression name.
function_name = 4
private_field = 5
private_method = 6
private_getter = 7
## Must come after private_getter.
private_setter = 8
## Must come after private_setter.
private_getter_setter = 9
## Global function definition, only in VarDef.
global_function_decl = 10
## A variable of a compiled function (JSBytecodeVarDef).
struct BytecodeVarDef:
var var_name: Atom
## Index into FunctionBytecode.vardefs of the next variable in the same or enclosing
## lexical scope.
var scope_next: i32
var is_const: bool
var is_lexical: bool
var is_captured: bool
## True if VarDef.scope_level != 0.
var has_scope: bool
var var_kind: VarKind
## If is_captured, the index of the corresponding VarRef on the stack.
var var_ref_idx: u16
# for the encoding of the pc2line table
let pc2line_base: i32 = -1
let pc2line_range: i32 = 5
let pc2line_op_first: i32 = 1
let pc2line_diff_pc_max: i32 = (255 - pc2line_op_first) // pc2line_range
## The kinds of functions (JSFunctionKindEnum), a bit set.
let js_func_normal: u8 = 0
let js_func_generator: u8 = 1 << 0
let js_func_async: u8 = 1 << 1
let js_func_async_generator: u8 = js_func_generator | js_func_async
## A compiled function's debug information (the debug struct of JSFunctionBytecode).
struct FunctionBytecodeDebug:
var filename: Atom
var source_len: i32
var pc2line_len: i32
var pc2line_buf: u8*?
var source: u8*?
## A compiled function (JSFunctionBytecode).
struct FunctionBytecode:
## Must come first.
var header: GCObjectHeader
var js_mode: u8
## True if a prototype field is necessary.
var has_prototype: bool
var has_simple_parameter_list: bool
var is_derived_class_constructor: bool
## True if home_object needs to be initialized.
var need_home_object: bool
var func_kind: u8
var new_target_allowed: bool
var super_call_allowed: bool
var super_allowed: bool
var arguments_allowed: bool
var has_debug: bool
var read_only_bytecode: bool
## Used by get_script_or_module_name().
var is_direct_or_indirect_eval: bool
## (Self pointer.)
var byte_code_buf: u8*
var byte_code_len: i32
var func_name: Atom
## Arguments + local variables (arg_count + var_count) (self pointer).
var vardefs: BytecodeVarDef*?
## The variables in the closure (self pointer).
var closure_var: ClosureVar*?
var arg_count: u16
var var_count: u16
## For the length function property.
var defined_arg_count: u16
## Maximum stack size.
var stack_size: u16
## Number of local variable references.
var var_ref_count: u16
## Function realm.
var realm: Context*
## Constant pool (self pointer).
var cpool: Value*?
var cpool_count: i32
var closure_var_count: i32
## The compiled programs of the function's regular expression literals, by first
## evaluation (the port's; see RegExpShared). Before `debug`, which a function without
## debug information does not allocate.
var regexp_cache: RegExpShared*?
var debug: FunctionBytecodeDebug
## A bound function's target and arguments (JSBoundFunction); `argc` values follow it.
struct BoundFunction:
var func_obj: Value
var this_val: Value
var argc: i32
## What an iterator yields (JSIteratorKindEnum).
enum IteratorKind as i32:
key = 0
value = 1
key_and_value = 2
## The state of a for-in loop (JSForInIterator).
struct ForInIterator:
var obj: Value
var idx: u32
var atom_count: u32
var in_prototype_chain: u8
var is_array: u8
## When is_array is false.
var tab_atom: PropertyEnum*?
## A regular expression's pattern and compiled program (JSRegExp). QuickJS keeps the
## program as a bytecode JSString; the port holds luce-regex's program, which the object
## owns, or shares with the other objects of one regular expression literal (see
## regexp_bridge.lucb).
struct RegExp:
var pattern: String*
## Also contains the flags; none until the object is set.
var program: regex.Regex*?
## The literal's shared program when `program` is it (the object holds a reference to
## it), else none (the object owns `program`).
var shared: RegExpShared*?
## The program of a regular expression literal, compiled when the literal is first
## evaluated and shared by the RegExp objects it makes, as QuickJS's objects share the
## bytecode string compiled with the function. The literal's function (its regexp_cache
## list) and each of the objects hold a reference.
struct RegExpShared:
var ref_count: i32
var program: regex.Regex*
## The literal's pattern and flags strings, constants of the function: the cache key.
var pattern: String*
var flags: String*
var next: RegExpShared*?
## A proxy's target and handler (JSProxyData).
struct ProxyData:
var target: Value
var handler: Value
var is_func: u8
var is_revoked: u8
## An ArrayBuffer's storage (JSArrayBuffer).
struct ArrayBuffer:
## 0 if detached.
var byte_length: i32
## -1 if not resizable; >= byte_length otherwise.
var max_byte_length: i32
var detached: u8
## If shared, the array buffer cannot be detached.
var shared: u8
## None if detached.
var data: u8*?
var array_list: cutils.ListHead
var opaque: void*?
var free_func: FreeArrayBufferDataFunc?
## A typed array's or DataView's view of its buffer (JSTypedArray).
struct TypedArray:
## Link to the array buffer.
var link: cutils.ListHead
## Back pointer to the TypedArray/DataView object.
var obj: Object*
## The array buffer it views.
var buffer: Object*
## Byte offset in the array buffer.
var offset: u32
## Byte length in the array buffer.
var length: u32
## Auto-track the length of the backing array buffer.
var track_rab: bool
## The global object's data (JSGlobalObject).
struct GlobalObject:
## Hidden object containing the list of uninitialized variables.
var uninitialized_vars: Value
## A suspended async function or generator (JSAsyncFunctionState); arg_buf, var_buf,
## stack_buf and var_refs follow it.
struct AsyncFunctionState:
var header: GCObjectHeader
## 'this' argument.
var this_val: Value
## Number of function arguments.
var argc: i32
## Used to throw an exception in call_internal().
var throw_flag: bool
## True if the function has returned. The stack frame is no longer valid.
var is_completed: bool
## Only used in JS async functions.
var resolving_funcs: Value[2]
var frame: StackFrame