6
6
#ifndef KRAKENBRIDGE_JS_CONTEXT_H
7
7
#define KRAKENBRIDGE_JS_CONTEXT_H
8
8
9
+ #include " js_context_macros.h"
9
10
#include " kraken_foundation.h"
11
+ #include " qjs_patch.h"
10
12
#include < memory>
11
- #include < unordered_map>
12
- #include < quickjs/quickjs.h>
13
13
#include < quickjs/list.h>
14
- #include " js_context_macros.h "
15
- #include " qjs_patch.h "
14
+ #include < quickjs/quickjs.h >
15
+ #include < unordered_map >
16
16
17
17
using QjsContext = JSContext;
18
18
using JSExceptionHandler = std::function<void (int32_t contextId, const char *message)>;
@@ -67,10 +67,10 @@ class JSContext {
67
67
bool handleException (JSValue *exc);
68
68
void drainPendingPromiseJobs ();
69
69
void defineGlobalProperty (const char *prop, JSValueConst value);
70
- uint8_t *dumpByteCode (const char * code, uint32_t codeLength, const char *sourceURL, size_t * bytecodeLength);
70
+ uint8_t *dumpByteCode (const char * code, uint32_t codeLength, const char *sourceURL, size_t * bytecodeLength);
71
71
72
72
std::chrono::time_point<std::chrono::system_clock> timeOrigin;
73
- std::unordered_map<std::string, void *> constructorMap;
73
+ std::unordered_map<std::string, void *> constructorMap;
74
74
75
75
int32_t uniqueId;
76
76
struct list_head node_job_list;
@@ -87,7 +87,6 @@ class JSContext {
87
87
static JSClassID kHostExoticObjectClassId ;
88
88
89
89
private:
90
-
91
90
static void promiseRejectTracker (QjsContext *ctx, JSValueConst promise, JSValueConst reason, JS_BOOL is_handled,
92
91
void *opaque);
93
92
void dispatchGlobalErrorEvent (JSValueConst error);
@@ -105,9 +104,10 @@ class JSContext {
105
104
WindowInstance *m_window{nullptr };
106
105
};
107
106
108
- // The read object's method or properties via Proxy, we should redirect this_val from Proxy into target property of proxy object.
107
+ // The read object's method or properties via Proxy, we should redirect this_val from Proxy into target property of
108
+ // proxy object.
109
109
static JSValue handleCallThisOnProxy (QjsContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int data_len,
110
- JSValueConst *data) {
110
+ JSValueConst *data) {
111
111
JSValue f = data[0 ];
112
112
JSValue result;
113
113
if (JS_IsProxy (this_val)) {
@@ -123,8 +123,8 @@ class ObjectProperty {
123
123
124
124
public:
125
125
ObjectProperty () = delete ;
126
- explicit ObjectProperty (JSContext *context, JSValueConst thisObject, const char *property,
127
- JSCFunction getterFunction, JSCFunction setterFunction) {
126
+ explicit ObjectProperty (JSContext *context, JSValueConst thisObject, const char *property, JSCFunction getterFunction,
127
+ JSCFunction setterFunction) {
128
128
JSValue ge = JS_NewCFunction (context->ctx (), getterFunction, " get" , 0 );
129
129
JSValue se = JS_NewCFunction (context->ctx (), setterFunction, " set" , 1 );
130
130
@@ -135,16 +135,15 @@ class ObjectProperty {
135
135
JS_FreeValue (context->ctx (), se);
136
136
137
137
JSAtom key = JS_NewAtom (context->ctx (), property);
138
- JS_DefinePropertyGetSet (context->ctx (), thisObject, key, pge, pse,
139
- JS_PROP_C_W_E);
138
+ JS_DefinePropertyGetSet (context->ctx (), thisObject, key, pge, pse, JS_PROP_C_W_E);
140
139
JS_FreeAtom (context->ctx (), key);
141
140
};
142
141
explicit ObjectProperty (JSContext *context, JSValueConst thisObject, const char *property,
143
142
JSCFunction getterFunction) {
144
143
JSValue get = JS_NewCFunction (context->ctx (), getterFunction, " get" , 0 );
145
144
JSAtom key = JS_NewAtom (context->ctx (), property);
146
145
JS_DefineProperty (context->ctx (), thisObject, key, JS_UNDEFINED, get, JS_UNDEFINED,
147
- JS_PROP_HAS_CONFIGURABLE | JS_PROP_ENUMERABLE | JS_PROP_HAS_GET);
146
+ JS_PROP_HAS_CONFIGURABLE | JS_PROP_ENUMERABLE | JS_PROP_HAS_GET);
148
147
JS_FreeAtom (context->ctx (), key);
149
148
}
150
149
};
@@ -154,8 +153,8 @@ class ObjectFunction {
154
153
155
154
public:
156
155
ObjectFunction () = delete ;
157
- explicit ObjectFunction (JSContext *context, JSValueConst thisObject, const char *functionName,
158
- JSCFunction function, int argc) {
156
+ explicit ObjectFunction (JSContext *context, JSValueConst thisObject, const char *functionName, JSCFunction function,
157
+ int argc) {
159
158
JSValue f = JS_NewCFunction (context->ctx (), function, functionName, argc);
160
159
JSValue pf = JS_NewCFunctionData (context->ctx (), handleCallThisOnProxy, argc, 0 , 1 , &f);
161
160
JSAtom key = JS_NewAtom (context->ctx (), functionName);
@@ -164,19 +163,19 @@ class ObjectFunction {
164
163
165
164
// We should avoid overwrite exist property functions.
166
165
#ifdef DEBUG
167
- assert_m (JS_HasProperty (context->ctx (), thisObject, key) == 0 , (std::string (" Found exist function property: " ) + std::string (functionName)).c_str ());
166
+ assert_m (JS_HasProperty (context->ctx (), thisObject, key) == 0 ,
167
+ (std::string (" Found exist function property: " ) + std::string (functionName)).c_str ());
168
168
#endif
169
169
170
- JS_DefinePropertyValue (context->ctx (), thisObject, key, pf,
171
- JS_PROP_ENUMERABLE);
170
+ JS_DefinePropertyValue (context->ctx (), thisObject, key, pf, JS_PROP_ENUMERABLE);
172
171
JS_FreeAtom (context->ctx (), key);
173
172
};
174
173
};
175
174
176
175
class JSValueHolder {
177
176
public:
178
177
JSValueHolder () = delete ;
179
- explicit JSValueHolder (QjsContext *ctx, JSValue value): m_value(value), m_ctx(ctx) {};
178
+ explicit JSValueHolder (QjsContext *ctx, JSValue value) : m_value(value), m_ctx(ctx){};
180
179
~JSValueHolder () {
181
180
JS_FreeValue (m_ctx, m_value);
182
181
}
@@ -186,7 +185,10 @@ class JSValueHolder {
186
185
}
187
186
m_value = JS_DupValue (m_ctx, value);
188
187
};
189
- inline JSValue value () const { return JS_DupValue (m_ctx, m_value); }
188
+ inline JSValue value () const {
189
+ return JS_DupValue (m_ctx, m_value);
190
+ }
191
+
190
192
private:
191
193
QjsContext *m_ctx{nullptr };
192
194
JSValue m_value{JS_NULL};
@@ -208,7 +210,6 @@ void arraySpliceValue(QjsContext *ctx, JSValue array, uint32_t start, uint32_t d
208
210
void arraySpliceValue (QjsContext *ctx, JSValue array, uint32_t start, uint32_t deleteCount, JSValue replacedValue);
209
211
JSValue objectGetKeys (QjsContext *ctx, JSValue obj);
210
212
211
-
212
213
} // namespace kraken::binding::qjs
213
214
214
215
#endif // KRAKENBRIDGE_JS_CONTEXT_H
0 commit comments