Example #1
0
void lua_rawseti(lua_State* L, int index, int n)
{

    Value* table = GetValueForIndex(L, index);
    luai_apicheck(L, Value_GetIsTable(table) );    

    Value* value = GetValueForIndex(L, -1);
    Table_SetTable(L, table->table, n, value);
    Pop(L, 1);

}
Example #2
0
int lua_setfenv(lua_State *L, int index)
{
    Value* object = GetValueForIndex(L, index);
    Value* env = GetValueForIndex(L, -1);
    
    luai_apicheck(L, Value_GetIsTable(env) );

    int result = Value_SetEnv(L, object, env->table);

    Pop(L, 1);
    return result;
}
Example #3
0
void lua_rawgeti(lua_State *L, int index, int n)
{

    Value* table = GetValueForIndex(L, index);
    luai_apicheck(L, Value_GetIsTable(table) );    

    const Value* value = Table_GetTable(L, table->table, n);
    if (value == NULL)
    {
        PushNil(L);
    }
    else
    {
        PushValue(L, value);
    }

}
Example #4
0
size_t lua_objlen(lua_State* L, int index)
{
    Value* value = GetValueForIndex(L, index);
    if (Value_GetIsTable(value))
    {
        return Table_GetSize(L, value->table);
    }
    else if (Value_GetIsUserData(value))
    {
        return value->userData->size;
    }
    else if (Value_GetIsString(value) || (Value_GetIsNumber(value) && ToString(L, value)))
    {
        return value->string->length;
    }
    return 0;
}
Example #5
0
int lua_next(lua_State* L, int index)
{

    Value* table = GetValueForIndex(L, index);
    luai_apicheck(L, Value_GetIsTable(table) );
    
    Value* key = GetValueForIndex(L, -1);

    const Value* value = Table_Next(table->table, key);
    if (value == NULL)
    {
        Pop(L, 1);
        return 0;
    }
    PushValue(L, value);
    return 1;

}
Example #6
0
void lua_rawget(lua_State* L, int index)
{

    Value* table = GetValueForIndex( L, index );
    luai_apicheck(L, Value_GetIsTable(table) );

    const Value* key = GetValueForIndex(L, -1);
    const Value* value = Table_GetTable(L, table->table, key);

    if (value != NULL)
    {
        *(L->stackTop - 1) = *value;
    }
    else
    {
        SetNil(L->stackTop - 1);
    }

}
Example #7
0
int lua_setmetatable(lua_State* L, int index)
{

    luai_apicheck(L, GetStackSize(L) >= 1);

    Value* object    = GetValueForIndex(L, index);
    Value* metatable = GetValueForIndex(L, -1);

    Table* table = NULL;
    if (!Value_GetIsNil(metatable))
    {
        luai_apicheck(L, Value_GetIsTable(metatable) );
        table = metatable->table;
    }

    Value_SetMetatable( L, object, table );

    Pop(L, 1);
    return 1;

}
Example #8
0
void lua_replace(lua_State *L, int index)
{
    if (index == LUA_ENVIRONINDEX)
    {
        // Special case for the environment index since we can only assign table
        // values, and the value returned by GetValueForIndex is a copy.
        CallFrame* frame = State_GetCallFrame(L);
        if (frame->function == NULL)
        {
            Vm_Error(L, "no calling environment");
        }
        const Value* src = L->stackTop - 1;
        luai_apicheck(L, Value_GetIsTable(src));
        frame->function->closure->env = src->table;
        --L->stackTop;
    }
    else
    {
        Value* dst = GetValueForIndex(L, index);
        --L->stackTop;
        *dst = *L->stackTop;
    }
}
Example #9
0
Prototype* Function_CreatePrototype(lua_State* L, Function* function, String* source)
{

    int convertedCodeSize = Prototype_GetConvertedCodeSize( function->code, function->codeSize );

    Prototype* prototype = Prototype_Create(
        L,
        function->codeSize,
        convertedCodeSize,
        function->numConstants,
        function->numFunctions,
        function->numUpValues);

    PushPrototype(L, prototype);

    // Store the up values.
    memcpy(prototype->upValue, function->upValue, sizeof(String*) * function->numUpValues);

    // Store the code.
    memcpy(prototype->code, function->code, function->codeSize * sizeof(Instruction));

    // Store the source information.
    prototype->source = source;
    Gc_IncrementReference(&L->gc, prototype, prototype->source);
    memcpy(prototype->sourceLine, function->sourceLine, function->codeSize * sizeof(int));

    // Store the functions.
    for (int i = 0; i < function->numFunctions; ++i)
    {
        prototype->prototype[i] = Function_CreatePrototype(L, function->function[i], source);
        Gc_IncrementReference(&L->gc, prototype, prototype->prototype[i]);
    }

    // Store the constants.

    Value key;
    SetNil(&key);

    Table* constants = function->constants;
    const Value* value;

    while (value = Table_Next(L, constants, &key))
    {
        int i;
        if (!Value_GetIsInteger(value, &i))
        {
            ASSERT(0);
        }
        if (Value_GetIsTable(&key) && key.table == constants)
        {
            // The table itself is used to indicate nil values since nil values
            // cannot be saved in the table.
            SetNil(&prototype->constant[i]);
        }
        else
        {
            prototype->constant[i] = key;
        }
        Gc_IncrementReference(&L->gc, prototype, &prototype->constant[i]);
    }
    
    prototype->varArg       = function->varArg;
    prototype->numParams    = function->numParams;
    prototype->maxStackSize = function->maxStackSize;
    prototype->numUpValues  = function->numUpValues;

    prototype->lineDefined      = 0;
    prototype->lastLineDefined  = 0;

    //PrintFunction(prototype);

    ASSERT( (L->stackTop - 1)->object == prototype );
    Pop(L, 1);

    return prototype;

}