示例#1
0
/* It works like:
 * 1 - boot smop,
 * 2 - init a new object, and set the methods
 * 3 - get a stack
 * 4 - call method 1
 * 5 - lower the refcount (should init destruction)
 * 6 - call method 2
 * 7 - call stack loop
 */
int main(int argc, char** argv) {
  printf("1..6\n");

  smop_init();

  SMOP__Object* obj = smop_lowlevel_alloc(sizeof(SMOP__ResponderInterface));
  if (!obj) {
    printf("not ");
  }
  printf("ok 1 - object allocated successfully.\n");

  SMOP__ResponderInterface* ri = (SMOP__ResponderInterface*)obj;
  ri->MESSAGE = custom_MESSAGE;
  ri->REFERENCE = smop_lowlevel_refcnt_inc;
  ri->RELEASE = smop_lowlevel_refcnt_dec;

  SMOP__Object* intrp = SMOP_DISPATCH(SMOP__INTPTR__InterpreterInstance,
                                      SMOP_RI(SMOP__INTPTR__InterpreterInstance),
                                      SMOP__ID__new,
                                      SMOP__NATIVE__capture_create(SMOP__INTPTR__InterpreterInstance,
                                                                   SMOP__INTPTR__InterpreterInstance, NULL, NULL));
  
  if (!intrp) {
    printf("not ");
  }
  printf("ok 2 - got new interp successfully %p.\n",intrp);

  SMOP_DISPATCH(intrp, ri, SMOP__ID__new, NULL);

  /* At this point, the destruction code for the object will be put in
   * the stack. That's why we still can call the second method just
   * below that. The object will only be invalidated when the stack
   * loop is called.
   */
  SMOP_RELEASE(intrp, obj);

  SMOP_DISPATCH(intrp, ri, SMOP__ID__invocant, NULL);

  SMOP_DISPATCH(intrp, SMOP_RI(intrp),
                SMOP__ID__loop, 
                SMOP__NATIVE__capture_create(intrp,
                                             SMOP_REFERENCE(intrp,intrp),
                                             NULL, NULL));

  SMOP_RELEASE(SMOP__INTPTR__InterpreterInstance, intrp);

  printf("ok 6 - finished succesfully.\n");

  smop_destr();

  return 0;
}
示例#2
0
文件: hash.c 项目: ab5tract/mu
smop_util_hash* smop_util_hash_copy(SMOP__Object* interpreter,smop_util_hash* hash) {
  smop_util_hash* ret = malloc(sizeof(smop_util_hash));
  ret->size = hash->size;
  ret->content = calloc(ret->size,sizeof(smop_util_hash_bucket*));
  int i;
  for (i=0;i < hash->size;i++) {
    smop_util_hash_bucket* bucket = hash->content[i];
    smop_util_hash_bucket* new_bucket = bucket ? (smop_util_hash_bucket*) malloc(sizeof(smop_util_hash_bucket)) : NULL;
    ret->content[i] = new_bucket;
    while (bucket) {
      new_bucket->value = SMOP_REFERENCE(interpreter,bucket->value);
      new_bucket->key = SMOP_REFERENCE(interpreter,bucket->key);
      smop_util_hash_bucket* next = bucket->next;
      if (next) {
       smop_util_hash_bucket* new_next = (smop_util_hash_bucket*) malloc(sizeof(smop_util_hash_bucket));
       new_bucket->next = new_next;
       new_bucket = new_next;
      }
      bucket = next;
    }
    if (new_bucket) new_bucket->next = NULL;
  }
  return ret;
}