11
11
#include " foundation/macros.h"
12
12
#include " foundation/native_string.h"
13
13
#include " native_string_utils.h"
14
+ #include " qjs_engine_patch.h"
14
15
15
16
namespace kraken {
16
17
@@ -20,77 +21,60 @@ namespace kraken {
20
21
// two String instances because we just check string storage identity.
21
22
class AtomicString {
22
23
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
+
23
32
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 ) {};
25
37
26
38
// 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
+ };
61
42
62
43
// Copy assignment
63
- PeriodicAtomicString (PeriodicAtomicString const & value) {
64
- if (&value != this ) {
44
+ AtomicString (AtomicString const & value) {
45
+ if (!is_static_atom_ && &value != this ) {
65
46
atom_ = JS_DupAtom (ctx_, value.atom_ );
66
47
}
67
48
ctx_ = value.ctx_ ;
68
49
};
69
- PeriodicAtomicString & operator =(const PeriodicAtomicString & other) {
70
- if (&other != this ) {
50
+ AtomicString & operator =(const AtomicString & other) {
51
+ if (!is_static_atom_ && &other != this ) {
71
52
atom_ = JS_DupAtom (ctx_, other.atom_ );
72
53
}
73
54
return *this ;
74
55
};
75
56
76
57
// Move assignment
77
- PeriodicAtomicString (PeriodicAtomicString && value) noexcept {
78
- if (&value != this ) {
58
+ AtomicString (AtomicString && value) noexcept {
59
+ if (!is_static_atom_ && &value != this ) {
79
60
atom_ = JS_DupAtom (ctx_, value.atom_ );
80
61
}
81
62
ctx_ = value.ctx_ ;
82
63
};
83
- PeriodicAtomicString & operator =(PeriodicAtomicString && value) noexcept {
84
- if (&value != this ) {
64
+ AtomicString & operator =(AtomicString && value) noexcept {
65
+ if (!is_static_atom_ && &value != this ) {
85
66
atom_ = JS_DupAtom (ctx_, value.atom_ );
86
67
}
87
68
ctx_ = value.ctx_ ;
88
69
return *this ;
89
70
}
90
71
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 ;
93
76
JSContext* ctx_{nullptr };
77
+ JSAtom atom_{JS_ATOM_NULL};
94
78
};
95
79
96
80
} // namespace kraken
0 commit comments