Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-js

Source of release 0.6.0

0.6.0 / src / engine / types_object.lucb

#==============================================================================================
#
#   types_object - Modules, jobs, properties, shapes, objects and the Map records
#
#   DESCRIPTION:
#       Port of the declarations of quickjs.c lines 843-1110: modules and their import and
#       export entries, jobs, properties, shapes, JSObject and the Map records. 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: Modules and jobs ========================================================================

## A module a module requests (JSReqModuleEntry).
struct ReqModuleEntry:
    var module_name: Atom
    ## Used during resolution.
    var module: ModuleDef*?
    ## undefined, or an object containing the attributes as key/value.
    var attributes: Value

## Whether an export is the module's own or re-exported (JSExportTypeEnum).
enum ExportType as i32:
    local_ = 0
    indirect = 1

## A local export's variable (the local struct of JSExportEntry).
struct ExportLocal:
    ## Closure variable index.
    var var_idx: i32
    ## If present, reference to the variable.
    var var_ref: VarRef*?

## The payload of an export entry.
union ExportEntryUnion:
    ## For a local export.
    local_: ExportLocal
    ## Module for an indirect export.
    req_module_idx: i32

## An export of a module (JSExportEntry).
struct ExportEntry:
    var u: ExportEntryUnion
    var export_type: ExportType
    ## '*' if export ns from. Not used for local export after compilation.
    var local_name: Atom
    ## Exported variable name.
    var export_name: Atom

## `export * from` (JSStarExportEntry).
struct StarExportEntry:
    ## In req_module_entries.
    var req_module_idx: i32

## An import of a module (JSImportEntry).
struct ImportEntry:
    ## Closure variable index.
    var var_idx: i32
    ## import_name = '*' is a valid import name, so need a flag.
    var is_star: bool
    var import_name: Atom
    ## In req_module_entries.
    var req_module_idx: i32

## A module's evaluation status (JSModuleStatus).
enum ModuleStatus as u8:
    unlinked = 0
    linking = 1
    linked = 2
    evaluating = 3
    evaluating_async = 4
    evaluated = 5

## A module (JSModuleDef).
pub struct ModuleDef:
    ## Must come first.
    var header: GCObjectHeader
    var module_name: Atom
    var link: cutils.ListHead

    var req_module_entries: ReqModuleEntry*?
    var req_module_entries_count: i32
    var req_module_entries_size: i32

    var export_entries: ExportEntry*?
    var export_entries_count: i32
    var export_entries_size: i32

    var star_export_entries: StarExportEntry*?
    var star_export_entries_count: i32
    var star_export_entries_size: i32

    var import_entries: ImportEntry*?
    var import_entries_count: i32
    var import_entries_size: i32

    var module_ns: Value
    ## Only used for JS modules.
    var func_obj: Value
    ## Only used for C modules.
    var init_func: ModuleInitFunc?
    ## True if func_obj contains await.
    var has_tla: bool
    var resolved: bool
    var func_created: bool
    var status: ModuleStatus
    # temp use during js_module_link() & js_module_evaluate()
    var dfs_index: i32
    var dfs_ancestor_index: i32
    var stack_prev: ModuleDef*?
    # temp use during js_module_evaluate()
    var async_parent_modules: ModuleDef**?
    var async_parent_modules_count: i32
    var async_parent_modules_size: i32
    var pending_async_dependencies: i32
    ## True: async_evaluation_timestamp corresponds to [[AsyncEvaluationOrder]]; false:
    ## [[AsyncEvaluationOrder]] is UNSET or DONE.
    var async_evaluation: bool
    var async_evaluation_timestamp: i64
    var cycle_root: ModuleDef*?
    ## Corresponds to the spec field: capability.
    var promise: Value
    ## Corresponds to the spec field: capability.
    var resolving_funcs: Value[2]

    ## True if evaluation yielded an exception. It is saved in eval_exception.
    var eval_has_exception: bool
    var eval_exception: Value
    ## For import.meta.
    var meta_obj: Value
    ## Private value for C modules.
    var private_value: Value

## A queued job (JSJobEntry); `argc` values follow it.
struct JobEntry:
    var link: cutils.ListHead
    var realm: Context*
    var job_func: JobFunc
    var argc: i32

# mark: Properties and shapes ===================================================================

## A getter/setter pair (the getset struct of JSProperty); none if undefined.
struct PropertyGetSet:
    var getter: Object*?
    var setter: Object*?

## An auto-initialized property's initializer (the init struct of JSProperty). In order
## to use only 2 pointers, the realm and the init id share a word.
struct PropertyInit:
    ## The realm and init_id (AutoInitId) in the 2 low bits.
    var realm_and_id: usize
    var opaque: void*?

## A property's storage, by its shape flags' kind (JSProperty).
union PropertyUnion:
    ## prop_normal.
    value: Value
    ## prop_getset.
    getset: PropertyGetSet
    ## prop_varref.
    var_ref: VarRef*
    ## prop_autoinit.
    init: PropertyInit

## A property's storage (JSProperty).
struct Property:
    var u: PropertyUnion

let js_prop_initial_size: i32 = 2
## Must be a power of two.
let js_prop_initial_hash_size: i32 = 4

## A property's key and flags in a shape (JSShapeProperty).
struct ShapeProperty:
    ## 0 if last in the list; at most 2^26 - 1.
    var hash_next: u32
    ## JS_PROP_XXX; 6 bits.
    var flags: u8
    ## no_atom = a free property entry.
    var atom: Atom

