-
Notifications
You must be signed in to change notification settings - Fork 238
Http support #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Http support #281
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2057,6 +2057,18 @@ gravity_list_t *gravity_list_new (gravity_vm *vm, uint32_t n) { | |
return list; | ||
} | ||
|
||
gravity_value_t gravity_list_to_value(gravity_vm *vm, gravity_list_t *list) { | ||
gravity_list_t *copy = mem_alloc(NULL, sizeof(gravity_list_t)); | ||
copy->array = list->array; | ||
|
||
gravity_value_t value; | ||
value.isa = gravity_class_list; | ||
value.p = (gravity_object_t *)copy; | ||
|
||
if (vm) gravity_vm_transfer(vm, (gravity_object_t*) copy); | ||
return value; | ||
} | ||
|
||
gravity_list_t *gravity_list_from_array (gravity_vm *vm, uint32_t n, gravity_value_t *p) { | ||
gravity_list_t *list = (gravity_list_t *)mem_alloc(NULL, sizeof(gravity_list_t)); | ||
|
||
|
@@ -2227,7 +2239,11 @@ static void gravity_map_stringify_iterator(gravity_hash_t *hashtable, gravity_va | |
// INT | ||
if (VALUE_ISA_INT(v)) { | ||
substr = mem_alloc(NULL, strlen(key_string) + sizeof(int64_t) + 10 * sizeof(char)); | ||
#ifdef __APPLE__ | ||
sprintf(substr, "%s\"%s\": %lld", delimiter, key_string, (int64_t)v.n); | ||
#else | ||
sprintf(substr, "%s\"%s\": %ld", delimiter, key_string, (int64_t)v.n); | ||
#endif | ||
gravity_string_concat_cstring(NULL, map_string, substr); | ||
return; | ||
} | ||
|
@@ -2259,20 +2275,29 @@ static void gravity_map_stringify_iterator(gravity_hash_t *hashtable, gravity_va | |
return; | ||
} | ||
|
||
if (VALUE_ISA_LIST(v)) { | ||
gravity_list_t *value_list = VALUE_AS_LIST(v); | ||
char *value_list_string = VALUE_AS_CSTRING(convert_value2string(NULL, v)); | ||
substr = mem_alloc(NULL, strlen(key_string) + strlen(value_list_string) + 10 * sizeof(char)); | ||
sprintf(substr, "%s%s: %s", delimiter, key_string, value_list_string); | ||
gravity_string_concat_cstring(NULL, map_string, substr); | ||
return; | ||
} | ||
|
||
// should never reach this point | ||
assert(0); | ||
} | ||
|
||
char *gravity_map_to_string(gravity_vm *vm, gravity_map_t *map) { | ||
gravity_string_t *map_string = gravity_string_new(NULL, "", 1000, 0); | ||
gravity_string_t *map_string = gravity_string_new(NULL, "", 0, 0); | ||
gravity_hash_iterate(map->hash, gravity_map_stringify_iterator, map_string); | ||
gravity_string_concat_cstring(NULL, map_string, "}"); | ||
return map_string->s; | ||
} | ||
|
||
gravity_value_t gravity_map_to_value (gravity_vm *vm, gravity_map_t *map) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure to understand the use case of this... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marcobambini Wouldn't you say there's a need for _to_value functions for all types? E.g. if I want to create a map with a key associated to an array, I'd have no way of doing that without doing something like: gravity_map_t *response = gravity_map_new(vm, 32);
gravity_list_t *headers = gravity_list_new(vm, 32);
gravity_map_insert(vm, response,
gravity_string_to_value("Headers"),
gravity_list_to_value(headers)); If there's a way for already doing something like this, let me know. I'm cleaning up the implementation at the moment, currently trying to get something like this going There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use the map_to_value method in a similar fashion in src/optionals/gravity_http.c There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I guess VALUE_FROM_OBJECT might be helpful in this case 😎 |
||
// allocate the size of a string | ||
gravity_hash_t *h = gravity_hash_create_from(map->hash); | ||
gravity_hash_t *h = gravity_hash_dup(map->hash); | ||
gravity_map_t *m = mem_alloc(vm, gravity_map_size(vm, map)); | ||
m->hash = h; | ||
|
||
|
@@ -2360,12 +2385,12 @@ inline gravity_string_t *gravity_string_new (gravity_vm *vm, char *s, uint32_t l | |
} | ||
|
||
inline void gravity_string_concat_cstring (gravity_vm *vm, gravity_string_t *obj, char *cstring) { | ||
uint32_t len = (uint32_t)(strlen(obj->s) + strlen(cstring)); | ||
uint32_t len = (uint32_t)(obj->len + strlen(cstring)); | ||
uint32_t alloc = MAXNUM(len+1, DEFAULT_MINSTRING_SIZE); | ||
obj->s = mem_realloc(NULL, obj->s, alloc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of always perform a men_realloc you should first check if (obj->alloc - obj->len) is big enough |
||
memcpy(obj->s + obj->len, cstring, 1 + strlen(cstring)); | ||
obj->len = len; | ||
obj->alloc = alloc; | ||
strcat(obj->s, cstring); | ||
} | ||
|
||
inline void gravity_string_set (gravity_string_t *obj, char *s, uint32_t len) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not to reuse
convert_map2string
defined in gravity_core.c?