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