@@ -10,22 +10,20 @@ namespace kraken::binding::qjs {
10
10
11
11
std::once_flag kBlobInitOnceFlag ;
12
12
13
- void bindBlob (std::unique_ptr<JSContext> & context) {
14
- auto * constructor = Blob::instance (context.get ());
13
+ void bindBlob (std::unique_ptr<JSContext>& context) {
14
+ auto * constructor = Blob::instance (context.get ());
15
15
context->defineGlobalProperty (" Blob" , constructor->classObject );
16
16
}
17
17
18
- Blob::Blob (JSContext *context) : HostClass(context, " Blob" ) {
19
- std::call_once (kBlobInitOnceFlag , []() {
20
- JS_NewClassID (&kBlobClassID );
21
- });
18
+ Blob::Blob (JSContext* context) : HostClass(context, " Blob" ) {
19
+ std::call_once (kBlobInitOnceFlag , []() { JS_NewClassID (&kBlobClassID ); });
22
20
}
23
21
24
22
JSClassID Blob::kBlobClassID {0 };
25
23
26
- JSValue Blob::instanceConstructor (QjsContext * ctx, JSValue func_obj, JSValue this_val, int argc, JSValue * argv) {
24
+ JSValue Blob::instanceConstructor (QjsContext* ctx, JSValue func_obj, JSValue this_val, int argc, JSValue* argv) {
27
25
BlobBuilder builder;
28
- auto constructor = static_cast <Blob *>(JS_GetOpaque (func_obj, JSContext::kHostClassClassId ));
26
+ auto constructor = static_cast <Blob*>(JS_GetOpaque (func_obj, JSContext::kHostClassClassId ));
29
27
if (argc == 0 ) {
30
28
auto blob = new BlobInstance (constructor);
31
29
return blob->instanceObject ;
@@ -49,8 +47,9 @@ JSValue Blob::instanceConstructor(QjsContext *ctx, JSValue func_obj, JSValue thi
49
47
}
50
48
51
49
if (!JS_IsObject (optionValue)) {
52
- return JS_ThrowTypeError (ctx, " Failed to construct 'Blob': parameter 2 ('options') "
53
- " is not an object" );
50
+ return JS_ThrowTypeError (ctx,
51
+ " Failed to construct 'Blob': parameter 2 ('options') "
52
+ " is not an object" );
54
53
}
55
54
56
55
JSAtom mimeTypeKey = JS_NewAtom (ctx, " type" );
@@ -60,7 +59,7 @@ JSValue Blob::instanceConstructor(QjsContext *ctx, JSValue func_obj, JSValue thi
60
59
const char * cMineType = JS_ToCString (ctx, mimeTypeValue);
61
60
std::string mimeType = std::string (cMineType);
62
61
63
- auto * blob = new BlobInstance (constructor, builder.finalize (), mimeType);
62
+ auto * blob = new BlobInstance (constructor, builder.finalize (), mimeType);
64
63
65
64
JS_FreeValue (ctx, mimeTypeValue);
66
65
JS_FreeCString (ctx, mimeType.c_str ());
@@ -69,39 +68,40 @@ JSValue Blob::instanceConstructor(QjsContext *ctx, JSValue func_obj, JSValue thi
69
68
return blob->instanceObject ;
70
69
}
71
70
72
- PROP_GETTER (BlobInstance, type)(QjsContext * ctx, JSValue this_val, int argc, JSValue * argv) {
73
- auto * blobInstance = static_cast <BlobInstance *>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
71
+ PROP_GETTER (BlobInstance, type)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
72
+ auto * blobInstance = static_cast <BlobInstance*>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
74
73
return JS_NewString (blobInstance->m_ctx , blobInstance->mimeType .empty () ? " " : blobInstance->mimeType .c_str ());
75
74
}
76
- PROP_SETTER (BlobInstance, type)(QjsContext * ctx, JSValue this_val, int argc, JSValue * argv) {
75
+ PROP_SETTER (BlobInstance, type)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
77
76
return JS_NULL;
78
77
}
79
78
80
- PROP_GETTER (BlobInstance, size)(QjsContext * ctx, JSValue this_val, int argc, JSValue * argv) {
81
- auto * blobInstance = static_cast <BlobInstance *>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
79
+ PROP_GETTER (BlobInstance, size)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
80
+ auto * blobInstance = static_cast <BlobInstance*>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
82
81
return JS_NewFloat64 (blobInstance->m_ctx , blobInstance->_size );
83
82
}
84
- PROP_SETTER (BlobInstance, size)(QjsContext * ctx, JSValue this_val, int argc, JSValue * argv) {
83
+ PROP_SETTER (BlobInstance, size)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
85
84
return JS_NULL;
86
85
}
87
86
88
- JSValue Blob::arrayBuffer (QjsContext * ctx, JSValue this_val, int argc, JSValue * argv) {
87
+ JSValue Blob::arrayBuffer (QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
89
88
JSValue resolving_funcs[2 ];
90
89
JSValue promise = JS_NewPromiseCapability (ctx, resolving_funcs);
91
90
92
- auto blob = static_cast <BlobInstance *>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
91
+ auto blob = static_cast <BlobInstance*>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
93
92
94
93
JS_DupValue (ctx, blob->instanceObject );
95
94
96
- auto *promiseContext = new PromiseContext{blob, blob->m_context , resolving_funcs[0 ], resolving_funcs[1 ], promise};
97
- auto callback = [](void *callbackContext, int32_t contextId, const char *errmsg) {
98
- if (!isContextValid (contextId)) return ;
99
- auto *promiseContext = static_cast <PromiseContext *>(callbackContext);
100
- auto *blob = static_cast <BlobInstance *>(promiseContext->data );
101
- QjsContext *ctx = blob->m_ctx ;
95
+ auto * promiseContext = new PromiseContext{blob, blob->m_context , resolving_funcs[0 ], resolving_funcs[1 ], promise};
96
+ auto callback = [](void * callbackContext, int32_t contextId, const char * errmsg) {
97
+ if (!isContextValid (contextId))
98
+ return ;
99
+ auto * promiseContext = static_cast <PromiseContext*>(callbackContext);
100
+ auto * blob = static_cast <BlobInstance*>(promiseContext->data );
101
+ QjsContext* ctx = blob->m_ctx ;
102
102
103
103
JSValue arrayBuffer = JS_NewArrayBuffer (
104
- ctx, blob->bytes (), blob->size (), [](JSRuntime * rt, void * opaque, void * ptr) {}, nullptr , false );
104
+ ctx, blob->bytes (), blob->size (), [](JSRuntime* rt, void * opaque, void * ptr) {}, nullptr , false );
105
105
JSValue arguments[] = {arrayBuffer};
106
106
JSValue returnValue = JS_Call (ctx, promiseContext->resolveFunc , blob->context ()->global (), 1 , arguments);
107
107
JS_FreeValue (ctx, returnValue);
@@ -128,12 +128,12 @@ JSValue Blob::arrayBuffer(QjsContext *ctx, JSValue this_val, int argc, JSValue *
128
128
return promise;
129
129
}
130
130
131
- JSValue Blob::slice (QjsContext * ctx, JSValue this_val, int argc, JSValue * argv) {
131
+ JSValue Blob::slice (QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
132
132
JSValue startValue = argv[0 ];
133
133
JSValue endValue = argv[1 ];
134
134
JSValue contentTypeValue = argv[2 ];
135
135
136
- auto * blob = static_cast <BlobInstance *>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
136
+ auto * blob = static_cast <BlobInstance*>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
137
137
int32_t start = 0 ;
138
138
int32_t end = blob->_data .size ();
139
139
std::string mimeType = blob->mimeType ;
@@ -153,35 +153,36 @@ JSValue Blob::slice(QjsContext *ctx, JSValue this_val, int argc, JSValue *argv)
153
153
}
154
154
155
155
if (start == 0 && end == blob->_data .size ()) {
156
- auto newBlob = new BlobInstance (reinterpret_cast <Blob *>(blob->m_hostClass ), std::move (blob->_data ), mimeType);
156
+ auto newBlob = new BlobInstance (reinterpret_cast <Blob*>(blob->m_hostClass ), std::move (blob->_data ), mimeType);
157
157
JS_SetPrototype (blob->m_ctx , newBlob->instanceObject , blob->m_hostClass ->prototype ());
158
158
return newBlob->instanceObject ;
159
159
}
160
160
std::vector<uint8_t > newData;
161
161
newData.reserve (blob->_data .size () - (end - start));
162
162
newData.insert (newData.begin (), blob->_data .begin () + start, blob->_data .end () - (blob->_data .size () - end));
163
163
164
- auto newBlob = new BlobInstance (reinterpret_cast <Blob *>(blob->m_hostClass ), std::move (newData), mimeType);
164
+ auto newBlob = new BlobInstance (reinterpret_cast <Blob*>(blob->m_hostClass ), std::move (newData), mimeType);
165
165
JS_SetPrototype (blob->m_ctx , newBlob->instanceObject , blob->m_hostClass ->prototype ());
166
166
return newBlob->instanceObject ;
167
167
}
168
168
169
- JSValue Blob::text (QjsContext * ctx, JSValue this_val, int argc, JSValue * argv) {
169
+ JSValue Blob::text (QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
170
170
JSValue resolving_funcs[2 ];
171
171
JSValue promise = JS_NewPromiseCapability (ctx, resolving_funcs);
172
172
173
- auto blob = static_cast <BlobInstance *>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
173
+ auto blob = static_cast <BlobInstance*>(JS_GetOpaque (this_val, Blob::kBlobClassID ));
174
174
JS_DupValue (ctx, blob->instanceObject );
175
175
176
- auto *promiseContext = new PromiseContext{blob, blob->m_context , resolving_funcs[0 ], resolving_funcs[1 ], promise};
177
- auto callback = [](void *callbackContext, int32_t contextId, const char *errmsg) {
178
- if (!isContextValid (contextId)) return ;
176
+ auto * promiseContext = new PromiseContext{blob, blob->m_context , resolving_funcs[0 ], resolving_funcs[1 ], promise};
177
+ auto callback = [](void * callbackContext, int32_t contextId, const char * errmsg) {
178
+ if (!isContextValid (contextId))
179
+ return ;
179
180
180
- auto * promiseContext = static_cast <PromiseContext *>(callbackContext);
181
- auto * blob = static_cast <BlobInstance *>(promiseContext->data );
182
- QjsContext * ctx = blob->m_ctx ;
181
+ auto * promiseContext = static_cast <PromiseContext*>(callbackContext);
182
+ auto * blob = static_cast <BlobInstance*>(promiseContext->data );
183
+ QjsContext* ctx = blob->m_ctx ;
183
184
184
- JSValue text = JS_NewStringLen (ctx, reinterpret_cast <const char *>(blob->bytes ()), blob->size ());
185
+ JSValue text = JS_NewStringLen (ctx, reinterpret_cast <const char *>(blob->bytes ()), blob->size ());
185
186
JSValue arguments[] = {text};
186
187
JSValue returnValue = JS_Call (ctx, promiseContext->resolveFunc , blob->context ()->global (), 1 , arguments);
187
188
JS_FreeValue (ctx, returnValue);
@@ -207,21 +208,20 @@ JSValue Blob::text(QjsContext *ctx, JSValue this_val, int argc, JSValue *argv) {
207
208
return promise;
208
209
}
209
210
210
- void BlobInstance::finalize (JSRuntime * rt, JSValue val) {
211
- auto * eventTarget = static_cast <BlobInstance *>(JS_GetOpaque (val, Blob::kBlobClassID ));
211
+ void BlobInstance::finalize (JSRuntime* rt, JSValue val) {
212
+ auto * eventTarget = static_cast <BlobInstance*>(JS_GetOpaque (val, Blob::kBlobClassID ));
212
213
delete eventTarget;
213
214
}
214
215
215
-
216
- void BlobBuilder::append (JSContext &context, BlobInstance *blob) {
216
+ void BlobBuilder::append (JSContext& context, BlobInstance* blob) {
217
217
std::vector<uint8_t > blobData = blob->_data ;
218
218
_data.reserve (_data.size () + blobData.size ());
219
219
_data.insert (_data.end (), blobData.begin (), blobData.end ());
220
220
}
221
221
222
- void BlobBuilder::append (JSContext & context, JSValue & value) {
222
+ void BlobBuilder::append (JSContext& context, JSValue& value) {
223
223
if (JS_IsString (value)) {
224
- const char * buffer = JS_ToCString (context.ctx (), value);
224
+ const char * buffer = JS_ToCString (context.ctx (), value);
225
225
std::string str = std::string (buffer);
226
226
std::vector<uint8_t > strArr (str.begin (), str.end ());
227
227
_data.reserve (_data.size () + strArr.size ());
@@ -243,16 +243,17 @@ void BlobBuilder::append(JSContext &context, JSValue &value) {
243
243
}
244
244
} else if (JS_IsObject (value)) {
245
245
if (JS_IsInstanceOf (context.ctx (), value, Blob::instance (&context)->classObject )) {
246
- auto blob = static_cast <BlobInstance *>(JS_GetOpaque (value, Blob::kBlobClassID ));
247
- if (blob == nullptr ) return ;
246
+ auto blob = static_cast <BlobInstance*>(JS_GetOpaque (value, Blob::kBlobClassID ));
247
+ if (blob == nullptr )
248
+ return ;
248
249
if (std::string (blob->m_name ) == " Blob" ) {
249
250
std::vector<uint8_t > blobData = blob->_data ;
250
251
_data.reserve (_data.size () + blobData.size ());
251
252
_data.insert (_data.end (), blobData.begin (), blobData.end ());
252
253
}
253
254
} else {
254
255
size_t length;
255
- uint8_t * buffer = JS_GetArrayBuffer (context.ctx (), &length, value);
256
+ uint8_t * buffer = JS_GetArrayBuffer (context.ctx (), &length, value);
256
257
257
258
if (buffer == nullptr ) {
258
259
size_t byte_offset;
@@ -282,7 +283,7 @@ int32_t BlobInstance::size() {
282
283
return _data.size ();
283
284
}
284
285
285
- uint8_t * BlobInstance::bytes () {
286
+ uint8_t * BlobInstance::bytes () {
286
287
return _data.data ();
287
288
}
288
- } // namespace kraken::binding::qjs
289
+ } // namespace kraken::binding::qjs
0 commit comments