## An object's prototype and property layout, shared between objects (JSShape).
## `prop_hash_mask + 1` u32 hash entries follow the header, then `prop_size`
## ShapeProperty entries.
struct Shape:
    var header: GCObjectHeader
    ## True if the shape is inserted in the shape hash table. If not, Shape.hash is not
    ## valid.
    var is_hashed: u8
    ## Current hash value.
    var hash: u32
    ## >= 2.
    var prop_hash_mask: u32
    ## Allocated properties.
    var prop_size: i32
    ## Includes deleted properties.
    var prop_count: i32
    var deleted_prop_count: i32
    ## In Runtime.shape_hash[h] list.
    var shape_hash_next: Shape*?
    var proto: Object*?

# mark: Objects =================================================================================

## A bytecode function's closure (the func struct of JSObject's union), also used by
## generator, async and async generator functions.
struct ObjectFunc:
    var function_bytecode: FunctionBytecode*
    var var_refs: VarRef*?*?
    ## For 'super' access.
    var home_object: Object*?

## A C function's data (the cfunc struct of JSObject's union).
struct ObjectCFunc:
    var realm: Context*
    var c_function: CFunctionType
    var length: u8
    var cproto: CFunctionEnum
    var magic: i16

## The first word of a fast array (the u1 union of JSObject.u.array).
union ObjectArrayU1:
    ## ClassId.array.
    size: u32
    ## Typed arrays.
    typed_array: TypedArray*

## The element storage of a fast array (the u union of JSObject.u.array).
union ObjectArrayValues:
    ## ClassId.array, ClassId.arguments.
    values: Value*
    ## ClassId.mapped_arguments.
    var_refs: VarRef**
    ## Typed arrays; none while the array has no storage.
    ptr: void*?
    int8_ptr: i8*
    uint8_ptr: u8*
    int16_ptr: i16*
    uint16_ptr: u16*
    int32_ptr: i32*
    uint32_ptr: u32*
    int64_ptr: i64*
    uint64_ptr: u64*
    fp16_ptr: u16*
    float_ptr: f32*
    double_ptr: f64*

## The array part of fast arrays and typed arrays (the array struct of JSObject's union).
struct ObjectArray:
    var u1: ObjectArrayU1
    var u: ObjectArrayValues
    ## <= 2^31-1. 0 for a detached typed array.
    var count: u32

## An object's class-specific data (the u union of JSObject).
union ObjectUnion:
    ## None for an object whose class data is not set yet.
    opaque: void*?
    bound_function: BoundFunction*
    c_function_data_record: CFunctionDataRecord*
    for_in_iterator: ForInIterator*
    array_buffer: ArrayBuffer*
    typed_array: TypedArray*
    map_state: MapState*
    map_iterator_data: MapIteratorData*
    array_iterator_data: ArrayIteratorData*
    regexp_string_iterator_data: RegExpStringIteratorData*
    generator_data: GeneratorData*
    iterator_concat_data: IteratorConcatData*
    iterator_helper_data: IteratorHelperData*
    iterator_wrap_data: IteratorWrapData*
    proxy_data: ProxyData*
    promise_data: PromiseData*
    promise_function_data: PromiseFunctionData*
    async_function_data: AsyncFunctionState*
    async_from_sync_iterator_data: AsyncFromSyncIteratorData*
    async_generator_data: AsyncGeneratorData*
    func_: ObjectFunc
    cfunc: ObjectCFunc
    array: ObjectArray
    regexp: RegExp
    object_data: Value
    global_object: GlobalObject

## A JavaScript object (JSObject).
pub struct Object:
    var header: GCObjectHeader
    ## True if the array prototype is "normal":
    ## - no small index properties which are get/set or non writable
    ## - its prototype is Object.prototype
    ## - Object.prototype has no small index properties which are get/set or non writable
    ## - the prototype of Object.prototype is null (always true as it is immutable)
    var is_std_array_prototype: bool
    var extensible: bool
    ## Only used when freeing objects with cycles.
    var free_mark: bool
    ## True if the object has exotic property handlers.
    var is_exotic: bool
    ## True if u.array is used for get/put (for ClassId.array, arguments, mapped arguments
    ## and typed arrays).
    var fast_array: bool
    ## True if the object is a constructor function.
    var is_constructor: bool
    ## Cannot modify the prototype.
    var has_immutable_prototype: bool
    ## Used in write_object_rec().
    var tmp_mark: bool
    ## Specific annex B IsHtmlDDA behavior.
    var is_htmldda: bool
    ## See ClassId; a run-time class id is carried by cast.
    var class_id: ClassId
    ## The number of weak references to this object. The object structure is freed only
    ## if its ref_count and weakref_count are both 0.
    var weakref_count: u32
    ## Prototype and property names + flags.
    var shape: Shape*
    ## Array of properties.
    var prop: Property*
    var u: ObjectUnion

# mark: Map records =============================================================================

## One entry of a Map or Set (JSMapRecord).
struct MapRecord:
    ## Used during enumeration to avoid freeing the record.
    var ref_count: i32
    ## True if the record is deleted.
    var empty: bool
    var link: cutils.ListHead
    var hash_next: MapRecord*?
    var key: Value
    var value: Value

## A Map, Set, WeakMap or WeakSet (JSMapState).
struct MapState:
    ## True if WeakSet/WeakMap.
    var is_weak: bool
    ## List of MapRecord.link.
    var records: cutils.ListHead
    var record_count: u32
    var hash_table: MapRecord*?*?
    var hash_bits: i32
    ## = 2 ^ hash_bits.
    var hash_size: u32
    ## The count at which a hash table resize is needed.
    var record_count_threshold: u32
    ## Only used if is_weak.
    var weakref_header: WeakRefHeader