Skip to content

Commit 4ee852f

Browse files
committed
feat: support generate dictionary type code.
1 parent b1b365f commit 4ee852f

40 files changed

+664
-413
lines changed

bridge/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ if ($ENV{KRAKEN_JS_ENGINE} MATCHES "quickjs")
311311
out/qjs_blob.h
312312
out/qjs_event.cc
313313
out/qjs_event.h
314+
out/qjs_error_event.h
315+
out/qjs_error_event.cc
316+
out/qjs_event_init.h
317+
out/qjs_event_init.cc
314318
out/qjs_event_target.cc
315319
out/qjs_event_target.h
316320
out/event_type_names.h

bridge/bindings/qjs/atom_string.cc

-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,5 @@
77

88
namespace kraken {
99

10-
JSValue StaticAtomicString::ToQuickJS(JSContext* ctx) const {
11-
return JS_AtomToValue(ctx, atom_);
12-
}
13-
1410

1511
} // namespace kraken

bridge/bindings/qjs/atom_string.h

+29-45
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "foundation/macros.h"
1212
#include "foundation/native_string.h"
1313
#include "native_string_utils.h"
14+
#include "qjs_engine_patch.h"
1415

1516
namespace kraken {
1617

@@ -20,77 +21,60 @@ namespace kraken {
2021
// two String instances because we just check string storage identity.
2122
class AtomicString {
2223
public:
24+
static AtomicString Empty(JSContext* ctx) { return AtomicString(ctx, JS_ATOM_NULL); };
25+
static AtomicString From(JSContext* ctx, NativeString* native_string) {
26+
JSValue str = JS_NewUnicodeString(ctx, native_string->string, native_string->length);
27+
auto result = AtomicString(ctx, str);
28+
JS_FreeValue(ctx, str);
29+
return result;
30+
};
31+
2332
AtomicString() = default;
24-
AtomicString(JSAtom atom) : atom_(atom) {}
33+
AtomicString(JSContext *ctx, JSAtom atom) : ctx_(ctx), atom_(atom) {};
34+
AtomicString(JSContext* ctx, const std::string& string) : ctx_(ctx), atom_(JS_NewAtom(ctx, string.c_str())) {};
35+
AtomicString(JSContext* ctx, JSValue value) : ctx_(ctx), atom_(JS_ValueToAtom(ctx, value)) {};
36+
AtomicString(JSAtom atom): atom_(atom), is_static_atom_(true) {};
2537

2638
// Return the undefined string value from atom key.
27-
virtual JSValue ToQuickJS(JSContext* ctx) const = 0;
28-
29-
bool operator==(const AtomicString& other) const { return other.atom_ == this->atom_; }
30-
31-
protected:
32-
JSAtom atom_{JS_ATOM_NULL};
33-
};
34-
35-
// AtomicString which holding quickjs built-in atoms string.
36-
// These string are stored in JSRuntime instead of JSContext.
37-
// So it can be used by any JSContext and don't needs to be freed.
38-
class PersistentAtomicString : public AtomicString {
39-
public:
40-
PersistentAtomicString(JSAtom atom): AtomicString(atom) {};
41-
42-
JSValue ToQuickJS(JSContext* ctx) const override;
43-
};
44-
45-
// PeriodicAtomicString holding string atom key created by JSContext.
46-
// Could be freed when string refer_count set to 0.
47-
class PeriodicAtomicString : public AtomicString {
48-
// Should only allocate on stack.
49-
KRAKEN_DISALLOW_NEW();
50-
51-
public:
52-
static PeriodicAtomicString Empty(JSContext* ctx) { return PeriodicAtomicString(ctx); };
53-
54-
explicit PeriodicAtomicString(JSContext* ctx) : ctx_(ctx), AtomicString(JS_ATOM_NULL) {}
55-
explicit PeriodicAtomicString(JSContext* ctx, const std::string& string) : ctx_(ctx), AtomicString(JS_NewAtom(ctx, string.c_str())) {}
56-
explicit PeriodicAtomicString(JSContext* ctx, JSAtom atom) : ctx_(ctx), AtomicString(JS_DupAtom(ctx, atom)) {};
57-
explicit PeriodicAtomicString(JSContext* ctx, JSValue value) : ctx_(ctx), AtomicString(JS_ValueToAtom(ctx, value)){};
58-
~PeriodicAtomicString() { JS_FreeAtom(ctx_, atom_); }
59-
60-
JSValue ToQuickJS(JSContext* ctx) const { return JS_AtomToValue(ctx, atom_); }
39+
JSValue ToQuickJS(JSContext* ctx) const {
40+
return JS_AtomToValue(ctx, atom_);
41+
};
6142

6243
// Copy assignment
63-
PeriodicAtomicString(PeriodicAtomicString const& value) {
64-
if (&value != this) {
44+
AtomicString(AtomicString const& value) {
45+
if (!is_static_atom_ && &value != this) {
6546
atom_ = JS_DupAtom(ctx_, value.atom_);
6647
}
6748
ctx_ = value.ctx_;
6849
};
69-
PeriodicAtomicString& operator=(const PeriodicAtomicString& other) {
70-
if (&other != this) {
50+
AtomicString& operator=(const AtomicString& other) {
51+
if (!is_static_atom_ && &other != this) {
7152
atom_ = JS_DupAtom(ctx_, other.atom_);
7253
}
7354
return *this;
7455
};
7556

7657
// Move assignment
77-
PeriodicAtomicString(PeriodicAtomicString&& value) noexcept {
78-
if (&value != this) {
58+
AtomicString(AtomicString&& value) noexcept {
59+
if (!is_static_atom_ && &value != this) {
7960
atom_ = JS_DupAtom(ctx_, value.atom_);
8061
}
8162
ctx_ = value.ctx_;
8263
};
83-
PeriodicAtomicString& operator=(PeriodicAtomicString&& value) noexcept {
84-
if (&value != this) {
64+
AtomicString& operator=(AtomicString&& value) noexcept {
65+
if (!is_static_atom_ && &value != this) {
8566
atom_ = JS_DupAtom(ctx_, value.atom_);
8667
}
8768
ctx_ = value.ctx_;
8869
return *this;
8970
}
9071

91-
private:
92-
PeriodicAtomicString() = delete;
72+
bool operator==(const AtomicString& other) const { return other.atom_ == this->atom_; }
73+
74+
protected:
75+
bool is_static_atom_ = false;
9376
JSContext* ctx_{nullptr};
77+
JSAtom atom_{JS_ATOM_NULL};
9478
};
9579

9680
} // namespace kraken

bridge/bindings/qjs/converter_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ struct Converter<IDLDOMString> : public ConverterBase<IDLDOMString> {
178178
return AtomicString(ctx, value);
179179
}
180180

181-
static JSValue ToValue(JSContext* ctx, const AtomicString& value) { return value.ToQuickJS(); }
181+
static JSValue ToValue(JSContext* ctx, const AtomicString& value) { return value.ToQuickJS(ctx); }
182182
static JSValue ToValue(JSContext* ctx, NativeString* str) { return JS_NewUnicodeString(ctx, str->string, str->length); }
183183
static JSValue ToValue(JSContext* ctx, std::unique_ptr<NativeString> str) { return JS_NewUnicodeString(ctx, str->string, str->length); }
184184
static JSValue ToValue(JSContext* ctx, uint16_t* bytes, size_t length) { return JS_NewUnicodeString(ctx, bytes, length); }
@@ -341,7 +341,7 @@ struct Converter<Event> : public ConverterBase<Event> {
341341
return toScriptWrappable<Event>(value);
342342
}
343343

344-
static JSValue ToValue(JSContext* ctx, ImplType value) { return value->ToQuickJS(); }
344+
static JSValue ToValue(JSContext* ctx, ImplType value) { return reinterpret_cast<ScriptWrappable*>(value)->ToQuickJS(); }
345345
};
346346

347347
} // namespace kraken

bridge/bindings/qjs/idl_type.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class NativeString;
5050
struct IDLDOMString final : public IDLTypeBaseHelper<AtomicString> {};
5151

5252
// https://developer.mozilla.org/en-US/docs/Web/API/USVString
53-
struct IDLUSVString final : public IDLTypeBaseHelper<std::string> {};
53+
struct IDLUSVString final : public IDLTypeBaseHelper<AtomicString> {};
5454

5555
// Object
5656
struct IDLObject : public IDLTypeBaseHelper<ScriptValue> {};

bridge/bindings/qjs/js_based_event_listener.h

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ class JSBasedEventListener : public EventListener {
2121
// Implements step 2. of "inner invoke".
2222
// See: https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
2323
void Invoke(ExecutingContext* context, Event* event) final;
24+
25+
// Implements "get the current value of the event handler".
26+
// https://html.spec.whatwg.org/C/#getting-the-current-value-of-the-event-handler
27+
// Returns v8::Null with firing error event instead of throwing an exception
28+
// on failing to compile the uncompiled script body in eventHandler's value.
29+
// Also, this can return empty because of crbug.com/881688 .
30+
virtual JSValue GetListenerObject(EventTarget&) = 0;
31+
2432
// Returns Functions that handles invoked event or undefined without
2533
// throwing any exception.
2634
virtual JSValue GetEffectiveFunction(EventTarget&) = 0;

0 commit comments

Comments
 (0)