为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)));
}

posted on 2010-08-17 17:25  baizx  阅读(1993)  评论(1编辑  收藏  举报