int main(int argc, const char * argv[]) { char code[1000] = ""; FILE *fp = fopen(argv[1], "r"); int i = 0; char c; while((c = fgetc(fp)) != EOF){ code[i++] = c; } fclose(fp); code[i] = '\0'; VM* vm = newVM(); Context* global = newContext(); Contexts* contexts = newContexts(); pushContext(contexts, global); Method* methods[MAX_METHOD_NUM] = { defineMethod("+", method_add), defineMethod("add", method_add), defineMethod("-", method_minus), defineMethod("minus", method_minus), defineMethod("*", method_multiply), defineMethod("multiply", method_multiply), defineMethod("/", method_divide), defineMethod("divide", method_divide), defineMethod("\\", method_last), defineMethod("last", method_last) }; return execute(code, vm, contexts, methods); }
void test1() { printf("Test 1: Objects on stack are preserved.\n"); VM* vm = newVM(); pushInt(vm, 1); pushInt(vm, 2); gc(vm); assert(vm->numObjects == 2, "Should have preserved objects."); freeVM(vm); }
void test2() { printf("Test 2: Unreached objects are collected.\n"); VM* vm = newVM(); pushInt(vm, 1); pushInt(vm, 2); pop(vm); pop(vm); gc(vm); assert(vm->numObjects == 0, "Should have collected objects."); freeVM(vm); }
object * new_obj(){ object *o=(object*)malloc(sizeof(object)); // memset(o,0,sizeof(object)); // printf("%p type:%d\n",o,o->type); if(global_vm==NULL){ global_vm = newVM(); // printf("%p type:%d\n",o,o->type); // test_vm_gc(); // printf("===test gc ok.===\n",o,o->type); } // vm_push_obj(o); return o; }
void perfTest() { printf("Performance Test.\n"); VM* vm = newVM(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 20; j++) { pushInt(vm, i); } for (int k = 0; k < 20; k++) { pop(vm); } } freeVM(vm); }
void test3() { printf("Test 3: Reach nested objects.\n"); VM* vm = newVM(); pushInt(vm, 1); pushInt(vm, 2); pushPair(vm); pushInt(vm, 3); pushInt(vm, 4); pushPair(vm); pushPair(vm); gc(vm); assert(vm->numObjects == 7, "Should have reached objects."); freeVM(vm); }
void test4() { printf("Test 4: Handle cycles.\n"); VM* vm = newVM(); pushInt(vm, 1); pushInt(vm, 2); Object* a = pushPair(vm); pushInt(vm, 3); pushInt(vm, 4); Object* b = pushPair(vm); a->tail = b; b->tail = a; gc(vm); assert(vm->numObjects == 4, "Should have collected objects."); freeVM(vm); }