-
Notifications
You must be signed in to change notification settings - Fork 2.2k
JPMemory 使用文档
bang edited this page May 11, 2017
·
3 revisions
JPMemory 提供了内存操作相关的系列函数,使 JS 脚本可以进行内存/指针操作。
- 把
JPMemory.h
/JPMemory.m
加入项目 - JS 脚本使用前调用
require('JPEngine').addExtensions(['JPMemory'])
与 C 同名函数作用/参数/用法相同:
//js
var o = malloc(1024)
memset(o, 1, 1024)
free(o)
获取指针对应的 NSObject 对象
//OC
@implementation JPTestObject
- (void)passingErrorPointer:(NSError **)errorPointer {
NSError *error = *errorPointer;
//use error
}
@end
//js
defineClass('JPTestObject', {
passingErrorPointer: function(errorPointer) {
var error = pval(errorPointer)
//use error
}
})
获取 BOOL 指针的值
//OC
@implementation JPTestObject
+ (void)passingBoolPointer:(BOOL *)p {
BOOL b = *p;
}
@end
...
BOOL b = YES;
[JPTestObject passingBoolPointer:&b];
//js
defineClass('JPTestObject', {
passingBoolPointer: function(p) {
var b = pvalBool(p)
}
})
若需要用到取其他类型的指针值,例如 int float double 等,可以自行在 JPMemory 上参照
pvalBool()
加上相关函数。
获取指针地址,相当于
&
符号
//OC
id obj = [[NSObject alloc] init];
void *pointer = &obj;
void *pool = malloc(1024);
void *poolPointer = &pool;
//js
var obj = require('NSObject').alloc().init()
var pointer = getPointer(obj)
var pool = malloc(1024)
var poolPointer = getPointer(pool)
获取类型大小
var obj = require('NSObject').alloc().init()
console.log(sizeof('id'))
console.log(sizeof('CGRect'))