為JavascriptCore添加自訂對象

來源:互聯網
上載者:User

只需在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)));
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.