webkit的js对象扩展(二)——binding方式创建自定义对象(多实例)

今天刚好有时间,把创建多实例对象的方法写一下,也就是可以像new Date()一样。关于new操作符有一篇讲的很详细,大家可以先看下http://www.pushiming.com/blog/2009/10/the-new-operator/

了解new操作符后大家应该已经知道,必须有一个构造器才能执行new操作。所以比单实例要多了一个过程,就是要创建构造器,下面我把步骤给贴出来。

以MultiObject为例。

1.添加MultiObject.h文件

  1. #ifndefMultiObject_h
  2. #defineMultiObject_h
  3. #include<wtf/PassRefPtr.h>
  4. #include<wtf/RefCounted.h>
  5. #include<wtf/RefPtr.h>
  6. namespaceWebCore{
  7. classFrame;
  8. classString;
  9. classMultiObject:publicRefCounted<MultiObject>{
  10. public:
  11. staticPassRefPtr<MultiObject>create(Frame*frame)
  12. {
  13. returnadoptRef(newMultiObject(frame));
  14. }
  15. ~MultiObject();
  16. voiddisconnectFrame();
  17. Frame*frame()const{returnm_frame;}
  18. Stringdescription()const;
  19. private:
  20. MultiObject(Frame*);
  21. Frame*m_frame;
  22. };
  23. }
  24. #endif

2.添加MultiObject.cpp文件

  1. #include"MultiObject.h"
  2. #include"PlatformString.h"
  3. #include"Frame.h"
  4. #include"Page.h"
  5. #include"RangerIFrameWidget.h"
  6. #include"config.h"
  7. namespaceWebCore{
  8. MultiObject::MultiObject(Frame*frame)
  9. :m_frame(frame)
  10. {
  11. }
  12. MultiObject::~MultiObject()
  13. {
  14. disconnectFrame();
  15. }
  16. voidMultiObject::disconnectFrame()
  17. {
  18. m_frame=0;
  19. }
  20. StringMultiObject::description()const//对象的属性
  21. {
  22. return"HelloWorld!";
  23. }
  24. }
3.添加MultiObject.idl文件

  1. modulewindow{
  2. interface[
  3. CustomConstructor
  4. ]MultiObject{
  5. readonlyattributeDOMStringdescription;
  6. };
  7. }

4.修改DOMWindow.h文件

添加声明如下:

  1. attribute[JSCCustomGetter]MultiObjectConstructorMultiObject;


WebCore/bindings/js

5.添加JSMultiObjectConstructor.h

  1. #ifndef__JSMultiObjectConstructor_h__
  2. #define__JSMultiObjectConstructor_h__
  3. #include"JSDOMBinding.h"
  4. namespaceWebCore{
  5. classJSMultiObjectConstructor:publicDOMConstructorObject{
  6. public:
  7. JSMultiObjectConstructor(JSC::ExecState*,JSDOMGlobalObject*);
  8. staticconstJSC::ClassInfos_info;
  9. private:
  10. virtualJSC::ConstructTypegetConstructData(JSC::ConstructData&);
  11. virtualconstJSC::ClassInfo*classInfo()const{return&s_info;}
  12. };
  13. }//namespaceWebCore
  14. #endif//__JSMultiObjectConstructor_h__
6.添加JSMultiObjectConstructor.cpp
  1. #include"config.h"
  2. #include"JSMultiObjectConstructor.h"
  3. #include"JSMultiObject.h"
  4. #include"ScriptExecutionContext.h"
  5. #include"MultiObject.h"
  6. #include<runtime/Error.h>
  7. #include"JSDOMWindowBase.h"
  8. usingnamespaceJSC;
  9. namespaceWebCore{
  10. ASSERT_CLASS_FITS_IN_CELL(JSMultiObjectConstructor);
  11. constClassInfoJSMultiObjectConstructor::s_info={"MultiObjectConstructor",0,0,0};
  12. JSMultiObjectConstructor::JSMultiObjectConstructor(ExecState*exec,JSDOMGlobalObject*globalObject)
  13. :DOMConstructorObject(JSMultiObjectConstructor::createStructure(globalObject->objectPrototype()),globalObject)
  14. {
  15. putDirect(exec->propertyNames().prototype,JSMultiObjectPrototype::self(exec,globalObject),None);
  16. }
  17. staticJSObject*constructMultiObject(ExecState*exec,JSObject*constructor,constArgList&)
  18. {
  19. JSMultiObjectConstructor*jsConstructor=static_cast<JSMultiObjectConstructor*>(constructor);
  20. JSDOMWindowBase*window=static_cast<JSDOMWindowBase*>(jsConstructor->globalObject());
  21. if(!window||!window->impl()||!window->impl()->frame())
  22. returnError::create(exec,ReferenceError,"CannotcreateMultiObject...",-1,-1,UString());
  23. RefPtr<MultiObject>MultiObject=MultiObject::create(window->impl()->frame());
  24. if(!MultiObject.get())
  25. returnError::create(exec,ReferenceError,"NotsupportMultiObject...",-1,-1,UString());
  26. returnCREATE_DOM_OBJECT_WRAPPER(exec,jsConstructor->globalObject(),MultiObject,MultiObject.get());
  27. }
  28. ConstructTypeJSMultiObjectConstructor::getConstructData(ConstructData&constructData)
  29. {
  30. constructData.native.function=constructMultiObject;
  31. returnConstructTypeHost;
  32. }
  33. }//namespaceWebCore
7.修改JSDOMWindowCustom.cpp

  1. #include"JSMultiObjectConstructor.h"
  2. JSValueJSDOMWindow::multiObject(ExecState*exec)const
  3. {
  4. returngetDOMConstructor<JSMultiObjectConstructor>(exec,this);
  5. }

OK,再修改相应的CMakeLists.txt就行了。

注意:头文件包含依据自己的项目。

你可能感兴趣的:(webkit)