Skip to content

Commit 3449e36

Browse files
committed
refactor: refactor node, element and document.
1 parent d6f00d7 commit 3449e36

File tree

16 files changed

+750
-699
lines changed

16 files changed

+750
-699
lines changed

bridge/bindings/qjs/bom/window.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,18 @@ IMPL_FUNCTION(Window, cancelAnimationFrame)(JSContext* ctx, JSValue this_val, in
162162
}
163163

164164
Window* Window::create(JSContext* ctx) {
165-
return makeGarbageCollected<Window>()->initialize<Window>(ctx, &classId, nullptr);
165+
auto* context = static_cast<ExecutionContext*>(JS_GetContextOpaque(ctx));
166+
JSValue prototype = context->contextData()->prototypeForType(&windowTypeInfo);
167+
168+
auto* window = makeGarbageCollected<Window>()->initialize<Window>(ctx, &classId, nullptr);
169+
170+
// Let window inherit Window prototype methods.
171+
JS_SetPrototype(ctx, window->toQuickJS(), prototype);
172+
173+
return window;
166174
}
167175

168-
DocumentInstance* Window::document() {
176+
Document* Window::document() {
169177
return context()->document();
170178
}
171179

bridge/bindings/qjs/bom/window.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,15 @@ class Window : public EventTarget {
4545
void trace(JSRuntime *rt, JSValue val, JS_MarkFunc *mark_func) const override;
4646

4747
private:
48-
DocumentInstance* document();
48+
Document* document();
4949

5050
Location* m_location{nullptr};
5151
JSValue onerror{JS_NULL};
5252
friend ExecutionContext;
5353
};
5454

5555
auto windowCreator = [](JSContext* ctx, JSValueConst func_obj, JSValueConst this_val, int argc, JSValueConst* argv, int flags) -> JSValue {
56-
57-
auto* type = static_cast<const WrapperTypeInfo*>(JS_GetOpaque(func_obj, JSValueGetClassId(func_obj)));
5856
auto* window = Window::create(ctx);
59-
auto* context = static_cast<ExecutionContext*>(JS_GetContextOpaque(ctx));
60-
JSValue prototype = context->contextData()->prototypeForType(type);
61-
62-
// Let eventTarget instance inherit EventTarget prototype methods.
63-
JS_SetPrototype(ctx, window->toQuickJS(), prototype);
6457
return window->toQuickJS();
6558
};
6659

bridge/bindings/qjs/dom/document.cc

Lines changed: 134 additions & 129 deletions
Large diffs are not rendered by default.

bridge/bindings/qjs/dom/document.h

Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,37 @@ namespace kraken::binding::qjs {
1515

1616
void bindDocument(std::unique_ptr<ExecutionContext>& context);
1717

18-
using TraverseHandler = std::function<bool(NodeInstance*)>;
18+
using TraverseHandler = std::function<bool(Node*)>;
1919

20-
void traverseNode(NodeInstance* node, TraverseHandler handler);
20+
void traverseNode(Node* node, TraverseHandler handler);
2121

22-
class Document : public Node {
23-
public:
24-
static JSClassID kDocumentClassID;
25-
26-
Document() = delete;
27-
Document(ExecutionContext* context);
28-
29-
static JSClassID classId();
22+
class DocumentCookie {
23+
public:
24+
DocumentCookie() = default;
3025

31-
JSValue instanceConstructor(JSContext* ctx, JSValue func_obj, JSValue this_val, int argc, JSValue* argv) override;
26+
std::string getCookie();
27+
void setCookie(std::string& str);
3228

33-
OBJECT_INSTANCE(Document);
29+
private:
30+
std::unordered_map<std::string, std::string> cookiePairs;
31+
};
3432

35-
static JSValue createEvent(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
36-
static JSValue createElement(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
37-
static JSValue createTextNode(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
38-
static JSValue createDocumentFragment(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
39-
static JSValue createComment(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
40-
static JSValue getElementById(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
41-
static JSValue getElementsByTagName(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
42-
static JSValue getElementsByClassName(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
4333

44-
JSValue getElementConstructor(ExecutionContext* context, const std::string& tagName);
45-
bool isCustomElement(const std::string& tagName);
34+
class Document : public Node {
35+
public:
36+
static JSClassID classId;
37+
Document* create(JSContext* ctx);
38+
explicit Document();
39+
40+
DEFINE_FUNCTION(createEvent);
41+
DEFINE_FUNCTION(createElement);
42+
DEFINE_FUNCTION(createTextNode);
43+
DEFINE_FUNCTION(createDocumentFragment);
44+
DEFINE_FUNCTION(createComment);
45+
DEFINE_FUNCTION(getElementById);
46+
DEFINE_FUNCTION(getElementsByTagName);
47+
DEFINE_FUNCTION(getElementsByClassName);
4648

47-
private:
4849
DEFINE_PROTOTYPE_READONLY_PROPERTY(nodeName);
4950
DEFINE_PROTOTYPE_READONLY_PROPERTY(all);
5051
DEFINE_PROTOTYPE_READONLY_PROPERTY(documentElement);
@@ -54,58 +55,48 @@ class Document : public Node {
5455
DEFINE_PROTOTYPE_PROPERTY(cookie);
5556
DEFINE_PROTOTYPE_PROPERTY(body);
5657

57-
DEFINE_PROTOTYPE_FUNCTION(createEvent, 1);
58-
DEFINE_PROTOTYPE_FUNCTION(createElement, 1);
59-
DEFINE_PROTOTYPE_FUNCTION(createDocumentFragment, 0);
60-
DEFINE_PROTOTYPE_FUNCTION(createTextNode, 1);
61-
DEFINE_PROTOTYPE_FUNCTION(createComment, 1);
62-
DEFINE_PROTOTYPE_FUNCTION(getElementById, 1);
63-
DEFINE_PROTOTYPE_FUNCTION(getElementsByTagName, 1);
64-
DEFINE_PROTOTYPE_FUNCTION(getElementsByClassName, 1);
58+
JSValue getElementConstructor(ExecutionContext* context, const std::string& tagName);
59+
bool isCustomElement(const std::string& tagName);
6560

66-
void defineElement(const std::string& tagName, Element* constructor);
61+
int32_t requestAnimationFrame(FrameCallback* frameCallback);
62+
void cancelAnimationFrame(uint32_t callbackId);
63+
void trace(JSRuntime* rt, JSValue val, JS_MarkFunc* mark_func) const override;
64+
void dispose() const override;
65+
66+
private:
67+
68+
void removeElementById(JSAtom id, Element* element);
69+
void addElementById(JSAtom id, Element* element);
70+
Element* getDocumentElement();
71+
std::unordered_map<JSAtom, std::vector<Element*>> m_elementMapById;
72+
Element* m_documentElement{nullptr};
73+
std::unique_ptr<DocumentCookie> m_cookie;
74+
75+
ScriptAnimationController* m_scriptAnimationController;
76+
// DEFINE_PROTOTYPE_FUNCTION(createEvent, 1);
77+
// DEFINE_PROTOTYPE_FUNCTION(createElement, 1);
78+
// DEFINE_PROTOTYPE_FUNCTION(createDocumentFragment, 0);
79+
// DEFINE_PROTOTYPE_FUNCTION(createTextNode, 1);
80+
// DEFINE_PROTOTYPE_FUNCTION(createComment, 1);
81+
// DEFINE_PROTOTYPE_FUNCTION(getElementById, 1);
82+
// DEFINE_PROTOTYPE_FUNCTION(getElementsByTagName, 1);
83+
// DEFINE_PROTOTYPE_FUNCTION(getElementsByClassName, 1);
6784

68-
friend DocumentInstance;
85+
void defineElement(const std::string& tagName, Element* constructor);
6986

7087
bool event_registered{false};
7188
bool document_registered{false};
7289
std::unordered_map<std::string, Element*> elementConstructorMap;
7390
};
7491

75-
class DocumentCookie {
76-
public:
77-
DocumentCookie() = default;
78-
79-
std::string getCookie();
80-
void setCookie(std::string& str);
81-
82-
private:
83-
std::unordered_map<std::string, std::string> cookiePairs;
92+
auto documentCreator = [](JSContext* ctx, JSValueConst func_obj, JSValueConst this_val, int argc, JSValueConst* argv, int flags) -> JSValue {
93+
return JS_ThrowTypeError(ctx, "Illegal constructor");
8494
};
8595

86-
class DocumentInstance : public NodeInstance {
87-
public:
88-
DocumentInstance() = delete;
89-
explicit DocumentInstance(Document* document);
90-
~DocumentInstance();
91-
92-
int32_t requestAnimationFrame(FrameCallback* frameCallback);
93-
void cancelAnimationFrame(uint32_t callbackId);
94-
void trace(JSRuntime* rt, JSValue val, JS_MarkFunc* mark_func) override;
95-
96-
private:
97-
void removeElementById(JSAtom id, ElementInstance* element);
98-
void addElementById(JSAtom id, ElementInstance* element);
99-
ElementInstance* getDocumentElement();
100-
std::unordered_map<JSAtom, std::vector<ElementInstance*>> m_elementMapById;
101-
ElementInstance* m_documentElement{nullptr};
102-
std::unique_ptr<DocumentCookie> m_cookie;
103-
104-
ScriptAnimationController* m_scriptAnimationController;
105-
106-
friend Document;
107-
friend ElementInstance;
108-
friend ExecutionContext;
96+
const WrapperTypeInfo documentTypeInfo = {
97+
"Document",
98+
&nodeTypeInfo,
99+
documentCreator
109100
};
110101

111102
} // namespace kraken::binding::qjs

bridge/bindings/qjs/dom/document_fragment.cc

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,28 @@
1010
namespace kraken::binding::qjs {
1111

1212
void bindDocumentFragment(std::unique_ptr<ExecutionContext>& context) {
13-
auto* constructor = DocumentFragment::instance(context.get());
14-
context->defineGlobalProperty("DocumentFragment", constructor->jsObject);
13+
JSValue classObject = context->contextData()->constructorForType(&documentFragmentInfo);
14+
context->defineGlobalProperty("DocumentFragment", classObject);
1515
}
1616

17-
std::once_flag kDocumentFragmentFlag;
17+
JSValue DocumentFragment::constructor(ExecutionContext* context) {
18+
return context->contextData()->constructorForType(&documentFragmentInfo);
19+
}
1820

19-
JSClassID DocumentFragment::kDocumentFragmentID{0};
21+
DocumentFragment * DocumentFragment::create(JSContext* ctx) {
22+
auto* context = static_cast<ExecutionContext*>(JS_GetContextOpaque(ctx));
23+
JSValue prototype = context->contextData()->prototypeForType(&documentFragmentInfo);
24+
auto* documentFragment = makeGarbageCollected<DocumentFragment>()->initialize<DocumentFragment>(ctx, &classId);
2025

21-
DocumentFragment::DocumentFragment(ExecutionContext* context) : Node(context) {
22-
std::call_once(kDocumentFragmentFlag, []() { JS_NewClassID(&kDocumentFragmentID); });
23-
JS_SetPrototype(m_ctx, m_prototypeObject, Node::instance(m_context)->prototype());
24-
}
26+
// Let documentFragment instance inherit Document prototype methods.
27+
JS_SetPrototype(ctx, documentFragment->toQuickJS(), prototype);
2528

26-
JSClassID DocumentFragment::classId() {
27-
return kDocumentFragmentID;
29+
return documentFragment;
2830
}
2931

30-
JSValue DocumentFragment::instanceConstructor(JSContext* ctx, JSValue func_obj, JSValue this_val, int argc, JSValue* argv) {
31-
return (new DocumentFragmentInstance(this))->jsObject;
32+
DocumentFragment::DocumentFragment() {
33+
setNodeFlag(DocumentFragment::NodeFlag::IsDocumentFragment);
34+
context()->uiCommandBuffer()->addCommand(eventTargetId(), UICommand::createDocumentFragment, nativeEventTarget);
3235
}
3336

34-
DocumentFragmentInstance::DocumentFragmentInstance(DocumentFragment* fragment) : NodeInstance(fragment, NodeType::DOCUMENT_FRAGMENT_NODE, DocumentFragment::classId(), "DocumentFragment") {
35-
setNodeFlag(DocumentFragmentInstance::NodeFlag::IsDocumentFragment);
36-
m_context->uiCommandBuffer()->addCommand(m_eventTargetId, UICommand::createDocumentFragment, nativeEventTarget);
37-
}
3837
} // namespace kraken::binding::qjs

bridge/bindings/qjs/dom/document_fragment.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@ void bindDocumentFragment(std::unique_ptr<ExecutionContext>& context);
1414

1515
class DocumentFragment : public Node {
1616
public:
17-
static JSClassID kDocumentFragmentID;
18-
static JSClassID classId();
19-
20-
DocumentFragment() = delete;
21-
explicit DocumentFragment(ExecutionContext* context);
22-
23-
JSValue instanceConstructor(JSContext* ctx, JSValue func_obj, JSValue this_val, int argc, JSValue* argv) override;
17+
static JSClassID classId;
18+
// Return the constructor class object of DocumentFragment.
19+
static JSValue constructor(ExecutionContext* context);
20+
DocumentFragment* create(JSContext* ctx);
21+
DocumentFragment();
22+
23+
private:
24+
friend Node;
25+
};
2426

25-
OBJECT_INSTANCE(DocumentFragment);
27+
auto documentFragmentCreator = [](JSContext* ctx, JSValueConst func_obj, JSValueConst this_val, int argc, JSValueConst* argv, int flags) -> JSValue {
28+
auto* eventTarget = EventTarget::create(ctx);
29+
return eventTarget->toQuickJS();
2630
};
2731

28-
class DocumentFragmentInstance : public NodeInstance {
29-
public:
30-
DocumentFragmentInstance() = delete;
31-
DocumentFragmentInstance(DocumentFragment* fragment);
32+
const WrapperTypeInfo documentFragmentInfo = {
33+
"DocumentFragment",
34+
&nodeTypeInfo,
35+
documentFragmentCreator
3236
};
3337

3438
} // namespace kraken::binding::qjs

0 commit comments

Comments
 (0)