Example #1
0
char * ScilabObjects::getSingleString(int pos, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    char * str = 0;

    err = getVarAddressFromPosition(pvApiCtx, pos, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid String"));
    }

    if (!isStringType(pvApiCtx, addr))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("A single string expected"));
    }

    if (!isScalar(pvApiCtx, addr))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("A single String expected"));
    }

    if (getAllocatedSingleString(pvApiCtx, addr, &str))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid String"));
    }

    return str;
}
int ScilabGateway::unwrapremove(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    int row = 0, col = 0;
    int * id = 0;

    if (Rhs == 0)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong number of arguments : more than 1 argument expected"));
    }

    CheckOutputArgument(pvApiCtx, Rhs, Rhs);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    for (int i = 1; i <= Rhs; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i, &addr);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (!ScilabObjects::isExternalObj(addr, pvApiCtx))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: An External Object expected."), i);

        }

        err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (!ScilabObjects::unwrap(*id, Rhs + i, envId, pvApiCtx))
        {
            try
            {
                ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + i, *id, envId, pvApiCtx);
            }
            catch (ScilabAbstractEnvironmentException & /*e*/)
            {
            }
        }

        LhsVar(i) = Rhs + i;

        env.removeobject(*id);
    }

    PutLhsVar();

    return 0;
}
int ScilabGateway::getRepresentation(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    int tmpvar[2] = {0, 0};
    int idObj = 0;

    CheckInputArgument(pvApiCtx, 1, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (!ScilabObjects::isExternalObjOrClass(addr, pvApiCtx))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: An External Object expected."), 1);
    }

    idObj = ScilabObjects::getArgumentId(addr, tmpvar, false, false, envId, pvApiCtx);

    env.getrepresentation(idObj, ScilabStringStackAllocator(pvApiCtx, Rhs + 1));

    LhsVar(1) = Rhs + 1;
    PutLhsVar();

    return 0;
}
Example #4
0
int ScilabGateway::exists(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    int * id = 0;
    int row = 0;
    int col = 0;
    bool exists = false;

    CheckInputArgument(pvApiCtx, 1, 1);
    CheckOutputArgument(pvApiCtx, 1, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccured(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (!ScilabObjects::isExternalObjOrClass(addr, pvApiCtx))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: An External Object expected."), 1);
        return 0;
    }

    err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    try
    {
        exists = env.isvalidobject(*id);
    }
    catch (std::exception & e)
    {
        throw;
    }

    createScalarBoolean(pvApiCtx, 1, exists ? 1 : 0);

    LhsVar(1) = 1;
    PutLhsVar();

    return 0;
}
Example #5
0
void ScilabObjects::createEnvironmentObjectAtPos(int type, int pos, int id, const int envId, void * pvApiCtx)
{
    const char ** fields = 0;
    int * mlistaddr = 0;
    SciErr err;

    if (envId < 0)
    {
        throw ScilabAbstractEnvironmentException("Invalid environment");
    }

    switch (type)
    {
        case EXTERNAL_OBJECT:
            fields = static_cast<const char **>(_EOBJ);
            break;
        case EXTERNAL_CLASS:
            fields = static_cast<const char **>(_ECLASS);
            break;
        case EXTERNAL_VOID:
            fields = static_cast<const char **>(_EVOID);
            break;
        default :
            fields = static_cast<const char **>(_EOBJ);
            break;
    }

    err = createMList(pvApiCtx, pos, FIELDS_LENGTH, &mlistaddr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot allocate memory"));
    }

    err = createMatrixOfStringInList(pvApiCtx, pos, mlistaddr, 1, 1, FIELDS_LENGTH, fields);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot allocate memory"));
    }

    err = createMatrixOfInteger32InList(pvApiCtx, pos, mlistaddr, EXTERNAL_ENV_ID_POSITION, 1, 1, &envId);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot allocate memory"));
    }

    err = createMatrixOfInteger32InList(pvApiCtx, pos, mlistaddr, EXTERNAL_OBJ_ID_POSITION, 1, 1, &id);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot allocate memory"));
    }
}
Example #6
0
int ScilabGateway::getsetOptions(char * fname, const int envId, ScilabAbstractOptionsSetter & setter, void * pvApiCtx)
{
    SciErr sciErr;
    int * addr = 0;
    int val = 0;

    CheckInputArgument(pvApiCtx, 0, 1);
    CheckOutputArgument(pvApiCtx, 1, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccured(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    if (Rhs == 0)
    {
        createScalarBoolean(pvApiCtx, 1, (int)setter.get());

        LhsVar(1) = 1;
        PutLhsVar();

        return 0;
    }

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &addr);
    if (sciErr.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (!isBooleanType(pvApiCtx, addr))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A boolean expected."), 1);
    }

    if (!isScalar(pvApiCtx, addr))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A single boolean expected."), 1);
    }

    getScalarBoolean(pvApiCtx, addr, &val);
    setter.set(val == 1);

    LhsVar(1) = 0;
    PutLhsVar();

    return 0;
}
ScilabAbstractEnvironment & ScilabEnvironments::getEnvironment(const int id)
{
    if (id >= 0 && id < environments.size() && environments[id])
    {
        return *(environments[id]);
    }

    throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid environment: identifier %d is invalid"), id);
}
int ScilabGateway::getEnvironmentName(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    int eId = 0;
    std::string envName;
    char * str;

    CheckInputArgument(pvApiCtx, 1, 1);

    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (!ScilabObjects::isExternalObjOrClass(addr, pvApiCtx))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: An External Object expected."), 1);
    }

    eId = ScilabObjects::getEnvironmentId(addr, pvApiCtx);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(eId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    envName = env.getEnvironmentName();
    str = strdup(envName.c_str());

    err = createMatrixOfString(pvApiCtx, Rhs + 1, 1, 1, (const char * const *)&str);
    free(str);

    LhsVar(1) = Rhs + 1;
    PutLhsVar();

    return 0;
}
Example #9
0
int ScilabObjects::createNamedEnvironmentObject(int type, const char * name, int id, const int envId, void * pvApiCtx)
{
    const char ** fields;
    int * mlistaddr = 0;
    SciErr err;

    if (envId < 0)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid environment"));
    }

    switch (type)
    {
        case EXTERNAL_OBJECT:
            fields = static_cast<const char **>(_EOBJ);
            break;
        case EXTERNAL_CLASS:
            fields = static_cast<const char **>(_ECLASS);
            break;
        case EXTERNAL_VOID:
            fields = static_cast<const char **>(_EVOID);
            break;
        default :
            fields = static_cast<const char **>(_EOBJ);
            break;
    }

    err = createNamedMList(pvApiCtx, name, FIELDS_LENGTH, &mlistaddr);
    if (err.iErr)
    {
        if (err.iErr == API_ERROR_INVALID_NAME)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable name: %s"), name);
        }

        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot allocate memory"));
    }

    err = createMatrixOfStringInNamedList(pvApiCtx, name, mlistaddr, 1, 1, FIELDS_LENGTH, fields);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot allocate memory"));
    }

    err = createMatrixOfInteger32InNamedList(pvApiCtx, name, mlistaddr, EXTERNAL_ENV_ID_POSITION, 1, 1, &envId);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot allocate memory"));
    }

    err = createMatrixOfInteger32InNamedList(pvApiCtx, name, mlistaddr, EXTERNAL_OBJ_ID_POSITION, 1, 1, &id);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot allocate memory"));
    }

    return 1;
}
Example #10
0
int ScilabObjects::getExternalId(int * addr, void * pvApiCtx)
{
    SciErr err;
    int row = 0, col = 0;
    int * id = 0;

    err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    return *id;
}
Example #11
0
int ScilabGateway::remove(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccured(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    if (Rhs == 0)
    {
        env.garbagecollect();

        LhsVar(1) = 0;
        PutLhsVar();

        return 0;
    }

    for (int i = 1; i <= Rhs; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i, &addr);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        ScilabObjects::removeVar(addr, pvApiCtx);
    }

    LhsVar(1) = 0;
    PutLhsVar();

    return 0;
}
Example #12
0
int ScilabGateway::getEnvId(char * fname, const int envId, void * pvApiCtx)
{
    SciErr sciErr;

    CheckInputArgument(pvApiCtx, 0, 0);
    CheckOutputArgument(pvApiCtx, 1, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    sciErr = createMatrixOfInteger32(pvApiCtx, 1, 1, 1, &envId);
    if (sciErr.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot create the identifier"));
    }

    LhsVar(1) = 1;
    PutLhsVar();

    return 0;
}
Example #13
0
int ScilabGateway::array(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int ret = 0;
    int * addr = 0;
    char * className = 0;
    int * args = 0;
    char * errmsg = 0;

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);

    if (Rhs < 2)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong number of arguments : more than %d expected."), 2);
    }

    OptionsHelper::setCopyOccured(false);
    ScilabObjects::initialization(env, pvApiCtx);
    env.getGatewayOptions().setIsNew(false);

    className = ScilabObjects::getSingleString(1, pvApiCtx);

    args = new int[Rhs - 1];

    for (int i = 0; i < Rhs - 1; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i + 2, &addr);
        if (err.iErr)
        {
            delete[] args;
            freeAllocatedSingleString(className);
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        args[i] = ScilabObjects::isPositiveIntegerAtAddress(addr, pvApiCtx);

        if (args[i] == -1 || args[i] == 0)
        {
            delete[] args;
            freeAllocatedSingleString(className);
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("A strictly positive integer is expected at position %d."), i + 2);
        }
    }

    try
    {
        ret = env.createarray(className, args, Rhs - 1);
    }
    catch (std::exception & e)
    {
        delete[] args;
        freeAllocatedSingleString(className);
        throw;
    }

    delete[] args;
    freeAllocatedSingleString(className);

    try
    {
        ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + 1, ret, envId, pvApiCtx);
    }
    catch (ScilabAbstractEnvironmentException & e)
    {
        env.removeobject(ret);
        throw;
    }

    LhsVar(1) = Rhs + 1;
    PutLhsVar();

    return 0;
}
Example #14
0
int ScilabGateway::invoke_lu(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int typ = 0;
    int * addr = 0;
    int * listaddr = 0;
    int len = 0;
    int * tmpvar = 0;
    int idObj = 0;
    int * args = 0;
    int * child = 0;
    char * methName = 0;
    int * eId;
    int row, col;
    int * ret = 0;
    int nbArgs = 0;
    std::vector<int> torem;

    CheckInputArgument(pvApiCtx, 4, 4);

    err = getVarAddressFromPosition(pvApiCtx, 4, &listaddr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    err = getVarType(pvApiCtx, listaddr, &typ);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (typ != sci_list)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A List expected."), 4);
    }

    err = getListItemNumber(pvApiCtx, listaddr, &len);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    // Get the environment id
    err = getVarAddressFromPosition(pvApiCtx, 2, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    err = getVarType(pvApiCtx, addr, &typ);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (typ != sci_ints)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: An Integer32 expected."), 2);
    }
    else
    {
        int prec;

        err = getMatrixOfIntegerPrecision(pvApiCtx, addr, &prec);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (prec != SCI_INT32)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: An Integer32 expected."), 2);
        }

        err = getMatrixOfInteger32(pvApiCtx, addr, &row, &col, &eId);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }
    }

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(*eId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    // Get the object id
    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    err = getVarType(pvApiCtx, addr, &typ);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (typ != sci_ints)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: An Integer32 expected."), 1);
    }
    else
    {
        int prec;
        int * id;

        err = getMatrixOfIntegerPrecision(pvApiCtx, addr, &prec);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (prec != SCI_INT32)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: An Integer32 expected."), 1);
        }

        err = getMatrixOfInteger32(pvApiCtx, addr, &row, &col, &id);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        idObj = *id;
    }

    if (idObj == 0)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot invoke on null object."), __FILE__, __LINE__);
    }

    tmpvar = new int[len + 1];
    *tmpvar = 0;

    args = new int[len];
    nbArgs = len;

    for (int i = 0; i < len; i++)
    {
        err = getListItemAddress(pvApiCtx, listaddr, i + 1, &child);
        if (err.iErr)
        {
            delete[] args;
            ScilabObjects::removeTemporaryVars(*eId, tmpvar);
            delete[] tmpvar;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        try
        {
            args[i] = ScilabObjects::getArgumentId(child, tmpvar, false, false, *eId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & e)
        {
            delete[] args;
            delete[] tmpvar;
            throw;
        }

        if (args[i] == VOID_OBJECT)
        {
            nbArgs = 0;
        }
    }

    try
    {
        methName = ScilabObjects::getSingleString(3, pvApiCtx);
    }
    catch (ScilabAbstractEnvironmentException & e)
    {
        delete[] args;
        ScilabObjects::removeTemporaryVars(*eId, tmpvar);
        delete[] tmpvar;
        throw;
    }

    try
    {
        ret = env.invoke(idObj, methName, args, nbArgs);
    }
    catch (std::exception & e)
    {
        delete[] args;
        ScilabObjects::removeTemporaryVars(*eId, tmpvar);
        delete[] tmpvar;
        freeAllocatedSingleString(methName);
        throw;
    }

    delete[] args;
    ScilabObjects::removeTemporaryVars(*eId, tmpvar);
    delete[] tmpvar;
    freeAllocatedSingleString(methName);

    if (!ret || *ret <= 0 || (*ret == 1 && ret[1] == VOID_OBJECT))
    {
        if (ret)
        {
            delete[] ret;
        }

        PutLhsVar();

        return 0;
    }

    torem.reserve(*ret);

    for (int i = 1; i <= *ret; i++)
    {
        if (!ScilabObjects::unwrap(ret[i], Rhs + i, *eId, pvApiCtx))
        {
            try
            {
                ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + i, ret[i], *eId, pvApiCtx);
            }
            catch (ScilabAbstractEnvironmentException & e)
            {
                if (!torem.empty())
                {
                    env.removeobject(&(torem[0]), torem.size());
                }
                env.removeobject(ret + 1, *ret);
                delete[] ret;
                throw;
            }
        }
        else
        {
            torem.push_back(ret[i]);
        }

        LhsVar(i) = Rhs + i;
    }

    if (!torem.empty())
    {
        env.removeobject(&(torem[0]), torem.size());
    }

    delete[] ret;

    PutLhsVar();

    return 0;
}
Example #15
0
int ScilabGateway::wrapAsRef(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * tmpvar = 0;
    int * addr = 0;
    char * varName = 0;
    int idObj;

    if (Rhs == 0)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong number of arguments : more than 1 argument expected."));
    }

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    CheckOutputArgument(pvApiCtx, Rhs, Rhs);

    tmpvar = new int[Rhs + 1];
    *tmpvar = 0;

    for (int i = 1; i < Rhs + 1; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i, &addr);
        if (err.iErr)
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (!isScalar(pvApiCtx, addr) || !isStringType(pvApiCtx, addr))
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Can only wrap as a reference to a named variable"));
        }

        if (getAllocatedSingleString(pvApiCtx, addr, &varName))
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        err = getVarAddressFromName(pvApiCtx, varName, &addr);
        freeAllocatedSingleString(varName);
        if (err.iErr)
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        try
        {
            idObj = ScilabObjects::getArgumentId(addr, tmpvar, true, false, envId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & /*e*/)
        {
            delete[] tmpvar;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot wrap argument %d."), i);
        }

        try
        {
            ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + i, idObj, envId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & /*e*/)
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            throw;
        }

        LhsVar(i) = Rhs + i;
    }

    // We don't remove tmpvar since it contains id which are put on rhs
    delete[] tmpvar;

    PutLhsVar();

    return 0;
}
Example #16
0
int ScilabGateway::deff(char * fname, const int envId, void * pvApiCtx)
{
    static int ONE = 1;
    static int TWO = 2;
    static int THREE = 3;

    SciErr err;
    char ** names[] = {0, 0, 0};
    int ret = 0;
    std::ostringstream os;
    int * addr[] = {0, 0, 0};
    int rows[] = {0, 0, 0};
    int cols[] = {0, 0, 0};
    int error = 0;
    char * cwd = 0;

    CheckInputArgument(pvApiCtx, 3, 3);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper & helper = env.getOptionsHelper();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    if (pCall == nullptr)
    {
        symbol::Context* ctx = symbol::Context::getInstance();
        types::InternalType* pIT = ctx->get(symbol::Symbol(L"#_deff_wrapper"));
        if (pIT && pIT->isCallable())
        {
            pCall = pIT->getAs<types::Callable>();
        }
        else
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }
    }

    for (int i = 0; i < 3; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i + 1, &(addr[i]));
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (!isStringType(pvApiCtx, addr[i]))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), 1);
        }

        err = getVarDimension(pvApiCtx, addr[i], &(rows[i]), &(cols[i]));
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }
    }

    if (rows[0] != 1 || cols[0] != 1)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid dimensions for input argument #%d: A single string expected."), 1);
    }

    if (rows[1] != rows[2] || cols[1] != cols[2])
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid dimensions: arguments #2 and #3 must have the same."));
    }

    for (int i = 0; i < 3; i++)
    {
        if (getAllocatedMatrixOfString(pvApiCtx, addr[i], &(rows[i]), &(cols[i]), &(names[i])))
        {
            for (int j = 0; j < i; j++)
            {
                freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
            }
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }
    }

    cwd = scigetcwd(&error);
    if (error)
    {
        FREE(cwd);
        cwd = 0;
    }

    try
    {
        ret = env.loadclass(names[0][0], cwd, false, helper.getAllowReload());
    }
    catch (std::exception & /*e*/)
    {
        FREE(cwd);
        for (int j = 0; j < 3; j++)
        {
            freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
        }
        throw;
    }
    FREE(cwd);

    for (int i = 0; i < rows[1] * cols[1]; i++)
    {
        //call #_deff_wrapper
        types::typed_list in, out;
        types::optional_list opt;

        //name
        in.push_back(new types::String(names[2][i]));


        //protopype
        os.str("");
        os << "y=" << names[2][i] << "(varargin)" << std::flush;
        in.push_back(new types::String(os.str().c_str()));

        //body
        os.str("");
        os << "y=invoke_lu(int32(" << ret << "),int32(" << envId << "),\"" << names[1][i] << "\",varargin)" << std::flush;
        in.push_back(new types::String(os.str().c_str()));

        ast::ExecVisitor exec;
        if (pCall->call(in, opt, 0, out, &exec) != types::Function::OK)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot create the data"));
        }
    }

    for (int i = 0; i < 3; i++)
    {
        freeAllocatedMatrixOfString(rows[0], cols[0], names[i]);
    }

    LhsVar(1) = 0;
    PutLhsVar();

    return 0;
}
int ScilabGateway::addToClasspath(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    int rows;
    int cols;
    char ** className = 0;

    CheckInputArgumentAtLeast(pvApiCtx, 1);
    CheckOutputArgument(pvApiCtx, 1, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    for (int i = 1; i <= Rhs; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i, &addr);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (!isStringType(pvApiCtx, addr))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for argument #%d: string expected."), 1);
        }

        if (getAllocatedMatrixOfString(pvApiCtx, addr, &rows, &cols, &className))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        for (int j = 0; j < rows * cols; j++)
        {
            char * expandedPath = expandPathVariable(const_cast<char *>(className[j]));
            if (expandedPath)
            {
                try
                {
                    env.addtoclasspath(expandedPath);
                }
                catch (std::exception & /*e*/)
                {
                    FREE(expandedPath);
                    freeAllocatedMatrixOfString(rows, cols, className);
                    throw;
                }
                FREE(expandedPath);
            }
            else
            {
                std::string str(className[j]);
                freeAllocatedMatrixOfString(rows, cols, className);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot open the given file %s."), str.c_str());
            }
        }

        freeAllocatedMatrixOfString(rows, cols, className);
        className = 0;
    }

    LhsVar(1) = 0;
    PutLhsVar();

    return 0;
}
Example #18
0
int ScilabGateway::import(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int rows, cols;
    char ** className = 0;
    std::string * name = 0;
    int named = 1;
    int * addr = 0;
    int ret = 0;
    int nbArgs = Rhs;
    int error = 0;
    char * cwd = 0;

    CheckInputArgumentAtLeast(pvApiCtx, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    OptionsHelper & helper = env.getOptionsHelper();
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    err = getVarAddressFromPosition(pvApiCtx, Rhs, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (isBooleanType(pvApiCtx, addr))
    {
        nbArgs = Rhs - 1;
        if (getScalarBoolean(pvApiCtx, addr, &named))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }
    }

    if (nbArgs == 1)
    {
        err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (!isStringType(pvApiCtx, addr))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), 1);
        }

        if (getAllocatedMatrixOfString(pvApiCtx, addr, &rows, &cols, &className))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }
    }
    else
    {
        className = (char**)MALLOC(sizeof(char *) * nbArgs);
        for (int i = 1; i <= nbArgs; i++)
        {
            err = getVarAddressFromPosition(pvApiCtx, i, &addr);
            if (err.iErr)
            {
                freeAllocatedMatrixOfString(1, i, className);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
            }

            if (!isStringType(pvApiCtx, addr))
            {
                freeAllocatedMatrixOfString(1, i, className);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), i);
            }

            if (getAllocatedSingleString(pvApiCtx, addr, &(className[i - 1])))
            {
                freeAllocatedMatrixOfString(1, i, className);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
            }
        }

        rows = 1;
        cols = nbArgs;
    }

    if (named)
    {
        name = new std::string[rows * cols];

        for (int i = 0; i < rows * cols; i++)
        {
            name[i] = std::string(className[i]);
            if (helper.getUseLastName())
            {
                std::size_t pos = name[i].find_last_of('.');
                if (pos != std::string::npos)
                {
                    if (pos == name[i].size() - 1)
                    {
                        freeAllocatedMatrixOfString(rows, cols, className);
                        delete[] name;
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("The class name cannot end with a dot."));
                    }
                    name[i] = name[i].substr(pos + 1);
                }
            }
            else
            {
                std::size_t pos = name[i].find_first_of('.');
                if (pos != std::string::npos)
                {
                    if (pos == 0)
                    {
                        freeAllocatedMatrixOfString(rows, cols, className);
                        delete[] name;
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("The class name cannot start with a dot."));
                    }
                    name[i] = name[i].substr(0, pos);
                }
            }

            if (isNamedVarExist(pvApiCtx, name[i].c_str()))
            {
                addr = 0;
                err = getVarAddressFromName(pvApiCtx, name[i].c_str(), &addr);
                if (err.iErr || addr == 0 || !ScilabObjects::isValidExternal(addr, pvApiCtx) || ScilabObjects::getEnvironmentId(addr, pvApiCtx) != envId)
                {
                    freeAllocatedMatrixOfString(rows, cols, className);
                    delete[] name;
                    throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("A variable with this name is already existing"));
                }
            }
        }
    }

    if (!named && rows * cols != Lhs)
    {
        freeAllocatedMatrixOfString(rows, cols, className);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Bad number of output arguments"), __FILE__, __LINE__);
    }

    const int type = helper.getNewAllowed() ? EXTERNAL_CLASS : EXTERNAL_OBJECT;

    cwd = scigetcwd(&error);
    if (error)
    {
        FREE(cwd);
        cwd = 0;
    }

    for (int i = 0; i < rows * cols; i++)
    {
        try
        {
            ret = env.loadclass(className[i], cwd, static_cast<bool>(named), helper.getAllowReload());
        }
        catch (std::exception & e)
        {
            FREE(cwd);
            freeAllocatedMatrixOfString(rows, cols, className);
            if (named)
            {
                delete[] name;
            }
            throw;
        }

        if (named)
        {
            try
            {
                ScilabObjects::createNamedEnvironmentObject(type, name[i].c_str(), ret, envId, pvApiCtx);
            }
            catch (ScilabAbstractEnvironmentException & e)
            {
                FREE(cwd);
                freeAllocatedMatrixOfString(rows, cols, className);
                delete[] name;
                throw;
            }
        }
        else
        {
            try
            {
                ScilabObjects::createEnvironmentObjectAtPos(type, Rhs + i + 1, ret, envId, pvApiCtx);
            }
            catch (ScilabAbstractEnvironmentException & e)
            {
                FREE(cwd);
                freeAllocatedMatrixOfString(rows, cols, className);
                env.removeobject(ret);
                throw;
            }
            LhsVar(i + 1) = Rhs + i + 1;
        }
    }

    FREE(cwd);

    freeAllocatedMatrixOfString(rows, cols, className);
    if (named)
    {
        delete[] name;
        LhsVar(1) = 0;
    }

    PutLhsVar();

    return 0;
}
Example #19
0
int ScilabGateway::getClassName(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * tmpvar = 0;
    int idObj = 0;
    int * addr = 0;
    std::string * classNames = 0;
    const char ** str = 0;

    CheckInputArgumentAtLeast(pvApiCtx, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    classNames = new std::string[Rhs];
    tmpvar = new int[Rhs + 1];
    *tmpvar = 0;

    for (int i = 1; i <= Rhs; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i, &addr);
        if (err.iErr)
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            delete[] classNames;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        try
        {
            idObj = ScilabObjects::getArgumentId(addr, tmpvar, false, false, envId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & e)
        {
            delete[] tmpvar;
            delete[] classNames;
            throw;
        }

        try
        {
            classNames[i - 1] = env.getclassname(idObj);
        }
        catch (std::exception & e)
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            delete[] classNames;
            throw;
        }
    }

    ScilabObjects::removeTemporaryVars(envId, tmpvar);

    str = new const char*[Rhs];
    for (int i = 0; i < Rhs; i++)
    {
        str[i] = classNames[i].c_str();
    }

    err = createMatrixOfString(pvApiCtx, Rhs + 1, 1, Rhs, (const char * const *)str);
    delete[] classNames;
    delete[] str;

    LhsVar(1) = Rhs + 1;
    PutLhsVar();

    return 0;
}
Example #20
0
int ScilabGateway::deff(char * fname, const int envId, void * pvApiCtx)
{
    static int ONE = 1;
    static int TWO = 2;
    static int THREE = 3;

    SciErr err;
    char ** names[] = {0, 0, 0};
    int ret = 0;
    std::ostringstream os;
    char * str;
    int * addr[] = {0, 0, 0};
    int rows[] = {0, 0, 0};
    int cols[] = {0, 0, 0};
    int error = 0;
    char * cwd = 0;

    CheckInputArgument(pvApiCtx, 3, 3);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper & helper = env.getOptionsHelper();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    for (int i = 0; i < 3; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i + 1, &(addr[i]));
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (!isStringType(pvApiCtx, addr[i]))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), 1);
        }

        err = getVarDimension(pvApiCtx, addr[i], &(rows[i]), &(cols[i]));
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }
    }

    if (rows[0] != 1 || cols[0] != 1)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid dimensions for input argument #%d: A single string expected."), 1);
    }

    if (rows[1] != rows[2] || cols[1] != cols[2])
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid dimensions: arguments #2 and #3 must have the same."));
    }

    for (int i = 0; i < 3; i++)
    {
        if (getAllocatedMatrixOfString(pvApiCtx, addr[i], &(rows[i]), &(cols[i]), &(names[i])))
        {
            for (int j = 0; j < i; j++)
            {
                freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
            }
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }
    }

    cwd = scigetcwd(&error);
    if (error)
    {
        FREE(cwd);
        cwd = 0;
    }

    try
    {
        ret = env.loadclass(names[0][0], cwd, false, helper.getAllowReload());
    }
    catch (std::exception & e)
    {
        FREE(cwd);
        for (int j = 0; j < 3; j++)
        {
            freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
        }
        throw;
    }
    FREE(cwd);

    for (int i = 0; i < rows[1] * cols[1]; i++)
    {
        err = createMatrixOfString(pvApiCtx, ONE, 1, 1, (const char * const *) & (names[2][i]));
        if (err.iErr)
        {
            for (int j = 0; j < 3; j++)
            {
                freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
            }
            env.removeobject(ret);
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot create the data"));
        }

        os.str("");
        os << "y=" << names[2][i] << "(varargin)" << std::flush;
        str = strdup(os.str().c_str());

        err = createMatrixOfString(pvApiCtx, TWO, 1, 1, (const char * const *)&str);
        free(str);
        if (err.iErr)
        {
            for (int j = 0; j < 3; j++)
            {
                freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
            }
            env.removeobject(ret);
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot create the data"));
        }

        os.str("");
        os << "y=invoke_lu(int32(" << ret << "),int32(" << envId << "),\"" << names[1][i] << "\",varargin)" << std::flush;
        str = strdup(os.str().c_str());

        err = createMatrixOfString(pvApiCtx, THREE, 1, 1, (const char * const *)&str);
        free(str);
        if (err.iErr)
        {
            for (int j = 0; j < 3; j++)
            {
                freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
            }
            env.removeobject(ret);
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot create the data"));
        }

        SciString(&ONE, const_cast<char *>("!_deff_wrapper"), &ONE, &THREE);
    }

    for (int i = 0; i < 3; i++)
    {
        freeAllocatedMatrixOfString(rows[0], cols[0], names[i]);
    }

    LhsVar(1) = 0;
    PutLhsVar();

    return 0;
}
Example #21
0
void ScilabObjects::removeVar(int * addr, void * pvApiCtx)
{
    SciErr err;
    int type, row, col, * id;

    err = getVarType(pvApiCtx, addr, &type);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (type == sci_mlist && (isExternalObjOrClass(addr, pvApiCtx)))
    {
        err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        int envId = getEnvironmentId(addr, pvApiCtx);
        ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);

        env.removeobject(*id);
    }
    else if (type == sci_strings)
    {
        char * varName = 0;
        if (getAllocatedSingleString(pvApiCtx, addr, &varName))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        err = getVarAddressFromName(pvApiCtx, varName, &addr);
        if (err.iErr)
        {
            freeAllocatedSingleString(varName);
            return;
        }

        err = getVarType(pvApiCtx, addr, &type);
        if (err.iErr)
        {
            freeAllocatedSingleString(varName);
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (type == sci_mlist && isExternalObjOrClass(addr, pvApiCtx))
        {
            err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id);
            if (err.iErr)
            {
                freeAllocatedSingleString(varName);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
            }

            int envId = getEnvironmentId(addr, pvApiCtx);
            ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);

            env.removeobject(*id);
            deleteNamedVariable(pvApiCtx, varName);
            freeAllocatedSingleString(varName);
        }
    }
}
Example #22
0
int ScilabGateway::classExtract(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int tmpvar[2] = {0, 0};
    int * addr = 0;
    int idObj = 0;
    int eId = 0;
    int type = 0;
    int ret = 0;
    char * fieldName = 0;

    CheckInputArgument(pvApiCtx, 2, 2);

    err = getVarAddressFromPosition(pvApiCtx, 2, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    eId = ScilabObjects::getEnvironmentId(addr, pvApiCtx);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(eId);
    OptionsHelper & helper = env.getOptionsHelper();
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    idObj = ScilabObjects::getArgumentId(addr, tmpvar, false, false, eId, pvApiCtx);

    try
    {
        fieldName = ScilabObjects::getSingleString(1, pvApiCtx);
    }
    catch (ScilabAbstractEnvironmentException & /*e*/)
    {
        ScilabObjects::removeTemporaryVars(eId, tmpvar);
        throw;
    }

    if (helper.getNewAllowed() && !std::strcmp(fieldName, "new"))
    {
        options.setIsNew(true);
    }
    else
    {
        try
        {
            type = env.getfieldtype(idObj, fieldName);
        }
        catch (std::exception & /*e*/)
        {
            ScilabObjects::removeTemporaryVars(eId, tmpvar);
            freeAllocatedSingleString(fieldName);
            throw;
        }
    }

    ScilabObjects::removeTemporaryVars(eId, tmpvar);

    if (type == 0 || options.getIsNew())
    {
        // setMethodName will free fieldName
        options.setMethodName(fieldName);
        options.setObjId(idObj);
        OptionsHelper::setEnvId(eId);
        OptionsHelper::setCopyOccurred(true);
        ScilabObjects::copyInvocationMacroToStack(Rhs + 1, eId, options.getIsNew(), pvApiCtx);

        LhsVar(1) = Rhs + 1;
        PutLhsVar();

        return 0;
    }
    else if (type == 1)
    {
        try
        {
            ret = env.getfield(idObj, fieldName);
        }
        catch (std::exception & /*e*/)
        {
            freeAllocatedSingleString(fieldName);
            throw;
        }

        freeAllocatedSingleString(fieldName);

        if (helper.getAutoUnwrap())
        {
            if (!ScilabObjects::unwrap(ret, Rhs + 1, eId, pvApiCtx))
            {
                try
                {
                    ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + 1, ret, eId, pvApiCtx);
                }
                catch (ScilabAbstractEnvironmentException & /*e*/)
                {
                    env.removeobject(ret);
                    throw;
                }
            }
            else
            {
                env.removeobject(ret);
            }
        }
        else
        {
            try
            {
                ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + 1, ret, eId, pvApiCtx);
            }
            catch (ScilabAbstractEnvironmentException & /*e*/)
            {
                env.removeobject(ret);
                throw;
            }
        }

        LhsVar(1) = Rhs + 1;
        PutLhsVar();
    }
    else
    {
        freeAllocatedSingleString(fieldName);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid name"));
    }

    return 0;
}
Example #23
0
int ScilabGateway::compile(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int row = 0, col = 0;
    int * addr = 0;
    char * className = 0;
    char ** code = 0;
    int ret = 0;
    int iType = 0;

    CheckInputArgument(pvApiCtx, 2, 2);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccured(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    className = ScilabObjects::getSingleString(1, pvApiCtx);

    err = getVarAddressFromPosition(pvApiCtx, 2, &addr);
    if (err.iErr)
    {
        freeAllocatedSingleString(className);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    err = getVarType(pvApiCtx, addr, &iType);
    if (err.iErr)
    {
        freeAllocatedSingleString(className);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (iType != sci_strings)
    {
        freeAllocatedSingleString(className);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), 2);
    }

    err = getVarDimension(pvApiCtx, addr, &row, &col);
    if (err.iErr)
    {
        freeAllocatedSingleString(className);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if ((row < 1 || col != 1) && (col < 1 || row != 1))
    {
        freeAllocatedSingleString(className);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid dimension for argument #%d: A row or a column expected."), 2);
    }

    if (getAllocatedMatrixOfString(pvApiCtx, addr, &row, &col, &code))
    {
        freeAllocatedSingleString(className);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    try
    {
        ret = env.compilecode(className, code, row != 1 ? row : col);
    }
    catch (std::exception & e)
    {
        freeAllocatedSingleString(className);
        freeAllocatedMatrixOfString(row, col, code);
        throw;
    }

    freeAllocatedSingleString(className);
    freeAllocatedMatrixOfString(row, col, code);

    try
    {
        ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_CLASS, Rhs + 1, ret, envId, pvApiCtx);
    }
    catch (ScilabAbstractEnvironmentException & e)
    {
        env.removeobject(ret);
        throw;
    }

    LhsVar(1) = Rhs + 1;
    PutLhsVar();

    return 0;
}
Example #24
0
bool ScilabObjects::unwrap(int idObj, int pos, const int envId, void * pvApiCtx)
{
    if (idObj == 0)
    {
        if (createEmptyMatrix(pvApiCtx, pos))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot create data"));
        }
        return true;
    }

    VariableType type = Nothing;
    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    const ScilabAbstractEnvironmentWrapper & wrapper = env.getWrapper();

    type = env.isunwrappable(idObj);

    switch (type)
    {
        case Nothing:
            return false;
        case Null:
            if (createEmptyMatrix(pvApiCtx, pos))
            {
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot create data"));
            }
        case SingleDouble:
            wrapper.unwrapdouble(idObj, ScilabDoubleStackAllocator(pvApiCtx, pos));
            break;
        case RowDouble:
            wrapper.unwraprowdouble(idObj, ScilabDoubleStackAllocator(pvApiCtx, pos));
            break;
        case MatDouble:
            wrapper.unwrapmatdouble(idObj, ScilabDoubleStackAllocator(pvApiCtx, pos));
            break;
        case SingleString:
            wrapper.unwrapstring(idObj, ScilabStringStackAllocator(pvApiCtx, pos));
            break;
        case RowString:
            wrapper.unwraprowstring(idObj, ScilabStringStackAllocator(pvApiCtx, pos));
            break;
        case MatString:
            wrapper.unwrapmatstring(idObj, ScilabStringStackAllocator(pvApiCtx, pos));
            break;
        case SingleBoolean:
            wrapper.unwrapboolean(idObj, ScilabBooleanStackAllocator(pvApiCtx, pos));
            break;
        case RowBoolean:
            wrapper.unwraprowboolean(idObj, ScilabBooleanStackAllocator(pvApiCtx, pos));
            break;
        case MatBoolean:
            wrapper.unwrapmatboolean(idObj, ScilabBooleanStackAllocator(pvApiCtx, pos));
            break;
        case SingleChar:
            wrapper.unwrapchar(idObj, ScilabCharStackAllocator(pvApiCtx, pos));
            break;
        case RowChar:
            wrapper.unwraprowchar(idObj, ScilabCharStackAllocator(pvApiCtx, pos));
            break;
        case MatChar:
            wrapper.unwrapmatchar(idObj, ScilabCharStackAllocator(pvApiCtx, pos));
            break;
        case SingleUChar:
            wrapper.unwrapuchar(idObj, ScilabUCharStackAllocator(pvApiCtx, pos));
            break;
        case RowUChar:
            wrapper.unwraprowuchar(idObj, ScilabUCharStackAllocator(pvApiCtx, pos));
            break;
        case MatUChar:
            wrapper.unwrapmatuchar(idObj, ScilabUCharStackAllocator(pvApiCtx, pos));
            break;
        case SingleShort:
            wrapper.unwrapshort(idObj, ScilabShortStackAllocator(pvApiCtx, pos));
            break;
        case RowShort:
            wrapper.unwraprowshort(idObj, ScilabShortStackAllocator(pvApiCtx, pos));
            break;
        case MatShort:
            wrapper.unwrapmatshort(idObj, ScilabShortStackAllocator(pvApiCtx, pos));
            break;
        case SingleUShort:
            wrapper.unwrapushort(idObj, ScilabUShortStackAllocator(pvApiCtx, pos));
            break;
        case RowUShort:
            wrapper.unwraprowushort(idObj, ScilabUShortStackAllocator(pvApiCtx, pos));
            break;
        case MatUShort:
            wrapper.unwrapmatushort(idObj, ScilabUShortStackAllocator(pvApiCtx, pos));
            break;
        case SingleInt:
            wrapper.unwrapint(idObj, ScilabIntStackAllocator(pvApiCtx, pos));
            break;
        case RowInt:
            wrapper.unwraprowint(idObj, ScilabIntStackAllocator(pvApiCtx, pos));
            break;
        case MatInt:
            wrapper.unwrapmatint(idObj, ScilabIntStackAllocator(pvApiCtx, pos));
            break;
        case SingleUInt:
            wrapper.unwrapuint(idObj, ScilabUIntStackAllocator(pvApiCtx, pos));
            break;
        case RowUInt:
            wrapper.unwraprowuint(idObj, ScilabUIntStackAllocator(pvApiCtx, pos));
            break;
        case MatUInt:
            wrapper.unwrapmatuint(idObj, ScilabUIntStackAllocator(pvApiCtx, pos));
            break;
        case SingleLong:
            wrapper.unwraplong(idObj, ScilabLongStackAllocator(pvApiCtx, pos));
            break;
        case RowLong:
            wrapper.unwraprowlong(idObj, ScilabLongStackAllocator(pvApiCtx, pos));
            break;
        case MatLong:
            wrapper.unwrapmatlong(idObj, ScilabLongStackAllocator(pvApiCtx, pos));
            break;
        case SingleULong:
            wrapper.unwrapulong(idObj, ScilabULongStackAllocator(pvApiCtx, pos));
            break;
        case RowULong:
            wrapper.unwraprowulong(idObj, ScilabULongStackAllocator(pvApiCtx, pos));
            break;
        case MatULong:
            wrapper.unwrapmatulong(idObj, ScilabULongStackAllocator(pvApiCtx, pos));
            break;
        case SingleFloat:
            wrapper.unwrapfloat(idObj, ScilabFloatStackAllocator(pvApiCtx, pos));
            break;
        case RowFloat:
            wrapper.unwraprowfloat(idObj, ScilabFloatStackAllocator(pvApiCtx, pos));
            break;
        case MatFloat:
            wrapper.unwrapmatfloat(idObj, ScilabFloatStackAllocator(pvApiCtx, pos));
            break;
        case SingleComplex:
            wrapper.unwrapcomplex(idObj, ScilabComplexStackAllocator(pvApiCtx, pos));
            break;
        case RowComplex:
            wrapper.unwraprowcomplex(idObj, ScilabComplexStackAllocator(pvApiCtx, pos));
            break;
        case MatComplex:
            wrapper.unwrapmatcomplex(idObj, ScilabComplexStackAllocator(pvApiCtx, pos));
            break;
        default:
            return false;
    }

    return true;
}
Example #25
0
int ScilabGateway::invoke(char * fname, const int envId, ScilabAbstractInvoker & invoker, void * pvApiCtx)
{
    SciErr err;
    int * tmpvar = 0;
    int * addr = 0;
    int * args = 0;
    int ret = 0;
    int nbArgs = Rhs;

    CheckOutputArgument(pvApiCtx, 1, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper & helper = env.getOptionsHelper();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    tmpvar = new int[Rhs + 1];
    *tmpvar = 0;
    args = new int[Rhs];

    for (int i = 0; i < Rhs; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i + 1, &addr);
        if (err.iErr)
        {
            delete[] args;
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }
        try
        {
            args[i] = ScilabObjects::getArgumentId(addr, tmpvar, false, false, envId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & /*e*/)
        {
            delete[] args;
            delete[] tmpvar;
            throw;
        }

        if (args[i] == VOID_OBJECT)
        {
            nbArgs = 0;
        }
    }

    try
    {
        ret = invoker.invoke(args, nbArgs);
    }
    catch (std::exception & /*e*/)
    {
        delete[] args;
        ScilabObjects::removeTemporaryVars(envId, tmpvar);
        delete[] tmpvar;
        throw;
    }

    delete[] args;
    ScilabObjects::removeTemporaryVars(envId, tmpvar);
    delete[] tmpvar;

    if (ret == VOID_OBJECT)
    {
        PutLhsVar();

        return 0;
    }

    if (helper.getAutoUnwrap())
    {
        if (!ScilabObjects::unwrap(ret, Rhs + 1, envId, pvApiCtx))
        {
            try
            {
                ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + 1, ret, envId, pvApiCtx);
            }
            catch (ScilabAbstractEnvironmentException & /*e*/)
            {
                env.removeobject(ret);
                throw;
            }
        }
        else
        {
            env.removeobject(ret);
        }
    }
    else
    {
        try
        {
            ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + 1, ret, envId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & /*e*/)
        {
            env.removeobject(ret);
            throw;
        }
    }

    LhsVar(1) = Rhs + 1;
    PutLhsVar();

    return 0;
}
Example #26
0
int ScilabGateway::wrap(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * tmpvar = 0;
    int * addr = 0;
    int idObj;

    if (Rhs == 0)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong number of arguments : more than 1 argument expected."));
    }

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccured(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    CheckOutputArgument(pvApiCtx, Rhs, Rhs);

    tmpvar = new int[Rhs + 1];
    *tmpvar = 0;

    for (int i = 1; i < Rhs + 1; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i, &addr);
        if (err.iErr)
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        try
        {
            idObj = ScilabObjects::getArgumentId(addr, tmpvar, false, false, envId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & e)
        {
            delete[] tmpvar;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot wrap argument %d."), i);
        }

        try
        {
            ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + i, idObj, envId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & e)
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            delete[] tmpvar;
            throw;
        }

        LhsVar(i) = Rhs + i;
    }

    PutLhsVar();

    return 0;
}
int ScilabGateway::doubleExclam_invoke(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    int * tmpvar = 0;
    int * args = 0;
    int eId = OptionsHelper::getEnvId();
    int ret = 0;
    int * sret = 0;
    int len = Rhs;
    int nbArgs = len;
    std::vector<int> torem;

    CheckOutputArgument(pvApiCtx, 1, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(eId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper & helper = env.getOptionsHelper();
    ScilabObjects::initialization(env, pvApiCtx);

    if (!OptionsHelper::getCopyOccurred()) // if the function is called outside a method context, then return null
    {
        //TODO: get null for environment
        ScilabObjects::unwrap(0, Rhs + 1, eId, pvApiCtx);

        LhsVar(1) = Rhs + 1;
        PutLhsVar();

        return 0;
    }

    OptionsHelper::setCopyOccurred(false);

    if (!options.getMethodName())
    {
        LhsVar(1) = Rhs + 1;
        PutLhsVar();

        return 0;
    }

    if (Rhs == 1)
    {
        err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (ScilabObjects::isExternalVoid(addr, pvApiCtx))
        {
            len = 0;
        }
    }

    tmpvar = new int[len + 1];
    *tmpvar = 0;
    args = new int[len];

    for (int i = 0; i < len; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i + 1, &addr);
        if (err.iErr)
        {
            ScilabObjects::removeTemporaryVars(eId, tmpvar);
            delete[] tmpvar;
            delete[] args;
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        try
        {
            args[i] = ScilabObjects::getArgumentId(addr, tmpvar, false, false, eId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & e)
        {
            delete[] tmpvar;
            delete[] args;
            throw;
        }
    }

    if (options.getIsNew())
    {
        try
        {
            ret = env.newinstance(options.getObjId(), args, len);
        }
        catch (std::exception & e)
        {
            options.setIsNew(false);
            ScilabObjects::removeTemporaryVars(eId, tmpvar);
            delete[] tmpvar;
            delete[] args;
            throw;
        }

        options.setIsNew(false);
        ScilabObjects::removeTemporaryVars(eId, tmpvar);
        delete[] tmpvar;
        delete[] args;
        options.setMethodName(0);

        if (ret == VOID_OBJECT)
        {
            PutLhsVar();

            return 0;
        }

        try
        {
            ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + 1, ret, eId, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & e)
        {
            env.removeobject(ret);
            throw;
        }

        LhsVar(1) = Rhs + 1;
        PutLhsVar();

        return 0;
    }
    else
    {
        try
        {
            sret = env.invoke(options.getObjId(), options.getMethodName(), args, len);
        }
        catch (std::exception & e)
        {
            ScilabObjects::removeTemporaryVars(eId, tmpvar);
            delete[] tmpvar;
            delete[] args;
            options.setMethodName(0);
            throw;
        }
    }

    ScilabObjects::removeTemporaryVars(eId, tmpvar);
    delete[] tmpvar;
    delete[] args;
    options.setMethodName(0);

    if (!sret || *sret <= 0 || (*sret == 1 && sret[1] == VOID_OBJECT))
    {
        if (sret)
        {
            delete[] sret;
        }

        PutLhsVar();

        return 0;
    }

    if (helper.getAutoUnwrap())
    {
        torem.reserve(*sret);
        for (int i = 1; i <= *sret; i++)
        {
            if (!ScilabObjects::unwrap(sret[i], Rhs + i, eId, pvApiCtx))
            {
                try
                {
                    ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + i, sret[i], eId, pvApiCtx);
                }
                catch (ScilabAbstractEnvironmentException & e)
                {
                    if (!torem.empty())
                    {
                        env.removeobject(&(torem[0]), torem.size());
                    }
                    env.removeobject(sret + 1, *sret);
                    delete[] sret;
                    throw;
                }
            }
            else
            {
                torem.push_back(sret[i]);
            }

            LhsVar(i) = Rhs + i;
        }

        if (!torem.empty())
        {
            env.removeobject(&(torem[0]), torem.size());
        }
    }
    else
    {
        for (int i = 1; i <= *sret; i++)
        {
            try
            {
                ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + i, sret[i], eId, pvApiCtx);
            }
            catch (ScilabAbstractEnvironmentException & e)
            {
                env.removeobject(sret + 1, *sret);
                delete[] sret;
                throw;
            }

            LhsVar(i) = Rhs + i;
        }
    }

    delete[] sret;

    PutLhsVar();

    return 0;
}
Example #28
0
int ScilabObjects::getArgumentId(int * addr, int * tmpvars, const bool isRef, const bool isClass, const int envId, void * pvApiCtx)
{
    SciErr err;
    int typ, row = 0, col = 0, returnId;
    const ScilabAbstractEnvironmentWrapper & wrapper = ScilabEnvironments::getEnvironment(envId).getWrapper();

    err = getVarType(pvApiCtx, addr, &typ);
    if (err.iErr)
    {
        removeTemporaryVars(envId, tmpvars);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (isClass && typ != sci_mlist)
    {
        removeTemporaryVars(envId, tmpvars);
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("External Class expected"));
    }

    switch (typ)
    {
        case sci_matrix :
        {
            double * mat = 0;

            if (isVarComplex(pvApiCtx, addr))
            {
                double * imag = 0;
                err = getComplexMatrixOfDouble(pvApiCtx, addr, &row, &col, &mat, &imag);
                if (err.iErr)
                {
                    removeTemporaryVars(envId, tmpvars);
                    throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                }

                returnId = wrap(row, col, mat, imag, wrapper, isRef);
            }
            else
            {
                err = getMatrixOfDouble(pvApiCtx, addr, &row, &col, &mat);
                if (err.iErr)
                {
                    removeTemporaryVars(envId, tmpvars);
                    throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                }

                returnId = wrap<double>(row, col, mat, wrapper, isRef);
            }

            tmpvars[++tmpvars[0]] = returnId;

            return returnId;
        }
        case sci_ints :
        {
            int prec = 0;
            void * ints = 0;

            err = getMatrixOfIntegerPrecision(pvApiCtx, addr, &prec);
            if (err.iErr)
            {
                removeTemporaryVars(envId, tmpvars);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
            }

            switch (prec)
            {
                case SCI_INT8 :
                    err = getMatrixOfInteger8(pvApiCtx, addr, &row, &col, (char**)(&ints));
                    if (err.iErr)
                    {
                        removeTemporaryVars(envId, tmpvars);
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                    }

                    returnId = wrap<char>(row, col, static_cast<char *>(ints), wrapper, isRef);
                    tmpvars[++tmpvars[0]] = returnId;
                    return returnId;
                case SCI_UINT8 :
                    err = getMatrixOfUnsignedInteger8(pvApiCtx, addr, &row, &col, (unsigned char**)(&ints));
                    if (err.iErr)
                    {
                        removeTemporaryVars(envId, tmpvars);
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                    }

                    returnId = wrap<unsigned char>(row, col, static_cast<unsigned char *>(ints), wrapper, isRef);
                    tmpvars[++tmpvars[0]] = returnId;
                    return returnId;
                case SCI_INT16 :
                    err = getMatrixOfInteger16(pvApiCtx, addr, &row, &col, (short**)(&ints));
                    if (err.iErr)
                    {
                        removeTemporaryVars(envId, tmpvars);
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                    }

                    returnId = wrap<short>(row, col, static_cast<short *>(ints), wrapper, isRef);
                    tmpvars[++tmpvars[0]] = returnId;
                    return returnId;
                case SCI_UINT16 :
                    err = getMatrixOfUnsignedInteger16(pvApiCtx, addr, &row, &col, (unsigned short**)(&ints));
                    if (err.iErr)
                    {
                        removeTemporaryVars(envId, tmpvars);
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                    }

                    returnId = wrap<unsigned short>(row, col, static_cast<unsigned short *>(ints), wrapper, isRef);
                    tmpvars[++tmpvars[0]] = returnId;
                    return returnId;
                case SCI_INT32 :
                    err = getMatrixOfInteger32(pvApiCtx, addr, &row, &col, (int**)(&ints));
                    if (err.iErr)
                    {
                        removeTemporaryVars(envId, tmpvars);
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                    }

                    returnId = wrap<int>(row, col, static_cast<int *>(ints), wrapper, isRef);
                    tmpvars[++tmpvars[0]] = returnId;
                    return returnId;
                case SCI_UINT32 :
                    err = getMatrixOfUnsignedInteger32(pvApiCtx, addr, &row, &col, (unsigned int**)(&ints));
                    if (err.iErr)
                    {
                        removeTemporaryVars(envId, tmpvars);
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                    }

                    returnId = wrap<unsigned int>(row, col, static_cast<unsigned int *>(ints), wrapper, isRef);
                    tmpvars[++tmpvars[0]] = returnId;
                    return returnId;

#ifdef __SCILAB_INT64__
                case SCI_INT64 :
                    err = getMatrixOfInteger64(pvApiCtx, addr, &row, &col, (long long**)(&ints));
                    if (err.iErr)
                    {
                        removeTemporaryVars(envId, tmpvars);
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                    }

                    returnId = wrap<long long>(row, col, static_cast<long long *>(ints), wrapper, isRef);
                    tmpvars[++tmpvars[0]] = returnId;
                    return returnId;
                case SCI_UINT64 :
                    err = getMatrixOfUnsignedInteger64(pvApiCtx, addr, &row, &col, (unsigned long long**)(&ints));
                    if (err.iErr)
                    {
                        removeTemporaryVars(envId, tmpvars);
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                    }

                    returnId = wrap<unsigned long long>(row, col, static_cast<unsigned long long *>(ints), wrapper, isRef);
                    tmpvars[++tmpvars[0]] = returnId;
                    return returnId;
#endif
            }
        }
        case sci_strings :
        {
            char ** matS = NULL;
            if (getAllocatedMatrixOfString(pvApiCtx, addr, &row, &col, &matS))
            {
                removeTemporaryVars(envId, tmpvars);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
            }

            returnId = wrap<char *>(row, col, matS, wrapper, isRef);
            freeAllocatedMatrixOfString(row, col, matS);
            tmpvars[++tmpvars[0]] = returnId;

            return returnId;
        }
        case sci_boolean :
        {
            int * matB;

            err = getMatrixOfBoolean(pvApiCtx, addr, &row, &col, &matB);
            if (err.iErr)
            {
                removeTemporaryVars(envId, tmpvars);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
            }

            returnId = wrapBool(row, col, matB, wrapper, isRef);
            tmpvars[++tmpvars[0]] = returnId;

            return returnId;
        }
        case sci_mlist :
        {
            int * id = 0;
            int type = getMListType(addr, pvApiCtx);
            int eId = getEnvironmentId(addr, pvApiCtx);

            if (eId != envId)
            {
                removeTemporaryVars(envId, tmpvars);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Incompatible External Objects"));
            }

            if (isClass)
            {
                if (type == EXTERNAL_CLASS)
                {
                    err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id);
                    if (err.iErr)
                    {
                        removeTemporaryVars(envId, tmpvars);
                        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                    }
                    return *id;
                }
                else
                {
                    removeTemporaryVars(envId, tmpvars);
                    throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("External Class expected"));
                }
            }

            if (type == EXTERNAL_OBJECT || type == EXTERNAL_CLASS)
            {
                err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id);
                if (err.iErr)
                {
                    removeTemporaryVars(envId, tmpvars);
                    throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
                }
                return *id;
            }
            else if (type == EXTERNAL_VOID)
            {
                return -1;
            }
            else
            {
                removeTemporaryVars(envId, tmpvars);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("External object expected"));
            }

            break;
        }
        default :
        {
            removeTemporaryVars(envId, tmpvars);
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Unable to wrap. Unmanaged datatype ?"));
        }
    }
}
Example #29
0
int ScilabGateway::evalString(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    int row;
    int col;
    char ** code = 0;
    ScilabStringStackAllocator * allocator = 0;

    CheckInputArgument(pvApiCtx, 1, 2);
    CheckOutputArgument(pvApiCtx, 1, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (!isStringType(pvApiCtx, addr))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: string expected."), 1);
    }

    err = getVarDimension(pvApiCtx, addr, &row, &col);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if ((row < 1 || col != 1) && (col < 1 || row != 1))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid dimension for argument #%d: A row or a column expected."), 1);
    }

    if (getAllocatedMatrixOfString(pvApiCtx, addr, &row, &col, &code))
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (Rhs == 2)
    {
        int val;

        err = getVarAddressFromPosition(pvApiCtx, 2, &addr);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (!isBooleanType(pvApiCtx, addr))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A boolean expected."), 2);
        }

        if (!isScalar(pvApiCtx, addr))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A single boolean expected."), 2);
        }

        getScalarBoolean(pvApiCtx, addr, &val);

        if (val)
        {
            allocator = new ScilabStringStackAllocator(pvApiCtx, Rhs + 1);
        }
    }

    try
    {
        env.evalString(const_cast<const char **>(code), row * col, allocator);
    }
    catch (std::exception & /*e*/)
    {
        freeAllocatedMatrixOfString(row, col, code);
        throw;
    }

    if (allocator)
    {
        delete allocator;
        LhsVar(1) = Rhs + 1;
    }
    else
    {
        LhsVar(1) = 0;
    }

    PutLhsVar();

    return 0;
}
Example #30
0
int ScilabGateway::cast(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int tmpvar[2] = {0, 0};
    int * addr = 0;
    int idObj = 0;
    int row = 0, col = 0;
    int * id = 0;
    char * objName = 0;
    int ret = 0;

    CheckInputArgument(pvApiCtx, 2, 2);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    idObj = ScilabObjects::getArgumentId(addr, tmpvar, false, false, envId, pvApiCtx);

    err = getVarAddressFromPosition(pvApiCtx, 2, &addr);
    if (err.iErr)
    {
        throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
    }

    if (ScilabObjects::isExternalClass(addr, pvApiCtx))
    {
        if (ScilabObjects::getEnvironmentId(addr, pvApiCtx) != envId)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Incompatible External Objects"));
        }

        err = getMatrixOfInteger32InList(pvApiCtx, addr, EXTERNAL_OBJ_ID_POSITION, &row, &col, &id);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        try
        {
            ret = env.castwithid(idObj, *id);
        }
        catch (std::exception & /*e*/)
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            throw;
        }
    }
    else
    {
        try
        {
            objName = ScilabObjects::getSingleString(2, pvApiCtx);
        }
        catch (ScilabAbstractEnvironmentException & /*e*/)
        {
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            throw;
        }

        try
        {
            ret = env.cast(idObj, objName);
        }
        catch (std::exception & /*e*/)
        {
            freeAllocatedSingleString(objName);
            ScilabObjects::removeTemporaryVars(envId, tmpvar);
            throw;
        }
        freeAllocatedSingleString(objName);
    }

    ScilabObjects::removeTemporaryVars(envId, tmpvar);

    try
    {
        ScilabObjects::createEnvironmentObjectAtPos(EXTERNAL_OBJECT, Rhs + 1, ret, envId, pvApiCtx);
    }
    catch (ScilabAbstractEnvironmentException & /*e*/)
    {
        env.removeobject(ret);
        throw;
    }

    LhsVar(1) = Rhs + 1;
    PutLhsVar();

    return 0;
}