-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Usage of JPMemory
bang edited this page Nov 6, 2015
·
1 revision
JPMemory is an extension of JSPatch, it provides a set of functions that manipulate memory.
##Usage
- Import
JPMemory.h
/JPMemory.m
to your project - Call
require('JPEngine').addExtensions(['JPMemory'])
before using it.
##API
####memset()
/ memmove()
/ memcpy()
/ malloc()
/ free()
The same of C functions:
//js var o = malloc(1024) memset(o, 1, 1024) free(o)
####`pval(pointer)`
>Get object from pointer
>```objc
//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 } })
####`pvalBool(pointer)`
>Get value of BOOL pointer
>```objc
//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) } })
>if you want to get pointer value of other types like int / float / double, you can add functions like `pvalInt()` / `pvalFloat()` in JPMemory by your self.
####`getPointer(obj)`
>Get pointer of the object, the same as `&` symbol in OC
>```objc
//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)
####`sizeof(typename)`
> Get the size of type
> ```js
var obj = require('NSObject').alloc().init()
console.log(sizeof('id'))
console.log(sizeof('CGRect'))