Esempio n. 1
0
luna::Closure * RandomClosure()
{
    if (g_globalFunction.empty())
        g_globalFunction.push_back(RandomFunction());

    auto index = RandomNum(g_globalFunction.size());
    auto proto = g_globalFunction[index];

    auto c = g_gc.NewClosure();
    c->SetPrototype(proto);
    return c;
}
Esempio n. 2
0
void NewObjInGlobal()
{
    int percent = RandomRange(1, 100);
    if (percent <= 20)
    {
        g_globalTable.push_back(g_gc.NewTable(luna::GCGen2));
    }
    else if (percent <= 50)
    {
        g_globalString.push_back(g_gc.NewString(luna::GCGen2));
    }
    else if (percent <= 60)
    {
        if (!g_globalFunction.empty())
        {
            auto index = RandomNum(g_globalFunction.size());
            auto proto = g_globalFunction[index];
            auto c = g_gc.NewClosure(luna::GCGen1);
            c->SetPrototype(proto);
            g_globalClosure.push_back(c);
        }
    }
}
Esempio n. 3
0
    void CodeGenerateVisitor::Visit(Chunk *chunk, void *data)
    {
        // Generate function
        auto func = state_->NewFunction();
        func->SetBaseInfo(chunk->module_, 0);
        func->SetSuperior(func_);
        func_ = func;

        func_state_ = gen_state_.PushFunctionState();

        chunk->block_->Accept(this, data);

        // Generate closure
        auto cl = state_->NewClosure();
        cl->SetPrototype(func);
        // Add Env as closure upvalue
        cl->AddUpvalue(state_->GetGlobal(), Upvalue::Stack);

        // Add closure to stack
        state_->stack_.top_->closure_ = cl;
        state_->stack_.top_->type_ = ValueT_Closure;
        state_->stack_.top_++;
    }