@nativescript/canvas 2.0.0-webgpu.3 → 2.0.0-webgpu.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/Canvas/common.d.ts +4 -9
  2. package/Canvas/common.js +37 -126
  3. package/Canvas/common.js.map +1 -1
  4. package/Canvas/index.android.d.ts +0 -1
  5. package/Canvas/index.android.js +45 -38
  6. package/Canvas/index.android.js.map +1 -1
  7. package/Canvas/index.d.ts +11 -3
  8. package/Canvas/index.ios.js +49 -40
  9. package/Canvas/index.ios.js.map +1 -1
  10. package/Canvas/utils.d.ts +3 -0
  11. package/Canvas/utils.js +85 -0
  12. package/Canvas/utils.js.map +1 -0
  13. package/Canvas2D/CanvasRenderingContext2D/index.d.ts +1 -0
  14. package/Canvas2D/CanvasRenderingContext2D/index.js +1 -0
  15. package/Canvas2D/CanvasRenderingContext2D/index.js.map +1 -1
  16. package/Dom/Group.d.ts +1 -2
  17. package/Dom/Group.js.map +1 -1
  18. package/Dom/Image.js +0 -1
  19. package/Dom/Image.js.map +1 -1
  20. package/Dom/Paint.d.ts +1 -1
  21. package/Dom/shapes/Path.d.ts +1 -1
  22. package/ImageAsset/index.js +77 -33
  23. package/ImageAsset/index.js.map +1 -1
  24. package/ImageBitmap/index.js +107 -20
  25. package/ImageBitmap/index.js.map +1 -1
  26. package/README.md +2 -4
  27. package/WebGL/WebGLRenderingContext/index.js +73 -31
  28. package/WebGL/WebGLRenderingContext/index.js.map +1 -1
  29. package/WebGL2/WebGL2RenderingContext/index.js +4 -4
  30. package/WebGL2/WebGL2RenderingContext/index.js.map +1 -1
  31. package/WebGPU/GPU.js +4 -3
  32. package/WebGPU/GPU.js.map +1 -1
  33. package/WebGPU/GPUAdapter.d.ts +6 -3
  34. package/WebGPU/GPUAdapter.js +31 -0
  35. package/WebGPU/GPUAdapter.js.map +1 -1
  36. package/WebGPU/GPUBindGroup.d.ts +1 -0
  37. package/WebGPU/GPUBindGroup.js +3 -0
  38. package/WebGPU/GPUBindGroup.js.map +1 -1
  39. package/WebGPU/GPUBindGroupLayout.d.ts +1 -0
  40. package/WebGPU/GPUBindGroupLayout.js +3 -0
  41. package/WebGPU/GPUBindGroupLayout.js.map +1 -1
  42. package/WebGPU/GPUBuffer.js +10 -2
  43. package/WebGPU/GPUBuffer.js.map +1 -1
  44. package/WebGPU/GPUCanvasContext.d.ts +3 -2
  45. package/WebGPU/GPUCanvasContext.js +52 -10
  46. package/WebGPU/GPUCanvasContext.js.map +1 -1
  47. package/WebGPU/GPUCommandBuffer.d.ts +1 -0
  48. package/WebGPU/GPUCommandBuffer.js +3 -0
  49. package/WebGPU/GPUCommandBuffer.js.map +1 -1
  50. package/WebGPU/GPUCommandEncoder.d.ts +3 -9
  51. package/WebGPU/GPUCommandEncoder.js +94 -41
  52. package/WebGPU/GPUCommandEncoder.js.map +1 -1
  53. package/WebGPU/GPUComputePassEncoder.d.ts +1 -0
  54. package/WebGPU/GPUComputePassEncoder.js +3 -0
  55. package/WebGPU/GPUComputePassEncoder.js.map +1 -1
  56. package/WebGPU/GPUComputePipeline.d.ts +1 -0
  57. package/WebGPU/GPUComputePipeline.js +3 -0
  58. package/WebGPU/GPUComputePipeline.js.map +1 -1
  59. package/WebGPU/GPUDevice.d.ts +17 -79
  60. package/WebGPU/GPUDevice.js +76 -91
  61. package/WebGPU/GPUDevice.js.map +1 -1
  62. package/WebGPU/GPUExternalTexture.d.ts +1 -0
  63. package/WebGPU/GPUExternalTexture.js +3 -0
  64. package/WebGPU/GPUExternalTexture.js.map +1 -1
  65. package/WebGPU/GPUPipelineLayout.d.ts +1 -0
  66. package/WebGPU/GPUPipelineLayout.js +3 -0
  67. package/WebGPU/GPUPipelineLayout.js.map +1 -1
  68. package/WebGPU/GPUQuerySet.d.ts +1 -0
  69. package/WebGPU/GPUQuerySet.js +3 -0
  70. package/WebGPU/GPUQuerySet.js.map +1 -1
  71. package/WebGPU/GPUQueue.d.ts +1 -0
  72. package/WebGPU/GPUQueue.js +56 -14
  73. package/WebGPU/GPUQueue.js.map +1 -1
  74. package/WebGPU/GPURenderBundle.d.ts +1 -0
  75. package/WebGPU/GPURenderBundle.js +3 -0
  76. package/WebGPU/GPURenderBundle.js.map +1 -1
  77. package/WebGPU/GPURenderBundleEncoder.d.ts +4 -2
  78. package/WebGPU/GPURenderBundleEncoder.js +7 -3
  79. package/WebGPU/GPURenderBundleEncoder.js.map +1 -1
  80. package/WebGPU/GPURenderPassEncoder.d.ts +2 -1
  81. package/WebGPU/GPURenderPassEncoder.js +5 -2
  82. package/WebGPU/GPURenderPassEncoder.js.map +1 -1
  83. package/WebGPU/GPURenderPipeline.d.ts +1 -0
  84. package/WebGPU/GPURenderPipeline.js +3 -0
  85. package/WebGPU/GPURenderPipeline.js.map +1 -1
  86. package/WebGPU/GPUSampler.d.ts +1 -0
  87. package/WebGPU/GPUSampler.js +3 -0
  88. package/WebGPU/GPUSampler.js.map +1 -1
  89. package/WebGPU/GPUShaderModule.d.ts +1 -1
  90. package/WebGPU/GPUShaderModule.js +2 -2
  91. package/WebGPU/GPUShaderModule.js.map +1 -1
  92. package/WebGPU/GPUTexture.d.ts +1 -0
  93. package/WebGPU/GPUTexture.js +3 -0
  94. package/WebGPU/GPUTexture.js.map +1 -1
  95. package/WebGPU/GPUTextureView.d.ts +1 -0
  96. package/WebGPU/GPUTextureView.js +3 -0
  97. package/WebGPU/GPUTextureView.js.map +1 -1
  98. package/WebGPU/Interfaces.d.ts +95 -4
  99. package/WebGPU/Types.d.ts +9 -1
  100. package/WebGPU/Utils.d.ts +6 -0
  101. package/WebGPU/Utils.js +224 -0
  102. package/WebGPU/Utils.js.map +1 -1
  103. package/angular/esm2022/index.mjs +4 -4
  104. package/angular/fesm2022/nativescript-canvas-angular.mjs +4 -4
  105. package/package.json +1 -1
  106. package/platforms/android/canvas-release.aar +0 -0
  107. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/CanvasNative +0 -0
  108. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/CanvasNative-Swift.h +39 -10
  109. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_ios.h +7 -0
  110. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_native.h +227 -62
  111. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Info.plist +0 -0
  112. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Modules/CanvasNative.swiftmodule/Project/arm64-apple-ios.swiftsourceinfo +0 -0
  113. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Modules/CanvasNative.swiftmodule/arm64-apple-ios.abi.json +1635 -151
  114. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Modules/CanvasNative.swiftmodule/arm64-apple-ios.private.swiftinterface +57 -1
  115. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Modules/CanvasNative.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
  116. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Modules/CanvasNative.swiftmodule/arm64-apple-ios.swiftinterface +57 -1
  117. package/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/NSCCanvas.nib +0 -0
  118. package/platforms/ios/CanvasNative.xcframework/ios-arm64/dSYMs/CanvasNative.framework.dSYM/Contents/Resources/DWARF/CanvasNative +0 -0
  119. package/platforms/ios/CanvasNative.xcframework/ios-arm64/dSYMs/CanvasNative.framework.dSYM/Contents/Resources/Relocations/aarch64/CanvasNative.yml +690 -644
  120. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/CanvasNative +0 -0
  121. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/CanvasNative-Swift.h +78 -20
  122. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_ios.h +7 -0
  123. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_native.h +227 -62
  124. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Info.plist +0 -0
  125. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/Project/arm64-apple-ios-simulator.swiftsourceinfo +0 -0
  126. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/Project/x86_64-apple-ios-simulator.swiftsourceinfo +0 -0
  127. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/arm64-apple-ios-simulator.abi.json +1635 -151
  128. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +57 -1
  129. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
  130. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/arm64-apple-ios-simulator.swiftinterface +57 -1
  131. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/x86_64-apple-ios-simulator.abi.json +1635 -151
  132. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +57 -1
  133. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
  134. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Modules/CanvasNative.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +57 -1
  135. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/NSCCanvas.nib +0 -0
  136. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/_CodeSignature/CodeResources +27 -27
  137. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/dSYMs/CanvasNative.framework.dSYM/Contents/Resources/DWARF/CanvasNative +0 -0
  138. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/dSYMs/CanvasNative.framework.dSYM/Contents/Resources/Relocations/aarch64/CanvasNative.yml +692 -646
  139. package/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/dSYMs/CanvasNative.framework.dSYM/Contents/Resources/Relocations/x86_64/CanvasNative.yml +730 -684
  140. package/platforms/ios/src/NSOperationQueueWrapper.h +1 -1
  141. package/platforms/ios/src/NSOperationQueueWrapper.mm +41 -9
  142. package/platforms/ios/src/cpp/AsyncCallback.h +16 -8
  143. package/platforms/ios/src/cpp/CanvasJSIModule.cpp +208 -789
  144. package/platforms/ios/src/cpp/ImageAssetImpl.cpp +18 -28
  145. package/platforms/ios/src/cpp/ImageAssetImpl.h +3 -3
  146. package/platforms/ios/src/cpp/ImageBitmapImpl.cpp +60 -3
  147. package/platforms/ios/src/cpp/ImageBitmapImpl.h +10 -3
  148. package/platforms/ios/src/cpp/PromiseCallback.h +1 -2
  149. package/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.cpp +1 -1
  150. package/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.h +3 -3
  151. package/platforms/ios/src/cpp/canvas2d/Path2D.h +5 -5
  152. package/platforms/ios/src/cpp/webgl/WebGLRenderingContext.cpp +106 -1
  153. package/platforms/ios/src/cpp/webgpu/GPUAdapterImpl.cpp +26 -23
  154. package/platforms/ios/src/cpp/webgpu/GPUBindGroupImpl.cpp +31 -5
  155. package/platforms/ios/src/cpp/webgpu/GPUBindGroupImpl.h +4 -1
  156. package/platforms/ios/src/cpp/webgpu/GPUBindGroupLayoutImpl.cpp +26 -0
  157. package/platforms/ios/src/cpp/webgpu/GPUBindGroupLayoutImpl.h +3 -0
  158. package/platforms/ios/src/cpp/webgpu/GPUBufferImpl.cpp +41 -7
  159. package/platforms/ios/src/cpp/webgpu/GPUBufferImpl.h +4 -0
  160. package/platforms/ios/src/cpp/webgpu/GPUCanvasContextImpl.cpp +11 -5
  161. package/platforms/ios/src/cpp/webgpu/GPUCommandBufferImpl.cpp +28 -1
  162. package/platforms/ios/src/cpp/webgpu/GPUCommandBufferImpl.h +3 -0
  163. package/platforms/ios/src/cpp/webgpu/GPUCommandEncoderImpl.cpp +75 -79
  164. package/platforms/ios/src/cpp/webgpu/GPUCommandEncoderImpl.h +4 -0
  165. package/platforms/ios/src/cpp/webgpu/GPUComputePassEncoderImpl.cpp +42 -14
  166. package/platforms/ios/src/cpp/webgpu/GPUComputePassEncoderImpl.h +3 -0
  167. package/platforms/ios/src/cpp/webgpu/GPUComputePipelineImpl.cpp +25 -0
  168. package/platforms/ios/src/cpp/webgpu/GPUComputePipelineImpl.h +2 -0
  169. package/platforms/ios/src/cpp/webgpu/GPUDeviceImpl.cpp +1597 -674
  170. package/platforms/ios/src/cpp/webgpu/GPUDeviceImpl.h +2 -0
  171. package/platforms/ios/src/cpp/webgpu/GPUImpl.cpp +2 -1
  172. package/platforms/ios/src/cpp/webgpu/GPUImpl.h +1 -1
  173. package/platforms/ios/src/cpp/webgpu/GPULabel.h +33 -0
  174. package/platforms/ios/src/cpp/webgpu/GPUPipelineLayoutImpl.cpp +26 -0
  175. package/platforms/ios/src/cpp/webgpu/GPUPipelineLayoutImpl.h +3 -1
  176. package/platforms/ios/src/cpp/webgpu/GPUQuerySetImpl.cpp +1 -2
  177. package/platforms/ios/src/cpp/webgpu/GPUQuerySetImpl.h +1 -1
  178. package/platforms/ios/src/cpp/webgpu/GPUQueueImpl.cpp +50 -13
  179. package/platforms/ios/src/cpp/webgpu/GPUQueueImpl.h +3 -0
  180. package/platforms/ios/src/cpp/webgpu/GPURenderBundleEncoderImpl.cpp +43 -14
  181. package/platforms/ios/src/cpp/webgpu/GPURenderBundleEncoderImpl.h +3 -0
  182. package/platforms/ios/src/cpp/webgpu/GPURenderBundleImpl.cpp +25 -0
  183. package/platforms/ios/src/cpp/webgpu/GPURenderBundleImpl.h +3 -0
  184. package/platforms/ios/src/cpp/webgpu/GPURenderPassEncoderImpl.cpp +39 -13
  185. package/platforms/ios/src/cpp/webgpu/GPURenderPassEncoderImpl.h +3 -0
  186. package/platforms/ios/src/cpp/webgpu/GPURenderPipelineImpl.cpp +25 -0
  187. package/platforms/ios/src/cpp/webgpu/GPURenderPipelineImpl.h +3 -0
  188. package/platforms/ios/src/cpp/webgpu/GPUSamplerImpl.cpp +26 -0
  189. package/platforms/ios/src/cpp/webgpu/GPUSamplerImpl.h +4 -1
  190. package/platforms/ios/src/cpp/webgpu/GPUShaderModuleImpl.cpp +24 -0
  191. package/platforms/ios/src/cpp/webgpu/GPUShaderModuleImpl.h +3 -0
  192. package/platforms/ios/src/cpp/webgpu/GPUTextureImpl.cpp +34 -8
  193. package/platforms/ios/src/cpp/webgpu/GPUTextureImpl.h +2 -0
  194. package/platforms/ios/src/cpp/webgpu/GPUTextureViewImpl.cpp +26 -0
  195. package/platforms/ios/src/cpp/webgpu/GPUTextureViewImpl.h +3 -0
  196. package/platforms/ios/src/cpp/webgpu/GPUUtils.h +511 -0
  197. package/utils.d.ts +1 -1
