iOS: implement Image Pixel
This commit is contained in:
@@ -20,8 +20,14 @@
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import <DoricCore/DoricExtensions.h>
|
||||
#import "DoricJSCoreExecutor.h"
|
||||
|
||||
void ReleaseArrayBufferData(void *bytes, void *deallocatorContext) {
|
||||
id data = (__bridge_transfer id) deallocatorContext;
|
||||
data = nil;
|
||||
}
|
||||
|
||||
@interface DoricJSCoreExecutor ()
|
||||
|
||||
@property(nonatomic, strong) JSContext *jsContext;
|
||||
@@ -57,7 +63,19 @@ - (void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
|
||||
|
||||
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args {
|
||||
JSValue *obj = [self.jsContext objectForKeyedSubscript:objName];
|
||||
JSValue *ret = [obj invokeMethod:funcName withArguments:args];
|
||||
JSValue *ret = [obj invokeMethod:funcName withArguments:[args map:^id(id obj) {
|
||||
if ([obj isKindOfClass:NSData.class]) {
|
||||
NSData *data = (NSData *) obj;
|
||||
JSObjectRef jsObject = JSObjectMakeArrayBufferWithBytesNoCopy(self.jsContext.JSGlobalContextRef,
|
||||
(void *) data.bytes,
|
||||
data.length,
|
||||
ReleaseArrayBufferData,
|
||||
(__bridge_retained void *) data,
|
||||
NULL);
|
||||
return [JSValue valueWithJSValueRef:jsObject inContext:self.jsContext];
|
||||
}
|
||||
return obj;
|
||||
}]];
|
||||
[self checkJSException];
|
||||
return ret;
|
||||
}
|
||||
|
12
doric-iOS/Pod/Classes/Engine/JSValue+Doric.h
Normal file
12
doric-iOS/Pod/Classes/Engine/JSValue+Doric.h
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2021/11/19.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <JavaScriptCore/JavaScriptCore.h>
|
||||
|
||||
@interface JSValue (Doric)
|
||||
- (BOOL)isArrayBuffer;
|
||||
|
||||
- (NSData *)toArrayBuffer;
|
||||
@end
|
31
doric-iOS/Pod/Classes/Engine/JSValue+Doric.m
Normal file
31
doric-iOS/Pod/Classes/Engine/JSValue+Doric.m
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2021/11/19.
|
||||
//
|
||||
|
||||
#import "JSValue+Doric.h"
|
||||
|
||||
@implementation JSValue (Doric)
|
||||
- (BOOL)isArrayBuffer {
|
||||
JSContextRef ctx = self.context.JSGlobalContextRef;
|
||||
JSValueRef jsValueRef = self.JSValueRef;
|
||||
if (self.isObject) {
|
||||
JSTypedArrayType type = JSValueGetTypedArrayType(ctx, jsValueRef, NULL);
|
||||
return type == kJSTypedArrayTypeArrayBuffer;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (NSData *)toArrayBuffer {
|
||||
if (!self.isArrayBuffer) {
|
||||
return nil;
|
||||
}
|
||||
JSContextRef ctx = self.context.JSGlobalContextRef;
|
||||
JSValueRef jsValueRef = self.JSValueRef;
|
||||
JSObjectRef ref = JSValueToObject(ctx, jsValueRef, NULL);
|
||||
size_t size = JSObjectGetArrayBufferByteLength(ctx, ref, NULL);
|
||||
void *ptr = JSObjectGetArrayBufferBytesPtr(ctx, ref, NULL);
|
||||
|
||||
return [[NSData alloc] initWithBytesNoCopy:ptr length:size freeWhenDone:NO];
|
||||
}
|
||||
|
||||
@end
|
Reference in New Issue
Block a user