Skip to content

Commit

Permalink
#341 Various bugfixes
Browse files Browse the repository at this point in the history
String deserializer collections, package installer, option to disable
JSON prefixes, prefix parameter names in C binding when they clash with
a keyword, copy corto::lang includes to right folder, add string to
octet conversion, fix issue in resolver w.r.t. capitals/loading
packages.
  • Loading branch information
SanderMertens committed Sep 21, 2015
1 parent c7bfa84 commit 62fbd00
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 51 deletions.
22 changes: 22 additions & 0 deletions build/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@

clobber_count = 1
clean_count = 1
collect_count = 1
install_count = 1

task :clean do
# Recursively call clean, prevent infinite recursion
Expand Down Expand Up @@ -98,6 +100,26 @@
end
end

task :collect do
# Recursively call collect, prevent infinite recursion
if collect_count == 1 then
collect_count += 1
Rake::Task["collect"].reenable
require "#{ENV['CORTO_BUILD']}/component"
Rake::Task["collect"].execute
end
end

task :install do
# Recursively call collect, prevent infinite recursion
if install_count == 1 then
install_count += 1
Rake::Task["install"].reenable
require "#{ENV['CORTO_BUILD']}/component"
Rake::Task["install"].execute
end
end

if File.exists? "test" then
COMPONENTS ||=[] << "test"
end
Expand Down
1 change: 1 addition & 0 deletions components/json/include/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef struct cx_json_ser_t {
cx_bool serializeValue;
cx_bool serializeScope;
cx_bool alwaysIncludeHeaders;
cx_bool serializePrefix;
} cx_json_ser_t;

struct cx_serializer_s cx_json_ser(cx_modifier access, cx_operatorKind accessKind, cx_serializerTraceKind trace);
Expand Down
33 changes: 22 additions & 11 deletions components/json/src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,25 @@ static cx_int16 serializePrimitive(cx_serializer s, cx_value *v, void *userData)

