Ejemplo n.º 1
0
/**
 * CTRAssignValueObjectLocal
 *
 * Assigns a value to a local of an object. 
 */
ctr_object* ctr_assign_value_to_local(ctr_object* key, ctr_object* o) {
	ctr_object* object;
	ctr_object* context;
	if (CtrStdError) return CtrStdNil;
	context = ctr_contexts[ctr_context_id];
	key->info.sticky = 0;
	if (o->info.type == CTR_OBJECT_TYPE_OTOBJECT || o->info.type == CTR_OBJECT_TYPE_OTMISC || o->info.type == CTR_OBJECT_TYPE_OTARRAY || o->info.type == CTR_OBJECT_TYPE_OTNIL) {
		ctr_internal_object_set_property(context, key, o, 0);
	} else {
		object = ctr_internal_create_object(o->info.type);
		object->properties = o->properties;
		object->methods = o->methods;
		object->link = o->link;
		object->info.sticky = 0;
		ctr_internal_object_set_property(context, key, object, 0);
	}
     /* depending on type, copy specific value */
    if (o->info.type == CTR_OBJECT_TYPE_OTBOOL) {
		object->value.bvalue = o->value.bvalue;
	 } else if (o->info.type == CTR_OBJECT_TYPE_OTNUMBER) {
		object->value.nvalue = o->value.nvalue;
	 } else if (o->info.type == CTR_OBJECT_TYPE_OTSTRING) {
		object->value.svalue = malloc(sizeof(ctr_string));
		object->value.svalue->value = malloc(sizeof(char)*o->value.svalue->vlen);
		memcpy(object->value.svalue->value, o->value.svalue->value,o->value.svalue->vlen);
		object->value.svalue->vlen = o->value.svalue->vlen;
	 } else if (o->info.type == CTR_OBJECT_TYPE_OTBLOCK) {
		object->value.block = o->value.block;
	 }
	return object;
}
Ejemplo n.º 2
0
/**
 * [Percolator] coffee: [Number] water: [Number]
 * 
 * Adds coffee and water to the perculator.
 * 
 * myPercolator := Percolator new.
 * cupOfCoffee  := myPercolator coffee: 1 water: 2, brew.
 * 
 */
ctr_object* ctr_percolator_add_coffee_water(ctr_object* myself, ctr_argument* argumentList) {
	
	ctr_internal_object_set_property(
		myself, 
		ctr_build_string_from_cstring( "coffee" ),
		ctr_internal_cast2number(argumentList->object),
		CTR_CATEGORY_PRIVATE_PROPERTY
	);
	
	ctr_internal_object_set_property(
		myself, 
		ctr_build_string_from_cstring( "water" ),
		ctr_internal_cast2number(argumentList->next->object),
		CTR_CATEGORY_PRIVATE_PROPERTY
	);
	
	return myself;	
}
Ejemplo n.º 3
0
/**
 * Request serverOption: [string] is: [string].
 * 
 * Sets a server option, available server option for SCGI server include:
 * 
 * - minidle, minimum number of idle processes
 * - maxidle, maximum number of idle processes
 * - maxproc, maximum number of processes
 * - maxreq,  maximum number of concurrent requests to allow
 * 
 * Usage:
 * 
 * Request
 *  serverOption: 'minidle' is: 8,
 *  serverOption: 'maxreq'  is: 100.
 * 
 * This sets the minimum number of idle processes to 8 and the
 * maximum number of concurrent requests to 100, you can chain
 * multiple options using a comma (,).
 */
ctr_object* ctr_request_server_option(ctr_object* myself, ctr_argument* argumentList) {
	ctr_internal_object_set_property(
		myself,
		ctr_internal_cast2string(argumentList->object),
		ctr_internal_cast2string(argumentList->next->object),
		CTR_CATEGORY_PRIVATE_PROPERTY
	);
	return myself;
}
Ejemplo n.º 4
0
/**
 * Percolator new
 * 
 * Creates a new instance of the percolator object.
 * 
 * Usage:
 * 
 * myPercolator := Percolator new.
 * cupOfCoffee  := myPercolator coffee: 1 water: 2, brew.
 * 
 */
