博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebKit之addToJavaScriptWindowObject()分析
阅读量:4026 次
发布时间:2019-05-24

本文共 4604 字,大约阅读时间需要 15 分钟。

## Qt暴露QObject给JSC引擎的API源码

void QWebFrameAdapter::addToJavaScriptWindowObject(const QString& name, QObject* object, ValueOwnership ownership){    if (!pageAdapter->settings->testAttribute(QWebSettings::JavascriptEnabled))        return;    JSC::Bindings::QtInstance::ValueOwnership valueOwnership = static_cast
(ownership); JSDOMWindow* window = toJSDOMWindow(frame, mainThreadNormalWorld()); JSC::Bindings::RootObject* root; if (valueOwnership == JSC::Bindings::QtInstance::QtOwnership) root = frame->script()->cacheableBindingRootObject(); else root = frame->script()->bindingRootObject(); if (!window) { qDebug() << "Warning: couldn't get window object"; return; } if (!root) { qDebug() << "Warning: couldn't get root object"; return; } JSC::ExecState* exec = window->globalExec(); JSC::JSLockHolder lock(exec); JSC::JSObject* runtimeObject = JSC::Bindings::QtInstance::getQtInstance(object, root, valueOwnership)->createRuntimeObject(exec); JSC::PutPropertySlot slot; window->methodTable()->put(window, exec, JSC::Identifier(&exec->vm(), reinterpret_cast_ptr
(name.constData()), name.length()), runtimeObject, slot);}

##   JSC桥实例的创建源码

#include "config.h"#include "BridgeJSC.h"#include "JSDOMWindowBase.h"#include "runtime_object.h"#include "runtime_root.h"#include "runtime/JSLock.h"#include "runtime/ObjectPrototype.h"#if PLATFORM(QT)#include "qt_instance.h"#endifnamespace JSC {namespace Bindings {Array::Array(PassRefPtr
rootObject) : m_rootObject(rootObject){ ASSERT(m_rootObject);}Array::~Array(){}Instance::Instance(PassRefPtr
rootObject) : m_rootObject(rootObject){ ASSERT(m_rootObject);}Instance::~Instance(){ ASSERT(!m_runtimeObject);}void Instance::begin(){ virtualBegin();}void Instance::end(){ virtualEnd();}JSObject* Instance::createRuntimeObject(ExecState* exec){ ASSERT(m_rootObject); ASSERT(m_rootObject->isValid()); if (RuntimeObject* existingObject = m_runtimeObject.get()) return existingObject; JSLockHolder lock(exec); RuntimeObject* newObject = newRuntimeObject(exec); m_runtimeObject = PassWeak
(newObject); m_rootObject->addRuntimeObject(exec->vm(), newObject); return newObject;}RuntimeObject* Instance::newRuntimeObject(ExecState* exec){ JSLockHolder lock(exec); // FIXME: deprecatedGetDOMStructure uses the prototype off of the wrong global object // We need to pass in the right global object for "i". return RuntimeObject::create(exec, exec->lexicalGlobalObject(), WebCore::deprecatedGetDOMStructure
(exec), this);}void Instance::willInvalidateRuntimeObject(){ m_runtimeObject.clear();}RootObject* Instance::rootObject() const{ return m_rootObject && m_rootObject->isValid() ? m_rootObject.get() : 0;}

## Qt基于JSC内定的bridge实例进行继承和扩展(为qt平台方便访问)

class QtInstance : public Instance {public:    enum ValueOwnership {        QtOwnership,        ScriptOwnership,        AutoOwnership    };    ~QtInstance();    virtual Class* getClass() const;    virtual RuntimeObject* newRuntimeObject(ExecState*);    virtual void begin();    virtual void end();    virtual JSValue valueOf(ExecState*) const;    virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const;    virtual JSValue getMethod(ExecState*, PropertyName);    virtual JSValue invokeMethod(ExecState*, RuntimeMethod*);    virtual void getPropertyNames(ExecState*, PropertyNameArray&);    JSValue stringValue(ExecState* exec) const;    JSValue numberValue(ExecState* exec) const;    JSValue booleanValue() const;    QObject* getObject() const { return m_object.data(); }    QObject* hashKey() const { return m_hashkey; }    static PassRefPtr
getQtInstance(QObject*, PassRefPtr
, ValueOwnership); virtual bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&); virtual void put(JSObject*, ExecState*, PropertyName, JSValue, PutPropertySlot&); static QtInstance* getInstance(JSObject*);private: static PassRefPtr
create(QObject *instance, PassRefPtr
rootObject, ValueOwnership ownership) { return adoptRef(new QtInstance(instance, rootObject, ownership)); } friend class QtClass; friend class QtField; friend class QtRuntimeMethod; QtInstance(QObject*, PassRefPtr
, ValueOwnership); // Factory produced only.. mutable QtClass* m_class; QPointer
m_object; QObject* m_hashkey; mutable QHash
m_methods; mutable QHash
m_fields; ValueOwnership m_ownership;};

转载地址:http://rfvbi.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]167. Two Sum II - Input array is sorted
查看>>
[LeetCode BY Python]169. Majority Element
查看>>
[LeetCode By Python]172. Factorial Trailing Zeroes
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
python jieba分词模块的基本用法
查看>>
[CCF BY C++]2017.12 最小差值
查看>>
[CCF BY C++]2017-12 游戏
查看>>
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>
面试---刷牛客算法题
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
在android上运行native可执行程序
查看>>
Phone双模修改涉及文件列表
查看>>
android UI小知识点
查看>>