@@ -145,6 +145,7 @@ void CanvasJSIModule::install(v8::Isolate *isolate) {
145
145
  Path2D::Init(canvasMod, isolate);
146
146
  ImageDataImpl::Init(canvasMod, isolate);
147
147
  ImageAssetImpl::Init(canvasMod, isolate);
148
+ ImageBitmapImpl::Init(canvasMod, isolate);
148
149
  CanvasGradient::Init(canvasMod, isolate);
149
150
  CanvasPattern::Init(canvasMod, isolate);
150
151
  MatrixImpl::Init(canvasMod, isolate);
@@ -251,6 +252,32 @@ void CanvasJSIModule::Create2DContext(const v8::FunctionCallbackInfo<v8::Value>
251
252
  args.GetReturnValue().Set(ret);
252
253
  }
253
254
 
255
+
256
+ struct Rect {
257
+ float x;
258
+ float y;
259
+ float width;
260
+ float height;
261
+ };
262
+
263
+ struct ImageBitmapData {
264
+ char *error_;
265
+ const ImageAsset *asset_;
266
+ bool done_;
267
+ v8::Isolate *isolate_;
268
+ std::shared_ptr<v8::Persistent<v8::ArrayBufferView>> data_;
269
+
270
+ ~ImageBitmapData() {
271
+ if (error_ != nullptr) {
272
+ canvas_native_string_destroy(error_);
273
+ error_ = nullptr;
274
+ }
275
+
276
+ data_->Reset();
277
+ }
278
+ };
279
+
280
+
254
281
  void CanvasJSIModule::CreateImageBitmap(const v8::FunctionCallbackInfo<v8::Value> &args) {
255
282
  auto count = args.Length();
256
283
  auto isolate = args.GetIsolate();
@@ -285,7 +312,7 @@ void CanvasJSIModule::CreateImageBitmap(const v8::FunctionCallbackInfo<v8::Value
285
312
 
286
313
  if (image->IsNullOrUndefined()) {
287
314
  v8::Local<v8::Value> ret[2] = {ConvertToV8String(isolate, "Failed to load image"),
288
- v8::Undefined(isolate)};
315
+ v8::Null(isolate)};
289
316
 
290
317
  cb.As<v8::Function>()->Call(context, context->Global(), 2, ret);
291
318
  return;
@@ -295,7 +322,7 @@ void CanvasJSIModule::CreateImageBitmap(const v8::FunctionCallbackInfo<v8::Value
295
322
 
296
323
  v8::Local<v8::Value> ret[2] = {ConvertToV8String(isolate,
297
324
  "Failed to execute 'createImageBitmap' : The crop rect width is 0"),
298
- v8::Undefined(isolate)};
325
+ v8::Null(isolate)};
299
326
 
300
327
  cb.As<v8::Function>()->Call(context, context->Global(), 2, ret);
301
328
 
@@ -304,624 +331,134 @@ void CanvasJSIModule::CreateImageBitmap(const v8::FunctionCallbackInfo<v8::Value
304
331
  if (len >= 5 && (sh->IsNumber() && sh->IsNumber() == 0)) {
305
332
  v8::Local<v8::Value> ret[2] = {ConvertToV8String(isolate,
306
333
  "Failed to execute 'createImageBitmap' : The crop rect height is 0"),
307
- v8::Undefined(isolate)};
334
+ v8::Null(isolate)};
308
335
 
309
336
  cb.As<v8::Function>()->Call(context, context->Global(), 2, ret);
310
337
  return;
311
338
  }
312
339
 
313
-
314
340
  if (image->IsObject()) {
315
341
  auto imageObject = image.As<v8::Object>();
316
342
  auto isArrayBuffer = imageObject->IsArrayBuffer();
317
343
  auto IsArrayBufferView = imageObject->IsArrayBufferView();
318
- if (isArrayBuffer || IsArrayBufferView) {
344
+ std::optional<Rect> rect = std::nullopt;
319
345
 
320
- if (len == 1 || len == 2) {
346
+ if (isArrayBuffer || IsArrayBufferView) {
347
+ if ((len == 1 || len == 2) || (len == 5 || len == 6)) {
321
348
  if (len == 2) {
322
349
  options = ImageBitmapImpl::HandleOptions(isolate, args[1]);
323
350
  }
324
351
 
325
- auto asset = canvas_native_image_asset_create();
326
-
327
- auto shared_asset = canvas_native_image_asset_reference(asset);
328
-
329
- auto ret = new ImageBitmapImpl(asset);
330
-
352
+ if (len == 6) {
353
+ options = ImageBitmapImpl::HandleOptions(isolate, args[5]);
354
+ }
331
355
 
332
356
  auto cbFunc = args[count - 1].As<v8::Function>();
333
- auto data = v8::External::New(isolate, ret);
334
-
335
- auto jsi_callback = new JSICallback(isolate, cbFunc, data);
336
-
337
-
338
- #ifdef __ANDROID__
339
- ALooper_addFd(jsi_callback->looper_,
340
- jsi_callback->fd_[0],
341
- ALOOPER_POLL_CALLBACK,
342
- ALOOPER_EVENT_INPUT,
343
- [](int fd, int events,
344
- void *data) {
345
- auto cb = static_cast<JSICallback *>(data);
346
- bool done;
347
- read(fd, &done,
348
- sizeof(bool));
349
-
350
-
351
- v8::Isolate *isolate = cb->isolate_;
352
- v8::Locker locker(isolate);
353
- v8::Isolate::Scope isolate_scope(isolate);
354
- v8::HandleScope handle_scope(isolate);
355
- v8::Local<v8::Function> callback = cb->callback_->Get(isolate);
356
- v8::Local<v8::External> cbData = cb->data_->Get(
357
- isolate).As<v8::External>();
358
- v8::Local<v8::Context> context = callback->GetCreationContextChecked();
359
- v8::Context::Scope context_scope(context);
360
-
361
- auto ret = ImageBitmapImpl::NewInstance(isolate, cbData);
362
-
363
- v8::Local<v8::Value> args[2];
364
-
365
- if (done) {
366
- args[0] = v8::Null(isolate);
367
- args[1] = ret;
368
-
369
- } else {
370
- args[0] = v8::Exception::Error(
371
- ConvertToV8String(isolate, "Failed to load image"));
372
- args[1] = v8::Null(isolate);
373
- }
374
-
375
-
376
- callback->Call(context, context->Global(), 2, args);
377
-
378
- delete static_cast<JSICallback *>(data);
379
- return 0;
380
- }, jsi_callback);
381
-
382
- ALooper_wake(jsi_callback->looper_);
383
357
 
384
358
  if (isArrayBuffer) {
385
359
  auto arrayBuffer = imageObject.As<v8::ArrayBuffer>();
386
- auto store = arrayBuffer->GetBackingStore();
387
- auto dataBuffer = (uint8_t *) store->Data();
388
- v8::Global<v8::ArrayBuffer> ab(isolate, arrayBuffer);
389
- std::thread thread(
390
- [&dataBuffer, jsi_callback, &options, store, shared_asset](
391
- v8::Global<v8::ArrayBuffer> ab, size_t size) {
392
-
393
-
394
- auto done = canvas_native_image_bitmap_create_from_encoded_bytes_with_output(
395
- dataBuffer, size,
396
- options.flipY,
397
- options.premultiplyAlpha,
398
- options.colorSpaceConversion,
399
- options.resizeQuality,
400
- options.resizeWidth,
401
- options.resizeHeight,
402
- shared_asset);
403
-
404
- canvas_native_image_asset_release(shared_asset);
405
-
406
- write(jsi_callback->fd_[1],
407
- &done,
408
- sizeof(bool));
409
-
410
-
411
- }, std::move(ab), arrayBuffer->ByteLength());
412
-
413
- thread.detach();
414
- return;
360
+ auto size = arrayBuffer->ByteLength();
361
+ imageObject = v8::Uint8Array::New(arrayBuffer, 0, size);
415
362
  }
416
363
 
417
-
418
364
  auto ta = imageObject.As<v8::ArrayBufferView>();
419
365
 
366
+ auto image_bitmap_async_data = new ImageBitmapData();
367
+ image_bitmap_async_data->isolate_ = isolate;
368
+ image_bitmap_async_data->data_ = std::make_shared<v8::Persistent<v8::ArrayBufferView>>(
369
+ isolate, ta);
420
370
 
421
- auto array = ta->Buffer();
422
- auto offset = ta->ByteOffset();
423
- auto size = ta->ByteLength();
424
- auto data_ptr = static_cast<uint8_t *>(array->GetBackingStore()->Data()) + offset;
425
-
426
-
427
- v8::Global<v8::ArrayBufferView> ab(isolate, ta);
428
-
429
- std::thread thread(
430
- [jsi_callback, &options, shared_asset, data_ptr, size](
431
- v8::Global<v8::ArrayBufferView> ab) {
432
-
433
- auto done = canvas_native_image_bitmap_create_from_encoded_bytes_with_output(
434
- data_ptr, size,
435
- options.flipY,
436
- options.premultiplyAlpha,
437
- options.colorSpaceConversion,
438
- options.resizeQuality,
439
- options.resizeWidth,
440
- options.resizeHeight,
441
- shared_asset);
442
-
443
- canvas_native_image_asset_release(shared_asset);
371
+ auto callback = new AsyncCallback(isolate, cbFunc, [](bool done, void *data) {
372
+ if(data == nullptr){return;}
373
+ auto async_data = static_cast<AsyncCallback *>(data);
374
+ auto func = async_data->inner_.get();
375
+ if (func != nullptr && func->isolate_ != nullptr) {
376
+ v8::Isolate *isolate = func->isolate_;
377
+ v8::Locker locker(isolate);
378
+ v8::Isolate::Scope isolate_scope(isolate);
379
+ v8::HandleScope handle_scope(isolate);
380
+ v8::Local<v8::Function> callback = func->callback_.Get(
381
+ isolate);
382
+ v8::Local<v8::Context> context = callback->GetCreationContextChecked();
383
+ v8::Context::Scope context_scope(context);
444
384
 
445
- write(jsi_callback->fd_[1],
446
- &done,
447
- sizeof(bool));
385
+ if (func->data != nullptr) {
386
+ auto asset_data = static_cast<ImageBitmapData *>(func->data);
448
387
 
388
+ auto bitmap = new ImageBitmapImpl(asset_data->asset_);
389
+ auto external = v8::External::New(isolate, bitmap);
449
390
 
450
- }, std::move(ab));
451
- thread.detach();
452
-
453
- #endif
454
-
455
-
456
- #ifdef __APPLE__
457
-
458
- auto current_queue = new NSOperationQueueWrapper(true);
459
-
460
-
461
- if (isArrayBuffer) {
462
- auto arrayBuffer = imageObject.As<v8::ArrayBuffer>();
463
- auto store = arrayBuffer->GetBackingStore();
464
- auto dataBuffer = (uint8_t *) store->Data();
465
- v8::Global<v8::ArrayBuffer> ab(isolate, arrayBuffer);
466
-
467
- std::thread thread(
468
- [&dataBuffer, jsi_callback, &options, store, shared_asset, current_queue](
469
- v8::Global<v8::ArrayBuffer> ab, size_t size
470
- ) {
471
-
472
- auto done = canvas_native_image_bitmap_create_from_encoded_bytes_with_output(
473
- dataBuffer, size,
474
- options.flipY,
475
- options.premultiplyAlpha,
476
- options.colorSpaceConversion,
477
- options.resizeQuality,
478
- options.resizeWidth,
479
- options.resizeHeight,
480
- shared_asset);
391
+ auto ret = ImageBitmapImpl::NewInstance(isolate, external);
481
392
 
482
- canvas_native_image_asset_release(shared_asset);
393
+ v8::Local<v8::Value> args[2] = {
394
+ v8::Null(isolate), ret};
483
395
 
484
396
 
485
- auto main_task = [jsi_callback, current_queue, done]() {
397
+ callback->Call(context, context->Global(),
398
+ 2,
399
+ args); // ignore JS return value
400
+
401
+ delete asset_data;
486
402
 
403
+ } else {
404
+ v8::Local<v8::Value> args[1] = {
405
+ v8::Null(isolate)};
487
406
 
488
- v8::Isolate *isolate = jsi_callback->isolate_;
489
- v8::Locker locker(isolate);
490
- v8::Isolate::Scope isolate_scope(isolate);
491
- v8::HandleScope handle_scope(isolate);
492
- v8::Local<v8::Function> callback = jsi_callback->callback_->Get(isolate);
493
- v8::Local<v8::External> cbData = jsi_callback->data_->Get(
494
- isolate).As<v8::External>();
495
- v8::Local<v8::Context> context = callback->GetCreationContextChecked();
496
- v8::Context::Scope context_scope(context);
497
-
498
- auto ret = ImageBitmapImpl::NewInstance(isolate, cbData);
499
-
500
- v8::Local<v8::Value> args[2];
501
-
502
- if (done) {
503
- args[0] = v8::Null(isolate);
504
- args[1] = ret;
505
-
506
- } else {
507
- args[0] = v8::Exception::Error(
508
- ConvertToV8String(isolate, "Failed to load image"));
509
- args[1] = v8::Null(isolate);
510
- }
511
-
512
-
513
- callback->Call(context, context->Global(), 2, args);
514
-
515
-
516
- delete jsi_callback;
517
- delete current_queue;
518
-
519
- };
520
- current_queue->addOperation(main_task);
521
-
522
- }, std::move(ab), arrayBuffer->ByteLength());
523
-
524
- thread.detach();
525
-
526
- return;
527
- }
528
-
529
- auto ta = imageObject.As<v8::ArrayBufferView>();
407
+ callback->Call(context, context->Global(),
408
+ 1,
409
+ args); // ignore JS return value
410
+ }
530
411
 
412
+ }
413
+ delete static_cast<AsyncCallback *>(data);
414
+ });
531
415
 
532
- auto array = ta->Buffer();
416
+ callback->inner_->data = image_bitmap_async_data;
417
+ callback->prepare();
418
+ auto store = ta->Buffer()->GetBackingStore();
533
419
  auto offset = ta->ByteOffset();
534
- auto size = ta->ByteLength();
535
- auto data_ptr = static_cast<uint8_t *>(array->GetBackingStore()->Data()) + offset;
536
-
537
-
538
- v8::Global<v8::ArrayBufferView> ab(isolate, ta);
539
-
540
420
 
541
421
  std::thread thread(
542
- [jsi_callback, &options, shared_asset, data_ptr, size, current_queue](
543
- v8::Global<v8::ArrayBufferView> ab
544
- ) {
545
-
546
- auto done = canvas_native_image_bitmap_create_from_encoded_bytes_with_output(
547
- data_ptr, size,
548
- options.flipY,
549
- options.premultiplyAlpha,
550
- options.colorSpaceConversion,
551
- options.resizeQuality,
552
- options.resizeWidth,
553
- options.resizeHeight,
554
- shared_asset);
555
-
556
- canvas_native_image_asset_release(shared_asset);
557
-
558
- auto main_task = [jsi_callback, current_queue, done]() {
559
-
560
-
561
- v8::Isolate *isolate = jsi_callback->isolate_;
562
- v8::Locker locker(isolate);
563
- v8::Isolate::Scope isolate_scope(isolate);
564
- v8::HandleScope handle_scope(isolate);
565
- v8::Local<v8::Function> callback = jsi_callback->callback_->Get(isolate);
566
- v8::Local<v8::External> cbData = jsi_callback->data_->Get(
567
- isolate).As<v8::External>();
568
- v8::Local<v8::Context> context = callback->GetCreationContextChecked();
569
- v8::Context::Scope context_scope(context);
570
-
571
- auto ret = ImageBitmapImpl::NewInstance(isolate, cbData);
572
-
573
- v8::Local<v8::Value> args[2];
574
-
575
- if (done) {
576
- args[0] = v8::Null(isolate);
577
- args[1] = ret;
578
- } else {
579
- args[0] = v8::Exception::Error(
580
- ConvertToV8String(isolate, "Failed to load image"));
581
- args[1] = v8::Null(isolate);
582
- }
583
-
584
- callback->Call(context, context->Global(), 2, args);
585
-
586
-
587
- delete jsi_callback;
588
- delete current_queue;
589
-
590
- };
591
- current_queue->addOperation(main_task);
592
-
593
-
594
-
595
- }, std::move(ab));
596
- thread.detach();
597
-
598
- #endif
599
-
600
- return;
601
- } else if (len == 5 || len == 6) {
602
- auto asset = canvas_native_image_asset_create();
603
-
604
- auto shared_asset = canvas_native_image_asset_reference(asset);
605
-
606
- auto ret = new ImageBitmapImpl(asset);
607
-
608
- auto cbFunc = args[count - 1].As<v8::Function>();
609
- auto data = v8::External::New(isolate, ret);
610
- if (len == 6) {
611
- options = ImageBitmapImpl::HandleOptions(isolate, args[5]);
612
- }
613
-
614
- auto jsi_callback = new JSICallback(isolate, cbFunc, data);
615
-
616
-
617
- #ifdef __ANDROID__
618
- ALooper_addFd(jsi_callback->looper_,
619
- jsi_callback->fd_[0],
620
- ALOOPER_POLL_CALLBACK,
621
- ALOOPER_EVENT_INPUT,
622
- [](int fd, int events,
623
- void *data) {
624
- auto cb = static_cast<JSICallback *>(data);
625
- bool done;
626
- read(fd, &done,
627
- sizeof(bool));
628
-
629
- v8::Isolate *isolate = cb->isolate_;
630
- v8::Locker locker(isolate);
631
- v8::Isolate::Scope isolate_scope(isolate);
632
- v8::HandleScope handle_scope(isolate);
633
- v8::Local<v8::Function> callback = cb->callback_->Get(isolate);
634
- v8::Local<v8::External> cbData = cb->data_->Get(
635
- isolate).As<v8::External>();
636
- v8::Local<v8::Context> context = callback->GetCreationContextChecked();
637
- v8::Context::Scope context_scope(context);
638
-
639
- auto ret = ImageBitmapImpl::NewInstance(isolate, cbData);
640
-
641
- v8::Local<v8::Value> args[2];
642
-
643
- if (done) {
644
- args[0] = v8::Null(isolate);
645
- args[1] = ret;
646
- } else {
647
- args[0] = v8::Exception::Error(
648
- ConvertToV8String(isolate, "Failed to load image"));
649
- args[1] = v8::Null(isolate);
650
- }
651
-
652
- callback->Call(context, context->Global(), 2, args);
653
-
654
- delete static_cast<JSICallback *>(data);
655
- return 0;
656
- }, jsi_callback);
657
-
658
- ALooper_wake(jsi_callback->looper_);
659
-
660
- auto bufferValue = args[0];
661
-
662
- if (isArrayBuffer) {
663
- auto arrayBuffer = bufferValue.As<v8::ArrayBuffer>();
664
- auto dataBuffer = (uint8_t *) arrayBuffer->GetBackingStore()->Data();
665
- v8::Global<v8::ArrayBuffer> ab(isolate, arrayBuffer);
666
- std::thread thread(
667
- [&dataBuffer, jsi_callback, &options, shared_asset](
668
- float sx_or_options,
669
- float sy,
670
- float sw,
671
- float sh,
672
- v8::Global<v8::ArrayBuffer> ab,
673
- size_t size
674
- ) {
675
-
676
- auto done = canvas_native_image_bitmap_create_from_encoded_bytes_src_rect_with_output(
677
- dataBuffer, size,
678
- sx_or_options,
679
- sy,
680
- sw,
681
- sh,
422
+ [callback, image_bitmap_async_data, offset, options, rect](
423
+ std::shared_ptr<v8::BackingStore> store) {
424
+
425
+ auto data = static_cast<uint8_t *>(store->Data()) + offset;
426
+ auto size = store->ByteLength();
427
+
428
+ auto asset = canvas_native_image_asset_create();
429
+ bool done;
430
+ if (rect.has_value()) {
431
+
432
+ done = canvas_native_image_bitmap_create_from_encoded_bytes_src_rect_with_output(
433
+ data, size,
434
+ rect.value().x,
435
+ rect.value().y,
436
+ rect.value().width,
437
+ rect.value().height,
682
438
  options.flipY,
683
439
  options.premultiplyAlpha,
684
440
  options.colorSpaceConversion,
685
441
  options.resizeQuality,
686
442
  options.resizeWidth,
687
- options.resizeHeight, shared_asset);
688
-
689
- canvas_native_image_asset_release(shared_asset);
690
-
691
-
692
- write(jsi_callback->fd_[1],
693
- &done,
694
- sizeof(bool));
695
-
696
- },
697
- (float) sx_or_options->NumberValue(context).ToChecked(),
698
- (float) sy->NumberValue(context).ToChecked(),
699
- (float) sw->NumberValue(context).ToChecked(),
700
- (float) sh->NumberValue(context).ToChecked(), std::move(ab),
701
- arrayBuffer->ByteLength());
702
-
703
- thread.detach();
704
-
705
- return;
706
- }
707
-
708
- auto ta = bufferValue.As<v8::ArrayBufferView>();
709
-
710
- auto array = ta->Buffer();
711
- auto offset = ta->ByteOffset();
712
- auto size = ta->ByteLength();
713
- auto data_ptr = static_cast<uint8_t *>(array->GetBackingStore()->Data()) + offset;
714
-
715
-
716
- v8::Global<v8::ArrayBufferView> ab(isolate, ta);
717
- std::thread thread(
718
- [jsi_callback, &options, shared_asset, data_ptr, size](
719
- float sx_or_options,
720
- float sy,
721
- float sw,
722
- float sh,
723
- v8::Global<v8::ArrayBufferView> ab
724
- ) {
725
-
726
- auto done = canvas_native_image_bitmap_create_from_encoded_bytes_src_rect_with_output(
727
- data_ptr, size,
728
- sx_or_options,
729
- sy,
730
- sw,
731
- sh,
732
- options.flipY,
733
- options.premultiplyAlpha,
734
- options.colorSpaceConversion,
735
- options.resizeQuality,
736
- options.resizeWidth,
737
- options.resizeHeight, shared_asset);
738
-
739
- canvas_native_image_asset_release(shared_asset);
740
-
741
-
742
- write(jsi_callback->fd_[1],
743
- &done,
744
- sizeof(bool));
745
-
746
- },
747
- (float) sx_or_options->NumberValue(context).ToChecked(),
748
- (float) sy->NumberValue(context).ToChecked(),
749
- (float) sw->NumberValue(context).ToChecked(),
750
- (float) sh->NumberValue(context).ToChecked(), std::move(ab));
751
- thread.detach();
752
-
753
- #endif
754
-
755
- #ifdef __APPLE__
756
-
757
- auto current_queue = new NSOperationQueueWrapper(true);
758
-
759
-
760
- auto bufferValue = args[0];
761
-
762
- if (isArrayBuffer) {
763
- auto arrayBuffer = bufferValue.As<v8::ArrayBuffer>();
764
- auto dataBuffer = (uint8_t *) arrayBuffer->GetBackingStore()->Data();
765
- v8::Global<v8::ArrayBuffer> ab(isolate, arrayBuffer);
766
- std::thread thread(
767
- [&dataBuffer, jsi_callback, &options, shared_asset, current_queue](
768
- float sx_or_options,
769
- float sy,
770
- float sw,
771
- float sh,
772
- v8::Global<v8::ArrayBuffer> ab,
773
- size_t size
774
- ) {
775
-
776
- auto done = canvas_native_image_bitmap_create_from_encoded_bytes_src_rect_with_output(
777
- dataBuffer, size,
778
- sx_or_options,
779
- sy,
780
- sw,
781
- sh,
443
+ options.resizeHeight, asset);
444
+ } else {
445
+ done = canvas_native_image_bitmap_create_from_encoded_bytes_with_output(
446
+ data, size,
782
447
  options.flipY,
783
448
  options.premultiplyAlpha,
784
449
  options.colorSpaceConversion,
785
450
  options.resizeQuality,
786
451
  options.resizeWidth,
787
- options.resizeHeight, shared_asset);
788
-
789
- canvas_native_image_asset_release(shared_asset);
790
-
791
-
792
- auto main_task = [jsi_callback, current_queue, done]() {
793
-
794
-
795
- v8::Isolate *isolate = jsi_callback->isolate_;
796
- v8::Locker locker(isolate);
797
- v8::Isolate::Scope isolate_scope(isolate);
798
- v8::HandleScope handle_scope(isolate);
799
- v8::Local<v8::Function> callback = jsi_callback->callback_->Get(isolate);
800
- v8::Local<v8::External> cbData = jsi_callback->data_->Get(
801
- isolate).As<v8::External>();
802
- v8::Local<v8::Context> context = callback->GetCreationContextChecked();
803
- v8::Context::Scope context_scope(context);
804
-
805
-
806
-
807
- auto ret = ImageBitmapImpl::NewInstance(isolate, cbData);
808
-
809
- v8::Local<v8::Value> args[2];
810
-
811
- if (done) {
812
- args[0] = v8::Null(isolate);
813
- args[1] = ret;
814
- } else {
815
- args[0] = v8::Exception::Error(
816
- ConvertToV8String(isolate, "Failed to load image"));
817
- args[1] = v8::Null(isolate);
818
- }
819
-
820
- callback->Call(context, context->Global(), 2, args);
821
-
822
-
823
- delete jsi_callback;
824
- delete current_queue;
825
-
826
- };
827
- current_queue->addOperation(main_task);
828
-
829
- },
830
- (float) sx_or_options->NumberValue(context).ToChecked(),
831
- (float) sy->NumberValue(context).ToChecked(),
832
- (float) sw->NumberValue(context).ToChecked(),
833
- (float) sh->NumberValue(context).ToChecked(),
834
- std::move(ab),
835
- arrayBuffer->ByteLength());
836
-
837
- thread.detach();
838
-
839
- return;
840
- }
841
-
842
- auto ta = bufferValue.As<v8::ArrayBufferView>();
843
-
844
- auto array = ta->Buffer();
845
- auto offset = ta->ByteOffset();
846
- auto size = ta->ByteLength();
847
- auto data_ptr = static_cast<uint8_t*>(array->GetBackingStore()->Data()) + offset;
848
-
849
-
850
-
851
- v8::Global<v8::ArrayBufferView> ab(isolate, ta);
852
- std::thread thread(
853
- [jsi_callback, &options, data_ptr, size, shared_asset, current_queue](
854
- float sx_or_options,
855
- float sy,
856
- float sw,
857
- float sh,
858
- v8::Global<v8::ArrayBufferView> ab
859
- ) {
860
-
861
- auto done = canvas_native_image_bitmap_create_from_encoded_bytes_src_rect_with_output(
862
- data_ptr, size,
863
- sx_or_options,
864
- sy,
865
- sw,
866
- sh,
867
- options.flipY,
868
- options.premultiplyAlpha,
869
- options.colorSpaceConversion,
870
- options.resizeQuality,
871
- options.resizeWidth,
872
- options.resizeHeight, shared_asset);
873
-
874
- canvas_native_image_asset_release(shared_asset);
875
-
876
- auto main_task = [jsi_callback, current_queue, done]() {
877
-
878
-
879
- v8::Isolate *isolate = jsi_callback->isolate_;
880
- v8::Locker locker(isolate);
881
- v8::Isolate::Scope isolate_scope(isolate);
882
- v8::HandleScope handle_scope(isolate);
883
- v8::Local<v8::Function> callback = jsi_callback->callback_->Get(isolate);
884
- v8::Local<v8::External> cbData = jsi_callback->data_->Get(
885
- isolate).As<v8::External>();
886
- v8::Local<v8::Context> context = callback->GetCreationContextChecked();
887
- v8::Context::Scope context_scope(context);
888
-
889
- auto ret = ImageBitmapImpl::NewInstance(isolate, cbData);
890
-
891
- v8::Local<v8::Value> args[2];
892
-
893
- if (done) {
894
- args[0] = v8::Null(isolate);
895
- args[1] = ret;
896
- } else {
897
- args[0] = v8::Exception::Error(
898
- ConvertToV8String(isolate, "Failed to load image"));
899
- args[1] = v8::Null(isolate);
900
- }
901
-
902
- callback->Call(context, context->Global(), 2, args);
903
-
904
-
905
- delete jsi_callback;
906
- delete current_queue;
907
-
908
- };
909
- current_queue->addOperation(main_task);
910
-
911
-
912
-
913
- },
914
- (float) sx_or_options->NumberValue(context).ToChecked(),
915
- (float) sy->NumberValue(context).ToChecked(),
916
- (float) sw->NumberValue(context).ToChecked(),
917
- (float) sh->NumberValue(context).ToChecked(), std::move(ab));
452
+ options.resizeHeight, asset);
453
+ }
454
+ if (callback != nullptr) {
455
+ image_bitmap_async_data->asset_ = asset;
456
+ callback->execute(done);
457
+ }
458
+
459
+ }, std::move(store));
918
460
  thread.detach();
919
461
 
920
-
921
-
922
- #endif
923
-
924
-
925
462
  return;
926
463
  }
927
464
  }
@@ -1070,242 +607,124 @@ void CanvasJSIModule::Create2DContextWithPointer(const v8::FunctionCallbackInfo<
1070
607
  args.GetReturnValue().Set(ret);
1071
608
  }
1072
609
 
610
+ struct FileData {
611
+ char *error_;
612
+ U8Buffer* data;
613
+
614
+ ~FileData() {
615
+ if (error_ != nullptr) {
616
+ canvas_native_string_destroy(error_);
617
+ error_ = nullptr;
618
+ }
619
+ }
620
+ };
621
+
1073
622
  void CanvasJSIModule::ReadFile(const v8::FunctionCallbackInfo<v8::Value> &args) {
1074
623
  auto isolate = args.GetIsolate();
1075
624
  auto file = ConvertFromV8String(isolate, args[0]);
1076
625
  auto cbFunc = args[1].As<v8::Function>();
626
+
627
+
628
+ auto callback = new AsyncCallback(isolate, cbFunc, [](bool done, void *data) {
629
+ if(data == nullptr){return;}
630
+ auto async_data = static_cast<AsyncCallback *>(data);
631
+ auto func = async_data->inner_.get();
632
+ if (func != nullptr && func->isolate_ != nullptr) {
633
+ v8::Isolate *isolate = func->isolate_;
634
+ v8::Locker locker(isolate);
635
+ v8::Isolate::Scope isolate_scope(isolate);
636
+ v8::HandleScope handle_scope(isolate);
637
+ v8::Local<v8::Function> callback = func->callback_.Get(
638
+ isolate);
639
+ v8::Local<v8::Context> context = callback->GetCreationContextChecked();
640
+ v8::Context::Scope context_scope(context);
641
+
642
+ if (func->data != nullptr) {
643
+ auto file_data = static_cast<FileData*>(func->data);
644
+
645
+ v8::Local<v8::Value> args[2];
646
+
647
+ if (done) {
648
+ args[0] = v8::Null(isolate);
649
+
650
+ auto buf = (void *) canvas_native_u8_buffer_get_bytes_mut(file_data->data);
651
+ auto size = (size_t) canvas_native_u8_buffer_get_length(file_data->data);
652
+
653
+ auto store = v8::ArrayBuffer::NewBackingStore(buf, size,
654
+ [](void *data,
655
+ size_t length,
656
+ void *deleter_data) {
657
+ if (deleter_data !=
658
+ nullptr) {
659
+ delete static_cast<FileData *>(deleter_data);
660
+
661
+ }
662
+ },
663
+ func->data);
664
+
665
+ args[1] = v8::ArrayBuffer::New(isolate, std::move(store));
666
+ } else {
667
+ auto error = file_data->error_;
668
+ args[0] = v8::Exception::Error(ConvertToV8String(isolate, error));
669
+ args[1] = v8::Null(isolate);
670
+ delete file_data;
671
+ }
1077
672
 
1078
- auto jsi_callback = new JSIReadFileCallback(isolate, cbFunc);
1079
-
1080
- #ifdef __ANDROID__
1081
- ALooper_addFd(jsi_callback->looper_,
1082
- jsi_callback->fd_[0],
1083
- ALOOPER_POLL_CALLBACK,
1084
- ALOOPER_EVENT_INPUT,
1085
- [](int fd, int events,
1086
- void *data) {
1087
-
1088
- auto cb = static_cast<JSIReadFileCallback *>(data);
1089
- bool done;
1090
- read(fd, &done,
1091
- sizeof(bool));
1092
-
1093
- if (cb->data_ == nullptr) {
1094
- return 1;
1095
- }
1096
-
1097
- v8::Isolate *isolate = cb->isolate_;
1098
- v8::Locker locker(isolate);
1099
- v8::Isolate::Scope isolate_scope(isolate);
1100
- v8::HandleScope handle_scope(isolate);
1101
- v8::Local<v8::Function> callback = cb->callback_.Get(isolate);
1102
- v8::Local<v8::Context> context = callback->GetCreationContextChecked();
1103
- v8::Context::Scope context_scope(context);
1104
-
1105
- v8::Local<v8::Value> args[2];
1106
-
1107
- if (done) {
1108
- args[0] = v8::Null(isolate);
1109
-
1110
- auto vec = cb->data_;
1111
-
1112
- auto buf = (void *) canvas_native_u8_buffer_get_bytes_mut(vec);
1113
- auto size = (size_t) canvas_native_u8_buffer_get_length(vec);
1114
-
1115
- auto store = v8::ArrayBuffer::NewBackingStore(buf, size,
1116
- [](void *data,
1117
- size_t length,
1118
- void *deleter_data) {
1119
- if (deleter_data !=
1120
- nullptr) {
1121
- // a little extreme :'D
1122
- delete static_cast<JSIReadFileCallback *>(deleter_data);
1123
- }
1124
- },
1125
- data);
1126
-
1127
- args[1] = v8::ArrayBuffer::New(isolate, std::move(store));
1128
- } else {
1129
- auto error = cb->error_;
1130
- args[0] = v8::Exception::Error(ConvertToV8String(isolate, error));
1131
- args[1] = v8::Null(isolate);
1132
- }
1133
-
1134
- v8::TryCatch tc(isolate);
1135
- v8::Local<v8::Value> result;
1136
- if (!callback->Call(context, context->Global(), 2, args).ToLocal(
1137
- &result)) {
1138
- if (tc.HasCaught()) {
1139
-
1140
- v8::Local<v8::Value> stack;
1141
- bool success = tc.StackTrace(context).ToLocal(&stack);
1142
- if (!success || stack.IsEmpty()) {
1143
- if (!done) {
1144
- delete cb;
1145
- }
1146
- return 0;
1147
- }
1148
-
1149
- v8::Local<v8::String> stackV8Str;
1150
- success = stack->ToDetailString(context).ToLocal(&stackV8Str);
1151
- if (!success || stackV8Str.IsEmpty()) {
1152
- if (!done) {
1153
- delete cb;
1154
- }
1155
- return 0;
1156
- }
1157
- LogToConsole(ConvertFromV8String(isolate, stackV8Str));
1158
-
1159
- }
1160
- }
1161
-
1162
- if (!done) {
1163
- delete cb;
1164
- }
1165
- return 0;
1166
- }, jsi_callback);
1167
-
1168
- ALooper_wake(jsi_callback->looper_);
673
+ v8::TryCatch tc(isolate);
674
+ v8::Local<v8::Value> result;
675
+ if (!callback->Call(context, context->Global(), 2, args).ToLocal(
676
+ &result)) {
677
+ if (tc.HasCaught()) {
678
+
679
+ // v8::Local<v8::Value> stack;
680
+ // bool success = tc.StackTrace(context).ToLocal(&stack);
681
+ // if (!success || stack.IsEmpty()) {
682
+ // if (!done) {
683
+ // delete async_data;
684
+ // }
685
+ // return;
686
+ // }
687
+ //
688
+ // v8::Local<v8::String> stackV8Str;
689
+ // success = stack->ToDetailString(context).ToLocal(&stackV8Str);
690
+ // if (!success || stackV8Str.IsEmpty()) {
691
+ // if (!done) {
692
+ // delete async_data;
693
+ // }
694
+ // return;
695
+ // }
1169
696
 
697
+ }
698
+ }
699
+
700
+ delete async_data;
701
+
702
+ }
703
+ }
704
+ });
705
+
706
+ callback->prepare();
707
+
1170
708
  std::thread thread(
1171
- [jsi_callback](const std::string &file) {
709
+ [callback, file]() {
1172
710
  bool done = false;
1173
711
  auto ret = canvas_native_helper_read_file(file.c_str());
1174
712
 
1175
713
  if (!canvas_native_helper_read_file_has_error(ret)) {
1176
- auto buf = canvas_native_helper_read_file_get_data(ret);
1177
-
1178
- jsi_callback->SetData(buf);
1179
-
714
+ auto buf = canvas_native_helper_read_file_take_data(ret);
715
+ callback->inner_->data = new FileData {nullptr, buf};
1180
716
  done = true;
1181
717
  } else {
1182
718
  auto error = canvas_native_helper_read_file_get_error(ret);
1183
-
1184
- jsi_callback->SetError(const_cast<char *>(error));
719
+ callback->inner_->data = new FileData {const_cast<char *>(error), nullptr};
1185
720
  }
1186
721
  canvas_native_helper_release(ret);
1187
-
1188
- write(jsi_callback->fd_[1],
1189
- &done,
1190
- sizeof(bool));
1191
-
1192
- }, std::move(file));
722
+ callback->execute(done);
723
+ });
1193
724
 
1194
725
  thread.detach();
1195
-
1196
- #endif
1197
-
1198
-
1199
- #ifdef __APPLE__
1200
-
1201
- auto current_queue = new NSOperationQueueWrapper(true);
1202
-
1203
- auto queue = new NSOperationQueueWrapper(false);
1204
-
1205
- auto task = [jsi_callback, current_queue, queue, file]() {
1206
-
1207
-
1208
-
1209
- bool done = false;
1210
- auto ret = canvas_native_helper_read_file(file.c_str());
1211
-
1212
- if (!canvas_native_helper_read_file_has_error(ret)) {
1213
- auto buf = canvas_native_helper_read_file_get_data(ret);
1214
-
1215
- jsi_callback->SetData(buf);
1216
-
1217
- done = true;
1218
- } else {
1219
- auto error = canvas_native_helper_read_file_get_error(ret);
1220
-
1221
- jsi_callback->SetError((char*)error);
1222
- }
1223
-
1224
- canvas_native_helper_release(ret);
1225
-
1226
- auto main_task = [jsi_callback, current_queue, queue, done]() {
1227
-
1228
- v8::Isolate *isolate = jsi_callback->isolate_;
1229
- v8::Locker locker(isolate);
1230
- v8::Isolate::Scope isolate_scope(isolate);
1231
- v8::HandleScope handle_scope(isolate);
1232
- v8::Local<v8::Function> callback = jsi_callback->callback_.Get(isolate);
1233
- v8::Local<v8::Context> context = callback->GetCreationContextChecked();
1234
- v8::Context::Scope context_scope(context);
1235
-
1236
- v8::Local<v8::Value> args[2];
1237
-
1238
- if (done) {
1239
- args[0] = v8::Null(isolate);
1240
-
1241
- auto vec = jsi_callback->data_;
1242
-
1243
-
1244
- auto buf = (void*)canvas_native_u8_buffer_get_bytes_mut(vec);
1245
-
1246
- auto size = (size_t)canvas_native_u8_buffer_get_length(vec);
1247
-
1248
- auto store = v8::ArrayBuffer::NewBackingStore(buf, size,
1249
- [](void *data,
1250
- size_t length,
1251
- void *deleter_data) {
1252
- if (deleter_data !=
1253
- nullptr) {
1254
- // a little extreme :'D
1255
- delete static_cast<JSIReadFileCallback *>(deleter_data);
1256
- }
1257
- },
1258
- jsi_callback);
1259
-
1260
- args[1] = v8::ArrayBuffer::New(isolate, std::move(store));
1261
- } else {
1262
- auto error = jsi_callback->error_;
1263
- args[0] = v8::Exception::Error(ConvertToV8String(isolate, error));
1264
- args[1] = v8::Null(isolate);
1265
- }
1266
-
1267
- v8::TryCatch tc(isolate);
1268
- v8::Local<v8::Value> result;
1269
- if (!callback->Call(context, context->Global(), 2, args).ToLocal(
1270
- &result)) {
1271
- if (tc.HasCaught()) {
1272
-
1273
- v8::Local<v8::Value> stack;
1274
- bool success = tc.StackTrace(context).ToLocal(&stack);
1275
- if (!success || stack.IsEmpty()) {
1276
- if (!done) {
1277
- delete jsi_callback;
1278
- }
1279
- return;
1280
- }
1281
-
1282
- v8::Local<v8::String> stackV8Str;
1283
- success = stack->ToDetailString(context).ToLocal(&stackV8Str);
1284
- if (!success || stackV8Str.IsEmpty()) {
1285
- if (!done) {
1286
- delete jsi_callback;
1287
- }
1288
- return;
1289
- }
1290
- LogToConsole(ConvertFromV8String(isolate, stackV8Str));
1291
-
1292
- }
1293
- }
1294
-
1295
- if (!done) {
1296
- delete jsi_callback;
1297
- }
1298
-
1299
- delete queue;
1300
- delete current_queue;
1301
-
1302
- };
1303
- current_queue->addOperation(main_task);
1304
- };
1305
-
1306
- queue->addOperation(task);
1307
- #endif
1308
-
726
+
727
+
1309
728
 
1310
729
  }
1311
730