Пример #1
0
/* $end */
cx_int16 cx_function_init(cx_function _this) {
/* $begin(::cortex::lang::function::init) */
    cx_functionLookup_t walkData;
    cx_ll scope;

    scope = cx_scopeClaim(cx_parentof(_this));

    walkData.f = _this;
    walkData.error = FALSE;
    cx_signatureName(cx_nameof(_this), walkData.name);
    cx_llWalk(scope, cx_functionLookupWalk, &walkData);
    if (walkData.error) {
        goto error;
    }

    cx_scopeRelease(scope);

    /* Parse arguments */
    if (cx_function_parseArguments(_this)) {
        goto error;
    }

    return 0;
error:
    _this->parameters.length = 0;
    return -1;
/* $end */
}
Пример #2
0
/* Write all parents to connection so that a reply is self-containing */
static int web_printParents(struct mg_connection *conn, cx_object o) {
    int result = 0;
    struct cx_serializer_s serializer = cx_json_ser(CX_PRIVATE, CX_NOT, CX_SERIALIZER_TRACE_NEVER);
    
    if (o) {
        cx_json_ser_t jsonData = {NULL, NULL, 0, 0, 0, TRUE, FALSE, FALSE, TRUE};
        if (cx_parentof(o)) {
            result += web_printParents(conn, cx_parentof(o));
        }

        if (result) {
            mg_printf_data(conn, ",");
        }

        cx_serialize(&serializer, o, &jsonData);
        mg_printf_data(conn, "%s\n", jsonData.buffer);
        cx_dealloc(jsonData.buffer);

        result++;
    }

    return result;
}
Пример #3
0
Fast_Call Fast_createCallFromExpr(Fast_Expression f, Fast_Expression arguments) {
    Fast_Call result = NULL;
    Fast_Expression instance = NULL;
    cx_id name;
    cx_object scope = yparser()->scope;
    Fast_CallBuilder builder;

    if (Fast_Node(f)->kind == Fast_StorageExpr) {
        switch(Fast_Storage(f)->kind) {

        case Fast_ObjectStorage: {
            cx_object o = Fast_Object(f)->value;
            cx_signatureName(cx_nameof(o), name);
            scope = cx_parentof(o);
            break;
        }

        case Fast_LocalStorage:
            strcpy(name, Fast_Local(f)->name);
            break;

        case Fast_MemberStorage:
            instance = Fast_Member(f)->lvalue;
            strcpy(name, Fast_String(Fast_Member(f)->rvalue)->value);
            break;

        case Fast_ElementStorage:
            result = Fast_Call(Fast_DelegateCall__create(NULL, arguments, f));
            break;

        default:
            Fast_Parser_error(yparser(), "'%s' expression is not callable",
                cx_nameof(cx_enum_constant(Fast_storageKind_o, Fast_Storage(f)->kind)));
            goto error;
        }
    }

    if (!result) {
        Fast_CallBuilder__init(&builder, name, arguments, instance, scope, yparser()->block);
        result = Fast_CallBuilder_build(&builder);
        Fast_CallBuilder__deinit(&builder);
    }

    return result;
error:
    return NULL;
}
Пример #4
0
/* ::cortex::lang::type::parentof() */
cx_object cx_type_parentof(cx_any _this) {
/* $begin(::cortex::lang::type::parentof) */
    cx_string result = NULL;

   if (cx_checkAttr(_this.value, CX_ATTR_SCOPED)) {
       result = cx_parentof(_this.value);
   } else {
       cx_id id;
       cx_error("cannot get parent from non-scoped object of type '%s'", cx_fullname(cx_typeof(_this.value), id));
   }
    
   if (result) {
       cx_keep(result);
   }

   return result;
/* $end */
}
Пример #5
0
/* ::cortex::lang::constant::init() */
cx_int16 cx_constant_init(cx_constant *_this) {
/* $begin(::cortex::lang::constant::init) */
    cx_object parent;

    parent = cx_parentof(_this);

    /* Parent must be an enum */
    if (cx_typeof(parent) == cx_type(cx_enum_o)) {
        cx__enum_bindConstant(parent, _this);
    } else if (cx_typeof(parent) == cx_type(cx_bitmask_o)) {
        cx__bitmask_bindConstant(parent, _this);
    } else {
        cx_id id;
        cx_error("::constant::init: parent of constant '%s' is not an enum.", cx_fullname(_this, id));
        goto error;
    }

    return 0;
error:
    return -1;/* $end */
}
Пример #6
0
/* $header(::cortex::lang::function::init) */
static cx_int16 cx_function_parseArguments(cx_function object) {
    object->parameters = cx_function_stringToParameterSeq(cx_nameof(object), cx_parentof(object));
    return object->parameters.length == (cx_uint32)-1;
}
Пример #7
0
/* Serve a Cortex object */
static int web_serveObject(struct mg_connection *conn) {
    cx_object o = NULL;

    /* Resolve object based on URI */
    o = cx_resolve(NULL, (cx_string)conn->uri + 2);

    if (!o) {
        mg_send_status(conn, 404);
        mg_printf_data(conn, "404: resource '%s' not found\n", conn->uri);
    } else {
        char option[6];
        cx_bool value = 0;
        cx_bool meta = 0;
        cx_bool scope = 0;

        /* Set correct content type */
        mg_send_header(conn, "Content-Type", "application/json; charset=utf-8");

        /* Determine what to show */
        mg_get_var(conn, "value", option, sizeof(option));
        if (!strcmp(option, "true")) { value = TRUE; }

        mg_get_var(conn, "meta", option, sizeof(option));
        if (!strcmp(option, "true")) { meta = TRUE; }

        mg_get_var(conn, "scope", option, sizeof(option));
        if (!strcmp(option, "true")) { scope = TRUE; }

        if (!(value || meta || scope)) {
            value = TRUE;
        }

        {
            /* Serialize object-value to JSON */
            struct cx_serializer_s serializer = cx_json_ser(CX_PRIVATE, CX_NOT, CX_SERIALIZER_TRACE_NEVER);

            if ((cx_typeof(o)->kind == CX_VOID) && (!meta && !scope)) {
                mg_printf_data(conn, "\n");
            } else {
                mg_printf_data(conn, "[");

                /* Serialize metadata of parents */
                if (meta) {
                    if (web_printParents(conn, cx_parentof(o))) {
                        mg_printf_data(conn, ",");
                    }
                }

                /* Serialize value */              
                {
                    cx_json_ser_t jsonData = {NULL, NULL, 0, 0, 0, meta, value, scope, TRUE};
                    cx_serialize(&serializer, o, &jsonData);
                    mg_printf_data(conn, "%s", jsonData.buffer);
                    cx_dealloc(jsonData.buffer);
                }
                mg_printf_data(conn, "]\n");
            }
        }

        cx_free(o);
    }

    return MG_TRUE;
}
Пример #8
0
/* Build dependency-administration for object */
int cx_genDepBuildAction(cx_object o, void* userData) {
    g_itemWalk_t data;
    struct g_depWalk_t walkData;
    struct cx_serializer_s s;
    int result;
    cx_object parent = NULL;

    if (cx_checkAttr(o, CX_ATTR_SCOPED)) {
        parent = cx_parentof(o);
    }

    data = userData;

    /* If object is cortex_lang_o, signal that a bootstrap is found, indicating
     * that dependencies should be disregarded. */
    if (o == cortex_lang_o) {
        data->bootstrap = TRUE;
        result = 0;
    } else {
        /* Insert type-dependency: object can be declared only after it's type is defined. */
        if (g_mustParse(data->g, cx_typeof(o))) {
            cx_depresolver_depend(data->resolver, o, CX_DECLARED, cx_typeof(o), CX_DEFINED);
        }

        /* TODO: this is not nice */
        if (cx_class_instanceof(cx_procedure_o, cx_typeof(o))) {
            /* Insert base-dependency: methods may only be declared after the base of a class has been defined. */
            if (cx_typeof(o) != cx_type(cx_function_o)) {
                if (cx_class_instanceof(cx_class_o, parent) && cx_interface(parent)->base) {
                    if (g_mustParse(data->g, cx_interface(parent)->base)) {
                        cx_depresolver_depend(data->resolver, o, CX_DECLARED, cx_interface(parent)->base, CX_DEFINED);
                    }
                }
            }

            /* Add dependencies on function parameters - types must be declared before function is declared. */
            if (cx_genDepBuildProc(cx_function(o), data)) {
                goto error;
            }
        }

        /* Insert dependency on parent */
        if (cx_checkAttr(o, CX_ATTR_SCOPED)) {
            if (parent != root_o) { /* Root is always available */
                cx_int8 parentState = cx_type(cx_typeof(o))->parentState;

                cx_depresolver_depend(data->resolver, o, CX_DECLARED, parent, parentState);
                if (parentState == CX_DECLARED) {
                    cx_depresolver_depend(data->resolver, parent, CX_DEFINED, o, CX_DEFINED);
                }
            }
        }

        /* Insert dependencies on references in the object-value */
        walkData.o = o;
        walkData.data = data;
        s = cx_genDepSerializer();
        if (cx_serialize(&s, o, &walkData)) {
            goto error;
        }

        result = 1;
    }

    return result;
error:
    return 0;
}
Пример #9
0
/* ::cortex::Fast::StaticInitializer::define() */
cx_int16 Fast_StaticInitializer_define(Fast_StaticInitializer _this) {
/* $begin(::cortex::Fast::StaticInitializer::define) */
    cx_uint32 variable;
    cx_object o;

    /* Types are defined during 1st pass. All other objects are defined when a program starts. Defining types
     * at compile-time is required to do validity checking while parsing. On the other hand, objects cannot be
     * defined at compile-time because class constructors\destructors are not yet defined at that point. This
     * would cause object creation\destruction without calling the appropriate constructors\destructors.
     */
    for(variable=0; variable<Fast_Initializer(_this)->variableCount; variable++) {
        o = (cx_object)Fast_Object(Fast_Initializer(_this)->variables[variable].object)->value;
        if (cx_instanceof(cx_type(cx_type_o), o)
                || (cx_checkAttr(o, CX_ATTR_SCOPED) && cx_instanceof(cx_type(cx_type_o), cx_parentof(o)))) {
            if (cx_define(o)) {
                cx_id id1, id2;
                Fast_Parser_error(yparser(), "define of variable '%s' of type '%s' failed",
                        Fast_Parser_id(o, id1),
                        Fast_Parser_id(cx_typeof(o), id2));
                goto error;
            }
        } else {
            Fast_Expression refVar = Fast_Expression(Fast_Object__create(o));
            refVar->isReference = TRUE; /* Always treat object as reference */
            Fast_Define defineStmt = Fast_Define__create(refVar);
            Fast_Parser_addStatement(yparser(), Fast_Node(defineStmt));
            Fast_Parser_collect(yparser(), defineStmt);
            Fast_Parser_collect(yparser(), refVar);
        }
    }
    
    Fast_Initializer_define_v(Fast_Initializer(_this));

    return 0;
error:
    return -1;
/* $end */
}
Пример #10
0
static cx_int16 serializeMeta(cx_serializer s, cx_value* v, void* userData) {
    CX_UNUSED(s);
    cx_json_ser_t *data = userData;
    cx_object o = cx_valueValue(v);

    if (!data->serializeMeta) {
        goto error;
    }

    if (!cx_ser_appendstr(userData, "{")) {
        goto finished;
    }

    cx_string name = cx_nameof(o);
    if (name) {
        if (!cx_appendStringAttr("name", name, userData)) {
            goto finished;
        }
    } else {
        if (!cx_ser_appendstr(userData, "\"name\":\"::\",")) {
            goto finished;
        }        
    }

    cx_id type_fullname;
    cx_fullname(cx_typeof(o), type_fullname);
    if (!cx_appendStringAttr("type", type_fullname, userData)) {
        goto finished;
    }

    if (cx_checkAttr(o, CX_ATTR_PERSISTENT)) {
        cx_time t = cx_timestampof(o);
        if (!cx_ser_appendstr(data, "\"timestamp\":\"%d.%.9d\",", t.tv_sec, t.tv_nsec)) {
            goto finished;
        }        
    }

    char states[sizeof("V|DCL|DEF")];
    dbsh_stateStr(o, states);
    if (!cx_ser_appendstr(data, "\"states\":\"%s\",", states)) {
        goto finished;
    }

    char attributes[sizeof("S|W|O|P")];
    dbsh_attrStr(o, attributes);
    if (!cx_ser_appendstr(data, "\"attributes\":\"%s\",", attributes)) {
        goto finished;
    }

    cx_id parent_fullname;
    cx_fullname(cx_parentof(o), parent_fullname);
    if (!cx_ser_appendstr(data, "\"parent\":\"%s\",", parent_fullname)) {
        goto finished;
    }

    cx_uint32 scopeSize = cx_scopeSize(o);
    if (!cx_ser_appendstr(data, "\"childCount\":%"PRId32"", scopeSize)) {
        goto finished;
    }

    if (!cx_ser_appendstr(data, "}")) {
        goto finished;
    }


    return 0;
error:
    return -1;
finished:
    return 1;
}