Exemplo n.º 1
0
NODE_IMPLEMENTATION(IStreamType::gets, Pointer)
{
    Process *p = NODE_THREAD.process();
    MuLangContext* context = static_cast<MuLangContext*>(p->context());
    const Class *stype = static_cast<const StringType*>(NODE_THIS.type());

    IStream* stream         = NODE_ARG_OBJECT(0, IStream);
    const StringType::String* s   = NODE_ARG_OBJECT(1, StringType::String);

    char c;
    const String del = s->c_str();
    istream* is = stream->_istream;

    ostringstream ostr;

    while (!is->eof() && !is->fail())
    {
        is->get(c);

        if (del.find(c) != String::npos) break;
        else ostr << char(c);
    };

    NODE_RETURN(context->stringType()->allocate(ostr));
}
Exemplo n.º 2
0
NODE_IMPLEMENTATION(IStreamType::putback, Pointer)
{
    IStream* stream = NODE_ARG_OBJECT(0, IStream);
    char c = NODE_ARG(1, char);
    stream->_istream->putback(c);
    NODE_RETURN(stream);
}
Exemplo n.º 3
0
static NODE_IMPLEMENTATION(resize, Pointer)
{
    MuLangContext* context = static_cast<MuLangContext*>(NODE_THREAD.context());
    const Class*   c       = static_cast<const ImageType*>(NODE_THIS.type());
    ClassInstance* inObj   = NODE_ARG_OBJECT(0, ClassInstance);
    int            width   = NODE_ARG(1, int);
    int            height  = NODE_ARG(2, int);
    ClassInstance* outObj  = makeImage(context, c, width, height);
    ImageStruct*   inIm    = inObj->data<ImageStruct>();
    ImageStruct*   outIm   = outObj->data<ImageStruct>();

    CvMat inMat;
    CvMat outMat;

    cvInitMatHeader(&inMat,
                    inIm->height,
                    inIm->width,
                    CV_32FC(4),
                    inIm->data->data<float>(),
                    0);

    cvInitMatHeader(&outMat,
                    outIm->height,
                    outIm->width,
                    CV_32FC(4),
                    outIm->data->data<float>(),
                    0);

    cvResize(&inMat, &outMat, CV_INTER_AREA);

    NODE_RETURN(outObj);
}
Exemplo n.º 4
0
NODE_IMPLEMENTATION(IStreamType::seek, Pointer)
{
    IStream* stream = NODE_ARG_OBJECT(0, IStream);
    size_t n = NODE_ARG(1, int);
    stream->_istream->seekg(n);
    NODE_RETURN(stream);
}
Exemplo n.º 5
0
NODE_IMPLEMENTATION(IStreamType::read, char)
{
    IStream* stream = NODE_ARG_OBJECT(0, IStream);
    char c;
    stream->_istream->get(c);
    NODE_RETURN(c);
}
Exemplo n.º 6
0
Handle<Value> Geometry::createFromWkb(const Arguments &args)
{
	HandleScope scope;

	std::string wkb_string;
	SpatialReference *srs = NULL;

	Handle<Object> wkb_obj; 
	NODE_ARG_OBJECT(0, "wkb", wkb_obj);
	NODE_ARG_WRAPPED_OPT(1, "srs", SpatialReference, srs);

	std::string obj_type = TOSTR(wkb_obj->GetConstructorName());

	if(obj_type != "Buffer"){
		return NODE_THROW("Argument must be a buffer object");
	}

	unsigned char* data = (unsigned char *) Buffer::Data(wkb_obj);
	size_t length = Buffer::Length(wkb_obj);

	OGRGeometry *geom = NULL;
	OGRSpatialReference *ogr_srs = NULL;
	if (srs) {
		ogr_srs = srs->get();
	}

	OGRErr err = OGRGeometryFactory::createFromWkb(data, ogr_srs, &geom, length);
	if (err) {
		return NODE_THROW_OGRERR(err);
	}

	return scope.Close(Geometry::New(geom, true));
}
Exemplo n.º 7
0
NODE_IMPLEMENTATION(IStreamType::seek2, Pointer)
{
    IStream* stream = NODE_ARG_OBJECT(0, IStream);
    size_t n = NODE_ARG(1, int);
    size_t dir = NODE_ARG(2, int);
    stream->_istream->seekg(n, ios_base::seekdir(dir));
    NODE_RETURN(stream);
}
Exemplo n.º 8
0
NODE_IMPLEMENTATION(IStreamType::readBytes, int)
{
    IStream* stream = NODE_ARG_OBJECT(0, IStream);
    DynamicArray* array = NODE_ARG_OBJECT(1, DynamicArray);
    size_t n = NODE_ARG(2, int);
    size_t size = array->size();    // we know its bytes
    array->resize(size + n);
    size_t nread = 0;
#if COMPILER == GCC2_95
    stream->_istream->read(array->data<char>() + size, n);
    nread = n;  // bogus return value here when using 2.95!
#else
    nread = stream->_istream->readsome(array->data<char>() + size, n);
#endif
    array->resize(size + nread);
    NODE_RETURN(nread);
}
Exemplo n.º 9
0
NODE_IMPLEMENTATION(DynamicCast::node, Pointer)
{
    const Symbol* sym = NODE_THIS.argNode(0)->symbol();

    if (const Class* c0 = dynamic_cast<const Class*>(sym))
    {
	if (ClassInstance* o = NODE_ARG_OBJECT(1, ClassInstance))
	{
	    if (const Class* c1 = dynamic_cast<const Class*>(o->type()))
	    {
                if (ClassInstance* dobj = c1->dynamicCast(o, c0, true))
                {
                    NODE_RETURN(dobj);
                }
		//if (c1->isA(c0)) NODE_RETURN((Pointer)o);
	    }
	}
	else
	{
	    //
	    //	Let nil pass through
	    //

	    NODE_RETURN((Pointer)o);
	}
    }
    else if (const Interface* i = dynamic_cast<const Interface*>(sym))
    {
	if (Object* o = reinterpret_cast<Object*>(NODE_ARG(1, Pointer)))
	{
	    if (const Class* c1 = dynamic_cast<const Class*>(o->type()))
	    {
		if (c1->implementation(i))
		{
		    NODE_RETURN((Pointer)o);
		}
	    }
	}
	else
	{
	    //
	    //	Can't attempt to cast nil to an interface. Let it throw.
	    //

            //  ALLOWING --NEEDS FIXING

            NODE_RETURN(Pointer(0));
	}
    }

    throw BadDynamicCastException(NODE_THREAD);
}
Exemplo n.º 10
0
NODE_IMPLEMENTATION(DynamicPartialEvaluate::node, Pointer)
{
    typedef FunctionSpecializer Generator;

    FunctionObject* f = NODE_ARG_OBJECT(1, FunctionObject);
    Generator::ArgumentVector args(f->function()->numArgs() +
                                   f->function()->numFreeVariables());
    Generator::ArgumentMask   mask(args.size());
    Process* p = NODE_THREAD.process();
    Context* c = p->context();

    for (int i=0; i < args.size(); i++)
    {
        mask[i] = NODE_THIS.argNode(i+2)->symbol() != c->noop();

        if (mask[i])
        {
            args[i] = NODE_ANY_TYPE_ARG(i+2);
        }
    }

    try
    {
        Generator evaluator(f->function(), p, &NODE_THREAD);
        evaluator.partiallyEvaluate(args, mask);

        const FunctionType* rt = evaluator.result()->type();
        assert(rt == NODE_THIS.argNode(0)->type());
        FunctionObject* o = new FunctionObject(rt);
        o->setFunction(evaluator.result());
        NODE_RETURN(Pointer(o));
    }
    catch (Exception& e)
    {
        ProgramException exc(NODE_THREAD);
        exc.message() = e.message();
        exc.message() += " during partial evaluation of ";
        exc.message() += f->function()->name().c_str();
        throw exc;
    }
}
Exemplo n.º 11
0
NODE_IMPLEMENTATION(Curry::node, Pointer)
{
    Process*                 p = NODE_THREAD.process();
    Context*                 c = p->context();
    FunctionObject*          o = NODE_ARG_OBJECT(1, FunctionObject);
    bool                     d = NODE_ARG(2, bool);
    const Function*          F = o->function();
    Function::ArgumentVector args(F->numArgs() + F->numFreeVariables());
    vector<bool>             mask(args.size());

    for (int i=0, s=args.size(); i < s; i++)
    {
        mask[i] = NODE_THIS.argNode(i+3)->symbol() != c->noop();

        if (mask[i])
        {
            args[i] = NODE_ANY_TYPE_ARG(i+3);
        }
    }

    NODE_RETURN(Pointer(evaluate(NODE_THREAD, o, args, mask, d)));
}
Exemplo n.º 12
0
NODE_IMPLEMENTATION(StreamType::setstate, void)
{
    Stream *s = NODE_ARG_OBJECT(0, Stream);
    int f = NODE_ARG(1, int);
    s->_ios->setstate((ios::iostate)f);
}
Exemplo n.º 13
0
NODE_IMPLEMENTATION(StreamType::rdstate, int)
{
    Stream *s = NODE_ARG_OBJECT(0, Stream);
    NODE_RETURN(int(s->_ios->rdstate()));
}
Exemplo n.º 14
0
NODE_IMPLEMENTATION(StreamType::clear, void)
{
    Stream *s = NODE_ARG_OBJECT(0, Stream);
    s->_ios->clear();
}
Exemplo n.º 15
0
NODE_IMPLEMENTATION(StreamType::clearflag, void)
{
    Stream *s = NODE_ARG_OBJECT(0, Stream);
    int f = NODE_ARG(1, int);
    s->_ios->clear((ios::iostate)f);
}
Exemplo n.º 16
0
NODE_IMPLEMENTATION(StreamType::print, void)
{
    Stream *i = NODE_ARG_OBJECT(0, Stream);
    i->type()->outputValue(cout, Value(i));
}
Exemplo n.º 17
0
NODE_IMPLEMENTATION(StreamType::toBool, bool)
{
    Stream *s = NODE_ARG_OBJECT(0, Stream);
    NODE_RETURN(!!(*s->_ios));
}
Exemplo n.º 18
0
NODE_IMPLEMENTATION(IStreamType::in_avail, int)
{
    IStream* stream = NODE_ARG_OBJECT(0, IStream);
    NODE_RETURN(stream->_istream->rdbuf()->in_avail());
}
Exemplo n.º 19
0
NODE_IMPLEMENTATION(IStreamType::tell, int)
{
    IStream* stream = NODE_ARG_OBJECT(0, IStream);
    NODE_RETURN(stream->_istream->tellg());
}
Exemplo n.º 20
0
NODE_IMPLEMENTATION(IStreamType::count, int)
{
    IStream* stream = NODE_ARG_OBJECT(0, IStream);
    NODE_RETURN(stream->_istream->gcount());
}
Exemplo n.º 21
0
NODE_IMPLEMENTATION(IStreamType::unget, Pointer)
{
    IStream* stream = NODE_ARG_OBJECT(0, IStream);
    stream->_istream->unget();
    NODE_RETURN(stream);
}
Exemplo n.º 22
0
NODE_IMPLEMENTATION(DynamicPartialApplication::node, Pointer)
{
    //
    //  Never do partial application on the result of a lambda
    //  expression (its too difficult to archive). Instead do
    //  partial evaluation. The good side is that there will never
    //  be more than one level of indirection in multiple-curried
    //  lambda expressions. the bad side is that there will be
    //  more overhead upfront.
    //

    Process* p = NODE_THREAD.process();
    Context* c = p->context();
    FunctionObject* f = NODE_ARG_OBJECT(1, FunctionObject);
    bool apply = f->function()->isLambda();

    try
    {
        if (apply)
        {
            typedef PartialApplicator Generator;
            Generator::ArgumentVector args(f->function()->numArgs() +
                                           f->function()->numFreeVariables());
            Generator::ArgumentMask   mask(args.size());

            for (int i=0; i < args.size(); i++)
            {
                mask[i] = NODE_THIS.argNode(i+2)->symbol() != c->noop();
                if (mask[i])
                {
                    args[i] = NODE_ANY_TYPE_ARG(i+2);
                }
            }

            Generator evaluator(f->function(), p, &NODE_THREAD, args, mask);

            const FunctionType* rt = evaluator.result()->type();
            assert(rt == NODE_THIS.argNode(0)->type());
            FunctionObject* o = new FunctionObject(rt);
            o->setDependent(f);
            o->setFunction(evaluator.result());
            NODE_RETURN(Pointer(o));
        }
        else
        {
            typedef FunctionSpecializer Generator;
            Generator::ArgumentVector args(f->function()->numArgs() +
                                           f->function()->numFreeVariables());
            Generator::ArgumentMask   mask(args.size());

            for (int i=0; i < args.size(); i++)
            {
                mask[i] = NODE_THIS.argNode(i+2)->symbol() != c->noop();

                if (mask[i])
                {
                    args[i] = NODE_ANY_TYPE_ARG(i+2);
                }
            }

            Generator evaluator(f->function(), p, &NODE_THREAD);
            evaluator.partiallyEvaluate(args, mask);

            const FunctionType* rt = evaluator.result()->type();
            assert(rt == NODE_THIS.argNode(0)->type());
            FunctionObject* o = new FunctionObject(rt);
            o->setFunction(evaluator.result());
            NODE_RETURN(Pointer(o));
        }
    }
    catch (Exception& e)
    {
        ProgramException exc(NODE_THREAD);
        exc.message() = e.message();
        exc.message() += " during partial ";
        exc.message() += (apply ? "application" : "evaluation");
        exc.message() += " of ";
        exc.message() += f->function()->name().c_str();
        throw exc;
    }
}