void begin() { initFibers(); CTR_FIBER_CONTROLLER = ctr_internal_create_object(CTR_OBJECT_TYPE_OTEX); ctr_internal_create_func(CTR_FIBER_CONTROLLER, ctr_build_string_from_cstring("toString"), &ctr_fiber_tostring); ctr_internal_create_func(CTR_FIBER_CONTROLLER, ctr_build_string_from_cstring("new:"), &ctr_fiber_spawn); ctr_internal_create_func(CTR_FIBER_CONTROLLER, ctr_build_string_from_cstring("yield:"), &ctr_fiber_yield); //with value ctr_internal_create_func(CTR_FIBER_CONTROLLER, ctr_build_string_from_cstring("yield"), &ctr_fiber_yield); //without value ctr_internal_create_func(CTR_FIBER_CONTROLLER, ctr_build_string_from_cstring("yielded"), &ctr_fiber_yielded); ctr_internal_create_func(CTR_FIBER_CONTROLLER, ctr_build_string_from_cstring("waitForAll"), &ctr_fiber_join_all); ctr_internal_object_add_property(CTR_FIBER_CONTROLLER, ctr_build_string_from_cstring("fiberId"), ctr_build_number_from_float(-1), CTR_CATEGORY_PRIVATE_PROPERTY); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string_from_cstring("Fiber"), CTR_FIBER_CONTROLLER, 0); }
/** * [Map] put: [Element] at: [Key] * * Puts a key-value pair in a map. * * Usage: * * map put: 'hello' at: 'world'. * * In other languages: * Dutch: [Lijst] zet: [Object] bij: [Object] * Zet het gespecificeerde object element bij de plek die bekend staat als * het andere object. Net als bij een reeks, alleen in dit geval is het tweede * Object de sleutel waarmee het eerste object weer uit de lijst gevist kan * worden. */ ctr_object* ctr_map_put(ctr_object* myself, ctr_argument* argumentList) { char* key; long keyLen; ctr_object* putKey; ctr_object* putValue = argumentList->object; ctr_argument* nextArgument = argumentList->next; ctr_argument* emptyArgumentList = ctr_heap_allocate(sizeof(ctr_argument)); emptyArgumentList->next = NULL; emptyArgumentList->object = NULL; putKey = ctr_send_message(nextArgument->object, CTR_DICT_TOSTRING, strlen(CTR_DICT_TOSTRING), emptyArgumentList); /* If developer returns something other than string (ouch, toString), then cast anyway */ if (putKey->info.type != CTR_OBJECT_TYPE_OTSTRING) { putKey = ctr_internal_cast2string(putKey); } key = ctr_heap_allocate( putKey->value.svalue->vlen * sizeof( char ) ); keyLen = putKey->value.svalue->vlen; memcpy(key, putKey->value.svalue->value, keyLen); ctr_internal_object_delete_property(myself, ctr_build_string(key, keyLen), 0); ctr_internal_object_add_property(myself, ctr_build_string(key, keyLen), putValue, 0); ctr_heap_free( emptyArgumentList ); ctr_heap_free( key ); return myself; }
ctr_object* ctr_fiber_spawn(ctr_object* myself, ctr_argument* argumentList) { ctr_object* fiberObj = ctr_fiber_create(myself, NULL); ctr_object* blk = argumentList->object; if(blk == NULL || blk->info.type != CTR_OBJECT_TYPE_OTBLOCK) {CtrStdFlow = ctr_build_string_from_cstring("Fiber's spawning requires a block."); return myself;} int fiber = spawnFiber(argumentList->object); ctr_internal_object_add_property(fiberObj, ctr_build_string_from_cstring("fiberId"), ctr_build_number_from_float(fiber), CTR_CATEGORY_PRIVATE_PROPERTY); return fiberObj; }
/** * File * * Represents a File object. * Creates a new file object based on the specified path. * * Usage: * * File new: '/example/path/to/file.txt'. */ ctr_object* ctr_file_new(ctr_object* myself, ctr_argument* argumentList) { ctr_object* s = ctr_object_make(myself, argumentList); ctr_object* pathObject; s->info.type = CTR_OBJECT_TYPE_OTEX; /* indicates resource for GC */ s->link = myself; s->value.rvalue = NULL; pathObject = ctr_build_string( argumentList->object->value.svalue->value, argumentList->object->value.svalue->vlen ); ctr_internal_object_add_property( s, ctr_build_string_from_cstring( "path" ), pathObject, 0 ); return s; }
/** * @internal * * Adds the Curl object into the world **/ void begin(){ ctr_object* CtrStdHandle = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdHandle, ctr_build_string_from_cstring( "new" ), &ctr_curl_new ); ctr_internal_create_func(CtrStdHandle, ctr_build_string_from_cstring( "nieuw" ), &ctr_curl_new ); ctr_internal_create_func(CtrStdHandle, ctr_build_string_from_cstring( "type" ), &ctr_curl_type ); ctr_internal_create_func(CtrStdHandle, ctr_build_string_from_cstring( "cleanup" ), &ctr_curl_cleanup ); ctr_internal_create_func(CtrStdHandle, ctr_build_string_from_cstring( "perform" ), &ctr_curl_perform ); ctr_internal_create_func(CtrStdHandle, ctr_build_string_from_cstring( "respondTo:and:"), &ctr_curl_respondto ); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string_from_cstring( "Curl" ), CtrStdHandle, CTR_CATEGORY_PUBLIC_PROPERTY); CtrStdHandle->link = CtrStdObject; CtrStdHandle->info.sticky = 1; }
/** * C-constructor function, as declared above. * * This function gets called when the plugin is loaded into memory. * Here you have a chance to add the new object(s) to the World. * * In our case, we are going to add the Percolator object to the * world. */ void begin(){ /* Create the Coffee Percolator Object - Use new, because its a prototype, not a class !*/ ctr_object* percolatorObject = ctr_percolator_new(CtrStdObject, NULL); /* Set the prototype */ percolatorObject->link = CtrStdObject; /* Add the method 'new' so people can create their percolators */ ctr_internal_create_func(percolatorObject, ctr_build_string_from_cstring( "new" ), &ctr_percolator_new ); ctr_internal_create_func(percolatorObject, ctr_build_string_from_cstring( "brew" ), &ctr_percolator_brew ); ctr_internal_create_func(percolatorObject, ctr_build_string_from_cstring( "coffee:water:" ), &ctr_percolator_add_coffee_water ); /* Make the Percolator accessible to the world */ ctr_internal_object_add_property(CtrStdWorld, ctr_build_string_from_cstring( "Percolator" ), percolatorObject, CTR_CATEGORY_PUBLIC_PROPERTY); }
/** * File * * Represents a File object. * Creates a new file object based on the specified path. * * Usage: * * File new: '/example/path/to/file.txt'. */ ctr_object* ctr_file_new(ctr_object* myself, ctr_argument* argumentList) { ctr_object* s = ctr_object_make(myself, argumentList); ctr_object* pathObject; s->info.type = CTR_OBJECT_TYPE_OTMISC; s->link = myself; s->value.rvalue = malloc(sizeof(ctr_resource)); s->value.rvalue->type = 1; pathObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTSTRING); pathObject->info.type = CTR_OBJECT_TYPE_OTSTRING; pathObject->value.svalue = (ctr_string*) malloc(sizeof(ctr_string)); pathObject->value.svalue->value = (char*) malloc(sizeof(char) * argumentList->object->value.svalue->vlen); memcpy(pathObject->value.svalue->value, argumentList->object->value.svalue->value, argumentList->object->value.svalue->vlen); pathObject->value.svalue->vlen = argumentList->object->value.svalue->vlen; ctr_internal_object_add_property(s, ctr_build_string("path",4), pathObject, 0); return s; }
/** * OnMessageDoAction * * Makes the object respond to a new kind of message. * * Usage: * object on: 'greet' do {\ ... }. */ ctr_object* ctr_object_on_do(ctr_object* myself, ctr_argument* argumentList) { ctr_argument* nextArgument; ctr_object* methodBlock; ctr_object* methodName = argumentList->object; if (methodName->info.type != CTR_OBJECT_TYPE_OTSTRING) { CtrStdError = ctr_build_string_from_cstring("Expected on: argument to be of type string."); return myself; } nextArgument = argumentList->next; methodBlock = nextArgument->object; if (methodBlock->info.type != CTR_OBJECT_TYPE_OTBLOCK) { CtrStdError = ctr_build_string_from_cstring("Expected argument do: to be of type block."); return myself; } ctr_internal_object_add_property(myself, methodName, methodBlock, 1); return myself; }
/** * @internal * * Adds the Request object to the World. */ void begin(){ ctr_object* requestObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); requestObject->link = CtrStdObject; ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "get:" ), &ctr_request_get_string ); ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "getArray:" ), &ctr_request_get_array ); ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "cookie:" ), &ctr_request_cookie_string ); ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "cookieArray:" ), &ctr_request_cookie_array ); ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "post:" ), &ctr_request_post_string ); ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "file:" ), &ctr_request_file ); ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "postArray:" ), &ctr_request_post_array ); ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "serverOption:is:" ), &ctr_request_server_option ); ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "host:listen:pid:callback:" ), &ctr_request_serve ); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string_from_cstring( "Request" ), requestObject, 0); varlistGet = CGI_get_query(NULL); varlistPost = CGI_get_post(NULL,0); varlistCookie = CGI_get_cookie(NULL); }
/** * catch: [otherBlock] * * Associates an error clause to a block. * If an error (exception) occurs within the block this block will be * executed. * * Example: * * #Raise error on division by zero. * {\ * var z := 4 / 0. * } catch: { errorMessage | * Pen write: e, brk. * }, run. */ ctr_object* ctr_block_catch(ctr_object* myself, ctr_argument* argumentList) { ctr_object* catchBlock = argumentList->object; ctr_internal_object_delete_property(myself, ctr_build_string("catch",5),0); ctr_internal_object_add_property(myself, ctr_build_string("catch",5), catchBlock, 0); return myself; }
/** * WorldInitialize * * Populate the World of Citrine. */ void ctr_initialize_world() { int i; srand((unsigned)time(NULL)); for(i=0; i<16; i++) { CtrHashKey[i] = (rand() % 255); } ctr_first_object = NULL; CtrStdWorld = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_contexts[0] = CtrStdWorld; /* Object */ CtrStdObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdObject, ctr_build_string("new", 3), &ctr_object_make); ctr_internal_create_func(CtrStdObject, ctr_build_string("equals:", 7), &ctr_object_equals); ctr_internal_create_func(CtrStdObject, ctr_build_string("=",1), &ctr_object_equals); ctr_internal_create_func(CtrStdObject, ctr_build_string("on:do:", 6), &ctr_object_on_do); ctr_internal_create_func(CtrStdObject, ctr_build_string("respondTo:", 10), &ctr_object_respond); ctr_internal_create_func(CtrStdObject, ctr_build_string("respondTo:with:", 15), &ctr_object_respond); ctr_internal_create_func(CtrStdObject, ctr_build_string("respondTo:with:and:", 19), &ctr_object_respond); ctr_internal_create_func(CtrStdObject, ctr_build_string("type", 4), &ctr_object_type); ctr_internal_create_func(CtrStdObject, ctr_build_string("new", 3), &ctr_object_make); ctr_internal_create_func(CtrStdObject, ctr_build_string("isNil", 5), &ctr_object_is_nil); ctr_internal_create_func(CtrStdObject, ctr_build_string("myself", 6), &ctr_object_myself); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Object", 6), CtrStdObject, 0); CtrStdObject->link = NULL; /* Nil */ CtrStdNil = ctr_internal_create_object(CTR_OBJECT_TYPE_OTNIL); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Nil", 3), CtrStdNil, 0); ctr_internal_create_func(CtrStdNil, ctr_build_string("isNil", 5), &ctr_nil_is_nil); CtrStdNil->link = CtrStdObject; /* Boolean */ CtrStdBool = ctr_internal_create_object(CTR_OBJECT_TYPE_OTBOOL); ctr_internal_create_func(CtrStdBool, ctr_build_string("ifTrue:", 7), &ctr_bool_iftrue); ctr_internal_create_func(CtrStdBool, ctr_build_string("ifFalse:", 8), &ctr_bool_ifFalse); ctr_internal_create_func(CtrStdBool, ctr_build_string("break", 5), &ctr_bool_break); ctr_internal_create_func(CtrStdBool, ctr_build_string("continue", 8), &ctr_bool_continue); ctr_internal_create_func(CtrStdBool, ctr_build_string("else:", 5), &ctr_bool_ifFalse); ctr_internal_create_func(CtrStdBool, ctr_build_string("not", 3), &ctr_bool_not); ctr_internal_create_func(CtrStdBool, ctr_build_string("∧", 3), &ctr_bool_and); ctr_internal_create_func(CtrStdBool, ctr_build_string("nor:", 4), &ctr_bool_nor); ctr_internal_create_func(CtrStdBool, ctr_build_string("∨", 3), &ctr_bool_or); ctr_internal_create_func(CtrStdBool, ctr_build_string("xor:", 4), &ctr_bool_xor); ctr_internal_create_func(CtrStdBool, ctr_build_string("=",1),&ctr_bool_eq); ctr_internal_create_func(CtrStdBool, ctr_build_string("≠",3),&ctr_bool_neq); ctr_internal_create_func(CtrStdBool, ctr_build_string("toNumber", 8), &ctr_bool_to_number); ctr_internal_create_func(CtrStdBool, ctr_build_string("toString", 8), &ctr_bool_to_string); ctr_internal_create_func(CtrStdBool, ctr_build_string("flip", 4), &ctr_bool_flip); ctr_internal_create_func(CtrStdBool, ctr_build_string("either:or:", 10), &ctr_bool_either_or); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Boolean", 7), CtrStdBool, 0); CtrStdBool->link = CtrStdObject; /* Number */ CtrStdNumber = ctr_internal_create_object(CTR_OBJECT_TYPE_OTNUMBER); ctr_internal_create_func(CtrStdNumber, ctr_build_string("to:by:do:", 9), &ctr_number_to_by_do); ctr_internal_create_func(CtrStdNumber, ctr_build_string("+", 1), &ctr_number_add); ctr_internal_create_func(CtrStdNumber, ctr_build_string("inc:",4), &ctr_number_inc); ctr_internal_create_func(CtrStdNumber, ctr_build_string("-",1), &ctr_number_minus); ctr_internal_create_func(CtrStdNumber, ctr_build_string("dec:",4), &ctr_number_dec); ctr_internal_create_func(CtrStdNumber, ctr_build_string("*",1),&ctr_number_multiply); ctr_internal_create_func(CtrStdNumber, ctr_build_string("times:",6),&ctr_number_times); ctr_internal_create_func(CtrStdNumber, ctr_build_string("mul:",4),&ctr_number_mul); ctr_internal_create_func(CtrStdNumber, ctr_build_string("/",1), &ctr_number_divide); ctr_internal_create_func(CtrStdNumber, ctr_build_string("div:",4),&ctr_number_div); ctr_internal_create_func(CtrStdNumber, ctr_build_string(">",1),&ctr_number_higherThan); ctr_internal_create_func(CtrStdNumber, ctr_build_string("≥",3),&ctr_number_higherEqThan); ctr_internal_create_func(CtrStdNumber, ctr_build_string("<",1),&ctr_number_lowerThan); ctr_internal_create_func(CtrStdNumber, ctr_build_string("≤",3),&ctr_number_lowerEqThan); ctr_internal_create_func(CtrStdNumber, ctr_build_string("=",1),&ctr_number_eq); ctr_internal_create_func(CtrStdNumber, ctr_build_string("≠",3),&ctr_number_neq); ctr_internal_create_func(CtrStdNumber, ctr_build_string("%",1),&ctr_number_modulo); ctr_internal_create_func(CtrStdNumber, ctr_build_string("factorial",9),&ctr_number_factorial); ctr_internal_create_func(CtrStdNumber, ctr_build_string("floor",5),&ctr_number_floor); ctr_internal_create_func(CtrStdNumber, ctr_build_string("ceil",4),&ctr_number_ceil); ctr_internal_create_func(CtrStdNumber, ctr_build_string("round",5),&ctr_number_round); ctr_internal_create_func(CtrStdNumber, ctr_build_string("abs",3),&ctr_number_abs); ctr_internal_create_func(CtrStdNumber, ctr_build_string("sin",3),&ctr_number_sin); ctr_internal_create_func(CtrStdNumber, ctr_build_string("cos",3),&ctr_number_cos); ctr_internal_create_func(CtrStdNumber, ctr_build_string("exp",3),&ctr_number_exp); ctr_internal_create_func(CtrStdNumber, ctr_build_string("sqrt",4),&ctr_number_sqrt); ctr_internal_create_func(CtrStdNumber, ctr_build_string("tan",3),&ctr_number_tan); ctr_internal_create_func(CtrStdNumber, ctr_build_string("atan",4),&ctr_number_atan); ctr_internal_create_func(CtrStdNumber, ctr_build_string("log",3),&ctr_number_log); ctr_internal_create_func(CtrStdNumber, ctr_build_string("pow:",4),&ctr_number_pow); ctr_internal_create_func(CtrStdNumber, ctr_build_string("min:",4),&ctr_number_min); ctr_internal_create_func(CtrStdNumber, ctr_build_string("max:",4),&ctr_number_max); ctr_internal_create_func(CtrStdNumber, ctr_build_string("odd",3),&ctr_number_odd); ctr_internal_create_func(CtrStdNumber, ctr_build_string("even",4),&ctr_number_even); ctr_internal_create_func(CtrStdNumber, ctr_build_string("toString", 8), &ctr_number_to_string); ctr_internal_create_func(CtrStdNumber, ctr_build_string("toBoolean", 9), &ctr_number_to_boolean); ctr_internal_create_func(CtrStdNumber, ctr_build_string("between:and:",12),&ctr_number_between); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Number", 6), CtrStdNumber, 0); CtrStdNumber->link = CtrStdObject; /* String */ CtrStdString = ctr_internal_create_object(CTR_OBJECT_TYPE_OTSTRING); ctr_internal_create_func(CtrStdString, ctr_build_string("bytes", 5), &ctr_string_bytes); ctr_internal_create_func(CtrStdString, ctr_build_string("length", 6), &ctr_string_length); ctr_internal_create_func(CtrStdString, ctr_build_string("from:to:", 8), &ctr_string_fromto); ctr_internal_create_func(CtrStdString, ctr_build_string("from:length:", 12), &ctr_string_from_length); ctr_internal_create_func(CtrStdString, ctr_build_string("+", 1), &ctr_string_concat); ctr_internal_create_func(CtrStdString, ctr_build_string("=", 1), &ctr_string_eq); ctr_internal_create_func(CtrStdString, ctr_build_string("≠", 3), &ctr_string_neq); ctr_internal_create_func(CtrStdString, ctr_build_string("trim", 4), &ctr_string_trim); ctr_internal_create_func(CtrStdString, ctr_build_string("ltrim", 5), &ctr_string_ltrim); ctr_internal_create_func(CtrStdString, ctr_build_string("rtrim", 5), &ctr_string_rtrim); ctr_internal_create_func(CtrStdString, ctr_build_string("htmlEscape", 10), &ctr_string_html_escape); ctr_internal_create_func(CtrStdString, ctr_build_string("at:", 3), &ctr_string_at); ctr_internal_create_func(CtrStdString, ctr_build_string("byteAt:", 7), &ctr_string_byte_at); ctr_internal_create_func(CtrStdString, ctr_build_string("indexOf:", 8), &ctr_string_index_of); ctr_internal_create_func(CtrStdString, ctr_build_string("lastIndexOf:", 12), &ctr_string_last_index_of); ctr_internal_create_func(CtrStdString, ctr_build_string("replace:with:", 13), &ctr_string_replace_with); ctr_internal_create_func(CtrStdString, ctr_build_string("split:", 6), &ctr_string_split); ctr_internal_create_func(CtrStdString, ctr_build_string("up", 2), &ctr_string_to_upper); ctr_internal_create_func(CtrStdString, ctr_build_string("low", 3), &ctr_string_to_lower); ctr_internal_create_func(CtrStdString, ctr_build_string("skip:", 5), &ctr_string_skip); ctr_internal_create_func(CtrStdString, ctr_build_string("toNumber", 8), &ctr_string_to_number); ctr_internal_create_func(CtrStdString, ctr_build_string("toBoolean", 9), &ctr_string_to_boolean); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("String", 6), CtrStdString, 0); CtrStdString->link = CtrStdObject; /* Block */ CtrStdBlock = ctr_internal_create_object(CTR_OBJECT_TYPE_OTBLOCK); ctr_internal_create_func(CtrStdBlock, ctr_build_string("run", 3), &ctr_block_runIt); ctr_internal_create_func(CtrStdBlock, ctr_build_string("*", 1), &ctr_block_times); ctr_internal_create_func(CtrStdBlock, ctr_build_string("applyTo:", 8), &ctr_block_runIt); ctr_internal_create_func(CtrStdBlock, ctr_build_string("applyTo:and:", 12), &ctr_block_runIt); ctr_internal_create_func(CtrStdBlock, ctr_build_string("applyTo:and:and:", 16), &ctr_block_runIt); ctr_internal_create_func(CtrStdBlock, ctr_build_string("set:value:", 10), &ctr_block_set); ctr_internal_create_func(CtrStdBlock, ctr_build_string("error:", 6), &ctr_block_error); ctr_internal_create_func(CtrStdBlock, ctr_build_string("catch:", 6), &ctr_block_catch); ctr_internal_create_func(CtrStdBlock, ctr_build_string("whileTrue:", 10), &ctr_block_while_true); ctr_internal_create_func(CtrStdBlock, ctr_build_string("whileFalse:", 11), &ctr_block_while_false); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("CodeBlock", 9), CtrStdBlock, 0); CtrStdBlock->link = CtrStdObject; /* Array */ CtrStdArray = ctr_array_new(CtrStdObject, NULL); ctr_internal_create_func(CtrStdArray, ctr_build_string("new", 3), &ctr_array_new); ctr_internal_create_func(CtrStdArray, ctr_build_string("←", 3), &ctr_array_new_and_push); ctr_internal_create_func(CtrStdArray, ctr_build_string("push:", 5), &ctr_array_push); ctr_internal_create_func(CtrStdArray, ctr_build_string(";", 1), &ctr_array_push); ctr_internal_create_func(CtrStdArray, ctr_build_string("unshift:", 8), &ctr_array_unshift); ctr_internal_create_func(CtrStdArray, ctr_build_string("shift", 5), &ctr_array_shift); ctr_internal_create_func(CtrStdArray, ctr_build_string("count", 5), &ctr_array_count); ctr_internal_create_func(CtrStdArray, ctr_build_string("join:", 5), &ctr_array_join); ctr_internal_create_func(CtrStdArray, ctr_build_string("pop", 3), &ctr_array_pop); ctr_internal_create_func(CtrStdArray, ctr_build_string("at:", 3), &ctr_array_get); ctr_internal_create_func(CtrStdArray, ctr_build_string("@", 1), &ctr_array_get); ctr_internal_create_func(CtrStdArray, ctr_build_string("sort:", 5), &ctr_array_sort); ctr_internal_create_func(CtrStdArray, ctr_build_string("put:at:", 7), &ctr_array_put); ctr_internal_create_func(CtrStdArray, ctr_build_string("from:length:", 12), &ctr_array_from_to); ctr_internal_create_func(CtrStdArray, ctr_build_string("+", 1), &ctr_array_add); ctr_internal_create_func(CtrStdArray, ctr_build_string("map:", 4), &ctr_array_map); ctr_internal_create_func(CtrStdArray, ctr_build_string("each:", 5), &ctr_array_map); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Array", 5), CtrStdArray, 0); CtrStdArray->link = CtrStdObject; /* Map */ CtrStdMap = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdMap, ctr_build_string("new", 3), &ctr_map_new); ctr_internal_create_func(CtrStdMap, ctr_build_string("put:at:", 7), &ctr_map_put); ctr_internal_create_func(CtrStdMap, ctr_build_string("at:", 3), &ctr_map_get); ctr_internal_create_func(CtrStdMap, ctr_build_string("@", 1), &ctr_map_get); ctr_internal_create_func(CtrStdMap, ctr_build_string("count", 5), &ctr_map_count); ctr_internal_create_func(CtrStdMap, ctr_build_string("each:", 5), &ctr_map_each); ctr_internal_create_func(CtrStdMap, ctr_build_string("map:", 4), &ctr_map_each); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Map", 3), CtrStdMap, 0); CtrStdMap->link = CtrStdObject; /* Console */ CtrStdConsole = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdConsole, ctr_build_string("write:", 6), &ctr_console_write); ctr_internal_create_func(CtrStdConsole, ctr_build_string(">", 1), &ctr_console_write); ctr_internal_create_func(CtrStdConsole, ctr_build_string("brk", 3), &ctr_console_brk); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Pen", 3), CtrStdConsole, 0); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("?", 1), CtrStdConsole, 0); CtrStdConsole->link = CtrStdObject; /* File */ CtrStdFile = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdFile, ctr_build_string("new:", 4), &ctr_file_new); ctr_internal_create_func(CtrStdFile, ctr_build_string("path", 4), &ctr_file_path); ctr_internal_create_func(CtrStdFile, ctr_build_string("read", 4), &ctr_file_read); ctr_internal_create_func(CtrStdFile, ctr_build_string("write:", 6), &ctr_file_write); ctr_internal_create_func(CtrStdFile, ctr_build_string("append:", 7), &ctr_file_append); ctr_internal_create_func(CtrStdFile, ctr_build_string("exists", 6), &ctr_file_exists); ctr_internal_create_func(CtrStdFile, ctr_build_string("size", 4), &ctr_file_size); ctr_internal_create_func(CtrStdFile, ctr_build_string("delete", 6), &ctr_file_delete); ctr_internal_create_func(CtrStdFile, ctr_build_string("include", 7), &ctr_file_include); ctr_internal_create_func(CtrStdFile, ctr_build_string("go", 2), &ctr_file_include_ast); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("File", 4), CtrStdFile, 0); CtrStdFile->link = CtrStdObject; /* Command */ CtrStdCommand = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdCommand, ctr_build_string("argument:", 9), &ctr_command_argument); ctr_internal_create_func(CtrStdCommand, ctr_build_string("argCount", 8), &ctr_command_num_of_args); ctr_internal_create_func(CtrStdCommand, ctr_build_string("env:", 4), &ctr_command_get_env); ctr_internal_create_func(CtrStdCommand, ctr_build_string("env:val:", 8), &ctr_command_set_env); ctr_internal_create_func(CtrStdCommand, ctr_build_string("??", 2), &ctr_command_question); ctr_internal_create_func(CtrStdCommand, ctr_build_string("exit", 4), &ctr_command_exit); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Command", 7), CtrStdCommand, 0); CtrStdCommand->link = CtrStdObject; /* Clock */ CtrStdClock = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdClock, ctr_build_string("wait:", 5), &ctr_clock_wait); ctr_internal_create_func(CtrStdClock, ctr_build_string("time", 4), &ctr_clock_time); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Clock", 5), CtrStdClock, 0); CtrStdClock->link = CtrStdObject; /* Dice */ CtrStdDice = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdDice, ctr_build_string("roll", 4), &ctr_dice_throw); ctr_internal_create_func(CtrStdDice, ctr_build_string("rollWithSides:", 14), &ctr_dice_sides); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Dice", 4), CtrStdDice, 0); CtrStdDice->link = CtrStdObject; /* Shell */ CtrStdShell = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdShell, ctr_build_string("call:", 5), &ctr_shell_call); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Shell", 5), CtrStdShell, 0); CtrStdShell->link = CtrStdObject; /* Broom */ CtrStdGC = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_internal_create_func(CtrStdGC, ctr_build_string("sweep", 5), &ctr_gc_collect); ctr_internal_create_func(CtrStdGC, ctr_build_string("dust", 4), &ctr_gc_dust); ctr_internal_create_func(CtrStdGC, ctr_build_string("objectCount", 11), &ctr_gc_object_count); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Broom", 5), CtrStdGC, 0); CtrStdGC->link = CtrStdObject; /* Other objects */ CtrStdBreak = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); CtrStdContinue = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); }
/** * InternalFunctionCreate * * Create a function and add this to the object as a method. */ void ctr_internal_create_func(ctr_object* o, ctr_object* key, ctr_object* (*func)( ctr_object*, ctr_argument* ) ) { ctr_object* methodObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTNATFUNC); methodObject->value.fvalue = func; ctr_internal_object_add_property(o, key, methodObject, 1); }
/** * InternalObjectSetProperty * * Sets a property on an object. */ void ctr_internal_object_set_property(ctr_object* owner, ctr_object* key, ctr_object* value, int is_method) { ctr_internal_object_delete_property(owner, key, is_method); ctr_internal_object_add_property(owner, key, value, is_method); }