ctr_object* ctr_percolator_new(ctr_object* myself, ctr_argument* argumentList) {
	
	ctr_object* percolatorInstance = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT);
	percolatorInstance->link = myself;
	ctr_internal_object_set_property(
		percolatorInstance, 
		ctr_build_string_from_cstring( "coffee" ),
		ctr_build_number_from_float(0),
		CTR_CATEGORY_PRIVATE_PROPERTY
	);
	ctr_internal_object_set_property(
		percolatorInstance, 
		ctr_build_string_from_cstring( "water" ),
		ctr_build_number_from_float(0),
		CTR_CATEGORY_PRIVATE_PROPERTY
	);
	
	return percolatorInstance;	
}
Ejemplo n.º 5
0
/**
 * CTRSetBasic
 *
 * Sets a proeprty in an object (context).
 */
void ctr_set(ctr_object* key, ctr_object* object) {
	int i = ctr_context_id;
	ctr_object* context;
	ctr_object* foundObject = NULL;
	if (ctr_contexts[ctr_context_id] == CtrStdWorld) {
		ctr_internal_object_set_property(ctr_contexts[ctr_context_id], key, object, 0);
		return;
	}
	while((i>-1 && foundObject == NULL)) {
		context = ctr_contexts[i];
		foundObject = ctr_internal_object_find_property(context, key, 0);
		if (foundObject) break;
		i--;
	}
	if (!foundObject) {
		printf("Error, cannot assign, key not found: %s, forgot to use var ?\n", key->value.svalue->value); exit(1);
	}
	ctr_internal_object_set_property(context, key, object, 0);
}
Ejemplo n.º 6
0
ctr_object* ctr_block_set(ctr_object* myself, ctr_argument* argumentList) {
	ctr_object* key = ctr_internal_cast2string(argumentList->object);
	ctr_object* value = argumentList->next->object;
	ctr_internal_object_set_property(myself, key, value, 0);
	return myself; 
}
Ejemplo n.º 7
0
/**
 * [Percolator] brew
 * 
 * Tries to brew some delicious coffee.
 * To brew one cup of coffee the Percolator needs two cups of water
 * and one spoon of coffee grounds.
 *
 * Read the inline comments to learn how to extend Citrine!
 */
ctr_object* ctr_percolator_brew(ctr_object* myself, ctr_argument* argumentList) {
	
	/**
	 * Fetch the coffee property, note that the key is a Citrine String object.
	 */
	ctr_object* coffee = ctr_internal_object_find_property(
		myself,                                    /* owner object */
		ctr_build_string_from_cstring( "coffee" ), /* key object */
		CTR_CATEGORY_PRIVATE_PROPERTY
	);
	
	/**
	 * Fetch the water property.
	 */
	ctr_object* water = ctr_internal_object_find_property(
		myself,
		ctr_build_string_from_cstring( "water" ),
		CTR_CATEGORY_PRIVATE_PROPERTY
	);
	
	/**
	 * To access a value:
	 * 
	 * - numeric values reside in value.nvalue
	 * - boolean values reside in value.bvalue
	 * - array   values reside in value.avalue (see ctr_collection)
	 * - string  values reside in value.svalue (see ctr_string)
	 * - block   values reside in value.block  (see ctr_tnode)
	 * - native functions      in value.fvalue
	 */
	if (coffee->value.nvalue < 1) {
		return ctr_build_string_from_cstring( "No more coffee." );
	}
	
	if (water->value.nvalue < 2) {
		return ctr_build_string_from_cstring( "No more water." );
	}
	
	coffee->value.nvalue -= 1;
	water->value.nvalue -= 2;
	
	/**
	 * To set a property of an object, use
	 * the set_property function.
	 */
	ctr_internal_object_set_property(
		myself, 
		ctr_build_string_from_cstring( "coffee" ),
		coffee,
		CTR_CATEGORY_PRIVATE_PROPERTY
	);
	
	ctr_internal_object_set_property(
		myself, 
		ctr_build_string_from_cstring( "water" ),
		water,
		CTR_CATEGORY_PRIVATE_PROPERTY
	);
	
	return ctr_build_string_from_cstring( "Coffee!" );
}