switch (cx_primitive(type)->kind) {
case CX_BINARY:
result = serializeBinary(v, &valueString);
result = serializeBinary(v, &valueString, data);
break;
case CX_BITMASK:
result = serializeBitmask(v, &valueString);
result = serializeBitmask(v, &valueString, data);
break;
case CX_BOOLEAN:
result = serializeBoolean(v, &valueString);
result = serializeBoolean(v, &valueString, data);
break;
case CX_ENUM:
result = serializeEnum(v, &valueString);
result = serializeEnum(v, &valueString, data);
break;
case CX_CHARACTER:
case CX_TEXT:
result = serializeText(v, &valueString);
result = serializeText(v, &valueString, data);
break;
case CX_UINTEGER:
case CX_INTEGER:
case CX_FLOAT:
result = serializeNumber(v, &valueString);
result = serializeNumber(v, &valueString, data);
break;
}
if (result) {
Expand Down Expand Up @@ -134,10 +134,18 @@ static cx_int16 serializeReference(cx_serializer s, cx_value *v, void *userData)
/* Escape value */
cx_string escapedValue = cx_alloc((length = stresc(NULL, 0, id)) + 1);
stresc(escapedValue, length + 1, id);
if (!cx_ser_appendstr(data, "\"@R %s\"", escapedValue)) {
cx_dealloc(escapedValue);
goto finished;
if (data->serializePrefix) {
if (!cx_ser_appendstr(data, "\"@R %s\"", escapedValue)) {
cx_dealloc(escapedValue);
goto finished;
}
} else {
if (!cx_ser_appendstr(data, "\"%s\"", escapedValue)) {
cx_dealloc(escapedValue);
goto finished;
}
}

cx_dealloc(escapedValue);
} else {
cx_ser_appendstr(data, "\"anonymous\"");
Expand Down Expand Up @@ -221,8 +229,11 @@ static cx_int16 serializeComplex(cx_serializer s, cx_value* v, void* userData) {
static cx_int16 serializeBase(cx_serializer s, cx_value* v, void* userData) {
cx_json_ser_t *data = userData;
cx_id id;
if (!cx_ser_appendstr(data, "\"@%s\":", cx_fullname(cx_valueType(v), id))) {
goto finished;

if (data->serializePrefix) {
if (!cx_ser_appendstr(data, "\"@%s\":", cx_fullname(cx_valueType(v), id))) {
goto finished;
}
}
if (cx_serializeValue(s, v, userData)) {
goto error;
Expand Down
45 changes: 28 additions & 17 deletions components/json/src/json_primitives.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,57 @@

#include "json_primitives.h"
#include "parson.h"

cx_int16 serializeNumber(cx_value *value, cx_string *out, cx_json_ser_t *data) {
CX_UNUSED(data);

cx_int16 serializeNumber(cx_value *value, cx_string *out) {
cx_type t = cx_valueType(value);

/* JSON doesn't support hex notation, so convert to integer */
if (cx_primitive(t)->kind == CX_BINARY) {
t = cx_type(cx_uint64_o);
}

cx_void *v = cx_valueValue(value);
cx_int16 result = cx_convert(cx_primitive(t), v, cx_primitive(cx_string_o), out);
return result;
}

static cx_int16 serializeNumericWithPrefix(cx_value *value, cx_string *out, const char *prefix) {
static cx_int16 serializeNumericWithPrefix(cx_value *value, cx_string *out, const char *prefix, cx_json_ser_t *data) {
cx_string raw;
cx_void *v = cx_valueValue(value);
if (cx_convert(cx_primitive(cx_valueType(value)), v, cx_primitive(cx_string_o), &raw)) {
goto error;
}
int length = snprintf(NULL, 0, "\"%s %s\"", prefix, raw);
if (length < 0) {
goto error;
}
*out = cx_alloc(length + 1);
if (sprintf(*out, "\"%s %s\"", prefix, raw) < 0) {
goto error;

if (data->serializePrefix) {
cx_asprintf(out, "\"%s %s\"", prefix, raw);
} else {
cx_asprintf(out, "\"%s\"", raw);
}

cx_dealloc(raw);

return 0;
error:
cx_dealloc(raw);
return -1;
}

cx_int16 serializeBinary(cx_value *value, cx_string *out) {
return serializeNumericWithPrefix(value, out, "@B");
cx_int16 serializeBinary(cx_value *value, cx_string *out, cx_json_ser_t *data) {
return serializeNumber(value, out, data);
}

cx_int16 serializeBitmask(cx_value *value, cx_string *out) {
return serializeNumericWithPrefix(value, out, "@M");
cx_int16 serializeBitmask(cx_value *value, cx_string *out, cx_json_ser_t *data) {
return serializeNumericWithPrefix(value, out, "@M", data);
}

cx_int16 serializeEnum(cx_value *value, cx_string *out) {
return serializeNumericWithPrefix(value, out, "@E");
cx_int16 serializeEnum(cx_value *value, cx_string *out, cx_json_ser_t *data) {
return serializeNumericWithPrefix(value, out, "@E", data);
}

cx_int16 serializeBoolean(cx_value *value, cx_string *out) {
cx_int16 serializeBoolean(cx_value *value, cx_string *out, cx_json_ser_t *data) {
CX_UNUSED(data);
cx_bool b = *(cx_bool *)cx_valueValue(value);
if (b) {
*out = cx_alloc(sizeof("true"));
Expand All @@ -53,7 +63,8 @@ cx_int16 serializeBoolean(cx_value *value, cx_string *out) {
return 0;
}

cx_int16 serializeText(cx_value *value, cx_string *out) {
cx_int16 serializeText(cx_value *value, cx_string *out, cx_json_ser_t *data) {
CX_UNUSED(data);
cx_type type = cx_valueType(value);
cx_void *v = cx_valueValue(value);
cx_primitiveKind kind = cx_primitive(type)->kind;
Expand Down
15 changes: 7 additions & 8 deletions components/json/src/json_primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
#define json_primitives_H

#include "parson.h"

#include "corto.h"
#include "json.h"

#ifdef __cplusplus
extern "C" {
#endif

cx_int16 serializeNumber(cx_value *value, cx_string *out);
cx_int16 serializeBinary(cx_value *value, cx_string *out);
cx_int16 serializeBitmask(cx_value *value, cx_string *out);
cx_int16 serializeEnum(cx_value *value, cx_string *out);
cx_int16 serializeBoolean(cx_value *value, cx_string *out);
cx_int16 serializeText(cx_value *value, cx_string *out);
cx_int16 serializeNumber(cx_value *value, cx_string *out, cx_json_ser_t *data);
cx_int16 serializeBinary(cx_value *value, cx_string *out, cx_json_ser_t *data);
cx_int16 serializeBitmask(cx_value *value, cx_string *out, cx_json_ser_t *data);
cx_int16 serializeEnum(cx_value *value, cx_string *out, cx_json_ser_t *data);
cx_int16 serializeBoolean(cx_value *value, cx_string *out, cx_json_ser_t *data);
cx_int16 serializeText(cx_value *value, cx_string *out, cx_json_ser_t *data);

cx_bool json_deserPrimitive(void* p, cx_type t, JSON_Value* v);

Expand Down
5 changes: 4 additions & 1 deletion libraries/c_common/src/c_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ cx_char* c_topath(cx_object o, cx_id id, cx_char separator) {
}

cx_string c_paramName(cx_string name, cx_string buffer) {

if (*name == '$') {
if (!strcmp(name, "$__line")) {
strcpy(buffer, name + 1);
Expand All @@ -414,7 +415,9 @@ cx_string c_paramName(cx_string name, cx_string buffer) {
sprintf(buffer, "str_%s", name + 1);
}
} else {
strcpy(buffer, name);
cx_id id;
corto_genId(name, id);
strcpy(buffer, id);
}
return buffer;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/corto/lang/include/cx_string_deser.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ typedef struct cx_string_deser_t {
cx_ll index;
cx_void* ptr;
cx_ll anonymousObjects;
void* (*allocValue)(void *ptr, void *udata);
void *allocUdata;
}cx_string_deser_t;

/* Deserialize string */
Expand Down
2 changes: 1 addition & 1 deletion packages/corto/lang/rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ end

task :collect do
verbose(false)
includePath = "#{ENV['HOME']}/.corto/pack/include/corto/#{VERSION}"
includePath = "#{ENV['HOME']}/.corto/pack/include/corto/#{VERSION}/packages/corto/lang"
sh "mkdir -p #{includePath}"
sh "cp include/* #{includePath}/"

Expand Down
11 changes: 11 additions & 0 deletions packages/corto/lang/src/cx_convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ CX_DECL_TRANSFORM(boolean, string) {
return 0;
}

/* string to octet */
CX_DECL_TRANSFORM(string, octet) {
CX_UNUSED(toType);
CX_UNUSED(fromType);
*(cx_octet*)to = (cx_int64)strtol(*(cx_string*)from, NULL, 16);
return 0;
}

/* string to boolean */
CX_DECL_TRANSFORM(string, boolean) {
cx_string str;
Expand Down Expand Up @@ -488,6 +496,9 @@ void cx_convertInit(void) {
CX_CONVERT_INIT_NUM(CX_UINTEGER, CX_WIDTH_64, CX_BITMASK, CX_WIDTH_32, int64, enum);
CX_CONVERT_INIT_NUM(CX_BITMASK, CX_WIDTH_32, CX_BOOLEAN, CX_WIDTH_8, int32, bool);

/* string to octet */
CX_CONVERT_INIT_NUM(CX_TEXT, CX_WIDTH_WORD, CX_BINARY, CX_WIDTH_8, string, octet);

/* string to character */
CX_CONVERT_INIT_NUM(CX_TEXT, CX_WIDTH_WORD, CX_CHARACTER, CX_WIDTH_8, string, char8);

Expand Down
23 changes: 17 additions & 6 deletions packages/corto/lang/src/cx_resolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ cx_object cx_lookupLowercase(cx_object o, cx_string name);
cx_object cx_resolve(cx_object _scope, cx_string str) {
cx_object scope, _scope_start, o, lookup;
const char* ptr;
char* bptr;
char *bptr, *bptrLc;
cx_id buffer;
cx_id bufferLc;
cx_char ch;
cx_bool overload;
cx_bool fullyQualified = FALSE;
Expand Down Expand Up @@ -102,27 +103,33 @@ cx_object cx_resolve(cx_object _scope, cx_string str) {
}

bptr = buffer;
bptrLc = bufferLc;
while ((ch = *ptr) && (ch != ':') && (ch != '{') && (ch != '/')) {
*bptr = tolower(ch);
*bptr = ch;
*bptrLc = tolower(ch);
bptr++;
bptrLc++;
ptr++;
if (ch == '(') {
overload = TRUE;
while ((ch = *ptr) && (ch != ')')) {
*bptr = tolower(ch);
*bptrLc = tolower(ch);
*bptr = ch;
bptrLc++;
bptr++;
ptr++;
}
}
}
*bptr = '\0';
*bptrLc = '\0';

if (cx_scopeSize(o)) {
if (!overload) {
cx_object prev = o;
int i;
for (i = 0; i < 2; i++) {
o = cx_lookupLowercase(o, buffer);
o = cx_lookupLowercase(o, bufferLc);
if (lookup) {
cx_release(lookup); /* Free reference */
}
Expand All @@ -131,7 +138,11 @@ cx_object cx_resolve(cx_object _scope, cx_string str) {
if (!o) {
if (!i && (prev != corto_lang_o) && cx_instanceof(cx_type(cx_package_o), prev)) {
cx_id load, id;
sprintf(load, "%s/%s", cx_fullname(prev, id), buffer);
if (prev != root_o) {
sprintf(load, "%s/%s", cx_fullname(prev, id), buffer);
} else {
sprintf(load, "/%s", buffer);
}
cx_load(load, 0, NULL);
o = prev;
} else {
Expand All @@ -147,7 +158,7 @@ cx_object cx_resolve(cx_object _scope, cx_string str) {
}
} else {
/* If argumentlist is provided, look for closest match */
o = cx_lookupFunction(o, buffer, NULL);
o = cx_lookupFunction(o, bufferLc, NULL);
if (lookup) {
cx_release(lookup);
}
Expand Down
Loading

0 comments on commit 62fbd00

Please sign in to comment.