zoukankan      html  css  js  c++  java
  • 为JavascriptCore添加自定义对象

    只需在GlobalObject的构造函数添加一行:

    putDirect(Identifier(globalExec(), "MyMath"), new (globalExec()) MyMathObject(globalExec(), MyMathObject::createStructure(d()->objectPrototype)), DontEnum | DontDelete);

    就可以直接访问MyMath全局变量了,思路非常简单,就是要添加一个属性到全局访问空间,第一参数是名字,第二个参数是创建的对象。

    所有的属性都必须是JSObject的实例,因此MyMathObject实际上继承与JSObject。

    这时只是添加了一个MyMath变量,但是MyMathObject的定义并不知道,要添加自定义的功能就必须完善这个MyMathObject类,下面是其定义:

    //头文件

    class MyMathObject : public JSObject {
    public:

    //构造函数是必须的,析构函数可无。

        MyMathObject(ExecState*, NonNullPassRefPtr<Structure>);

    /

    MyMathObject(NonNullPassRefPtr<Structure>, const ArgList & args);
        //test for release; 通过createobject返回的jsobject,可以被回收,
        ~MyMathObject();

    //这个是自定义的获取属性的方法,其实非常通用,我也是直接复制过来的。

        virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
        virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);

    //这个是必需的,除非你这个Object没有任何意义,只要有自定义的数据,是必须要有ClassInfo的。

        virtual const ClassInfo* classInfo() const { return &info; }
        static const ClassInfo info;

    //这个是通用的,

        static PassRefPtr<Structure> createStructure(JSValue prototype)
        {
            return Structure::create(prototype, TypeInfo(ObjectType, StructureFlags));
        }
    protected:
        static const unsigned StructureFlags = OverridesGetOwnPropertySlot | JSObject::StructureFlags;
    };

     

    //实现

    //这个大小不能超过最大尺寸,为了实现的方便,每个JSObject有其最大大小

    ASSERT_CLASS_FITS_IN_CELL(MyMathObject);

    static JSValue JSC_HOST_CALL mathProtoFuncCreateObject(ExecState*, JSObject*, JSValue, const ArgList&);
    static JSValue JSC_HOST_CALL mathProtoFuncCos(ExecState*, JSObject*, JSValue, const ArgList&);

    //这个是自定义的属性,通过HashTable组织,

    static const struct HashTableValue mathTableValues[3] = {
       { "createObject", DontEnum|Function, (intptr_t)mathProtoFuncCreateObject, (intptr_t)1 },
       { "cos", DontEnum|Function, (intptr_t)mathProtoFuncCos, (intptr_t)1 },
       { 0, 0, 0, 0 }

    JSC_CONST_HASHTABLE HashTable myMathTable =
        { 4, 3, mathTableValues, 0 };

    // ------------------------------ MyMathObject --------------------------------
    //这个用于for/in,列表
    const ClassInfo MyMathObject::info = { "MyMath", 0, &myMathTable, 0 };

     

    MyMathObject::MyMathObject(ExecState* exec, NonNullPassRefPtr<Structure> structure)
        : JSObject(structure)
    {

    //也可以这种方式添加属性
        putDirectWithoutTransition(Identifier(exec, "E"), jsNumber(exec, exp(1.0)), DontDelete  | ReadOnly);
    }

    MyMathObject::~MyMathObject()
    {
    }
    // ECMA 15.8

    bool MyMathObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
    {
        return getStaticFunctionSlot<JSObject>(exec, &myMathTable, this, propertyName, slot);
    }

    bool MyMathObject::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
    {

        return getStaticFunctionDescriptor<JSObject>(exec, &myMathTable, this, propertyName, descriptor);
    }
    // ------------------------------ Functions --------------------------------

    //也可以方便的创建更多的实例,当然不一定要是MyMathObject,任何对象都可以,只不过这里简单起见

    JSValue  JSC_HOST_CALL mathProtoFuncCreateObject(ExecState* exec, JSObject*, JSValue, const ArgList& args)
    {
        JSValue arg0 = args.at(0);
        JSValue arg1 = args.at(1);
        GlobalObject * g=static_cast<GlobalObject * > (exec->lexicalGlobalObject());
        return new (exec) MyMathObject(g->god->myMathStructure,args);
        //return jsNumber(exec, fabs(args.at(0).toNumber(exec)));
    }

    JSValue JSC_HOST_CALL mathProtoFuncCos(ExecState* exec, JSObject*, JSValue, const ArgList& args)
    {
        return jsNumber(exec, cos(args.at(0).toNumber(exec)));
    }

  • 相关阅读:
    41.分词器简单介绍
    40.倒排索引核心原理
    39.exact value and full text
    38.mapping小例子
    37.query string、_all metadata
    36.分页及deep paging
    35.multi-index和multi-type搜索模式
    BZOJ 1680 [Usaco2005 Mar]Yogurt factory:贪心【只用考虑上一个】
    BZOJ 1637 [Usaco2007 Mar]Balanced Lineup:前缀和 + 差分
    BZOJ 1647 [Usaco2007 Open]Fliptile 翻格子游戏:部分枚举 位运算
  • 原文地址:https://www.cnblogs.com/baizx/p/1801662.html
Copyright © 2011-2022 走看看