zoukankan      html  css  js  c++  java
  • 内置对象

    3. 内置对象

    list对象

    
    typedef struct objlist {
        ObjHeader objHeader;
        ValueBuffer elements;
    } ObjList;
    
    newObjList
    removeElement
    insertElement
    

    range对象

    
    typedef struct objrange {
        ObjHeader objHeader;
        int from;
        int to;
    } ObjRange;
    

    map对象

    
    typedef struct Entry {
        Value key;
        Value value;
    } Entry; // entry是分配在堆中的, 因为ObjMap中Entries是可变的
    
    typedef struct ObjMap {
        ObjHeader objHeader;
        int capacity;
        int count;
        Entry *entries;
    } ObjMap;
    
    方法
    mapSet
        注意: 在mapSet过程中, 可能容量会不够, 所以需要扩容, 但是与List扩容方式不同, 因为存储元素不是连续的, 需要先分配一个新的entries, 接着为新的entries的key和value赋新值, 接着遍历老的entries, 将key为undefined类型的value赋值到新的entries中
    mapGet
    clearMap
        注意: 这里的clear是清空entries中的key与value的type, 同时去掉ObjHeader(考虑到GC回收), 而不是回收entries或者map
    removeMap
    

    thread对象

    
    typedef struct ObjThread {
        ObjHeader objHeader;
        Value *stack; // 线程栈的栈底
        Value *esp; // 线程栈的栈顶
        Frame *frames; // 数组, 初始一个线程有4个
        uint32_t unused_frame; // 最新可用frame
        uint32_t frame_capacity; // 该线程对象的Frame容量
        ObjUpvalue *openUpvalue; // 用于缓存
        ObjHeader *caller; // 调用该线程的程序
        Value errorObj; // 导致运行时错误的对象会放在此处, 否则为空, 存放的报错信息, 可想而知是ObjString对象
    } ObjThread;
    
    prepareThread(ObjThread, ObjClosure, Value *stackStart) -> 主要是初始化该线程对象中最新的Frame, 将ObjClosure和stackStart赋给Frame, 接下来可能要通过Frame进行函数调用了
    

    算法

    • hashObj, 主要是从对象中提取出数字或者字符串进行hash即可
  • 相关阅读:
    读书笔记之理想设计的特征
    一些javascript 变量声明的 疑惑
    LINQ 使用方法
    Google MySQL tool releases
    读书笔记之设计的层次
    EF之数据库连接问题The specified named connection is either not found in the configuration, not intended to be used with the Ent
    转载 什么是闭包
    javascript面向对象起步
    Tips
    数据结构在游戏中的应用
  • 原文地址:https://www.cnblogs.com/megachen/p/10383659.html
Copyright © 2011-2022 走看看